Przeglądaj źródła

s-s-d: Add a new --remove-pidfile option

This is the counter-option to --make-pidfile, so that programs that need
their pidfile created can use an option to remove them without needing
to do that manually.
Guillem Jover 11 lat temu
rodzic
commit
d163f7f5c0
3 zmienionych plików z 30 dodań i 1 usunięć
  1. 1 0
      debian/changelog
  2. 10 1
      man/start-stop-daemon.8
  3. 19 0
      utils/start-stop-daemon.c

+ 1 - 0
debian/changelog

@@ -7,6 +7,7 @@ dpkg (1.17.19) UNRELEASED; urgency=low
     the process itself. Although a bit of a dubious usage, because any error
     before executing the program will not be properly reported to the caller.
     Regression introduced in dpkg 1.17.14. Closes: #765110
+  * Add new --remove-pidfile option to start-stop-daemon.
 
   [ Updated programs translations ]
   * Italian (Milo Casagrande): Closes: #765748

+ 10 - 1
man/start-stop-daemon.8

@@ -286,7 +286,8 @@ option will make
 create the file referenced with
 .B \-\-pidfile
 and place the pid into it just before executing the process. Note, the
-file will not be removed when stopping the program.
+file will only be removed when stopping the program if
+\fB\-\-remove\-pidfile\fP is used.
 .B NOTE:
 This feature may not work in all cases. Most notably when the program
 being executed forks from its main process. Because of this, it is usually
@@ -294,6 +295,14 @@ only useful when combined with the
 .B \-\-background
 option.
 .TP
+.B \-\-remove\-pidfile
+Used when stopping a program that does not remove its own pid file. This
+option will make
+.B start\-stop\-daemon
+remove the file referenced with
+.B \-\-pidfile
+after terminating the process.
+.TP
 .BR \-v ", " \-\-verbose
 Print verbose informational messages.
 .

+ 19 - 0
utils/start-stop-daemon.c

@@ -174,6 +174,7 @@ static int exitnodo = 1;
 static bool background = false;
 static bool close_io = true;
 static bool mpidfile = false;
+static bool rpidfile = false;
 static int signal_nr = SIGTERM;
 static int user_id = -1;
 static int runas_uid = -1;
@@ -415,6 +416,13 @@ write_pidfile(const char *filename, pid_t pid)
 		fatal("unable to close pidfile '%s'", filename);
 }
 
+static void
+remove_pidfile(const char *filename)
+{
+	if (unlink(filename) < 0 && errno != ENOENT)
+		fatal("cannot remove pidfile '%s'", filename);
+}
+
 static void
 daemonize(void)
 {
@@ -535,6 +543,7 @@ usage(void)
 "  -b|--background               force the process to detach\n"
 "  -C|--no-close                 do not close any file descriptor\n"
 "  -m|--make-pidfile             create the pidfile before starting\n"
+"    |--remove-pidfile           delete the pidfile after stopping\n"
 "  -R|--retry <schedule>         check whether processes die, and retry\n"
 "  -t|--test                     test mode, don't do anything\n"
 "  -o|--oknodo                   exit status 0 (not 1) if nothing done\n"
@@ -878,6 +887,7 @@ set_action(enum action_code new_action)
 
 #define OPT_PID		500
 #define OPT_PPID	501
+#define OPT_RM_PIDFILE	502
 
 static void
 parse_options(int argc, char * const *argv)
@@ -910,6 +920,7 @@ parse_options(int argc, char * const *argv)
 		{ "background",	  0, NULL, 'b'},
 		{ "no-close",	  0, NULL, 'C'},
 		{ "make-pidfile", 0, NULL, 'm'},
+		{ "remove-pidfile", 0, NULL, OPT_RM_PIDFILE},
 		{ "retry",	  1, NULL, 'R'},
 		{ "chdir",	  1, NULL, 'd'},
 		{ NULL,		  0, NULL, 0  }
@@ -1015,6 +1026,9 @@ parse_options(int argc, char * const *argv)
 		case 'm':  /* --make-pidfile */
 			mpidfile = true;
 			break;
+		case OPT_RM_PIDFILE: /* --remove-pidfile */
+			rpidfile = true;
+			break;
 		case 'R':  /* --retry <schedule>|<timeout> */
 			schedule_str = optarg;
 			break;
@@ -1080,6 +1094,8 @@ parse_options(int argc, char * const *argv)
 
 	if (mpidfile && pidfile == NULL)
 		badusage("--make-pidfile requires --pidfile");
+	if (rpidfile && pidfile == NULL)
+		badusage("--remove-pidfile requires --pidfile");
 
 	if (pid_str && pidfile)
 		badusage("need either --pid of --pidfile, not both");
@@ -2033,6 +2049,9 @@ do_stop_timeout(int timeout, int *n_killed, int *n_notkilled)
 static int
 finish_stop_schedule(bool anykilled)
 {
+	if (rpidfile && pidfile && !testmode)
+		remove_pidfile(pidfile);
+
 	if (anykilled)
 		return 0;