]> git.pld-linux.org Git - projects/rc-scripts.git/blobdiff - lib/functions
daemon ssd mode: always setsid, so programs that properly don't daemonize get detache...
[projects/rc-scripts.git] / lib / functions
index e90ae013adb004e1f01159b0f337df62f565b915..aee3cc1fff66dddf06e08dbcd3f12c5690132895 100644 (file)
@@ -2,7 +2,6 @@
 # functions    This file contains functions to be used by most or all
 #              shell scripts in the /etc/rc.d/init.d directory.
 #
-# $Id$
 #
 # Author:      Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
 # Hacked by:   Greg Galloway and Marc Ewing
@@ -494,14 +493,23 @@ checkpid() {
 # (note: some processes like named are chrooted but run outside chroot)
 # - do nothing inside vserver
 filter_chroot() {
+       # filter by pid namespace if such dir exists for current process
+       # we do filter in containers as stacked containers are possible with LXC
+       if [ -d /proc/$$/ns ]; then
+               local pids
+               pids=$(filter_ns "$@") && set -- "$pids"
+       fi
+
        if is_yes "$VSERVER"; then
                echo $@
                return
        fi
+
        if [ $# -lt 1 -o ! -d /proc/1 ]; then
                echo $@
                return
        fi
+
        local root_dir good_pids="" good_add_pid
        for root_pid in $@; do
                root_dir=$(resolvesymlink /proc/${root_pid}/root)
@@ -520,6 +528,30 @@ filter_chroot() {
        echo $good_pids
 }
 
+# similar to filter_chroot, but filter based on /proc/PID/ns/pid value
+filter_ns() {
+       local cur_ns=$(resolvesymlink /proc/$$/ns/pid)
+       [ "$cur_ns" ] || return 1
+
+       local pid ns pids=""
+       # add pids if it matches current pid namespace
+       # we should add pids what do not exist (dead processes),
+       # but not add pids whose namespace does not match
+       # (processes belonging to different NS do exist in /proc)
+       for pid in "$@"; do
+               if [ ! -d /proc/$pid ]; then
+                       pids="$pids $pid"
+                       continue
+               fi
+               ns=$(resolvesymlink /proc/$pid/ns/pid)
+               if [ "$ns" = "$cur_ns" ]; then
+                       pids="$pids $pid"
+               fi
+       done
+       echo $pids
+       return 0
+}
+
 # Usage:
 # run_cmd Message command_to_run
 # run_cmd -a Message command_to_run
@@ -694,13 +726,14 @@ daemon() {
                if [ "$closefds" = 1 ]; then
                        exec 1>&-
                        exec 2>&-
-                       exec 0>&-
+                       exec 0<&-
                elif [ "$redirfds" = 1 ]; then
                        exec 1>/dev/null
                        exec 2>/dev/null
-                       exec 0>/dev/null
+                       exec 0</dev/null
                else
                        exec 2>&1
+                       exec 0</dev/null
                fi
 
                if is_no "$RC_LOGGING"; then
@@ -717,6 +750,14 @@ daemon() {
                                done
                                IFS=$o
                        fi
+                       set -- "$prog" "$@"
+
+                       # use setsid to detach from terminal,
+                       # needs pidfile or ssd would check setsid program instead of real program
+                       if [ "$pidfile" ]; then
+                               set -- /usr/bin/setsid "$@"
+                       fi
+
                        /sbin/start-stop-daemon -q --start \
                                --nicelevel $nice \
                                ${pidfile:+--pidfile $pidfile} \
@@ -726,8 +767,8 @@ daemon() {
                                ${fork:+--background} \
                                ${waitname:+--name $waitname} \
                                ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
-                               --exec "$prog" \
-                               -- ${1:+"$@"}
+                               --exec "$1" \
+                               -- "$@"
                else
                        nice -n $nice initlog -c "$prog" 2>&1 </dev/null
                fi
@@ -933,7 +974,7 @@ pidofproc() {
 
        # Next try "pidof"
        [ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
-       pid=$(filter_chroot "$pid")
+       pid=$(filter_chroot $pid)
        echo $pid
 }
 
@@ -965,7 +1006,7 @@ status() {
        else
                pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
        fi
-       pid=$(filter_chroot "$pid")
+       pid=$(filter_chroot $pid)
 
        if [ "$pid" ]; then
                cpuset_msg="..."
@@ -984,7 +1025,7 @@ status() {
        local base=${daemon##*/}
        if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
                read pid < /var/run/${base}.pid
-               pid=$(filter_chroot "$pid")
+               pid=$(filter_chroot $pid)
                if [ "$pid" ]; then
                        nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
                        return 1
This page took 0.030229 seconds and 4 git commands to generate.