#!/bin/dash
#
# Unmount the libudev-compat eventfs serial queue.
# Called from vdev SysV init on system shutdown only (runlevels 0/6), not on
# "vdev stop" or "vdev restart".
#
# Usage: vdev-eventfs-umount.sh [mountpoint]
#   mountpoint defaults to /dev

VDEV_MOUNTPOINT="${1:-/dev}"
VDEV_SERIAL="$VDEV_MOUNTPOINT/metadata/udev/events/serial"
VDEV_EVENTFS_PERMS_STAMP="/run/eventfs/perms-set"

if /bin/mountpoint -q "$VDEV_SERIAL" 2>/dev/null; then
   if command -v fusermount >/dev/null 2>&1; then
      fusermount -u "$VDEV_SERIAL" 2>/dev/null ||
         fusermount -uz "$VDEV_SERIAL" 2>/dev/null || true
   else
      umount -l "$VDEV_SERIAL" 2>/dev/null || true
   fi
fi

rm -f "$VDEV_EVENTFS_PERMS_STAMP" 2>/dev/null || true
