]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/main.c
Add systemd support to cupsd (STR #3282)
[thirdparty/cups.git] / scheduler / main.c
index f847e275c8ec7cd6976c9b9d5f1ac90594a3a659..555b6d6d818e1a11c2b5f188fc1d50a30f94db96 100644 (file)
@@ -34,6 +34,11 @@ extern int launch_activate_socket(const char *name, int **fds, size_t *cnt);
 #  endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
 #endif /* HAVE_LAUNCH_H */
 
+#ifdef HAVE_SYSTEMD
+#  define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
+                                       /* Name of the systemd path file */
+#endif /* HAVE_SYSTEMD */
+
 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
 #  include <malloc.h>
 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
@@ -51,16 +56,16 @@ extern int launch_activate_socket(const char *name, int **fds, size_t *cnt);
  * Local functions...
  */
 
-#ifdef HAVE_LAUNCHD
-static void            launchd_checkin(void);
-static void            launchd_checkout(void);
-#endif /* HAVE_LAUNCHD */
 static void            parent_handler(int sig);
 static void            process_children(void);
 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)
+static void            service_checkin(void);
+static void            service_checkout(void);
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 static void            usage(int status) __attribute__((noreturn));
 
 
@@ -114,10 +119,10 @@ main(int  argc,                           /* I - Number of command-line args */
 #else
   time_t               netif_time = 0; /* Time since last network update */
 #endif /* __APPLE__ */
-#if HAVE_LAUNCHD
-  int                  launchd_idle_exit;
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+  int                  service_idle_exit;
                                        /* Idle exit on select timeout? */
-#endif /* HAVE_LAUNCHD */
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 
 
 #ifdef HAVE_GETEUID
@@ -141,8 +146,8 @@ main(int  argc,                             /* I - Number of command-line args */
 #ifdef HAVE_LAUNCHD
   if (getenv("CUPSD_LAUNCHD"))
   {
-    Launchd = 1;
-    fg      = 1;
+    OnDemand = 1;
+    fg       = 1;
   }
 #endif /* HAVE_LAUNCHD */
 
@@ -219,15 +224,15 @@ main(int  argc,                           /* I - Number of command-line args */
              usage(0);
              break;
 
-          case 'l' : /* Started by launchd... */
-#ifdef HAVE_LAUNCHD
-             Launchd = 1;
-             fg      = 1;
+          case 'l' : /* Started by launchd/systemd... */
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+             OnDemand = 1;
+             fg       = 1;
 #else
-             _cupsLangPuts(stderr, _("cupsd: launchd(8) support not compiled "
+             _cupsLangPuts(stderr, _("cupsd: On-demand support not compiled "
                                      "in, running in normal mode."));
               fg = 0;
-#endif /* HAVE_LAUNCHD */
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
              break;
 
           case 'p' : /* Stop immediately for profiling */
@@ -531,17 +536,18 @@ main(int  argc,                           /* I - Number of command-line args */
 
   cupsdCleanFiles(CacheDir, "*.ipp");
 
-#if HAVE_LAUNCHD
-  if (Launchd)
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+  if (OnDemand)
   {
    /*
-    * If we were started by launchd get the listen sockets file descriptors...
+    * If we were started on demand by launchd or systemd get the listen sockets
+    * file descriptors...
     */
 
-    launchd_checkin();
-    launchd_checkout();
+    service_checkin();
+    service_checkout();
   }
-#endif /* HAVE_LAUNCHD */
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 
  /*
   * Startup the server...
@@ -628,18 +634,15 @@ main(int  argc,                           /* I - Number of command-line args */
   * Send server-started event...
   */
 
-#ifdef HAVE_LAUNCHD
-  if (Launchd)
-    cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL,
-                  "Scheduler started via launchd.");
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+  if (OnDemand)
+    cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started on demand.");
   else
-#endif /* HAVE_LAUNCHD */
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
   if (fg)
-    cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL,
-                  "Scheduler started in foreground.");
+    cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in foreground.");
   else
-    cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL,
-                  "Scheduler started in background.");
+    cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in background.");
 
  /*
   * Start any pending print jobs...
@@ -718,18 +721,18 @@ main(int  argc,                           /* I - Number of command-line args */
           break;
        }
 
-#if HAVE_LAUNCHD
-       if (Launchd)
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+       if (OnDemand)
        {
         /*
-         * If we were started by launchd, get the listen socket file
+         * If we were started by launchd or systemd, get the listen socket file
          * descriptors...
          */
 
-         launchd_checkin();
-         launchd_checkout();
+         service_checkin();
+         service_checkout();
        }
-#endif /* HAVE_LAUNCHD */
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 
        /*
         * Startup the server...
@@ -760,23 +763,23 @@ main(int  argc,                           /* I - Number of command-line args */
     if ((timeout = select_timeout(fds)) > 1 && LastEvent)
       timeout = 1;
 
-#if HAVE_LAUNCHD
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
    /*
     * If no other work is scheduled and we're being controlled by
     * launchd then timeout after 'LaunchdTimeout' seconds of
     * inactivity...
     */
 
-    if (timeout == 86400 && Launchd && LaunchdTimeout &&
+    if (timeout == 86400 && OnDemand && IdleExitTimeout &&
         !cupsArrayCount(ActiveJobs) &&
        (!Browsing || !BrowseLocalProtocols || !cupsArrayCount(Printers)))
     {
-      timeout          = LaunchdTimeout;
-      launchd_idle_exit = 1;
+      timeout          = IdleExitTimeout;
+      service_idle_exit = 1;
     }
     else
-      launchd_idle_exit = 0;
-#endif /* HAVE_LAUNCHD */
+      service_idle_exit = 0;
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 
     if ((fds = cupsdDoSelect(timeout)) < 0)
     {
@@ -873,13 +876,13 @@ main(int  argc,                           /* I - Number of command-line args */
     }
 #endif /* !__APPLE__ */
 
-#if HAVE_LAUNCHD
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
    /*
     * If no other work was scheduled and we're being controlled by launchd
     * then timeout after 'LaunchdTimeout' seconds of inactivity...
     */
 
-    if (!fds && launchd_idle_exit)
+    if (!fds && service_idle_exit)
     {
       cupsdLogMessage(CUPSD_LOG_INFO,
                       "Printer sharing is off and there are no jobs pending, "
@@ -887,7 +890,7 @@ main(int  argc,                             /* I - Number of command-line args */
       stop_scheduler = 1;
       break;
     }
-#endif /* HAVE_LAUNCHD */
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 
    /*
     * Resume listening for new connections as needed...
@@ -941,8 +944,7 @@ main(int  argc,                             /* I - Number of command-line args */
       * Process pending data in the input buffer...
       */
 
-      // TODO: Use httpGetReady()
-      if (con->http->used)
+      if (httpGetReady(con->http))
       {
         cupsdReadClient(con);
        continue;
@@ -952,13 +954,10 @@ main(int  argc,                           /* I - Number of command-line args */
       * Check the activity and close old clients...
       */
 
-      // TODO: Use httpGetActivity()
       activity = current_time - Timeout;
-      if (con->http->activity < activity && !con->pipe_pid)
+      if (httpGetActivity(con->http) < activity && !con->pipe_pid)
       {
-        cupsdLogMessage(CUPSD_LOG_DEBUG,
-                       "Closing client %d after %d seconds of inactivity...",
-                       con->number, Timeout);
+        cupsdLogMessage(CUPSD_LOG_DEBUG, "Closing client %d after %d seconds of inactivity.", con->number, Timeout);
 
         cupsdCloseClient(con);
         continue;
@@ -1095,14 +1094,14 @@ main(int  argc,                         /* I - Number of command-line args */
 
   cupsdStopServer();
 
-#ifdef HAVE_LAUNCHD
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
  /*
-  * Update the launchd KeepAlive file as needed...
+  * Update the keep-alive file as needed...
   */
 
-  if (Launchd)
-    launchd_checkout();
-#endif /* HAVE_LAUNCHD */
+  if (OnDemand)
+    service_checkout();
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
 
  /*
   * Stop all jobs...
@@ -1292,278 +1291,6 @@ cupsdSetStringf(char       **s,         /* O - New string */
 }
 
 
-#ifdef HAVE_LAUNCHD
-/*
- * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
- */
-
-static void
-launchd_checkin(void)
-{
-#  ifdef HAVE_LAUNCH_ACTIVATE_SOCKET
-  int                  error;          /* Check-in error, if any */
-  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, "launchd_checkin: pid=%d", (int)getpid());
-
- /*
-  * Check-in with launchd...
-  */
-
-  if ((error = launch_activate_socket("Listeners", &ld_sockets, &count)) != 0)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR, "launchd_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, "launchd_checkin: %d listeners.", (int)count);
-
-  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, "launchd_checkin: Unable to get local address for listener #%d: %s", (int)i + 1, strerror(errno));
-      continue;
-    }
-
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_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, "launchd_checkin: Matched existing listener #%d to %s.", (int)i + 1, httpAddrString(&(lis->address), s, sizeof(s)));
-    }
-    else
-    {
-      cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_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, "launchd_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];
-
-#    ifdef HAVE_SSL
-    if (httpAddrPort(&(lis->address)) == 443)
-      lis->encryption = HTTP_ENCRYPT_ALWAYS;
-#    endif /* HAVE_SSL */
-  }
-
-#  else
-  size_t               i,              /* Looping var */
-                       count;          /* Number of listeners */
-  launch_data_t                ld_msg,         /* Launch data message */
-                       ld_resp,        /* Launch data response */
-                       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, "launchd_checkin: pid=%d", (int)getpid());
-
- /*
-  * Check-in with launchd...
-  */
-
-  ld_msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);
-  if ((ld_resp = launch_msg(ld_msg)) == NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                   "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
-                   "\") IPC failure");
-    exit(EXIT_FAILURE);
-    return; /* anti-compiler-warning */
-  }
-
-  if (launch_data_get_type(ld_resp) == LAUNCH_DATA_ERRNO)
-  {
-    errno = launch_data_get_errno(ld_resp);
-    cupsdLogMessage(CUPSD_LOG_ERROR, "launchd_checkin: Check-in failed: %s",
-                    strerror(errno));
-    exit(EXIT_FAILURE);
-    return; /* anti-compiler-warning */
-  }
-
- /*
-  * Get the sockets dictionary...
-  */
-
-  if ((ld_sockets = launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_SOCKETS))
-          == NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "launchd_checkin: No sockets found to answer requests on.");
-    exit(EXIT_FAILURE);
-    return; /* anti-compiler-warning */
-  }
-
- /*
-  * Get the array of listener sockets...
-  */
-
-  if ((ld_array = launch_data_dict_lookup(ld_sockets, "Listeners")) == NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "launchd_checkin: No sockets found to answer requests on.");
-    exit(EXIT_FAILURE);
-    return; /* anti-compiler-warning */
-  }
-
- /*
-  * Add listening fd(s) to the Listener array...
-  */
-
-  if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY)
-  {
-    count = launch_data_array_get_count(ld_array);
-
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: %d listeners.", (int)count);
-
-    for (i = 0; i < count; i ++)
-    {
-     /*
-      * Get the launchd file descriptor and address...
-      */
-
-      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, "launchd_checkin: Unable to get local address for listener #%d: %s", (int)i + 1, strerror(errno));
-         continue;
-       }
-
-        cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_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, "launchd_checkin: Matched existing listener #%d to %s.", (int)i + 1, httpAddrString(&(lis->address), s, sizeof(s)));
-       }
-       else
-       {
-         cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_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, "launchd_checkin: Unable to allocate listener: %s.", strerror(errno));
-           exit(EXIT_FAILURE);
-         }
-
-         cupsArrayAdd(Listeners, lis);
-
-         memcpy(&lis->address, &addr, sizeof(lis->address));
-       }
-
-       lis->fd = fd;
-
-#    ifdef HAVE_SSL
-       if (httpAddrPort(&(lis->address)) == 443)
-         lis->encryption = HTTP_ENCRYPT_ALWAYS;
-#    endif /* HAVE_SSL */
-      }
-    }
-  }
-
-  launch_data_free(ld_msg);
-  launch_data_free(ld_resp);
-#  endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
-}
-
-
-/*
- * 'launchd_checkout()' - Update the launchd KeepAlive file as needed.
- */
-
-static void
-launchd_checkout(void)
-{
-  int  fd;                             /* File descriptor */
-
-
- /*
-  * Create or remove the launchd KeepAlive file based on whether
-  * there are active jobs, polling, browsing for remote printers or
-  * shared printers to advertise...
-  */
-
-  if (cupsArrayCount(ActiveJobs) ||
-      (Browsing && BrowseLocalProtocols && cupsArrayCount(Printers)))
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "Creating launchd keepalive file \"" CUPS_KEEPALIVE
-                    "\"...");
-
-    if ((fd = open(CUPS_KEEPALIVE, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR)) >= 0)
-      close(fd);
-  }
-  else
-  {
-    cupsdLogMessage(CUPSD_LOG_DEBUG,
-                    "Removing launchd keepalive file \"" CUPS_KEEPALIVE
-                    "\"...");
-
-    unlink(CUPS_KEEPALIVE);
-  }
-}
-#endif /* HAVE_LAUNCHD */
-
-
 /*
  * 'parent_handler()' - Catch USR1/CHLD signals...
  */
