]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
The scheduler now creates a PID file when not running on demand with a modern
authorMichael Sweet <michael.r.sweet@gmail.com>
Tue, 1 Aug 2017 23:16:25 +0000 (19:16 -0400)
committerMichael Sweet <michael.r.sweet@gmail.com>
Tue, 1 Aug 2017 23:16:25 +0000 (19:16 -0400)
service launcher (Issue #5080)

CHANGES.md
scheduler/main.c

index 1ed21316782317efd68b05f16fbdebbe28191a47..be246c4830cae301101404d6ab23811290ea912c 100644 (file)
@@ -1,4 +1,4 @@
-CHANGES - 2.2.5 - 2017-07-31
+CHANGES - 2.2.5 - 2017-08-01
 ============================
 
 CHANGES IN CUPS V2.2.5
@@ -24,6 +24,8 @@ CHANGES IN CUPS V2.2.5
 - Fixed the default `ServerAlias` value from `ServerName` (Issue #5072)
 - Fixed the adminurl field in the TXT record for fully-qualified `ServerName`
   values (Issue #5074)
+- The scheduler now creates a PID file when not running on demand with a modern
+  service launcher (Issue #5080)
 - Fixed an issue with Chinese localizations on macOS (rdar://32419311)
 - The IPP backend now always sends the "finishings" attribute for printers that
   support it because otherwise the client cannot override printer defaults
index a8cd30e8eeb536d64f4c8951f5b3b9ca49aab21b..49b7f18bcc39e70671909789710747471b6067f4 100644 (file)
@@ -38,7 +38,7 @@
 #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>
@@ -67,10 +67,8 @@ static void          sigchld_handler(int sig);
 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));
 
 
@@ -241,7 +239,7 @@ main(int  argc,                             /* I - Number of command-line args */
              break;
 
           case 'l' : /* Started by launchd/systemd/upstart... */
-#if defined(HAVE_ONDEMAND)
+#ifdef HAVE_ONDEMAND
              OnDemand   = 1;
              fg         = 1;
              close_all  = 0;
@@ -582,18 +580,13 @@ main(int  argc,                           /* I - Number of command-line args */
 
   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...
@@ -680,7 +673,7 @@ main(int  argc,                             /* I - Number of command-line args */
   * 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
@@ -1147,14 +1140,11 @@ main(int  argc,                         /* I - Number of command-line args */
 
   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...
@@ -1904,6 +1894,7 @@ service_add_listener(int fd,              /* I - Socket file descriptor */
     lis->encryption = HTTP_ENCRYPT_ALWAYS;
 #  endif /* HAVE_SSL */
 }
+#endif /* HAVE_ONDEMAND */
 
 
 /*
@@ -1913,146 +1904,170 @@ service_add_listener(int fd,          /* I - Socket file descriptor */
 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 */
 
 
 /*