Просмотр исходного кода

s-s-d: Add new --pid option

This option is useful when the user knows the pid of the process to act
on, and wants to use some of the features from s-s-d, w/o needing to
create a fake pid file.

Closes: #253265
Guillem Jover лет назад: 12
Родитель
Сommit
80de58344c
3 измененных файлов с 38 добавлено и 5 удалено
  1. 1 0
      debian/changelog
  2. 6 2
      man/start-stop-daemon.8
  3. 31 3
      utils/start-stop-daemon.c

+ 1 - 0
debian/changelog

@@ -14,6 +14,7 @@ dpkg (1.17.6) UNRELEASED; urgency=low
     reject those packages at install time anyway.
   * Unify and clarify dpkg-deb and dpkg conffile name length error message.
     Closes: #108196
+  * Add new start-stop-daemon --pid option. Closes: #253265
 
   [ Updated scripts translations ]
   * German (Helge Kreutzmann).

+ 6 - 2
man/start-stop-daemon.8

@@ -5,7 +5,7 @@
 .\" Copyright © 2000-2001 Wichert Akkerman <wakkerma@debian.org>
 .\" Copyright © 2002-2003 Adam Heath <doogie@debian.org>
 .\" Copyright © 2004 Scott James Remnant <keybuk@debian.org>
-.\" Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
+.\" Copyright © 2008-2013 Guillem Jover <guillem@debian.org>
 .\"
 .\" This is free software; you can redistribute it and/or modify
 .\" it under the terms of the GNU General Public License as published by
@@ -20,7 +20,7 @@
 .\" You should have received a copy of the GNU General Public License
 .\" along with this program.  If not, see <https://www.gnu.org/licenses/>.
 .
-.TH start\-stop\-daemon 8 "2012-06-17" "Debian Project" "dpkg utilities"
+.TH start\-stop\-daemon 8 "2013-12-14" "Debian Project" "dpkg utilities"
 .SH NAME
 start\-stop\-daemon \- start and stop system daemon programs
 .
@@ -103,6 +103,10 @@ Show the program version and exit.
 .SH OPTIONS
 .SS Matching options
 .TP
+.BR \-\-pid " \fIpid\fP"
+Check for a process with the specified \fIpid\fP. The \fIpid\fP must be a
+number greater than 0.
+.TP
 .BR \-p ", " \-\-pidfile " \fIpid-file\fP"
 Check whether a process has created the file \fIpid-file\fP. Note: using this
 matching option alone might cause unintended processes to be acted on, if the

+ 31 - 3
utils/start-stop-daemon.c

@@ -174,6 +174,7 @@ static const char *changedir = "/";
 static const char *cmdname = NULL;
 static char *execname = NULL;
 static char *startas = NULL;
+static pid_t match_pid = -1;
 static const char *pidfile = NULL;
 static char what_stop[1024];
 static const char *progname = "";
@@ -437,6 +438,7 @@ usage(void)
 "  -V|--version                  print version\n"
 "\n"
 "Matching options (at least one is required):\n"
+"     --pid <pid>                pid to check\n"
 "  -p|--pidfile <pid-file>       pid file to check\n"
 "  -x|--exec <executable>        program to start/check if it is running\n"
 "  -n|--name <process-name>      process name to check\n"
@@ -559,6 +561,17 @@ parse_unsigned(const char *string, int base, int *value_r)
 	return 0;
 }
 
+static int
+parse_pid(const char *pid_str, int *pid_num)
+{
+	if (parse_unsigned(pid_str, 10, pid_num) != 0)
+		return -1;
+	if (*pid_num == 0)
+		return -1;
+
+	return 0;
+}
+
 static int
 parse_signal(const char *sig_str, int *sig_num)
 {
@@ -803,6 +816,7 @@ parse_options(int argc, char * const *argv)
 		{ "startas",	  1, NULL, 'a'},
 		{ "name",	  1, NULL, 'n'},
 		{ "oknodo",	  0, NULL, 'o'},
+		{ "pid",	  1, NULL, 500},
 		{ "pidfile",	  1, NULL, 'p'},
 		{ "quiet",	  0, NULL, 'q'},
 		{ "signal",	  1, NULL, 's'},
@@ -824,6 +838,7 @@ parse_options(int argc, char * const *argv)
 		{ "chdir",	  1, NULL, 'd'},
 		{ NULL,		  0, NULL, 0  }
 	};
+	const char *pid_str = NULL;
 	const char *umask_str = NULL;
 	const char *signal_str = NULL;
 	const char *schedule_str = NULL;
@@ -862,6 +877,9 @@ parse_options(int argc, char * const *argv)
 		case 'o':  /* --oknodo */
 			exitnodo = 0;
 			break;
+		case 500: /* --pid <pid> */
+			pid_str = optarg;
+			break;
 		case 'p':  /* --pidfile <pid-file> */
 			pidfile = optarg;
 			break;
@@ -929,6 +947,11 @@ parse_options(int argc, char * const *argv)
 		}
 	}
 
+	if (pid_str != NULL) {
+		if (parse_pid(pid_str, &match_pid) != 0)
+			badusage("pid value must be a number greater than 0");
+	}
+
 	if (signal_str != NULL) {
 		if (parse_signal(signal_str, &signal_nr) != 0)
 			badusage("signal value must be numeric or name"
@@ -953,8 +976,8 @@ parse_options(int argc, char * const *argv)
 	if (action == action_none)
 		badusage("need one of --start or --stop or --status");
 
-	if (!execname && !pidfile && !userspec && !cmdname)
-		badusage("need at least one of --exec, --pidfile, --user or --name");
+	if (!execname && !pid_str && !pidfile && !userspec && !cmdname)
+		badusage("need at least one of --exec, --pid, --pidfile, --user or --name");
 
 #ifdef PROCESS_NAME_SIZE
 	if (cmdname && strlen(cmdname) > PROCESS_NAME_SIZE)
@@ -972,6 +995,9 @@ parse_options(int argc, char * const *argv)
 	if (mpidfile && pidfile == NULL)
 		badusage("--make-pidfile requires --pidfile");
 
+	if (pid_str && pidfile)
+		badusage("need either --pid of --pidfile, not both");
+
 	if (background && action != action_start)
 		badusage("--background is only relevant with --start");
 
@@ -1481,7 +1507,9 @@ do_findprocs(void)
 {
 	pid_list_free(&found);
 
-	if (pidfile)
+	if (match_pid > 0)
+		return pid_check(match_pid);
+	else if (pidfile)
 		return do_pidfile(pidfile);
 	else
 		return do_procinit();