@@ -1838,11 +1565,10 @@ select_timeout(int fds)                 /* I - Number of descriptors returned */
   * processed; if so, the timeout should be 0...
   */
 
-  // TODO: Use httpGetReady()
   for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
        con;
        con = (cupsd_client_t *)cupsArrayNext(Clients))
-    if (con->http->used > 0)
+    if (httpGetReady(con->http))
       return (0);
 
  /*
@@ -1893,13 +1619,12 @@ select_timeout(int fds)                 /* I - Number of descriptors returned */
   * Check the activity and close old clients...
   */
 
-  // TODO: Use httpGetActivity()
   for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
        con;
        con = (cupsd_client_t *)cupsArrayNext(Clients))
-    if ((con->http->activity + Timeout) < timeout)
+    if ((httpGetActivity(con->http) + Timeout) < timeout)
     {
-      timeout = con->http->activity + Timeout;
+      timeout = httpGetActivity(con->http) + Timeout;
       why     = "timeout a client connection";
     }
 
@@ -2063,6 +1788,357 @@ sigterm_handler(int sig)                /* I - Signal number */
 }
 
 
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+/*
+ * 'service_checkin()' - Check-in with launchd and collect the listening fds.
+ */
+
+static void
+service_checkin(void)
+{
+#  ifdef HAVE_LAUNCH_ACTIVATE_SOCKET
+  int                  error;          /* Check-in error, if any */
+  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());
+
+ /*
+  * Check-in with launchd...
+  */
+
+  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: %d listeners.", (int)count);
+
+  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 */
+  }
+
+#  elif defined(HAVE_LAUNCHD)
+  size_t               i,              /* Looping var */
+                       count;          /* Number of listeners */
+  launch_data_t                ld_msg,         /* Launch data message */
+                       ld_resp,        /* Launch data response */
+                       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());
+
+ /*
+  * Check-in with launchd...
+  */
+
+  ld_msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);
+  if ((ld_resp = launch_msg(ld_msg)) == NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR,
+                   "service_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
+                   "\") IPC failure");
+    exit(EXIT_FAILURE);
+    return; /* anti-compiler-warning */
+  }
+
+  if (launch_data_get_type(ld_resp) == LAUNCH_DATA_ERRNO)
+  {
+    errno = launch_data_get_errno(ld_resp);
+    cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Check-in failed: %s",
+                    strerror(errno));
+    exit(EXIT_FAILURE);
+    return; /* anti-compiler-warning */
+  }
+
+ /*
+  * Get the sockets dictionary...
+  */
+
+  if ((ld_sockets = launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_SOCKETS))
+          == NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR,
+                    "service_checkin: No sockets found to answer requests on.");
+    exit(EXIT_FAILURE);
+    return; /* anti-compiler-warning */
+  }
+
+ /*
+  * Get the array of listener sockets...
+  */
+
+  if ((ld_array = launch_data_dict_lookup(ld_sockets, "Listeners")) == NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR,
+                    "service_checkin: No sockets found to answer requests on.");
+    exit(EXIT_FAILURE);
+    return; /* anti-compiler-warning */
+  }
+
+ /*
+  * Add listening fd(s) to the Listener array...
+  */
+
+  if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY)
+  {
+    count = launch_data_array_get_count(ld_array);
+
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count);
+
+    for (i = 0; i < count; i ++)
+    {
+     /*
+      * Get the launchd file descriptor and address...
+      */
+
+      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 */
+      }
+    }
+  }
+
+  launch_data_free(ld_msg);
+  launch_data_free(ld_resp);
+
+#  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());
+
+ /*
+  * Check-in with systemd...
+  */
+
+  if ((count = sd_listen_fds(0)) < 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 systemd sockets to the cupsd listeners...
+  */
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", count);
+
+  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 */
+  }
+#  endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
+}
+
+
+/*
+ * 'service_checkout()' - Update the CUPS_KEEPALIVE file as needed.
+ */
+
+static void
+service_checkout(void)
+{
+  int  fd;                             /* File descriptor */
+
+
+ /*
+  * Create or remove the systemd path file based on whether there are active
+  * jobs or shared printers to advertise...
+  */
+
+  if (cupsArrayCount(ActiveJobs) ||
+      (Browsing && BrowseLocalProtocols && cupsArrayCount(Printers)))
+  {
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "Creating keep-alive file \"" CUPS_KEEPALIVE "\".");
+
+    if ((fd = open(CUPS_KEEPALIVE, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR)) >= 0)
+      close(fd);
+  }
+  else
+  {
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "Removing keep-alive file \"" CUPS_KEEPALIVE "\".");
+
+    unlink(CUPS_KEEPALIVE);
+  }
+}
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+
+
 /*
  * 'usage()' - Show scheduler usage.
  */
@@ -2080,7 +2156,7 @@ usage(int status)                 /* O - Exit status */
   _cupsLangPuts(fp, _("  -F                      Run in the foreground but "
                       "detach from console."));
   _cupsLangPuts(fp, _("  -h                      Show this usage message."));
-  _cupsLangPuts(fp, _("  -l                      Run cupsd from launchd(8)."));
+  _cupsLangPuts(fp, _("  -l                      Run cupsd on demand."));
   _cupsLangPuts(fp, _("  -t                      Test the configuration "
                       "file."));