From c187d9abe735d867f1dc47db5646495250394bc9 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Fri, 27 May 2016 13:06:58 -0700 Subject: Factor out generic 'HAVE_ONDEMAND' macro flag HAVE_SYSTEMD and HAVE_LAUNCHD use very similar code for on-demand launching of cupsd, and if we want to add one more (e.g., upstart) it's easier to just use a single common flag: HAVE_ONDEMAND. --- config.h.in | 6 ++++++ scheduler/client.h | 4 ++-- scheduler/conf.c | 12 ++++++------ scheduler/conf.h | 4 ++-- scheduler/cupsd.h | 4 ++-- scheduler/listen.c | 8 ++++---- scheduler/main.c | 36 ++++++++++++++++++------------------ vcnet/config.h | 6 ++++++ xcode/config.h | 6 ++++++ 9 files changed, 52 insertions(+), 34 deletions(-) diff --git a/config.h.in b/config.h.in index e242d5611..526f91a5e 100644 --- a/config.h.in +++ b/config.h.in @@ -717,4 +717,10 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } # endif /* __GNUC__ || __STDC_VERSION__ */ #endif /* !HAVE_ABS && !abs */ +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +# define HAVE_ONDEMAND +#else +# undef HAVE_ONDEMAND +#endif + #endif /* !_CUPS_CONFIG_H_ */ diff --git a/scheduler/client.h b/scheduler/client.h index a7811fa78..174b7271e 100644 --- a/scheduler/client.h +++ b/scheduler/client.h @@ -79,9 +79,9 @@ typedef struct int fd; /* File descriptor for this server */ http_addr_t address; /* Bind address of socket */ http_encryption_t encryption; /* To encrypt or not to encrypt... */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) int on_demand; /* Is this a socket from launchd/systemd? */ -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ } cupsd_listener_t; diff --git a/scheduler/conf.c b/scheduler/conf.c index 5f97fa3b8..7695f56f4 100644 --- a/scheduler/conf.c +++ b/scheduler/conf.c @@ -89,9 +89,9 @@ static const cupsd_var_t cupsd_vars[] = #ifdef HAVE_GSSAPI { "GSSServiceName", &GSSServiceName, CUPSD_VARTYPE_STRING }, #endif /* HAVE_GSSAPI */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) { "IdleExitTimeout", &IdleExitTimeout, CUPSD_VARTYPE_TIME }, -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ { "JobKillDelay", &JobKillDelay, CUPSD_VARTYPE_TIME }, { "JobRetryLimit", &JobRetryLimit, CUPSD_VARTYPE_INTEGER }, { "JobRetryInterval", &JobRetryInterval, CUPSD_VARTYPE_TIME }, @@ -810,9 +810,9 @@ cupsdReadConfiguration(void) DefaultLeaseDuration = 86400; MaxLeaseDuration = 0; -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) IdleExitTimeout = 60; -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ /* * Setup environment variables... @@ -3149,9 +3149,9 @@ read_cupsd_conf(cups_file_t *fp) /* I - File to read from */ if (lis) { -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) if (!lis->on_demand) -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ { httpAddrString(&lis->address, temp, sizeof(temp)); cupsdLogMessage(CUPSD_LOG_WARN, diff --git a/scheduler/conf.h b/scheduler/conf.h index 076d63db0..ff18b7750 100644 --- a/scheduler/conf.h +++ b/scheduler/conf.h @@ -244,10 +244,10 @@ VAR char *ServerKeychain VALUE(NULL); /* Keychain holding cert + key */ #endif /* HAVE_SSL */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) VAR int IdleExitTimeout VALUE(60); /* Time after which an idle cupsd will exit */ -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ #ifdef HAVE_AUTHORIZATION_H VAR char *SystemGroupAuthKey VALUE(NULL); diff --git a/scheduler/cupsd.h b/scheduler/cupsd.h index a36ae88d1..669560c15 100644 --- a/scheduler/cupsd.h +++ b/scheduler/cupsd.h @@ -157,10 +157,10 @@ VAR int NeedReload VALUE(RELOAD_ALL), VAR void *DefaultProfile VALUE(0); /* Default security profile */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) VAR int OnDemand VALUE(0); /* Launched on demand */ -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ /* diff --git a/scheduler/listen.c b/scheduler/listen.c index 06e2ed6aa..7cfe8dbd1 100644 --- a/scheduler/listen.c +++ b/scheduler/listen.c @@ -41,9 +41,9 @@ cupsdDeleteAllListeners(void) for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); lis; lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) if (!lis->on_demand) -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ { cupsArrayRemove(Listeners, lis); free(lis); @@ -281,7 +281,7 @@ cupsdStopListening(void) lis; lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) { -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) if (!lis->on_demand && lis->fd != -1) { httpAddrClose(&(lis->address), lis->fd); @@ -294,6 +294,6 @@ cupsdStopListening(void) httpAddrClose(&(lis->address), lis->fd); lis->fd = -1; } -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ } } diff --git a/scheduler/main.c b/scheduler/main.c index f9b46bad6..8b65ae1e4 100644 --- a/scheduler/main.c +++ b/scheduler/main.c @@ -71,10 +71,10 @@ static void sigchld_handler(int sig); static void sighup_handler(int sig); static void sigterm_handler(int sig); static long select_timeout(int fds); -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) static void service_checkin(void); static void service_checkout(void); -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ static void usage(int status) __attribute__((noreturn)); @@ -131,10 +131,10 @@ main(int argc, /* I - Number of command-line args */ #else time_t netif_time = 0; /* Time since last network update */ #endif /* __APPLE__ */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) int service_idle_exit; /* Idle exit on select timeout? */ -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ #ifdef HAVE_GETEUID @@ -243,7 +243,7 @@ main(int argc, /* I - Number of command-line args */ break; case 'l' : /* Started by launchd/systemd... */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) OnDemand = 1; fg = 1; close_all = 0; @@ -254,7 +254,7 @@ main(int argc, /* I - Number of command-line args */ fg = 0; disconnect = 1; close_all = 1; -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ break; case 'p' : /* Stop immediately for profiling */ @@ -584,7 +584,7 @@ main(int argc, /* I - Number of command-line args */ cupsdCleanFiles(CacheDir, "*.ipp"); -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) if (OnDemand) { /* @@ -595,7 +595,7 @@ main(int argc, /* I - Number of command-line args */ service_checkin(); service_checkout(); } -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ /* * Startup the server... @@ -682,11 +682,11 @@ main(int argc, /* I - Number of command-line args */ * Send server-started event... */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) if (OnDemand) cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started on demand."); else -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ if (fg) cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in foreground."); else @@ -809,7 +809,7 @@ main(int argc, /* I - Number of command-line args */ if ((timeout = select_timeout(fds)) > 1 && LastEvent) timeout = 1; -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) /* * If no other work is scheduled and we're being controlled by * launchd then timeout after 'LaunchdTimeout' seconds of @@ -828,7 +828,7 @@ main(int argc, /* I - Number of command-line args */ } else service_idle_exit = 0; -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ if ((fds = cupsdDoSelect(timeout)) < 0) { @@ -925,7 +925,7 @@ main(int argc, /* I - Number of command-line args */ } #endif /* !__APPLE__ */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) /* * If no other work was scheduled and we're being controlled by launchd * then timeout after 'LaunchdTimeout' seconds of inactivity... @@ -939,7 +939,7 @@ main(int argc, /* I - Number of command-line args */ stop_scheduler = 1; break; } -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ /* * Resume listening for new connections as needed... @@ -1143,14 +1143,14 @@ main(int argc, /* I - Number of command-line args */ cupsdStopServer(); -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) /* * Update the keep-alive file as needed... */ if (OnDemand) service_checkout(); -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ /* * Stop all jobs... @@ -1822,7 +1822,7 @@ sigterm_handler(int sig) /* I - Signal number */ } -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_ONDEMAND) /* * 'service_checkin()' - Check-in with launchd and collect the listening fds. */ @@ -2174,7 +2174,7 @@ service_checkout(void) unlink(CUPS_KEEPALIVE); } } -#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */ +#endif /* HAVE_ONDEMAND */ /* diff --git a/vcnet/config.h b/vcnet/config.h index e8e73f6b1..0f1291cc5 100644 --- a/vcnet/config.h +++ b/vcnet/config.h @@ -777,4 +777,10 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } # endif /* __GNUC__ || __STDC_VERSION__ */ #endif /* !HAVE_ABS && !abs */ +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +# define HAVE_ONDEMAND +#else +# undef HAVE_ONDEMAND +#endif + #endif /* !_CUPS_CONFIG_H_ */ diff --git a/xcode/config.h b/xcode/config.h index e16c6667c..a0c5c9b3d 100644 --- a/xcode/config.h +++ b/xcode/config.h @@ -733,4 +733,10 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } # endif /* __GNUC__ || __STDC_VERSION__ */ #endif /* !HAVE_ABS && !abs */ +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +# define HAVE_ONDEMAND +#else +# undef HAVE_ONDEMAND +#endif + #endif /* !_CUPS_CONFIG_H_ */ -- cgit v1.2.3 From 26c14fa6e1b9639d47aca3032337d9fe728ee2dc Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Fri, 27 May 2016 13:07:01 -0700 Subject: Refactor common on-demand socket setup code This really can be in its own function, to avoid duplicating the same code for various init methods. --- scheduler/main.c | 240 ++++++++++++++++++------------------------------------- 1 file changed, 78 insertions(+), 162 deletions(-) diff --git a/scheduler/main.c b/scheduler/main.c index 8b65ae1e4..81e04ce49 100644 --- a/scheduler/main.c +++ b/scheduler/main.c @@ -1823,6 +1823,81 @@ sigterm_handler(int sig) /* I - Signal number */ #if defined(HAVE_ONDEMAND) + +/* + * 'add_ondemand_listener()' - Bind an open fd as a Listener. + */ + +static void +add_ondemand_listener(int fd, /* I - Socket file descriptor */ + int idx) /* I - Listener number, for logging */ +{ + cupsd_listener_t *lis; /* Listeners array */ + http_addr_t addr; /* Address variable */ + socklen_t addrlen; /* Length of address */ + char s[256]; /* String addresss */ + + addrlen = sizeof(addr); + + if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) + { + cupsdLogMessage(CUPSD_LOG_ERROR, + "service_checkin: Unable to get local address for listener #%d: %s", + idx + 1, strerror(errno)); + return; + } + + cupsdLogMessage(CUPSD_LOG_DEBUG, + "service_checkin: Listener #%d at fd %d, \"%s\".", + idx + 1, fd, httpAddrString(&addr, s, sizeof(s))); + + /* + * Try to match the on-demand socket address to one of the listeners... + */ + + for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); + lis; + lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) + if (httpAddrEqual(&lis->address, &addr)) + break; + + /* + * Add a new listener If there's no match... + */ + + if (lis) + { + cupsdLogMessage(CUPSD_LOG_DEBUG, + "service_checkin: Matched existing listener #%d to %s.", + idx + 1, httpAddrString(&(lis->address), s, sizeof(s))); + } + else + { + cupsdLogMessage(CUPSD_LOG_DEBUG, + "service_checkin: Adding new listener #%d for %s.", + idx + 1, httpAddrString(&addr, s, sizeof(s))); + + if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) + { + cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to allocate listener: %s.", strerror(errno)); + exit(EXIT_FAILURE); + return; + } + + cupsArrayAdd(Listeners, lis); + + memcpy(&lis->address, &addr, sizeof(lis->address)); + } + + lis->fd = fd; + lis->on_demand = 1; + +# ifdef HAVE_SSL + if (httpAddrPort(&(lis->address)) == 443) + lis->encryption = HTTP_ENCRYPT_ALWAYS; +# endif /* HAVE_SSL */ +} + /* * 'service_checkin()' - Check-in with launchd and collect the listening fds. */ @@ -1835,10 +1910,6 @@ service_checkin(void) size_t i, /* Looping var */ count; /* Number of listeners */ int *ld_sockets; /* Listener sockets */ - cupsd_listener_t *lis; /* Listeners array */ - http_addr_t addr; /* Address variable */ - socklen_t addrlen; /* Length of address */ - char s[256]; /* String addresss */ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid()); @@ -1862,56 +1933,7 @@ service_checkin(void) for (i = 0; i < count; i ++) { - /* - * Get the launchd socket address... - */ - - addrlen = sizeof(addr); - - if (getsockname(ld_sockets[i], (struct sockaddr *)&addr, &addrlen)) - { - cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get local address for listener #%d: %s", (int)i + 1, strerror(errno)); - continue; - } - - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Listener #%d at fd %d, \"%s\".", (int)i + 1, ld_sockets[i], httpAddrString(&addr, s, sizeof(s))); - - for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); - lis; - lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) - if (httpAddrEqual(&lis->address, &addr)) - break; - - /* - * Add a new listener if there's no match... - */ - - if (lis) - { - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Matched existing listener #%d to %s.", (int)i + 1, httpAddrString(&(lis->address), s, sizeof(s))); - } - else - { - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Adding new listener #%d for %s.", (int)i + 1, httpAddrString(&addr, s, sizeof(s))); - - if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) - { - cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to allocate listener: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - cupsArrayAdd(Listeners, lis); - - memcpy(&lis->address, &addr, sizeof(lis->address)); - } - - lis->fd = ld_sockets[i]; - lis->on_demand = 1; - -# ifdef HAVE_SSL - if (httpAddrPort(&(lis->address)) == 443) - lis->encryption = HTTP_ENCRYPT_ALWAYS; -# endif /* HAVE_SSL */ + add_ondemand_listener(ld_sockets[i], i); } free(ld_sockets); @@ -1924,11 +1946,7 @@ service_checkin(void) ld_array, /* Launch data array */ ld_sockets, /* Launch data sockets dictionary */ tmp; /* Launch data */ - cupsd_listener_t *lis; /* Listeners array */ - http_addr_t addr; /* Address variable */ - socklen_t addrlen; /* Length of address */ int fd; /* File descriptor */ - char s[256]; /* String addresss */ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid()); @@ -2000,56 +2018,7 @@ service_checkin(void) if ((tmp = launch_data_array_get_index(ld_array, i)) != NULL) { fd = launch_data_get_fd(tmp); - addrlen = sizeof(addr); - - if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) - { - cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get local address for listener #%d: %s", (int)i + 1, strerror(errno)); - continue; - } - - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Listener #%d at fd %d, \"%s\".", (int)i + 1, fd, httpAddrString(&addr, s, sizeof(s))); - - /* - * Try to match the launchd socket address to one of the listeners... - */ - - for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); - lis; - lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) - if (httpAddrEqual(&lis->address, &addr)) - break; - - /* - * Add a new listener If there's no match... - */ - - if (lis) - { - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Matched existing listener #%d to %s.", (int)i + 1, httpAddrString(&(lis->address), s, sizeof(s))); - } - else - { - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Adding new listener #%d for %s.", (int)i + 1, httpAddrString(&addr, s, sizeof(s))); - - if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) - { - cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to allocate listener: %s.", strerror(errno)); - exit(EXIT_FAILURE); - } - - cupsArrayAdd(Listeners, lis); - - memcpy(&lis->address, &addr, sizeof(lis->address)); - } - - lis->fd = fd; - lis->on_demand = 1; - -# ifdef HAVE_SSL - if (httpAddrPort(&(lis->address)) == 443) - lis->encryption = HTTP_ENCRYPT_ALWAYS; -# endif /* HAVE_SSL */ + add_ondemand_listener(fd, i); } } } @@ -2060,10 +2029,6 @@ service_checkin(void) # else /* HAVE_SYSTEMD */ int i, /* Looping var */ count; /* Number of listeners */ - cupsd_listener_t *lis; /* Listeners array */ - http_addr_t addr; /* Address variable */ - socklen_t addrlen; /* Length of address */ - char s[256]; /* String addresss */ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid()); @@ -2087,56 +2052,7 @@ service_checkin(void) for (i = 0; i < count; i ++) { - /* - * Get the launchd socket address... - */ - - addrlen = sizeof(addr); - - if (getsockname(SD_LISTEN_FDS_START + i, (struct sockaddr *)&addr, &addrlen)) - { - cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get local address for listener #%d: %s", (int)i + 1, strerror(errno)); - continue; - } - - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Listener #%d at fd %d, \"%s\".", (int)i + 1, SD_LISTEN_FDS_START + i, httpAddrString(&addr, s, sizeof(s))); - - for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); - lis; - lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) - if (httpAddrEqual(&lis->address, &addr)) - break; - - /* - * Add a new listener if there's no match... - */ - - if (lis) - { - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Matched existing listener #%d to %s.", (int)i + 1, httpAddrString(&(lis->address), s, sizeof(s))); - } - else - { - cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: Adding new listener #%d for %s.", (int)i + 1, httpAddrString(&addr, s, sizeof(s))); - - if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) - { - cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to allocate listener: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - cupsArrayAdd(Listeners, lis); - - memcpy(&lis->address, &addr, sizeof(lis->address)); - } - - lis->fd = SD_LISTEN_FDS_START + i; - lis->on_demand = 1; - -# ifdef HAVE_SSL - if (httpAddrPort(&(lis->address)) == 443) - lis->encryption = HTTP_ENCRYPT_ALWAYS; -# endif /* HAVE_SSL */ + add_ondemand_listener(SD_LISTEN_FDS_START + i, i); } # endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */ } -- cgit v1.2.3 From 441de8b2e031703feec2d8e7bacab0e1223e818a Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Fri, 27 May 2016 13:07:05 -0700 Subject: support Upstart socket activation Inspired by code here: https://bugs.launchpad.net/upstart/+bug/1276713 With significant refactoring and bug-fixing. Notably, Upstart only supports passing a single file descriptor via UPSTART_FDS, so systems that want to use this support will need to configure CUPS appropriately in order to use this. --- config-scripts/cups-startup.m4 | 9 +++++++ config.h.in | 8 +++++- configure | 25 +++++++++++++++---- scheduler/client.h | 2 +- scheduler/main.c | 56 +++++++++++++++++++++++++++++++++++++----- vcnet/config.h | 2 +- xcode/config.h | 2 +- 7 files changed, 89 insertions(+), 15 deletions(-) diff --git a/config-scripts/cups-startup.m4 b/config-scripts/cups-startup.m4 index bbd6b891d..051339a5d 100644 --- a/config-scripts/cups-startup.m4 +++ b/config-scripts/cups-startup.m4 @@ -85,6 +85,15 @@ if test x$enable_systemd != xno; then fi fi +dnl Upstart is also used on Linux (e.g., Chrome OS) +AC_ARG_ENABLE(upstart, [ --enable-upstart enable upstart support]) +if test "x$enable_upstart" = "xyes"; then + if test "x$have_systemd" = "xyes"; then + AC_MSG_ERROR(Cannot support both systemd and upstart.) + fi + AC_DEFINE(HAVE_UPSTART) +fi + dnl Solaris uses smf SMFMANIFESTDIR="" AC_SUBST(SMFMANIFESTDIR) diff --git a/config.h.in b/config.h.in index 526f91a5e..918d90f08 100644 --- a/config.h.in +++ b/config.h.in @@ -494,6 +494,12 @@ #undef HAVE_SYSTEMD +/* + * Do we have upstart support? + */ + +#undef HAVE_UPSTART + /* * Various scripting languages... @@ -717,7 +723,7 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } # endif /* __GNUC__ || __STDC_VERSION__ */ #endif /* !HAVE_ABS && !abs */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) || defined(HAVE_UPSTART) # define HAVE_ONDEMAND #else # undef HAVE_ONDEMAND diff --git a/configure b/configure index c3af70dd1..96adade7d 100755 --- a/configure +++ b/configure @@ -878,6 +878,7 @@ with_dnssd_includes enable_launchd enable_systemd with_systemd +enable_upstart with_smfmanifestdir with_rcdir with_rclevels @@ -1560,6 +1561,7 @@ Optional Features: --disable-dnssd disable DNS Service Discovery support using mDNSResponder --disable-launchd disable launchd support --disable-systemd disable systemd support + --enable-upstart enable upstart support --enable-page-logging enable page_log by default --disable-browsing disable Browsing by default --disable-default-shared @@ -8750,7 +8752,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -8796,7 +8798,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -8820,7 +8822,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -8865,7 +8867,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -8889,7 +8891,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -9210,6 +9212,19 @@ fi fi fi +# Check whether --enable-upstart was given. +if test "${enable_upstart+set}" = set; then : + enableval=$enable_upstart; +fi + +if test "x$enable_upstart" = "xyes"; then + if test "x$have_systemd" = "xyes"; then + as_fn_error $? "Cannot support both systemd and upstart." "$LINENO" 5 + fi + $as_echo "#define HAVE_UPSTART 1" >>confdefs.h + +fi + SMFMANIFESTDIR="" diff --git a/scheduler/client.h b/scheduler/client.h index 174b7271e..f97f41def 100644 --- a/scheduler/client.h +++ b/scheduler/client.h @@ -80,7 +80,7 @@ typedef struct http_addr_t address; /* Bind address of socket */ http_encryption_t encryption; /* To encrypt or not to encrypt... */ #if defined(HAVE_ONDEMAND) - int on_demand; /* Is this a socket from launchd/systemd? */ + int on_demand; /* Is this a socket from launchd/systemd/upstart? */ #endif /* HAVE_ONDEMAND */ } cupsd_listener_t; diff --git a/scheduler/main.c b/scheduler/main.c index 81e04ce49..8d07e5396 100644 --- a/scheduler/main.c +++ b/scheduler/main.c @@ -30,8 +30,6 @@ #ifdef HAVE_LAUNCH_H # include # include -# define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd" - /* Name of the launchd KeepAlive file */ # ifdef HAVE_LAUNCH_ACTIVATE_SOCKET /* Update when we have a public header we can include */ extern int launch_activate_socket(const char *name, int **fds, size_t *cnt); @@ -40,10 +38,13 @@ extern int launch_activate_socket(const char *name, int **fds, size_t *cnt); #ifdef HAVE_SYSTEMD # include -# define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd" - /* Name of the systemd path file */ #endif /* HAVE_SYSTEMD */ +#ifdef HAVE_ONDEMAND +# define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd" + /* Name of the KeepAlive file */ +#endif + #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO) # include #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */ @@ -242,7 +243,7 @@ main(int argc, /* I - Number of command-line args */ usage(0); break; - case 'l' : /* Started by launchd/systemd... */ + case 'l' : /* Started by launchd/systemd/upstart... */ #if defined(HAVE_ONDEMAND) OnDemand = 1; fg = 1; @@ -2026,7 +2027,7 @@ service_checkin(void) launch_data_free(ld_msg); launch_data_free(ld_resp); -# else /* HAVE_SYSTEMD */ +# elif defined(HAVE_SYSTEMD) int i, /* Looping var */ count; /* Number of listeners */ @@ -2054,6 +2055,49 @@ service_checkin(void) { add_ondemand_listener(SD_LISTEN_FDS_START + i, i); } +# elif defined(HAVE_UPSTART) + const char *e; /* Environment var */ + int fd; /* File descriptor */ + + if (!(e = getenv("UPSTART_EVENTS"))) + { + cupsdLogMessage(CUPSD_LOG_ERROR, + "service_checkin: We did not get started via Upstart."); + exit(EXIT_FAILURE); + return; + } + + if (strcasecmp(e, "socket")) + { + cupsdLogMessage(CUPSD_LOG_ERROR, + "service_checkin: We did not get triggered via an Upstart socket event."); + exit(EXIT_FAILURE); + return; + } + + if (!(e = getenv("UPSTART_FDS"))) + { + cupsdLogMessage(CUPSD_LOG_ERROR, + "service_checkin: Unable to get listener sockets from UPSTART_FDS."); + exit(EXIT_FAILURE); + return; + } + + cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: UPSTART_FDS=%s", e); + + fd = strtol(e, NULL, 10); + if (fd < 0) { + cupsdLogMessage(CUPSD_LOG_ERROR, + "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno)); + exit(EXIT_FAILURE); + return; + } + + /* Upstart only supportst a single on-demand socket fd */ + add_ondemand_listener(fd, 0); + +# else +# error "Error: defined HAVE_ONDEMAND but no launchd/systemd/upstart selection" # endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */ } diff --git a/vcnet/config.h b/vcnet/config.h index 0f1291cc5..a4b01138c 100644 --- a/vcnet/config.h +++ b/vcnet/config.h @@ -777,7 +777,7 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } # endif /* __GNUC__ || __STDC_VERSION__ */ #endif /* !HAVE_ABS && !abs */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) || defined(HAVE_UPSTART) # define HAVE_ONDEMAND #else # undef HAVE_ONDEMAND diff --git a/xcode/config.h b/xcode/config.h index a0c5c9b3d..7e871a0d0 100644 --- a/xcode/config.h +++ b/xcode/config.h @@ -733,7 +733,7 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); } # endif /* __GNUC__ || __STDC_VERSION__ */ #endif /* !HAVE_ABS && !abs */ -#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) +#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD) || defined(HAVE_UPSTART) # define HAVE_ONDEMAND #else # undef HAVE_ONDEMAND -- cgit v1.2.3