#ifdef HAVE_LAUNCH_H
# include <launch.h>
# include <libgen.h>
-# 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);
#ifdef HAVE_SYSTEMD
# include <systemd/sd-daemon.h>
-# 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 <malloc.h>
#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
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));
#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
usage(0);
break;
- case 'l' : /* Started by launchd/systemd... */
-#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+ case 'l' : /* Started by launchd/systemd/upstart... */
+#if defined(HAVE_ONDEMAND)
OnDemand = 1;
fg = 1;
close_all = 0;
fg = 0;
disconnect = 1;
close_all = 1;
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
break;
case 'p' : /* Stop immediately for profiling */
cupsdCleanFiles(CacheDir, "*.ipp");
-#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+#if defined(HAVE_ONDEMAND)
if (OnDemand)
{
/*
service_checkin();
service_checkout();
}
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
/*
* Startup the server...
* 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
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
}
else
service_idle_exit = 0;
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
if ((fds = cupsdDoSelect(timeout)) < 0)
{
}
#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...
stop_scheduler = 1;
break;
}
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
/*
* Resume listening for new connections as needed...
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...
}
-#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+#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.
*/
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());
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);
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());
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);
}
}
}
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 */
- 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());
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_ondemand_listener(SD_LISTEN_FDS_START + i, i);
+ }
+# elif defined(HAVE_UPSTART)
+ const char *e; /* Environment var */
+ int fd; /* File descriptor */
- /*
- * Add a new listener if there's no match...
- */
+ if (!(e = getenv("UPSTART_EVENTS")))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: We did not get started via Upstart.");
+ exit(EXIT_FAILURE);
+ return;
+ }
- 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 (strcasecmp(e, "socket"))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: We did not get triggered via an Upstart socket event.");
+ exit(EXIT_FAILURE);
+ return;
+ }
- 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);
- }
+ if (!(e = getenv("UPSTART_FDS")))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "service_checkin: Unable to get listener sockets from UPSTART_FDS.");
+ exit(EXIT_FAILURE);
+ return;
+ }
- cupsArrayAdd(Listeners, lis);
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: UPSTART_FDS=%s", e);
- memcpy(&lis->address, &addr, sizeof(lis->address));
- }
+ 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;
+ }
- lis->fd = SD_LISTEN_FDS_START + i;
- lis->on_demand = 1;
+ /* Upstart only supportst a single on-demand socket fd */
+ add_ondemand_listener(fd, 0);
-# ifdef HAVE_SSL
- if (httpAddrPort(&(lis->address)) == 443)
- lis->encryption = HTTP_ENCRYPT_ALWAYS;
-# endif /* HAVE_SSL */
- }
+# else
+# error "Error: defined HAVE_ONDEMAND but no launchd/systemd/upstart selection"
# endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
}
unlink(CUPS_KEEPALIVE);
}
}
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
/*