#!/bin/ksh93 # # media_update # # Setup/teardown a service that refreshes media servers when theor directories # are updated. # # Constants # Path to the DTrace script DIRTRAP_PATH=/lib/svc/method/dirtrap.d # We want to watch two media directories and restart two associated daemons. INSTANCES="/export/zones/constant/root/export/home/constant/PowerBookHome/Music/iTunes:mt-daapd /krongi/stuff/media:mediatomb" # Set up a temporary file trap '${TMPFILE:+rm ${TMPFILE}}' EXIT TMPFILE=$(mktemp /tmp/$0.temp.XXXXXX) # Read in the first argument. CMD=$1 # Install everything and enable the media update service function enable_media_update { # Write the D script into a file. cat >$DIRTRAP_PATH <<'EOF' #!/usr/sbin/dtrace -wqs /* * dirtrap.d * * Detect when new files have been written to a given directory. * Start a command when this happens. */ BEGIN /* Initialize stuff. */ { dir = $1; dirlen = strlen(dir); flag = 0; } syscall::open:return, syscall::open64:return / !flag && /* Exit if we've already found a hit. */ fds[arg0].fi_oflags & (O_WRONLY | O_RDWR) && /* Check if opened for writes */ substr(fds[arg0].fi_pathname, 0, dirlen) == dir /* Check directory */ / { flag = 1; } tick-60s /flag/ /* Periodic check if we need to do something. */ { system($2); flag = 0; } EOF chown root:bin $DIRTRAP_PATH chmod 755 $DIRTRAP_PATH # Import the service manifest. cat >$TMPFILE <<'EOF' EOF svccfg import $TMPFILE # Configure and enable all instances of the manifest for instance in $INSTANCES; do directory=$(echo $instance | cut -d: -f1) daemon=$(echo $instance | cut -d: -f2) svccfg -s media-update add $daemon svccfg -s media-update:$daemon addpg script application svccfg -s media-update:$daemon \ "setprop script/directory = astring: $directory" svccfg -s media-update:$daemon \ "setprop script/command = astring: \"/bin/pkill -HUP $daemon\"" svcadm enable media-update:$daemon done } # Disable and de-install everything. function disable_media_update { # Disable and delete SMF services for instance in $INSTANCES; do daemon=$(echo $instance | cut -d: -f2) svcadm disable -s media-update:$daemon svccfg delete media-update:$daemon done svccfg delete media-update # Delete the DTrace script. /bin/rm -f $DIRTRAP_PATH } # Show a help message. function show_help { echo "Usage: $1 command" echo "Supported commands are:" echo " on" echo " off" } # # Main program. # case $CMD in "on" ) enable_media_update ;; "off" ) disable_media_update ;; * ) show_help $0 ;; esac exit 0