]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Main loop for the CUPS scheduler. | |
3 | * | |
4 | * Copyright 2007-2017 by Apple Inc. | |
5 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. | |
6 | * | |
7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more | |
8 | * information. | |
9 | */ | |
10 | ||
11 | /* | |
12 | * Include necessary headers... | |
13 | */ | |
14 | ||
15 | #define _MAIN_C_ | |
16 | #include "cupsd.h" | |
17 | #include <sys/resource.h> | |
18 | #ifdef HAVE_ASL_H | |
19 | # include <asl.h> | |
20 | #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) | |
21 | # define SD_JOURNAL_SUPPRESS_LOCATION | |
22 | # include <systemd/sd-journal.h> | |
23 | #endif /* HAVE_ASL_H */ | |
24 | #include <syslog.h> | |
25 | #include <grp.h> | |
26 | ||
27 | #ifdef HAVE_LAUNCH_H | |
28 | # include <launch.h> | |
29 | #endif /* HAVE_LAUNCH_H */ | |
30 | ||
31 | #ifdef HAVE_SYSTEMD | |
32 | # include <systemd/sd-daemon.h> | |
33 | #endif /* HAVE_SYSTEMD */ | |
34 | ||
35 | #ifdef HAVE_ONDEMAND | |
36 | # define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd" | |
37 | /* Name of the KeepAlive file */ | |
38 | #endif /* HAVE_ONDEMAND */ | |
39 | ||
40 | #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO) | |
41 | # include <malloc.h> | |
42 | #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */ | |
43 | ||
44 | #ifdef HAVE_NOTIFY_H | |
45 | # include <notify.h> | |
46 | #endif /* HAVE_NOTIFY_H */ | |
47 | ||
48 | #ifdef HAVE_DBUS | |
49 | # include <dbus/dbus.h> | |
50 | #endif /* HAVE_DBUS */ | |
51 | ||
52 | #ifdef HAVE_SYS_PARAM_H | |
53 | # include <sys/param.h> | |
54 | #endif /* HAVE_SYS_PARAM_H */ | |
55 | ||
56 | ||
57 | /* | |
58 | * Local functions... | |
59 | */ | |
60 | ||
61 | static void parent_handler(int sig); | |
62 | static void process_children(void); | |
63 | static void sigchld_handler(int sig); | |
64 | static void sighup_handler(int sig); | |
65 | static void sigterm_handler(int sig); | |
66 | static long select_timeout(int fds); | |
67 | static void service_checkin(void); | |
68 | static void service_checkout(int shutdown); | |
69 | static void usage(int status) __attribute__((noreturn)); | |
70 | ||
71 | ||
72 | /* | |
73 | * Local globals... | |
74 | */ | |
75 | ||
76 | static int parent_signal = 0; | |
77 | /* Set to signal number from child */ | |
78 | static int holdcount = 0; /* Number of times "hold" was called */ | |
79 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
80 | static sigset_t holdmask; /* Old POSIX signal mask */ | |
81 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
82 | static int dead_children = 0; | |
83 | /* Dead children? */ | |
84 | static int stop_scheduler = 0; | |
85 | /* Should the scheduler stop? */ | |
86 | static time_t local_timeout = 0; | |
87 | /* Next local printer timeout */ | |
88 | ||
89 | ||
90 | /* | |
91 | * 'main()' - Main entry for the CUPS scheduler. | |
92 | */ | |
93 | ||
94 | int /* O - Exit status */ | |
95 | main(int argc, /* I - Number of command-line args */ | |
96 | char *argv[]) /* I - Command-line arguments */ | |
97 | { | |
98 | int i; /* Looping var */ | |
99 | char *opt; /* Option character */ | |
100 | int close_all = 1, /* Close all file descriptors? */ | |
101 | disconnect = 1, /* Disconnect from controlling terminal? */ | |
102 | fg = 0, /* Run in foreground? */ | |
103 | run_as_child = 0, | |
104 | /* Running as child process? */ | |
105 | print_profile = 0; | |
106 | /* Print the sandbox profile to stdout? */ | |
107 | int fds; /* Number of ready descriptors */ | |
108 | cupsd_client_t *con; /* Current client */ | |
109 | cupsd_job_t *job; /* Current job */ | |
110 | cupsd_listener_t *lis; /* Current listener */ | |
111 | time_t current_time, /* Current time */ | |
112 | activity, /* Client activity timer */ | |
113 | senddoc_time, /* Send-Document time */ | |
114 | expire_time, /* Subscription expire time */ | |
115 | report_time, /* Malloc/client/job report time */ | |
116 | event_time; /* Last event notification time */ | |
117 | long timeout; /* Timeout for cupsdDoSelect() */ | |
118 | struct rlimit limit; /* Runtime limit */ | |
119 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
120 | struct sigaction action; /* Actions for POSIX signals */ | |
121 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
122 | #ifdef __APPLE__ | |
123 | int use_sysman = 1; /* Use system management functions? */ | |
124 | #else | |
125 | time_t netif_time = 0; /* Time since last network update */ | |
126 | #endif /* __APPLE__ */ | |
127 | #if defined(HAVE_ONDEMAND) | |
128 | int service_idle_exit; | |
129 | /* Idle exit on select timeout? */ | |
130 | #endif /* HAVE_ONDEMAND */ | |
131 | ||
132 | ||
133 | #ifdef HAVE_GETEUID | |
134 | /* | |
135 | * Check for setuid invocation, which we do not support! | |
136 | */ | |
137 | ||
138 | if (getuid() != geteuid()) | |
139 | { | |
140 | fputs("cupsd: Cannot run as a setuid program.\n", stderr); | |
141 | return (1); | |
142 | } | |
143 | #endif /* HAVE_GETEUID */ | |
144 | ||
145 | /* | |
146 | * Check for command-line arguments... | |
147 | */ | |
148 | ||
149 | fg = 0; | |
150 | ||
151 | #ifdef HAVE_LAUNCHD | |
152 | if (getenv("CUPSD_LAUNCHD")) | |
153 | { | |
154 | OnDemand = 1; | |
155 | fg = 1; | |
156 | close_all = 0; | |
157 | disconnect = 0; | |
158 | } | |
159 | #endif /* HAVE_LAUNCHD */ | |
160 | ||
161 | for (i = 1; i < argc; i ++) | |
162 | if (argv[i][0] == '-') | |
163 | for (opt = argv[i] + 1; *opt != '\0'; opt ++) | |
164 | switch (*opt) | |
165 | { | |
166 | case 'C' : /* Run as child with config file */ | |
167 | run_as_child = 1; | |
168 | fg = 1; | |
169 | close_all = 0; | |
170 | ||
171 | case 'c' : /* Configuration file */ | |
172 | i ++; | |
173 | if (i >= argc) | |
174 | { | |
175 | _cupsLangPuts(stderr, _("cupsd: Expected config filename " | |
176 | "after \"-c\" option.")); | |
177 | usage(1); | |
178 | } | |
179 | ||
180 | if (argv[i][0] == '/') | |
181 | { | |
182 | /* | |
183 | * Absolute directory... | |
184 | */ | |
185 | ||
186 | cupsdSetString(&ConfigurationFile, argv[i]); | |
187 | } | |
188 | else | |
189 | { | |
190 | /* | |
191 | * Relative directory... | |
192 | */ | |
193 | ||
194 | char *current; /* Current directory */ | |
195 | ||
196 | /* | |
197 | * Allocate a buffer for the current working directory to | |
198 | * reduce run-time stack usage; this approximates the | |
199 | * behavior of some implementations of getcwd() when they | |
200 | * are passed a NULL pointer. | |
201 | */ | |
202 | ||
203 | if ((current = malloc(1024)) == NULL) | |
204 | { | |
205 | _cupsLangPuts(stderr, | |
206 | _("cupsd: Unable to get current directory.")); | |
207 | return (1); | |
208 | } | |
209 | ||
210 | if (!getcwd(current, 1024)) | |
211 | { | |
212 | _cupsLangPuts(stderr, | |
213 | _("cupsd: Unable to get current directory.")); | |
214 | free(current); | |
215 | return (1); | |
216 | } | |
217 | ||
218 | cupsdSetStringf(&ConfigurationFile, "%s/%s", current, argv[i]); | |
219 | free(current); | |
220 | } | |
221 | break; | |
222 | ||
223 | case 'f' : /* Run in foreground... */ | |
224 | fg = 1; | |
225 | disconnect = 0; | |
226 | close_all = 0; | |
227 | break; | |
228 | ||
229 | case 'F' : /* Run in foreground, but disconnect from terminal... */ | |
230 | fg = 1; | |
231 | close_all = 0; | |
232 | break; | |
233 | ||
234 | case 'h' : /* Show usage/help */ | |
235 | usage(0); | |
236 | break; | |
237 | ||
238 | case 'l' : /* Started by launchd/systemd/upstart... */ | |
239 | #ifdef HAVE_ONDEMAND | |
240 | OnDemand = 1; | |
241 | fg = 1; | |
242 | close_all = 0; | |
243 | disconnect = 0; | |
244 | #else | |
245 | _cupsLangPuts(stderr, _("cupsd: On-demand support not compiled " | |
246 | "in, running in normal mode.")); | |
247 | fg = 0; | |
248 | disconnect = 1; | |
249 | close_all = 1; | |
250 | #endif /* HAVE_ONDEMAND */ | |
251 | break; | |
252 | ||
253 | case 'p' : /* Stop immediately for profiling */ | |
254 | fputs("cupsd: -p (startup profiling) is for internal testing " | |
255 | "use only!\n", stderr); | |
256 | stop_scheduler = 1; | |
257 | fg = 1; | |
258 | disconnect = 0; | |
259 | close_all = 0; | |
260 | break; | |
261 | ||
262 | case 'P' : /* Disable security profiles */ | |
263 | fputs("cupsd: -P (disable sandboxing) is for internal testing use only.\n", stderr); | |
264 | UseSandboxing = 0; | |
265 | break; | |
266 | ||
267 | case 's' : /* Set cups-files.conf location */ | |
268 | i ++; | |
269 | if (i >= argc) | |
270 | { | |
271 | _cupsLangPuts(stderr, _("cupsd: Expected cups-files.conf " | |
272 | "filename after \"-s\" option.")); | |
273 | usage(1); | |
274 | } | |
275 | ||
276 | if (argv[i][0] != '/') | |
277 | { | |
278 | /* | |
279 | * Relative filename not allowed... | |
280 | */ | |
281 | ||
282 | _cupsLangPuts(stderr, _("cupsd: Relative cups-files.conf " | |
283 | "filename not allowed.")); | |
284 | usage(1); | |
285 | } | |
286 | ||
287 | cupsdSetString(&CupsFilesFile, argv[i]); | |
288 | break; | |
289 | ||
290 | #ifdef __APPLE__ | |
291 | case 'S' : /* Disable system management functions */ | |
292 | fputs("cupsd: -S (disable system management) for internal " | |
293 | "testing use only!\n", stderr); | |
294 | use_sysman = 0; | |
295 | break; | |
296 | #endif /* __APPLE__ */ | |
297 | ||
298 | case 't' : /* Test the cupsd.conf file... */ | |
299 | TestConfigFile = 1; | |
300 | fg = 1; | |
301 | disconnect = 0; | |
302 | close_all = 0; | |
303 | break; | |
304 | ||
305 | case 'T' : /* Print security profile */ | |
306 | print_profile = 1; | |
307 | fg = 1; | |
308 | disconnect = 0; | |
309 | close_all = 0; | |
310 | break; | |
311 | ||
312 | default : /* Unknown option */ | |
313 | _cupsLangPrintf(stderr, _("cupsd: Unknown option \"%c\" - " | |
314 | "aborting."), *opt); | |
315 | usage(1); | |
316 | break; | |
317 | } | |
318 | else | |
319 | { | |
320 | _cupsLangPrintf(stderr, _("cupsd: Unknown argument \"%s\" - aborting."), | |
321 | argv[i]); | |
322 | usage(1); | |
323 | } | |
324 | ||
325 | if (!ConfigurationFile) | |
326 | cupsdSetString(&ConfigurationFile, CUPS_SERVERROOT "/cupsd.conf"); | |
327 | ||
328 | if (!CupsFilesFile) | |
329 | { | |
330 | char *filename, /* Copy of cupsd.conf filename */ | |
331 | *slash; /* Final slash in cupsd.conf filename */ | |
332 | size_t len; /* Size of buffer */ | |
333 | ||
334 | len = strlen(ConfigurationFile) + 15; | |
335 | if ((filename = malloc(len)) == NULL) | |
336 | { | |
337 | _cupsLangPrintf(stderr, | |
338 | _("cupsd: Unable to get path to " | |
339 | "cups-files.conf file.")); | |
340 | return (1); | |
341 | } | |
342 | ||
343 | strlcpy(filename, ConfigurationFile, len); | |
344 | if ((slash = strrchr(filename, '/')) == NULL) | |
345 | { | |
346 | _cupsLangPrintf(stderr, | |
347 | _("cupsd: Unable to get path to " | |
348 | "cups-files.conf file.")); | |
349 | return (1); | |
350 | } | |
351 | ||
352 | strlcpy(slash, "/cups-files.conf", len - (size_t)(slash - filename)); | |
353 | cupsdSetString(&CupsFilesFile, filename); | |
354 | free(filename); | |
355 | } | |
356 | ||
357 | if (disconnect) | |
358 | { | |
359 | /* | |
360 | * Make sure we aren't tying up any filesystems... | |
361 | */ | |
362 | ||
363 | chdir("/"); | |
364 | ||
365 | /* | |
366 | * Disconnect from the controlling terminal... | |
367 | */ | |
368 | ||
369 | setsid(); | |
370 | } | |
371 | ||
372 | if (close_all) | |
373 | { | |
374 | /* | |
375 | * Close all open files... | |
376 | */ | |
377 | ||
378 | getrlimit(RLIMIT_NOFILE, &limit); | |
379 | ||
380 | for (i = 0; i < (int)limit.rlim_cur && i < 1024; i ++) | |
381 | close(i); | |
382 | ||
383 | /* | |
384 | * Redirect stdin/out/err to /dev/null... | |
385 | */ | |
386 | ||
387 | if ((i = open("/dev/null", O_RDONLY)) != 0) | |
388 | { | |
389 | dup2(i, 0); | |
390 | close(i); | |
391 | } | |
392 | ||
393 | if ((i = open("/dev/null", O_WRONLY)) != 1) | |
394 | { | |
395 | dup2(i, 1); | |
396 | close(i); | |
397 | } | |
398 | ||
399 | if ((i = open("/dev/null", O_WRONLY)) != 2) | |
400 | { | |
401 | dup2(i, 2); | |
402 | close(i); | |
403 | } | |
404 | } | |
405 | else | |
406 | LogStderr = cupsFileStderr(); | |
407 | ||
408 | /* | |
409 | * Run in the background as needed... | |
410 | */ | |
411 | ||
412 | if (!fg) | |
413 | { | |
414 | /* | |
415 | * Setup signal handlers for the parent... | |
416 | */ | |
417 | ||
418 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
419 | sigset(SIGUSR1, parent_handler); | |
420 | sigset(SIGCHLD, parent_handler); | |
421 | ||
422 | sigset(SIGHUP, SIG_IGN); | |
423 | #elif defined(HAVE_SIGACTION) | |
424 | memset(&action, 0, sizeof(action)); | |
425 | sigemptyset(&action.sa_mask); | |
426 | sigaddset(&action.sa_mask, SIGUSR1); | |
427 | action.sa_handler = parent_handler; | |
428 | sigaction(SIGUSR1, &action, NULL); | |
429 | sigaction(SIGCHLD, &action, NULL); | |
430 | ||
431 | sigemptyset(&action.sa_mask); | |
432 | action.sa_handler = SIG_IGN; | |
433 | sigaction(SIGHUP, &action, NULL); | |
434 | #else | |
435 | signal(SIGUSR1, parent_handler); | |
436 | signal(SIGCLD, parent_handler); | |
437 | ||
438 | signal(SIGHUP, SIG_IGN); | |
439 | #endif /* HAVE_SIGSET */ | |
440 | ||
441 | if (fork() > 0) | |
442 | { | |
443 | /* | |
444 | * OK, wait for the child to startup and send us SIGUSR1 or to crash | |
445 | * and the OS send us SIGCHLD... We also need to ignore SIGHUP which | |
446 | * might be sent by the init script to restart the scheduler... | |
447 | */ | |
448 | ||
449 | for (; parent_signal == 0;) | |
450 | sleep(1); | |
451 | ||
452 | if (parent_signal == SIGUSR1) | |
453 | return (0); | |
454 | ||
455 | if (wait(&i) < 0) | |
456 | { | |
457 | perror("cupsd"); | |
458 | return (1); | |
459 | } | |
460 | else if (WIFEXITED(i)) | |
461 | { | |
462 | fprintf(stderr, "cupsd: Child exited with status %d\n", | |
463 | WEXITSTATUS(i)); | |
464 | return (2); | |
465 | } | |
466 | else | |
467 | { | |
468 | fprintf(stderr, "cupsd: Child exited on signal %d\n", WTERMSIG(i)); | |
469 | return (3); | |
470 | } | |
471 | } | |
472 | ||
473 | #if defined(__OpenBSD__) && OpenBSD < 201211 | |
474 | /* | |
475 | * Call _thread_sys_closefrom() so the child process doesn't reset the | |
476 | * parent's file descriptors to be blocking. This is a workaround for a | |
477 | * limitation of userland libpthread on older versions of OpenBSD. | |
478 | */ | |
479 | ||
480 | _thread_sys_closefrom(0); | |
481 | #endif /* __OpenBSD__ && OpenBSD < 201211 */ | |
482 | ||
483 | /* | |
484 | * Since many system libraries create fork-unsafe data on execution of a | |
485 | * program, we need to re-execute the background cupsd with the "-C" and "-s" | |
486 | * options to avoid problems. Unfortunately, we also have to assume that | |
487 | * argv[0] contains the name of the cupsd executable - there is no portable | |
488 | * way to get the real pathname... | |
489 | */ | |
490 | ||
491 | execlp(argv[0], argv[0], "-C", ConfigurationFile, "-s", CupsFilesFile, (char *)0); | |
492 | exit(errno); | |
493 | } | |
494 | ||
495 | /* | |
496 | * Let the system know we are busy while we bring up cupsd... | |
497 | */ | |
498 | ||
499 | cupsdSetBusyState(1); | |
500 | ||
501 | /* | |
502 | * Set the timezone info... | |
503 | */ | |
504 | ||
505 | tzset(); | |
506 | ||
507 | #ifdef LC_TIME | |
508 | setlocale(LC_TIME, ""); | |
509 | #endif /* LC_TIME */ | |
510 | ||
511 | #ifdef HAVE_DBUS_THREADS_INIT | |
512 | /* | |
513 | * Enable threading support for D-BUS... | |
514 | */ | |
515 | ||
516 | dbus_threads_init_default(); | |
517 | #endif /* HAVE_DBUS_THREADS_INIT */ | |
518 | ||
519 | /* | |
520 | * Set the maximum number of files... | |
521 | */ | |
522 | ||
523 | getrlimit(RLIMIT_NOFILE, &limit); | |
524 | ||
525 | #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE) | |
526 | if (limit.rlim_max > FD_SETSIZE) | |
527 | MaxFDs = FD_SETSIZE; | |
528 | else | |
529 | #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */ | |
530 | #ifdef RLIM_INFINITY | |
531 | if (limit.rlim_max == RLIM_INFINITY) | |
532 | MaxFDs = 16384; | |
533 | else | |
534 | #endif /* RLIM_INFINITY */ | |
535 | MaxFDs = limit.rlim_max; | |
536 | ||
537 | limit.rlim_cur = (rlim_t)MaxFDs; | |
538 | ||
539 | setrlimit(RLIMIT_NOFILE, &limit); | |
540 | ||
541 | cupsdStartSelect(); | |
542 | ||
543 | /* | |
544 | * Read configuration... | |
545 | */ | |
546 | ||
547 | if (!cupsdReadConfiguration()) | |
548 | return (1); | |
549 | else if (TestConfigFile) | |
550 | { | |
551 | printf("\"%s\" is OK.\n", CupsFilesFile); | |
552 | printf("\"%s\" is OK.\n", ConfigurationFile); | |
553 | return (0); | |
554 | } | |
555 | else if (print_profile) | |
556 | { | |
557 | cups_file_t *fp; /* File pointer */ | |
558 | const char *profile = cupsdCreateProfile(42, 0); | |
559 | /* Profile */ | |
560 | char line[1024]; /* Line from file */ | |
561 | ||
562 | ||
563 | if ((fp = cupsFileOpen(profile, "r")) == NULL) | |
564 | { | |
565 | printf("Unable to open profile file \"%s\": %s\n", profile ? profile : "(null)", strerror(errno)); | |
566 | return (1); | |
567 | } | |
568 | ||
569 | while (cupsFileGets(fp, line, sizeof(line))) | |
570 | puts(line); | |
571 | ||
572 | cupsFileClose(fp); | |
573 | ||
574 | return (0); | |
575 | } | |
576 | ||
577 | /* | |
578 | * Clean out old temp files and printer cache data. | |
579 | */ | |
580 | ||
581 | if (!strncmp(TempDir, RequestRoot, strlen(RequestRoot))) | |
582 | cupsdCleanFiles(TempDir, NULL); | |
583 | ||
584 | cupsdCleanFiles(CacheDir, "*.ipp"); | |
585 | ||
586 | /* | |
587 | * If we were started on demand by launchd or systemd get the listen sockets | |
588 | * file descriptors... | |
589 | */ | |
590 | ||
591 | service_checkin(); | |
592 | service_checkout(0); | |
593 | ||
594 | /* | |
595 | * Startup the server... | |
596 | */ | |
597 | ||
598 | httpInitialize(); | |
599 | ||
600 | cupsdStartServer(); | |
601 | ||
602 | /* | |
603 | * Catch hangup and child signals and ignore broken pipes... | |
604 | */ | |
605 | ||
606 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
607 | sigset(SIGCHLD, sigchld_handler); | |
608 | sigset(SIGHUP, sighup_handler); | |
609 | sigset(SIGPIPE, SIG_IGN); | |
610 | sigset(SIGTERM, sigterm_handler); | |
611 | #elif defined(HAVE_SIGACTION) | |
612 | memset(&action, 0, sizeof(action)); | |
613 | ||
614 | sigemptyset(&action.sa_mask); | |
615 | sigaddset(&action.sa_mask, SIGTERM); | |
616 | sigaddset(&action.sa_mask, SIGCHLD); | |
617 | action.sa_handler = sigchld_handler; | |
618 | sigaction(SIGCHLD, &action, NULL); | |
619 | ||
620 | sigemptyset(&action.sa_mask); | |
621 | sigaddset(&action.sa_mask, SIGHUP); | |
622 | action.sa_handler = sighup_handler; | |
623 | sigaction(SIGHUP, &action, NULL); | |
624 | ||
625 | sigemptyset(&action.sa_mask); | |
626 | action.sa_handler = SIG_IGN; | |
627 | sigaction(SIGPIPE, &action, NULL); | |
628 | ||
629 | sigemptyset(&action.sa_mask); | |
630 | sigaddset(&action.sa_mask, SIGTERM); | |
631 | sigaddset(&action.sa_mask, SIGCHLD); | |
632 | action.sa_handler = sigterm_handler; | |
633 | sigaction(SIGTERM, &action, NULL); | |
634 | #else | |
635 | signal(SIGCLD, sigchld_handler); /* No, SIGCLD isn't a typo... */ | |
636 | signal(SIGHUP, sighup_handler); | |
637 | signal(SIGPIPE, SIG_IGN); | |
638 | signal(SIGTERM, sigterm_handler); | |
639 | #endif /* HAVE_SIGSET */ | |
640 | ||
641 | /* | |
642 | * Initialize authentication certificates... | |
643 | */ | |
644 | ||
645 | cupsdInitCerts(); | |
646 | ||
647 | /* | |
648 | * If we are running in the background, signal the parent process that | |
649 | * we are up and running... | |
650 | */ | |
651 | ||
652 | if (!fg || run_as_child) | |
653 | { | |
654 | /* | |
655 | * Send a signal to the parent process, but only if the parent is | |
656 | * not PID 1 (init). This avoids accidentally shutting down the | |
657 | * system on OpenBSD if you CTRL-C the server before it is up... | |
658 | */ | |
659 | ||
660 | i = getppid(); /* Save parent PID to avoid race condition */ | |
661 | ||
662 | if (i != 1) | |
663 | kill(i, SIGUSR1); | |
664 | } | |
665 | ||
666 | #ifdef __APPLE__ | |
667 | /* | |
668 | * Start power management framework... | |
669 | */ | |
670 | ||
671 | if (use_sysman) | |
672 | cupsdStartSystemMonitor(); | |
673 | #endif /* __APPLE__ */ | |
674 | ||
675 | /* | |
676 | * Send server-started event... | |
677 | */ | |
678 | ||
679 | #ifdef HAVE_ONDEMAND | |
680 | if (OnDemand) | |
681 | cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started on demand."); | |
682 | else | |
683 | #endif /* HAVE_ONDEMAND */ | |
684 | if (fg) | |
685 | cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in foreground."); | |
686 | else | |
687 | cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in background."); | |
688 | ||
689 | cupsdSetBusyState(0); | |
690 | ||
691 | /* | |
692 | * Start any pending print jobs... | |
693 | */ | |
694 | ||
695 | cupsdCheckJobs(); | |
696 | ||
697 | /* | |
698 | * Loop forever... | |
699 | */ | |
700 | ||
701 | current_time = time(NULL); | |
702 | event_time = current_time; | |
703 | expire_time = current_time; | |
704 | local_timeout = 0; | |
705 | fds = 1; | |
706 | report_time = 0; | |
707 | senddoc_time = current_time; | |
708 | ||
709 | while (!stop_scheduler) | |
710 | { | |
711 | /* | |
712 | * Check if there are dead children to handle... | |
713 | */ | |
714 | ||
715 | if (dead_children) | |
716 | process_children(); | |
717 | ||
718 | /* | |
719 | * Check if we need to load the server configuration file... | |
720 | */ | |
721 | ||
722 | if (NeedReload) | |
723 | { | |
724 | /* | |
725 | * Close any idle clients... | |
726 | */ | |
727 | ||
728 | if (cupsArrayCount(Clients) > 0) | |
729 | { | |
730 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); | |
731 | con; | |
732 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
733 | if (httpGetState(con->http) == HTTP_WAITING) | |
734 | cupsdCloseClient(con); | |
735 | else | |
736 | con->http->keep_alive = HTTP_KEEPALIVE_OFF; | |
737 | ||
738 | cupsdPauseListening(); | |
739 | } | |
740 | ||
741 | /* | |
742 | * Restart if all clients are closed and all jobs finished, or | |
743 | * if the reload timeout has elapsed... | |
744 | */ | |
745 | ||
746 | if ((cupsArrayCount(Clients) == 0 && | |
747 | (cupsArrayCount(PrintingJobs) == 0 || NeedReload != RELOAD_ALL)) || | |
748 | (time(NULL) - ReloadTime) >= ReloadTimeout) | |
749 | { | |
750 | /* | |
751 | * Shutdown the server... | |
752 | */ | |
753 | ||
754 | #ifdef HAVE_ONDEMAND | |
755 | if (OnDemand) | |
756 | break; | |
757 | #endif /* HAVE_ONDEMAND */ | |
758 | ||
759 | DoingShutdown = 1; | |
760 | ||
761 | cupsdStopServer(); | |
762 | ||
763 | /* | |
764 | * Read configuration... | |
765 | */ | |
766 | ||
767 | if (!cupsdReadConfiguration()) | |
768 | { | |
769 | #ifdef HAVE_SYSTEMD_SD_JOURNAL_H | |
770 | sd_journal_print(LOG_ERR, "Unable to read configuration file \"%s\" - exiting.", ConfigurationFile); | |
771 | #else | |
772 | syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting.", ConfigurationFile); | |
773 | #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */ | |
774 | ||
775 | break; | |
776 | } | |
777 | ||
778 | /* | |
779 | * Startup the server... | |
780 | */ | |
781 | ||
782 | DoingShutdown = 0; | |
783 | ||
784 | cupsdStartServer(); | |
785 | ||
786 | /* | |
787 | * Send a server-restarted event... | |
788 | */ | |
789 | ||
790 | cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED, NULL, NULL, | |
791 | "Scheduler restarted."); | |
792 | } | |
793 | } | |
794 | ||
795 | /* | |
796 | * Check for available input or ready output. If cupsdDoSelect() | |
797 | * returns 0 or -1, something bad happened and we should exit | |
798 | * immediately. | |
799 | * | |
800 | * Note that we at least have one listening socket open at all | |
801 | * times. | |
802 | */ | |
803 | ||
804 | if ((timeout = select_timeout(fds)) > 1 && LastEvent) | |
805 | timeout = 1; | |
806 | ||
807 | #ifdef HAVE_ONDEMAND | |
808 | /* | |
809 | * If no other work is scheduled and we're being controlled by | |
810 | * launchd then timeout after 'LaunchdTimeout' seconds of | |
811 | * inactivity... | |
812 | */ | |
813 | ||
814 | if (timeout == 86400 && OnDemand && IdleExitTimeout && | |
815 | !cupsArrayCount(ActiveJobs) && | |
816 | # ifdef HAVE_SYSTEMD | |
817 | !WebInterface && | |
818 | # endif /* HAVE_SYSTEMD */ | |
819 | (!Browsing || !BrowseLocalProtocols || !cupsArrayCount(Printers))) | |
820 | { | |
821 | timeout = IdleExitTimeout; | |
822 | service_idle_exit = 1; | |
823 | } | |
824 | else | |
825 | service_idle_exit = 0; | |
826 | #endif /* HAVE_ONDEMAND */ | |
827 | ||
828 | if ((fds = cupsdDoSelect(timeout)) < 0) | |
829 | { | |
830 | /* | |
831 | * Got an error from select! | |
832 | */ | |
833 | ||
834 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) | |
835 | cupsd_printer_t *p; /* Current printer */ | |
836 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ | |
837 | ||
838 | if (errno == EINTR) /* Just interrupted by a signal */ | |
839 | continue; | |
840 | ||
841 | /* | |
842 | * Log all sorts of debug info to help track down the problem. | |
843 | */ | |
844 | ||
845 | cupsdLogMessage(CUPSD_LOG_EMERG, "cupsdDoSelect() failed - %s!", | |
846 | strerror(errno)); | |
847 | ||
848 | for (i = 0, con = (cupsd_client_t *)cupsArrayFirst(Clients); | |
849 | con; | |
850 | i ++, con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
851 | cupsdLogMessage(CUPSD_LOG_EMERG, | |
852 | "Clients[%d] = %d, file = %d, state = %d", | |
853 | i, con->number, con->file, httpGetState(con->http)); | |
854 | ||
855 | for (i = 0, lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); | |
856 | lis; | |
857 | i ++, lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
858 | cupsdLogMessage(CUPSD_LOG_EMERG, "Listeners[%d] = %d", i, lis->fd); | |
859 | ||
860 | cupsdLogMessage(CUPSD_LOG_EMERG, "CGIPipes[0] = %d", CGIPipes[0]); | |
861 | ||
862 | #ifdef __APPLE__ | |
863 | cupsdLogMessage(CUPSD_LOG_EMERG, "SysEventPipes[0] = %d", | |
864 | SysEventPipes[0]); | |
865 | #endif /* __APPLE__ */ | |
866 | ||
867 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
868 | job; | |
869 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
870 | cupsdLogMessage(CUPSD_LOG_EMERG, "Jobs[%d] = %d < [%d %d] > [%d %d]", | |
871 | job->id, | |
872 | job->status_buffer ? job->status_buffer->fd : -1, | |
873 | job->print_pipes[0], job->print_pipes[1], | |
874 | job->back_pipes[0], job->back_pipes[1]); | |
875 | ||
876 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) | |
877 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
878 | p; | |
879 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
880 | cupsdLogMessage(CUPSD_LOG_EMERG, "printer[%s] reg_name=\"%s\"", p->name, | |
881 | p->reg_name ? p->reg_name : "(null)"); | |
882 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ | |
883 | ||
884 | break; | |
885 | } | |
886 | ||
887 | current_time = time(NULL); | |
888 | ||
889 | /* | |
890 | * Write dirty config/state files... | |
891 | */ | |
892 | ||
893 | if (DirtyCleanTime && current_time >= DirtyCleanTime) | |
894 | cupsdCleanDirty(); | |
895 | ||
896 | #ifdef __APPLE__ | |
897 | /* | |
898 | * If we are going to sleep and still have pending jobs, stop them after | |
899 | * a period of time... | |
900 | */ | |
901 | ||
902 | if (SleepJobs > 0 && current_time >= SleepJobs && | |
903 | cupsArrayCount(PrintingJobs) > 0) | |
904 | { | |
905 | SleepJobs = 0; | |
906 | cupsdStopAllJobs(CUPSD_JOB_DEFAULT, 5); | |
907 | } | |
908 | #endif /* __APPLE__ */ | |
909 | ||
910 | #ifndef __APPLE__ | |
911 | /* | |
912 | * Update the network interfaces once a minute... | |
913 | */ | |
914 | ||
915 | if ((current_time - netif_time) >= 60) | |
916 | { | |
917 | netif_time = current_time; | |
918 | NetIFUpdate = 1; | |
919 | } | |
920 | #endif /* !__APPLE__ */ | |
921 | ||
922 | #ifdef HAVE_ONDEMAND | |
923 | /* | |
924 | * If no other work was scheduled and we're being controlled by launchd, | |
925 | * systemd, or upstart then timeout after 'LaunchdTimeout' seconds of | |
926 | * inactivity... | |
927 | */ | |
928 | ||
929 | if (!fds && service_idle_exit) | |
930 | { | |
931 | cupsdLogMessage(CUPSD_LOG_INFO, | |
932 | "Printer sharing is off and there are no jobs pending, " | |
933 | "will restart on demand."); | |
934 | stop_scheduler = 1; | |
935 | break; | |
936 | } | |
937 | #endif /* HAVE_ONDEMAND */ | |
938 | ||
939 | /* | |
940 | * Resume listening for new connections as needed... | |
941 | */ | |
942 | ||
943 | if (ListeningPaused && ListeningPaused <= current_time && | |
944 | cupsArrayCount(Clients) < MaxClients) | |
945 | cupsdResumeListening(); | |
946 | ||
947 | /* | |
948 | * Expire subscriptions and unload completed jobs as needed... | |
949 | */ | |
950 | ||
951 | if (current_time > expire_time) | |
952 | { | |
953 | if (cupsArrayCount(Subscriptions) > 0) | |
954 | cupsdExpireSubscriptions(NULL, NULL); | |
955 | ||
956 | cupsdUnloadCompletedJobs(); | |
957 | ||
958 | expire_time = current_time; | |
959 | } | |
960 | ||
961 | /* | |
962 | * Delete stale local printers... | |
963 | */ | |
964 | ||
965 | if (current_time >= local_timeout) | |
966 | { | |
967 | cupsdDeleteTemporaryPrinters(0); | |
968 | local_timeout = 0; | |
969 | } | |
970 | ||
971 | #ifndef HAVE_AUTHORIZATION_H | |
972 | /* | |
973 | * Update the root certificate once every 5 minutes if we have client | |
974 | * connections... | |
975 | */ | |
976 | ||
977 | if ((current_time - RootCertTime) >= RootCertDuration && RootCertDuration && | |
978 | !RunUser && cupsArrayCount(Clients)) | |
979 | { | |
980 | /* | |
981 | * Update the root certificate... | |
982 | */ | |
983 | ||
984 | cupsdDeleteCert(0); | |
985 | cupsdAddCert(0, "root", cupsdDefaultAuthType()); | |
986 | } | |
987 | #endif /* !HAVE_AUTHORIZATION_H */ | |
988 | ||
989 | /* | |
990 | * Check for new data on the client sockets... | |
991 | */ | |
992 | ||
993 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); | |
994 | con; | |
995 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
996 | { | |
997 | /* | |
998 | * Process pending data in the input buffer... | |
999 | */ | |
1000 | ||
1001 | if (httpGetReady(con->http)) | |
1002 | { | |
1003 | cupsdReadClient(con); | |
1004 | continue; | |
1005 | } | |
1006 | ||
1007 | /* | |
1008 | * Check the activity and close old clients... | |
1009 | */ | |
1010 | ||
1011 | activity = current_time - Timeout; | |
1012 | if (httpGetActivity(con->http) < activity && !con->pipe_pid) | |
1013 | { | |
1014 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Closing client %d after %d seconds of inactivity.", con->number, Timeout); | |
1015 | ||
1016 | cupsdCloseClient(con); | |
1017 | continue; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | /* | |
1022 | * Update any pending multi-file documents... | |
1023 | */ | |
1024 | ||
1025 | if ((current_time - senddoc_time) >= 10) | |
1026 | { | |
1027 | cupsdCheckJobs(); | |
1028 | senddoc_time = current_time; | |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * Clean job history... | |
1033 | */ | |
1034 | ||
1035 | if (JobHistoryUpdate && current_time >= JobHistoryUpdate) | |
1036 | cupsdCleanJobs(); | |
1037 | ||
1038 | /* | |
1039 | * Log statistics at most once a minute when in debug mode... | |
1040 | */ | |
1041 | ||
1042 | if ((current_time - report_time) >= 60 && LogLevel >= CUPSD_LOG_DEBUG) | |
1043 | { | |
1044 | size_t string_count, /* String count */ | |
1045 | alloc_bytes, /* Allocated string bytes */ | |
1046 | total_bytes; /* Total string bytes */ | |
1047 | #ifdef HAVE_MALLINFO | |
1048 | struct mallinfo mem; /* Malloc information */ | |
1049 | ||
1050 | ||
1051 | mem = mallinfo(); | |
1052 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-arena=%lu", mem.arena); | |
1053 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-used=%lu", | |
1054 | mem.usmblks + mem.uordblks); | |
1055 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-free=%lu", | |
1056 | mem.fsmblks + mem.fordblks); | |
1057 | #endif /* HAVE_MALLINFO */ | |
1058 | ||
1059 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: clients=%d", | |
1060 | cupsArrayCount(Clients)); | |
1061 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: jobs=%d", | |
1062 | cupsArrayCount(Jobs)); | |
1063 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: jobs-active=%d", | |
1064 | cupsArrayCount(ActiveJobs)); | |
1065 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: printers=%d", | |
1066 | cupsArrayCount(Printers)); | |
1067 | ||
1068 | string_count = _cupsStrStatistics(&alloc_bytes, &total_bytes); | |
1069 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1070 | "Report: stringpool-string-count=" CUPS_LLFMT, | |
1071 | CUPS_LLCAST string_count); | |
1072 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1073 | "Report: stringpool-alloc-bytes=" CUPS_LLFMT, | |
1074 | CUPS_LLCAST alloc_bytes); | |
1075 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1076 | "Report: stringpool-total-bytes=" CUPS_LLFMT, | |
1077 | CUPS_LLCAST total_bytes); | |
1078 | ||
1079 | report_time = current_time; | |
1080 | } | |
1081 | ||
1082 | /* | |
1083 | * Handle OS-specific event notification for any events that have | |
1084 | * accumulated. Don't send these more than once a second... | |
1085 | */ | |
1086 | ||
1087 | if (LastEvent && (current_time - event_time) >= 1) | |
1088 | { | |
1089 | #ifdef HAVE_NOTIFY_POST | |
1090 | if (LastEvent & (CUPSD_EVENT_PRINTER_ADDED | | |
1091 | CUPSD_EVENT_PRINTER_DELETED | | |
1092 | CUPSD_EVENT_PRINTER_MODIFIED)) | |
1093 | { | |
1094 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1095 | "notify_post(\"com.apple.printerListChange\")"); | |
1096 | notify_post("com.apple.printerListChange"); | |
1097 | } | |
1098 | ||
1099 | if (LastEvent & CUPSD_EVENT_PRINTER_STATE_CHANGED) | |
1100 | { | |
1101 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1102 | "notify_post(\"com.apple.printerHistoryChange\")"); | |
1103 | notify_post("com.apple.printerHistoryChange"); | |
1104 | } | |
1105 | ||
1106 | if (LastEvent & (CUPSD_EVENT_JOB_STATE_CHANGED | | |
1107 | CUPSD_EVENT_JOB_CONFIG_CHANGED | | |
1108 | CUPSD_EVENT_JOB_PROGRESS)) | |
1109 | { | |
1110 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1111 | "notify_post(\"com.apple.jobChange\")"); | |
1112 | notify_post("com.apple.jobChange"); | |
1113 | } | |
1114 | #endif /* HAVE_NOTIFY_POST */ | |
1115 | ||
1116 | /* | |
1117 | * Reset the accumulated events... | |
1118 | */ | |
1119 | ||
1120 | LastEvent = CUPSD_EVENT_NONE; | |
1121 | event_time = current_time; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | /* | |
1126 | * Log a message based on what happened... | |
1127 | */ | |
1128 | ||
1129 | if (stop_scheduler) | |
1130 | { | |
1131 | cupsdLogMessage(CUPSD_LOG_INFO, "Scheduler shutting down normally."); | |
1132 | cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED, NULL, NULL, | |
1133 | "Scheduler shutting down normally."); | |
1134 | } | |
1135 | else | |
1136 | { | |
1137 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1138 | "Scheduler shutting down due to program error."); | |
1139 | cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED, NULL, NULL, | |
1140 | "Scheduler shutting down due to program error."); | |
1141 | } | |
1142 | ||
1143 | /* | |
1144 | * Close all network clients... | |
1145 | */ | |
1146 | ||
1147 | DoingShutdown = 1; | |
1148 | ||
1149 | cupsdStopServer(); | |
1150 | ||
1151 | /* | |
1152 | * Update the KeepAlive/PID file as needed... | |
1153 | */ | |
1154 | ||
1155 | service_checkout(1); | |
1156 | ||
1157 | /* | |
1158 | * Stop all jobs... | |
1159 | */ | |
1160 | ||
1161 | cupsdFreeAllJobs(); | |
1162 | ||
1163 | /* | |
1164 | * Delete all temporary printers... | |
1165 | */ | |
1166 | ||
1167 | cupsdDeleteTemporaryPrinters(1); | |
1168 | ||
1169 | #ifdef __APPLE__ | |
1170 | /* | |
1171 | * Stop monitoring system event monitoring... | |
1172 | */ | |
1173 | ||
1174 | if (use_sysman) | |
1175 | cupsdStopSystemMonitor(); | |
1176 | #endif /* __APPLE__ */ | |
1177 | ||
1178 | cupsdStopSelect(); | |
1179 | ||
1180 | return (!stop_scheduler); | |
1181 | } | |
1182 | ||
1183 | ||
1184 | /* | |
1185 | * 'cupsdAddString()' - Copy and add a string to an array. | |
1186 | */ | |
1187 | ||
1188 | int /* O - 1 on success, 0 on failure */ | |
1189 | cupsdAddString(cups_array_t **a, /* IO - String array */ | |
1190 | const char *s) /* I - String to copy and add */ | |
1191 | { | |
1192 | if (!*a) | |
1193 | *a = cupsArrayNew3((cups_array_func_t)strcmp, NULL, | |
1194 | (cups_ahash_func_t)NULL, 0, | |
1195 | (cups_acopy_func_t)strdup, | |
1196 | (cups_afree_func_t)free); | |
1197 | ||
1198 | return (cupsArrayAdd(*a, (char *)s)); | |
1199 | } | |
1200 | ||
1201 | ||
1202 | /* | |
1203 | * 'cupsdCheckProcess()' - Tell the main loop to check for dead children. | |
1204 | */ | |
1205 | ||
1206 | void | |
1207 | cupsdCheckProcess(void) | |
1208 | { | |
1209 | /* | |
1210 | * Flag that we have dead children... | |
1211 | */ | |
1212 | ||
1213 | dead_children = 1; | |
1214 | } | |
1215 | ||
1216 | ||
1217 | /* | |
1218 | * 'cupsdClearString()' - Clear a string. | |
1219 | */ | |
1220 | ||
1221 | void | |
1222 | cupsdClearString(char **s) /* O - String value */ | |
1223 | { | |
1224 | if (s && *s) | |
1225 | { | |
1226 | free(*s); | |
1227 | *s = NULL; | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | ||
1232 | /* | |
1233 | * 'cupsdFreeStrings()' - Free an array of strings. | |
1234 | */ | |
1235 | ||
1236 | void | |
1237 | cupsdFreeStrings(cups_array_t **a) /* IO - String array */ | |
1238 | { | |
1239 | if (*a) | |
1240 | { | |
1241 | cupsArrayDelete(*a); | |
1242 | *a = NULL; | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | ||
1247 | /* | |
1248 | * 'cupsdHoldSignals()' - Hold child and termination signals. | |
1249 | */ | |
1250 | ||
1251 | void | |
1252 | cupsdHoldSignals(void) | |
1253 | { | |
1254 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
1255 | sigset_t newmask; /* New POSIX signal mask */ | |
1256 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
1257 | ||
1258 | ||
1259 | holdcount ++; | |
1260 | if (holdcount > 1) | |
1261 | return; | |
1262 | ||
1263 | #ifdef HAVE_SIGSET | |
1264 | sighold(SIGTERM); | |
1265 | sighold(SIGCHLD); | |
1266 | #elif defined(HAVE_SIGACTION) | |
1267 | sigemptyset(&newmask); | |
1268 | sigaddset(&newmask, SIGTERM); | |
1269 | sigaddset(&newmask, SIGCHLD); | |
1270 | sigprocmask(SIG_BLOCK, &newmask, &holdmask); | |
1271 | #endif /* HAVE_SIGSET */ | |
1272 | } | |
1273 | ||
1274 | ||
1275 | /* | |
1276 | * 'cupsdReleaseSignals()' - Release signals for delivery. | |
1277 | */ | |
1278 | ||
1279 | void | |
1280 | cupsdReleaseSignals(void) | |
1281 | { | |
1282 | holdcount --; | |
1283 | if (holdcount > 0) | |
1284 | return; | |
1285 | ||
1286 | #ifdef HAVE_SIGSET | |
1287 | sigrelse(SIGTERM); | |
1288 | sigrelse(SIGCHLD); | |
1289 | #elif defined(HAVE_SIGACTION) | |
1290 | sigprocmask(SIG_SETMASK, &holdmask, NULL); | |
1291 | #endif /* HAVE_SIGSET */ | |
1292 | } | |
1293 | ||
1294 | ||
1295 | /* | |
1296 | * 'cupsdSetString()' - Set a string value. | |
1297 | */ | |
1298 | ||
1299 | void | |
1300 | cupsdSetString(char **s, /* O - New string */ | |
1301 | const char *v) /* I - String value */ | |
1302 | { | |
1303 | if (!s || *s == v) | |
1304 | return; | |
1305 | ||
1306 | if (*s) | |
1307 | free(*s); | |
1308 | ||
1309 | if (v) | |
1310 | *s = strdup(v); | |
1311 | else | |
1312 | *s = NULL; | |
1313 | } | |
1314 | ||
1315 | ||
1316 | /* | |
1317 | * 'cupsdSetStringf()' - Set a formatted string value. | |
1318 | */ | |
1319 | ||
1320 | void | |
1321 | cupsdSetStringf(char **s, /* O - New string */ | |
1322 | const char *f, /* I - Printf-style format string */ | |
1323 | ...) /* I - Additional args as needed */ | |
1324 | { | |
1325 | char v[65536 + 64]; /* Formatting string value */ | |
1326 | va_list ap; /* Argument pointer */ | |
1327 | char *olds; /* Old string */ | |
1328 | ||
1329 | ||
1330 | if (!s) | |
1331 | return; | |
1332 | ||
1333 | olds = *s; | |
1334 | ||
1335 | if (f) | |
1336 | { | |
1337 | va_start(ap, f); | |
1338 | vsnprintf(v, sizeof(v), f, ap); | |
1339 | va_end(ap); | |
1340 | ||
1341 | *s = strdup(v); | |
1342 | } | |
1343 | else | |
1344 | *s = NULL; | |
1345 | ||
1346 | if (olds) | |
1347 | free(olds); | |
1348 | } | |
1349 | ||
1350 | ||
1351 | /* | |
1352 | * 'parent_handler()' - Catch USR1/CHLD signals... | |
1353 | */ | |
1354 | ||
1355 | static void | |
1356 | parent_handler(int sig) /* I - Signal */ | |
1357 | { | |
1358 | /* | |
1359 | * Store the signal we got from the OS and return... | |
1360 | */ | |
1361 | ||
1362 | parent_signal = sig; | |
1363 | } | |
1364 | ||
1365 | ||
1366 | /* | |
1367 | * 'process_children()' - Process all dead children... | |
1368 | */ | |
1369 | ||
1370 | static void | |
1371 | process_children(void) | |
1372 | { | |
1373 | int status; /* Exit status of child */ | |
1374 | int pid, /* Process ID of child */ | |
1375 | job_id; /* Job ID of child */ | |
1376 | cupsd_job_t *job; /* Current job */ | |
1377 | int i; /* Looping var */ | |
1378 | char name[1024]; /* Process name */ | |
1379 | const char *type; /* Type of program */ | |
1380 | ||
1381 | ||
1382 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_children()"); | |
1383 | ||
1384 | /* | |
1385 | * Reset the dead_children flag... | |
1386 | */ | |
1387 | ||
1388 | dead_children = 0; | |
1389 | ||
1390 | /* | |
1391 | * Collect the exit status of some children... | |
1392 | */ | |
1393 | ||
1394 | #ifdef HAVE_WAITPID | |
1395 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) | |
1396 | #elif defined(HAVE_WAIT3) | |
1397 | while ((pid = wait3(&status, WNOHANG, NULL)) > 0) | |
1398 | #else | |
1399 | if ((pid = wait(&status)) > 0) | |
1400 | #endif /* HAVE_WAITPID */ | |
1401 | { | |
1402 | /* | |
1403 | * Collect the name of the process that finished... | |
1404 | */ | |
1405 | ||
1406 | cupsdFinishProcess(pid, name, sizeof(name), &job_id); | |
1407 | ||
1408 | /* | |
1409 | * Delete certificates for CGI processes... | |
1410 | */ | |
1411 | ||
1412 | if (pid) | |
1413 | cupsdDeleteCert(pid); | |
1414 | ||
1415 | /* | |
1416 | * Handle completed job filters... | |
1417 | */ | |
1418 | ||
1419 | if (job_id > 0) | |
1420 | job = cupsdFindJob(job_id); | |
1421 | else | |
1422 | job = NULL; | |
1423 | ||
1424 | if (job) | |
1425 | { | |
1426 | for (i = 0; job->filters[i]; i ++) | |
1427 | if (job->filters[i] == pid) | |
1428 | break; | |
1429 | ||
1430 | if (job->filters[i] || job->backend == pid) | |
1431 | { | |
1432 | /* | |
1433 | * OK, this process has gone away; what's left? | |
1434 | */ | |
1435 | ||
1436 | if (job->filters[i]) | |
1437 | { | |
1438 | job->filters[i] = -pid; | |
1439 | type = "Filter"; | |
1440 | } | |
1441 | else | |
1442 | { | |
1443 | job->backend = -pid; | |
1444 | type = "Backend"; | |
1445 | } | |
1446 | ||
1447 | if (status && status != SIGTERM && status != SIGKILL && | |
1448 | status != SIGPIPE) | |
1449 | { | |
1450 | /* | |
1451 | * An error occurred; save the exit status so we know to stop | |
1452 | * the printer or cancel the job when all of the filters finish... | |
1453 | * | |
1454 | * A negative status indicates that the backend failed and the | |
1455 | * printer needs to be stopped. | |
1456 | * | |
1457 | * In order to preserve the most serious status, we always log | |
1458 | * when a process dies due to a signal (e.g. SIGABRT, SIGSEGV, | |
1459 | * and SIGBUS) and prefer to log the backend exit status over a | |
1460 | * filter's. | |
1461 | */ | |
1462 | ||
1463 | int old_status = abs(job->status); | |
1464 | ||
1465 | if (WIFSIGNALED(status) || /* This process crashed, or */ | |
1466 | !job->status || /* No process had a status, or */ | |
1467 | (!job->filters[i] && WIFEXITED(old_status))) | |
1468 | { /* Backend and filter didn't crash */ | |
1469 | if (job->filters[i]) | |
1470 | job->status = status; /* Filter failed */ | |
1471 | else | |
1472 | job->status = -status; /* Backend failed */ | |
1473 | } | |
1474 | ||
1475 | if (job->state_value == IPP_JOB_PROCESSING && | |
1476 | job->status_level > CUPSD_LOG_ERROR && | |
1477 | (job->filters[i] || !WIFEXITED(status))) | |
1478 | { | |
1479 | char message[1024]; /* New printer-state-message */ | |
1480 | ||
1481 | ||
1482 | job->status_level = CUPSD_LOG_ERROR; | |
1483 | ||
1484 | snprintf(message, sizeof(message), "%s failed", type); | |
1485 | ||
1486 | if (job->printer) | |
1487 | { | |
1488 | strlcpy(job->printer->state_message, message, | |
1489 | sizeof(job->printer->state_message)); | |
1490 | } | |
1491 | ||
1492 | if (!job->attrs) | |
1493 | cupsdLoadJob(job); | |
1494 | ||
1495 | if (!job->printer_message && job->attrs) | |
1496 | { | |
1497 | if ((job->printer_message = | |
1498 | ippFindAttribute(job->attrs, "job-printer-state-message", | |
1499 | IPP_TAG_TEXT)) == NULL) | |
1500 | job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, | |
1501 | IPP_TAG_TEXT, | |
1502 | "job-printer-state-message", | |
1503 | NULL, NULL); | |
1504 | } | |
1505 | ||
1506 | if (job->printer_message) | |
1507 | ippSetString(job->attrs, &job->printer_message, 0, message); | |
1508 | } | |
1509 | } | |
1510 | ||
1511 | /* | |
1512 | * If this is not the last file in a job, see if all of the | |
1513 | * filters are done, and if so move to the next file. | |
1514 | */ | |
1515 | ||
1516 | if (job->state_value >= IPP_JOB_CANCELED) | |
1517 | { | |
1518 | /* | |
1519 | * Remove the job from the active list if there are no processes still | |
1520 | * running for it... | |
1521 | */ | |
1522 | ||
1523 | for (i = 0; job->filters[i] < 0; i++); | |
1524 | ||
1525 | if (!job->filters[i] && job->backend <= 0) | |
1526 | cupsArrayRemove(ActiveJobs, job); | |
1527 | } | |
1528 | else if (job->current_file < job->num_files && job->printer) | |
1529 | { | |
1530 | for (i = 0; job->filters[i] < 0; i ++); | |
1531 | ||
1532 | if (!job->filters[i] && | |
1533 | (!job->printer->pc || !job->printer->pc->single_file || | |
1534 | job->backend <= 0)) | |
1535 | { | |
1536 | /* | |
1537 | * Process the next file... | |
1538 | */ | |
1539 | ||
1540 | cupsdContinueJob(job); | |
1541 | } | |
1542 | } | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | /* | |
1547 | * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors | |
1548 | * since they come when we kill/end a process... | |
1549 | */ | |
1550 | ||
1551 | if (status == SIGTERM || status == SIGKILL) | |
1552 | { | |
1553 | cupsdLogJob(job, CUPSD_LOG_DEBUG, | |
1554 | "PID %d (%s) was terminated normally with signal %d.", pid, | |
1555 | name, status); | |
1556 | } | |
1557 | else if (status == SIGPIPE) | |
1558 | { | |
1559 | cupsdLogJob(job, CUPSD_LOG_DEBUG, | |
1560 | "PID %d (%s) did not catch or ignore signal %d.", pid, name, | |
1561 | status); | |
1562 | } | |
1563 | else if (status) | |
1564 | { | |
1565 | if (WIFEXITED(status)) | |
1566 | { | |
1567 | int code = WEXITSTATUS(status); /* Exit code */ | |
1568 | ||
1569 | if (code > 100) | |
1570 | cupsdLogJob(job, CUPSD_LOG_DEBUG, | |
1571 | "PID %d (%s) stopped with status %d (%s)", pid, name, | |
1572 | code, strerror(code - 100)); | |
1573 | else | |
1574 | cupsdLogJob(job, CUPSD_LOG_DEBUG, | |
1575 | "PID %d (%s) stopped with status %d.", pid, name, code); | |
1576 | } | |
1577 | else | |
1578 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "PID %d (%s) crashed on signal %d.", | |
1579 | pid, name, WTERMSIG(status)); | |
1580 | ||
1581 | if (LogLevel < CUPSD_LOG_DEBUG) | |
1582 | cupsdLogJob(job, CUPSD_LOG_INFO, | |
1583 | "Hint: Try setting the LogLevel to \"debug\" to find out " | |
1584 | "more."); | |
1585 | } | |
1586 | else | |
1587 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "PID %d (%s) exited with no errors.", | |
1588 | pid, name); | |
1589 | } | |
1590 | ||
1591 | /* | |
1592 | * If wait*() is interrupted by a signal, tell main() to call us again... | |
1593 | */ | |
1594 | ||
1595 | if (pid < 0 && errno == EINTR) | |
1596 | dead_children = 1; | |
1597 | } | |
1598 | ||
1599 | ||
1600 | /* | |
1601 | * 'select_timeout()' - Calculate the select timeout value. | |
1602 | * | |
1603 | */ | |
1604 | ||
1605 | static long /* O - Number of seconds */ | |
1606 | select_timeout(int fds) /* I - Number of descriptors returned */ | |
1607 | { | |
1608 | long timeout; /* Timeout for select */ | |
1609 | time_t now; /* Current time */ | |
1610 | cupsd_client_t *con; /* Client information */ | |
1611 | cupsd_job_t *job; /* Job information */ | |
1612 | cupsd_printer_t *printer; /* Printer information */ | |
1613 | const char *why; /* Debugging aid */ | |
1614 | ||
1615 | ||
1616 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: JobHistoryUpdate=%ld", | |
1617 | (long)JobHistoryUpdate); | |
1618 | ||
1619 | /* | |
1620 | * Check to see if any of the clients have pending data to be | |
1621 | * processed; if so, the timeout should be 0... | |
1622 | */ | |
1623 | ||
1624 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); | |
1625 | con; | |
1626 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
1627 | if (httpGetReady(con->http)) | |
1628 | return (0); | |
1629 | ||
1630 | /* | |
1631 | * If select has been active in the last second (fds > 0) or we have | |
1632 | * many resources in use then don't bother trying to optimize the | |
1633 | * timeout, just make it 1 second. | |
1634 | */ | |
1635 | ||
1636 | if (fds > 0 || cupsArrayCount(Clients) > 50) | |
1637 | return (1); | |
1638 | ||
1639 | /* | |
1640 | * Otherwise, check all of the possible events that we need to wake for... | |
1641 | */ | |
1642 | ||
1643 | now = time(NULL); | |
1644 | timeout = now + 86400; /* 86400 == 1 day */ | |
1645 | why = "do nothing"; | |
1646 | ||
1647 | #ifdef __APPLE__ | |
1648 | /* | |
1649 | * When going to sleep, wake up to abort jobs that don't complete in time. | |
1650 | */ | |
1651 | ||
1652 | if (SleepJobs > 0 && SleepJobs < timeout) | |
1653 | { | |
1654 | timeout = SleepJobs; | |
1655 | why = "abort jobs before sleeping"; | |
1656 | } | |
1657 | #endif /* __APPLE__ */ | |
1658 | ||
1659 | /* | |
1660 | * Check whether we are accepting new connections... | |
1661 | */ | |
1662 | ||
1663 | if (ListeningPaused > 0 && cupsArrayCount(Clients) < MaxClients && | |
1664 | ListeningPaused < timeout) | |
1665 | { | |
1666 | if (ListeningPaused <= now) | |
1667 | timeout = now; | |
1668 | else | |
1669 | timeout = ListeningPaused; | |
1670 | ||
1671 | why = "resume listening"; | |
1672 | } | |
1673 | ||
1674 | /* | |
1675 | * Check the activity and close old clients... | |
1676 | */ | |
1677 | ||
1678 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); | |
1679 | con; | |
1680 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
1681 | if ((httpGetActivity(con->http) + Timeout) < timeout) | |
1682 | { | |
1683 | timeout = httpGetActivity(con->http) + Timeout; | |
1684 | why = "timeout a client connection"; | |
1685 | } | |
1686 | ||
1687 | /* | |
1688 | * Write out changes to configuration and state files... | |
1689 | */ | |
1690 | ||
1691 | if (DirtyCleanTime && timeout > DirtyCleanTime) | |
1692 | { | |
1693 | timeout = DirtyCleanTime; | |
1694 | why = "write dirty config/state files"; | |
1695 | } | |
1696 | ||
1697 | /* | |
1698 | * Check for any job activity... | |
1699 | */ | |
1700 | ||
1701 | if (JobHistoryUpdate && timeout > JobHistoryUpdate) | |
1702 | { | |
1703 | timeout = JobHistoryUpdate; | |
1704 | why = "update job history"; | |
1705 | } | |
1706 | ||
1707 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
1708 | job; | |
1709 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
1710 | { | |
1711 | if (job->cancel_time && job->cancel_time < timeout) | |
1712 | { | |
1713 | timeout = job->cancel_time; | |
1714 | why = "cancel stuck jobs"; | |
1715 | } | |
1716 | ||
1717 | if (job->kill_time && job->kill_time < timeout) | |
1718 | { | |
1719 | timeout = job->kill_time; | |
1720 | why = "kill unresponsive jobs"; | |
1721 | } | |
1722 | ||
1723 | if (job->state_value == IPP_JOB_HELD && job->hold_until < timeout) | |
1724 | { | |
1725 | timeout = job->hold_until; | |
1726 | why = "release held jobs"; | |
1727 | } | |
1728 | ||
1729 | if (job->state_value == IPP_JOB_PENDING && timeout > (now + 10)) | |
1730 | { | |
1731 | timeout = now + 10; | |
1732 | why = "start pending jobs"; | |
1733 | break; | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | /* | |
1738 | * Check for temporary printers that need to be deleted... | |
1739 | */ | |
1740 | ||
1741 | for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers); printer; printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1742 | { | |
1743 | if (printer->temporary && !printer->job && (!local_timeout || local_timeout > (printer->state_time + 60))) | |
1744 | local_timeout = printer->state_time + 60; | |
1745 | } | |
1746 | ||
1747 | if (timeout > local_timeout && local_timeout) | |
1748 | { | |
1749 | timeout = local_timeout; | |
1750 | why = "delete stale local printers"; | |
1751 | } | |
1752 | ||
1753 | /* | |
1754 | * Adjust from absolute to relative time. We add 1 second to the timeout since | |
1755 | * events occur after the timeout expires, and limit the timeout to 86400 | |
1756 | * seconds (1 day) to avoid select() timeout limits present on some operating | |
1757 | * systems... | |
1758 | */ | |
1759 | ||
1760 | timeout = timeout - now + 1; | |
1761 | ||
1762 | if (timeout < 1) | |
1763 | timeout = 1; | |
1764 | else if (timeout > 86400) | |
1765 | timeout = 86400; | |
1766 | ||
1767 | /* | |
1768 | * Log and return the timeout value... | |
1769 | */ | |
1770 | ||
1771 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout(%d): %ld seconds to %s", | |
1772 | fds, timeout, why); | |
1773 | ||
1774 | return (timeout); | |
1775 | } | |
1776 | ||
1777 | ||
1778 | /* | |
1779 | * 'sigchld_handler()' - Handle 'child' signals from old processes. | |
1780 | */ | |
1781 | ||
1782 | static void | |
1783 | sigchld_handler(int sig) /* I - Signal number */ | |
1784 | { | |
1785 | (void)sig; | |
1786 | ||
1787 | /* | |
1788 | * Flag that we have dead children... | |
1789 | */ | |
1790 | ||
1791 | dead_children = 1; | |
1792 | ||
1793 | /* | |
1794 | * Reset the signal handler as needed... | |
1795 | */ | |
1796 | ||
1797 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1798 | signal(SIGCLD, sigchld_handler); | |
1799 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1800 | } | |
1801 | ||
1802 | ||
1803 | /* | |
1804 | * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler. | |
1805 | */ | |
1806 | ||
1807 | static void | |
1808 | sighup_handler(int sig) /* I - Signal number */ | |
1809 | { | |
1810 | (void)sig; | |
1811 | ||
1812 | NeedReload = RELOAD_ALL; | |
1813 | ReloadTime = time(NULL); | |
1814 | ||
1815 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1816 | signal(SIGHUP, sighup_handler); | |
1817 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1818 | } | |
1819 | ||
1820 | ||
1821 | /* | |
1822 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler. | |
1823 | */ | |
1824 | ||
1825 | static void | |
1826 | sigterm_handler(int sig) /* I - Signal number */ | |
1827 | { | |
1828 | (void)sig; /* remove compiler warnings... */ | |
1829 | ||
1830 | /* | |
1831 | * Flag that we should stop and return... | |
1832 | */ | |
1833 | ||
1834 | stop_scheduler = 1; | |
1835 | } | |
1836 | ||
1837 | ||
1838 | #ifdef HAVE_ONDEMAND | |
1839 | /* | |
1840 | * 'service_add_listener()' - Bind an open fd as a Listener. | |
1841 | */ | |
1842 | ||
1843 | static void | |
1844 | service_add_listener(int fd, /* I - Socket file descriptor */ | |
1845 | int idx) /* I - Listener number, for logging */ | |
1846 | { | |
1847 | cupsd_listener_t *lis; /* Listeners array */ | |
1848 | http_addr_t addr; /* Address variable */ | |
1849 | socklen_t addrlen; /* Length of address */ | |
1850 | char s[256]; /* String addresss */ | |
1851 | ||
1852 | ||
1853 | addrlen = sizeof(addr); | |
1854 | ||
1855 | if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) | |
1856 | { | |
1857 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_add_listener: Unable to get local address for listener #%d: %s", idx + 1, strerror(errno)); | |
1858 | return; | |
1859 | } | |
1860 | ||
1861 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_add_listener: Listener #%d at fd %d, \"%s\".", idx + 1, fd, httpAddrString(&addr, s, sizeof(s))); | |
1862 | ||
1863 | /* | |
1864 | * Try to match the on-demand socket address to one of the listeners... | |
1865 | */ | |
1866 | ||
1867 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); | |
1868 | lis; | |
1869 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
1870 | if (httpAddrEqual(&lis->address, &addr)) | |
1871 | break; | |
1872 | ||
1873 | /* | |
1874 | * Add a new listener If there's no match... | |
1875 | */ | |
1876 | ||
1877 | if (lis) | |
1878 | { | |
1879 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_add_listener: Matched existing listener #%d to %s.", idx + 1, httpAddrString(&(lis->address), s, sizeof(s))); | |
1880 | } | |
1881 | else | |
1882 | { | |
1883 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_add_listener: Adding new listener #%d for %s.", idx + 1, httpAddrString(&addr, s, sizeof(s))); | |
1884 | ||
1885 | if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) | |
1886 | { | |
1887 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_add_listener: Unable to allocate listener: %s.", strerror(errno)); | |
1888 | exit(EXIT_FAILURE); | |
1889 | return; | |
1890 | } | |
1891 | ||
1892 | cupsArrayAdd(Listeners, lis); | |
1893 | ||
1894 | memcpy(&lis->address, &addr, sizeof(lis->address)); | |
1895 | } | |
1896 | ||
1897 | lis->fd = fd; | |
1898 | lis->on_demand = 1; | |
1899 | ||
1900 | # ifdef HAVE_SSL | |
1901 | if (httpAddrPort(&(lis->address)) == 443) | |
1902 | lis->encryption = HTTP_ENCRYPT_ALWAYS; | |
1903 | # endif /* HAVE_SSL */ | |
1904 | } | |
1905 | #endif /* HAVE_ONDEMAND */ | |
1906 | ||
1907 | ||
1908 | /* | |
1909 | * 'service_checkin()' - Check-in with launchd and collect the listening fds. | |
1910 | */ | |
1911 | ||
1912 | static void | |
1913 | service_checkin(void) | |
1914 | { | |
1915 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid()); | |
1916 | ||
1917 | #ifdef HAVE_LAUNCHD | |
1918 | if (OnDemand) | |
1919 | { | |
1920 | int error; /* Check-in error, if any */ | |
1921 | size_t i, /* Looping var */ | |
1922 | count; /* Number of listeners */ | |
1923 | int *ld_sockets; /* Listener sockets */ | |
1924 | ||
1925 | /* | |
1926 | * Check-in with launchd... | |
1927 | */ | |
1928 | ||
1929 | if ((error = launch_activate_socket("Listeners", &ld_sockets, &count)) != 0) | |
1930 | { | |
1931 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(error)); | |
1932 | exit(EXIT_FAILURE); | |
1933 | return; /* anti-compiler-warning */ | |
1934 | } | |
1935 | ||
1936 | /* | |
1937 | * Try to match the launchd sockets to the cupsd listeners... | |
1938 | */ | |
1939 | ||
1940 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count); | |
1941 | ||
1942 | for (i = 0; i < count; i ++) | |
1943 | service_add_listener(ld_sockets[i], (int)i); | |
1944 | ||
1945 | free(ld_sockets); | |
1946 | } | |
1947 | ||
1948 | #elif defined(HAVE_SYSTEMD) | |
1949 | if (OnDemand) | |
1950 | { | |
1951 | int i, /* Looping var */ | |
1952 | count; /* Number of listeners */ | |
1953 | ||
1954 | /* | |
1955 | * Check-in with systemd... | |
1956 | */ | |
1957 | ||
1958 | if ((count = sd_listen_fds(0)) < 0) | |
1959 | { | |
1960 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(-count)); | |
1961 | exit(EXIT_FAILURE); | |
1962 | return; /* anti-compiler-warning */ | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Try to match the systemd sockets to the cupsd listeners... | |
1967 | */ | |
1968 | ||
1969 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", count); | |
1970 | ||
1971 | for (i = 0; i < count; i ++) | |
1972 | service_add_listener(SD_LISTEN_FDS_START + i, i); | |
1973 | } | |
1974 | ||
1975 | #elif defined(HAVE_UPSTART) | |
1976 | if (OnDemand) | |
1977 | { | |
1978 | const char *e; /* Environment var */ | |
1979 | int fd; /* File descriptor */ | |
1980 | ||
1981 | ||
1982 | if (!(e = getenv("UPSTART_EVENTS"))) | |
1983 | { | |
1984 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get started via Upstart."); | |
1985 | exit(EXIT_FAILURE); | |
1986 | return; | |
1987 | } | |
1988 | ||
1989 | if (strcasecmp(e, "socket")) | |
1990 | { | |
1991 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get triggered via an Upstart socket event."); | |
1992 | exit(EXIT_FAILURE); | |
1993 | return; | |
1994 | } | |
1995 | ||
1996 | if ((e = getenv("UPSTART_FDS")) == NULL) | |
1997 | { | |
1998 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets from UPSTART_FDS."); | |
1999 | exit(EXIT_FAILURE); | |
2000 | return; | |
2001 | } | |
2002 | ||
2003 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: UPSTART_FDS=%s", e); | |
2004 | ||
2005 | fd = (int)strtol(e, NULL, 10); | |
2006 | if (fd < 0) | |
2007 | { | |
2008 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno)); | |
2009 | exit(EXIT_FAILURE); | |
2010 | return; | |
2011 | } | |
2012 | ||
2013 | /* | |
2014 | * Upstart only supportst a single on-demand socket file descriptor... | |
2015 | */ | |
2016 | ||
2017 | service_add_listener(fd, 0); | |
2018 | } | |
2019 | #endif /* HAVE_LAUNCHD */ | |
2020 | } | |
2021 | ||
2022 | ||
2023 | /* | |
2024 | * 'service_checkout()' - Update the KeepAlive/PID file as needed. | |
2025 | */ | |
2026 | ||
2027 | static void | |
2028 | service_checkout(int shutdown) /* I - Shutting down? */ | |
2029 | { | |
2030 | cups_file_t *fp; /* File */ | |
2031 | char pidfile[1024]; /* PID/KeepAlive file */ | |
2032 | ||
2033 | ||
2034 | /* | |
2035 | * When running on-demand, use the KeepAlive file, otherwise write a PID file | |
2036 | * to StateDir... | |
2037 | */ | |
2038 | ||
2039 | #ifdef HAVE_ONDEMAND | |
2040 | if (OnDemand) | |
2041 | { | |
2042 | strlcpy(pidfile, CUPS_KEEPALIVE, sizeof(pidfile)); | |
2043 | ||
2044 | if (cupsArrayCount(ActiveJobs) || /* Active jobs */ | |
2045 | WebInterface || /* Web interface enabled */ | |
2046 | NeedReload || /* Doing a reload */ | |
2047 | (Browsing && BrowseLocalProtocols && cupsArrayCount(Printers))) | |
2048 | /* Printers being shared */ | |
2049 | { | |
2050 | /* | |
2051 | * Create or remove the "keep-alive" file based on whether there are active | |
2052 | * jobs or shared printers to advertise... | |
2053 | */ | |
2054 | ||
2055 | shutdown = 0; | |
2056 | } | |
2057 | } | |
2058 | else | |
2059 | #endif /* HAVE_ONDEMAND */ | |
2060 | snprintf(pidfile, sizeof(pidfile), "%s/cupsd.pid", StateDir); | |
2061 | ||
2062 | if (shutdown) | |
2063 | { | |
2064 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Removing KeepAlive/PID file \"%s\".", pidfile); | |
2065 | ||
2066 | unlink(pidfile); | |
2067 | } | |
2068 | else | |
2069 | { | |
2070 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Creating KeepAlive/PID file \"%s\".", pidfile); | |
2071 | ||
2072 | if ((fp = cupsFileOpen(pidfile, "w")) != NULL) | |
2073 | { | |
2074 | /* | |
2075 | * Save the PID in the file... | |
2076 | */ | |
2077 | ||
2078 | cupsFilePrintf(fp, "%d\n", (int)getpid()); | |
2079 | cupsFileClose(fp); | |
2080 | } | |
2081 | else | |
2082 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create KeepAlive/PID file \"%s\": %s", pidfile, strerror(errno)); | |
2083 | } | |
2084 | } | |
2085 | ||
2086 | ||
2087 | /* | |
2088 | * 'usage()' - Show scheduler usage. | |
2089 | */ | |
2090 | ||
2091 | static void | |
2092 | usage(int status) /* O - Exit status */ | |
2093 | { | |
2094 | FILE *fp = status ? stderr : stdout; /* Output file */ | |
2095 | ||
2096 | ||
2097 | _cupsLangPuts(fp, _("Usage: cupsd [options]")); | |
2098 | _cupsLangPuts(fp, _("Options:")); | |
2099 | _cupsLangPuts(fp, _(" -c cupsd.conf Set cupsd.conf file to use.")); | |
2100 | _cupsLangPuts(fp, _(" -f Run in the foreground.")); | |
2101 | _cupsLangPuts(fp, _(" -F Run in the foreground but detach from console.")); | |
2102 | _cupsLangPuts(fp, _(" -h Show this usage message.")); | |
2103 | #ifdef HAVE_ONDEMAND | |
2104 | _cupsLangPuts(fp, _(" -l Run cupsd on demand.")); | |
2105 | #endif /* HAVE_ONDEMAND */ | |
2106 | _cupsLangPuts(fp, _(" -s cups-files.conf Set cups-files.conf file to use.")); | |
2107 | _cupsLangPuts(fp, _(" -t Test the configuration file.")); | |
2108 | ||
2109 | exit(status); | |
2110 | } |