]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/main.c
Merge pull request #4825 from computersforpeace/upstart
[thirdparty/cups.git] / scheduler / main.c
index 225892e6215c3901f6df6922d5e9538d8b94a714..26417b9c759ea1123810884f3bcc6b3174b6b149 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * "$Id$"
- *
  * Main loop for the CUPS scheduler.
  *
  * Copyright 2007-2016 by Apple Inc.
@@ -32,8 +30,6 @@
 #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);
@@ -42,10 +38,13 @@ 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 */
@@ -73,10 +72,10 @@ 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)
+#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));
 
 
@@ -133,10 +132,10 @@ main(int  argc,                           /* I - Number of command-line args */
 #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
@@ -244,8 +243,8 @@ main(int  argc,                             /* I - Number of command-line args */
              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;
@@ -256,7 +255,7 @@ main(int  argc,                             /* I - Number of command-line args */
               fg         = 0;
              disconnect = 1;
              close_all  = 1;
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
              break;
 
           case 'p' : /* Stop immediately for profiling */
@@ -586,7 +585,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
   cupsdCleanFiles(CacheDir, "*.ipp");
 
-#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+#if defined(HAVE_ONDEMAND)
   if (OnDemand)
   {
    /*
@@ -597,7 +596,7 @@ main(int  argc,                             /* I - Number of command-line args */
     service_checkin();
     service_checkout();
   }
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
 
  /*
   * Startup the server...
@@ -684,11 +683,11 @@ main(int  argc,                           /* I - Number of command-line args */
   * 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
@@ -756,6 +755,11 @@ main(int  argc,                            /* I - Number of command-line args */
        * Shutdown the server...
        */
 
+#if defined(HAVE_LAUNCHD) || defined(HAVE_SYSTEMD)
+       if (OnDemand)
+         break;
+#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+
         DoingShutdown = 1;
 
        cupsdStopServer();
@@ -811,7 +815,7 @@ main(int  argc,                             /* I - Number of command-line args */
     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
@@ -830,7 +834,7 @@ main(int  argc,                             /* I - Number of command-line args */
     }
     else
       service_idle_exit = 0;
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
 
     if ((fds = cupsdDoSelect(timeout)) < 0)
     {
@@ -927,7 +931,7 @@ main(int  argc,                             /* I - Number of command-line args */
     }
 #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...
@@ -941,7 +945,7 @@ main(int  argc,                             /* I - Number of command-line args */
       stop_scheduler = 1;
       break;
     }
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
 
    /*
     * Resume listening for new connections as needed...
@@ -1145,14 +1149,14 @@ main(int  argc,                         /* I - Number of command-line args */
 
   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...
@@ -1507,7 +1511,19 @@ process_children(void)
        * filters are done, and if so move to the next file.
        */
 
-       if (job->current_file < job->num_files && job->printer)
+       if (job->state_value >= IPP_JOB_CANCELED)
+       {
+        /*
+         * Remove the job from the active list if there are no processes still
+         * running for it...
+         */
+
+         for (i = 0; job->filters[i] < 0; i++);
+
+         if (!job->filters[i] && job->backend <= 0)
+           cupsArrayRemove(ActiveJobs, job);
+       }
+       else if (job->current_file < job->num_files && job->printer)
        {
          for (i = 0; job->filters[i] < 0; i ++);
 
@@ -1522,18 +1538,6 @@ process_children(void)
            cupsdContinueJob(job);
          }
        }
-       else if (job->state_value >= IPP_JOB_CANCELED)
-       {
-        /*
-         * Remove the job from the active list if there are no processes still
-         * running for it...
-         */
-
-         for (i = 0; job->filters[i] < 0; i++);
-
-         if (!job->filters[i] && job->backend <= 0)
-           cupsArrayRemove(ActiveJobs, job);
-       }
       }
     }
 
@@ -1824,7 +1828,82 @@ sigterm_handler(int sig)         /* I - Signal number */
 }
 
 
-#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.
  */
@@ -1837,10 +1916,6 @@ service_checkin(void)
   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());
@@ -1864,56 +1939,7 @@ service_checkin(void)
 
   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);
@@ -1926,11 +1952,7 @@ service_checkin(void)
                        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());
@@ -2002,56 +2024,7 @@ service_checkin(void)
       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);
       }
     }
   }
@@ -2059,13 +2032,9 @@ 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 */
-  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());
@@ -2089,57 +2058,51 @@ service_checkin(void)
 
   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 */
 }
 
@@ -2161,6 +2124,7 @@ service_checkout(void)
 
   if (cupsArrayCount(ActiveJobs) ||    /* Active jobs */
       WebInterface ||                  /* Web interface enabled */
+      NeedReload ||                    /* Doing a reload */
       (Browsing && BrowseLocalProtocols && cupsArrayCount(Printers)))
                                        /* Printers being shared */
   {
@@ -2176,7 +2140,7 @@ service_checkout(void)
     unlink(CUPS_KEEPALIVE);
   }
 }
-#endif /* HAVE_LAUNCHD || HAVE_SYSTEMD */
+#endif /* HAVE_ONDEMAND */
 
 
 /*
@@ -2193,17 +2157,11 @@ usage(int status)                       /* O - Exit status */
   _cupsLangPuts(fp, _("Options:"));
   _cupsLangPuts(fp, _("  -c cupsd.conf           Set cupsd.conf file to use."));
   _cupsLangPuts(fp, _("  -f                      Run in the foreground."));
-  _cupsLangPuts(fp, _("  -F                      Run in the foreground but "
-                      "detach from console."));
+  _cupsLangPuts(fp, _("  -F                      Run in the foreground but detach from console."));
   _cupsLangPuts(fp, _("  -h                      Show this usage message."));
   _cupsLangPuts(fp, _("  -l                      Run cupsd on demand."));
-  _cupsLangPuts(fp, _("  -t                      Test the configuration "
-                      "file."));
+  _cupsLangPuts(fp, _("  -s cups-files.conf      Set cups-files.conf file to use."));
+  _cupsLangPuts(fp, _("  -t                      Test the configuration file."));
 
   exit(status);
 }
-
-
-/*
- * End of "$Id$".
- */