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 */
150 use_sysman
= 1; /* Use system management functions? */
152 time_t netif_time
= 0; /* Time since last network update */
153 #endif /* __APPLE__ */
155 int launchd_idle_exit
;
156 /* Idle exit on select timeout? */
157 #endif /* HAVE_LAUNCHD */
162 * Check for setuid invocation, which we do not support!
165 if (getuid() != geteuid())
167 fputs("cupsd: Cannot run as a setuid program!\n", stderr
);
170 #endif /* HAVE_GETEUID */
173 * Check for command-line arguments...
179 if (getenv("CUPSD_LAUNCHD"))
184 #endif /* HAVE_LAUNCHD */
186 for (i
= 1; i
< argc
; i
++)
187 if (argv
[i
][0] == '-')
188 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
192 case 'S' : /* Disable system management functions */
193 puts("Warning: -S (disable system management) for internal testing use only!");
197 case 'C' : /* Run as child with config file */
200 #endif /* __APPLE__ */
202 case 'c' : /* Configuration file */
206 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
207 "after \"-c\" option!\n"));
211 if (argv
[i
][0] == '/')
214 * Absolute directory...
217 cupsdSetString(&ConfigurationFile
, argv
[i
]);
222 * Relative directory...
225 char *current
; /* Current directory */
229 * Allocate a buffer for the current working directory to
230 * reduce run-time stack usage; this approximates the
231 * behavior of some implementations of getcwd() when they
232 * are passed a NULL pointer.
235 if ((current
= malloc(1024)) == NULL
)
237 _cupsLangPuts(stderr
,
238 _("cupsd: Unable to get current directory!\n"));
242 if (!getcwd(current
, 1024))
244 _cupsLangPuts(stderr
,
245 _("cupsd: Unable to get current directory!\n"));
250 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
255 case 'f' : /* Run in foreground... */
259 case 'F' : /* Run in foreground, but disconnect from terminal... */
263 case 'h' : /* Show usage/help */
267 case 'l' : /* Started by launchd... */
272 _cupsLangPuts(stderr
, _("cupsd: launchd(8) support not compiled "
273 "in, running in normal mode.\n"));
275 #endif /* HAVE_LAUNCHD */
278 case 'p' : /* Stop immediately for profiling */
279 puts("Warning: -p (startup profiling) is for internal testing use only!");
284 case 'P' : /* Disable security profiles */
285 puts("Warning: -P (disable security profiles) is for internal testing use only!");
289 case 't' : /* Test the cupsd.conf file... */
294 default : /* Unknown option */
295 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
296 "aborting!\n"), *opt
);
302 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting!\n"),
307 if (!ConfigurationFile
)
308 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
311 * If the user hasn't specified "-f", run in the background...
317 * Setup signal handlers for the parent...
320 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
321 sigset(SIGUSR1
, parent_handler
);
322 sigset(SIGCHLD
, parent_handler
);
324 sigset(SIGHUP
, SIG_IGN
);
325 #elif defined(HAVE_SIGACTION)
326 memset(&action
, 0, sizeof(action
));
327 sigemptyset(&action
.sa_mask
);
328 sigaddset(&action
.sa_mask
, SIGUSR1
);
329 action
.sa_handler
= parent_handler
;
330 sigaction(SIGUSR1
, &action
, NULL
);
331 sigaction(SIGCHLD
, &action
, NULL
);
333 sigemptyset(&action
.sa_mask
);
334 action
.sa_handler
= SIG_IGN
;
335 sigaction(SIGHUP
, &action
, NULL
);
337 signal(SIGUSR1
, parent_handler
);
338 signal(SIGCLD
, parent_handler
);
340 signal(SIGHUP
, SIG_IGN
);
341 #endif /* HAVE_SIGSET */
346 * OK, wait for the child to startup and send us SIGUSR1 or to crash
347 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
348 * might be sent by the init script to restart the scheduler...
351 for (; parent_signal
== 0;)
354 if (parent_signal
== SIGUSR1
)
362 else if (WIFEXITED(i
))
364 fprintf(stderr
, "cupsd: Child exited with status %d!\n",
370 fprintf(stderr
, "cupsd: Child exited on signal %d!\n", WTERMSIG(i
));
377 * Since CoreFoundation has an overly-agressive check for whether a
378 * process has forked but not exec'd (whether CF has been called or
379 * not...), we now have to exec ourselves with the "-f" option to
380 * eliminate their bogus warning messages.
383 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, (char *)0);
385 #endif /* __APPLE__ */
391 * Make sure we aren't tying up any filesystems...
398 * Disable core dumps...
401 getrlimit(RLIMIT_CORE
, &limit
);
403 setrlimit(RLIMIT_CORE
, &limit
);
406 * Disconnect from the controlling terminal...
412 * Close all open files...
415 getrlimit(RLIMIT_NOFILE
, &limit
);
417 for (i
= 0; i
< limit
.rlim_cur
&& i
< 1024; i
++)
421 * Redirect stdin/out/err to /dev/null...
424 open("/dev/null", O_RDONLY
);
425 open("/dev/null", O_WRONLY
);
426 open("/dev/null", O_WRONLY
);
431 * Set the timezone info...
437 setlocale(LC_TIME
, "");
441 * Set the maximum number of files...
444 getrlimit(RLIMIT_NOFILE
, &limit
);
446 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
447 if (limit
.rlim_max
> FD_SETSIZE
)
450 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
452 if (limit
.rlim_max
== RLIM_INFINITY
)
455 #endif /* RLIM_INFINITY */
456 MaxFDs
= limit
.rlim_max
;
458 limit
.rlim_cur
= MaxFDs
;
460 setrlimit(RLIMIT_NOFILE
, &limit
);
465 * Read configuration...
468 if (!cupsdReadConfiguration())
471 printf("%s contains errors\n", ConfigurationFile
);
473 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
477 else if (TestConfigFile
)
479 printf("%s is OK\n", ConfigurationFile
);
483 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
486 * Clean out the temporary directory...
489 cups_dir_t
*dir
; /* Temporary directory */
490 cups_dentry_t
*dent
; /* Directory entry */
491 char tempfile
[1024]; /* Temporary filename */
494 if ((dir
= cupsDirOpen(TempDir
)) != NULL
)
496 cupsdLogMessage(CUPSD_LOG_INFO
,
497 "Cleaning out old temporary files in \"%s\"...", TempDir
);
499 while ((dent
= cupsDirRead(dir
)) != NULL
)
501 snprintf(tempfile
, sizeof(tempfile
), "%s/%s", TempDir
, dent
->filename
);
503 if (unlink(tempfile
))
504 cupsdLogMessage(CUPSD_LOG_ERROR
,
505 "Unable to remove temporary file \"%s\" - %s",
506 tempfile
, strerror(errno
));
508 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Removed temporary file \"%s\"...",
515 cupsdLogMessage(CUPSD_LOG_ERROR
,
516 "Unable to open temporary directory \"%s\" - %s",
517 TempDir
, strerror(errno
));
524 * If we were started by launchd get the listen sockets file descriptors...
530 #endif /* HAVE_LAUNCHD */
532 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
534 * Load Print Service quota enforcement library (X Server only)
537 PSQLibRef
= dlopen(PSQLibPath
, RTLD_LAZY
);
540 PSQUpdateQuotaProc
= dlsym(PSQLibRef
, PSQLibFuncName
);
541 #endif /* __APPLE__ && HAVE_DLFCN_H */
544 * Startup the server...
550 * Catch hangup and child signals and ignore broken pipes...
553 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
554 sigset(SIGCHLD
, sigchld_handler
);
555 sigset(SIGHUP
, sighup_handler
);
556 sigset(SIGPIPE
, SIG_IGN
);
557 sigset(SIGTERM
, sigterm_handler
);
558 #elif defined(HAVE_SIGACTION)
559 memset(&action
, 0, sizeof(action
));
561 sigemptyset(&action
.sa_mask
);
562 sigaddset(&action
.sa_mask
, SIGTERM
);
563 sigaddset(&action
.sa_mask
, SIGCHLD
);
564 action
.sa_handler
= sigchld_handler
;
565 sigaction(SIGCHLD
, &action
, NULL
);
567 sigemptyset(&action
.sa_mask
);
568 sigaddset(&action
.sa_mask
, SIGHUP
);
569 action
.sa_handler
= sighup_handler
;
570 sigaction(SIGHUP
, &action
, NULL
);
572 sigemptyset(&action
.sa_mask
);
573 action
.sa_handler
= SIG_IGN
;
574 sigaction(SIGPIPE
, &action
, NULL
);
576 sigemptyset(&action
.sa_mask
);
577 sigaddset(&action
.sa_mask
, SIGTERM
);
578 sigaddset(&action
.sa_mask
, SIGCHLD
);
579 action
.sa_handler
= sigterm_handler
;
580 sigaction(SIGTERM
, &action
, NULL
);
582 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
583 signal(SIGHUP
, sighup_handler
);
584 signal(SIGPIPE
, SIG_IGN
);
585 signal(SIGTERM
, sigterm_handler
);
586 #endif /* HAVE_SIGSET */
590 * Try to create a fake lpsched lock file if one is not already there.
591 * Some Adobe applications need it under IRIX in order to enable
595 if ((fp
= cupsFileOpen("/var/spool/lp/SCHEDLOCK", "w")) == NULL
)
597 syslog(LOG_LPR
, "Unable to create fake lpsched lock file "
598 "\"/var/spool/lp/SCHEDLOCK\"\' - %s!",
603 fchmod(cupsFileNumber(fp
), 0644);
604 fchown(cupsFileNumber(fp
), User
, Group
);
611 * Initialize authentication certificates...
617 * If we are running in the background, signal the parent process that
618 * we are up and running...
622 if (!fg
|| run_as_child
)
625 #endif /* __APPLE__ */
628 * Send a signal to the parent process, but only if the parent is
629 * not PID 1 (init). This avoids accidentally shutting down the
630 * system on OpenBSD if you CTRL-C the server before it is up...
633 i
= getppid(); /* Save parent PID to avoid race condition */
641 * Start power management framework...
645 cupsdStartSystemMonitor();
646 #endif /* __APPLE__ */
649 * Send server-started event...
654 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
655 "Scheduler started via launchd.");
657 #endif /* HAVE_LAUNCHD */
659 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
660 "Scheduler started in foreground.");
662 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
663 "Scheduler started in background.");
666 * Start any pending print jobs...
675 current_time
= time(NULL
);
676 browse_time
= current_time
;
677 event_time
= current_time
;
678 expire_time
= current_time
;
681 senddoc_time
= current_time
;
683 while (!stop_scheduler
)
686 * Check if there are dead children to handle...
693 * Check if we need to load the server configuration file...
699 * Close any idle clients...
702 if (cupsArrayCount(Clients
) > 0)
704 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
706 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
707 if (con
->http
.state
== HTTP_WAITING
)
708 cupsdCloseClient(con
);
710 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
712 cupsdPauseListening();
716 * Check for any active jobs...
719 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
721 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
722 if (job
->state_value
== IPP_JOB_PROCESSING
)
726 * Restart if all clients are closed and all jobs finished, or
727 * if the reload timeout has elapsed...
730 if ((cupsArrayCount(Clients
) == 0 &&
731 (!job
|| NeedReload
!= RELOAD_ALL
)) ||
732 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
735 * Shutdown the server...
741 * Read configuration...
744 if (!cupsdReadConfiguration())
746 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
755 * If we were started by launchd get the listen sockets file descriptors...
761 #endif /* HAVE_LAUNCHD */
764 * Startup the server...
770 * Send a server-restarted event...
773 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
774 "Scheduler restarted.");
779 * Check for available input or ready output. If cupsdDoSelect()
780 * returns 0 or -1, something bad happened and we should exit
783 * Note that we at least have one listening socket open at all
787 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
792 * If no other work is scheduled and we're being controlled by
793 * launchd then timeout after 'LaunchdTimeout' seconds of
797 if (timeout
== 86400 && Launchd
&& LaunchdTimeout
&& !NumPolled
&&
798 !cupsArrayCount(ActiveJobs
) &&
800 (!BrowseRemoteProtocols
&&
801 (!NumBrowsers
|| !BrowseLocalProtocols
||
802 cupsArrayCount(Printers
) == 0))))
804 timeout
= LaunchdTimeout
;
805 launchd_idle_exit
= 1;
808 launchd_idle_exit
= 0;
809 #endif /* HAVE_LAUNCHD */
811 if ((fds
= cupsdDoSelect(timeout
)) < 0)
814 * Got an error from select!
818 cupsd_printer_t
*p
; /* Current printer */
819 #endif /* HAVE_DNSSD */
822 if (errno
== EINTR
) /* Just interrupted by a signal */
826 * Log all sorts of debug info to help track down the problem.
829 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
832 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
834 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
835 cupsdLogMessage(CUPSD_LOG_EMERG
,
836 "Clients[%d] = %d, file = %d, state = %d",
837 i
, con
->http
.fd
, con
->file
, con
->http
.state
);
839 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
841 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
842 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
844 cupsdLogMessage(CUPSD_LOG_EMERG
, "BrowseSocket = %d", BrowseSocket
);
846 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
849 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
851 #endif /* __APPLE__ */
853 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
855 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
856 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
858 job
->status_buffer
? job
->status_buffer
->fd
: -1,
859 job
->print_pipes
[0], job
->print_pipes
[1],
860 job
->back_pipes
[0], job
->back_pipes
[1]);
863 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
865 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
866 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
867 p
->reg_name
? p
->reg_name
: "(null)");
868 #endif /* HAVE_DNSSD */
873 current_time
= time(NULL
);
876 * Write dirty config/state files...
879 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
884 * If we are going to sleep and still have pending jobs, stop them after
885 * a period of time...
888 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
889 cupsArrayCount(PrintingJobs
) > 0)
894 #endif /* __APPLE__ */
898 * Update the network interfaces once a minute...
901 if ((current_time
- netif_time
) >= 60)
903 netif_time
= current_time
;
906 #endif /* !__APPLE__ */
910 * If no other work was scheduled and we're being controlled by launchd
911 * then timeout after 'LaunchdTimeout' seconds of inactivity...
914 if (!fds
&& launchd_idle_exit
)
916 cupsdLogMessage(CUPSD_LOG_INFO
,
917 "Printer sharing is off and there are no jobs pending, "
918 "will restart on demand.");
922 #endif /* HAVE_LAUNCHD */
925 * Resume listening for new connections as needed...
928 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
929 cupsArrayCount(Clients
) < MaxClients
)
930 cupsdResumeListening();
933 * Expire subscriptions and unload completed jobs as needed...
936 if (current_time
> expire_time
)
938 if (cupsArrayCount(Subscriptions
) > 0)
939 cupsdExpireSubscriptions(NULL
, NULL
);
941 cupsdUnloadCompletedJobs();
943 expire_time
= current_time
;
947 * Update the browse list as needed...
953 if ((BrowseRemoteProtocols
& BROWSE_SLP
) &&
954 BrowseSLPRefresh
<= current_time
)
955 cupsdUpdateSLPBrowse();
956 #endif /* HAVE_LIBSLP */
959 if ((BrowseRemoteProtocols
& BROWSE_LDAP
) &&
960 BrowseLDAPRefresh
<= current_time
)
961 cupsdUpdateLDAPBrowse();
962 #endif /* HAVE_LDAP */
965 if (Browsing
&& current_time
> browse_time
)
967 cupsdSendBrowseList();
968 browse_time
= current_time
;
972 * Update the root certificate once every 5 minutes if we have client
976 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
977 !RunUser
&& cupsArrayCount(Clients
))
980 * Update the root certificate...
984 cupsdAddCert(0, "root", NULL
);
988 * Check for new data on the client sockets...
991 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
993 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
996 * Process pending data in the input buffer...
1001 cupsdReadClient(con
);
1006 * Check the activity and close old clients...
1009 activity
= current_time
- Timeout
;
1010 if (con
->http
.activity
< activity
&& !con
->pipe_pid
)
1012 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1013 "Closing client %d after %d seconds of inactivity...",
1014 con
->http
.fd
, Timeout
);
1016 cupsdCloseClient(con
);
1022 * Update any pending multi-file documents...
1025 if ((current_time
- senddoc_time
) >= 10)
1028 senddoc_time
= current_time
;
1032 * Log statistics at most once a minute when in debug mode...
1035 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
1037 size_t string_count
, /* String count */
1038 alloc_bytes
, /* Allocated string bytes */
1039 total_bytes
; /* Total string bytes */
1040 #ifdef HAVE_MALLINFO
1041 struct mallinfo mem
; /* Malloc information */
1045 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1046 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1047 mem
.usmblks
+ mem
.uordblks
);
1048 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1049 mem
.fsmblks
+ mem
.fordblks
);
1050 #endif /* HAVE_MALLINFO */
1052 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1053 cupsArrayCount(Clients
));
1054 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1055 cupsArrayCount(Jobs
));
1056 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1057 cupsArrayCount(ActiveJobs
));
1058 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1059 cupsArrayCount(Printers
));
1060 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers-implicit=%d",
1061 cupsArrayCount(ImplicitPrinters
));
1063 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1064 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1065 "Report: stringpool-string-count=" CUPS_LLFMT
,
1066 CUPS_LLCAST string_count
);
1067 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1068 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1069 CUPS_LLCAST alloc_bytes
);
1070 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1071 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1072 CUPS_LLCAST total_bytes
);
1074 report_time
= current_time
;
1078 * Handle OS-specific event notification for any events that have
1079 * accumulated. Don't send these more than once a second...
1082 if (LastEvent
&& (current_time
- event_time
) >= 1)
1084 #ifdef HAVE_NOTIFY_POST
1085 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1086 CUPSD_EVENT_PRINTER_DELETED
|
1087 CUPSD_EVENT_PRINTER_MODIFIED
))
1089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1090 "notify_post(\"com.apple.printerListChange\")");
1091 notify_post("com.apple.printerListChange");
1094 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1096 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1097 "notify_post(\"com.apple.printerHistoryChange\")");
1098 notify_post("com.apple.printerHistoryChange");
1101 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1102 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1103 CUPSD_EVENT_JOB_PROGRESS
))
1105 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1106 "notify_post(\"com.apple.jobChange\")");
1107 notify_post("com.apple.jobChange");
1109 #endif /* HAVE_NOTIFY_POST */
1112 * Reset the accumulated events...
1115 LastEvent
= CUPSD_EVENT_NONE
;
1116 event_time
= current_time
;
1121 * Log a message based on what happened...
1126 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1127 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1128 "Scheduler shutting down normally.");
1132 cupsdLogMessage(CUPSD_LOG_ERROR
,
1133 "Scheduler shutting down due to program error.");
1134 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1135 "Scheduler shutting down due to program error.");
1139 * Close all network clients...
1146 * Update the launchd KeepAlive file as needed...
1151 #endif /* HAVE_LAUNCHD */
1161 * Stop monitoring system event monitoring...
1165 cupsdStopSystemMonitor();
1166 #endif /* __APPLE__ */
1170 * Free the scheduler's Kerberos context...
1175 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1179 if (krb5_init_context
!= NULL
)
1180 # endif /* __APPLE__ */
1181 if (KerberosContext
)
1182 krb5_free_context(KerberosContext
);
1183 #endif /* HAVE_GSSAPI */
1185 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
1187 * Unload Print Service quota enforcement library (X Server only)
1190 PSQUpdateQuotaProc
= NULL
;
1196 #endif /* __APPLE__ && HAVE_DLFCN_H */
1200 * Remove the fake IRIX lpsched lock file, but only if the existing
1201 * file is not a FIFO which indicates that the real IRIX lpsched is
1205 if (!stat("/var/spool/lp/FIFO", &statbuf
))
1206 if (!S_ISFIFO(statbuf
.st_mode
))
1207 unlink("/var/spool/lp/SCHEDLOCK");
1212 return (!stop_scheduler
);
1217 * 'cupsdClosePipe()' - Close a pipe as necessary.
1221 cupsdClosePipe(int *fds
) /* I - Pipe file descriptors (2) */
1224 * Close file descriptors as needed...
1242 * 'cupsdOpenPipe()' - Create a pipe which is closed on exec.
1245 int /* O - 0 on success, -1 on error */
1246 cupsdOpenPipe(int *fds
) /* O - Pipe file descriptors (2) */
1249 * Create the pipe...
1261 * Set the "close on exec" flag on each end of the pipe...
1264 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1275 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1287 * Return 0 indicating success...
1295 * 'cupsdClearString()' - Clear a string.
1299 cupsdClearString(char **s
) /* O - String value */
1310 * 'cupsdHoldSignals()' - Hold child and termination signals.
1314 cupsdHoldSignals(void)
1316 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1317 sigset_t newmask
; /* New POSIX signal mask */
1318 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1328 #elif defined(HAVE_SIGACTION)
1329 sigemptyset(&newmask
);
1330 sigaddset(&newmask
, SIGTERM
);
1331 sigaddset(&newmask
, SIGCHLD
);
1332 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1333 #endif /* HAVE_SIGSET */
1338 * 'cupsdReleaseSignals()' - Release signals for delivery.
1342 cupsdReleaseSignals(void)
1351 #elif defined(HAVE_SIGACTION)
1352 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1353 #endif /* HAVE_SIGSET */
1358 * 'cupsdSetString()' - Set a string value.
1362 cupsdSetString(char **s
, /* O - New string */
1363 const char *v
) /* I - String value */
1372 *s
= _cupsStrAlloc(v
);
1379 * 'cupsdSetStringf()' - Set a formatted string value.
1383 cupsdSetStringf(char **s
, /* O - New string */
1384 const char *f
, /* I - Printf-style format string */
1385 ...) /* I - Additional args as needed */
1387 char v
[4096]; /* Formatting string value */
1388 va_list ap
; /* Argument pointer */
1389 char *olds
; /* Old string */
1400 vsnprintf(v
, sizeof(v
), f
, ap
);
1403 *s
= _cupsStrAlloc(v
);
1415 * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
1419 launchd_checkin(void)
1421 size_t i
, /* Looping var */
1422 count
; /* Numebr of listeners */
1423 int portnum
; /* Port number */
1424 launch_data_t ld_msg
, /* Launch data message */
1425 ld_resp
, /* Launch data response */
1426 ld_array
, /* Launch data array */
1427 ld_sockets
, /* Launch data sockets dictionary */
1428 tmp
; /* Launch data */
1429 cupsd_listener_t
*lis
; /* Listeners array */
1430 http_addr_t addr
; /* Address variable */
1431 socklen_t addrlen
; /* Length of address */
1432 int fd
; /* File descriptor */
1433 char s
[256]; /* String addresss */
1436 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: pid=%d", (int)getpid());
1439 * Check-in with launchd...
1442 ld_msg
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
1443 if ((ld_resp
= launch_msg(ld_msg
)) == NULL
)
1445 cupsdLogMessage(CUPSD_LOG_ERROR
,
1446 "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
1449 return; /* anti-compiler-warning */
1452 if (launch_data_get_type(ld_resp
) == LAUNCH_DATA_ERRNO
)
1454 errno
= launch_data_get_errno(ld_resp
);
1455 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Check-in failed: %s",
1458 return; /* anti-compiler-warning */
1462 * Get the sockets dictionary...
1465 if ((ld_sockets
= launch_data_dict_lookup(ld_resp
, LAUNCH_JOBKEY_SOCKETS
))
1468 cupsdLogMessage(CUPSD_LOG_ERROR
,
1469 "launchd_checkin: No sockets found to answer requests on!");
1471 return; /* anti-compiler-warning */
1475 * Get the array of listener sockets...
1478 if ((ld_array
= launch_data_dict_lookup(ld_sockets
, "Listeners")) == NULL
)
1480 cupsdLogMessage(CUPSD_LOG_ERROR
,
1481 "launchd_checkin: No sockets found to answer requests on!");
1483 return; /* anti-compiler-warning */
1487 * Add listening fd(s) to the Listener array...
1490 if (launch_data_get_type(ld_array
) == LAUNCH_DATA_ARRAY
)
1492 count
= launch_data_array_get_count(ld_array
);
1494 for (i
= 0; i
< count
; i
++)
1497 * Get the launchd file descriptor and address...
1500 if ((tmp
= launch_data_array_get_index(ld_array
, i
)) != NULL
)
1502 fd
= launch_data_get_fd(tmp
);
1503 addrlen
= sizeof(addr
);
1505 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1507 cupsdLogMessage(CUPSD_LOG_ERROR
,
1508 "launchd_checkin: Unable to get local address - %s",
1514 * Try to match the launchd socket address to one of the listeners...
1517 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1519 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1520 if (httpAddrEqual(&lis
->address
, &addr
))
1524 * Add a new listener If there's no match...
1529 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1530 "launchd_checkin: Matched existing listener %s with fd %d...",
1531 httpAddrString(&(lis
->address
), s
, sizeof(s
)), fd
);
1535 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1536 "launchd_checkin: Adding new listener %s with fd %d...",
1537 httpAddrString(&addr
, s
, sizeof(s
)), fd
);
1539 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1541 cupsdLogMessage(CUPSD_LOG_ERROR
,
1542 "launchd_checkin: Unable to allocate listener - %s.",
1547 cupsArrayAdd(Listeners
, lis
);
1549 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1558 if (lis
->address
.addr
.sa_family
== AF_INET6
)
1559 portnum
= ntohs(lis
->address
.ipv6
.sin6_port
);
1561 # endif /* AF_INET6 */
1562 if (lis
->address
.addr
.sa_family
== AF_INET
)
1563 portnum
= ntohs(lis
->address
.ipv4
.sin_port
);
1566 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1567 # endif /* HAVE_SSL */
1572 launch_data_free(ld_msg
);
1573 launch_data_free(ld_resp
);
1578 * 'launchd_checkout()' - Update the launchd KeepAlive file as needed.
1582 launchd_checkout(void)
1584 int fd
; /* File descriptor */
1588 * Create or remove the launchd KeepAlive file based on whether
1589 * there are active jobs, polling, browsing for remote printers or
1590 * shared printers to advertise...
1593 if ((cupsArrayCount(ActiveJobs
) || NumPolled
||
1595 (BrowseRemoteProtocols
||
1596 (BrowseLocalProtocols
&& NumBrowsers
&& cupsArrayCount(Printers
))))))
1598 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1599 "Creating launchd keepalive file \"" CUPS_KEEPALIVE
"\"...");
1601 if ((fd
= open(CUPS_KEEPALIVE
, O_RDONLY
| O_CREAT
| O_EXCL
, S_IRUSR
)) >= 0)
1606 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1607 "Removing launchd keepalive file \"" CUPS_KEEPALIVE
"\"...");
1609 unlink(CUPS_KEEPALIVE
);
1612 #endif /* HAVE_LAUNCHD */
1616 * 'parent_handler()' - Catch USR1/CHLD signals...
1620 parent_handler(int sig
) /* I - Signal */
1623 * Store the signal we got from the OS and return...
1626 parent_signal
= sig
;
1631 * 'process_children()' - Process all dead children...
1635 process_children(void)
1637 int status
; /* Exit status of child */
1638 int pid
, /* Process ID of child */
1639 job_id
; /* Job ID of child */
1640 cupsd_job_t
*job
; /* Current job */
1641 int i
; /* Looping var */
1642 char name
[1024]; /* Process name */
1645 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1648 * Reset the dead_children flag...
1654 * Collect the exit status of some children...
1658 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1659 #elif defined(HAVE_WAIT3)
1660 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1662 if ((pid
= wait(&status
)) > 0)
1663 #endif /* HAVE_WAITPID */
1666 * Collect the name of the process that finished...
1669 cupsdFinishProcess(pid
, name
, sizeof(name
), &job_id
);
1672 * Delete certificates for CGI processes...
1676 cupsdDeleteCert(pid
);
1679 * Handle completed job filters...
1682 if (job_id
> 0 && (job
= cupsdFindJob(job_id
)) != NULL
)
1684 for (i
= 0; job
->filters
[i
]; i
++)
1685 if (job
->filters
[i
] == pid
)
1688 if (job
->filters
[i
] || job
->backend
== pid
)
1691 * OK, this process has gone away; what's left?
1694 if (job
->filters
[i
])
1695 job
->filters
[i
] = -pid
;
1697 job
->backend
= -pid
;
1699 if (status
&& status
!= SIGTERM
&& status
!= SIGKILL
&&
1703 * An error occurred; save the exit status so we know to stop
1704 * the printer or cancel the job when all of the filters finish...
1706 * A negative status indicates that the backend failed and the
1707 * printer needs to be stopped.
1710 if (job
->filters
[i
])
1711 job
->status
= status
; /* Filter failed */
1713 job
->status
= -status
; /* Backend failed */
1715 if (!(job
->printer
->type
& CUPS_PRINTER_FAX
) &&
1716 job
->status_level
>= CUPSD_LOG_ERROR
)
1718 job
->status_level
= CUPSD_LOG_ERROR
;
1720 snprintf(job
->printer
->state_message
,
1721 sizeof(job
->printer
->state_message
), "%s failed", name
);
1722 cupsdAddPrinterHistory(job
->printer
);
1724 if (!job
->printer_message
)
1726 if ((job
->printer_message
=
1727 ippFindAttribute(job
->attrs
, "job-printer-state-message",
1728 IPP_TAG_TEXT
)) == NULL
)
1729 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
,
1731 "job-printer-state-message",
1735 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
1736 job
->printer
->state_message
);
1741 * If this is not the last file in a job, see if all of the
1742 * filters are done, and if so move to the next file.
1745 if (job
->current_file
< job
->num_files
)
1747 for (i
= 0; job
->filters
[i
] < 0; i
++);
1749 if (!job
->filters
[i
])
1752 * Process the next file...
1755 cupsdContinueJob(job
);
1763 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1764 * since they come when we kill/end a process...
1767 if (status
== SIGTERM
|| status
== SIGKILL
)
1769 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1770 "PID %d (%s) was terminated normally with signal %d.",
1775 if (WIFEXITED(status
))
1776 cupsdLogMessage(CUPSD_LOG_DEBUG
, "PID %d (%s) stopped with status %d!",
1777 pid
, name
, WEXITSTATUS(status
));
1779 cupsdLogMessage(CUPSD_LOG_ERROR
, "PID %d (%s) crashed on signal %d!",
1780 pid
, name
, WTERMSIG(status
));
1782 if (LogLevel
< CUPSD_LOG_DEBUG
)
1783 cupsdLogMessage(CUPSD_LOG_INFO
,
1784 "Hint: Try setting the LogLevel to \"debug\" to find "
1788 cupsdLogMessage(CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1793 * If wait*() is interrupted by a signal, tell main() to call us again...
1796 if (pid
< 0 && errno
== EINTR
)
1802 * 'select_timeout()' - Calculate the select timeout value.
1806 static long /* O - Number of seconds */
1807 select_timeout(int fds
) /* I - Number of descriptors returned */
1809 long timeout
; /* Timeout for select */
1810 time_t now
; /* Current time */
1811 cupsd_client_t
*con
; /* Client information */
1812 cupsd_printer_t
*p
; /* Printer information */
1813 cupsd_job_t
*job
; /* Job information */
1814 cupsd_subscription_t
*sub
; /* Subscription information */
1815 const char *why
; /* Debugging aid */
1819 * Check to see if any of the clients have pending data to be
1820 * processed; if so, the timeout should be 0...
1823 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1825 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1826 if (con
->http
.used
> 0)
1830 * If select has been active in the last second (fds > 0) or we have
1831 * many resources in use then don't bother trying to optimize the
1832 * timeout, just make it 1 second.
1835 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1839 * Otherwise, check all of the possible events that we need to wake for...
1843 timeout
= now
+ 86400; /* 86400 == 1 day */
1848 * When going to sleep, wake up to cancel jobs that don't complete in time.
1851 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1853 timeout
= SleepJobs
;
1854 why
= "cancel jobs before sleeping";
1856 #endif /* __APPLE__ */
1859 * Check whether we are accepting new connections...
1862 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1863 ListeningPaused
< timeout
)
1865 if (ListeningPaused
<= now
)
1868 timeout
= ListeningPaused
;
1870 why
= "resume listening";
1874 * Check the activity and close old clients...
1877 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1879 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1880 if ((con
->http
.activity
+ Timeout
) < timeout
)
1882 timeout
= con
->http
.activity
+ Timeout
;
1883 why
= "timeout a client connection";
1887 * Update the browse list as needed...
1890 if (Browsing
&& BrowseLocalProtocols
)
1893 if ((BrowseLocalProtocols
& BROWSE_SLP
) && (BrowseSLPRefresh
< timeout
))
1895 timeout
= BrowseSLPRefresh
;
1896 why
= "update SLP browsing";
1898 #endif /* HAVE_LIBSLP */
1901 if ((BrowseLocalProtocols
& BROWSE_LDAP
) && (BrowseLDAPRefresh
< timeout
))
1903 timeout
= BrowseLDAPRefresh
;
1904 why
= "update LDAP browsing";
1906 #endif /* HAVE_LDAP */
1908 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && NumBrowsers
)
1910 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1912 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1914 if (p
->type
& CUPS_PRINTER_REMOTE
)
1916 if ((p
->browse_time
+ BrowseTimeout
) < timeout
)
1918 timeout
= p
->browse_time
+ BrowseTimeout
;
1919 why
= "browse timeout a printer";
1922 else if (p
->shared
&& !(p
->type
& CUPS_PRINTER_IMPLICIT
))
1924 if (BrowseInterval
&& (p
->browse_time
+ BrowseInterval
) < timeout
)
1926 timeout
= p
->browse_time
+ BrowseInterval
;
1927 why
= "send browse update";
1935 * Write out changes to configuration and state files...
1938 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1940 timeout
= DirtyCleanTime
;
1941 why
= "write dirty config/state files";
1945 * Check for any active jobs...
1948 if (cupsArrayCount(ActiveJobs
) > 0)
1950 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1952 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1953 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1955 timeout
= job
->hold_until
;
1956 why
= "release held jobs";
1958 else if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1961 why
= "start pending jobs";
1966 #ifdef HAVE_MALLINFO
1968 * Log memory usage every minute...
1971 if (LogLevel
>= CUPSD_LOG_DEBUG
&& (mallinfo_time
+ 60) < timeout
)
1973 timeout
= mallinfo_time
+ 60;
1974 why
= "display memory usage";
1976 #endif /* HAVE_MALLINFO */
1979 * Expire subscriptions as needed...
1982 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1984 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1985 if (!sub
->job
&& sub
->expire
&& sub
->expire
< timeout
)
1987 timeout
= sub
->expire
;
1988 why
= "expire subscription";
1992 * Adjust from absolute to relative time. If p->browse_time above
1993 * was 0 then we can end up with a negative value here, so check.
1994 * We add 1 second to the timeout since events occur after the
1995 * timeout expires, and limit the timeout to 86400 seconds (1 day)
1996 * to avoid select() timeout limits present on some operating
2000 timeout
= timeout
- now
+ 1;
2004 else if (timeout
> 86400)
2008 * Log and return the timeout value...
2011 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
2019 * 'sigchld_handler()' - Handle 'child' signals from old processes.
2023 sigchld_handler(int sig
) /* I - Signal number */
2028 * Flag that we have dead children...
2034 * Reset the signal handler as needed...
2037 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2038 signal(SIGCLD
, sigchld_handler
);
2039 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2044 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
2048 sighup_handler(int sig
) /* I - Signal number */
2052 NeedReload
= RELOAD_ALL
;
2053 ReloadTime
= time(NULL
);
2055 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2056 signal(SIGHUP
, sighup_handler
);
2057 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2062 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
2066 sigterm_handler(int sig
) /* I - Signal number */
2068 (void)sig
; /* remove compiler warnings... */
2071 * Flag that we should stop and return...
2079 * 'usage()' - Show scheduler usage.
2083 usage(int status
) /* O - Exit status */
2085 _cupsLangPuts(status
? stderr
: stdout
,
2086 _("Usage: cupsd [-c config-file] [-f] [-F] [-h] [-l]\n"
2088 "-c config-file Load alternate configuration file\n"
2089 "-f Run in the foreground\n"
2090 "-F Run in the foreground but detach\n"
2091 "-h Show this usage message\n"
2092 "-l Run cupsd from launchd(8)\n"));
2098 * End of "$Id: main.c 7925 2008-09-10 17:47:26Z mike $".