integration with daemon managers such as Upstart or systemd which assume the
process they initiated is the daemon with a PID to control.
-<p>The squid binary now has a new <em>--foreground</em> command line option
- which prevents the process from exiting early while background workers
- continue their processing. When run with this option Squid will now wait
- for the worker(s) to finish before exiting. Unlike the old <em>-N</em> option
+<p>The squid binary now has a new <em>--foreground</em> command line option,
+ which (only) prevents daemonizing the master process.
+ Unlike the old <em>-N</em> option,
<em>--foreground</em> supports SMP workers and multi-process features.
<em>--foreground</em> is particularly useful for use with <em>-z</em> (disk
cache structures creation), as it allows the caller to wait until Squid has
" -C Do not catch fatal signals.\n"
" -D OBSOLETE. Scheduled for removal.\n"
" -F Don't serve any requests until store is rebuilt.\n"
- " -N No daemon mode.\n"
+ " -N Master process runs in foreground and is a worker. No kids.\n"
" --foreground\n"
- " Parent process does not exit until its children have finished.\n"
+ " Master process runs in foreground and creates worker kids.\n"
#if USE_WIN32_SERVICE
" -O options\n"
" Set Windows Service Command line options in Registry.\n"
return (DebugSignal > 0 || RotateSignal > 0 || ReconfigureSignal > 0 || ShutdownSignal > 0);
}
+static void GoIntoBackground()
+{
+ pid_t pid;
+ if ((pid = fork()) < 0) {
+ int xerrno = errno;
+ syslog(LOG_ALERT, "fork failed: %s", xstrerr(xerrno));
+ // continue anyway, mimicking --foreground mode (XXX?)
+ } else if (pid > 0) {
+ // parent
+ exit(EXIT_SUCCESS);
+ }
+}
+
static void
watch_child(char *argv[])
{
#if !_SQUID_WINDOWS_
char *prog;
- PidStatus status_f, status;
+ PidStatus status;
pid_t pid;
#ifdef TIOCNOTTY
openlog(APP_SHORTNAME, LOG_PID | LOG_NDELAY | LOG_CONS, LOG_LOCAL4);
- if ((pid = fork()) < 0) {
- int xerrno = errno;
- syslog(LOG_ALERT, "fork failed: %s", xstrerr(xerrno));
- } else if (pid > 0) {
- // parent
- if (opt_foreground) {
- if (WaitForAnyPid(status_f, 0) < 0) {
- int xerrno = errno;
- syslog(LOG_ALERT, "WaitForAnyPid failed: %s", xstrerr(xerrno));
- }
- }
-
- exit(EXIT_SUCCESS);
- }
+ if (!opt_foreground)
+ GoIntoBackground();
+ // TODO: Fails with --foreground if the calling process is process group
+ // leader, which is always (?) the case. Should probably moved to
+ // GoIntoBackground and executed only after successfully forking
if (setsid() < 0) {
int xerrno = errno;
syslog(LOG_ALERT, "setsid failed: %s", xstrerr(xerrno));