Hi!

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.

Thanks,
Guillem
From bf2baa0e6f3b93b3488f1a1f86e4a40c13369229 Mon Sep 17 00:00:00 2001
From: Guillem Jover <[email protected]>
Date: Thu, 7 May 2026 20:50:07 +0200
Subject: [PATCH] s-s-d: Add new --pidof command to print the PIDs found

This is just like --status, except that any PID that gets matched gets
printed as well. This new command can be used to replace existing uses
of the pidof(8) tool, when only essential tools are available.

Ref: #810018
---
 man/start-stop-daemon.pod | 12 ++++++++++--
 utils/start-stop-daemon.c | 20 +++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/man/start-stop-daemon.pod b/man/start-stop-daemon.pod
index 93e0dc876..7fe77349c 100644
--- a/man/start-stop-daemon.pod
+++ b/man/start-stop-daemon.pod
@@ -109,6 +109,14 @@ will check that the process(es) have terminated.
 Check for the existence of a specified process, and returns an exit status
 code, according to the LSB Init Script Actions (since version 1.16.1).
 
+=item B<--pidof>
+
+Check for the existence of a specified process,
+print its PID,
+and returns an exist status just like B<--status>.
+
+Supported since version 1.23.8.
+
 =item B<-H>, B<--help>
 
 Show usage information and exit.
@@ -484,8 +492,8 @@ Any other error.
 
 =back
 
-When using the B<--status> command, the following status codes are
-returned:
+When using the B<--status> or B<--pidof> commands,
+the following status codes are returned:
 
 =over
 
diff --git a/utils/start-stop-daemon.c b/utils/start-stop-daemon.c
index 84baa120d..8a45eda2f 100644
--- a/utils/start-stop-daemon.c
+++ b/utils/start-stop-daemon.c
@@ -215,6 +215,7 @@ enum action_code {
 	ACTION_START,
 	ACTION_STOP,
 	ACTION_STATUS,
+	ACTION_PIDOF,
 };
 
 enum LIBCOMPAT_ATTR_ENUM_FLAGS match_code {
@@ -370,7 +371,7 @@ fatalv(int errno_fatal, const char *format, va_list args)
 	else
 		fprintf(stderr, "\n");
 
-	if (action == ACTION_STATUS)
+	if (action == ACTION_STATUS || action == ACTION_PIDOF)
 		exit(STATUS_UNKNOWN);
 	else
 		exit(2);
@@ -411,7 +412,7 @@ bug(const char *file, int line, const char *func, const char *format, ...)
 	vfprintf(stderr, format, arglist);
 	va_end(arglist);
 
-	if (action == ACTION_STATUS)
+	if (action == ACTION_STATUS || action == ACTION_PIDOF)
 		exit(STATUS_UNKNOWN);
 	else
 		exit(3);
@@ -929,6 +930,7 @@ usage(void)
 "  -S, --start -- <argument>...  start a program and pass <arguments> to it\n"
 "  -K, --stop                    stop a program\n"
 "  -T, --status                  get the program status\n"
+"      --pidof                   print the porgram pid\n"
 "  -H, --help                    print help information\n"
 "  -V, --version                 print version\n"
 "\n");
@@ -998,7 +1000,7 @@ usage(void)
 "  1 = nothing done (=> 0 if --oknodo)\n"
 "  2 = with --retry, processes would not die\n"
 "  3 = trouble\n"
-"Exit status with --status:\n"
+"Exit status with --status and --pidof:\n"
 "  0 = program is running\n"
 "  1 = program is not running and the pid file exists\n"
 "  3 = program is not running\n"
@@ -1020,7 +1022,7 @@ badusage(const char *msg)
 		fprintf(stderr, "%s: %s\n", progname, msg);
 	fprintf(stderr, "Try '%s --help' for more information.\n", progname);
 
-	if (action == ACTION_STATUS)
+	if (action == ACTION_STATUS || action == ACTION_PIDOF)
 		exit(STATUS_UNKNOWN);
 	else
 		exit(3);
@@ -1309,6 +1311,7 @@ set_action(enum action_code new_action)
 #define OPT_RM_PIDFILE	502
 #define OPT_NOTIFY_AWAIT	503
 #define OPT_NOTIFY_TIMEOUT	504
+#define OPT_PIDOF	505
 
 static void
 parse_options(int argc, char * const *argv)
@@ -1318,6 +1321,7 @@ parse_options(int argc, char * const *argv)
 		{ "stop",	  0, NULL, 'K'},
 		{ "start",	  0, NULL, 'S'},
 		{ "status",	  0, NULL, 'T'},
+		{ "pidof",	  0, NULL, OPT_PIDOF},
 		{ "version",	  0, NULL, 'V'},
 		{ "startas",	  1, NULL, 'a'},
 		{ "name",	  1, NULL, 'n'},
@@ -1379,6 +1383,9 @@ parse_options(int argc, char * const *argv)
 		case 'T':  /* --status */
 			set_action(ACTION_STATUS);
 			break;
+		case OPT_PIDOF:
+			set_action(ACTION_PIDOF);
+			break;
 		case 'V':  /* --version */
 			do_version();
 			exit(0);
@@ -2355,6 +2362,9 @@ pid_check(pid_t pid)
 
 	pid_list_push(&found, pid);
 
+	if (action == ACTION_PIDOF)
+		printf("%d\n", pid);
+
 	return STATUS_OK;
 }
 
@@ -2967,7 +2977,7 @@ main(int argc, char **argv)
 		return do_start(argc, argv);
 	else if (action == ACTION_STOP)
 		return run_stop_schedule();
-	else if (action == ACTION_STATUS)
+	else if (action == ACTION_STATUS || action == ACTION_PIDOF)
 		return do_findprocs();
 
 	return 0;
-- 
2.53.0

Reply via email to