diff --git a/src/rc/start-stop-daemon.c b/src/rc/start-stop-daemon.c index cc47c0b..cea2c86 100644 --- a/src/rc/start-stop-daemon.c +++ b/src/rc/start-stop-daemon.c @@ -673,6 +673,7 @@ start_stop_daemon(int argc, char **argv) char *startas = NULL; char *name = NULL; char *pidfile = NULL; + char *spidfile; //chroot aware pidfile char *retry = NULL; int sig = -1; int nicelevel = 0, ionicec = -1, ioniced = 0; @@ -1001,10 +1002,23 @@ start_stop_daemon(int argc, char **argv) exit(EXIT_FAILURE); } + /* if we are chrooting our service then pidfile will be created + * in the chroot folder, and it can't be controled in a natural way. + * in order to workaround this problem we introducing a chroot aware + * version of a pidfile variable. + */ + if (ch_root && pidfile) { + spidfile = malloc(strlen(ch_root) + strlen(pidfile)); + strcpy(spidfile, ch_root); + strcat(spidfile, pidfile); + } else { + spidfile = pidfile; + } + /* If we don't have a pidfile we should check if it's interpreted * or not. If it we, we need to pass the interpreter through * to our daemon calls to find it correctly. */ - if (interpreted && !pidfile) { + if (interpreted && !spidfile) { fp = fopen(exec_file, "r"); if (fp) { p = fgets(line, sizeof(line), fp); @@ -1048,7 +1062,7 @@ start_stop_daemon(int argc, char **argv) else parse_schedule(NULL, sig); i = run_stop_schedule(exec, (const char *const *)margv, - pidfile, uid, test, progress); + spidfile, uid, test, progress); if (i < 0) /* We failed to stop something */ @@ -1060,17 +1074,17 @@ start_stop_daemon(int argc, char **argv) * remove information about it as it may have unexpectedly * crashed out. We should also return success as the end * result would be the same. */ - if (pidfile && exists(pidfile)) - unlink(pidfile); + if (spidfile && exists(spidfile)) + unlink(spidfile); if (svcname) rc_service_daemon_set(svcname, exec, (const char *const *)argv, - pidfile, false); + spidfile, false); exit(EXIT_SUCCESS); } - if (pidfile) - pid = get_pid(pidfile); + if (spidfile) + pid = get_pid(spidfile); else pid = 0; @@ -1107,8 +1121,8 @@ start_stop_daemon(int argc, char **argv) eindentv(); /* Remove existing pidfile */ - if (pidfile) - unlink(pidfile); + if (spidfile) + unlink(spidfile); if (background) signal_setup(SIGCHLD, handle_signal); @@ -1341,13 +1355,13 @@ start_stop_daemon(int argc, char **argv) if (kill(pid, 0) == 0) alive = true; } else { - if (pidfile) { - pid = get_pid(pidfile); + if (spidfile) { + pid = get_pid(spidfile); if (pid == -1) { eerrorx("%s: did not " "create a valid" " pid in `%s'", - applet, pidfile); + applet, spidfile); } } else pid = 0; @@ -1359,10 +1373,9 @@ start_stop_daemon(int argc, char **argv) if (!alive) eerrorx("%s: %s died", applet, exec); } - if (svcname) rc_service_daemon_set(svcname, exec, - (const char *const *)margv, pidfile, true); + (const char *const *)margv, spidfile, true); exit(EXIT_SUCCESS); /* NOTREACHED */