]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/main.c
Remove svn:keywords since they cause svn_load_dirs.pl to complain about every file.
[thirdparty/cups.git] / scheduler / main.c
index b0b581cdec8980676b126e0cf67eac709a3de541..0b8e34c9b201e04cb9ccc0d7c18df7fc58eff2a3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: main.c 5216 2006-03-02 17:47:53Z mike $"
+ * "$Id: main.c 177 2006-06-21 00:20:03Z jlovell $"
  *
  *   Scheduler main loop for the Common UNIX Printing System (CUPS).
  *
@@ -26,9 +26,7 @@
  *   main()                    - Main entry for the CUPS scheduler.
  *   cupsdClosePipe()          - Close a pipe as necessary.
  *   cupsdOpenPipe()           - Create a pipe which is closed on exec.
- *   cupsdCatchChildSignals()  - Catch SIGCHLD signals...
  *   cupsdHoldSignals()        - Hold child and termination signals.
- *   cupsdIgnoreChildSignals() - Ignore SIGCHLD signals...
  *   cupsdReleaseSignals()     - Release signals for delivery.
  *   cupsdSetString()          - Set a string value.
  *   cupsdSetStringf()         - Set a formatted string value.
@@ -221,6 +219,12 @@ main(int  argc,                            /* I - Number of command-line args */
 #endif /* HAVE_LAUNCHD */
              break;
 
+          case 'p' : /* Stop immediately for profiling */
+              puts("Warning: -p option is for internal testing use only!");
+             stop_scheduler = 1;
+             fg             = 1;
+             break;
+
          default : /* Unknown option */
               _cupsLangPrintf(stderr, _("cupsd: Unknown option \"%c\" - "
                                        "aborting!\n"), *opt);
@@ -428,12 +432,19 @@ main(int  argc,                           /* I - Number of command-line args */
   */
 
 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
+  sigset(SIGCHLD, sigchld_handler);
   sigset(SIGHUP, sighup_handler);
   sigset(SIGPIPE, SIG_IGN);
   sigset(SIGTERM, sigterm_handler);
 #elif defined(HAVE_SIGACTION)
   memset(&action, 0, sizeof(action));
 
+  sigemptyset(&action.sa_mask);
+  sigaddset(&action.sa_mask, SIGTERM);
+  sigaddset(&action.sa_mask, SIGCHLD);
+  action.sa_handler = sigchld_handler;
+  sigaction(SIGCHLD, &action, NULL);
+
   sigemptyset(&action.sa_mask);
   sigaddset(&action.sa_mask, SIGHUP);
   action.sa_handler = sighup_handler;
@@ -449,6 +460,7 @@ main(int  argc,                             /* I - Number of command-line args */
   action.sa_handler = sigterm_handler;
   sigaction(SIGTERM, &action, NULL);
 #else
+  signal(SIGCLD, sigchld_handler);     /* No, SIGCLD isn't a typo... */
   signal(SIGHUP, sighup_handler);
   signal(SIGPIPE, SIG_IGN);
   signal(SIGTERM, sigterm_handler);
@@ -501,17 +513,13 @@ main(int  argc,                           /* I - Number of command-line args */
       kill(i, SIGUSR1);
   }
 
+#ifdef __APPLE__
  /*
   * Start power management framework...
   */
 
   cupsdStartSystemMonitor();
-
- /*
-  * Catch signals...
-  */
-
-  cupsdCatchChildSignals();
+#endif /* __APPLE__ */
 
  /*
   * Start any pending print jobs...
@@ -701,7 +709,7 @@ main(int  argc,                             /* I - Number of command-line args */
          sptr += strlen(sptr);
        }
 
-      cupsdLogMessage(CUPSD_LOG_EMERG, s);
+      cupsdLogMessage(CUPSD_LOG_EMERG, "%s", s);
 
       strcpy(s, "OutputSet =");
       slen = 11;
@@ -715,7 +723,7 @@ main(int  argc,                             /* I - Number of command-line args */
          sptr += strlen(sptr);
        }
 
-      cupsdLogMessage(CUPSD_LOG_EMERG, s);
+      cupsdLogMessage(CUPSD_LOG_EMERG, "%s", s);
 
       for (i = 0, con = (cupsd_client_t *)cupsArrayFirst(Clients);
           con;
@@ -980,7 +988,7 @@ main(int  argc,                             /* I - Number of command-line args */
                      mem.fsmblks + mem.fordblks);
 #endif /* HAVE_MALLINFO */
 
-      string_count = _cups_sp_statistics(&alloc_bytes, &total_bytes);
+      string_count = _cupsStrStatistics(&alloc_bytes, &total_bytes);
       cupsdLogMessage(CUPSD_LOG_DEBUG2,
                       "stringpool: " CUPS_LLFMT " strings, "
                      CUPS_LLFMT " allocated, " CUPS_LLFMT " total bytes",
@@ -991,11 +999,12 @@ main(int  argc,                           /* I - Number of command-line args */
     }
 
    /*
-    * Update the root certificate once every 5 minutes...
+    * Update the root certificate once every 5 minutes if we have client
+    * connections...
     */
 
     if ((current_time - RootCertTime) >= RootCertDuration && RootCertDuration &&
-        !RunUser)
+        !RunUser && cupsArrayCount(Clients))
     {
      /*
       * Update the root certificate...
@@ -1010,7 +1019,7 @@ main(int  argc,                           /* I - Number of command-line args */
     * accumulated.  Don't send these more than once a second...
     */
 
-    if (LastEvent && (time(NULL) - LastEventTime) > 1)
+    if (LastEvent)
     {
 #ifdef HAVE_NOTIFY_POST
       if (LastEvent & CUPSD_EVENT_PRINTER_CHANGED)
@@ -1038,10 +1047,9 @@ main(int  argc,                          /* I - Number of command-line args */
 #endif /* HAVE_NOTIFY_POST */
 
      /*
-      * Reset the accumulated events and notification time...
+      * Reset the accumulated events...
       */
 
-      LastEventTime = time(NULL);
       LastEvent     = CUPSD_EVENT_NONE;
     }
   }
@@ -1064,7 +1072,9 @@ main(int  argc,                           /* I - Number of command-line args */
 
   cupsdFreeAllJobs();
 
+#ifdef __APPLE__
   cupsdStopSystemMonitor();
+#endif /* __APPLE__ */
 
 #ifdef HAVE_LAUNCHD
  /*
@@ -1136,7 +1146,12 @@ cupsdOpenPipe(int *fds)                  /* O - Pipe file descriptors (2) */
   */
 
   if (pipe(fds))
+  {
+    fds[0] = -1;
+    fds[1] = -1;
+
     return (-1);
+  }
 
  /*
   * Set the "close on exec" flag on each end of the pipe...
@@ -1146,6 +1161,10 @@ cupsdOpenPipe(int *fds)                  /* O - Pipe file descriptors (2) */
   {
     close(fds[0]);
     close(fds[1]);
+
+    fds[0] = -1;
+    fds[1] = -1;
+
     return (-1);
   }
 
@@ -1153,6 +1172,10 @@ cupsdOpenPipe(int *fds)                  /* O - Pipe file descriptors (2) */
   {
     close(fds[0]);
     close(fds[1]);
+
+    fds[0] = -1;
+    fds[1] = -1;
+
     return (-1);
   }
 
@@ -1164,34 +1187,6 @@ cupsdOpenPipe(int *fds)                  /* O - Pipe file descriptors (2) */
 }
 
 
-/*
- * 'cupsdCatchChildSignals()' - Catch SIGCHLD signals...
- */
-
-void
-cupsdCatchChildSignals(void)
-{
-#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
-  struct sigaction     action;         /* Actions for POSIX signals */
-#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
-
-
-#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
-  sigset(SIGCHLD, sigchld_handler);
-#elif defined(HAVE_SIGACTION)
-  memset(&action, 0, sizeof(action));
-
-  sigemptyset(&action.sa_mask);
-  sigaddset(&action.sa_mask, SIGTERM);
-  sigaddset(&action.sa_mask, SIGCHLD);
-  action.sa_handler = sigchld_handler;
-  sigaction(SIGCHLD, &action, NULL);
-#else
-  signal(SIGCLD, sigchld_handler);     /* No, SIGCLD isn't a typo... */
-#endif /* HAVE_SIGSET */
-}
-
-
 /*
  * 'cupsdClearString()' - Clear a string.
  */
@@ -1201,7 +1196,7 @@ cupsdClearString(char **s)                /* O - String value */
 {
   if (s && *s)
   {
-    free(*s);
+    _cupsStrFree(*s);
     *s = NULL;
   }
 }
@@ -1235,36 +1230,6 @@ cupsdHoldSignals(void)
 }
 
 
-/*
- * 'cupsdIgnoreChildSignals()' - Ignore SIGCHLD signals...
- *
- * We don't really ignore them, we set the signal handler to SIG_DFL,
- * since some OS's rely on signals for the wait4() function to work.
- */
-
-void
-cupsdIgnoreChildSignals(void)
-{
-#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
-  struct sigaction     action;         /* Actions for POSIX signals */
-#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
-
-
-#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
-  sigset(SIGCHLD, SIG_DFL);
-#elif defined(HAVE_SIGACTION)
-  memset(&action, 0, sizeof(action));
-
-  sigemptyset(&action.sa_mask);
-  sigaddset(&action.sa_mask, SIGCHLD);
-  action.sa_handler = SIG_DFL;
-  sigaction(SIGCHLD, &action, NULL);
-#else
-  signal(SIGCLD, SIG_DFL);     /* No, SIGCLD isn't a typo... */
-#endif /* HAVE_SIGSET */
-}
-
-
 /*
  * 'cupsdReleaseSignals()' - Release signals for delivery.
  */
@@ -1297,10 +1262,10 @@ cupsdSetString(char       **s,          /* O - New string */
     return;
 
   if (*s)
-    free(*s);
+    _cupsStrFree(*s);
 
   if (v)
-    *s = strdup(v);
+    *s = _cupsStrAlloc(v);
   else
     *s = NULL;
 }
@@ -1331,13 +1296,13 @@ cupsdSetStringf(char       **s,         /* O - New string */
     vsnprintf(v, sizeof(v), f, ap);
     va_end(ap);
 
-    *s = strdup(v);
+    *s = _cupsStrAlloc(v);
   }
   else
     *s = NULL;
 
   if (olds)
-    free(olds);
+    _cupsStrFree(olds);
 }
 
 
@@ -1356,12 +1321,10 @@ launchd_checkin(void)
                        ld_resp,        /* Launch data response */
                        ld_array,       /* Launch data array */
                        ld_sockets,     /* Launch data sockets dictionary */
-                       ld_runatload,   /* Run-at-load setting */
                        tmp;            /* Launch data */
   cupsd_listener_t     *lis;           /* Listeners array */
   http_addr_t          addr;           /* Address variable */
   socklen_t            addrlen;        /* Length of address */
-  bool                 runatload;      /* Run-at-load setting value */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: pid=%d", (int)getpid());
@@ -1387,26 +1350,6 @@ launchd_checkin(void)
     exit(EXIT_FAILURE);
   }
 
- /*
-  * Get the "run-at-load" setting...
-  */
-
-  if ((ld_runatload =
-           launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_RUNATLOAD)) != NULL &&
-      launch_data_get_type(ld_runatload) == LAUNCH_DATA_BOOL)
-    runatload = launch_data_get_bool(ld_runatload);
-  else
-  {
-    errno = launch_data_get_errno(ld_resp);
-    cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "launchd_checkin: Unable to find Run-at-load setting: %s",
-                    strerror(errno));
-    exit(EXIT_FAILURE);
-  }
-
-  cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: Run-at-load=%s",
-                  runatload ? "true" : "false");
-
  /*
   * Get the sockets dictionary...
   */
