#ifdef HAVE_ONDEMAND
# define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
/* Name of the KeepAlive file */
-#endif
+#endif /* HAVE_ONDEMAND */
#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
# include <malloc.h>
static void sighup_handler(int sig);
static void sigterm_handler(int sig);
static long select_timeout(int fds);
-#ifdef HAVE_ONDEMAND
static void service_checkin(void);
-static void service_checkout(void);
-#endif /* HAVE_ONDEMAND */
+static void service_checkout(int shutdown);
static void usage(int status) __attribute__((noreturn));
break;
case 'l' : /* Started by launchd/systemd/upstart... */
-#if defined(HAVE_ONDEMAND)
+#ifdef HAVE_ONDEMAND
OnDemand = 1;
fg = 1;
close_all = 0;
cupsdCleanFiles(CacheDir, "*.ipp");
-#if defined(HAVE_ONDEMAND)
- if (OnDemand)
- {
- /*
- * If we were started on demand by launchd or systemd get the listen sockets
- * file descriptors...
- */
+ /*
+ * If we were started on demand by launchd or systemd get the listen sockets
+ * file descriptors...
+ */
- service_checkin();
- service_checkout();
- }
-#endif /* HAVE_ONDEMAND */
+ service_checkin();
+ service_checkout(0);
/*
* Startup the server...
* Send server-started event...
*/
-#if defined(HAVE_ONDEMAND)
+#ifdef HAVE_ONDEMAND
if (OnDemand)
cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started on demand.");
else
cupsdStopServer();
-#ifdef HAVE_ONDEMAND
/*
- * Update the keep-alive file as needed...
+ * Update the KeepAlive/PID file as needed...
*/
- if (OnDemand)
- service_checkout();
-#endif /* HAVE_ONDEMAND */
+ service_checkout(1);
/*
* Stop all jobs...
lis->encryption = HTTP_ENCRYPT_ALWAYS;
# endif /* HAVE_SSL */
}
+#endif /* HAVE_ONDEMAND */
/*
static void
service_checkin(void)
{
-# ifdef HAVE_LAUNCHD
- int error; /* Check-in error, if any */
- size_t i, /* Looping var */
- count; /* Number of listeners */
- int *ld_sockets; /* Listener sockets */
-
-
cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid());
- /*
- * Check-in with launchd...
- */
-
- if ((error = launch_activate_socket("Listeners", &ld_sockets, &count)) != 0)
+#ifdef HAVE_LAUNCHD
+ if (OnDemand)
{
- cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(error));
- exit(EXIT_FAILURE);
- return; /* anti-compiler-warning */
- }
-
- /*
- * Try to match the launchd sockets to the cupsd listeners...
- */
+ int error; /* Check-in error, if any */
+ size_t i, /* Looping var */
+ count; /* Number of listeners */
+ int *ld_sockets; /* Listener sockets */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count);
-
- for (i = 0; i < count; i ++)
- service_add_listener(ld_sockets[i], (int)i);
-
- free(ld_sockets);
+ /*
+ * Check-in with launchd...
+ */
-# elif defined(HAVE_SYSTEMD)
- int i, /* Looping var */
- count; /* Number of listeners */
+ if ((error = launch_activate_socket("Listeners", &ld_sockets, &count)) != 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(error));
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
+ /*
+ * Try to match the launchd sockets to the cupsd listeners...
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid());
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count);
- /*
- * Check-in with systemd...
- */
+ for (i = 0; i < count; i ++)
+ service_add_listener(ld_sockets[i], (int)i);
- if ((count = sd_listen_fds(0)) < 0)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(-count));
- exit(EXIT_FAILURE);
- return; /* anti-compiler-warning */
+ free(ld_sockets);
}
- /*
- * Try to match the systemd sockets to the cupsd listeners...
- */
+#elif defined(HAVE_SYSTEMD)
+ if (OnDemand)
+ {
+ int i, /* Looping var */
+ count; /* Number of listeners */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", count);
+ /*
+ * Check-in with systemd...
+ */
- for (i = 0; i < count; i ++)
- service_add_listener(SD_LISTEN_FDS_START + i, i);
+ if ((count = sd_listen_fds(0)) < 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(-count));
+ exit(EXIT_FAILURE);
+ return; /* anti-compiler-warning */
+ }
-# elif defined(HAVE_UPSTART)
- const char *e; /* Environment var */
- int fd; /* File descriptor */
+ /*
+ * Try to match the systemd sockets to the cupsd listeners...
+ */
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", count);
- if (!(e = getenv("UPSTART_EVENTS")))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get started via Upstart.");
- exit(EXIT_FAILURE);
- return;
+ for (i = 0; i < count; i ++)
+ service_add_listener(SD_LISTEN_FDS_START + i, i);
}
- if (strcasecmp(e, "socket"))
+#elif defined(HAVE_UPSTART)
+ if (OnDemand)
{
- cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get triggered via an Upstart socket event.");
- exit(EXIT_FAILURE);
- return;
- }
+ const char *e; /* Environment var */
+ int fd; /* File descriptor */
- if ((e = getenv("UPSTART_FDS")) == NULL)
- {
- 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);
+ if (!(e = getenv("UPSTART_EVENTS")))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get started via Upstart.");
+ exit(EXIT_FAILURE);
+ return;
+ }
- fd = (int)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;
- }
+ if (strcasecmp(e, "socket"))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get triggered via an Upstart socket event.");
+ exit(EXIT_FAILURE);
+ return;
+ }
- /*
- * Upstart only supportst a single on-demand socket file descriptor...
- */
+ if ((e = getenv("UPSTART_FDS")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets from UPSTART_FDS.");
+ exit(EXIT_FAILURE);
+ return;
+ }
- service_add_listener(fd, 0);
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: UPSTART_FDS=%s", e);
-# else
-# error "Error: defined HAVE_ONDEMAND but no launchd/systemd/upstart selection"
-# endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
+ fd = (int)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 file descriptor...
+ */
+
+ service_add_listener(fd, 0);
+ }
+#endif /* HAVE_LAUNCHD */
}
/*
- * 'service_checkout()' - Update the CUPS_KEEPALIVE file as needed.
+ * 'service_checkout()' - Update the KeepAlive/PID file as needed.
*/
static void
-service_checkout(void)
+service_checkout(int shutdown) /* I - Shutting down? */
{
- int fd; /* File descriptor */
+ cups_file_t *fp; /* File */
+ char pidfile[1024]; /* PID/KeepAlive file */
+
+
+ /*
+ * When running on-demand, use the KeepAlive file, otherwise write a PID file
+ * to StateDir...
+ */
+#ifdef HAVE_ONDEMAND
+ if (OnDemand)
+ strlcpy(pidfile, CUPS_KEEPALIVE, sizeof(pidfile));
+ else
+#endif /* HAVE_ONDEMAND */
+ snprintf(pidfile, sizeof(pidfile), "%s/cupsd.pid", StateDir);
/*
* Create or remove the "keep-alive" file based on whether there are active
* jobs or shared printers to advertise...
*/
- if (cupsArrayCount(ActiveJobs) || /* Active jobs */
+ if (!OnDemand || /* Not running on-demand */
+ cupsArrayCount(ActiveJobs) || /* Active jobs */
WebInterface || /* Web interface enabled */
NeedReload || /* Doing a reload */
(Browsing && BrowseLocalProtocols && cupsArrayCount(Printers)))
/* Printers being shared */
{
- cupsdLogMessage(CUPSD_LOG_DEBUG, "Creating keep-alive file \"" CUPS_KEEPALIVE "\".");
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Creating KeepAlive/PID file \"%s\".", pidfile);
- if ((fd = open(CUPS_KEEPALIVE, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR)) >= 0)
- close(fd);
+ if ((fp = cupsFileOpen(pidfile, "w")) != NULL)
+ {
+ /*
+ * Save the PID in the file...
+ */
+
+ cupsFilePrintf(fp, "%d\n", (int)getpid());
+ cupsFileClose(fp);
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create KeepAlive/PID file \"%s\": %s", pidfile, strerror(errno));
}
- else
+ else if (shutdown)
{
- cupsdLogMessage(CUPSD_LOG_DEBUG, "Removing keep-alive file \"" CUPS_KEEPALIVE "\".");
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Removing KeepAlive/PID file \"%s\".", pidfile);
- unlink(CUPS_KEEPALIVE);
+ unlink(pidfile);
}
}
-#endif /* HAVE_ONDEMAND */
/*