]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/main.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / main.c
1 /*
2 * "$Id: main.c 5108 2006-02-15 19:33:09Z mike $"
3 *
4 * Scheduler main loop for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for the CUPS scheduler.
27 * cupsdClosePipe() - Close a pipe as necessary.
28 * cupsdOpenPipe() - Create a pipe which is closed on exec.
29 * cupsdCatchChildSignals() - Catch SIGCHLD signals...
30 * cupsdHoldSignals() - Hold child and termination signals.
31 * cupsdIgnoreChildSignals() - Ignore SIGCHLD signals...
32 * cupsdReleaseSignals() - Release signals for delivery.
33 * cupsdSetString() - Set a string value.
34 * cupsdSetStringf() - Set a formatted string value.
35 * launchd_checkin() - Check-in with launchd and collect the
36 * listening fds.
37 * launchd_reload() - Tell launchd to reload the configuration
38 * file to pick up the new listening directives.
39 * launchd_sync_conf() - Re-write the launchd(8) config file
40 * org.cups.cupsd.plist based on cupsd.conf.
41 * parent_handler() - Catch USR1/CHLD signals...
42 * process_children() - Process all dead children...
43 * sigchld_handler() - Handle 'child' signals from old processes.
44 * sighup_handler() - Handle 'hangup' signals to reconfigure the
45 * scheduler.
46 * sigterm_handler() - Handle 'terminate' signals that stop the
47 * scheduler.
48 * select_timeout() - Calculate the select timeout value.
49 * usage() - Show scheduler usage.
50 */
51
52 /*
53 * Include necessary headers...
54 */
55
56 #define _MAIN_C_
57 #include "cupsd.h"
58 #include <sys/resource.h>
59 #include <syslog.h>
60 #include <grp.h>
61
62 #ifdef HAVE_LAUNCH_H
63 # include <launch.h>
64 # include <libgen.h>
65 #endif /* HAVE_LAUNCH_H */
66
67 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
68 # include <malloc.h>
69 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
70 #ifdef HAVE_NOTIFY_H
71 # include <notify.h>
72 #endif /* HAVE_NOTIFY_H */
73
74
75 /*
76 * Local functions...
77 */
78
79 #ifdef HAVE_LAUNCHD
80 static void launchd_checkin(void);
81 static void launchd_reload(void);
82 static int launchd_sync_conf(void);
83 #endif /* HAVE_LAUNCHD */
84
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);
92
93
94 /*
95 * Local globals...
96 */
97
98 static int parent_signal = 0; /* Set to signal number from child */
99 static int holdcount = 0; /* Number of times "hold" was called */
100 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
101 static sigset_t holdmask; /* Old POSIX signal mask */
102 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
103 static int dead_children = 0; /* Dead children? */
104 static int stop_scheduler = 0; /* Should the scheduler stop? */
105
106
107 /*
108 * 'main()' - Main entry for the CUPS scheduler.
109 */
110
111 int /* O - Exit status */
112 main(int argc, /* I - Number of command-line args */
113 char *argv[]) /* I - Command-line arguments */
114 {
115 int i; /* Looping var */
116 char *opt; /* Option character */
117 int fg; /* Run in the foreground */
118 int fds; /* Number of ready descriptors */
119 fd_set *input, /* Input set for select() */
120 *output; /* Output set for select() */
121 cupsd_client_t *con; /* Current client */
122 cupsd_job_t *job; /* Current job */
123 cupsd_listener_t *lis; /* Current listener */
124 time_t current_time, /* Current time */
125 activity, /* Client activity timer */
126 browse_time, /* Next browse send time */
127 senddoc_time, /* Send-Document time */
128 expire_time; /* Subscription expire time */
129 time_t mallinfo_time; /* Malloc information time */
130 size_t string_count, /* String count */
131 alloc_bytes, /* Allocated string bytes */
132 total_bytes; /* Total string bytes */
133 struct timeval timeout; /* select() timeout */
134 struct rlimit limit; /* Runtime limit */
135 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
136 struct sigaction action; /* Actions for POSIX signals */
137 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
138 #ifdef __sgi
139 cups_file_t *fp; /* Fake lpsched lock file */
140 struct stat statbuf; /* Needed for checking lpsched FIFO */
141 #endif /* __sgi */
142 #if HAVE_LAUNCHD
143 int launchd_idle_exit;
144 /* Idle exit on select timeout? */
145 #endif /* HAVE_LAUNCHD */
146
147
148 /*
149 * Check for command-line arguments...
150 */
151
152 fg = 0;
153
154 for (i = 1; i < argc; i ++)
155 if (argv[i][0] == '-')
156 for (opt = argv[i] + 1; *opt != '\0'; opt ++)
157 switch (*opt)
158 {
159 case 'c' : /* Configuration file */
160 i ++;
161 if (i >= argc)
162 {
163 _cupsLangPuts(stderr, _("cupsd: Expected config filename "
164 "after \"-c\" option!\n"));
165 usage(1);
166 }
167
168 if (argv[i][0] == '/')
169 {
170 /*
171 * Absolute directory...
172 */
173
174 cupsdSetString(&ConfigurationFile, argv[i]);
175 }
176 else
177 {
178 /*
179 * Relative directory...
180 */
181
182 char *current; /* Current directory */
183
184
185 /*
186 * Allocate a buffer for the current working directory to
187 * reduce run-time stack usage; this approximates the
188 * behavior of some implementations of getcwd() when they
189 * are passed a NULL pointer.
190 */
191
192 current = malloc(1024);
193 getcwd(current, 1024);
194
195 cupsdSetStringf(&ConfigurationFile, "%s/%s", current, argv[i]);
196
197 free(current);
198 }
199 break;
200
201 case 'f' : /* Run in foreground... */
202 fg = 1;
203 break;
204
205 case 'F' : /* Run in foreground, but disconnect from terminal... */
206 fg = -1;
207 break;
208
209 case 'h' : /* Show usage/help */
210 usage(0);
211 break;
212
213 case 'l' : /* Started by launchd... */
214 #ifdef HAVE_LAUNCHD
215 Launchd = 1;
216 fg = 1;
217 #else
218 _cupsLangPuts(stderr, _("cupsd: launchd(8) support not compiled "
219 "in, running in normal mode.\n"));
220 fg = 0;
221 #endif /* HAVE_LAUNCHD */
222 break;
223
224 default : /* Unknown option */
225 _cupsLangPrintf(stderr, _("cupsd: Unknown option \"%c\" - "
226 "aborting!\n"), *opt);
227 usage(1);
228 break;
229 }
230 else
231 {
232 _cupsLangPrintf(stderr, _("cupsd: Unknown argument \"%s\" - aborting!\n"),
233 argv[i]);
234 usage(1);
235 }
236
237 if (!ConfigurationFile)
238 cupsdSetString(&ConfigurationFile, CUPS_SERVERROOT "/cupsd.conf");
239
240 /*
241 * If the user hasn't specified "-f", run in the background...
242 */
243
244 if (!fg)
245 {
246 /*
247 * Setup signal handlers for the parent...
248 */
249
250 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
251 sigset(SIGUSR1, parent_handler);
252 sigset(SIGCHLD, parent_handler);
253
254 sigset(SIGHUP, SIG_IGN);
255 #elif defined(HAVE_SIGACTION)
256 memset(&action, 0, sizeof(action));
257 sigemptyset(&action.sa_mask);
258 sigaddset(&action.sa_mask, SIGUSR1);
259 action.sa_handler = parent_handler;
260 sigaction(SIGUSR1, &action, NULL);
261 sigaction(SIGCHLD, &action, NULL);
262
263 sigemptyset(&action.sa_mask);
264 action.sa_handler = SIG_IGN;
265 sigaction(SIGHUP, &action, NULL);
266 #else
267 signal(SIGUSR1, parent_handler);
268 signal(SIGCLD, parent_handler);
269
270 signal(SIGHUP, SIG_IGN);
271 #endif /* HAVE_SIGSET */
272
273 if (fork() > 0)
274 {
275 /*
276 * OK, wait for the child to startup and send us SIGUSR1 or to crash
277 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
278 * might be sent by the init script to restart the scheduler...
279 */
280
281 for (; parent_signal == 0;)
282 sleep(1);
283
284 if (parent_signal == SIGUSR1)
285 return (0);
286
287 if (wait(&i) < 0)
288 {
289 perror("cupsd");
290 return (1);
291 }
292 else if (WIFEXITED(i))
293 {
294 fprintf(stderr, "cupsd: Child exited with status %d!\n",
295 WEXITSTATUS(i));
296 return (2);
297 }
298 else
299 {
300 fprintf(stderr, "cupsd: Child exited on signal %d!\n", WTERMSIG(i));
301 return (3);
302 }
303 }
304 }
305
306 if (fg < 1)
307 {
308 /*
309 * Make sure we aren't tying up any filesystems...
310 */
311
312 chdir("/");
313
314 #ifndef DEBUG
315 /*
316 * Disable core dumps...
317 */
318
319 getrlimit(RLIMIT_CORE, &limit);
320 limit.rlim_cur = 0;
321 setrlimit(RLIMIT_CORE, &limit);
322
323 /*
324 * Disconnect from the controlling terminal...
325 */
326
327 setsid();
328
329 /*
330 * Close all open files...
331 */
332
333 getrlimit(RLIMIT_NOFILE, &limit);
334
335 for (i = 0; i < limit.rlim_cur; i ++)
336 close(i);
337 #endif /* DEBUG */
338 }
339
340 /*
341 * Set the timezone info...
342 */
343
344 tzset();
345
346 #ifdef LC_TIME
347 setlocale(LC_TIME, "");
348 #endif /* LC_TIME */
349
350 /*
351 * Set the maximum number of files...
352 */
353
354 getrlimit(RLIMIT_NOFILE, &limit);
355
356 if (limit.rlim_max > CUPS_MAX_FDS)
357 MaxFDs = CUPS_MAX_FDS;
358 else
359 MaxFDs = limit.rlim_max;
360
361 limit.rlim_cur = MaxFDs;
362
363 setrlimit(RLIMIT_NOFILE, &limit);
364
365 /*
366 * Allocate memory for the input and output sets...
367 */
368
369 SetSize = (MaxFDs + 31) / 8 + 4;
370 if (SetSize < sizeof(fd_set))
371 SetSize = sizeof(fd_set);
372
373 InputSet = (fd_set *)calloc(1, SetSize);
374 OutputSet = (fd_set *)calloc(1, SetSize);
375 input = (fd_set *)calloc(1, SetSize);
376 output = (fd_set *)calloc(1, SetSize);
377
378 if (InputSet == NULL || OutputSet == NULL || input == NULL || output == NULL)
379 {
380 syslog(LOG_LPR, "Unable to allocate memory for select() sets - exiting!");
381 return (1);
382 }
383
384 /*
385 * Read configuration...
386 */
387
388 if (!cupsdReadConfiguration())
389 {
390 syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting!",
391 ConfigurationFile);
392 return (1);
393 }
394
395 #if HAVE_LAUNCHD
396 if (Launchd)
397 {
398 /*
399 * If we were started by launchd make sure the cupsd plist file contains the
400 * same listeners as cupsd.conf; If it didn't then reload it before getting
401 * the list of listening file descriptors...
402 */
403
404 if (launchd_sync_conf())
405 {
406 launchd_reload();
407
408 /*
409 * Until rdar://3854821 is fixed we have to exit after the reload...
410 */
411
412 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Exiting on launchd_reload");
413 exit(0);
414 }
415
416 launchd_checkin();
417 }
418 #endif /* HAVE_LAUNCHD */
419
420 /*
421 * Startup the server...
422 */
423
424 cupsdStartServer();
425
426 /*
427 * Catch hangup and child signals and ignore broken pipes...
428 */
429
430 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
431 if (RunAsUser)
432 sigset(SIGHUP, sigterm_handler);
433 else
434 sigset(SIGHUP, sighup_handler);
435
436 sigset(SIGPIPE, SIG_IGN);
437 sigset(SIGTERM, sigterm_handler);
438 #elif defined(HAVE_SIGACTION)
439 memset(&action, 0, sizeof(action));
440
441 sigemptyset(&action.sa_mask);
442 sigaddset(&action.sa_mask, SIGHUP);
443
444 if (RunAsUser)
445 action.sa_handler = sigterm_handler;
446 else
447 action.sa_handler = sighup_handler;
448
449 sigaction(SIGHUP, &action, NULL);
450
451 sigemptyset(&action.sa_mask);
452 action.sa_handler = SIG_IGN;
453 sigaction(SIGPIPE, &action, NULL);
454
455 sigemptyset(&action.sa_mask);
456 sigaddset(&action.sa_mask, SIGTERM);
457 sigaddset(&action.sa_mask, SIGCHLD);
458 action.sa_handler = sigterm_handler;
459 sigaction(SIGTERM, &action, NULL);
460 #else
461 if (RunAsUser)
462 signal(SIGHUP, sigterm_handler);
463 else
464 signal(SIGHUP, sighup_handler);
465
466 signal(SIGPIPE, SIG_IGN);
467 signal(SIGTERM, sigterm_handler);
468 #endif /* HAVE_SIGSET */
469
470 #ifdef __sgi
471 /*
472 * Try to create a fake lpsched lock file if one is not already there.
473 * Some Adobe applications need it under IRIX in order to enable
474 * printing...
475 */
476
477 if ((fp = cupsFileOpen("/var/spool/lp/SCHEDLOCK", "w")) == NULL)
478 {
479 syslog(LOG_LPR, "Unable to create fake lpsched lock file "
480 "\"/var/spool/lp/SCHEDLOCK\"\' - %s!",
481 strerror(errno));
482 }
483 else
484 {
485 fchmod(cupsFileNumber(fp), 0644);
486 fchown(cupsFileNumber(fp), User, Group);
487
488 cupsFileClose(fp);
489 }
490 #endif /* __sgi */
491
492 /*
493 * Initialize authentication certificates...
494 */
495
496 cupsdInitCerts();
497
498 /*
499 * If we are running in the background, signal the parent process that
500 * we are up and running...
501 */
502
503 if (!fg)
504 {
505 /*
506 * Send a signal to the parent process, but only if the parent is
507 * not PID 1 (init). This avoids accidentally shutting down the
508 * system on OpenBSD if you CTRL-C the server before it is up...
509 */
510
511 i = getppid(); /* Save parent PID to avoid race condition */
512
513 if (i != 1)
514 kill(i, SIGUSR1);
515 }
516
517 /*
518 * Start power management framework...
519 */
520
521 cupsdStartSystemMonitor();
522
523 /*
524 * If the administrator has configured the server to run as an unpriviledged
525 * user, change to that user now...
526 */
527
528 if (RunAsUser)
529 {
530 setgid(Group);
531 setgroups(1, &Group);
532 setuid(User);
533 }
534
535 /*
536 * Catch signals...
537 */
538
539 cupsdCatchChildSignals();
540
541 /*
542 * Start any pending print jobs...
543 */
544
545 cupsdCheckJobs();
546
547 /*
548 * Loop forever...
549 */
550
551 mallinfo_time = 0;
552 browse_time = time(NULL);
553 senddoc_time = time(NULL);
554 expire_time = time(NULL);
555 fds = 1;
556
557 while (!stop_scheduler)
558 {
559 #ifdef DEBUG
560 cupsdLogMessage(CUPSD_LOG_DEBUG2,
561 "main: Top of loop, dead_children=%d, NeedReload=%d",
562 dead_children, NeedReload);
563 #endif /* DEBUG */
564
565 /*
566 * Check if there are dead children to handle...
567 */
568
569 if (dead_children)
570 process_children();
571
572 /*
573 * Check if we need to load the server configuration file...
574 */
575
576 if (NeedReload)
577 {
578 /*
579 * Close any idle clients...
580 */
581
582 if (cupsArrayCount(Clients) > 0)
583 {
584 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
585 con;
586 con = (cupsd_client_t *)cupsArrayNext(Clients))
587 if (con->http.state == HTTP_WAITING)
588 cupsdCloseClient(con);
589 else
590 con->http.keep_alive = HTTP_KEEPALIVE_OFF;
591
592 cupsdPauseListening();
593 }
594
595 /*
596 * Check for any active jobs...
597 */
598
599 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
600 job;
601 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
602 if (job->state_value == IPP_JOB_PROCESSING)
603 break;
604
605 /*
606 * Restart if all clients are closed and all jobs finished, or
607 * if the reload timeout has elapsed...
608 */
609
610 if ((cupsArrayCount(Clients) == 0 &&
611 (!job || NeedReload != RELOAD_ALL)) ||
612 (time(NULL) - ReloadTime) >= ReloadTimeout)
613 {
614 /*
615 * Shutdown the server...
616 */
617
618 cupsdStopServer();
619
620 /*
621 * Read configuration...
622 */
623
624 if (!cupsdReadConfiguration())
625 {
626 syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting!",
627 ConfigurationFile);
628 break;
629 }
630
631 #if HAVE_LAUNCHD
632 if (Launchd)
633 {
634 if (launchd_sync_conf())
635 {
636 launchd_reload();
637
638 /*
639 * Until rdar://3854821 is fixed we have to exit after the reload...
640 */
641
642 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Exiting on launchd_reload");
643 stop_scheduler = 1;
644 break;
645 }
646
647 launchd_checkin();
648 }
649 #endif /* HAVE_LAUNCHD */
650
651 /*
652 * Startup the server...
653 */
654
655 cupsdStartServer();
656 }
657 }
658
659 /*
660 * Check for available input or ready output. If select() returns
661 * 0 or -1, something bad happened and we should exit immediately.
662 *
663 * Note that we at least have one listening socket open at all
664 * times.
665 */
666
667 memcpy(input, InputSet, SetSize);
668 memcpy(output, OutputSet, SetSize);
669
670 timeout.tv_sec = select_timeout(fds);
671 timeout.tv_usec = 0;
672
673 #if HAVE_LAUNCHD
674 /*
675 * If no other work is scheduled and we're being controlled by
676 * launchd(8) then timeout after 'LaunchdTimeout' seconds of
677 * inactivity...
678 */
679
680 if (timeout.tv_sec == 86400 && Launchd && LaunchdTimeout &&
681 (!Browsing || !(BrowseLocalProtocols & BROWSE_DNSSD) ||
682 cupsArrayCount(Printers) == 0))
683 {
684 timeout.tv_sec = LaunchdTimeout;
685 launchd_idle_exit = 1;
686 }
687 else
688 launchd_idle_exit = 0;
689 #endif /* HAVE_LAUNCHD */
690
691 if (timeout.tv_sec < 86400) /* Only use timeout for < 1 day */
692 fds = select(MaxFDs, input, output, NULL, &timeout);
693 else
694 fds = select(MaxFDs, input, output, NULL, NULL);
695
696 if (fds < 0)
697 {
698 char s[16384], /* String buffer */
699 *sptr; /* Pointer into buffer */
700 int slen; /* Length of string buffer */
701
702
703 /*
704 * Got an error from select!
705 */
706
707 if (errno == EINTR) /* Just interrupted by a signal */
708 continue;
709
710 /*
711 * Log all sorts of debug info to help track down the problem.
712 */
713
714 cupsdLogMessage(CUPSD_LOG_EMERG, "select() failed - %s!",
715 strerror(errno));
716
717 strcpy(s, "InputSet =");
718 slen = 10;
719 sptr = s + 10;
720
721 for (i = 0; i < MaxFDs; i ++)
722 if (FD_ISSET(i, InputSet))
723 {
724 snprintf(sptr, sizeof(s) - slen, " %d", i);
725 slen += strlen(sptr);
726 sptr += strlen(sptr);
727 }
728
729 cupsdLogMessage(CUPSD_LOG_EMERG, s);
730
731 strcpy(s, "OutputSet =");
732 slen = 11;
733 sptr = s + 11;
734
735 for (i = 0; i < MaxFDs; i ++)
736 if (FD_ISSET(i, OutputSet))
737 {
738 snprintf(sptr, sizeof(s) - slen, " %d", i);
739 slen += strlen(sptr);
740 sptr += strlen(sptr);
741 }
742
743 cupsdLogMessage(CUPSD_LOG_EMERG, s);
744
745 for (i = 0, con = (cupsd_client_t *)cupsArrayFirst(Clients);
746 con;
747 i ++, con = (cupsd_client_t *)cupsArrayNext(Clients))
748 cupsdLogMessage(CUPSD_LOG_EMERG,
749 "Clients[%d] = %d, file = %d, state = %d",
750 i, con->http.fd, con->file, con->http.state);
751
752 for (i = 0, lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
753 lis;
754 i ++, lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
755 cupsdLogMessage(CUPSD_LOG_EMERG, "Listeners[%d] = %d", i, lis->fd);
756
757 cupsdLogMessage(CUPSD_LOG_EMERG, "BrowseSocket = %d", BrowseSocket);
758
759 cupsdLogMessage(CUPSD_LOG_EMERG, "CGIPipes[0] = %d", CGIPipes[0]);
760
761 #ifdef __APPLE__
762 cupsdLogMessage(CUPSD_LOG_EMERG, "SysEventPipes[0] = %d",
763 SysEventPipes[0]);
764 #endif /* __APPLE__ */
765
766 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
767 job;
768 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
769 cupsdLogMessage(CUPSD_LOG_EMERG, "Jobs[%d] = %d < [%d %d] > [%d %d]",
770 job->id,
771 job->status_buffer ? job->status_buffer->fd : -1,
772 job->print_pipes[0], job->print_pipes[1],
773 job->back_pipes[0], job->back_pipes[1]);
774 break;
775 }
776
777 current_time = time(NULL);
778
779 #if HAVE_LAUNCHD
780 /*
781 * If no other work was scheduled and we're being controlled by launchd(8)
782 * then timeout after 'LaunchdTimeout' seconds of inactivity...
783 */
784
785 if (!fds && launchd_idle_exit)
786 {
787 cupsdLogMessage(CUPSD_LOG_INFO,
788 "Printer sharing is off and there are no jobs pending, "
789 "will restart on demand.");
790 stop_scheduler = 1;
791 break;
792 }
793 #endif /* HAVE_LAUNCHD */
794
795 /*
796 * Check for status info from job filters...
797 */
798
799 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
800 job;
801 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
802 if (job->status_buffer && FD_ISSET(job->status_buffer->fd, input))
803 {
804 /*
805 * Clear the input bit to avoid updating the next job
806 * using the same status pipe file descriptor...
807 */
808
809 FD_CLR(job->status_buffer->fd, input);
810
811 /*
812 * Read any status messages from the filters...
813 */
814
815 cupsdUpdateJob(job);
816 }
817
818 /*
819 * Update CGI messages as needed...
820 */
821
822 if (CGIPipes[0] >= 0 && FD_ISSET(CGIPipes[0], input))
823 cupsdUpdateCGI();
824
825 /*
826 * Handle system management events as needed...
827 */
828
829 #ifdef __APPLE__
830 if (SysEventPipes[0] >= 0 && FD_ISSET(SysEventPipes[0], input))
831 cupsdUpdateSystemMonitor();
832 #endif /* __APPLE__ */
833
834 /*
835 * Update notifier messages as needed...
836 */
837
838 if (NotifierPipes[0] >= 0 && FD_ISSET(NotifierPipes[0], input))
839 cupsdUpdateNotifierStatus();
840
841 /*
842 * Expire subscriptions and unload completed jobs as needed...
843 */
844
845 if (current_time > expire_time)
846 {
847 if (cupsArrayCount(Subscriptions) > 0)
848 cupsdExpireSubscriptions(NULL, NULL);
849
850 cupsdUnloadCompletedJobs();
851
852 expire_time = current_time;
853 }
854
855 /*
856 * Update the browse list as needed...
857 */
858
859 if (Browsing && BrowseRemoteProtocols)
860 {
861 if (BrowseSocket >= 0 && FD_ISSET(BrowseSocket, input))
862 cupsdUpdateCUPSBrowse();
863
864 if (PollPipe >= 0 && FD_ISSET(PollPipe, input))
865 cupsdUpdatePolling();
866
867 #ifdef HAVE_LIBSLP
868 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
869 BrowseSLPRefresh <= current_time)
870 cupsdUpdateSLPBrowse();
871 #endif /* HAVE_LIBSLP */
872 }
873
874 if (Browsing && BrowseLocalProtocols && current_time > browse_time)
875 {
876 cupsdSendBrowseList();
877 browse_time = current_time;
878 }
879
880 /*
881 * Check for new connections on the "listen" sockets...
882 */
883
884 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
885 lis;
886 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
887 if (lis->fd >= 0 && FD_ISSET(lis->fd, input))
888 {
889 FD_CLR(lis->fd, input);
890 cupsdAcceptClient(lis);
891 }
892
893 /*
894 * Check for new data on the client sockets...
895 */
896
897 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
898 con;
899 con = (cupsd_client_t *)cupsArrayNext(Clients))
900 {
901 /*
902 * Process the input buffer...
903 */
904
905 if (FD_ISSET(con->http.fd, input) || con->http.used)
906 {
907 int fd = con->file;
908
909
910 FD_CLR(con->http.fd, input);
911
912 if (!cupsdReadClient(con))
913 {
914 if (fd >= 0)
915 FD_CLR(fd, input);
916
917 continue;
918 }
919 }
920
921 /*
922 * Write data as needed...
923 */
924
925 if (con->pipe_pid && FD_ISSET(con->file, input))
926 {
927 /*
928 * Keep track of pending input from the file/pipe separately
929 * so that we don't needlessly spin on select() when the web
930 * client is not ready to receive data...
931 */
932
933 FD_CLR(con->file, input);
934 con->file_ready = 1;
935
936 #ifdef DEBUG
937 cupsdLogMessage(CUPSD_LOG_DEBUG2, "main: Data ready file %d!",
938 con->file);
939 #endif /* DEBUG */
940
941 if (!FD_ISSET(con->http.fd, output))
942 {
943 cupsdLogMessage(CUPSD_LOG_DEBUG2,
944 "main: Removing fd %d from InputSet...", con->file);
945 FD_CLR(con->file, input);
946 FD_CLR(con->file, InputSet);
947 }
948 }
949
950 if (FD_ISSET(con->http.fd, output))
951 {
952 FD_CLR(con->http.fd, output);
953
954 if (!con->pipe_pid || con->file_ready)
955 if (!cupsdWriteClient(con))
956 continue;
957 }
958
959 /*
960 * Check the activity and close old clients...
961 */
962
963 activity = current_time - Timeout;
964 if (con->http.activity < activity && !con->pipe_pid)
965 {
966 cupsdLogMessage(CUPSD_LOG_DEBUG,
967 "Closing client %d after %d seconds of inactivity...",
968 con->http.fd, Timeout);
969
970 cupsdCloseClient(con);
971 continue;
972 }
973 }
974
975 /*
976 * Update any pending multi-file documents...
977 */
978
979 if ((current_time - senddoc_time) >= 10)
980 {
981 cupsdCheckJobs();
982 senddoc_time = current_time;
983 }
984
985 /*
986 * Log memory usage every minute...
987 */
988
989 if ((current_time - mallinfo_time) >= 60 && LogLevel >= CUPSD_LOG_DEBUG2)
990 {
991 #ifdef HAVE_MALLINFO
992 struct mallinfo mem; /* Malloc information */
993
994
995 mem = mallinfo();
996 cupsdLogMessage(CUPSD_LOG_DEBUG2,
997 "mallinfo: arena = %d, used = %d, free = %d\n",
998 mem.arena, mem.usmblks + mem.uordblks,
999 mem.fsmblks + mem.fordblks);
1000 #endif /* HAVE_MALLINFO */
1001
1002 string_count = _cups_sp_statistics(&alloc_bytes, &total_bytes);
1003 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1004 "stringpool: " CUPS_LLFMT " strings, "
1005 CUPS_LLFMT " allocated, " CUPS_LLFMT " total bytes",
1006 CUPS_LLCAST string_count, CUPS_LLCAST alloc_bytes,
1007 CUPS_LLCAST total_bytes);
1008
1009 mallinfo_time = current_time;
1010 }
1011
1012 /*
1013 * Update the root certificate once every 5 minutes...
1014 */
1015
1016 if ((current_time - RootCertTime) >= RootCertDuration && RootCertDuration &&
1017 !RunUser)
1018 {
1019 /*
1020 * Update the root certificate...
1021 */
1022
1023 cupsdDeleteCert(0);
1024 cupsdAddCert(0, "root");
1025 }
1026
1027 /*
1028 * Handle OS-specific event notification for any events that have
1029 * accumulated. Don't send these more than once a second...
1030 */
1031
1032 if (LastEvent && (time(NULL) - LastEventTime) > 1)
1033 {
1034 #ifdef HAVE_NOTIFY_POST
1035 if (LastEvent & CUPSD_EVENT_PRINTER_CHANGED)
1036 {
1037 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1038 "notify_post(\"com.apple.printerListChange\")");
1039 notify_post("com.apple.printerListChange");
1040 }
1041
1042 if (LastEvent & CUPSD_EVENT_PRINTER_STATE_CHANGED)
1043 {
1044 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1045 "notify_post(\"com.apple.printerHistoryChange\")");
1046 notify_post("com.apple.printerHistoryChange");
1047 }
1048
1049 if (LastEvent & (CUPSD_EVENT_JOB_STATE_CHANGED |
1050 CUPSD_EVENT_JOB_CONFIG_CHANGED |
1051 CUPSD_EVENT_JOB_PROGRESS))
1052 {
1053 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1054 "notify_post(\"com.apple.jobChange\")");
1055 notify_post("com.apple.jobChange");
1056 }
1057 #endif /* HAVE_NOTIFY_POST */
1058
1059 /*
1060 * Reset the accumulated events and notification time...
1061 */
1062
1063 LastEventTime = time(NULL);
1064 LastEvent = CUPSD_EVENT_NONE;
1065 }
1066 }
1067
1068 /*
1069 * Log a message based on what happened...
1070 */
1071
1072 if (stop_scheduler)
1073 cupsdLogMessage(CUPSD_LOG_INFO, "Scheduler shutting down normally.");
1074 else
1075 cupsdLogMessage(CUPSD_LOG_ERROR,
1076 "Scheduler shutting down due to program error.");
1077
1078 /*
1079 * Close all network clients and stop all jobs...
1080 */
1081
1082 cupsdStopServer();
1083
1084 cupsdFreeAllJobs();
1085
1086 cupsdStopSystemMonitor();
1087
1088 #ifdef HAVE_LAUNCHD
1089 /*
1090 * Update the launchd config file as needed...
1091 */
1092
1093 launchd_sync_conf();
1094 #endif /* HAVE_LAUNCHD */
1095
1096 #ifdef __sgi
1097 /*
1098 * Remove the fake IRIX lpsched lock file, but only if the existing
1099 * file is not a FIFO which indicates that the real IRIX lpsched is
1100 * running...
1101 */
1102
1103 if (!stat("/var/spool/lp/FIFO", &statbuf))
1104 if (!S_ISFIFO(statbuf.st_mode))
1105 unlink("/var/spool/lp/SCHEDLOCK");
1106 #endif /* __sgi */
1107
1108 /*
1109 * Free memory used by FD sets and return...
1110 */
1111
1112 free(InputSet);
1113 free(OutputSet);
1114 free(input);
1115 free(output);
1116
1117 return (!stop_scheduler);
1118 }
1119
1120
1121 /*
1122 * 'cupsdClosePipe()' - Close a pipe as necessary.
1123 */
1124
1125 void
1126 cupsdClosePipe(int *fds) /* I - Pipe file descriptors (2) */
1127 {
1128 /*
1129 * Close file descriptors as needed...
1130 */
1131
1132 if (fds[0] >= 0)
1133 {
1134 close(fds[0]);
1135 fds[0] = -1;
1136 }
1137
1138 if (fds[1] >= 0)
1139 {
1140 close(fds[1]);
1141 fds[1] = -1;
1142 }
1143 }
1144
1145
1146 /*
1147 * 'cupsdOpenPipe()' - Create a pipe which is closed on exec.
1148 */
1149
1150 int /* O - 0 on success, -1 on error */
1151 cupsdOpenPipe(int *fds) /* O - Pipe file descriptors (2) */
1152 {
1153 /*
1154 * Create the pipe...
1155 */
1156
1157 if (pipe(fds))
1158 return (-1);
1159
1160 /*
1161 * Set the "close on exec" flag on each end of the pipe...
1162 */
1163
1164 if (fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC))
1165 {
1166 close(fds[0]);
1167 close(fds[1]);
1168 return (-1);
1169 }
1170
1171 if (fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD) | FD_CLOEXEC))
1172 {
1173 close(fds[0]);
1174 close(fds[1]);
1175 return (-1);
1176 }
1177
1178 /*
1179 * Return 0 indicating success...
1180 */
1181
1182 return (0);
1183 }
1184
1185
1186 /*
1187 * 'cupsdCatchChildSignals()' - Catch SIGCHLD signals...
1188 */
1189
1190 void
1191 cupsdCatchChildSignals(void)
1192 {
1193 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1194 struct sigaction action; /* Actions for POSIX signals */
1195 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1196
1197
1198 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1199 sigset(SIGCHLD, sigchld_handler);
1200 #elif defined(HAVE_SIGACTION)
1201 memset(&action, 0, sizeof(action));
1202
1203 sigemptyset(&action.sa_mask);
1204 sigaddset(&action.sa_mask, SIGTERM);
1205 sigaddset(&action.sa_mask, SIGCHLD);
1206 action.sa_handler = sigchld_handler;
1207 sigaction(SIGCHLD, &action, NULL);
1208 #else
1209 signal(SIGCLD, sigchld_handler); /* No, SIGCLD isn't a typo... */
1210 #endif /* HAVE_SIGSET */
1211 }
1212
1213
1214 /*
1215 * 'cupsdClearString()' - Clear a string.
1216 */
1217
1218 void
1219 cupsdClearString(char **s) /* O - String value */
1220 {
1221 if (s && *s)
1222 {
1223 free(*s);
1224 *s = NULL;
1225 }
1226 }
1227
1228
1229 /*
1230 * 'cupsdHoldSignals()' - Hold child and termination signals.
1231 */
1232
1233 void
1234 cupsdHoldSignals(void)
1235 {
1236 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1237 sigset_t newmask; /* New POSIX signal mask */
1238 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1239
1240
1241 holdcount ++;
1242 if (holdcount > 1)
1243 return;
1244
1245 #ifdef HAVE_SIGSET
1246 sighold(SIGTERM);
1247 sighold(SIGCHLD);
1248 #elif defined(HAVE_SIGACTION)
1249 sigemptyset(&newmask);
1250 sigaddset(&newmask, SIGTERM);
1251 sigaddset(&newmask, SIGCHLD);
1252 sigprocmask(SIG_BLOCK, &newmask, &holdmask);
1253 #endif /* HAVE_SIGSET */
1254 }
1255
1256
1257 /*
1258 * 'cupsdIgnoreChildSignals()' - Ignore SIGCHLD signals...
1259 *
1260 * We don't really ignore them, we set the signal handler to SIG_DFL,
1261 * since some OS's rely on signals for the wait4() function to work.
1262 */
1263
1264 void
1265 cupsdIgnoreChildSignals(void)
1266 {
1267 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1268 struct sigaction action; /* Actions for POSIX signals */
1269 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1270
1271
1272 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1273 sigset(SIGCHLD, SIG_DFL);
1274 #elif defined(HAVE_SIGACTION)
1275 memset(&action, 0, sizeof(action));
1276
1277 sigemptyset(&action.sa_mask);
1278 sigaddset(&action.sa_mask, SIGCHLD);
1279 action.sa_handler = SIG_DFL;
1280 sigaction(SIGCHLD, &action, NULL);
1281 #else
1282 signal(SIGCLD, SIG_DFL); /* No, SIGCLD isn't a typo... */
1283 #endif /* HAVE_SIGSET */
1284 }
1285
1286
1287 /*
1288 * 'cupsdReleaseSignals()' - Release signals for delivery.
1289 */
1290
1291 void
1292 cupsdReleaseSignals(void)
1293 {
1294 holdcount --;
1295 if (holdcount > 0)
1296 return;
1297
1298 #ifdef HAVE_SIGSET
1299 sigrelse(SIGTERM);
1300 sigrelse(SIGCHLD);
1301 #elif defined(HAVE_SIGACTION)
1302 sigprocmask(SIG_SETMASK, &holdmask, NULL);
1303 #endif /* HAVE_SIGSET */
1304 }
1305
1306
1307 /*
1308 * 'cupsdSetString()' - Set a string value.
1309 */
1310
1311 void
1312 cupsdSetString(char **s, /* O - New string */
1313 const char *v) /* I - String value */
1314 {
1315 if (!s || *s == v)
1316 return;
1317
1318 if (*s)
1319 free(*s);
1320
1321 if (v)
1322 *s = strdup(v);
1323 else
1324 *s = NULL;
1325 }
1326
1327
1328 /*
1329 * 'cupsdSetStringf()' - Set a formatted string value.
1330 */
1331
1332 void
1333 cupsdSetStringf(char **s, /* O - New string */
1334 const char *f, /* I - Printf-style format string */
1335 ...) /* I - Additional args as needed */
1336 {
1337 char v[4096]; /* Formatting string value */
1338 va_list ap; /* Argument pointer */
1339 char *olds; /* Old string */
1340
1341
1342 if (!s)
1343 return;
1344
1345 olds = *s;
1346
1347 if (f)
1348 {
1349 va_start(ap, f);
1350 vsnprintf(v, sizeof(v), f, ap);
1351 va_end(ap);
1352
1353 *s = strdup(v);
1354 }
1355 else
1356 *s = NULL;
1357
1358 if (olds)
1359 free(olds);
1360 }
1361
1362
1363 #ifdef HAVE_LAUNCHD
1364 /*
1365 * 'launchd_checkin()' - Check-in with launchd and collect the listening fds.
1366 */
1367
1368 static void
1369 launchd_checkin(void)
1370 {
1371 int i, /* Looping var */
1372 count, /* Numebr of listeners */
1373 portnum; /* Port number */
1374 launch_data_t ld_msg, /* Launch data message */
1375 ld_resp, /* Launch data response */
1376 ld_array, /* Launch data array */
1377 ld_sockets, /* Launch data sockets dictionary */
1378 ld_runatload, /* Run-at-load setting */
1379 tmp; /* Launch data */
1380 cupsd_listener_t *lis; /* Listeners array */
1381 http_addr_t addr; /* Address variable */
1382 socklen_t addrlen; /* Length of address */
1383 bool runatload; /* Run-at-load setting value */
1384
1385
1386 cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: pid=%d", (int)getpid());
1387
1388 /*
1389 * Check-in with launchd...
1390 */
1391
1392 ld_msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);
1393 if ((ld_resp = launch_msg(ld_msg)) == NULL)
1394 {
1395 cupsdLogMessage(CUPSD_LOG_ERROR,
1396 "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN
1397 "\") IPC failure");
1398 exit(EXIT_FAILURE);
1399 }
1400
1401 if (launch_data_get_type(ld_resp) == LAUNCH_DATA_ERRNO)
1402 {
1403 errno = launch_data_get_errno(ld_resp);
1404 cupsdLogMessage(CUPSD_LOG_ERROR, "launchd_checkin: Check-in failed: %s",
1405 strerror(errno));
1406 exit(EXIT_FAILURE);
1407 }
1408
1409 /*
1410 * Get the "run-at-load" setting...
1411 */
1412
1413 if ((ld_runatload =
1414 launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_RUNATLOAD)) != NULL &&
1415 launch_data_get_type(ld_runatload) == LAUNCH_DATA_BOOL)
1416 runatload = launch_data_get_bool(ld_runatload);
1417 else
1418 {
1419 errno = launch_data_get_errno(ld_resp);
1420 cupsdLogMessage(CUPSD_LOG_ERROR,
1421 "launchd_checkin: Unable to find Run-at-load setting: %s",
1422 strerror(errno));
1423 exit(EXIT_FAILURE);
1424 }
1425
1426 cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: Run-at-load=%s",
1427 runatload ? "true" : "false");
1428
1429 /*
1430 * Get the sockets dictionary...
1431 */
1432
1433 if (!(ld_sockets = launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_SOCKETS)))
1434 {
1435 cupsdLogMessage(CUPSD_LOG_ERROR,
1436 "launchd_checkin: No sockets found to answer requests on!");
1437 exit(EXIT_FAILURE);
1438 }
1439
1440 /*
1441 * Get the array of listener sockets...
1442 */
1443
1444 if (!(ld_array = launch_data_dict_lookup(ld_sockets, "Listeners")))
1445 {
1446 cupsdLogMessage(CUPSD_LOG_ERROR,
1447 "launchd_checkin: No sockets found to answer requests on!");
1448 exit(EXIT_FAILURE);
1449 }
1450
1451 /*
1452 * Add listening fd(s) to the Listener array...
1453 */
1454
1455 if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY)
1456 {
1457 /*
1458 * Free the listeners array built from cupsd.conf...
1459 */
1460
1461 cupsdDeleteAllListeners();
1462
1463 /*
1464 * Create a new array of listeners from the launchd data...
1465 */
1466
1467 Listeners = cupsArrayNew(NULL, NULL);
1468 count = launch_data_array_get_count(ld_array);
1469
1470 for (i = 0; i < count; i ++)
1471 {
1472 /*
1473 * Copy the current address and log it...
1474 */
1475
1476 if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL)
1477 {
1478 cupsdLogMessage(CUPSD_LOG_ERROR,
1479 "launchd_checkin: Unable to allocate listener - %s.",
1480 strerror(errno));
1481 exit(EXIT_FAILURE);
1482 }
1483
1484 cupsArrayAdd(Listeners, lis);
1485
1486 tmp = launch_data_array_get_index(ld_array, i);
1487 lis->fd = launch_data_get_fd(tmp);
1488 addrlen = sizeof(lis->address);
1489
1490 if (getsockname(lis->fd, (struct sockaddr *)&(lis->address), &addrlen))
1491 {
1492 cupsdLogMessage(CUPSD_LOG_ERROR,
1493 "launchd_checkin: Unable to get local address - %s",
1494 strerror(errno));
1495 }
1496
1497 # ifdef HAVE_SSL
1498 portnum = 0;
1499
1500 # ifdef AF_INET6
1501 if (addr.addr.sa_family == AF_INET6)
1502 portnum = ntohs(addr.ipv6.sin6_port);
1503 else
1504 # endif /* AF_INET6 */
1505 if (addr.addr.sa_family == AF_INET)
1506 portnum = ntohs(addr.ipv4.sin_port);
1507
1508 if (portnum == 443)
1509 lis->encryption = HTTP_ENCRYPT_ALWAYS;
1510 # endif /* HAVE_SSL */
1511 }
1512 }
1513
1514 /*
1515 * Collect the browse socket (if there is one)...
1516 */
1517
1518 if ((ld_array = launch_data_dict_lookup(ld_sockets, "BrowseSockets")))
1519 {
1520 if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY)
1521 {
1522 tmp = launch_data_array_get_index(ld_array, 0);
1523
1524 if (launch_data_get_type(tmp) == LAUNCH_DATA_FD)
1525 {
1526 if (BrowseSocket != -1)
1527 close(BrowseSocket);
1528
1529 BrowseSocket = launch_data_get_fd(tmp);
1530 }
1531 else
1532 cupsdLogMessage(CUPSD_LOG_WARN,
1533 "launchd_checkin: BrowseSocket not a fd!");
1534 }
1535 else
1536 cupsdLogMessage(CUPSD_LOG_WARN,
1537 "launchd_checkin: BrowseSockets is not an array!");
1538 }
1539 else
1540 cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: No BrowseSockets");
1541
1542 launch_data_free(ld_msg);
1543 launch_data_free(ld_resp);
1544 }
1545
1546
1547 /*
1548 * 'launchd_reload()' - Tell launchd to reload the configuration file to pick
1549 * up the new listening directives.
1550 */
1551
1552 static void
1553 launchd_reload(void)
1554 {
1555 int child_status; /* Exit status of child process */
1556 pid_t child_pid, /* Child PID */
1557 waitpid_status; /* Child process exit status */
1558 char *argv[4]; /* Argument strings */
1559
1560
1561 /*
1562 * The current launchd doesn't support a reload option (rdar://3854821).
1563 * Until this is fixed we need to reload the config file by execing launchctl
1564 * twice (to unload then load). NOTE: This will cause us to exit on SIGTERM
1565 * which will cancel all client & job activity.
1566 *
1567 * After this is fixed we'll be able to tell launchd to reload the file
1568 * and pick up the new listening descriptors without disrupting current
1569 * activity.
1570 */
1571
1572 /*
1573 * Unloading the current configuration will cause launchd to send us a SIGTERM;
1574 * block it for now so we can get our work done...
1575 */
1576
1577 cupsdHoldSignals();
1578
1579 /*
1580 * Set up the unload arguments to launchctl...
1581 */
1582
1583 argv[0] = "/bin/launchctl";
1584 argv[1] = "unload";
1585 argv[2] = LaunchdConf;
1586 argv[3] = NULL;
1587
1588 if (cupsdStartProcess(argv[0], argv, NULL, -1, -1, -1, -1, 1, &child_pid) < 0)
1589 cupsdLogMessage(CUPSD_LOG_ERROR,
1590 "launchd_reload: Unable to execute %s - %s", argv[0],
1591 strerror(errno));
1592 else
1593 {
1594 do
1595 {
1596 waitpid_status = waitpid(child_pid, &child_status, 0);
1597 }
1598 while (waitpid_status == (pid_t)-1 && errno == EINTR);
1599
1600 if (WIFSIGNALED(child_status))
1601 cupsdLogMessage(CUPSD_LOG_DEBUG,
1602 "launchd_reload: %s pid %d crashed on signal %d!",
1603 basename(argv[0]), child_pid, WTERMSIG(child_status));
1604 else
1605 cupsdLogMessage(CUPSD_LOG_DEBUG,
1606 "launchd_reload: %s pid %d stopped with status %d!",
1607 basename(argv[0]), child_pid, WEXITSTATUS(child_status));
1608
1609 /*
1610 * Do it again with the load command...
1611 */
1612
1613 argv[1] = "load";
1614
1615 if (cupsdStartProcess(argv[0], argv, NULL, -1, -1, -1, -1, 1,
1616 &child_pid) < 0)
1617 {
1618 cupsdLogMessage(CUPSD_LOG_ERROR,
1619 "launchd_reload: Unable to fork for %s - %s", argv[0],
1620 strerror(errno));
1621 }
1622 else
1623 {
1624 do
1625 {
1626 waitpid_status = waitpid(child_pid, &child_status, 0);
1627 } while (waitpid_status == (pid_t)-1 && errno == EINTR);
1628
1629 if (WIFSIGNALED(child_status))
1630 cupsdLogMessage(CUPSD_LOG_DEBUG,
1631 "launchd_reload: %s pid %d crashed on signal %d!",
1632 basename(argv[0]), child_pid, WTERMSIG(child_status));
1633 else
1634 cupsdLogMessage(CUPSD_LOG_DEBUG,
1635 "launchd_reload: %s pid %d stopped with status %d",
1636 basename(argv[0]), child_pid,
1637 WEXITSTATUS(child_status));
1638 }
1639 }
1640
1641 /*
1642 * Leave signals blocked since exit() will be called momentarily anyways...
1643 */
1644 }
1645
1646
1647 /*
1648 * 'launchd_sync_conf()' - Re-write the launchd(8) config file
1649 * org.cups.cupsd.plist based on cupsd.conf.
1650 */
1651
1652 static int /* O - 1 if the file was updated */
1653 launchd_sync_conf(void)
1654 {
1655 int portnum; /* Port number */
1656 CFMutableDictionaryRef cupsd_dict, /* org.cups.cupsd.plist dictionary */
1657 sockets, /* Sockets dictionary */
1658 listener; /* Listener dictionary */
1659 CFDataRef resourceData; /* XML property list */
1660 CFMutableArrayRef array; /* Array */
1661 CFNumberRef socket_mode; /* Domain socket mode bits */
1662 CFStringRef socket_path; /* Domain socket path */
1663 CFTypeRef value; /* CF value */
1664 CFURLRef fileURL; /* File URL */
1665 SInt32 errorCode; /* Error code */
1666 cupsd_listener_t *lis; /* Current listening socket */
1667 struct servent *service; /* Services data base entry */
1668 char temp[1024]; /* Temporary buffer for value */
1669 struct stat cupsd_sb, /* File info for cupsd.conf */
1670 launchd_sb; /* File info for org.cups.cupsd.plist */
1671
1672
1673 /*
1674 * If the launchd conf file modification time is newer than the cupsd.conf
1675 * time then there's nothing to do...
1676 */
1677
1678 if (!stat(ConfigurationFile, &cupsd_sb) &&
1679 !stat(LaunchdConf, &launchd_sb) &&
1680 launchd_sb.st_mtimespec.tv_sec >= cupsd_sb.st_mtimespec.tv_sec)
1681 {
1682 cupsdLogMessage(CUPSD_LOG_DEBUG,
1683 "launchd_sync_conf: Nothing to do, pid=%d.",
1684 (int)getpid());
1685 return (0);
1686 }
1687
1688 /*
1689 * Time to write a new 'org.cups.cupsd.plist' file.
1690 * Create the new dictionary and populate it with values...
1691 */
1692
1693 if ((cupsd_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
1694 &kCFTypeDictionaryKeyCallBacks,
1695 &kCFTypeDictionaryValueCallBacks)) != NULL)
1696 {
1697 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_LABEL),
1698 CFSTR("org.cups.cupsd"));
1699 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_ONDEMAND),
1700 kCFBooleanTrue);
1701
1702 if ((Browsing && BrowseLocalProtocols && cupsArrayCount(Printers)) ||
1703 cupsArrayCount(ActiveJobs))
1704 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_RUNATLOAD),
1705 kCFBooleanTrue);
1706 else
1707 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_RUNATLOAD),
1708 kCFBooleanFalse);
1709
1710 #ifdef LAUNCH_JOBKEY_SERVICEIPC
1711 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_SERVICEIPC),
1712 kCFBooleanTrue);
1713 #endif /* LAUNCH_JOBKEY_SERVICEIPC */
1714
1715 if ((array = CFArrayCreateMutable(kCFAllocatorDefault, 2,
1716 &kCFTypeArrayCallBacks)) != NULL)
1717 {
1718 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_PROGRAMARGUMENTS),
1719 array);
1720 CFArrayAppendValue(array, CFSTR("/usr/sbin/cupsd"));
1721 CFArrayAppendValue(array, CFSTR("-l"));
1722 CFRelease(array);
1723 }
1724
1725 /*
1726 * Add a sockets dictionary...
1727 */
1728
1729 if ((sockets = (CFMutableDictionaryRef)CFDictionaryCreateMutable(
1730 kCFAllocatorDefault, 0,
1731 &kCFTypeDictionaryKeyCallBacks,
1732 &kCFTypeDictionaryValueCallBacks)) != NULL)
1733 {
1734 CFDictionaryAddValue(cupsd_dict, CFSTR(LAUNCH_JOBKEY_SOCKETS), sockets);
1735
1736 /*
1737 * Add a Listeners array to the sockets dictionary...
1738 */
1739
1740 if ((array = CFArrayCreateMutable(kCFAllocatorDefault, 0,
1741 &kCFTypeArrayCallBacks)) != NULL)
1742 {
1743 CFDictionaryAddValue(sockets, CFSTR("Listeners"), array);
1744
1745 /*
1746 * For each listener add a dictionary to the listeners array...
1747 */
1748
1749 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
1750 lis;
1751 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
1752 {
1753 if ((listener = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
1754 &kCFTypeDictionaryKeyCallBacks,
1755 &kCFTypeDictionaryValueCallBacks)) != NULL)
1756 {
1757 CFArrayAppendValue(array, listener);
1758
1759 # ifdef AF_LOCAL
1760 if (lis->address.addr.sa_family == AF_LOCAL)
1761 {
1762 if ((socket_path = CFStringCreateWithCString(kCFAllocatorDefault,
1763 lis->address.un.sun_path,
1764 kCFStringEncodingUTF8)))
1765 {
1766 CFDictionaryAddValue(listener,
1767 CFSTR(LAUNCH_JOBSOCKETKEY_PATHNAME),
1768 socket_path);
1769 CFRelease(socket_path);
1770 }
1771 portnum = 0140777; /* (S_IFSOCK|S_IRWXU|S_IRWXG|S_IRWXO) or *
1772 * 49663d decimal */
1773 if ((socket_mode = CFNumberCreate(kCFAllocatorDefault,
1774 kCFNumberIntType, &portnum)))
1775 {
1776 CFDictionaryAddValue(listener, CFSTR("SockPathMode"),
1777 socket_mode);
1778 CFRelease(socket_mode);
1779 }
1780 }
1781 else
1782 # endif /* AF_LOCAL */
1783 {
1784 # ifdef AF_INET6
1785 if (lis->address.addr.sa_family == AF_INET6)
1786 {
1787 CFDictionaryAddValue(listener,
1788 CFSTR(LAUNCH_JOBSOCKETKEY_FAMILY),
1789 CFSTR("IPv6"));
1790 portnum = lis->address.ipv6.sin6_port;
1791 }
1792 else
1793 # endif /* AF_INET6 */
1794 {
1795 CFDictionaryAddValue(listener,
1796 CFSTR(LAUNCH_JOBSOCKETKEY_FAMILY),
1797 CFSTR("IPv4"));
1798 portnum = lis->address.ipv4.sin_port;
1799 }
1800
1801 if ((service = getservbyport(portnum, NULL)))
1802 value = CFStringCreateWithCString(kCFAllocatorDefault,
1803 service->s_name,
1804 kCFStringEncodingUTF8);
1805 else
1806 value = CFNumberCreate(kCFAllocatorDefault,
1807 kCFNumberIntType, &portnum);
1808
1809 if (value)
1810 {
1811 CFDictionaryAddValue(listener,
1812 CFSTR(LAUNCH_JOBSOCKETKEY_SERVICENAME),
1813 value);
1814 CFRelease(value);
1815 }
1816
1817 httpAddrString(&lis->address, temp, sizeof(temp));
1818 if ((value = CFStringCreateWithCString(kCFAllocatorDefault, temp,
1819 kCFStringEncodingUTF8)))
1820 {
1821 CFDictionaryAddValue(listener,
1822 CFSTR(LAUNCH_JOBSOCKETKEY_NODENAME),
1823 value);
1824 CFRelease(value);
1825 }
1826 }
1827
1828 CFRelease(listener);
1829 }
1830 }
1831
1832 CFRelease(array);
1833 }
1834
1835 /*
1836 * Add the BrowseSocket to the sockets dictionary...
1837 */
1838
1839 if (Browsing && (BrowseRemoteProtocols & BROWSE_CUPS))
1840 {
1841 if ((array = CFArrayCreateMutable(kCFAllocatorDefault, 0,
1842 &kCFTypeArrayCallBacks)) != NULL)
1843 {
1844 CFDictionaryAddValue(sockets, CFSTR("BrowseSockets"), array);
1845
1846 if ((listener = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
1847 &kCFTypeDictionaryKeyCallBacks,
1848 &kCFTypeDictionaryValueCallBacks)) != NULL)
1849 {
1850 CFArrayAppendValue(array, listener);
1851
1852 CFDictionaryAddValue(listener, CFSTR(LAUNCH_JOBSOCKETKEY_FAMILY),
1853 CFSTR("IPv4"));
1854 CFDictionaryAddValue(listener, CFSTR(LAUNCH_JOBSOCKETKEY_TYPE),
1855 CFSTR("dgram"));
1856
1857 if ((service = getservbyport(BrowsePort, NULL)))
1858 value = CFStringCreateWithCString(kCFAllocatorDefault,
1859 service->s_name,
1860 kCFStringEncodingUTF8);
1861 else
1862 value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType,
1863 &BrowsePort);
1864
1865 CFDictionaryAddValue(listener,
1866 CFSTR(LAUNCH_JOBSOCKETKEY_SERVICENAME), value);
1867 CFRelease(value);
1868
1869 CFRelease(listener);
1870 }
1871
1872 CFRelease(array);
1873 }
1874 }
1875
1876 CFRelease(sockets);
1877 }
1878
1879 cupsdLogMessage(CUPSD_LOG_DEBUG,
1880 "launchd_sync_conf: Updating \"%s\", pid=%d\n",
1881 LaunchdConf, (int)getpid());
1882
1883 if ((fileURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
1884 (const unsigned char *)LaunchdConf,
1885 strlen(LaunchdConf), false)))
1886 {
1887 if ((resourceData = CFPropertyListCreateXMLData(kCFAllocatorDefault,
1888 cupsd_dict)))
1889 {
1890 if (!CFURLWriteDataAndPropertiesToResource(fileURL, resourceData,
1891 NULL, &errorCode))
1892 {
1893 cupsdLogMessage(CUPSD_LOG_WARN,
1894 "launchd_sync_conf: "
1895 "CFURLWriteDataAndPropertiesToResource(\"%s\") "
1896 "failed: %d\n",
1897 LaunchdConf, (int)errorCode);
1898 }
1899
1900 CFRelease(resourceData);
1901 }
1902
1903 CFRelease(fileURL);
1904 }
1905
1906 CFRelease(cupsd_dict);
1907 }
1908
1909 /*
1910 * Let the caller know we updated the file...
1911 */
1912
1913 return (1);
1914 }
1915 #endif /* HAVE_LAUNCHD */
1916
1917
1918 /*
1919 * 'parent_handler()' - Catch USR1/CHLD signals...
1920 */
1921
1922 static void
1923 parent_handler(int sig) /* I - Signal */
1924 {
1925 /*
1926 * Store the signal we got from the OS and return...
1927 */
1928
1929 parent_signal = sig;
1930 }
1931
1932
1933 /*
1934 * 'process_children()' - Process all dead children...
1935 */
1936
1937 static void
1938 process_children(void)
1939 {
1940 int status; /* Exit status of child */
1941 int pid; /* Process ID of child */
1942 cupsd_job_t *job; /* Current job */
1943 int i; /* Looping var */
1944 char name[1024]; /* Process name */
1945
1946
1947 cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_children()");
1948
1949 /*
1950 * Reset the dead_children flag...
1951 */
1952
1953 dead_children = 0;
1954
1955 /*
1956 * Collect the exit status of some children...
1957 */
1958
1959 #ifdef HAVE_WAITPID
1960 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1961 #elif defined(HAVE_WAIT3)
1962 while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
1963 #else
1964 if ((pid = wait(&status)) > 0)
1965 #endif /* HAVE_WAITPID */
1966 {
1967 /*
1968 * Ignore SIGTERM errors - that comes when a job is cancelled...
1969 */
1970
1971 cupsdFinishProcess(pid, name, sizeof(name));
1972
1973 if (status == SIGTERM)
1974 status = 0;
1975
1976 if (status)
1977 {
1978 if (WIFEXITED(status))
1979 cupsdLogMessage(CUPSD_LOG_ERROR, "PID %d (%s) stopped with status %d!",
1980 pid, name, WEXITSTATUS(status));
1981 else
1982 cupsdLogMessage(CUPSD_LOG_ERROR, "PID %d (%s) crashed on signal %d!",
1983 pid, name, WTERMSIG(status));
1984
1985 if (LogLevel < CUPSD_LOG_DEBUG)
1986 cupsdLogMessage(CUPSD_LOG_INFO,
1987 "Hint: Try setting the LogLevel to \"debug\" to find "
1988 "out more.");
1989 }
1990 else
1991 cupsdLogMessage(CUPSD_LOG_DEBUG, "PID %d (%s) exited with no errors.",
1992 pid, name);
1993
1994 /*
1995 * Delete certificates for CGI processes...
1996 */
1997
1998 if (pid)
1999 cupsdDeleteCert(pid);
2000
2001 /*
2002 * Lookup the PID in the jobs list...
2003 */
2004
2005 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
2006 job;
2007 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
2008 if (job->state_value == IPP_JOB_PROCESSING)
2009 {
2010 for (i = 0; job->filters[i]; i ++)
2011 if (job->filters[i] == pid)
2012 break;
2013
2014 if (job->filters[i] || job->backend == pid)
2015 {
2016 /*
2017 * OK, this process has gone away; what's left?
2018 */
2019
2020 if (job->filters[i])
2021 job->filters[i] = -pid;
2022 else
2023 job->backend = -pid;
2024
2025 if (status && job->status >= 0)
2026 {
2027 /*
2028 * An error occurred; save the exit status so we know to stop
2029 * the printer or cancel the job when all of the filters finish...
2030 *
2031 * A negative status indicates that the backend failed and the
2032 * printer needs to be stopped.
2033 */
2034
2035 if (job->filters[i])
2036 job->status = status; /* Filter failed */
2037 else
2038 job->status = -status; /* Backend failed */
2039
2040 if (job->printer && !(job->printer->type & CUPS_PRINTER_FAX))
2041 {
2042 snprintf(job->printer->state_message,
2043 sizeof(job->printer->state_message), "%s failed", name);
2044 cupsdAddPrinterHistory(job->printer);
2045 }
2046 }
2047
2048 /*
2049 * If this is not the last file in a job, see if all of the
2050 * filters are done, and if so move to the next file.
2051 */
2052
2053 if (job->current_file < job->num_files)
2054 {
2055 for (i = 0; job->filters[i] < 0; i ++);
2056
2057 if (!job->filters[i])
2058 {
2059 /*
2060 * Process the next file...
2061 */
2062
2063 cupsdFinishJob(job);
2064 }
2065 }
2066 break;
2067 }
2068 }
2069 }
2070 }
2071
2072
2073 /*
2074 * 'sigchld_handler()' - Handle 'child' signals from old processes.
2075 */
2076
2077 static void
2078 sigchld_handler(int sig) /* I - Signal number */
2079 {
2080 (void)sig;
2081
2082 /*
2083 * Flag that we have dead children...
2084 */
2085
2086 dead_children = 1;
2087
2088 /*
2089 * Reset the signal handler as needed...
2090 */
2091
2092 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2093 signal(SIGCLD, sigchld_handler);
2094 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2095 }
2096
2097
2098 /*
2099 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
2100 */
2101
2102 static void
2103 sighup_handler(int sig) /* I - Signal number */
2104 {
2105 (void)sig;
2106
2107 NeedReload = RELOAD_ALL;
2108 ReloadTime = time(NULL);
2109
2110 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
2111 signal(SIGHUP, sighup_handler);
2112 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
2113 }
2114
2115
2116 /*
2117 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
2118 */
2119
2120 static void
2121 sigterm_handler(int sig) /* I - Signal */
2122 {
2123 (void)sig; /* remove compiler warnings... */
2124
2125 /*
2126 * Flag that we should stop and return...
2127 */
2128
2129 stop_scheduler = 1;
2130 }
2131
2132
2133 /*
2134 * 'select_timeout()' - Calculate the select timeout value.
2135 *
2136 */
2137
2138 static long /* O - Number of seconds */
2139 select_timeout(int fds) /* I - Number of descriptors returned */
2140 {
2141 long timeout; /* Timeout for select */
2142 time_t now; /* Current time */
2143 cupsd_client_t *con; /* Client information */
2144 cupsd_printer_t *p; /* Printer information */
2145 cupsd_job_t *job; /* Job information */
2146 cupsd_subscription_t *sub; /* Subscription information */
2147 const char *why; /* Debugging aid */
2148
2149
2150 /*
2151 * Check to see if any of the clients have pending data to be
2152 * processed; if so, the timeout should be 0...
2153 */
2154
2155 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
2156 con;
2157 con = (cupsd_client_t *)cupsArrayNext(Clients))
2158 if (con->http.used > 0)
2159 return (0);
2160
2161 /*
2162 * If select has been active in the last second (fds != 0) or we have
2163 * many resources in use then don't bother trying to optimize the
2164 * timeout, just make it 1 second.
2165 */
2166
2167 if (fds || cupsArrayCount(Clients) > 50)
2168 return (1);
2169
2170 /*
2171 * If we had a recent event notification, timeout in 1 second...
2172 */
2173
2174 if (LastEvent)
2175 return (1);
2176
2177 /*
2178 * Otherwise, check all of the possible events that we need to wake for...
2179 */
2180
2181 now = time(NULL);
2182 timeout = now + 86400; /* 86400 == 1 day */
2183 why = "do nothing";
2184
2185 /*
2186 * Check the activity and close old clients...
2187 */
2188
2189 for (con = (cupsd_client_t *)cupsArrayFirst(Clients);
2190 con;
2191 con = (cupsd_client_t *)cupsArrayNext(Clients))
2192 if ((con->http.activity + Timeout) < timeout)
2193 {
2194 timeout = con->http.activity + Timeout;
2195 why = "timeout a client connection";
2196 }
2197
2198 /*
2199 * Update the browse list as needed...
2200 */
2201
2202 if (Browsing && BrowseLocalProtocols)
2203 {
2204 #ifdef HAVE_LIBSLP
2205 if ((BrowseLocalProtocols & BROWSE_SLP) && (BrowseSLPRefresh < timeout))
2206 {
2207 timeout = BrowseSLPRefresh;
2208 why = "update SLP browsing";
2209 }
2210 #endif /* HAVE_LIBSLP */
2211
2212 if (BrowseLocalProtocols & BROWSE_CUPS)
2213 {
2214 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2215 p;
2216 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2217 {
2218 if (p->type & CUPS_PRINTER_REMOTE)
2219 {
2220 if ((p->browse_time + BrowseTimeout) < timeout)
2221 {
2222 timeout = p->browse_time + BrowseTimeout;
2223 why = "browse timeout a printer";
2224 }
2225 }
2226 else if (!(p->type & CUPS_PRINTER_IMPLICIT))
2227 {
2228 if (BrowseInterval && (p->browse_time + BrowseInterval) < timeout)
2229 {
2230 timeout = p->browse_time + BrowseInterval;
2231 why = "send browse update";
2232 }
2233 }
2234 }
2235 }
2236 }
2237
2238 /*
2239 * Check for any active jobs...
2240 */
2241
2242 if (timeout > (now + 10) && ActiveJobs)
2243 {
2244 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
2245 job;
2246 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
2247 if (job->state_value <= IPP_JOB_PROCESSING)
2248 {
2249 timeout = now + 10;
2250 why = "process active jobs";
2251 break;
2252 }
2253 }
2254
2255 #ifdef HAVE_MALLINFO
2256 /*
2257 * Log memory usage every minute...
2258 */
2259
2260 if (LogLevel >= CUPSD_LOG_DEBUG && (mallinfo_time + 60) < timeout)
2261 {
2262 timeout = mallinfo_time + 60;
2263 why = "display memory usage";
2264 }
2265 #endif /* HAVE_MALLINFO */
2266
2267 /*
2268 * Expire subscriptions as needed...
2269 */
2270
2271 for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
2272 sub;
2273 sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
2274 if (!sub->job && sub->expire && sub->expire < timeout)
2275 {
2276 timeout = sub->expire;
2277 why = "expire subscription";
2278 }
2279
2280 /*
2281 * Adjust from absolute to relative time. If p->browse_time above
2282 * was 0 then we can end up with a negative value here, so check.
2283 * We add 1 second to the timeout since events occur after the
2284 * timeout expires, and limit the timeout to 86400 seconds (1 day)
2285 * to avoid select() timeout limits present on some operating
2286 * systems...
2287 */
2288
2289 timeout = timeout - now + 1;
2290
2291 if (timeout < 1)
2292 timeout = 1;
2293 else if (timeout > 86400)
2294 timeout = 86400;
2295
2296 /*
2297 * Log and return the timeout value...
2298 */
2299
2300 cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: %ld seconds to %s",
2301 timeout, why);
2302
2303 return (timeout);
2304 }
2305
2306
2307 /*
2308 * 'usage()' - Show scheduler usage.
2309 */
2310
2311 static void
2312 usage(int status) /* O - Exit status */
2313 {
2314 _cupsLangPuts(status ? stderr : stdout,
2315 _("Usage: cupsd [-c config-file] [-f] [-F] [-h] [-l]\n"
2316 "\n"
2317 "-c config-file Load alternate configuration file\n"
2318 "-f Run in the foreground\n"
2319 "-F Run in the foreground but detach\n"
2320 "-h Show this usage message\n"
2321 "-l Run cupsd from launchd(8)\n"));
2322 exit(status);
2323 }
2324
2325
2326 /*
2327 * End of "$Id: main.c 5108 2006-02-15 19:33:09Z mike $".
2328 */