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
= !getuid();
151 /* Use system management functions? */
153 time_t netif_time
= 0; /* Time since last network update */
154 #endif /* __APPLE__ */
156 int launchd_idle_exit
;
157 /* Idle exit on select timeout? */
158 #endif /* HAVE_LAUNCHD */
163 * Check for setuid invocation, which we do not support!
166 if (getuid() != geteuid())
168 fputs("cupsd: Cannot run as a setuid program!\n", stderr
);
171 #endif /* HAVE_GETEUID */
174 * Check for command-line arguments...
180 if (getenv("CUPSD_LAUNCHD"))
185 #endif /* HAVE_LAUNCHD */
187 for (i
= 1; i
< argc
; i
++)
188 if (argv
[i
][0] == '-')
189 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
193 case 'S' : /* Disable system management functions */
194 puts("Warning: -S (disable system management) for internal testing use only!");
198 case 'C' : /* Run as child with config file */
201 #endif /* __APPLE__ */
203 case 'c' : /* Configuration file */
207 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
208 "after \"-c\" option!\n"));
212 if (argv
[i
][0] == '/')
215 * Absolute directory...
218 cupsdSetString(&ConfigurationFile
, argv
[i
]);
223 * Relative directory...
226 char *current
; /* Current directory */
230 * Allocate a buffer for the current working directory to
231 * reduce run-time stack usage; this approximates the
232 * behavior of some implementations of getcwd() when they
233 * are passed a NULL pointer.
236 if ((current
= malloc(1024)) == NULL
)
238 _cupsLangPuts(stderr
,
239 _("cupsd: Unable to get current directory!\n"));
243 if (!getcwd(current
, 1024))
245 _cupsLangPuts(stderr
,
246 _("cupsd: Unable to get current directory!\n"));
251 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
256 case 'f' : /* Run in foreground... */
260 case 'F' : /* Run in foreground, but disconnect from terminal... */
264 case 'h' : /* Show usage/help */
268 case 'l' : /* Started by launchd... */
273 _cupsLangPuts(stderr
, _("cupsd: launchd(8) support not compiled "
274 "in, running in normal mode.\n"));
276 #endif /* HAVE_LAUNCHD */
279 case 'p' : /* Stop immediately for profiling */
280 puts("Warning: -p (startup profiling) is for internal testing use only!");
285 case 'P' : /* Disable security profiles */
286 puts("Warning: -P (disable security profiles) is for internal testing use only!");
290 case 't' : /* Test the cupsd.conf file... */
295 default : /* Unknown option */
296 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
297 "aborting!\n"), *opt
);
303 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting!\n"),
308 if (!ConfigurationFile
)
309 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
312 * If the user hasn't specified "-f", run in the background...
318 * Setup signal handlers for the parent...
321 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
322 sigset(SIGUSR1
, parent_handler
);
323 sigset(SIGCHLD
, parent_handler
);
325 sigset(SIGHUP
, SIG_IGN
);
326 #elif defined(HAVE_SIGACTION)
327 memset(&action
, 0, sizeof(action
));
328 sigemptyset(&action
.sa_mask
);
329 sigaddset(&action
.sa_mask
, SIGUSR1
);
330 action
.sa_handler
= parent_handler
;
331 sigaction(SIGUSR1
, &action
, NULL
);
332 sigaction(SIGCHLD
, &action
, NULL
);
334 sigemptyset(&action
.sa_mask
);
335 action
.sa_handler
= SIG_IGN
;
336 sigaction(SIGHUP
, &action
, NULL
);
338 signal(SIGUSR1
, parent_handler
);
339 signal(SIGCLD
, parent_handler
);
341 signal(SIGHUP
, SIG_IGN
);
342 #endif /* HAVE_SIGSET */
347 * OK, wait for the child to startup and send us SIGUSR1 or to crash
348 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
349 * might be sent by the init script to restart the scheduler...
352 for (; parent_signal
== 0;)
355 if (parent_signal
== SIGUSR1
)
363 else if (WIFEXITED(i
))
365 fprintf(stderr
, "cupsd: Child exited with status %d!\n",
371 fprintf(stderr
, "cupsd: Child exited on signal %d!\n", WTERMSIG(i
));
378 * Since CoreFoundation has an overly-agressive check for whether a
379 * process has forked but not exec'd (whether CF has been called or
380 * not...), we now have to exec ourselves with the "-f" option to
381 * eliminate their bogus warning messages.
384 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, (char *)0);
386 #endif /* __APPLE__ */
392 * Make sure we aren't tying up any filesystems...
399 * Disable core dumps...
402 getrlimit(RLIMIT_CORE
, &limit
);
404 setrlimit(RLIMIT_CORE
, &limit
);
407 * Disconnect from the controlling terminal...
413 * Close all open files...
416 getrlimit(RLIMIT_NOFILE
, &limit
);
418 for (i
= 0; i
< limit
.rlim_cur
&& i
< 1024; i
++)
422 * Redirect stdin/out/err to /dev/null...
425 open("/dev/null", O_RDONLY
);
426 open("/dev/null", O_WRONLY
);
427 open("/dev/null", O_WRONLY
);
432 * Set the timezone info...
438 setlocale(LC_TIME
, "");
442 * Set the maximum number of files...
445 getrlimit(RLIMIT_NOFILE
, &limit
);
447 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
448 if (limit
.rlim_max
> FD_SETSIZE
)
451 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
453 if (limit
.rlim_max
== RLIM_INFINITY
)
456 #endif /* RLIM_INFINITY */
457 MaxFDs
= limit
.rlim_max
;
459 limit
.rlim_cur
= MaxFDs
;
461 setrlimit(RLIMIT_NOFILE
, &limit
);
466 * Read configuration...
469 if (!cupsdReadConfiguration())
472 printf("%s contains errors\n", ConfigurationFile
);
474 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
478 else if (TestConfigFile
)
480 printf("%s is OK\n", ConfigurationFile
);
484 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
487 * Clean out the temporary directory...
490 cups_dir_t
*dir
; /* Temporary directory */
491 cups_dentry_t
*dent
; /* Directory entry */
492 char tempfile
[1024]; /* Temporary filename */
495 if ((dir
= cupsDirOpen(TempDir
)) != NULL
)
497 cupsdLogMessage(CUPSD_LOG_INFO
,
498 "Cleaning out old temporary files in \"%s\"...", TempDir
);
500 while ((dent
= cupsDirRead(dir
)) != NULL
)
502 snprintf(tempfile
, sizeof(tempfile
), "%s/%s", TempDir
, dent
->filename
);
504 if (unlink(tempfile
))
505 cupsdLogMessage(CUPSD_LOG_ERROR
,
506 "Unable to remove temporary file \"%s\" - %s",
507 tempfile
, strerror(errno
));
509 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Removed temporary file \"%s\"...",
516 cupsdLogMessage(CUPSD_LOG_ERROR
,
517 "Unable to open temporary directory \"%s\" - %s",
518 TempDir
, strerror(errno
));
525 * If we were started by launchd get the listen sockets file descriptors...
531 #endif /* HAVE_LAUNCHD */
533 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
535 * Load Print Service quota enforcement library (X Server only)
538 PSQLibRef
= dlopen(PSQLibPath
, RTLD_LAZY
);
541 PSQUpdateQuotaProc
= dlsym(PSQLibRef
, PSQLibFuncName
);
542 #endif /* __APPLE__ && HAVE_DLFCN_H */
545 * Startup the server...
551 * Catch hangup and child signals and ignore broken pipes...
554 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
555 sigset(SIGCHLD
, sigchld_handler
);
556 sigset(SIGHUP
, sighup_handler
);
557 sigset(SIGPIPE
, SIG_IGN
);
558 sigset(SIGTERM
, sigterm_handler
);
559 #elif defined(HAVE_SIGACTION)
560 memset(&action
, 0, sizeof(action
));
562 sigemptyset(&action
.sa_mask
);
563 sigaddset(&action
.sa_mask
, SIGTERM
);
564 sigaddset(&action
.sa_mask
, SIGCHLD
);
565 action
.sa_handler
= sigchld_handler
;
566 sigaction(SIGCHLD
, &action
, NULL
);
568 sigemptyset(&action
.sa_mask
);
569 sigaddset(&action
.sa_mask
, SIGHUP
);
570 action
.sa_handler
= sighup_handler
;
571 sigaction(SIGHUP
, &action
, NULL
);
573 sigemptyset(&action
.sa_mask
);
574 action
.sa_handler
= SIG_IGN
;
575 sigaction(SIGPIPE
, &action
, NULL
);
577 sigemptyset(&action
.sa_mask
);
578 sigaddset(&action
.sa_mask
, SIGTERM
);
579 sigaddset(&action
.sa_mask
, SIGCHLD
);
580 action
.sa_handler
= sigterm_handler
;
581 sigaction(SIGTERM
, &action
, NULL
);
583 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
584 signal(SIGHUP
, sighup_handler
);
585 signal(SIGPIPE
, SIG_IGN
);
586 signal(SIGTERM
, sigterm_handler
);
587 #endif /* HAVE_SIGSET */
591 * Try to create a fake lpsched lock file if one is not already there.
592 * Some Adobe applications need it under IRIX in order to enable
596 if ((fp
= cupsFileOpen("/var/spool/lp/SCHEDLOCK", "w")) == NULL
)
598 syslog(LOG_LPR
, "Unable to create fake lpsched lock file "
599 "\"/var/spool/lp/SCHEDLOCK\"\' - %s!",
604 fchmod(cupsFileNumber(fp
), 0644);
605 fchown(cupsFileNumber(fp
), User
, Group
);
612 * Initialize authentication certificates...
618 * If we are running in the background, signal the parent process that
619 * we are up and running...
623 if (!fg
|| run_as_child
)
626 #endif /* __APPLE__ */
629 * Send a signal to the parent process, but only if the parent is
630 * not PID 1 (init). This avoids accidentally shutting down the
631 * system on OpenBSD if you CTRL-C the server before it is up...
634 i
= getppid(); /* Save parent PID to avoid race condition */
642 * Start power management framework...
646 cupsdStartSystemMonitor();
647 #endif /* __APPLE__ */
650 * Send server-started event...
655 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
656 "Scheduler started via launchd.");
658 #endif /* HAVE_LAUNCHD */
660 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
661 "Scheduler started in foreground.");
663 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
,
664 "Scheduler started in background.");
667 * Start any pending print jobs...
676 current_time
= time(NULL
);
677 browse_time
= current_time
;
678 event_time
= current_time
;
679 expire_time
= current_time
;
682 senddoc_time
= current_time
;
684 while (!stop_scheduler
)
687 * Check if there are dead children to handle...
694 * Check if we need to load the server configuration file...
700 * Close any idle clients...
703 if (cupsArrayCount(Clients
) > 0)
705 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
707 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
708 if (con
->http
.state
== HTTP_WAITING
)
709 cupsdCloseClient(con
);
711 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
713 cupsdPauseListening();
717 * Restart if all clients are closed and all jobs finished, or
718 * if the reload timeout has elapsed...
721 if ((cupsArrayCount(Clients
) == 0 &&
722 (cupsArrayCount(PrintingJobs
) == 0 || NeedReload
!= RELOAD_ALL
)) ||
723 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
726 * Shutdown the server...
732 * Read configuration...
735 if (!cupsdReadConfiguration())
737 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting!",
746 * If we were started by launchd get the listen sockets file descriptors...
752 #endif /* HAVE_LAUNCHD */
755 * Startup the server...
761 * Send a server-restarted event...
764 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
765 "Scheduler restarted.");
770 * Check for available input or ready output. If cupsdDoSelect()
771 * returns 0 or -1, something bad happened and we should exit
774 * Note that we at least have one listening socket open at all
778 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
783 * If no other work is scheduled and we're being controlled by
784 * launchd then timeout after 'LaunchdTimeout' seconds of
788 if (timeout
== 86400 && Launchd
&& LaunchdTimeout
&& !NumPolled
&&
789 !cupsArrayCount(ActiveJobs
) &&
791 (!BrowseRemoteProtocols
&&
792 (!NumBrowsers
|| !BrowseLocalProtocols
||
793 cupsArrayCount(Printers
) == 0))))
795 timeout
= LaunchdTimeout
;
796 launchd_idle_exit
= 1;
799 launchd_idle_exit
= 0;
800 #endif /* HAVE_LAUNCHD */
802 if ((fds
= cupsdDoSelect(timeout
)) < 0)
805 * Got an error from select!
809 cupsd_printer_t
*p
; /* Current printer */
810 #endif /* HAVE_DNSSD */
813 if (errno
== EINTR
) /* Just interrupted by a signal */
817 * Log all sorts of debug info to help track down the problem.
820 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
823 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
825 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
826 cupsdLogMessage(CUPSD_LOG_EMERG
,
827 "Clients[%d] = %d, file = %d, state = %d",
828 i
, con
->http
.fd
, con
->file
, con
->http
.state
);
830 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
832 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
833 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
835 cupsdLogMessage(CUPSD_LOG_EMERG
, "BrowseSocket = %d", BrowseSocket
);
837 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
840 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
842 #endif /* __APPLE__ */
844 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
846 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
847 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
849 job
->status_buffer
? job
->status_buffer
->fd
: -1,
850 job
->print_pipes
[0], job
->print_pipes
[1],
851 job
->back_pipes
[0], job
->back_pipes
[1]);
854 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
856 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
857 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
858 p
->reg_name
? p
->reg_name
: "(null)");
859 #endif /* HAVE_DNSSD */
864 current_time
= time(NULL
);
867 * Write dirty config/state files...
870 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
875 * If we are going to sleep and still have pending jobs, stop them after
876 * a period of time...
879 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
880 cupsArrayCount(PrintingJobs
) > 0)
883 cupsdStopAllJobs(CUPSD_JOB_DEFAULT
, 10);
885 #endif /* __APPLE__ */
889 * Update the network interfaces once a minute...
892 if ((current_time
- netif_time
) >= 60)
894 netif_time
= current_time
;
897 #endif /* !__APPLE__ */
901 * If no other work was scheduled and we're being controlled by launchd
902 * then timeout after 'LaunchdTimeout' seconds of inactivity...
905 if (!fds
&& launchd_idle_exit
)
907 cupsdLogMessage(CUPSD_LOG_INFO
,
908 "Printer sharing is off and there are no jobs pending, "
909 "will restart on demand.");
913 #endif /* HAVE_LAUNCHD */
916 * Resume listening for new connections as needed...
919 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
920 cupsArrayCount(Clients
) < MaxClients
)
921 cupsdResumeListening();
924 * Expire subscriptions and unload completed jobs as needed...
927 if (current_time
> expire_time
)
929 if (cupsArrayCount(Subscriptions
) > 0)
930 cupsdExpireSubscriptions(NULL
, NULL
);
932 cupsdUnloadCompletedJobs();
934 expire_time
= current_time
;
938 * Update the browse list as needed...
944 if ((BrowseRemoteProtocols
& BROWSE_SLP
) &&
945 BrowseSLPRefresh
<= current_time
)
946 cupsdUpdateSLPBrowse();
947 #endif /* HAVE_LIBSLP */
950 if ((BrowseRemoteProtocols
& BROWSE_LDAP
) &&
951 BrowseLDAPRefresh
<= current_time
)
952 cupsdUpdateLDAPBrowse();
953 #endif /* HAVE_LDAP */
956 if (Browsing
&& current_time
> browse_time
)
958 cupsdSendBrowseList();
959 browse_time
= current_time
;
963 * Update the root certificate once every 5 minutes if we have client
967 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
968 !RunUser
&& cupsArrayCount(Clients
))
971 * Update the root certificate...
975 cupsdAddCert(0, "root", NULL
);
979 * Check for new data on the client sockets...
982 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
984 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
987 * Process pending data in the input buffer...
992 cupsdReadClient(con
);
997 * Check the activity and close old clients...
1000 activity
= current_time
- Timeout
;
1001 if (con
->http
.activity
< activity
&& !con
->pipe_pid
)
1003 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1004 "Closing client %d after %d seconds of inactivity...",
1005 con
->http
.fd
, Timeout
);
1007 cupsdCloseClient(con
);
1013 * Update any pending multi-file documents...
1016 if ((current_time
- senddoc_time
) >= 10)
1019 senddoc_time
= current_time
;
1023 * Log statistics at most once a minute when in debug mode...
1026 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
1028 size_t string_count
, /* String count */
1029 alloc_bytes
, /* Allocated string bytes */
1030 total_bytes
; /* Total string bytes */
1031 #ifdef HAVE_MALLINFO
1032 struct mallinfo mem
; /* Malloc information */
1036 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1037 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1038 mem
.usmblks
+ mem
.uordblks
);
1039 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1040 mem
.fsmblks
+ mem
.fordblks
);
1041 #endif /* HAVE_MALLINFO */
1043 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1044 cupsArrayCount(Clients
));
1045 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1046 cupsArrayCount(Jobs
));
1047 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1048 cupsArrayCount(ActiveJobs
));
1049 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1050 cupsArrayCount(Printers
));
1051 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers-implicit=%d",
1052 cupsArrayCount(ImplicitPrinters
));
1054 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1055 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1056 "Report: stringpool-string-count=" CUPS_LLFMT
,
1057 CUPS_LLCAST string_count
);
1058 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1059 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1060 CUPS_LLCAST alloc_bytes
);
1061 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1062 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1063 CUPS_LLCAST total_bytes
);
1065 report_time
= current_time
;
1069 * Handle OS-specific event notification for any events that have
1070 * accumulated. Don't send these more than once a second...
1073 if (LastEvent
&& (current_time
- event_time
) >= 1)
1075 #ifdef HAVE_NOTIFY_POST
1076 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1077 CUPSD_EVENT_PRINTER_DELETED
|
1078 CUPSD_EVENT_PRINTER_MODIFIED
))
1080 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1081 "notify_post(\"com.apple.printerListChange\")");
1082 notify_post("com.apple.printerListChange");
1085 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1087 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1088 "notify_post(\"com.apple.printerHistoryChange\")");
1089 notify_post("com.apple.printerHistoryChange");
1092 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1093 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1094 CUPSD_EVENT_JOB_PROGRESS
))
1096 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1097 "notify_post(\"com.apple.jobChange\")");
1098 notify_post("com.apple.jobChange");
1100 #endif /* HAVE_NOTIFY_POST */
1103 * Reset the accumulated events...
1106 LastEvent
= CUPSD_EVENT_NONE
;
1107 event_time
= current_time
;
1112 * Log a message based on what happened...
1117 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1118 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1119 "Scheduler shutting down normally.");
1123 cupsdLogMessage(CUPSD_LOG_ERROR
,
1124 "Scheduler shutting down due to program error.");
1125 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1126 "Scheduler shutting down due to program error.");
1130 * Close all network clients...
1137 * Update the launchd KeepAlive file as needed...
1142 #endif /* HAVE_LAUNCHD */
1152 * Stop monitoring system event monitoring...
1156 cupsdStopSystemMonitor();
1157 #endif /* __APPLE__ */
1161 * Free the scheduler's Kerberos context...
1166 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
1170 if (krb5_init_context
!= NULL
)
1171 # endif /* __APPLE__ */
1172 if (KerberosContext
)
1173 krb5_free_context(KerberosContext
);
1174 #endif /* HAVE_GSSAPI */
1176 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
1178 * Unload Print Service quota enforcement library (X Server only)
1181 PSQUpdateQuotaProc
= NULL
;
1187 #endif /* __APPLE__ && HAVE_DLFCN_H */
1191 * Remove the fake IRIX lpsched lock file, but only if the existing
1192 * file is not a FIFO which indicates that the real IRIX lpsched is
1196 if (!stat("/var/spool/lp/FIFO", &statbuf
))
1197 if (!S_ISFIFO(statbuf
.st_mode
))
1198 unlink("/var/spool/lp/SCHEDLOCK");
1203 return (!stop_scheduler
);
1208 * 'cupsdCheckProcess()' - Tell the main loop to check for dead children.
1212 cupsdCheckProcess(void)
1215 * Flag that we have dead children...
1223 * 'cupsdClosePipe()' - Close a pipe as necessary.
1227 cupsdClosePipe(int *fds
) /* I - Pipe file descriptors (2) */
1230 * Close file descriptors as needed...
1248 * 'cupsdOpenPipe()' - Create a pipe which is closed on exec.
1251 int /* O - 0 on success, -1 on error */
1252 cupsdOpenPipe(int *fds
) /* O - Pipe file descriptors (2) */
1255 * Create the pipe...
1267 * Set the "close on exec" flag on each end of the pipe...
1270 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1281 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1293 * Return 0 indicating success...
1301 * 'cupsdClearString()' - Clear a string.
1305 cupsdClearString(char **s
) /* O - String value */
1316 * 'cupsdHoldSignals()' - Hold child and termination signals.
1320 cupsdHoldSignals(void)
1322 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1323 sigset_t newmask
; /* New POSIX signal mask */
1324 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1334 #elif defined(HAVE_SIGACTION)
1335 sigemptyset(&newmask
);
1336 sigaddset(&newmask
, SIGTERM
);
1337 sigaddset(&newmask
, SIGCHLD
);
1338 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1339 #endif /* HAVE_SIGSET */
1344 * 'cupsdReleaseSignals()' - Release signals for delivery.
1348 cupsdReleaseSignals(void)
1357 #elif defined(HAVE_SIGACTION)
1358 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1359 #endif /* HAVE_SIGSET */
1364 * 'cupsdSetString()' - Set a string value.
1368 cupsdSetString(char **s
, /* O - New string */
1369 const char *v
) /* I - String value */
1378 *s
= _cupsStrAlloc(v
);
1385 * 'cupsdSetStringf()' - Set a formatted string value.
1389 cupsdSetStringf(char **s
, /* O - New string */
1390 const char *f
, /* I - Printf-style format string */
1391 ...) /* I - Additional args as needed */
1393 char v
[4096]; /* Formatting string value */
1394 va_list ap
; /* Argument pointer */
1395 char *olds
; /* Old string */
1406 vsnprintf(v
, sizeof(v
), f
, ap
);
1409 *s
= _cupsStrAlloc(v
);
1421 * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
1425 launchd_checkin(void)
1427 size_t i
, /* Looping var */
1428 count
; /* Numebr of listeners */
1429 int portnum
; /* Port number */
1430 launch_data_t ld_msg
, /* Launch data message */
1431 ld_resp
, /* Launch data response */
1432 ld_array
, /* Launch data array */
1433 ld_sockets
, /* Launch data sockets dictionary */
1434 tmp
; /* Launch data */
1435 cupsd_listener_t
*lis
; /* Listeners array */
1436 http_addr_t addr
; /* Address variable */
1437 socklen_t addrlen
; /* Length of address */
1438 int fd
; /* File descriptor */
1439 char s
[256]; /* String addresss */
1442 cupsdLogMessage(CUPSD_LOG_DEBUG
, "launchd_checkin: pid=%d", (int)getpid());
1445 * Check-in with launchd...
1448 ld_msg
= launch_data_new_string(LAUNCH_KEY_CHECKIN
);
1449 if ((ld_resp
= launch_msg(ld_msg
)) == NULL
)
1451 cupsdLogMessage(CUPSD_LOG_ERROR
,
1452 "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
1455 return; /* anti-compiler-warning */
1458 if (launch_data_get_type(ld_resp
) == LAUNCH_DATA_ERRNO
)
1460 errno
= launch_data_get_errno(ld_resp
);
1461 cupsdLogMessage(CUPSD_LOG_ERROR
, "launchd_checkin: Check-in failed: %s",
1464 return; /* anti-compiler-warning */
1468 * Get the sockets dictionary...
1471 if ((ld_sockets
= launch_data_dict_lookup(ld_resp
, LAUNCH_JOBKEY_SOCKETS
))
1474 cupsdLogMessage(CUPSD_LOG_ERROR
,
1475 "launchd_checkin: No sockets found to answer requests on!");
1477 return; /* anti-compiler-warning */
1481 * Get the array of listener sockets...
1484 if ((ld_array
= launch_data_dict_lookup(ld_sockets
, "Listeners")) == NULL
)
1486 cupsdLogMessage(CUPSD_LOG_ERROR
,
1487 "launchd_checkin: No sockets found to answer requests on!");
1489 return; /* anti-compiler-warning */
1493 * Add listening fd(s) to the Listener array...
1496 if (launch_data_get_type(ld_array
) == LAUNCH_DATA_ARRAY
)
1498 count
= launch_data_array_get_count(ld_array
);
1500 for (i
= 0; i
< count
; i
++)
1503 * Get the launchd file descriptor and address...
1506 if ((tmp
= launch_data_array_get_index(ld_array
, i
)) != NULL
)
1508 fd
= launch_data_get_fd(tmp
);
1509 addrlen
= sizeof(addr
);
1511 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1513 cupsdLogMessage(CUPSD_LOG_ERROR
,
1514 "launchd_checkin: Unable to get local address - %s",
1520 * Try to match the launchd socket address to one of the listeners...
1523 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1525 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1526 if (httpAddrEqual(&lis
->address
, &addr
))
1530 * Add a new listener If there's no match...
1535 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1536 "launchd_checkin: Matched existing listener %s with fd %d...",
1537 httpAddrString(&(lis
->address
), s
, sizeof(s
)), fd
);
1541 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1542 "launchd_checkin: Adding new listener %s with fd %d...",
1543 httpAddrString(&addr
, s
, sizeof(s
)), fd
);
1545 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1547 cupsdLogMessage(CUPSD_LOG_ERROR
,
1548 "launchd_checkin: Unable to allocate listener - %s.",
1553 cupsArrayAdd(Listeners
, lis
);
1555 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1564 if (lis
->address
.addr
.sa_family
== AF_INET6
)
1565 portnum
= ntohs(lis
->address
.ipv6
.sin6_port
);
1567 # endif /* AF_INET6 */
1568 if (lis
->address
.addr
.sa_family
== AF_INET
)
1569 portnum
= ntohs(lis
->address
.ipv4
.sin_port
);
1572 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1573 # endif /* HAVE_SSL */
1578 launch_data_free(ld_msg
);
1579 launch_data_free(ld_resp
);
1584 * 'launchd_checkout()' - Update the launchd KeepAlive file as needed.
1588 launchd_checkout(void)
1590 int fd
; /* File descriptor */
1594 * Create or remove the launchd KeepAlive file based on whether
1595 * there are active jobs, polling, browsing for remote printers or
1596 * shared printers to advertise...
1599 if ((cupsArrayCount(ActiveJobs
) || NumPolled
||
1601 (BrowseRemoteProtocols
||
1602 (BrowseLocalProtocols
&& NumBrowsers
&& cupsArrayCount(Printers
))))))
1604 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1605 "Creating launchd keepalive file \"" CUPS_KEEPALIVE
"\"...");
1607 if ((fd
= open(CUPS_KEEPALIVE
, O_RDONLY
| O_CREAT
| O_EXCL
, S_IRUSR
)) >= 0)
1612 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1613 "Removing launchd keepalive file \"" CUPS_KEEPALIVE
"\"...");
1615 unlink(CUPS_KEEPALIVE
);
1618 #endif /* HAVE_LAUNCHD */
1622 * 'parent_handler()' - Catch USR1/CHLD signals...
1626 parent_handler(int sig
) /* I - Signal */
1629 * Store the signal we got from the OS and return...
1632 parent_signal
= sig
;
1637 * 'process_children()' - Process all dead children...
1641 process_children(void)
1643 int status
; /* Exit status of child */
1644 int pid
, /* Process ID of child */
1645 job_id
; /* Job ID of child */
1646 cupsd_job_t
*job
; /* Current job */
1647 int i
; /* Looping var */
1648 char name
[1024]; /* Process name */
1651 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1654 * Reset the dead_children flag...
1660 * Collect the exit status of some children...
1664 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1665 #elif defined(HAVE_WAIT3)
1666 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1668 if ((pid
= wait(&status
)) > 0)
1669 #endif /* HAVE_WAITPID */
1672 * Collect the name of the process that finished...
1675 cupsdFinishProcess(pid
, name
, sizeof(name
), &job_id
);
1678 * Delete certificates for CGI processes...
1682 cupsdDeleteCert(pid
);
1685 * Handle completed job filters...
1688 if (job_id
> 0 && (job
= cupsdFindJob(job_id
)) != NULL
)
1690 for (i
= 0; job
->filters
[i
]; i
++)
1691 if (job
->filters
[i
] == pid
)
1694 if (job
->filters
[i
] || job
->backend
== pid
)
1697 * OK, this process has gone away; what's left?
1700 if (job
->filters
[i
])
1701 job
->filters
[i
] = -pid
;
1703 job
->backend
= -pid
;
1705 if (status
&& status
!= SIGTERM
&& status
!= SIGKILL
&&
1709 * An error occurred; save the exit status so we know to stop
1710 * the printer or cancel the job when all of the filters finish...
1712 * A negative status indicates that the backend failed and the
1713 * printer needs to be stopped.
1716 if (job
->filters
[i
])
1717 job
->status
= status
; /* Filter failed */
1719 job
->status
= -status
; /* Backend failed */
1721 if (job
->state_value
== IPP_JOB_PROCESSING
&&
1722 job
->status_level
> CUPSD_LOG_ERROR
)
1724 char message
[1024]; /* New printer-state-message */
1727 job
->status_level
= CUPSD_LOG_ERROR
;
1729 snprintf(message
, sizeof(message
), "%s failed", name
);
1733 strlcpy(job
->printer
->state_message
, message
,
1734 sizeof(job
->printer
->state_message
));
1735 cupsdAddPrinterHistory(job
->printer
);
1741 if (!job
->printer_message
&& job
->attrs
)
1743 if ((job
->printer_message
=
1744 ippFindAttribute(job
->attrs
, "job-printer-state-message",
1745 IPP_TAG_TEXT
)) == NULL
)
1746 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
,
1748 "job-printer-state-message",
1752 if (job
->printer_message
)
1753 cupsdSetString(&(job
->printer_message
->values
[0].string
.text
),
1759 * If this is not the last file in a job, see if all of the
1760 * filters are done, and if so move to the next file.
1763 if (job
->current_file
< job
->num_files
)
1765 for (i
= 0; job
->filters
[i
] < 0; i
++);
1767 if (!job
->filters
[i
])
1770 * Process the next file...
1773 cupsdContinueJob(job
);
1780 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1781 * since they come when we kill/end a process...
1784 if (status
== SIGTERM
|| status
== SIGKILL
)
1786 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1787 "PID %d (%s) was terminated normally with signal %d.",
1792 if (WIFEXITED(status
))
1793 cupsdLogMessage(CUPSD_LOG_DEBUG
, "PID %d (%s) stopped with status %d!",
1794 pid
, name
, WEXITSTATUS(status
));
1796 cupsdLogMessage(CUPSD_LOG_ERROR
, "PID %d (%s) crashed on signal %d!",
1797 pid
, name
, WTERMSIG(status
));
1799 if (LogLevel
< CUPSD_LOG_DEBUG
)
1800 cupsdLogMessage(CUPSD_LOG_INFO
,
1801 "Hint: Try setting the LogLevel to \"debug\" to find "
1805 cupsdLogMessage(CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1810 * If wait*() is interrupted by a signal, tell main() to call us again...
1813 if (pid
< 0 && errno
== EINTR
)
1819 * 'select_timeout()' - Calculate the select timeout value.
1823 static long /* O - Number of seconds */
1824 select_timeout(int fds
) /* I - Number of descriptors returned */
1826 long timeout
; /* Timeout for select */
1827 time_t now
; /* Current time */
1828 cupsd_client_t
*con
; /* Client information */
1829 cupsd_printer_t
*p
; /* Printer information */
1830 cupsd_job_t
*job
; /* Job information */
1831 cupsd_subscription_t
*sub
; /* Subscription information */
1832 const char *why
; /* Debugging aid */
1836 * Check to see if any of the clients have pending data to be
1837 * processed; if so, the timeout should be 0...
1840 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1842 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1843 if (con
->http
.used
> 0)
1847 * If select has been active in the last second (fds > 0) or we have
1848 * many resources in use then don't bother trying to optimize the
1849 * timeout, just make it 1 second.
1852 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1856 * Otherwise, check all of the possible events that we need to wake for...
1860 timeout
= now
+ 86400; /* 86400 == 1 day */
1865 * When going to sleep, wake up to cancel jobs that don't complete in time.
1868 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1870 timeout
= SleepJobs
;
1871 why
= "cancel jobs before sleeping";
1873 #endif /* __APPLE__ */
1876 * Check whether we are accepting new connections...
1879 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1880 ListeningPaused
< timeout
)
1882 if (ListeningPaused
<= now
)
1885 timeout
= ListeningPaused
;
1887 why
= "resume listening";
1891 * Check the activity and close old clients...
1894 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1896 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1897 if ((con
->http
.activity
+ Timeout
) < timeout
)
1899 timeout
= con
->http
.activity
+ Timeout
;
1900 why
= "timeout a client connection";
1904 * Update the browse list as needed...
1907 if (Browsing
&& BrowseLocalProtocols
)
1910 if ((BrowseLocalProtocols
& BROWSE_SLP
) && (BrowseSLPRefresh
< timeout
))
1912 timeout
= BrowseSLPRefresh
;
1913 why
= "update SLP browsing";
1915 #endif /* HAVE_LIBSLP */
1918 if ((BrowseLocalProtocols
& BROWSE_LDAP
) && (BrowseLDAPRefresh
< timeout
))
1920 timeout
= BrowseLDAPRefresh
;
1921 why
= "update LDAP browsing";
1923 #endif /* HAVE_LDAP */
1925 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && NumBrowsers
)
1927 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1929 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1931 if (p
->type
& CUPS_PRINTER_REMOTE
)
1933 if ((p
->browse_time
+ BrowseTimeout
) < timeout
)
1935 timeout
= p
->browse_time
+ BrowseTimeout
;
1936 why
= "browse timeout a printer";
1939 else if (p
->shared
&& !(p
->type
& CUPS_PRINTER_IMPLICIT
))
1941 if (BrowseInterval
&& (p
->browse_time
+ BrowseInterval
) < timeout
)
1943 timeout
= p
->browse_time
+ BrowseInterval
;
1944 why
= "send browse update";
1952 * Write out changes to configuration and state files...
1955 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1957 timeout
= DirtyCleanTime
;
1958 why
= "write dirty config/state files";
1962 * Check for any active jobs...
1965 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1967 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1969 if (job
->kill_time
&& job
->kill_time
< timeout
)
1971 timeout
= job
->kill_time
;
1972 why
= "kill unresponsive jobs";
1975 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1977 timeout
= job
->hold_until
;
1978 why
= "release held jobs";
1980 else if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1983 why
= "start pending jobs";
1988 #ifdef HAVE_MALLINFO
1990 * Log memory usage every minute...
1993 if (LogLevel
>= CUPSD_LOG_DEBUG
&& (mallinfo_time
+ 60) < timeout
)
1995 timeout
= mallinfo_time
+ 60;
1996 why
= "display memory usage";
1998 #endif /* HAVE_MALLINFO */
2001 * Expire subscriptions as needed...
2004 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
2006 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
2007 if (!sub
->job
&& sub
->expire
&& sub
->expire
< timeout
)
2009 timeout
= sub
->expire
;
2010 why
= "expire subscription";
2014 * Adjust from absolute to relative time. If p->browse_time above
2015 * was 0 then we can end up with a negative value here, so check.
2016 * We add 1 second to the timeout since events occur after the
2017 * timeout expires, and limit the timeout to 86400 seconds (1 day)
2018 * to avoid select() timeout limits present on some operating
2022 timeout
= timeout
- now
+ 1;
2026 else if (timeout
> 86400)
2030 * Log and return the timeout value...
2033 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
2041 * 'sigchld_handler()' - Handle 'child' signals from old processes.
2045 sigchld_handler(int sig
) /* I - Signal number */
2050 * Flag that we have dead children...
2056 * Reset the signal handler as needed...
2059 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2060 signal(SIGCLD
, sigchld_handler
);
2061 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2066 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
2070 sighup_handler(int sig
) /* I - Signal number */
2074 NeedReload
= RELOAD_ALL
;
2075 ReloadTime
= time(NULL
);
2077 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2078 signal(SIGHUP
, sighup_handler
);
2079 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2084 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
2088 sigterm_handler(int sig
) /* I - Signal number */
2090 (void)sig
; /* remove compiler warnings... */
2093 * Flag that we should stop and return...
2101 * 'usage()' - Show scheduler usage.
2105 usage(int status
) /* O - Exit status */
2107 _cupsLangPuts(status
? stderr
: stdout
,
2108 _("Usage: cupsd [-c config-file] [-f] [-F] [-h] [-l]\n"
2110 "-c config-file Load alternate configuration file\n"
2111 "-f Run in the foreground\n"
2112 "-F Run in the foreground but detach\n"
2113 "-h Show this usage message\n"
2114 "-l Run cupsd from launchd(8)\n"));
2120 * End of "$Id: main.c 7925 2008-09-10 17:47:26Z mike $".