4 * Main loop for the CUPS scheduler.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * Include necessary headers...
22 #include <sys/resource.h>
29 # define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
30 /* Name of the launchd KeepAlive file */
31 # ifdef HAVE_LAUNCH_ACTIVATE_SOCKET
32 /* Update when we have a public header we can include */
33 extern int launch_activate_socket(const char *name
, int **fds
, size_t *cnt
);
34 # endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
35 #endif /* HAVE_LAUNCH_H */
37 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
39 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
43 #endif /* HAVE_NOTIFY_H */
45 #ifdef HAVE_SYS_PARAM_H
46 # include <sys/param.h>
47 #endif /* HAVE_SYS_PARAM_H */
55 static void launchd_checkin(void);
56 static void launchd_checkout(void);
57 #endif /* HAVE_LAUNCHD */
58 static void parent_handler(int sig
);
59 static void process_children(void);
60 static void sigchld_handler(int sig
);
61 static void sighup_handler(int sig
);
62 static void sigterm_handler(int sig
);
63 static long select_timeout(int fds
);
64 static void usage(int status
) __attribute__((noreturn
));
71 static int parent_signal
= 0;
72 /* Set to signal number from child */
73 static int holdcount
= 0; /* Number of times "hold" was called */
74 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
75 static sigset_t holdmask
; /* Old POSIX signal mask */
76 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
77 static int dead_children
= 0;
79 static int stop_scheduler
= 0;
80 /* Should the scheduler stop? */
84 * 'main()' - Main entry for the CUPS scheduler.
87 int /* O - Exit status */
88 main(int argc
, /* I - Number of command-line args */
89 char *argv
[]) /* I - Command-line arguments */
91 int i
; /* Looping var */
92 char *opt
; /* Option character */
93 int fg
; /* Run in the foreground */
94 int fds
; /* Number of ready descriptors */
95 cupsd_client_t
*con
; /* Current client */
96 cupsd_job_t
*job
; /* Current job */
97 cupsd_listener_t
*lis
; /* Current listener */
98 time_t current_time
, /* Current time */
99 activity
, /* Client activity timer */
100 senddoc_time
, /* Send-Document time */
101 expire_time
, /* Subscription expire time */
102 report_time
, /* Malloc/client/job report time */
103 event_time
; /* Last event notification time */
104 long timeout
; /* Timeout for cupsdDoSelect() */
105 struct rlimit limit
; /* Runtime limit */
106 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
107 struct sigaction action
; /* Actions for POSIX signals */
108 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
109 int run_as_child
= 0;
110 /* Needed for background fork/exec */
112 int use_sysman
= !getuid();
113 /* Use system management functions? */
115 time_t netif_time
= 0; /* Time since last network update */
116 #endif /* __APPLE__ */
118 int launchd_idle_exit
;
119 /* Idle exit on select timeout? */
120 #endif /* HAVE_LAUNCHD */
125 * Check for setuid invocation, which we do not support!
128 if (getuid() != geteuid())
130 fputs("cupsd: Cannot run as a setuid program\n", stderr
);
133 #endif /* HAVE_GETEUID */
136 * Check for command-line arguments...
142 if (getenv("CUPSD_LAUNCHD"))
147 #endif /* HAVE_LAUNCHD */
149 for (i
= 1; i
< argc
; i
++)
150 if (argv
[i
][0] == '-')
151 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
154 case 'C' : /* Run as child with config file */
158 case 'c' : /* Configuration file */
162 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
163 "after \"-c\" option."));
167 if (argv
[i
][0] == '/')
170 * Absolute directory...
173 cupsdSetString(&ConfigurationFile
, argv
[i
]);
178 * Relative directory...
181 char *current
; /* Current directory */
184 * Allocate a buffer for the current working directory to
185 * reduce run-time stack usage; this approximates the
186 * behavior of some implementations of getcwd() when they
187 * are passed a NULL pointer.
190 if ((current
= malloc(1024)) == NULL
)
192 _cupsLangPuts(stderr
,
193 _("cupsd: Unable to get current directory."));
197 if (!getcwd(current
, 1024))
199 _cupsLangPuts(stderr
,
200 _("cupsd: Unable to get current directory."));
205 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
210 case 'f' : /* Run in foreground... */
214 case 'F' : /* Run in foreground, but disconnect from terminal... */
218 case 'h' : /* Show usage/help */
222 case 'l' : /* Started by launchd... */
227 _cupsLangPuts(stderr
, _("cupsd: launchd(8) support not compiled "
228 "in, running in normal mode."));
230 #endif /* HAVE_LAUNCHD */
233 case 'p' : /* Stop immediately for profiling */
234 fputs("cupsd: -p (startup profiling) is for internal testing "
235 "use only!\n", stderr
);
240 case 'P' : /* Disable security profiles */
241 fputs("cupsd: -P (disable sandboxing) is for internal testing use only.\n", stderr
);
245 case 's' : /* Set cups-files.conf location */
249 _cupsLangPuts(stderr
, _("cupsd: Expected cups-files.conf "
250 "filename after \"-s\" option."));
254 if (argv
[i
][0] != '/')
257 * Relative filename not allowed...
260 _cupsLangPuts(stderr
, _("cupsd: Relative cups-files.conf "
261 "filename not allowed."));
265 cupsdSetString(&CupsFilesFile
, argv
[i
]);
269 case 'S' : /* Disable system management functions */
270 fputs("cupsd: -S (disable system management) for internal "
271 "testing use only!\n", stderr
);
274 #endif /* __APPLE__ */
276 case 't' : /* Test the cupsd.conf file... */
281 default : /* Unknown option */
282 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
289 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting."),
294 if (!ConfigurationFile
)
295 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
299 char *filename
, /* Copy of cupsd.conf filename */
300 *slash
; /* Final slash in cupsd.conf filename */
301 size_t len
; /* Size of buffer */
303 len
= strlen(ConfigurationFile
) + 15;
304 if ((filename
= malloc(len
)) == NULL
)
306 _cupsLangPrintf(stderr
,
307 _("cupsd: Unable to get path to "
308 "cups-files.conf file."));
312 strlcpy(filename
, ConfigurationFile
, len
);
313 if ((slash
= strrchr(filename
, '/')) == NULL
)
315 _cupsLangPrintf(stderr
,
316 _("cupsd: Unable to get path to "
317 "cups-files.conf file."));
321 strlcpy(slash
, "/cups-files.conf", len
- (size_t)(slash
- filename
));
322 cupsdSetString(&CupsFilesFile
, filename
);
327 * If the user hasn't specified "-f", run in the background...
333 * Setup signal handlers for the parent...
336 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
337 sigset(SIGUSR1
, parent_handler
);
338 sigset(SIGCHLD
, parent_handler
);
340 sigset(SIGHUP
, SIG_IGN
);
341 #elif defined(HAVE_SIGACTION)
342 memset(&action
, 0, sizeof(action
));
343 sigemptyset(&action
.sa_mask
);
344 sigaddset(&action
.sa_mask
, SIGUSR1
);
345 action
.sa_handler
= parent_handler
;
346 sigaction(SIGUSR1
, &action
, NULL
);
347 sigaction(SIGCHLD
, &action
, NULL
);
349 sigemptyset(&action
.sa_mask
);
350 action
.sa_handler
= SIG_IGN
;
351 sigaction(SIGHUP
, &action
, NULL
);
353 signal(SIGUSR1
, parent_handler
);
354 signal(SIGCLD
, parent_handler
);
356 signal(SIGHUP
, SIG_IGN
);
357 #endif /* HAVE_SIGSET */
362 * OK, wait for the child to startup and send us SIGUSR1 or to crash
363 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
364 * might be sent by the init script to restart the scheduler...
367 for (; parent_signal
== 0;)
370 if (parent_signal
== SIGUSR1
)
378 else if (WIFEXITED(i
))
380 fprintf(stderr
, "cupsd: Child exited with status %d\n",
386 fprintf(stderr
, "cupsd: Child exited on signal %d\n", WTERMSIG(i
));
391 #if defined(__OpenBSD__) && OpenBSD < 201211
393 * Call _thread_sys_closefrom() so the child process doesn't reset the
394 * parent's file descriptors to be blocking. This is a workaround for a
395 * limitation of userland libpthread on older versions of OpenBSD.
398 _thread_sys_closefrom(0);
399 #endif /* __OpenBSD__ && OpenBSD < 201211 */
402 * Since CoreFoundation and DBUS both create fork-unsafe data on execution of
403 * a program, and since this kind of really unfriendly behavior seems to be
404 * more common these days in system libraries, we need to re-execute the
405 * background cupsd with the "-C" option to avoid problems. Unfortunately,
406 * we also have to assume that argv[0] contains the name of the cupsd
407 * executable - there is no portable way to get the real pathname...
410 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, (char *)0);
417 * Make sure we aren't tying up any filesystems...
424 * Disable core dumps...
427 getrlimit(RLIMIT_CORE
, &limit
);
429 setrlimit(RLIMIT_CORE
, &limit
);
432 * Disconnect from the controlling terminal...
438 * Close all open files...
441 getrlimit(RLIMIT_NOFILE
, &limit
);
443 for (i
= 0; i
< limit
.rlim_cur
&& i
< 1024; i
++)
447 * Redirect stdin/out/err to /dev/null...
450 if ((i
= open("/dev/null", O_RDONLY
)) != 0)
456 if ((i
= open("/dev/null", O_WRONLY
)) != 1)
462 if ((i
= open("/dev/null", O_WRONLY
)) != 2)
471 * Set the timezone info...
477 setlocale(LC_TIME
, "");
480 #ifdef HAVE_DBUS_THREADS_INIT
482 * Enable threading support for D-BUS...
486 #endif /* HAVE_DBUS_THREADS_INIT */
489 * Set the maximum number of files...
492 getrlimit(RLIMIT_NOFILE
, &limit
);
494 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
495 if (limit
.rlim_max
> FD_SETSIZE
)
498 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
500 if (limit
.rlim_max
== RLIM_INFINITY
)
503 #endif /* RLIM_INFINITY */
504 MaxFDs
= limit
.rlim_max
;
506 limit
.rlim_cur
= (rlim_t
)MaxFDs
;
508 setrlimit(RLIMIT_NOFILE
, &limit
);
513 * Read configuration...
516 if (!cupsdReadConfiguration())
518 else if (TestConfigFile
)
520 printf("\"%s\" is OK.\n", CupsFilesFile
);
521 printf("\"%s\" is OK.\n", ConfigurationFile
);
526 * Clean out old temp files and printer cache data.
529 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
530 cupsdCleanFiles(TempDir
, NULL
);
532 cupsdCleanFiles(CacheDir
, "*.ipp");
538 * If we were started by launchd get the listen sockets file descriptors...
544 #endif /* HAVE_LAUNCHD */
547 * Startup the server...
555 * Catch hangup and child signals and ignore broken pipes...
558 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
559 sigset(SIGCHLD
, sigchld_handler
);
560 sigset(SIGHUP
, sighup_handler
);
561 sigset(SIGPIPE
, SIG_IGN
);
562 sigset(SIGTERM
, sigterm_handler
);
563 #elif defined(HAVE_SIGACTION)
564 memset(&action
, 0, sizeof(action
));
566 sigemptyset(&action
.sa_mask
);
567 sigaddset(&action
.sa_mask
, SIGTERM
);
568 sigaddset(&action
.sa_mask
, SIGCHLD
);
569 action
.sa_handler
= sigchld_handler
;
570 sigaction(SIGCHLD
, &action
, NULL
);
572 sigemptyset(&action
.sa_mask
);
573 sigaddset(&action
.sa_mask
, SIGHUP
);
574 action
.sa_handler
= sighup_handler
;
575 sigaction(SIGHUP
, &action
, NULL
);
577 sigemptyset(&action
.sa_mask
);
578 action
.sa_handler
= SIG_IGN
;
579 sigaction(SIGPIPE
, &action
, NULL
);
581 sigemptyset(&action
.sa_mask
);
582 sigaddset(&action
.sa_mask
, SIGTERM
);
583 sigaddset(&action
.sa_mask
, SIGCHLD
);
584 action
.sa_handler
= sigterm_handler
;
585 sigaction(SIGTERM
, &action
, NULL
);
587 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
588 signal(SIGHUP
, sighup_handler
);
589 signal(SIGPIPE
, SIG_IGN
);
590 signal(SIGTERM
, sigterm_handler
);
591 #endif /* HAVE_SIGSET */
594 * Initialize authentication certificates...
600 * If we are running in the background, signal the parent process that
601 * we are up and running...
604 if (!fg
|| run_as_child
)
607 * Send a signal to the parent process, but only if the parent is
608 * not PID 1 (init). This avoids accidentally shutting down the
609 * system on OpenBSD if you CTRL-C the server before it is up...
612 i
= getppid(); /* Save parent PID to avoid race condition */
620 * Start power management framework...
624 cupsdStartSystemMonitor();
625 #endif /* __APPLE__ */
628 * Send server-started event...
633 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
634 "Scheduler started via launchd.");
636 #endif /* HAVE_LAUNCHD */
638 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
639 "Scheduler started in foreground.");
641 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
642 "Scheduler started in background.");
645 * Start any pending print jobs...
654 current_time
= time(NULL
);
655 event_time
= current_time
;
656 expire_time
= current_time
;
659 senddoc_time
= current_time
;
661 while (!stop_scheduler
)
664 * Check if there are dead children to handle...
671 * Check if we need to load the server configuration file...
677 * Close any idle clients...
680 if (cupsArrayCount(Clients
) > 0)
682 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
684 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
685 if (httpGetState(con
->http
) == HTTP_WAITING
)
686 cupsdCloseClient(con
);
688 con
->http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
690 cupsdPauseListening();
694 * Restart if all clients are closed and all jobs finished, or
695 * if the reload timeout has elapsed...
698 if ((cupsArrayCount(Clients
) == 0 &&
699 (cupsArrayCount(PrintingJobs
) == 0 || NeedReload
!= RELOAD_ALL
)) ||
700 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
703 * Shutdown the server...
711 * Read configuration...
714 if (!cupsdReadConfiguration())
716 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
725 * If we were started by launchd, get the listen socket file
732 #endif /* HAVE_LAUNCHD */
735 * Startup the server...
743 * Send a server-restarted event...
746 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
747 "Scheduler restarted.");
752 * Check for available input or ready output. If cupsdDoSelect()
753 * returns 0 or -1, something bad happened and we should exit
756 * Note that we at least have one listening socket open at all
760 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
765 * If no other work is scheduled and we're being controlled by
766 * launchd then timeout after 'LaunchdTimeout' seconds of
770 if (timeout
== 86400 && Launchd
&& LaunchdTimeout
&&
771 !cupsArrayCount(ActiveJobs
) &&
772 (!Browsing
|| !BrowseLocalProtocols
|| !cupsArrayCount(Printers
)))
774 timeout
= LaunchdTimeout
;
775 launchd_idle_exit
= 1;
778 launchd_idle_exit
= 0;
779 #endif /* HAVE_LAUNCHD */
781 if ((fds
= cupsdDoSelect(timeout
)) < 0)
784 * Got an error from select!
787 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
788 cupsd_printer_t
*p
; /* Current printer */
789 #endif /* HAVE_DNSSD || HAVE_AVAHI */
792 if (errno
== EINTR
) /* Just interrupted by a signal */
796 * Log all sorts of debug info to help track down the problem.
799 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
802 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
804 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
805 cupsdLogMessage(CUPSD_LOG_EMERG
,
806 "Clients[%d] = %d, file = %d, state = %d",
807 i
, con
->number
, con
->file
, httpGetState(con
->http
));
809 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
811 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
812 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
814 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
817 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
819 #endif /* __APPLE__ */
821 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
823 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
824 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
826 job
->status_buffer
? job
->status_buffer
->fd
: -1,
827 job
->print_pipes
[0], job
->print_pipes
[1],
828 job
->back_pipes
[0], job
->back_pipes
[1]);
830 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
831 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
833 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
834 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
835 p
->reg_name
? p
->reg_name
: "(null)");
836 #endif /* HAVE_DNSSD || HAVE_AVAHI */
841 current_time
= time(NULL
);
844 * Write dirty config/state files...
847 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
852 * If we are going to sleep and still have pending jobs, stop them after
853 * a period of time...
856 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
857 cupsArrayCount(PrintingJobs
) > 0)
860 cupsdStopAllJobs(CUPSD_JOB_DEFAULT
, 5);
862 #endif /* __APPLE__ */
866 * Update the network interfaces once a minute...
869 if ((current_time
- netif_time
) >= 60)
871 netif_time
= current_time
;
874 #endif /* !__APPLE__ */
878 * If no other work was scheduled and we're being controlled by launchd
879 * then timeout after 'LaunchdTimeout' seconds of inactivity...
882 if (!fds
&& launchd_idle_exit
)
884 cupsdLogMessage(CUPSD_LOG_INFO
,
885 "Printer sharing is off and there are no jobs pending, "
886 "will restart on demand.");
890 #endif /* HAVE_LAUNCHD */
893 * Resume listening for new connections as needed...
896 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
897 cupsArrayCount(Clients
) < MaxClients
)
898 cupsdResumeListening();
901 * Expire subscriptions and unload completed jobs as needed...
904 if (current_time
> expire_time
)
906 if (cupsArrayCount(Subscriptions
) > 0)
907 cupsdExpireSubscriptions(NULL
, NULL
);
909 cupsdUnloadCompletedJobs();
911 expire_time
= current_time
;
914 #ifndef HAVE_AUTHORIZATION_H
916 * Update the root certificate once every 5 minutes if we have client
920 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
921 !RunUser
&& cupsArrayCount(Clients
))
924 * Update the root certificate...
928 cupsdAddCert(0, "root", cupsdDefaultAuthType());
930 #endif /* !HAVE_AUTHORIZATION_H */
933 * Check for new data on the client sockets...
936 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
938 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
941 * Process pending data in the input buffer...
944 // TODO: Use httpGetReady()
947 cupsdReadClient(con
);
952 * Check the activity and close old clients...
955 // TODO: Use httpGetActivity()
956 activity
= current_time
- Timeout
;
957 if (con
->http
->activity
< activity
&& !con
->pipe_pid
)
959 cupsdLogMessage(CUPSD_LOG_DEBUG
,
960 "Closing client %d after %d seconds of inactivity...",
961 con
->number
, Timeout
);
963 cupsdCloseClient(con
);
969 * Update any pending multi-file documents...
972 if ((current_time
- senddoc_time
) >= 10)
975 senddoc_time
= current_time
;
979 * Clean job history...
982 if (JobHistoryUpdate
&& current_time
>= JobHistoryUpdate
)
986 * Log statistics at most once a minute when in debug mode...
989 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
991 size_t string_count
, /* String count */
992 alloc_bytes
, /* Allocated string bytes */
993 total_bytes
; /* Total string bytes */
995 struct mallinfo mem
; /* Malloc information */
999 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1000 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1001 mem
.usmblks
+ mem
.uordblks
);
1002 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1003 mem
.fsmblks
+ mem
.fordblks
);
1004 #endif /* HAVE_MALLINFO */
1006 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1007 cupsArrayCount(Clients
));
1008 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1009 cupsArrayCount(Jobs
));
1010 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1011 cupsArrayCount(ActiveJobs
));
1012 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1013 cupsArrayCount(Printers
));
1015 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1016 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1017 "Report: stringpool-string-count=" CUPS_LLFMT
,
1018 CUPS_LLCAST string_count
);
1019 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1020 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1021 CUPS_LLCAST alloc_bytes
);
1022 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1023 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1024 CUPS_LLCAST total_bytes
);
1026 report_time
= current_time
;
1030 * Handle OS-specific event notification for any events that have
1031 * accumulated. Don't send these more than once a second...
1034 if (LastEvent
&& (current_time
- event_time
) >= 1)
1036 #ifdef HAVE_NOTIFY_POST
1037 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1038 CUPSD_EVENT_PRINTER_DELETED
|
1039 CUPSD_EVENT_PRINTER_MODIFIED
))
1041 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1042 "notify_post(\"com.apple.printerListChange\")");
1043 notify_post("com.apple.printerListChange");
1046 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1048 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1049 "notify_post(\"com.apple.printerHistoryChange\")");
1050 notify_post("com.apple.printerHistoryChange");
1053 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1054 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1055 CUPSD_EVENT_JOB_PROGRESS
))
1057 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1058 "notify_post(\"com.apple.jobChange\")");
1059 notify_post("com.apple.jobChange");
1061 #endif /* HAVE_NOTIFY_POST */
1064 * Reset the accumulated events...
1067 LastEvent
= CUPSD_EVENT_NONE
;
1068 event_time
= current_time
;
1073 * Log a message based on what happened...
1078 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1079 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1080 "Scheduler shutting down normally.");
1084 cupsdLogMessage(CUPSD_LOG_ERROR
,
1085 "Scheduler shutting down due to program error.");
1086 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1087 "Scheduler shutting down due to program error.");
1091 * Close all network clients...
1100 * Update the launchd KeepAlive file as needed...
1105 #endif /* HAVE_LAUNCHD */
1115 * Stop monitoring system event monitoring...
1119 cupsdStopSystemMonitor();
1120 #endif /* __APPLE__ */
1124 return (!stop_scheduler
);
1129 * 'cupsdAddString()' - Copy and add a string to an array.
1132 int /* O - 1 on success, 0 on failure */
1133 cupsdAddString(cups_array_t
**a
, /* IO - String array */
1134 const char *s
) /* I - String to copy and add */
1137 *a
= cupsArrayNew3((cups_array_func_t
)strcmp
, NULL
,
1138 (cups_ahash_func_t
)NULL
, 0,
1139 (cups_acopy_func_t
)_cupsStrAlloc
,
1140 (cups_afree_func_t
)_cupsStrFree
);
1142 return (cupsArrayAdd(*a
, (char *)s
));
1147 * 'cupsdCheckProcess()' - Tell the main loop to check for dead children.
1151 cupsdCheckProcess(void)
1154 * Flag that we have dead children...
1162 * 'cupsdClearString()' - Clear a string.
1166 cupsdClearString(char **s
) /* O - String value */
1177 * 'cupsdFreeStrings()' - Free an array of strings.
1181 cupsdFreeStrings(cups_array_t
**a
) /* IO - String array */
1185 cupsArrayDelete(*a
);
1192 * 'cupsdHoldSignals()' - Hold child and termination signals.
1196 cupsdHoldSignals(void)
1198 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1199 sigset_t newmask
; /* New POSIX signal mask */
1200 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1210 #elif defined(HAVE_SIGACTION)
1211 sigemptyset(&newmask
);
1212 sigaddset(&newmask
, SIGTERM
);
1213 sigaddset(&newmask
, SIGCHLD
);
1214 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1215 #endif /* HAVE_SIGSET */
1220 * 'cupsdReleaseSignals()' - Release signals for delivery.
1224 cupsdReleaseSignals(void)
1233 #elif defined(HAVE_SIGACTION)
1234 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1235 #endif /* HAVE_SIGSET */
1240 * 'cupsdSetString()' - Set a string value.
1244 cupsdSetString(char **s
, /* O - New string */
1245 const char *v
) /* I - String value */
1254 *s
= _cupsStrAlloc(v
);
1261 * 'cupsdSetStringf()' - Set a formatted string value.
1265 cupsdSetStringf(char **s
, /* O - New string */
1266 const char *f
, /* I - Printf-style format string */
1267 ...) /* I - Additional args as needed */
1269 char v
[65536 + 64]; /* Formatting string value */
1270 va_list ap
; /* Argument pointer */
1271 char *olds
; /* Old string */
1282 vsnprintf(v
, sizeof(v
), f
, ap
);
1285 *s
= _cupsStrAlloc(v
);
1297 * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
1301 launchd_checkin(void)
1303 # ifdef HAVE_LAUNCH_ACTIVATE_SOCKET
1304 int error
; /* Check-in error, if any */
1305 size_t i
, /* Looping var */
1306 count
; /* Number of listeners */
1307 int *ld_sockets
; /* Listener sockets */
1308 cupsd_listener_t
*lis
; /* Listeners array */
1309 http_addr_t addr
; /* Address variable */
1310 socklen_t addrlen
; /* Length of address */
1311 char s
[256]; /* String addresss */
1314 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: pid=%d", (int)getpid());
1317 * Check-in with launchd...
1320 if ((error
= launch_activate_socket("Listeners", &ld_sockets
, &count
)) != 0)
1322 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Unable to get listener sockets: %s", strerror(error
));
1324 return; /* anti-compiler-warning */
1328 * Try to match the launchd sockets to the cupsd listeners...
1331 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: %d listeners.", (int)count
);
1333 for (i
= 0; i
< count
; i
++)
1336 * Get the launchd socket address...
1339 addrlen
= sizeof(addr
);
1341 if (getsockname(ld_sockets
[i
], (struct sockaddr
*)&addr
, &addrlen
))
1343 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Unable to get local address for listener #%d: %s", (int)i
+ 1, strerror(errno
));
1347 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: Listener #%d at fd %d, \"%s\".", (int)i
+ 1, ld_sockets
[i
], httpAddrString(&addr
, s
, sizeof(s
)));
1349 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1351 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1352 if (httpAddrEqual(&lis
->address
, &addr
))
1356 * Add a new listener if there's no match...
1361 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: Matched existing listener #%d to %s.", (int)i
+ 1, httpAddrString(&(lis
->address
), s
, sizeof(s
)));
1365 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: Adding new listener #%d for %s.", (int)i
+ 1, httpAddrString(&addr
, s
, sizeof(s
)));
1367 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1369 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Unable to allocate listener: %s", strerror(errno
));
1373 cupsArrayAdd(Listeners
, lis
);
1375 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1378 lis
->fd
= ld_sockets
[i
];
1381 if (httpAddrPort(&(lis
->address
)) == 443)
1382 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1383 # endif /* HAVE_SSL */
1387 size_t i
, /* Looping var */
1388 count
; /* Number of listeners */
1389 launch_data_t ld_msg
, /* Launch data message */
1390 ld_resp
, /* Launch data response */
1391 ld_array
, /* Launch data array */
1392 ld_sockets
, /* Launch data sockets dictionary */
1393 tmp
; /* Launch data */
1394 cupsd_listener_t
*lis
; /* Listeners array */
1395 http_addr_t addr
; /* Address variable */
1396 socklen_t addrlen
; /* Length of address */
1397 int fd
; /* File descriptor */
1398 char s
[256]; /* String addresss */
1401 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: pid=%d", (int)getpid());
1404 * Check-in with launchd...
1407 ld_msg
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
1408 if ((ld_resp
= launch_msg(ld_msg
)) == NULL
)
1410 cupsdLogMessage(CUPSD_LOG_ERROR
,
1411 "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
1414 return; /* anti-compiler-warning */
1417 if (launch_data_get_type(ld_resp
) == LAUNCH_DATA_ERRNO
)
1419 errno
= launch_data_get_errno(ld_resp
);
1420 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Check-in failed: %s",
1423 return; /* anti-compiler-warning */
1427 * Get the sockets dictionary...
1430 if ((ld_sockets
= launch_data_dict_lookup(ld_resp
, LAUNCH_JOBKEY_SOCKETS
))
1433 cupsdLogMessage(CUPSD_LOG_ERROR
,
1434 "launchd_checkin: No sockets found to answer requests on.");
1436 return; /* anti-compiler-warning */
1440 * Get the array of listener sockets...
1443 if ((ld_array
= launch_data_dict_lookup(ld_sockets
, "Listeners")) == NULL
)
1445 cupsdLogMessage(CUPSD_LOG_ERROR
,
1446 "launchd_checkin: No sockets found to answer requests on.");
1448 return; /* anti-compiler-warning */
1452 * Add listening fd(s) to the Listener array...
1455 if (launch_data_get_type(ld_array
) == LAUNCH_DATA_ARRAY
)
1457 count
= launch_data_array_get_count(ld_array
);
1459 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: %d listeners.", (int)count
);
1461 for (i
= 0; i
< count
; i
++)
1464 * Get the launchd file descriptor and address...
1467 if ((tmp
= launch_data_array_get_index(ld_array
, i
)) != NULL
)
1469 fd
= launch_data_get_fd(tmp
);
1470 addrlen
= sizeof(addr
);
1472 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1474 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Unable to get local address for listener #%d: %s", (int)i
+ 1, strerror(errno
));
1478 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: Listener #%d at fd %d, \"%s\".", (int)i
+ 1, fd
, httpAddrString(&addr
, s
, sizeof(s
)));
1481 * Try to match the launchd socket address to one of the listeners...
1484 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1486 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1487 if (httpAddrEqual(&lis
->address
, &addr
))
1491 * Add a new listener If there's no match...
1496 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: Matched existing listener #%d to %s.", (int)i
+ 1, httpAddrString(&(lis
->address
), s
, sizeof(s
)));
1500 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: Adding new listener #%d for %s.", (int)i
+ 1, httpAddrString(&addr
, s
, sizeof(s
)));
1502 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1504 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Unable to allocate listener: %s.", strerror(errno
));
1508 cupsArrayAdd(Listeners
, lis
);
1510 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1516 if (httpAddrPort(&(lis
->address
)) == 443)
1517 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1518 # endif /* HAVE_SSL */
1523 launch_data_free(ld_msg
);
1524 launch_data_free(ld_resp
);
1525 # endif /* HAVE_LAUNCH_ACTIVATE_SOCKET */
1530 * 'launchd_checkout()' - Update the launchd KeepAlive file as needed.
1534 launchd_checkout(void)
1536 int fd
; /* File descriptor */
1540 * Create or remove the launchd KeepAlive file based on whether
1541 * there are active jobs, polling, browsing for remote printers or
1542 * shared printers to advertise...
1545 if (cupsArrayCount(ActiveJobs
) ||
1546 (Browsing
&& BrowseLocalProtocols
&& cupsArrayCount(Printers
)))
1548 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1549 "Creating launchd keepalive file \"" CUPS_KEEPALIVE
1552 if ((fd
= open(CUPS_KEEPALIVE
, O_RDONLY
| O_CREAT
| O_EXCL
, S_IRUSR
)) >= 0)
1557 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1558 "Removing launchd keepalive file \"" CUPS_KEEPALIVE
1561 unlink(CUPS_KEEPALIVE
);
1564 #endif /* HAVE_LAUNCHD */
1568 * 'parent_handler()' - Catch USR1/CHLD signals...
1572 parent_handler(int sig
) /* I - Signal */
1575 * Store the signal we got from the OS and return...
1578 parent_signal
= sig
;
1583 * 'process_children()' - Process all dead children...
1587 process_children(void)
1589 int status
; /* Exit status of child */
1590 int pid
, /* Process ID of child */
1591 job_id
; /* Job ID of child */
1592 cupsd_job_t
*job
; /* Current job */
1593 int i
; /* Looping var */
1594 char name
[1024]; /* Process name */
1595 const char *type
; /* Type of program */
1598 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1601 * Reset the dead_children flag...
1607 * Collect the exit status of some children...
1611 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1612 #elif defined(HAVE_WAIT3)
1613 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1615 if ((pid
= wait(&status
)) > 0)
1616 #endif /* HAVE_WAITPID */
1619 * Collect the name of the process that finished...
1622 cupsdFinishProcess(pid
, name
, sizeof(name
), &job_id
);
1625 * Delete certificates for CGI processes...
1629 cupsdDeleteCert(pid
);
1632 * Handle completed job filters...
1636 job
= cupsdFindJob(job_id
);
1642 for (i
= 0; job
->filters
[i
]; i
++)
1643 if (job
->filters
[i
] == pid
)
1646 if (job
->filters
[i
] || job
->backend
== pid
)
1649 * OK, this process has gone away; what's left?
1652 if (job
->filters
[i
])
1654 job
->filters
[i
] = -pid
;
1659 job
->backend
= -pid
;
1663 if (status
&& status
!= SIGTERM
&& status
!= SIGKILL
&&
1667 * An error occurred; save the exit status so we know to stop
1668 * the printer or cancel the job when all of the filters finish...
1670 * A negative status indicates that the backend failed and the
1671 * printer needs to be stopped.
1673 * In order to preserve the most serious status, we always log
1674 * when a process dies due to a signal (e.g. SIGABRT, SIGSEGV,
1675 * and SIGBUS) and prefer to log the backend exit status over a
1679 int old_status
= abs(job
->status
);
1681 if (WIFSIGNALED(status
) || /* This process crashed, or */
1682 !job
->status
|| /* No process had a status, or */
1683 (!job
->filters
[i
] && WIFEXITED(old_status
)))
1684 { /* Backend and filter didn't crash */
1685 if (job
->filters
[i
])
1686 job
->status
= status
; /* Filter failed */
1688 job
->status
= -status
; /* Backend failed */
1691 if (job
->state_value
== IPP_JOB_PROCESSING
&&
1692 job
->status_level
> CUPSD_LOG_ERROR
&&
1693 (job
->filters
[i
] || !WIFEXITED(status
)))
1695 char message
[1024]; /* New printer-state-message */
1698 job
->status_level
= CUPSD_LOG_ERROR
;
1700 snprintf(message
, sizeof(message
), "%s failed", type
);
1704 strlcpy(job
->printer
->state_message
, message
,
1705 sizeof(job
->printer
->state_message
));
1711 if (!job
->printer_message
&& job
->attrs
)
1713 if ((job
->printer_message
=
1714 ippFindAttribute(job
->attrs
, "job-printer-state-message",
1715 IPP_TAG_TEXT
)) == NULL
)
1716 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
,
1718 "job-printer-state-message",
1722 if (job
->printer_message
)
1723 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
1729 * If this is not the last file in a job, see if all of the
1730 * filters are done, and if so move to the next file.
1733 if (job
->current_file
< job
->num_files
&& job
->printer
)
1735 for (i
= 0; job
->filters
[i
] < 0; i
++);
1737 if (!job
->filters
[i
] &&
1738 (!job
->printer
->pc
|| !job
->printer
->pc
->single_file
||
1742 * Process the next file...
1745 cupsdContinueJob(job
);
1748 else if (job
->state_value
>= IPP_JOB_CANCELED
)
1751 * Remove the job from the active list if there are no processes still
1755 for (i
= 0; job
->filters
[i
] < 0; i
++);
1757 if (!job
->filters
[i
] && job
->backend
<= 0)
1758 cupsArrayRemove(ActiveJobs
, job
);
1764 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1765 * since they come when we kill/end a process...
1768 if (status
== SIGTERM
|| status
== SIGKILL
)
1770 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1771 "PID %d (%s) was terminated normally with signal %d.", pid
,
1774 else if (status
== SIGPIPE
)
1776 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1777 "PID %d (%s) did not catch or ignore signal %d.", pid
, name
,
1782 if (WIFEXITED(status
))
1784 int code
= WEXITSTATUS(status
); /* Exit code */
1787 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1788 "PID %d (%s) stopped with status %d (%s)", pid
, name
,
1789 code
, strerror(code
- 100));
1791 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1792 "PID %d (%s) stopped with status %d.", pid
, name
, code
);
1795 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) crashed on signal %d.",
1796 pid
, name
, WTERMSIG(status
));
1798 if (LogLevel
< CUPSD_LOG_DEBUG
)
1799 cupsdLogJob(job
, CUPSD_LOG_INFO
,
1800 "Hint: Try setting the LogLevel to \"debug\" to find out "
1804 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1809 * If wait*() is interrupted by a signal, tell main() to call us again...
1812 if (pid
< 0 && errno
== EINTR
)
1818 * 'select_timeout()' - Calculate the select timeout value.
1822 static long /* O - Number of seconds */
1823 select_timeout(int fds
) /* I - Number of descriptors returned */
1825 long timeout
; /* Timeout for select */
1826 time_t now
; /* Current time */
1827 cupsd_client_t
*con
; /* Client information */
1828 cupsd_job_t
*job
; /* Job information */
1829 cupsd_subscription_t
*sub
; /* Subscription information */
1830 const char *why
; /* Debugging aid */
1833 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout: JobHistoryUpdate=%ld",
1834 (long)JobHistoryUpdate
);
1837 * Check to see if any of the clients have pending data to be
1838 * processed; if so, the timeout should be 0...
1841 // TODO: Use httpGetReady()
1842 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1844 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1845 if (con
->http
->used
> 0)
1849 * If select has been active in the last second (fds > 0) or we have
1850 * many resources in use then don't bother trying to optimize the
1851 * timeout, just make it 1 second.
1854 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1858 * Otherwise, check all of the possible events that we need to wake for...
1862 timeout
= now
+ 86400; /* 86400 == 1 day */
1867 * When going to sleep, wake up to cancel jobs that don't complete in time.
1870 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1872 timeout
= SleepJobs
;
1873 why
= "cancel jobs before sleeping";
1875 #endif /* __APPLE__ */
1878 * Check whether we are accepting new connections...
1881 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1882 ListeningPaused
< timeout
)
1884 if (ListeningPaused
<= now
)
1887 timeout
= ListeningPaused
;
1889 why
= "resume listening";
1893 * Check the activity and close old clients...
1896 // TODO: Use httpGetActivity()
1897 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1899 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1900 if ((con
->http
->activity
+ Timeout
) < timeout
)
1902 timeout
= con
->http
->activity
+ Timeout
;
1903 why
= "timeout a client connection";
1907 * Write out changes to configuration and state files...
1910 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1912 timeout
= DirtyCleanTime
;
1913 why
= "write dirty config/state files";
1917 * Check for any job activity...
1920 if (JobHistoryUpdate
&& timeout
> JobHistoryUpdate
)
1922 timeout
= JobHistoryUpdate
;
1923 why
= "update job history";
1926 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1928 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1930 if (job
->cancel_time
&& job
->cancel_time
< timeout
)
1932 timeout
= job
->cancel_time
;
1933 why
= "cancel stuck jobs";
1936 if (job
->kill_time
&& job
->kill_time
< timeout
)
1938 timeout
= job
->kill_time
;
1939 why
= "kill unresponsive jobs";
1942 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1944 timeout
= job
->hold_until
;
1945 why
= "release held jobs";
1948 if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1951 why
= "start pending jobs";
1956 #ifdef HAVE_MALLINFO
1958 * Log memory usage every minute...
1961 if (LogLevel
>= CUPSD_LOG_DEBUG
&& (mallinfo_time
+ 60) < timeout
)
1963 timeout
= mallinfo_time
+ 60;
1964 why
= "display memory usage";
1966 #endif /* HAVE_MALLINFO */
1969 * Expire subscriptions as needed...
1972 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1974 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1975 if (!sub
->job
&& sub
->expire
&& sub
->expire
< timeout
)
1977 timeout
= sub
->expire
;
1978 why
= "expire subscription";
1982 * Adjust from absolute to relative time. We add 1 second to the timeout since
1983 * events occur after the timeout expires, and limit the timeout to 86400
1984 * seconds (1 day) to avoid select() timeout limits present on some operating
1988 timeout
= timeout
- now
+ 1;
1992 else if (timeout
> 86400)
1996 * Log and return the timeout value...
1999 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
2007 * 'sigchld_handler()' - Handle 'child' signals from old processes.
2011 sigchld_handler(int sig
) /* I - Signal number */
2016 * Flag that we have dead children...
2022 * Reset the signal handler as needed...
2025 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2026 signal(SIGCLD
, sigchld_handler
);
2027 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2032 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
2036 sighup_handler(int sig
) /* I - Signal number */
2040 NeedReload
= RELOAD_ALL
;
2041 ReloadTime
= time(NULL
);
2043 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2044 signal(SIGHUP
, sighup_handler
);
2045 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2050 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
2054 sigterm_handler(int sig
) /* I - Signal number */
2056 (void)sig
; /* remove compiler warnings... */
2059 * Flag that we should stop and return...
2067 * 'usage()' - Show scheduler usage.
2071 usage(int status
) /* O - Exit status */
2073 FILE *fp
= status
? stderr
: stdout
; /* Output file */
2076 _cupsLangPuts(fp
, _("Usage: cupsd [options]"));
2077 _cupsLangPuts(fp
, _("Options:"));
2078 _cupsLangPuts(fp
, _(" -c cupsd.conf Set cupsd.conf file to use."));
2079 _cupsLangPuts(fp
, _(" -f Run in the foreground."));
2080 _cupsLangPuts(fp
, _(" -F Run in the foreground but "
2081 "detach from console."));
2082 _cupsLangPuts(fp
, _(" -h Show this usage message."));
2083 _cupsLangPuts(fp
, _(" -l Run cupsd from launchd(8)."));
2084 _cupsLangPuts(fp
, _(" -t Test the configuration "