From 441de8b2e031703feec2d8e7bacab0e1223e818a Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Fri, 27 May 2016 13:07:05 -0700 Subject: [PATCH] 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 bbd6b891db..051339a5d0 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 526f91a5eb..918d90f081 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 c3af70dd1c..96adade7d3 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 174b7271e7..f97f41defa 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 81e04ce499..8d07e53968 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 0f1291cc59..a4b01138c2 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 a0c5c9b3d4..7e871a0d03 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 -- 2.47.3