From: msweet Date: Wed, 12 Nov 2014 14:35:59 +0000 (+0000) Subject: Fix a random crash in the scheduler when not using systemd (STR #4484) X-Git-Tag: v2.2b1~445 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=108f75052df52a54c8d7c7eaf732dd100b5c949c;p=thirdparty%2Fcups.git Fix a random crash in the scheduler when not using systemd (STR #4484) git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@12245 a1ca3aef-8c08-0410-bb20-df032aa958be --- diff --git a/CHANGES-2.0.txt b/CHANGES-2.0.txt index 52dc04cf4a..41e5fca203 100644 --- a/CHANGES-2.0.txt +++ b/CHANGES-2.0.txt @@ -13,6 +13,8 @@ CHANGES IN CUPS V2.0.1 - Fixed a crash in ippAttributeString () - Fixed a crash in the scheduler on Linux/*BSD if colord was not running (STR #4496) + - Fixed a random crash in the scheduler when not using systemd + (STR #4484) - Added systemd support for cups-lpd (STR #4493) - The scheduler did not honor the FatalErrors directive for mis- configured Group and SystemGroup values (STR #4495) diff --git a/scheduler/main.c b/scheduler/main.c index 78243def0e..21985b228b 100644 --- a/scheduler/main.c +++ b/scheduler/main.c @@ -100,7 +100,11 @@ main(int argc, /* I - Number of command-line args */ { int i; /* Looping var */ char *opt; /* Option character */ - int fg; /* Run in the foreground */ + int close_all = 1, /* Close all file descriptors? */ + disconnect = 1, /* Disconnect from controlling terminal? */ + fg = 0, /* Run in foreground? */ + run_as_child = 0; + /* Running as child process? */ int fds; /* Number of ready descriptors */ cupsd_client_t *con; /* Current client */ cupsd_job_t *job; /* Current job */ @@ -116,8 +120,6 @@ main(int argc, /* I - Number of command-line args */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ - int run_as_child = 0; - /* Needed for background fork/exec */ #ifdef __APPLE__ int use_sysman = 1; /* Use system management functions? */ #else @@ -150,8 +152,10 @@ main(int argc, /* I - Number of command-line args */ #ifdef HAVE_LAUNCHD if (getenv("CUPSD_LAUNCHD")) { - OnDemand = 1; - fg = 1; + OnDemand = 1; + fg = 1; + close_all = 0; + disconnect = 0; } #endif /* HAVE_LAUNCHD */ @@ -162,7 +166,8 @@ main(int argc, /* I - Number of command-line args */ { case 'C' : /* Run as child with config file */ run_as_child = 1; - fg = -1; + fg = 1; + close_all = 0; case 'c' : /* Configuration file */ i ++; @@ -217,11 +222,14 @@ main(int argc, /* I - Number of command-line args */ break; case 'f' : /* Run in foreground... */ - fg = 1; + fg = 1; + disconnect = 0; + close_all = 0; break; case 'F' : /* Run in foreground, but disconnect from terminal... */ - fg = -1; + fg = 1; + close_all = 0; break; case 'h' : /* Show usage/help */ @@ -230,12 +238,16 @@ main(int argc, /* I - Number of command-line args */ case 'l' : /* Started by launchd/systemd... */ #if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) - OnDemand = 1; - fg = 1; + OnDemand = 1; + fg = 1; + close_all = 0; + disconnect = 0; #else _cupsLangPuts(stderr, _("cupsd: On-demand support not compiled " "in, running in normal mode.")); - fg = 0; + fg = 0; + disconnect = 1; + close_all = 1; #endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ break; @@ -244,6 +256,8 @@ main(int argc, /* I - Number of command-line args */ "use only!\n", stderr); stop_scheduler = 1; fg = 1; + disconnect = 0; + close_all = 0; break; case 'P' : /* Disable security profiles */ @@ -285,6 +299,8 @@ main(int argc, /* I - Number of command-line args */ case 't' : /* Test the cupsd.conf file... */ TestConfigFile = 1; fg = 1; + disconnect = 0; + close_all = 0; break; default : /* Unknown option */ @@ -332,8 +348,57 @@ main(int argc, /* I - Number of command-line args */ free(filename); } + if (disconnect) + { + /* + * Make sure we aren't tying up any filesystems... + */ + + chdir("/"); + + /* + * Disconnect from the controlling terminal... + */ + + setsid(); + } + + if (close_all) + { + /* + * Close all open files... + */ + + getrlimit(RLIMIT_NOFILE, &limit); + + for (i = 0; i < (int)limit.rlim_cur && i < 1024; i ++) + close(i); + + /* + * Redirect stdin/out/err to /dev/null... + */ + + if ((i = open("/dev/null", O_RDONLY)) != 0) + { + dup2(i, 0); + close(i); + } + + if ((i = open("/dev/null", O_WRONLY)) != 1) + { + dup2(i, 1); + close(i); + } + + if ((i = open("/dev/null", O_WRONLY)) != 2) + { + dup2(i, 2); + close(i); + } + } + /* - * If the user hasn't specified "-f", run in the background... + * Run in the background as needed... */ if (!fg) @@ -408,74 +473,17 @@ main(int argc, /* I - Number of command-line args */ #endif /* __OpenBSD__ && OpenBSD < 201211 */ /* - * Since CoreFoundation and DBUS both create fork-unsafe data on execution of - * a program, and since this kind of really unfriendly behavior seems to be - * more common these days in system libraries, we need to re-execute the - * background cupsd with the "-C" option to avoid problems. Unfortunately, - * we also have to assume that argv[0] contains the name of the cupsd - * executable - there is no portable way to get the real pathname... + * Since many system libraries create fork-unsafe data on execution of a + * program, we need to re-execute the background cupsd with the "-C" and "-s" + * options to avoid problems. Unfortunately, we also have to assume that + * argv[0] contains the name of the cupsd executable - there is no portable + * way to get the real pathname... */ - execlp(argv[0], argv[0], "-C", ConfigurationFile, (char *)0); + execlp(argv[0], argv[0], "-C", ConfigurationFile, "-s", CupsFilesFile, (char *)0); exit(errno); } - if (fg < 1) - { - /* - * Make sure we aren't tying up any filesystems... - */ - - chdir("/"); - -#ifndef DEBUG - /* - * Disable core dumps... - */ - - getrlimit(RLIMIT_CORE, &limit); - limit.rlim_cur = 0; - setrlimit(RLIMIT_CORE, &limit); - - /* - * Disconnect from the controlling terminal... - */ - - setsid(); - - /* - * Close all open files... - */ - - getrlimit(RLIMIT_NOFILE, &limit); - - for (i = 0; i < limit.rlim_cur && i < 1024; i ++) - close(i); - - /* - * Redirect stdin/out/err to /dev/null... - */ - - if ((i = open("/dev/null", O_RDONLY)) != 0) - { - dup2(i, 0); - close(i); - } - - if ((i = open("/dev/null", O_WRONLY)) != 1) - { - dup2(i, 1); - close(i); - } - - if ((i = open("/dev/null", O_WRONLY)) != 2) - { - dup2(i, 2); - close(i); - } -#endif /* DEBUG */ - } - /* * Set the timezone info... */ diff --git a/xcode/CUPS.xcodeproj/project.pbxproj b/xcode/CUPS.xcodeproj/project.pbxproj index 52509f2c35..e48390b2b4 100644 --- a/xcode/CUPS.xcodeproj/project.pbxproj +++ b/xcode/CUPS.xcodeproj/project.pbxproj @@ -1509,6 +1509,8 @@ 724379C41333FFC7009631B9 /* usb-darwin.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = "usb-darwin.c"; path = "../backend/usb-darwin.c"; sourceTree = ""; }; 724379C51333FFC7009631B9 /* usb.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = usb.c; path = ../backend/usb.c; sourceTree = ""; }; 724379CA1334000E009631B9 /* ieee1284.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ieee1284.c; path = ../backend/ieee1284.c; sourceTree = ""; }; + 72496E161A13A03B0051899C /* org.cups.cups-lpd.socket */ = {isa = PBXFileReference; lastKnownFileType = text; name = "org.cups.cups-lpd.socket"; path = "../scheduler/org.cups.cups-lpd.socket"; sourceTree = SOURCE_ROOT; }; + 72496E171A13A03B0051899C /* org.cups.cups-lpdAT.service.in */ = {isa = PBXFileReference; lastKnownFileType = text; name = "org.cups.cups-lpdAT.service.in"; path = "../scheduler/org.cups.cups-lpdAT.service.in"; sourceTree = SOURCE_ROOT; }; 7258EAE2134594C4009286F1 /* rastertopwg */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = rastertopwg; sourceTree = BUILT_PRODUCTS_DIR; }; 7258EAEC134594EB009286F1 /* rastertopwg.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = rastertopwg.c; path = ../filter/rastertopwg.c; sourceTree = ""; }; 726AD6F7135E88F0002C930D /* ippserver */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ippserver; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -2267,6 +2269,8 @@ 72E65BDC18DC852700097E89 /* Makefile */, 7226369B18AE6D19004ED309 /* org.cups.cups-lpd.plist */, 72E65BD518DC818400097E89 /* org.cups.cups-lpd.plist.in */, + 72496E171A13A03B0051899C /* org.cups.cups-lpdAT.service.in */, + 72496E161A13A03B0051899C /* org.cups.cups-lpd.socket */, 72E65BD618DC818400097E89 /* org.cups.cupsd.path.in */, 7226369C18AE6D19004ED309 /* org.cups.cupsd.plist */, 72E65BD718DC818400097E89 /* org.cups.cupsd.service.in */, diff --git a/xcode/config.h b/xcode/config.h index affa9bc9b2..58b9c9b463 100644 --- a/xcode/config.h +++ b/xcode/config.h @@ -454,7 +454,7 @@ #define HAVE_LAUNCH_H 1 #define HAVE_LAUNCHD 1 -#undef HAVE_LAUNCH_ACTIVATE_SOCKET +#define HAVE_LAUNCH_ACTIVATE_SOCKET 1 /*