diff --git a/src/rc/start-stop-daemon.c b/src/rc/start-stop-daemon.c index bfa8852..9e484d9 100644 --- a/src/rc/start-stop-daemon.c +++ b/src/rc/start-stop-daemon.c @@ -596,7 +596,7 @@ expand_home(const char *home, const char *path) } #include "_usage.h" -#define getoptstring "I:KN:PR:Sa:bc:d:e:g:ik:mn:op:s:tu:r:w:x:1:2:" getoptstring_COMMON +#define getoptstring "I:KN:PR:Sa:bc:d:e:g:ik:mn:op:s:tu:r:w:x:1:2:lL" getoptstring_COMMON static const struct option longopts[] = { { "ionice", 1, NULL, 'I'}, { "stop", 0, NULL, 'K'}, @@ -624,6 +624,8 @@ static const struct option longopts[] = { { "stdout", 1, NULL, '1'}, { "stderr", 1, NULL, '2'}, { "progress", 0, NULL, 'P'}, + { "userlog", 0, NULL, 'l'}, + { "rootlog", 0, NULL, 'L'}, longopts_COMMON }; static const char * const longopts_help[] = { @@ -653,6 +655,8 @@ static const char * const longopts_help[] = { "Redirect stdout to file", "Redirect stderr to file", "Print dots each second while waiting", + "Opens log redirect file after chuid (default)", + "Opens log redirect file before chuid (root owns log files)", longopts_help_COMMON }; #include "_usage.c" @@ -693,6 +697,7 @@ start_stop_daemon(int argc, char **argv) gid_t gid = 0; char *home = NULL; int tid = 0; + bool user_log = true; char *redirect_stderr = NULL; char *redirect_stdout = NULL; int stdout_fd; @@ -900,6 +905,12 @@ start_stop_daemon(int argc, char **argv) exec = optarg; break; + case 'l': + user_log = true; + break; + case 'L': + user_log = false; + break; case '1': /* --stdout /path/to/stdout.lgfile */ redirect_stdout = optarg; break; @@ -1168,6 +1179,26 @@ start_stop_daemon(int argc, char **argv) fprintf(fp, "%d\n", mypid); fclose(fp); } + stdout_fd = devnull_fd; + stderr_fd = devnull_fd; + if (!user_log) { + if (redirect_stdout) { + if ((stdout_fd = open(redirect_stdout, + O_WRONLY | O_CREAT | O_APPEND, + S_IRUSR | S_IWUSR)) == -1) + eerrorx("%s: unable to open the logfile" + " for stdout `%s': %s", + applet, redirect_stdout, strerror(errno)); + } + if (redirect_stderr) { + if ((stderr_fd = open(redirect_stderr, + O_WRONLY | O_CREAT | O_APPEND, + S_IRUSR | S_IWUSR)) == -1) + eerrorx("%s: unable to open the logfile" + " for stderr `%s': %s", + applet, redirect_stderr, strerror(errno)); + } + } #ifdef HAVE_PAM if (changeuser != NULL) { @@ -1265,8 +1296,7 @@ start_stop_daemon(int argc, char **argv) setenv("PATH", newpath, 1); } - stdout_fd = devnull_fd; - stderr_fd = devnull_fd; + if (user_log) { if (redirect_stdout) { if ((stdout_fd = open(redirect_stdout, O_WRONLY | O_CREAT | O_APPEND, @@ -1283,6 +1313,7 @@ start_stop_daemon(int argc, char **argv) " for stderr `%s': %s", applet, redirect_stderr, strerror(errno)); } + } /* We don't redirect stdin as some daemons may need it */ if (background || quiet || redirect_stdout)