#!/bin/sh -Cefu # # Copyright (c) 2014 Sulev-Madis Silber # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # set -Cefu readonly script_basename="${0##*/}" readonly arg="${1:-}" readonly procname=^poe-daemon readonly script=poe-daemon.pl readonly pid_file=/var/run/poe-daemon.pid source="$arg" cmdprefix='rtprio 0' runasuser=nobody fg= stdout= syslog=1 from_delay_start= ciphers= [ "`sysctl -n security.jail.jailed`" = 1 ] && cmdprefix= getent passwd poe > /dev/null 2>&1 && runasuser=poe log() { [ "$stdout" ] && echo "$script_basename: $1" [ "$syslog" ] && logger -i -t "$script_basename" "$1" } wait_and_write_pid() { local pid while true do sleep 1 pid="`pgrep -f \"$procname\"`" if [ "$pid" ] then log "source = '$source', write pid '$pid' to file '$pid_file'" echo "$pid" > "$pid_file" break fi done } remove_pid_file() { if [ "$fg" ] then if [ -e "$pid_file" ] then log "source = '$source', remove pid file '$pid_file'" rm "$pid_file" fi fi } case "$arg" in ''|bg) source=command-line-bg ;; fg) fg=1 stdout=1 source=command-line-fg ;; cron*|rcngstart) from_delay_start=1 if `pgrep -q ntpd` then i=0 while [ "$i" -lt 60 ] do i="$(($i + 1))" ntpq -pn 2> /dev/null | cut -c 1 | fgrep -q '*' \ && log "source = '$source', tick = '#$i', ntp sync" \ && break sleep 1 done unset i fi ;; kill|stop|rcngstop) case "$arg" in rcngstop) ;; *) stdout=1 ;; esac log stop pkill -lf "$procname" | cut -d ' ' -f 3 | xargs -n 1 pwait -v exit ;; init|start) stdout=1 log start ;; syslog-watch-ntp-time-reset.sh) source=syslog-ntpd-time-reset ;; ciphers|cipher|cip|ssl) fg=1 stdout=1 ciphers=1 source=command-line-fg ;; esac if [ "$ciphers" ] then cd "${0%/*}" $cmdprefix su -fm "$runasuser" -c "./$script ciphers" exit fi if [ ! "$from_delay_start" ] then delay_start_procname_match="`echo \"$script_basename\" | sed 's|\.|\\\.|g'`.*(cron|rcngstart)" pkill -f "$delay_start_procname_match" \ && log "source = '$source', killed process(es) matching '$delay_start_procname_match'" unset delay_start_procname_match fi if `pgrep -qf "$procname"` then log "source = '$source', '$procname' already running" exit fi cd "${0%/*}" pkill -9 -f POE::Component::EasyDBI::SubProcess \ && log "source = '$source', killed POE::Component::EasyDBI::SubProcess" if [ "$fg" ] then log "source = '$source', run fg" trap remove_pid_file `trap -l` remove_pid_file wait_and_write_pid & $cmdprefix su -fm "$runasuser" -c "./$script" remove_pid_file else log "source = '$source', run bg" $cmdprefix daemon -f -u "$runasuser" -p "$pid_file" "./$script" quiet fi