Revised patch:

Fix option --foreground to implement expectable behavior and allow usage of SMP mode with service supervisors that do not work well with daemons.

Currently, --foreground behavior is counter-intuitive in that the launched
process, while staying in the foreground, forks another "master" process,
which will create additional children (Kids), depending on the number of
configured workers/diskers.

Furthermore, sending a SIGINT/SIGTERM signal to this foreground process
terminates it, but leaves all the children running.

The behavior got introduces with revno 14561.

From discussion on squid-dev, the following behavior is implemented by this patch:

* -N: The initial process is a master and a worker process.
  No kids.
  No daemonimization.

* --foreground: The initial process is the master process.
  One or more worker kids (depending on workers=N).
  No daemonimization.

* neither: The initial process double-forks the master process.
  One or more worker kids (depending on workers=N).
  Daemonimization.

The Release Notes for v4 were updated to reflect the corrected behavior.

Kind Regards,
Andreas

--
Mit freundlichem Gruß / Best regards,

Andreas Weigel
UTM Backend Developer

Securepoint GmbH
Salzstrasse 1
D-21335 Lüneburg

https://www.securepoint.de

Tel.: +49(0)413124010
Fax: +49(0)4131240118

Geschäftsführer: Lutz Hausmann, Claudia Hausmann
Amtsgericht Lüneburg HRB 1776
USt.-ID-Nr.: DE 188 528 597

# Bazaar merge directive format 2 (Bazaar 0.90)
# revision_id: [email protected]\
#   vo7a07fz16rjl5yn
# target_branch: http://bazaar.launchpad.net/~squid/squid/4/
# testament_sha1: 7ea5eef797b0b68a3c3cf9a0a65baa62f33929b1
# timestamp: 2017-06-15 12:50:42 +0200
# source_branch: http://bazaar.launchpad.net/~squid/squid/4/
# base_revision_id: [email protected]\
#   pbj41bxbuk1t1ccz
# 
# Begin patch
=== modified file 'doc/release-notes/release-4.sgml'
--- doc/release-notes/release-4.sgml	2017-06-01 12:38:26 +0000
+++ doc/release-notes/release-4.sgml	2017-06-15 10:50:17 +0000
@@ -165,10 +165,9 @@
    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

=== modified file 'src/main.cc'
--- src/main.cc	2017-05-29 03:19:47 +0000
+++ src/main.cc	2017-06-15 10:50:17 +0000
@@ -387,9 +387,9 @@
             "       -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"
@@ -1762,12 +1762,25 @@
     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(0); // successfully daemonized; TODO: Use EXIT_SUCCESS
+    }
+}
+
 static void
 watch_child(char *argv[])
 {
 #if !_SQUID_WINDOWS_
     char *prog;
-    PidStatus status_f, status;
+    PidStatus status;
     pid_t pid;
 #ifdef TIOCNOTTY
 
@@ -1780,21 +1793,13 @@
 
     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(0);
+    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));

# Begin bundle
IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWYqulL4ABQtfgFxScnf///+n
3sS////6YAltczHr4+dVe7lVd4K7jbndGXbUU+mkfCSRJiZGmRpMnojaTJkgwE9T1NqaMg0ZpDTT
QSUINGjIJlT/URMho0GgAGgAAANAyRoJ6qGnpHomjagGgaAABoAAAAYkTKnpGjTQMnplGQDTJkAD
QDQBoZAikmjQmiap+jyTTITaJ6Eg9E9TEaPUAAAARRI0JoTGip+iephT9IjamYoNNNDINBoAGkSE
elG8xtqw8BfmrO2C8Md8Xmf439pnM2XOWfHPHLc9NmZz0+kdEXLaA2y8hrFIejQIBs+05C5fopqo
LjU+N+UTbhimm3t9IAiEYvWOOpkPYCApr7zy8GKdsDZquQx0x7iYJIlIbXDH5AElqAamRWkrzPkZ
MjOjQnTxTCLg8Bk98+I5MW6hPNYc47L2CazXgZFQRFW1vuMoocXCtnPwxOYvcUK9WZjmCM4IVxoz
ZXIHlHaGK8IIBFr95HSWAbRF6T5VCX9aU0WmFiq7RdVMJJg2ZkQDFGhAGiSWSIjmtK6+ThfipjhG
vG6WrFta4D3GbS3BguO08KjNPsn4EoObC9F9hMsOZr76mRS1XVPBglgs0FjJxjooONUHfJayzBmp
MO6sFRAkViRiSxOYn1SyvS2IhoWJckhW976nFrAktH3uWIhyM2el5LNYWuAIbVOGBWguIWpaylWa
GZmuHSPMGa0RA9hXWBzObGmK7r1pHw2m7+srHExWgF1hgBJKgf2s131bjMUO0xLdfJgY3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5N2dDbqSFM1nUBfOnZvx60Gxrii
wvQvL3rBk5q/gl1LFULsMHJiKVVapj7mYYgT2Zbm4kpYu0DYtNLttiJMpEjQ9y7NS8ONj4T/F3JF
OFCQiq6Uvg==
_______________________________________________
squid-dev mailing list
[email protected]
http://lists.squid-cache.org/listinfo/squid-dev

Reply via email to