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