@@ -1500,18 +1443,22 @@ launchd_checkin(void)
   {
     if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY)
     {
-      tmp = launch_data_array_get_index(ld_array, 0);
-
-      if (launch_data_get_type(tmp) == LAUNCH_DATA_FD)
+      if ((tmp = launch_data_array_get_index(ld_array, 0)))
       {
-        if (BrowseSocket != -1)
-         close(BrowseSocket);
-
-       BrowseSocket = launch_data_get_fd(tmp);
-      }
-      else
-       cupsdLogMessage(CUPSD_LOG_WARN,
-                       "launchd_checkin: BrowseSocket not a fd!");
+       if (launch_data_get_type(tmp) == LAUNCH_DATA_FD)
+       {
+         if (BrowseSocket != -1)
+           close(BrowseSocket);
+  
+         BrowseSocket = launch_data_get_fd(tmp);
+       }
+       else
+         cupsdLogMessage(CUPSD_LOG_WARN,
+                         "launchd_checkin: BrowseSocket not a fd!");
+     }
+     else
+       cupsdLogMessage(CUPSD_LOG_WARN,
+                      "launchd_checkin: BrowseSockets is an empty array!");
    }
    else
      cupsdLogMessage(CUPSD_LOG_WARN,
@@ -2148,13 +2095,6 @@ select_timeout(int fds)                  /* I - Number of descriptors returned */
   if (fds || cupsArrayCount(Clients) > 50)
     return (1);
 
- /*
-  * If we had a recent event notification, timeout in 1 second...
-  */
-
-  if (LastEvent)
-    return (1);
-
  /*
   * Otherwise, check all of the possible events that we need to wake for...
   */
@@ -2313,5 +2253,5 @@ usage(int status)                 /* O - Exit status */
 
 
 /*
- * End of "$Id: main.c 5216 2006-03-02 17:47:53Z mike $".
+ * End of "$Id: main.c 177 2006-06-21 00:20:03Z jlovell $".
  */