Browse Source

s-s-d: Add new --no-close option to disable closing fds on --background

This enabled the caller to see process messages for debugging purposes,
or to be able to redirect file descriptors to log files, syslog or
similar.

Closes: #627333, #646425
Guillem Jover 12 years ago
parent
commit
3de1552982
3 changed files with 25 additions and 8 deletions
  1. 2 0
      debian/changelog
  2. 7 1
      man/start-stop-daemon.8
  3. 16 7
      utils/start-stop-daemon.c

+ 2 - 0
debian/changelog

@@ -27,6 +27,8 @@ dpkg (1.16.5) UNRELEASED; urgency=low
     Thanks to Niels Thykier <niels@thykier.net>. Closes: #677631
   * Document in more detail in deb(5) the supported ar archive format.
   * Document in deb-src-control(5) the “Private-” field prefix.
+  * Add new start-stop-daemon --no-close option to disable closing file
+    descriptors on --background. Closes: #627333, #64642
 
   [ Updated dpkg translations ]
   * Swedish (Peter Krefting).

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

@@ -20,7 +20,7 @@
 .\" You should have received a copy of the GNU General Public License
 .\" along with this program.  If not, see <http://www.gnu.org/licenses/>.
 .
-.TH start\-stop\-daemon 8 "2012-05-17" "Debian Project" "dpkg utilities"
+.TH start\-stop\-daemon 8 "2012-06-17" "Debian Project" "dpkg utilities"
 .SH NAME
 start\-stop\-daemon \- start and stop system daemon programs
 .
@@ -245,6 +245,12 @@ reason. This is a last resort, and is only meant for programs that either
 make no sense forking on their own, or where it's not feasible to add the
 code for them to do this themselves.
 .TP
+.BR \-C ", " \-\-no\-close
+Do not close any file descriptor when forcing the daemon into the background.
+Used for debugging purposes to see the process output, or to redirect file
+descriptors to log the process output.
+Only relevant when using \fB\-\-background\fP.
+.TP
 .BR \-N ", " \-\-nicelevel " \fIint\fP"
 This alters the priority of the process before starting it.
 .TP

+ 16 - 7
utils/start-stop-daemon.c

@@ -155,6 +155,7 @@ static int testmode = 0;
 static int quietmode = 0;
 static int exitnodo = 1;
 static int background = 0;
+static int close_io = 1;
 static int mpidfile = 0;
 static int signal_nr = SIGTERM;
 static int user_id = -1;
@@ -454,6 +455,7 @@ usage(void)
 "                                  scheduler (default prio is 4)\n"
 "  -k|--umask <mask>             change the umask to <mask> before starting\n"
 "  -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"
 "  -R|--retry <schedule>         check whether processes die, and retry\n"
 "  -t|--test                     test mode, don't do anything\n"
@@ -812,6 +814,7 @@ parse_options(int argc, char * const *argv)
 		{ "iosched",	  1, NULL, 'I'},
 		{ "umask",	  1, NULL, 'k'},
 		{ "background",	  0, NULL, 'b'},
+		{ "no-close",	  0, NULL, 'C'},
 		{ "make-pidfile", 0, NULL, 'm'},
 		{ "retry",	  1, NULL, 'R'},
 		{ "chdir",	  1, NULL, 'd'},
@@ -904,6 +907,9 @@ parse_options(int argc, char * const *argv)
 		case 'b':  /* --background */
 			background = 1;
 			break;
+		case 'C': /* --no-close */
+			close_io = 0;
+			break;
 		case 'm':  /* --make-pidfile */
 			mpidfile = 1;
 			break;
@@ -964,6 +970,9 @@ parse_options(int argc, char * const *argv)
 
 	if (background && action != action_start)
 		badusage("--background is only relevant with --start");
+
+	if (!close_io && !background)
+		badusage("--no-close is only relevant with --background");
 }
 
 #if defined(OSHurd)
@@ -1731,10 +1740,10 @@ main(int argc, char **argv)
 	if (quietmode < 0)
 		printf("Starting %s...\n", startas);
 	*--argv = startas;
-	if (background) {
+	if (background)
 		/* Ok, we need to detach this process. */
 		daemonize();
-
+	if (background && close_io) {
 		devnull_fd = open("/dev/null", O_RDWR);
 		if (devnull_fd < 0)
 			fatal("unable to open '%s'", "/dev/null");
@@ -1783,12 +1792,12 @@ main(int argc, char **argv)
 				fatal("unable to set uid to %s", changeuser);
 	}
 
-	if (background) {
-		int i;
+	/* Set a default umask for dumb programs. */
+	if (background && umask_value < 0)
+		umask(022);
 
-		/* Set a default umask for dumb programs. */
-		if (umask_value < 0)
-			umask(022);
+	if (background && close_io) {
+		int i;
 
 		dup2(devnull_fd, 0); /* stdin */
 		dup2(devnull_fd, 1); /* stdout */