Hi Guillem,

On Sat, May 16, 2026 at 04:36:17AM +0200, Guillem Jover wrote:
> On Thu, 2026-05-14 at 00:38:28 +0100, Andrew Bower wrote:
> > On Thu, May 07, 2026 at 08:53:34PM +0200, Guillem Jover wrote:
> > > I just checked now and it was really trivial. I'm undecided whether to
> > > go with a new --print-pid command or an option to pass along the other
> > > commands to print the PIDs matche. I'm attaching both patches, and will
> > > be mulling over what feels like the better interface, before merging it
> > > and after adding proper man page updates, for the next dpkg release.
> 
> > Thank you so much for knocking up these patches!
> > 
> > I literally just tried the first one, the new command, along with a
> > pidof() emulation function in /lib/lsb/init-functions, as a transparent
> > solution for all existing initscripts and it appeared to work very well!
> > I see the MBF just went through so we can't refer to this approach in
> > guidance but I think we might have a good way through here with this
> > enhancement.
> 
> I've been thinking, and the new command makes most sense to me as it
> has clearer semantics, so went with that but changed the command name
> to be the obvious --pidof. :D Attached revised patch with
> documentation now, which I've queued for my next push.

Thank you! :-D

I agree this version is more intuitive but there was some good
conceptual sense in the 'option' version and in the '--print-pid' name,
namely that we confirm we aren't, and don't in full, emulate pidof,
but providing access to s-s-d's own determination. Anyway, I think it's
not worth expending any thought on this - the solution is great!

I have tested your latest patch and attach a patch to src:sysvinit that
uses this command to emulate pidof for initscripts that call pidof
either directly, or indirectly through pidofproc().

Again, thanks for helping out with this capability - it's really nice to
be able to solve this transparently without heavyweight extra
dependencies. I hope that I and others, over time, can contribute
improvements to specific initscripts that call s-s-d in more idiomatic
ways and, preferrably, find that in a simplified form they don't need to
do this at all!

Andrew
>From 84e48d318888f3c893fb0f4a6a2ccf99d1e2749f Mon Sep 17 00:00:00 2001
From: Andrew Bower <[email protected]>
Date: Sat, 16 May 2026 19:14:49 +0100
Subject: [PATCH] init-functions: emulate pidof when absent

---
 debian/control                     |  2 +
 debian/src/lsb-base/init-functions | 62 +++++++++++++++++++++++++++++-
 2 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/debian/control b/debian/control
index 1cb434fa..b0778db6 100644
--- a/debian/control
+++ b/debian/control
@@ -74,6 +74,8 @@ Depends:
 Conflicts: lsb-base (<< 11.3~)
 Provides: lsb-base (= 11.1.0)
 Replaces: lsb-base
+Breaks:
+ dpkg (<< 1.23.8),
 Description: System-V-like utilities
  This package contains the important System-V-like utilities.
  .
diff --git a/debian/src/lsb-base/init-functions b/debian/src/lsb-base/init-functions
index 14acc335..63f2aea4 100644
--- a/debian/src/lsb-base/init-functions
+++ b/debian/src/lsb-base/init-functions
@@ -61,6 +61,64 @@ start_daemon () {
     fi
 }
 
+_lsb_filter_out () {
+    local exclude
+    exclude="$1"
+    shift
+    for word in "$@"; do
+        [ "$word" = "$exclude" ] || echo "$word"
+    done
+}
+
+_lsb_filter_process_root () {
+    local ours
+    ours=$(readlink /proc/self/root)
+    for word in "$@"; do
+        ! x=$(readlink "/proc/$word/root" 2> /dev/null) || [ "$x" = "$ours" ] && echo "$word"
+    done
+}
+
+_lsb_head () {
+    echo "$1"
+}
+
+_lsb_emulate_pidof () {
+    local omit s c x q pids matcher
+    omit=; s=; c=; x=; q=
+    OPTIND=1
+    while getopts scqxo: opt ; do
+      case "$opt" in
+        o) omit="$omit $OPTARG";;
+        c) c=$opt;;
+        s) s=$opt;;
+        x) x=$opt;;
+        q) q=$opt;;
+        *) return 4;;
+      esac
+    done
+    shift $((OPTIND - 1))
+    pids=$(for prog in "$@"; do
+          matcher="--name"
+          [ -z "$x" ] && [ "${prog##/}" != "$prog" ] && matcher="--exec"
+          start-stop-daemon --pidof $matcher "$prog"
+        done)
+    for o in $omit; do
+        pids=$(_lsb_filter_out "$o" $pids)
+    done
+    [ -z "$c" ] || pids=$(_lsb_filter_process_root $pids)
+    [ -z "$s" ] || pids=$(_lsb_head $pids)
+    [ -n "$q" ] || [ -z "$pids" ] || echo $pids
+    [ -n "$pids" ]
+}
+
+pidof () {
+    if [ ! -x /bin/pidof ] && [ -x /sbin/start-stop-daemon ]; then
+        _lsb_emulate_pidof "$@"
+    else
+        /bin/pidof "$@"
+    fi
+}
+
 pidofproc () {
     local pidfile base status specified pid OPTIND
     pidfile=
@@ -105,9 +163,9 @@ pidofproc () {
       fi
      else
        # pid file doesn't exist, try to find the pid nevertheless
-       if [ -x /bin/pidof ] && [ ! "$specified" ]; then
+       if [ ! "$specified" ]; then
          status="0"
-         /bin/pidof -c -o %PPID -x $1 || status="$?"
+         pidof -c -o %PPID -x $1 || status="$?"
          if [ "$status" = 1 ]; then
              return 3 # program is not running
          fi
-- 
2.53.0

Reply via email to