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