packages: rc-scripts-user/userscripts (NEW) - NEW: runs user-devined script...
sparky
sparky at pld-linux.org
Mon Jan 31 02:21:57 CET 2011
Author: sparky Date: Mon Jan 31 01:21:57 2011 GMT
Module: packages Tag: HEAD
---- Log message:
- NEW: runs user-devined scripts on system startup
---- Files affected:
packages/rc-scripts-user:
userscripts (NONE -> 1.1) (NEW)
---- Diffs:
================================================================
Index: packages/rc-scripts-user/userscripts
diff -u /dev/null packages/rc-scripts-user/userscripts:1.1
--- /dev/null Mon Jan 31 02:21:57 2011
+++ packages/rc-scripts-user/userscripts Mon Jan 31 02:21:52 2011
@@ -0,0 +1,155 @@
+#!/bin/sh
+#
+# userscripts: Executes user-defined events.
+#
+# Version: $Revision$
+#
+# chkconfig: 345 99 01
+# description: Executes $HOME/.config/init.d/* start on startup,
+# and $HOME/.config/init.d/* stop on shutdown.
+
+. /etc/rc.d/init.d/functions
+
+ALLOWED=""
+BANNED=""
+STOP_WAIT_TIME=5
+NICE=15
+
+# subdirectory with user scripts
+scripts_dir=".config/init.d"
+
+if [ -f /etc/sysconfig/userscripts ]; then
+ . /etc/sysconfig/userscripts
+fi
+
+VALID_SHELLS=$(< /etc/shells)
+
+is_on_list()
+{
+ local word=$1
+ local list=$2
+
+ local IFS
+ unset IFS
+ for tword in $list; do
+ [ "$tword" = "$word" ] && return 0
+ done
+ return 1
+}
+
+is_allowed()
+{
+ local user=$1
+ local shell=$2
+
+ is_on_list "$shell" $VALID_SHELLS || return 1
+ if [ -n "$ALLOWED" ]; then
+ is_on_list "$user" $ALLOWED || return 1
+ fi
+ if [ -n "$BANNED" ]; then
+ is_on_list "$user" $BANNED && return 1
+ fi
+
+ return 0
+}
+
+run_scripts()
+{
+ local action="$1"
+
+ SCRIPTS_RUN=0
+ local IFS=":"
+ while read user pass uid gid name home shell; do
+ [ -d "$home/$scripts_dir" ] || continue
+
+ if ! is_allowed "$user" "$shell"; then
+ nls "User %s is not allowed to run scripts" "$user"
+ continue
+ fi
+
+ for script in "$home/$scripts_dir"/*; do
+
+ # skip backups
+ [[ $script == *~ ]] && continue
+ [[ $script == *.bak ]] && continue
+
+ show "Running %s %s as %s" "$script" "$action" "$user"
+ busy
+
+ if [ ! -x "$script" ]; then
+ fail
+ continue
+ fi
+
+ nice -n "$NICE" su - "$user" "$script" "$action" > /dev/null 2>&1 &
+ ok
+
+ : $((SCRIPTS_RUN++))
+ done
+ done < /etc/passwd
+}
+
+start()
+{
+ if [ -f /var/lock/subsys/userscripts ]; then
+ msg_already_running "User scripts"
+ return 1
+ fi
+
+ msg_starting "User scripts"; ok
+
+ run_scripts start
+
+ touch /var/lock/subsys/userscripts
+}
+
+stop()
+{
+ if [ ! -f /var/lock/subsys/userscripts ]; then
+ msg_not_running "User scripts"
+ return 2
+ fi
+
+ run_scripts stop
+
+ msg_stopping "User scripts"; busy
+
+ # give them some time to stop, but don't wait for finish
+ [ "$SCRIPTS_RUN" -gt 0 ] && sleep $STOP_WAIT_TIME
+ ok
+
+ rm -f /var/lock/subsys/userscripts
+}
+
+status()
+{
+ if [ -f /var/lock/subsys/userscripts ]; then
+ nls "User scripts should be running";
+ else
+ nls "User scripts should be stopped";
+ fi
+}
+
+RETVAL=0
+# See how we were called.
+case "$1" in
+ start)
+ start
+ ;;
+ stop)
+ stop
+ ;;
+ status)
+ status
+ ;;
+ restart|force-reload)
+ stop
+ start
+ exit $?
+ ;;
+ *)
+ msg_usage "$0 {start|stop|restart|force-reload|status}"
+ exit 3
+esac
+
+exit $RETVAL
================================================================
More information about the pld-cvs-commit
mailing list