2 * "$Id: main.c 7925 2008-09-10 17:47:26Z mike $"
4 * Scheduler main loop for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * main() - Main entry for the CUPS scheduler.
18 * cupsdClosePipe() - Close a pipe as necessary.
19 * cupsdOpenPipe() - Create a pipe which is closed on exec.
20 * cupsdHoldSignals() - Hold child and termination signals.
21 * cupsdReleaseSignals() - Release signals for delivery.
22 * cupsdSetString() - Set a string value.
23 * cupsdSetStringf() - Set a formatted string value.
24 * launchd_checkin() - Check-in with launchd and collect the
26 * launchd_checkout() - Check-out with launchd.
27 * parent_handler() - Catch USR1/CHLD signals...
28 * process_children() - Process all dead children...
29 * select_timeout() - Calculate the select timeout value.
30 * sigchld_handler() - Handle 'child' signals from old processes.
31 * sighup_handler() - Handle 'hangup' signals to reconfigure the
33 * sigterm_handler() - Handle 'terminate' signals that stop the
35 * usage() - Show scheduler usage.
39 * Include necessary headers...
44 #include <sys/resource.h>
52 # define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
53 /* Name of the launchd KeepAlive file */
54 # ifndef LAUNCH_JOBKEY_KEEPALIVE
55 # define LAUNCH_JOBKEY_KEEPALIVE "KeepAlive"
56 # endif /* !LAUNCH_JOBKEY_KEEPALIVE */
57 # ifndef LAUNCH_JOBKEY_PATHSTATE
58 # define LAUNCH_JOBKEY_PATHSTATE "PathState"
59 # endif /* !LAUNCH_JOBKEY_PATHSTATE */
60 # ifndef LAUNCH_JOBKEY_SERVICEIPC
61 # define LAUNCH_JOBKEY_SERVICEIPC "ServiceIPC"
62 # endif /* !LAUNCH_JOBKEY_SERVICEIPC */
63 #endif /* HAVE_LAUNCH_H */
65 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
67 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
70 #endif /* HAVE_NOTIFY_H */
72 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
74 #endif /* __APPLE__ && HAVE_DLFCN_H */
82 static void launchd_checkin(void);
83 static void launchd_checkout(void);
84 #endif /* HAVE_LAUNCHD */
85 static void parent_handler(int sig
);
86 static void process_children(void);
87 static void sigchld_handler(int sig
);
88 static void sighup_handler(int sig
);
89 static void sigterm_handler(int sig
);
90 static long select_timeout(int fds
);
91 static void usage(int status
);
98 static int parent_signal
= 0;
99 /* Set to signal number from child */
100 static int holdcount
= 0; /* Number of times "hold" was called */
101 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
102 static sigset_t holdmask
; /* Old POSIX signal mask */
103 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
104 static int dead_children
= 0;
106 static int stop_scheduler
= 0;
107 /* Should the scheduler stop? */
109 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
110 static const char *PSQLibPath
= "/usr/lib/libPrintServiceQuota.dylib";
111 static const char *PSQLibFuncName
= "PSQUpdateQuota";
112 static void *PSQLibRef
; /* libPrintServiceQuota.dylib */
113 #endif /* HAVE_DLFCN_H */
117 * 'main()' - Main entry for the CUPS scheduler.
120 int /* O - Exit status */
121 main(int argc
, /* I - Number of command-line args */
122 char *argv
[]) /* I - Command-line arguments */
124 int i
; /* Looping var */
125 char *opt
; /* Option character */
126 int fg
; /* Run in the foreground */
127 int fds
; /* Number of ready descriptors */
128 cupsd_client_t
*con
; /* Current client */
129 cupsd_job_t
*job
; /* Current job */
130 cupsd_listener_t
*lis
; /* Current listener */
131 time_t current_time
, /* Current time */
132 activity
, /* Client activity timer */
133 browse_time
, /* Next browse send time */
134 senddoc_time
, /* Send-Document time */
135 expire_time
, /* Subscription expire time */
136 report_time
, /* Malloc/client/job report time */
137 event_time
; /* Last time an event notification was done */
138 long timeout
; /* Timeout for cupsdDoSelect() */
139 struct rlimit limit
; /* Runtime limit */
140 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
141 struct sigaction action
; /* Actions for POSIX signals */
142 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
144 cups_file_t
*fp
; /* Fake lpsched lock file */
145 struct stat statbuf
; /* Needed for checking lpsched FIFO */
148 int run_as_child
= 0;
149 /* Needed for Mac OS X fork/exec */
151 time_t netif_time
= 0; /* Time since last network update */
152 #endif /* __APPLE__ */
154 int launchd_idle_exit
;
155 /* Idle exit on select timeout? */
156 #endif /* HAVE_LAUNCHD */
161 * Check for setuid invocation, which we do not support!
164 if (getuid() != geteuid())
166 fputs("cupsd: Cannot run as a setuid program!\n", stderr
);
169 #endif /* HAVE_GETEUID */
172 * Check for command-line arguments...
178 if (getenv("CUPSD_LAUNCHD"))
183 #endif /* HAVE_LAUNCHD */
185 for (i
= 1; i
< argc
; i
++)
186 if (argv
[i
][0] == '-')
187 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
191 case 'C' : /* Run as child with config file */
194 #endif /* __APPLE__ */
196 case 'c' : /* Configuration file */
200 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
201 "after \"-c\" option!\n"));
205 if (argv
[i
][0] == '/')
208 * Absolute directory...
211 cupsdSetString(&ConfigurationFile
, argv
[i
]);
216 * Relative directory...
219 char *current
; /* Current directory */
223 * Allocate a buffer for the current working directory to
224 * reduce run-time stack usage; this approximates the
225 * behavior of some implementations of getcwd() when they
226 * are passed a NULL pointer.
229 if ((current
= malloc(1024)) == NULL
)
231 _cupsLangPuts(stderr
,
232 _("cupsd: Unable to get current directory!\n"));
236 if (!getcwd(current
, 1024))
238 _cupsLangPuts(stderr
,
239 _("cupsd: Unable to get current directory!\n"));
244 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
249 case 'f' : /* Run in foreground... */
253 case 'F' : /* Run in foreground, but disconnect from terminal... */
257 case 'h' : /* Show usage/help */
261 case 'l' : /* Started by launchd... */
266 _cupsLangPuts(stderr
, _("cupsd: launchd(8) support not compiled "
267 "in, running in normal mode.\n"));
269 #endif /* HAVE_LAUNCHD */
272 case 'p' : /* Stop immediately for profiling */
273 puts("Warning: -p option is for internal testing use only!");
278 case 't' : /* Test the cupsd.conf file... */
283 default : /* Unknown option */
284 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
285 "aborting!\n"), *opt
);
291 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting!\n"),
296 if (!ConfigurationFile
)
297 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
300 * If the user hasn't specified "-f", run in the background...
306 * Setup signal handlers for the parent...
309 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
310 sigset(SIGUSR1
, parent_handler
);
311 sigset(SIGCHLD
, parent_handler
);
313 sigset(SIGHUP
, SIG_IGN
);
314 #elif defined(HAVE_SIGACTION)
315 memset(&action
, 0, sizeof(action
));
316 sigemptyset(&action
.sa_mask
);
317 sigaddset(&action
.sa_mask
, SIGUSR1
);
318 action
.sa_handler
= parent_handler
;
319 sigaction(SIGUSR1
, &action
, NULL
);
320 sigaction(SIGCHLD
, &action
, NULL
);
322 sigemptyset(&action
.sa_mask
);
323 action
.sa_handler
= SIG_IGN
;
324 sigaction(SIGHUP
, &action
, NULL
);
326 signal(SIGUSR1
, parent_handler
);
327 signal(SIGCLD
, parent_handler
);
329 signal(SIGHUP
, SIG_IGN
);
330 #endif /* HAVE_SIGSET */
335 * OK, wait for the child to startup and send us SIGUSR1 or to crash
336 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
337 * might be sent by the init script to restart the scheduler...
340 for (; parent_signal
== 0;)
343 if (parent_signal
== SIGUSR1
)
351 else if (WIFEXITED(i
))
353 fprintf(stderr
, "cupsd: Child exited with status %d!\n",
359 fprintf(stderr
, "cupsd: Child exited on signal %d!\n", WTERMSIG(i
));
366 * Since CoreFoundation has an overly-agressive check for whether a
367 * process has forked but not exec'd (whether CF has been called or
368 * not...), we now have to exec ourselves with the "-f" option to
369 * eliminate their bogus warning messages.
372 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, (char *)0);
374 #endif /* __APPLE__ */
380 * Make sure we aren't tying up any filesystems...
387 * Disable core dumps...
390 getrlimit(RLIMIT_CORE
, &limit
);
392 setrlimit(RLIMIT_CORE
, &limit
);
395 * Disconnect from the controlling terminal...
401 * Close all open files...
404 getrlimit(RLIMIT_NOFILE
, &limit
);
406 for (i
= 0; i
< limit
.rlim_cur
&& i
< 1024; i
++)
410 * Redirect stdin/out/err to /dev/null...
413 open("/dev/null", O_RDONLY
);
414 open("/dev/null", O_WRONLY
);
415 open("/dev/null", O_WRONLY
);
420 * Set the timezone info...
426 setlocale(LC_TIME
, "");
430 * Set the maximum number of files...
433 getrlimit(RLIMIT_NOFILE
, &limit
);
435 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
436 if (limit
.rlim_max
> FD_SETSIZE
)
439 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
441 if (limit
.rlim_max
== RLIM_INFINITY
)
444 #endif /* RLIM_INFINITY */
445 MaxFDs
= limit
.rlim_max
;
447 limit
.rlim_cur
= MaxFDs
;
449 setrlimit(RLIMIT_NOFILE
, &limit
);
454 * Read configuration...
457 if (!cupsdReadConfiguration())
460 printf("%s contains errors\n", ConfigurationFile
);
462 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
466 else if (TestConfigFile
)
468 printf("%s is OK\n", ConfigurationFile
);
472 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
475 * Clean out the temporary directory...
478 cups_dir_t
*dir
; /* Temporary directory */
479 cups_dentry_t
*dent
; /* Directory entry */
480 char tempfile
[1024]; /* Temporary filename */
483 if ((dir
= cupsDirOpen(TempDir
)) != NULL
)
485 cupsdLogMessage(CUPSD_LOG_INFO
,
486 "Cleaning out old temporary files in \"%s\"...", TempDir
);
488 while ((dent
= cupsDirRead(dir
)) != NULL
)
490 snprintf(tempfile
, sizeof(tempfile
), "%s/%s", TempDir
, dent
->filename
);
492 if (unlink(tempfile
))
493 cupsdLogMessage(CUPSD_LOG_ERROR
,
494 "Unable to remove temporary file \"%s\" - %s",
495 tempfile
, strerror(errno
));
497 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Removed temporary file \"%s\"...",
504 cupsdLogMessage(CUPSD_LOG_ERROR
,
505 "Unable to open temporary directory \"%s\" - %s",
506 TempDir
, strerror(errno
));
513 * If we were started by launchd get the listen sockets file descriptors...
519 #endif /* HAVE_LAUNCHD */
521 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
523 * Load Print Service quota enforcement library (X Server only)
526 PSQLibRef
= dlopen(PSQLibPath
, RTLD_LAZY
);
529 PSQUpdateQuotaProc
= dlsym(PSQLibRef
, PSQLibFuncName
);
530 #endif /* __APPLE__ && HAVE_DLFCN_H */
533 * Startup the server...
539 * Catch hangup and child signals and ignore broken pipes...
542 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
543 sigset(SIGCHLD
, sigchld_handler
);
544 sigset(SIGHUP
, sighup_handler
);
545 sigset(SIGPIPE
, SIG_IGN
);
546 sigset(SIGTERM
, sigterm_handler
);
547 #elif defined(HAVE_SIGACTION)
548 memset(&action
, 0, sizeof(action
));
550 sigemptyset(&action
.sa_mask
);
551 sigaddset(&action
.sa_mask
, SIGTERM
);
552 sigaddset(&action
.sa_mask
, SIGCHLD
);
553 action
.sa_handler
= sigchld_handler
;
554 sigaction(SIGCHLD
, &action
, NULL
);
556 sigemptyset(&action
.sa_mask
);
557 sigaddset(&action
.sa_mask
, SIGHUP
);
558 action
.sa_handler
= sighup_handler
;
559 sigaction(SIGHUP
, &action
, NULL
);
561 sigemptyset(&action
.sa_mask
);
562 action
.sa_handler
= SIG_IGN
;
563 sigaction(SIGPIPE
, &action
, NULL
);
565 sigemptyset(&action
.sa_mask
);
566 sigaddset(&action
.sa_mask
, SIGTERM
);
567 sigaddset(&action
.sa_mask
, SIGCHLD
);
568 action
.sa_handler
= sigterm_handler
;
569 sigaction(SIGTERM
, &action
, NULL
);
571 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
572 signal(SIGHUP
, sighup_handler
);
573 signal(SIGPIPE
, SIG_IGN
);
574 signal(SIGTERM
, sigterm_handler
);
575 #endif /* HAVE_SIGSET */
579 * Try to create a fake lpsched lock file if one is not already there.
580 * Some Adobe applications need it under IRIX in order to enable
584 if ((fp
= cupsFileOpen("/var/spool/lp/SCHEDLOCK", "w")) == NULL
)
586 syslog(LOG_LPR
, "Unable to create fake lpsched lock file "
587 "\"/var/spool/lp/SCHEDLOCK\"\' - %s!",
592 fchmod(cupsFileNumber(fp
), 0644);
593 fchown(cupsFileNumber(fp
), User
, Group
);
600 * Initialize authentication certificates...
606 * If we are running in the background, signal the parent process that
607 * we are up and running...
611 if (!fg
|| run_as_child
)
614 #endif /* __APPLE__ */
617 * Send a signal to the parent process, but only if the parent is
618 * not PID 1 (init). This avoids accidentally shutting down the
619 * system on OpenBSD if you CTRL-C the server before it is up...
622 i
= getppid(); /* Save parent PID to avoid race condition */
630 * Start power management framework...
633 cupsdStartSystemMonitor();
634 #endif /* __APPLE__ */
637 * Send server-started event...
642 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
643 "Scheduler started via launchd.");
645 #endif /* HAVE_LAUNCHD */
647 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
648 "Scheduler started in foreground.");
650 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
651 "Scheduler started in background.");
654 * Start any pending print jobs...
663 current_time
= time(NULL
);
664 browse_time
= current_time
;
665 event_time
= current_time
;
666 expire_time
= current_time
;
669 senddoc_time
= current_time
;
671 while (!stop_scheduler
)
674 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
675 "main: Top of loop, dead_children=%d, NeedReload=%d",
676 dead_children
, NeedReload
);
680 * Check if there are dead children to handle...
687 * Check if we need to load the server configuration file...
693 * Close any idle clients...
696 if (cupsArrayCount(Clients
) > 0)
698 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
700 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
701 if (con
->http
.state
== HTTP_WAITING
)
702 cupsdCloseClient(con
);
704 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
706 cupsdPauseListening();
710 * Check for any active jobs...
713 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
715 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
716 if (job
->state_value
== IPP_JOB_PROCESSING
)
720 * Restart if all clients are closed and all jobs finished, or
721 * if the reload timeout has elapsed...
724 if ((cupsArrayCount(Clients
) == 0 &&
725 (!job
|| NeedReload
!= RELOAD_ALL
)) ||
726 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
729 * Shutdown the server...
735 * Read configuration...
738 if (!cupsdReadConfiguration())
740 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
749 * If we were started by launchd get the listen sockets file descriptors...
755 #endif /* HAVE_LAUNCHD */
758 * Startup the server...
764 * Send a server-restarted event...
767 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
768 "Scheduler restarted.");
773 * Check for available input or ready output. If cupsdDoSelect()
774 * returns 0 or -1, something bad happened and we should exit
777 * Note that we at least have one listening socket open at all
781 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
786 * If no other work is scheduled and we're being controlled by
787 * launchd then timeout after 'LaunchdTimeout' seconds of
791 if (timeout
== 86400 && Launchd
&& LaunchdTimeout
&& !NumPolled
&&
792 !cupsArrayCount(ActiveJobs
) &&
794 (!BrowseRemoteProtocols
&&
795 (!NumBrowsers
|| !BrowseLocalProtocols
||
796 cupsArrayCount(Printers
) == 0))))
798 timeout
= LaunchdTimeout
;
799 launchd_idle_exit
= 1;
802 launchd_idle_exit
= 0;
803 #endif /* HAVE_LAUNCHD */
805 if ((fds
= cupsdDoSelect(timeout
)) < 0)
808 * Got an error from select!
812 cupsd_printer_t
*p
; /* Current printer */
813 #endif /* HAVE_DNSSD */
816 if (errno
== EINTR
) /* Just interrupted by a signal */
820 * Log all sorts of debug info to help track down the problem.
823 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
826 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
828 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
829 cupsdLogMessage(CUPSD_LOG_EMERG
,
830 "Clients[%d] = %d, file = %d, state = %d",
831 i
, con
->http
.fd
, con
->file
, con
->http
.state
);
833 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
835 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
836 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
838 cupsdLogMessage(CUPSD_LOG_EMERG
, "BrowseSocket = %d", BrowseSocket
);
840 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
843 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
845 #endif /* __APPLE__ */
847 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
849 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
850 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
852 job
->status_buffer
? job
->status_buffer
->fd
: -1,
853 job
->print_pipes
[0], job
->print_pipes
[1],
854 job
->back_pipes
[0], job
->back_pipes
[1]);
857 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
859 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
860 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
861 p
->reg_name
? p
->reg_name
: "(null)");
862 #endif /* HAVE_DNSSD */
867 current_time
= time(NULL
);
870 * Write dirty config/state files...
873 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
878 * If we are going to sleep and still have pending jobs, stop them after
879 * a period of time...
882 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
883 cupsArrayCount(PrintingJobs
) > 0)
888 #endif /* __APPLE__ */
892 * Update the network interfaces once a minute...
895 if ((current_time
- netif_time
) >= 60)
897 netif_time
= current_time
;
900 #endif /* !__APPLE__ */
904 * If no other work was scheduled and we're being controlled by launchd
905 * then timeout after 'LaunchdTimeout' seconds of inactivity...
908 if (!fds
&& launchd_idle_exit
)
910 cupsdLogMessage(CUPSD_LOG_INFO
,
911 "Printer sharing is off and there are no jobs pending, "
912 "will restart on demand.");
916 #endif /* HAVE_LAUNCHD */
919 * Resume listening for new connections as needed...
922 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
923 cupsArrayCount(Clients
) < MaxClients
)
924 cupsdResumeListening();
927 * Expire subscriptions and unload completed jobs as needed...
930 if (current_time
> expire_time
)
932 if (cupsArrayCount(Subscriptions
) > 0)
933 cupsdExpireSubscriptions(NULL
, NULL
);
935 cupsdUnloadCompletedJobs();
937 expire_time
= current_time
;
941 * Update the browse list as needed...
947 if ((BrowseRemoteProtocols
& BROWSE_SLP
) &&
948 BrowseSLPRefresh
<= current_time
)
949 cupsdUpdateSLPBrowse();
950 #endif /* HAVE_LIBSLP */
953 if ((BrowseRemoteProtocols
& BROWSE_LDAP
) &&
954 BrowseLDAPRefresh
<= current_time
)
955 cupsdUpdateLDAPBrowse();
956 #endif /* HAVE_LDAP */
959 if (Browsing
&& current_time
> browse_time
)
961 cupsdSendBrowseList();
962 browse_time
= current_time
;
966 * Update the root certificate once every 5 minutes if we have client
970 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
971 !RunUser
&& cupsArrayCount(Clients
))
974 * Update the root certificate...
978 cupsdAddCert(0, "root", NULL
);
982 * Check for new data on the client sockets...
985 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
987 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
990 * Process pending data in the input buffer...
995 cupsdReadClient(con
);
1000 * Check the activity and close old clients...
1003 activity
= current_time
- Timeout
;
1004 if (con
->http
.activity
< activity
&& !con
->pipe_pid
)
1006 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1007 "Closing client %d after %d seconds of inactivity...",
1008 con
->http
.fd
, Timeout
);
1010 cupsdCloseClient(con
);
1016 * Update any pending multi-file documents...
1019 if ((current_time
- senddoc_time
) >= 10)
1022 senddoc_time
= current_time
;
1026 * Log statistics at most once a minute when in debug mode...
1029 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
1031 size_t string_count
, /* String count */
1032 alloc_bytes
, /* Allocated string bytes */
1033 total_bytes
; /* Total string bytes */
1034 #ifdef HAVE_MALLINFO
1035 struct mallinfo mem
; /* Malloc information */
1039 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1040 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1041 mem
.usmblks
+ mem
.uordblks
);
1042 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1043 mem
.fsmblks
+ mem
.fordblks
);
1044 #endif /* HAVE_MALLINFO */
1046 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1047 cupsArrayCount(Clients
));
1048 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1049 cupsArrayCount(Jobs
));
1050 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1051 cupsArrayCount(ActiveJobs
));
1052 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1053 cupsArrayCount(Printers
));
1054 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers-implicit=%d",
1055 cupsArrayCount(ImplicitPrinters
));
1057 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1058 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1059 "Report: stringpool-string-count=" CUPS_LLFMT
,
1060 CUPS_LLCAST string_count
);
1061 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1062 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1063 CUPS_LLCAST alloc_bytes
);
1064 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1065 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1066 CUPS_LLCAST total_bytes
);
1068 report_time
= current_time
;
1072 * Handle OS-specific event notification for any events that have
1073 * accumulated. Don't send these more than once a second...
1076 if (LastEvent
&& (current_time
- event_time
) >= 1)
1078 #ifdef HAVE_NOTIFY_POST
1079 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1080 CUPSD_EVENT_PRINTER_DELETED
|
1081 CUPSD_EVENT_PRINTER_MODIFIED
))
1083 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1084 "notify_post(\"com.apple.printerListChange\")");
1085 notify_post("com.apple.printerListChange");
1088 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1090 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1091 "notify_post(\"com.apple.printerHistoryChange\")");
1092 notify_post("com.apple.printerHistoryChange");
1095 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1096 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1097 CUPSD_EVENT_JOB_PROGRESS
))
1099 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1100 "notify_post(\"com.apple.jobChange\")");
1101 notify_post("com.apple.jobChange");
1103 #endif /* HAVE_NOTIFY_POST */
1106 * Reset the accumulated events...
1109 LastEvent
= CUPSD_EVENT_NONE
;
1110 event_time
= current_time
;
1115 * Log a message based on what happened...
1120 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1121 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1122 "Scheduler shutting down normally.");
1126 cupsdLogMessage(CUPSD_LOG_ERROR
,
1127 "Scheduler shutting down due to program error.");
1128 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1129 "Scheduler shutting down due to program error.");
1133 * Close all network clients...
1140 * Update the launchd KeepAlive file as needed...
1145 #endif /* HAVE_LAUNCHD */
1155 * Stop monitoring system event monitoring...
1158 cupsdStopSystemMonitor();
1159 #endif /* __APPLE__ */
1163 * Free the scheduler's Kerberos context...
1168 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1172 if (krb5_init_context
!= NULL
)
1173 # endif /* __APPLE__ */
1174 if (KerberosContext
)
1175 krb5_free_context(KerberosContext
);
1176 #endif /* HAVE_GSSAPI */
1178 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
1180 * Unload Print Service quota enforcement library (X Server only)
1183 PSQUpdateQuotaProc
= NULL
;
1189 #endif /* __APPLE__ && HAVE_DLFCN_H */
1193 * Remove the fake IRIX lpsched lock file, but only if the existing
1194 * file is not a FIFO which indicates that the real IRIX lpsched is
1198 if (!stat("/var/spool/lp/FIFO", &statbuf
))
1199 if (!S_ISFIFO(statbuf
.st_mode
))
1200 unlink("/var/spool/lp/SCHEDLOCK");
1205 return (!stop_scheduler
);
1210 * 'cupsdClosePipe()' - Close a pipe as necessary.
1214 cupsdClosePipe(int *fds
) /* I - Pipe file descriptors (2) */
1217 * Close file descriptors as needed...
1235 * 'cupsdOpenPipe()' - Create a pipe which is closed on exec.
1238 int /* O - 0 on success, -1 on error */
1239 cupsdOpenPipe(int *fds
) /* O - Pipe file descriptors (2) */
1242 * Create the pipe...
1254 * Set the "close on exec" flag on each end of the pipe...
1257 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1268 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1280 * Return 0 indicating success...
1288 * 'cupsdClearString()' - Clear a string.
1292 cupsdClearString(char **s
) /* O - String value */
1303 * 'cupsdHoldSignals()' - Hold child and termination signals.
1307 cupsdHoldSignals(void)
1309 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1310 sigset_t newmask
; /* New POSIX signal mask */
1311 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1321 #elif defined(HAVE_SIGACTION)
1322 sigemptyset(&newmask
);
1323 sigaddset(&newmask
, SIGTERM
);
1324 sigaddset(&newmask
, SIGCHLD
);
1325 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1326 #endif /* HAVE_SIGSET */
1331 * 'cupsdReleaseSignals()' - Release signals for delivery.
1335 cupsdReleaseSignals(void)
1344 #elif defined(HAVE_SIGACTION)
1345 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1346 #endif /* HAVE_SIGSET */
1351 * 'cupsdSetString()' - Set a string value.
1355 cupsdSetString(char **s
, /* O - New string */
1356 const char *v
) /* I - String value */
1365 *s
= _cupsStrAlloc(v
);
1372 * 'cupsdSetStringf()' - Set a formatted string value.
1376 cupsdSetStringf(char **s
, /* O - New string */
1377 const char *f
, /* I - Printf-style format string */
1378 ...) /* I - Additional args as needed */
1380 char v
[4096]; /* Formatting string value */
1381 va_list ap
; /* Argument pointer */
1382 char *olds
; /* Old string */
1393 vsnprintf(v
, sizeof(v
), f
, ap
);
1396 *s
= _cupsStrAlloc(v
);
1408 * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
1412 launchd_checkin(void)
1414 size_t i
, /* Looping var */
1415 count
; /* Numebr of listeners */
1416 int portnum
; /* Port number */
1417 launch_data_t ld_msg
, /* Launch data message */
1418 ld_resp
, /* Launch data response */
1419 ld_array
, /* Launch data array */
1420 ld_sockets
, /* Launch data sockets dictionary */
1421 tmp
; /* Launch data */
1422 cupsd_listener_t
*lis
; /* Listeners array */
1423 http_addr_t addr
; /* Address variable */
1424 socklen_t addrlen
; /* Length of address */
1425 int fd
; /* File descriptor */
1426 char s
[256]; /* String addresss */
1429 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: pid=%d", (int)getpid());
1432 * Check-in with launchd...
1435 ld_msg
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
1436 if ((ld_resp
= launch_msg(ld_msg
)) == NULL
)
1438 cupsdLogMessage(CUPSD_LOG_ERROR
,
1439 "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
1442 return; /* anti-compiler-warning */
1445 if (launch_data_get_type(ld_resp
) == LAUNCH_DATA_ERRNO
)
1447 errno
= launch_data_get_errno(ld_resp
);
1448 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Check-in failed: %s",
1451 return; /* anti-compiler-warning */
1455 * Get the sockets dictionary...
1458 if ((ld_sockets
= launch_data_dict_lookup(ld_resp
, LAUNCH_JOBKEY_SOCKETS
))
1461 cupsdLogMessage(CUPSD_LOG_ERROR
,
1462 "launchd_checkin: No sockets found to answer requests on!");
1464 return; /* anti-compiler-warning */
1468 * Get the array of listener sockets...
1471 if ((ld_array
= launch_data_dict_lookup(ld_sockets
, "Listeners")) == NULL
)
1473 cupsdLogMessage(CUPSD_LOG_ERROR
,
1474 "launchd_checkin: No sockets found to answer requests on!");
1476 return; /* anti-compiler-warning */
1480 * Add listening fd(s) to the Listener array...
1483 if (launch_data_get_type(ld_array
) == LAUNCH_DATA_ARRAY
)
1485 count
= launch_data_array_get_count(ld_array
);
1487 for (i
= 0; i
< count
; i
++)
1490 * Get the launchd file descriptor and address...
1493 if ((tmp
= launch_data_array_get_index(ld_array
, i
)) != NULL
)
1495 fd
= launch_data_get_fd(tmp
);
1496 addrlen
= sizeof(addr
);
1498 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1500 cupsdLogMessage(CUPSD_LOG_ERROR
,
1501 "launchd_checkin: Unable to get local address - %s",
1507 * Try to match the launchd socket address to one of the listeners...
1510 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1512 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1513 if (httpAddrEqual(&lis
->address
, &addr
))
1517 * Add a new listener If there's no match...
1522 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1523 "launchd_checkin: Matched existing listener %s with fd %d...",
1524 httpAddrString(&(lis
->address
), s
, sizeof(s
)), fd
);
1528 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1529 "launchd_checkin: Adding new listener %s with fd %d...",
1530 httpAddrString(&addr
, s
, sizeof(s
)), fd
);
1532 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1534 cupsdLogMessage(CUPSD_LOG_ERROR
,
1535 "launchd_checkin: Unable to allocate listener - %s.",
1540 cupsArrayAdd(Listeners
, lis
);
1542 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1551 if (lis
->address
.addr
.sa_family
== AF_INET6
)
1552 portnum
= ntohs(lis
->address
.ipv6
.sin6_port
);
1554 # endif /* AF_INET6 */
1555 if (lis
->address
.addr
.sa_family
== AF_INET
)
1556 portnum
= ntohs(lis
->address
.ipv4
.sin_port
);
1559 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1560 # endif /* HAVE_SSL */
1565 launch_data_free(ld_msg
);
1566 launch_data_free(ld_resp
);
1571 * 'launchd_checkout()' - Update the launchd KeepAlive file as needed.
1575 launchd_checkout(void)
1577 int fd
; /* File descriptor */
1581 * Create or remove the launchd KeepAlive file based on whether
1582 * there are active jobs, polling, browsing for remote printers or
1583 * shared printers to advertise...
1586 if ((cupsArrayCount(ActiveJobs
) || NumPolled
||
1588 (BrowseRemoteProtocols
||
1589 (BrowseLocalProtocols
&& NumBrowsers
&& cupsArrayCount(Printers
))))))
1591 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1592 "Creating launchd keepalive file \"" CUPS_KEEPALIVE
"\"...");
1594 if ((fd
= open(CUPS_KEEPALIVE
, O_RDONLY
| O_CREAT
| O_EXCL
, S_IRUSR
)) >= 0)
1599 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1600 "Removing launchd keepalive file \"" CUPS_KEEPALIVE
"\"...");
1602 unlink(CUPS_KEEPALIVE
);
1605 #endif /* HAVE_LAUNCHD */
1609 * 'parent_handler()' - Catch USR1/CHLD signals...
1613 parent_handler(int sig
) /* I - Signal */
1616 * Store the signal we got from the OS and return...
1619 parent_signal
= sig
;
1624 * 'process_children()' - Process all dead children...
1628 process_children(void)
1630 int status
; /* Exit status of child */
1631 int pid
; /* Process ID of child */
1632 cupsd_job_t
*job
; /* Current job */
1633 int i
; /* Looping var */
1634 char name
[1024]; /* Process name */
1637 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1640 * Reset the dead_children flag...
1646 * Collect the exit status of some children...
1650 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1651 #elif defined(HAVE_WAIT3)
1652 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1654 if ((pid
= wait(&status
)) > 0)
1655 #endif /* HAVE_WAITPID */
1658 * Collect the name of the process that finished...
1661 cupsdFinishProcess(pid
, name
, sizeof(name
));
1664 * Delete certificates for CGI processes...
1668 cupsdDeleteCert(pid
);
1671 * Lookup the PID in the jobs list...
1674 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1676 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1677 if (job
->state_value
>= IPP_JOB_HELD
&& (job
->filters
[0] || job
->backend
))
1679 for (i
= 0; job
->filters
[i
]; i
++)
1680 if (job
->filters
[i
] == pid
)
1683 if (job
->filters
[i
] || job
->backend
== pid
)
1686 * OK, this process has gone away; what's left?
1689 if (job
->filters
[i
])
1690 job
->filters
[i
] = -pid
;
1692 job
->backend
= -pid
;
1694 if (status
&& job
->status
>= 0)
1697 * An error occurred; save the exit status so we know to stop
1698 * the printer or cancel the job when all of the filters finish...
1700 * A negative status indicates that the backend failed and the
1701 * printer needs to be stopped.
1704 if (job
->filters
[i
])
1705 job
->status
= status
; /* Filter failed */
1707 job
->status
= -status
; /* Backend failed */
1709 if (job
->printer
&& !(job
->printer
->type
& CUPS_PRINTER_FAX
) &&
1710 job
->status_level
> CUPSD_LOG_ERROR
)
1712 job
->status_level
= CUPSD_LOG_ERROR
;
1714 snprintf(job
->printer
->state_message
,
1715 sizeof(job
->printer
->state_message
), "%s failed", name
);
1716 cupsdAddPrinterHistory(job
->printer
);
1721 * If this is not the last file in a job, see if all of the
1722 * filters are done, and if so move to the next file.
1725 if (job
->current_file
< job
->num_files
)
1727 for (i
= 0; job
->filters
[i
] < 0; i
++);
1729 if (!job
->filters
[i
])
1732 * Process the next file...
1735 cupsdFinishJob(job
);
1743 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1744 * since they come when we kill/end a process...
1747 if (status
== SIGTERM
|| status
== SIGKILL
)
1749 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1750 "PID %d (%s) was terminated normally with signal %d.",
1755 if (WIFEXITED(status
))
1756 cupsdLogMessage(CUPSD_LOG_DEBUG
, "PID %d (%s) stopped with status %d!",
1757 pid
, name
, WEXITSTATUS(status
));
1759 cupsdLogMessage(CUPSD_LOG_ERROR
, "PID %d (%s) crashed on signal %d!",
1760 pid
, name
, WTERMSIG(status
));
1762 if (LogLevel
< CUPSD_LOG_DEBUG
)
1763 cupsdLogMessage(CUPSD_LOG_INFO
,
1764 "Hint: Try setting the LogLevel to \"debug\" to find "
1768 cupsdLogMessage(CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1773 * If wait*() is interrupted by a signal, tell main() to call us again...
1776 if (pid
< 0 && errno
== EINTR
)
1782 * 'select_timeout()' - Calculate the select timeout value.
1786 static long /* O - Number of seconds */
1787 select_timeout(int fds
) /* I - Number of descriptors returned */
1789 long timeout
; /* Timeout for select */
1790 time_t now
; /* Current time */
1791 cupsd_client_t
*con
; /* Client information */
1792 cupsd_printer_t
*p
; /* Printer information */
1793 cupsd_job_t
*job
; /* Job information */
1794 cupsd_subscription_t
*sub
; /* Subscription information */
1795 const char *why
; /* Debugging aid */
1799 * Check to see if any of the clients have pending data to be
1800 * processed; if so, the timeout should be 0...
1803 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1805 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1806 if (con
->http
.used
> 0)
1810 * If select has been active in the last second (fds > 0) or we have
1811 * many resources in use then don't bother trying to optimize the
1812 * timeout, just make it 1 second.
1815 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1819 * Otherwise, check all of the possible events that we need to wake for...
1823 timeout
= now
+ 86400; /* 86400 == 1 day */
1828 * When going to sleep, wake up to cancel jobs that don't complete in time.
1831 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1833 timeout
= SleepJobs
;
1834 why
= "cancel jobs before sleeping";
1836 #endif /* __APPLE__ */
1839 * Check whether we are accepting new connections...
1842 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1843 ListeningPaused
< timeout
)
1845 if (ListeningPaused
<= now
)
1848 timeout
= ListeningPaused
;
1850 why
= "resume listening";
1854 * Check the activity and close old clients...
1857 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1859 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1860 if ((con
->http
.activity
+ Timeout
) < timeout
)
1862 timeout
= con
->http
.activity
+ Timeout
;
1863 why
= "timeout a client connection";
1867 * Update the browse list as needed...
1870 if (Browsing
&& BrowseLocalProtocols
)
1873 if ((BrowseLocalProtocols
& BROWSE_SLP
) && (BrowseSLPRefresh
< timeout
))
1875 timeout
= BrowseSLPRefresh
;
1876 why
= "update SLP browsing";
1878 #endif /* HAVE_LIBSLP */
1881 if ((BrowseLocalProtocols
& BROWSE_LDAP
) && (BrowseLDAPRefresh
< timeout
))
1883 timeout
= BrowseLDAPRefresh
;
1884 why
= "update LDAP browsing";
1886 #endif /* HAVE_LDAP */
1888 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && NumBrowsers
)
1890 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1892 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1894 if (p
->type
& CUPS_PRINTER_REMOTE
)
1896 if ((p
->browse_time
+ BrowseTimeout
) < timeout
)
1898 timeout
= p
->browse_time
+ BrowseTimeout
;
1899 why
= "browse timeout a printer";
1902 else if (p
->shared
&& !(p
->type
& CUPS_PRINTER_IMPLICIT
))
1904 if (BrowseInterval
&& (p
->browse_time
+ BrowseInterval
) < timeout
)
1906 timeout
= p
->browse_time
+ BrowseInterval
;
1907 why
= "send browse update";
1915 * Write out changes to configuration and state files...
1918 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1920 timeout
= DirtyCleanTime
;
1921 why
= "write dirty config/state files";
1925 * Check for any active jobs...
1928 if (cupsArrayCount(ActiveJobs
) > 0)
1930 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1932 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1933 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1935 timeout
= job
->hold_until
;
1936 why
= "release held jobs";
1938 else if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1941 why
= "start pending jobs";
1946 #ifdef HAVE_MALLINFO
1948 * Log memory usage every minute...
1951 if (LogLevel
>= CUPSD_LOG_DEBUG
&& (mallinfo_time
+ 60) < timeout
)
1953 timeout
= mallinfo_time
+ 60;
1954 why
= "display memory usage";
1956 #endif /* HAVE_MALLINFO */
1959 * Expire subscriptions as needed...
1962 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1964 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1965 if (!sub
->job
&& sub
->expire
&& sub
->expire
< timeout
)
1967 timeout
= sub
->expire
;
1968 why
= "expire subscription";
1972 * Adjust from absolute to relative time. If p->browse_time above
1973 * was 0 then we can end up with a negative value here, so check.
1974 * We add 1 second to the timeout since events occur after the
1975 * timeout expires, and limit the timeout to 86400 seconds (1 day)
1976 * to avoid select() timeout limits present on some operating
1980 timeout
= timeout
- now
+ 1;
1984 else if (timeout
> 86400)
1988 * Log and return the timeout value...
1991 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
1999 * 'sigchld_handler()' - Handle 'child' signals from old processes.
2003 sigchld_handler(int sig
) /* I - Signal number */
2008 * Flag that we have dead children...
2014 * Reset the signal handler as needed...
2017 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2018 signal(SIGCLD
, sigchld_handler
);
2019 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2024 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
2028 sighup_handler(int sig
) /* I - Signal number */
2032 NeedReload
= RELOAD_ALL
;
2033 ReloadTime
= time(NULL
);
2035 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2036 signal(SIGHUP
, sighup_handler
);
2037 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2042 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
2046 sigterm_handler(int sig
) /* I - Signal number */
2048 (void)sig
; /* remove compiler warnings... */
2051 * Flag that we should stop and return...
2059 * 'usage()' - Show scheduler usage.
2063 usage(int status
) /* O - Exit status */
2065 _cupsLangPuts(status
? stderr
: stdout
,
2066 _("Usage: cupsd [-c config-file] [-f] [-F] [-h] [-l]\n"
2068 "-c config-file Load alternate configuration file\n"
2069 "-f Run in the foreground\n"
2070 "-F Run in the foreground but detach\n"
2071 "-h Show this usage message\n"
2072 "-l Run cupsd from launchd(8)\n"));
2078 * End of "$Id: main.c 7925 2008-09-10 17:47:26Z mike $".