]>
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 | * |
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 */ | |
03bc3ef4 | 41 | #endif /* HAVE_ONDEMAND */ |
441de8b2 | 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); | |
1720786e | 70 | static void service_checkin(void); |
03bc3ef4 | 71 | static void service_checkout(int shutdown); |
85dda01c | 72 | static void usage(int status) __attribute__((noreturn)); |
ef416fc2 | 73 | |
74 | ||
75 | /* | |
76 | * Local globals... | |
77 | */ | |
78 | ||
411affcf | 79 | static int parent_signal = 0; |
80 | /* Set to signal number from child */ | |
81 | static int holdcount = 0; /* Number of times "hold" was called */ | |
ef416fc2 | 82 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
411affcf | 83 | static sigset_t holdmask; /* Old POSIX signal mask */ |
ef416fc2 | 84 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ |
411affcf | 85 | static int dead_children = 0; |
86 | /* Dead children? */ | |
87 | static int stop_scheduler = 0; | |
88 | /* Should the scheduler stop? */ | |
e1bb4a6e MS |
89 | static time_t local_timeout = 0; |
90 | /* Next local printer timeout */ | |
411affcf | 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... */ |
03bc3ef4 | 242 | #ifdef 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 | |
03bc3ef4 MS |
583 | /* |
584 | * If we were started on demand by launchd or systemd get the listen sockets | |
585 | * file descriptors... | |
586 | */ | |
a4d04587 | 587 | |
03bc3ef4 MS |
588 | service_checkin(); |
589 | service_checkout(0); | |
a4d04587 | 590 | |
591 | /* | |
592 | * Startup the server... | |
593 | */ | |
594 | ||
6d2f911b MS |
595 | httpInitialize(); |
596 | ||
a4d04587 | 597 | cupsdStartServer(); |
598 | ||
ef416fc2 | 599 | /* |
600 | * Catch hangup and child signals and ignore broken pipes... | |
601 | */ | |
602 | ||
603 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
e1d6a774 | 604 | sigset(SIGCHLD, sigchld_handler); |
4744bd90 | 605 | sigset(SIGHUP, sighup_handler); |
ef416fc2 | 606 | sigset(SIGPIPE, SIG_IGN); |
607 | sigset(SIGTERM, sigterm_handler); | |
608 | #elif defined(HAVE_SIGACTION) | |
609 | memset(&action, 0, sizeof(action)); | |
610 | ||
e1d6a774 | 611 | sigemptyset(&action.sa_mask); |
612 | sigaddset(&action.sa_mask, SIGTERM); | |
613 | sigaddset(&action.sa_mask, SIGCHLD); | |
614 | action.sa_handler = sigchld_handler; | |
615 | sigaction(SIGCHLD, &action, NULL); | |
616 | ||
ef416fc2 | 617 | sigemptyset(&action.sa_mask); |
618 | sigaddset(&action.sa_mask, SIGHUP); | |
4744bd90 | 619 | action.sa_handler = sighup_handler; |
ef416fc2 | 620 | sigaction(SIGHUP, &action, NULL); |
621 | ||
622 | sigemptyset(&action.sa_mask); | |
623 | action.sa_handler = SIG_IGN; | |
624 | sigaction(SIGPIPE, &action, NULL); | |
625 | ||
626 | sigemptyset(&action.sa_mask); | |
627 | sigaddset(&action.sa_mask, SIGTERM); | |
628 | sigaddset(&action.sa_mask, SIGCHLD); | |
629 | action.sa_handler = sigterm_handler; | |
630 | sigaction(SIGTERM, &action, NULL); | |
631 | #else | |
e1d6a774 | 632 | signal(SIGCLD, sigchld_handler); /* No, SIGCLD isn't a typo... */ |
4744bd90 | 633 | signal(SIGHUP, sighup_handler); |
ef416fc2 | 634 | signal(SIGPIPE, SIG_IGN); |
635 | signal(SIGTERM, sigterm_handler); | |
636 | #endif /* HAVE_SIGSET */ | |
637 | ||
ef416fc2 | 638 | /* |
639 | * Initialize authentication certificates... | |
640 | */ | |
641 | ||
642 | cupsdInitCerts(); | |
643 | ||
644 | /* | |
645 | * If we are running in the background, signal the parent process that | |
646 | * we are up and running... | |
647 | */ | |
648 | ||
a41f09e2 | 649 | if (!fg || run_as_child) |
ef416fc2 | 650 | { |
651 | /* | |
652 | * Send a signal to the parent process, but only if the parent is | |
653 | * not PID 1 (init). This avoids accidentally shutting down the | |
654 | * system on OpenBSD if you CTRL-C the server before it is up... | |
655 | */ | |
656 | ||
657 | i = getppid(); /* Save parent PID to avoid race condition */ | |
658 | ||
659 | if (i != 1) | |
660 | kill(i, SIGUSR1); | |
661 | } | |
662 | ||
e1d6a774 | 663 | #ifdef __APPLE__ |
09ec0018 | 664 | /* |
665 | * Start power management framework... | |
666 | */ | |
667 | ||
1340db2d MS |
668 | if (use_sysman) |
669 | cupsdStartSystemMonitor(); | |
e1d6a774 | 670 | #endif /* __APPLE__ */ |
ef416fc2 | 671 | |
49d87452 MS |
672 | /* |
673 | * Send server-started event... | |
674 | */ | |
675 | ||
03bc3ef4 | 676 | #ifdef HAVE_ONDEMAND |
1720786e MS |
677 | if (OnDemand) |
678 | cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started on demand."); | |
49d87452 | 679 | else |
c187d9ab | 680 | #endif /* HAVE_ONDEMAND */ |
49d87452 | 681 | if (fg) |
1720786e | 682 | cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in foreground."); |
49d87452 | 683 | else |
1720786e | 684 | cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED, NULL, NULL, "Scheduler started in background."); |
49d87452 | 685 | |
ef416fc2 | 686 | /* |
687 | * Start any pending print jobs... | |
688 | */ | |
689 | ||
690 | cupsdCheckJobs(); | |
691 | ||
692 | /* | |
693 | * Loop forever... | |
694 | */ | |
695 | ||
50fe7201 | 696 | current_time = time(NULL); |
50fe7201 MS |
697 | event_time = current_time; |
698 | expire_time = current_time; | |
123cfe02 | 699 | local_timeout = 0; |
ef416fc2 | 700 | fds = 1; |
5bd77a73 | 701 | report_time = 0; |
50fe7201 | 702 | senddoc_time = current_time; |
ef416fc2 | 703 | |
704 | while (!stop_scheduler) | |
705 | { | |
ef416fc2 | 706 | /* |
707 | * Check if there are dead children to handle... | |
708 | */ | |
709 | ||
710 | if (dead_children) | |
711 | process_children(); | |
712 | ||
713 | /* | |
714 | * Check if we need to load the server configuration file... | |
715 | */ | |
716 | ||
717 | if (NeedReload) | |
718 | { | |
719 | /* | |
720 | * Close any idle clients... | |
721 | */ | |
722 | ||
bd7854cb | 723 | if (cupsArrayCount(Clients) > 0) |
ef416fc2 | 724 | { |
bd7854cb | 725 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
726 | con; | |
727 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
996acce8 | 728 | if (httpGetState(con->http) == HTTP_WAITING) |
ef416fc2 | 729 | cupsdCloseClient(con); |
ef416fc2 | 730 | else |
996acce8 | 731 | con->http->keep_alive = HTTP_KEEPALIVE_OFF; |
ef416fc2 | 732 | |
733 | cupsdPauseListening(); | |
734 | } | |
735 | ||
ef416fc2 | 736 | /* |
737 | * Restart if all clients are closed and all jobs finished, or | |
738 | * if the reload timeout has elapsed... | |
739 | */ | |
740 | ||
bd7854cb | 741 | if ((cupsArrayCount(Clients) == 0 && |
238c3832 | 742 | (cupsArrayCount(PrintingJobs) == 0 || NeedReload != RELOAD_ALL)) || |
ef416fc2 | 743 | (time(NULL) - ReloadTime) >= ReloadTimeout) |
744 | { | |
a4d04587 | 745 | /* |
746 | * Shutdown the server... | |
747 | */ | |
748 | ||
1166bf58 | 749 | #ifdef HAVE_ONDEMAND |
83d8789d MS |
750 | if (OnDemand) |
751 | break; | |
1166bf58 | 752 | #endif /* HAVE_ONDEMAND */ |
83d8789d | 753 | |
7cf5915e MS |
754 | DoingShutdown = 1; |
755 | ||
a4d04587 | 756 | cupsdStopServer(); |
757 | ||
758 | /* | |
759 | * Read configuration... | |
760 | */ | |
761 | ||
ef416fc2 | 762 | if (!cupsdReadConfiguration()) |
763 | { | |
250bb8a9 | 764 | #ifdef HAVE_SYSTEMD_SD_JOURNAL_H |
a1797929 MS |
765 | sd_journal_print(LOG_ERR, "Unable to read configuration file \"%s\" - exiting.", ConfigurationFile); |
766 | #else | |
767 | syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting.", ConfigurationFile); | |
250bb8a9 | 768 | #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */ |
a1797929 | 769 | |
ef416fc2 | 770 | break; |
771 | } | |
a4d04587 | 772 | |
a4d04587 | 773 | /* |
774 | * Startup the server... | |
775 | */ | |
776 | ||
7cf5915e MS |
777 | DoingShutdown = 0; |
778 | ||
a4d04587 | 779 | cupsdStartServer(); |
49d87452 MS |
780 | |
781 | /* | |
782 | * Send a server-restarted event... | |
783 | */ | |
784 | ||
785 | cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED, NULL, NULL, | |
786 | "Scheduler restarted."); | |
ef416fc2 | 787 | } |
788 | } | |
789 | ||
790 | /* | |
f7deaa1a | 791 | * Check for available input or ready output. If cupsdDoSelect() |
792 | * returns 0 or -1, something bad happened and we should exit | |
793 | * immediately. | |
ef416fc2 | 794 | * |
795 | * Note that we at least have one listening socket open at all | |
796 | * times. | |
797 | */ | |
798 | ||
50fe7201 MS |
799 | if ((timeout = select_timeout(fds)) > 1 && LastEvent) |
800 | timeout = 1; | |
ef416fc2 | 801 | |
1166bf58 | 802 | #ifdef HAVE_ONDEMAND |
a4d04587 | 803 | /* |
804 | * If no other work is scheduled and we're being controlled by | |
411affcf | 805 | * launchd then timeout after 'LaunchdTimeout' seconds of |
a4d04587 | 806 | * inactivity... |
807 | */ | |
808 | ||
1720786e | 809 | if (timeout == 86400 && OnDemand && IdleExitTimeout && |
bc44d920 | 810 | !cupsArrayCount(ActiveJobs) && |
0627e869 MS |
811 | # ifdef HAVE_SYSTEMD |
812 | !WebInterface && | |
813 | # endif /* HAVE_SYSTEMD */ | |
a2326b5b | 814 | (!Browsing || !BrowseLocalProtocols || !cupsArrayCount(Printers))) |
a4d04587 | 815 | { |
1720786e MS |
816 | timeout = IdleExitTimeout; |
817 | service_idle_exit = 1; | |
a4d04587 | 818 | } |
819 | else | |
1720786e | 820 | service_idle_exit = 0; |
c187d9ab | 821 | #endif /* HAVE_ONDEMAND */ |
a4d04587 | 822 | |
f7deaa1a | 823 | if ((fds = cupsdDoSelect(timeout)) < 0) |
ef416fc2 | 824 | { |
ef416fc2 | 825 | /* |
826 | * Got an error from select! | |
827 | */ | |
828 | ||
37e7e6e0 | 829 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
f7deaa1a | 830 | cupsd_printer_t *p; /* Current printer */ |
37e7e6e0 | 831 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
f7deaa1a | 832 | |
f7deaa1a | 833 | if (errno == EINTR) /* Just interrupted by a signal */ |
ef416fc2 | 834 | continue; |
835 | ||
836 | /* | |
837 | * Log all sorts of debug info to help track down the problem. | |
838 | */ | |
839 | ||
f7deaa1a | 840 | cupsdLogMessage(CUPSD_LOG_EMERG, "cupsdDoSelect() failed - %s!", |
ef416fc2 | 841 | strerror(errno)); |
842 | ||
bd7854cb | 843 | for (i = 0, con = (cupsd_client_t *)cupsArrayFirst(Clients); |
844 | con; | |
845 | i ++, con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 846 | cupsdLogMessage(CUPSD_LOG_EMERG, |
847 | "Clients[%d] = %d, file = %d, state = %d", | |
996acce8 | 848 | i, con->number, con->file, httpGetState(con->http)); |
ef416fc2 | 849 | |
bd7854cb | 850 | for (i = 0, lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); |
851 | lis; | |
852 | i ++, lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
ef416fc2 | 853 | cupsdLogMessage(CUPSD_LOG_EMERG, "Listeners[%d] = %d", i, lis->fd); |
854 | ||
923edb68 | 855 | cupsdLogMessage(CUPSD_LOG_EMERG, "CGIPipes[0] = %d", CGIPipes[0]); |
856 | ||
09ec0018 | 857 | #ifdef __APPLE__ |
858 | cupsdLogMessage(CUPSD_LOG_EMERG, "SysEventPipes[0] = %d", | |
859 | SysEventPipes[0]); | |
860 | #endif /* __APPLE__ */ | |
861 | ||
ef416fc2 | 862 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
863 | job; | |
864 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
865 | cupsdLogMessage(CUPSD_LOG_EMERG, "Jobs[%d] = %d < [%d %d] > [%d %d]", | |
866 | job->id, | |
867 | job->status_buffer ? job->status_buffer->fd : -1, | |
868 | job->print_pipes[0], job->print_pipes[1], | |
869 | job->back_pipes[0], job->back_pipes[1]); | |
f7deaa1a | 870 | |
37e7e6e0 | 871 | #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) |
f7deaa1a | 872 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); |
873 | p; | |
874 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
7a14d768 MS |
875 | cupsdLogMessage(CUPSD_LOG_EMERG, "printer[%s] reg_name=\"%s\"", p->name, |
876 | p->reg_name ? p->reg_name : "(null)"); | |
37e7e6e0 | 877 | #endif /* HAVE_DNSSD || HAVE_AVAHI */ |
f7deaa1a | 878 | |
ef416fc2 | 879 | break; |
880 | } | |
881 | ||
882 | current_time = time(NULL); | |
883 | ||
3dfe78b3 MS |
884 | /* |
885 | * Write dirty config/state files... | |
886 | */ | |
887 | ||
b393bc25 | 888 | if (DirtyCleanTime && current_time >= DirtyCleanTime && cupsArrayCount(Clients) == 0) |
3dfe78b3 | 889 | cupsdCleanDirty(); |
3dfe78b3 | 890 | |
e6013cfa MS |
891 | #ifdef __APPLE__ |
892 | /* | |
893 | * If we are going to sleep and still have pending jobs, stop them after | |
894 | * a period of time... | |
895 | */ | |
896 | ||
897 | if (SleepJobs > 0 && current_time >= SleepJobs && | |
898 | cupsArrayCount(PrintingJobs) > 0) | |
899 | { | |
900 | SleepJobs = 0; | |
e60ec91f | 901 | cupsdStopAllJobs(CUPSD_JOB_DEFAULT, 5); |
e6013cfa MS |
902 | } |
903 | #endif /* __APPLE__ */ | |
904 | ||
a4924f6c MS |
905 | #ifndef __APPLE__ |
906 | /* | |
907 | * Update the network interfaces once a minute... | |
908 | */ | |
909 | ||
910 | if ((current_time - netif_time) >= 60) | |
911 | { | |
912 | netif_time = current_time; | |
913 | NetIFUpdate = 1; | |
914 | } | |
915 | #endif /* !__APPLE__ */ | |
916 | ||
1166bf58 | 917 | #ifdef HAVE_ONDEMAND |
a4d04587 | 918 | /* |
1166bf58 MS |
919 | * If no other work was scheduled and we're being controlled by launchd, |
920 | * systemd, or upstart then timeout after 'LaunchdTimeout' seconds of | |
921 | * inactivity... | |
a4d04587 | 922 | */ |
923 | ||
1720786e | 924 | if (!fds && service_idle_exit) |
a4d04587 | 925 | { |
926 | cupsdLogMessage(CUPSD_LOG_INFO, | |
927 | "Printer sharing is off and there are no jobs pending, " | |
928 | "will restart on demand."); | |
929 | stop_scheduler = 1; | |
930 | break; | |
931 | } | |
c187d9ab | 932 | #endif /* HAVE_ONDEMAND */ |
a4d04587 | 933 | |
76cd9e37 MS |
934 | /* |
935 | * Resume listening for new connections as needed... | |
936 | */ | |
937 | ||
938 | if (ListeningPaused && ListeningPaused <= current_time && | |
939 | cupsArrayCount(Clients) < MaxClients) | |
940 | cupsdResumeListening(); | |
941 | ||
ef416fc2 | 942 | /* |
bd7854cb | 943 | * Expire subscriptions and unload completed jobs as needed... |
ef416fc2 | 944 | */ |
945 | ||
bd7854cb | 946 | if (current_time > expire_time) |
ef416fc2 | 947 | { |
bd7854cb | 948 | if (cupsArrayCount(Subscriptions) > 0) |
949 | cupsdExpireSubscriptions(NULL, NULL); | |
950 | ||
951 | cupsdUnloadCompletedJobs(); | |
ef416fc2 | 952 | |
953 | expire_time = current_time; | |
954 | } | |
955 | ||
e1bb4a6e MS |
956 | /* |
957 | * Delete stale local printers... | |
958 | */ | |
959 | ||
960 | if (current_time >= local_timeout) | |
123cfe02 | 961 | { |
e1bb4a6e | 962 | cupsdDeleteTemporaryPrinters(0); |
123cfe02 MS |
963 | local_timeout = 0; |
964 | } | |
e1bb4a6e | 965 | |
07ed0e9a | 966 | #ifndef HAVE_AUTHORIZATION_H |
ef416fc2 | 967 | /* |
f7deaa1a | 968 | * Update the root certificate once every 5 minutes if we have client |
969 | * connections... | |
ef416fc2 | 970 | */ |
971 | ||
f7deaa1a | 972 | if ((current_time - RootCertTime) >= RootCertDuration && RootCertDuration && |
973 | !RunUser && cupsArrayCount(Clients)) | |
974 | { | |
975 | /* | |
976 | * Update the root certificate... | |
977 | */ | |
978 | ||
979 | cupsdDeleteCert(0); | |
0fa6c7fa | 980 | cupsdAddCert(0, "root", cupsdDefaultAuthType()); |
f7deaa1a | 981 | } |
07ed0e9a | 982 | #endif /* !HAVE_AUTHORIZATION_H */ |
ef416fc2 | 983 | |
984 | /* | |
985 | * Check for new data on the client sockets... | |
986 | */ | |
987 | ||
bd7854cb | 988 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
989 | con; | |
990 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 991 | { |
992 | /* | |
f7deaa1a | 993 | * Process pending data in the input buffer... |
ef416fc2 | 994 | */ |
995 | ||
1720786e | 996 | if (httpGetReady(con->http)) |
ef416fc2 | 997 | { |
f7deaa1a | 998 | cupsdReadClient(con); |
999 | continue; | |
ef416fc2 | 1000 | } |
1001 | ||
1002 | /* | |
1003 | * Check the activity and close old clients... | |
1004 | */ | |
1005 | ||
1006 | activity = current_time - Timeout; | |
1720786e | 1007 | if (httpGetActivity(con->http) < activity && !con->pipe_pid) |
ef416fc2 | 1008 | { |
1720786e | 1009 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Closing client %d after %d seconds of inactivity.", con->number, Timeout); |
ef416fc2 | 1010 | |
1011 | cupsdCloseClient(con); | |
ef416fc2 | 1012 | continue; |
1013 | } | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Update any pending multi-file documents... | |
1018 | */ | |
1019 | ||
1020 | if ((current_time - senddoc_time) >= 10) | |
1021 | { | |
1022 | cupsdCheckJobs(); | |
1023 | senddoc_time = current_time; | |
1024 | } | |
1025 | ||
82cc1f9a MS |
1026 | /* |
1027 | * Clean job history... | |
1028 | */ | |
1029 | ||
1030 | if (JobHistoryUpdate && current_time >= JobHistoryUpdate) | |
1031 | cupsdCleanJobs(); | |
1032 | ||
ef416fc2 | 1033 | /* |
5bd77a73 | 1034 | * Log statistics at most once a minute when in debug mode... |
ef416fc2 | 1035 | */ |
1036 | ||
5bd77a73 | 1037 | if ((current_time - report_time) >= 60 && LogLevel >= CUPSD_LOG_DEBUG) |
ef416fc2 | 1038 | { |
5bd77a73 MS |
1039 | size_t string_count, /* String count */ |
1040 | alloc_bytes, /* Allocated string bytes */ | |
1041 | total_bytes; /* Total string bytes */ | |
4400e98d | 1042 | #ifdef HAVE_MALLINFO |
5bd77a73 | 1043 | struct mallinfo mem; /* Malloc information */ |
ef416fc2 | 1044 | |
1045 | ||
1046 | mem = mallinfo(); | |
5bd77a73 MS |
1047 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-arena=%lu", mem.arena); |
1048 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-used=%lu", | |
1049 | mem.usmblks + mem.uordblks); | |
1050 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-free=%lu", | |
ef416fc2 | 1051 | mem.fsmblks + mem.fordblks); |
4400e98d | 1052 | #endif /* HAVE_MALLINFO */ |
1053 | ||
5bd77a73 MS |
1054 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: clients=%d", |
1055 | cupsArrayCount(Clients)); | |
1056 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: jobs=%d", | |
1057 | cupsArrayCount(Jobs)); | |
1058 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: jobs-active=%d", | |
1059 | cupsArrayCount(ActiveJobs)); | |
1060 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: printers=%d", | |
1061 | cupsArrayCount(Printers)); | |
5bd77a73 | 1062 | |
757d2cad | 1063 | string_count = _cupsStrStatistics(&alloc_bytes, &total_bytes); |
5bd77a73 MS |
1064 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1065 | "Report: stringpool-string-count=" CUPS_LLFMT, | |
1066 | CUPS_LLCAST string_count); | |
1067 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1068 | "Report: stringpool-alloc-bytes=" CUPS_LLFMT, | |
1069 | CUPS_LLCAST alloc_bytes); | |
1070 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1071 | "Report: stringpool-total-bytes=" CUPS_LLFMT, | |
4400e98d | 1072 | CUPS_LLCAST total_bytes); |
1073 | ||
5bd77a73 | 1074 | report_time = current_time; |
ef416fc2 | 1075 | } |
ef416fc2 | 1076 | |
fa73b229 | 1077 | /* |
1078 | * Handle OS-specific event notification for any events that have | |
1079 | * accumulated. Don't send these more than once a second... | |
1080 | */ | |
1081 | ||
50fe7201 | 1082 | if (LastEvent && (current_time - event_time) >= 1) |
fa73b229 | 1083 | { |
1084 | #ifdef HAVE_NOTIFY_POST | |
a41f09e2 MS |
1085 | if (LastEvent & (CUPSD_EVENT_PRINTER_ADDED | |
1086 | CUPSD_EVENT_PRINTER_DELETED | | |
1087 | CUPSD_EVENT_PRINTER_MODIFIED)) | |
fa73b229 | 1088 | { |
e00b005a | 1089 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
fa73b229 | 1090 | "notify_post(\"com.apple.printerListChange\")"); |
1091 | notify_post("com.apple.printerListChange"); | |
1092 | } | |
1093 | ||
1094 | if (LastEvent & CUPSD_EVENT_PRINTER_STATE_CHANGED) | |
1095 | { | |
e00b005a | 1096 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
fa73b229 | 1097 | "notify_post(\"com.apple.printerHistoryChange\")"); |
1098 | notify_post("com.apple.printerHistoryChange"); | |
1099 | } | |
1100 | ||
1101 | if (LastEvent & (CUPSD_EVENT_JOB_STATE_CHANGED | | |
1102 | CUPSD_EVENT_JOB_CONFIG_CHANGED | | |
1103 | CUPSD_EVENT_JOB_PROGRESS)) | |
1104 | { | |
e00b005a | 1105 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
fa73b229 | 1106 | "notify_post(\"com.apple.jobChange\")"); |
1107 | notify_post("com.apple.jobChange"); | |
1108 | } | |
1109 | #endif /* HAVE_NOTIFY_POST */ | |
1110 | ||
1111 | /* | |
e53920b9 | 1112 | * Reset the accumulated events... |
fa73b229 | 1113 | */ |
1114 | ||
50fe7201 MS |
1115 | LastEvent = CUPSD_EVENT_NONE; |
1116 | event_time = current_time; | |
fa73b229 | 1117 | } |
ef416fc2 | 1118 | } |
1119 | ||
1120 | /* | |
1121 | * Log a message based on what happened... | |
1122 | */ | |
1123 | ||
1124 | if (stop_scheduler) | |
49d87452 | 1125 | { |
ef416fc2 | 1126 | cupsdLogMessage(CUPSD_LOG_INFO, "Scheduler shutting down normally."); |
49d87452 MS |
1127 | cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED, NULL, NULL, |
1128 | "Scheduler shutting down normally."); | |
1129 | } | |
ef416fc2 | 1130 | else |
49d87452 | 1131 | { |
ef416fc2 | 1132 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1133 | "Scheduler shutting down due to program error."); | |
49d87452 MS |
1134 | cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED, NULL, NULL, |
1135 | "Scheduler shutting down due to program error."); | |
1136 | } | |
ef416fc2 | 1137 | |
1138 | /* | |
7ff4fea9 | 1139 | * Close all network clients... |
ef416fc2 | 1140 | */ |
1141 | ||
7cf5915e MS |
1142 | DoingShutdown = 1; |
1143 | ||
ef416fc2 | 1144 | cupsdStopServer(); |
1145 | ||
7ff4fea9 | 1146 | /* |
03bc3ef4 | 1147 | * Update the KeepAlive/PID file as needed... |
7ff4fea9 MS |
1148 | */ |
1149 | ||
03bc3ef4 | 1150 | service_checkout(1); |
7ff4fea9 MS |
1151 | |
1152 | /* | |
1153 | * Stop all jobs... | |
1154 | */ | |
1155 | ||
bd7854cb | 1156 | cupsdFreeAllJobs(); |
ef416fc2 | 1157 | |
b4974142 MS |
1158 | /* |
1159 | * Delete all temporary printers... | |
1160 | */ | |
1161 | ||
1162 | cupsdDeleteTemporaryPrinters(1); | |
1163 | ||
e1d6a774 | 1164 | #ifdef __APPLE__ |
7ff4fea9 MS |
1165 | /* |
1166 | * Stop monitoring system event monitoring... | |
1167 | */ | |
1168 | ||
1340db2d MS |
1169 | if (use_sysman) |
1170 | cupsdStopSystemMonitor(); | |
e1d6a774 | 1171 | #endif /* __APPLE__ */ |
09ec0018 | 1172 | |
f7deaa1a | 1173 | cupsdStopSelect(); |
ef416fc2 | 1174 | |
1175 | return (!stop_scheduler); | |
1176 | } | |
1177 | ||
1178 | ||
10d09e33 MS |
1179 | /* |
1180 | * 'cupsdAddString()' - Copy and add a string to an array. | |
1181 | */ | |
1182 | ||
1183 | int /* O - 1 on success, 0 on failure */ | |
1184 | cupsdAddString(cups_array_t **a, /* IO - String array */ | |
1185 | const char *s) /* I - String to copy and add */ | |
1186 | { | |
1187 | if (!*a) | |
1188 | *a = cupsArrayNew3((cups_array_func_t)strcmp, NULL, | |
1189 | (cups_ahash_func_t)NULL, 0, | |
5e6c3df7 MS |
1190 | (cups_acopy_func_t)strdup, |
1191 | (cups_afree_func_t)free); | |
10d09e33 MS |
1192 | |
1193 | return (cupsArrayAdd(*a, (char *)s)); | |
1194 | } | |
1195 | ||
1196 | ||
d7871c8c MS |
1197 | /* |
1198 | * 'cupsdCheckProcess()' - Tell the main loop to check for dead children. | |
1199 | */ | |
1200 | ||
1201 | void | |
1202 | cupsdCheckProcess(void) | |
1203 | { | |
1204 | /* | |
1205 | * Flag that we have dead children... | |
1206 | */ | |
1207 | ||
1208 | dead_children = 1; | |
1209 | } | |
1210 | ||
1211 | ||
10d09e33 MS |
1212 | /* |
1213 | * 'cupsdClearString()' - Clear a string. | |
1214 | */ | |
1215 | ||
1216 | void | |
1217 | cupsdClearString(char **s) /* O - String value */ | |
1218 | { | |
1219 | if (s && *s) | |
1220 | { | |
5e6c3df7 | 1221 | free(*s); |
10d09e33 MS |
1222 | *s = NULL; |
1223 | } | |
1224 | } | |
1225 | ||
1226 | ||
10d09e33 MS |
1227 | /* |
1228 | * 'cupsdFreeStrings()' - Free an array of strings. | |
1229 | */ | |
1230 | ||
1231 | void | |
1232 | cupsdFreeStrings(cups_array_t **a) /* IO - String array */ | |
1233 | { | |
1234 | if (*a) | |
1235 | { | |
1236 | cupsArrayDelete(*a); | |
1237 | *a = NULL; | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | ||
1242 | /* | |
1243 | * 'cupsdHoldSignals()' - Hold child and termination signals. | |
1244 | */ | |
1245 | ||
1246 | void | |
1247 | cupsdHoldSignals(void) | |
1248 | { | |
1249 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
1250 | sigset_t newmask; /* New POSIX signal mask */ | |
1251 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
1252 | ||
1253 | ||
1254 | holdcount ++; | |
1255 | if (holdcount > 1) | |
1256 | return; | |
1257 | ||
1258 | #ifdef HAVE_SIGSET | |
1259 | sighold(SIGTERM); | |
1260 | sighold(SIGCHLD); | |
1261 | #elif defined(HAVE_SIGACTION) | |
1262 | sigemptyset(&newmask); | |
1263 | sigaddset(&newmask, SIGTERM); | |
1264 | sigaddset(&newmask, SIGCHLD); | |
1265 | sigprocmask(SIG_BLOCK, &newmask, &holdmask); | |
1266 | #endif /* HAVE_SIGSET */ | |
1267 | } | |
1268 | ||
1269 | ||
ef416fc2 | 1270 | /* |
1271 | * 'cupsdReleaseSignals()' - Release signals for delivery. | |
1272 | */ | |
1273 | ||
1274 | void | |
1275 | cupsdReleaseSignals(void) | |
1276 | { | |
1277 | holdcount --; | |
1278 | if (holdcount > 0) | |
1279 | return; | |
1280 | ||
1281 | #ifdef HAVE_SIGSET | |
1282 | sigrelse(SIGTERM); | |
1283 | sigrelse(SIGCHLD); | |
1284 | #elif defined(HAVE_SIGACTION) | |
1285 | sigprocmask(SIG_SETMASK, &holdmask, NULL); | |
1286 | #endif /* HAVE_SIGSET */ | |
1287 | } | |
1288 | ||
1289 | ||
1290 | /* | |
1291 | * 'cupsdSetString()' - Set a string value. | |
1292 | */ | |
1293 | ||
1294 | void | |
1295 | cupsdSetString(char **s, /* O - New string */ | |
1296 | const char *v) /* I - String value */ | |
1297 | { | |
1298 | if (!s || *s == v) | |
1299 | return; | |
1300 | ||
1301 | if (*s) | |
5e6c3df7 | 1302 | free(*s); |
ef416fc2 | 1303 | |
1304 | if (v) | |
5e6c3df7 | 1305 | *s = strdup(v); |
ef416fc2 | 1306 | else |
1307 | *s = NULL; | |
1308 | } | |
1309 | ||
1310 | ||
1311 | /* | |
1312 | * 'cupsdSetStringf()' - Set a formatted string value. | |
1313 | */ | |
1314 | ||
1315 | void | |
1316 | cupsdSetStringf(char **s, /* O - New string */ | |
1317 | const char *f, /* I - Printf-style format string */ | |
1318 | ...) /* I - Additional args as needed */ | |
1319 | { | |
94436c5a | 1320 | char v[65536 + 64]; /* Formatting string value */ |
ef416fc2 | 1321 | va_list ap; /* Argument pointer */ |
1322 | char *olds; /* Old string */ | |
1323 | ||
1324 | ||
1325 | if (!s) | |
1326 | return; | |
1327 | ||
1328 | olds = *s; | |
1329 | ||
1330 | if (f) | |
1331 | { | |
1332 | va_start(ap, f); | |
1333 | vsnprintf(v, sizeof(v), f, ap); | |
1334 | va_end(ap); | |
1335 | ||
5e6c3df7 | 1336 | *s = strdup(v); |
ef416fc2 | 1337 | } |
1338 | else | |
1339 | *s = NULL; | |
1340 | ||
1341 | if (olds) | |
5e6c3df7 | 1342 | free(olds); |
ef416fc2 | 1343 | } |
1344 | ||
1345 | ||
1346 | /* | |
1347 | * 'parent_handler()' - Catch USR1/CHLD signals... | |
1348 | */ | |
1349 | ||
1350 | static void | |
1351 | parent_handler(int sig) /* I - Signal */ | |
1352 | { | |
1353 | /* | |
1354 | * Store the signal we got from the OS and return... | |
1355 | */ | |
1356 | ||
1357 | parent_signal = sig; | |
1358 | } | |
1359 | ||
1360 | ||
1361 | /* | |
1362 | * 'process_children()' - Process all dead children... | |
1363 | */ | |
1364 | ||
1365 | static void | |
1366 | process_children(void) | |
1367 | { | |
1368 | int status; /* Exit status of child */ | |
b9faaae1 MS |
1369 | int pid, /* Process ID of child */ |
1370 | job_id; /* Job ID of child */ | |
ef416fc2 | 1371 | cupsd_job_t *job; /* Current job */ |
1372 | int i; /* Looping var */ | |
e00b005a | 1373 | char name[1024]; /* Process name */ |
dcb445bc | 1374 | const char *type; /* Type of program */ |
ef416fc2 | 1375 | |
1376 | ||
1377 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_children()"); | |
1378 | ||
1379 | /* | |
1380 | * Reset the dead_children flag... | |
1381 | */ | |
1382 | ||
1383 | dead_children = 0; | |
1384 | ||
1385 | /* | |
1386 | * Collect the exit status of some children... | |
1387 | */ | |
1388 | ||
1389 | #ifdef HAVE_WAITPID | |
1390 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) | |
1391 | #elif defined(HAVE_WAIT3) | |
1392 | while ((pid = wait3(&status, WNOHANG, NULL)) > 0) | |
1393 | #else | |
1394 | if ((pid = wait(&status)) > 0) | |
1395 | #endif /* HAVE_WAITPID */ | |
1396 | { | |
ef416fc2 | 1397 | /* |
745129be | 1398 | * Collect the name of the process that finished... |
ef416fc2 | 1399 | */ |
1400 | ||
b9faaae1 | 1401 | cupsdFinishProcess(pid, name, sizeof(name), &job_id); |
e00b005a | 1402 | |
ef416fc2 | 1403 | /* |
1404 | * Delete certificates for CGI processes... | |
1405 | */ | |
1406 | ||
1407 | if (pid) | |
1408 | cupsdDeleteCert(pid); | |
1409 | ||
1410 | /* | |
b9faaae1 | 1411 | * Handle completed job filters... |
ef416fc2 | 1412 | */ |
1413 | ||
dcb445bc MS |
1414 | if (job_id > 0) |
1415 | job = cupsdFindJob(job_id); | |
1416 | else | |
1417 | job = NULL; | |
1418 | ||
1419 | if (job) | |
b9faaae1 MS |
1420 | { |
1421 | for (i = 0; job->filters[i]; i ++) | |
1422 | if (job->filters[i] == pid) | |
1423 | break; | |
1424 | ||
1425 | if (job->filters[i] || job->backend == pid) | |
ef416fc2 | 1426 | { |
b9faaae1 MS |
1427 | /* |
1428 | * OK, this process has gone away; what's left? | |
1429 | */ | |
1430 | ||
1431 | if (job->filters[i]) | |
dcb445bc | 1432 | { |
b9faaae1 | 1433 | job->filters[i] = -pid; |
dcb445bc MS |
1434 | type = "Filter"; |
1435 | } | |
b9faaae1 | 1436 | else |
dcb445bc | 1437 | { |
b9faaae1 | 1438 | job->backend = -pid; |
dcb445bc MS |
1439 | type = "Backend"; |
1440 | } | |
ef416fc2 | 1441 | |
b9faaae1 | 1442 | if (status && status != SIGTERM && status != SIGKILL && |
f3c17241 | 1443 | status != SIGPIPE) |
ef416fc2 | 1444 | { |
1445 | /* | |
b9faaae1 MS |
1446 | * An error occurred; save the exit status so we know to stop |
1447 | * the printer or cancel the job when all of the filters finish... | |
1448 | * | |
1449 | * A negative status indicates that the backend failed and the | |
1450 | * printer needs to be stopped. | |
f3c17241 MS |
1451 | * |
1452 | * In order to preserve the most serious status, we always log | |
1453 | * when a process dies due to a signal (e.g. SIGABRT, SIGSEGV, | |
1454 | * and SIGBUS) and prefer to log the backend exit status over a | |
1455 | * filter's. | |
ef416fc2 | 1456 | */ |
1457 | ||
f3c17241 MS |
1458 | int old_status = abs(job->status); |
1459 | ||
1460 | if (WIFSIGNALED(status) || /* This process crashed, or */ | |
1461 | !job->status || /* No process had a status, or */ | |
1462 | (!job->filters[i] && WIFEXITED(old_status))) | |
1463 | { /* Backend and filter didn't crash */ | |
1464 | if (job->filters[i]) | |
1465 | job->status = status; /* Filter failed */ | |
1466 | else | |
1467 | job->status = -status; /* Backend failed */ | |
1468 | } | |
ef416fc2 | 1469 | |
f11a948a | 1470 | if (job->state_value == IPP_JOB_PROCESSING && |
dcb445bc MS |
1471 | job->status_level > CUPSD_LOG_ERROR && |
1472 | (job->filters[i] || !WIFEXITED(status))) | |
ef416fc2 | 1473 | { |
f11a948a MS |
1474 | char message[1024]; /* New printer-state-message */ |
1475 | ||
1476 | ||
b9faaae1 | 1477 | job->status_level = CUPSD_LOG_ERROR; |
ef416fc2 | 1478 | |
dcb445bc | 1479 | snprintf(message, sizeof(message), "%s failed", type); |
f11a948a | 1480 | |
e07d4801 MS |
1481 | if (job->printer) |
1482 | { | |
f11a948a MS |
1483 | strlcpy(job->printer->state_message, message, |
1484 | sizeof(job->printer->state_message)); | |
e07d4801 MS |
1485 | } |
1486 | ||
1487 | if (!job->attrs) | |
1488 | cupsdLoadJob(job); | |
e00b005a | 1489 | |
e07d4801 | 1490 | if (!job->printer_message && job->attrs) |
e00b005a | 1491 | { |
b9faaae1 MS |
1492 | if ((job->printer_message = |
1493 | ippFindAttribute(job->attrs, "job-printer-state-message", | |
1494 | IPP_TAG_TEXT)) == NULL) | |
1495 | job->printer_message = ippAddString(job->attrs, IPP_TAG_JOB, | |
1496 | IPP_TAG_TEXT, | |
1497 | "job-printer-state-message", | |
5a662dc0 | 1498 | NULL, NULL); |
e00b005a | 1499 | } |
b9faaae1 | 1500 | |
e07d4801 | 1501 | if (job->printer_message) |
5e6c3df7 | 1502 | ippSetString(job->attrs, &job->printer_message, 0, message); |
ef416fc2 | 1503 | } |
b9faaae1 | 1504 | } |
ef416fc2 | 1505 | |
b9faaae1 MS |
1506 | /* |
1507 | * If this is not the last file in a job, see if all of the | |
1508 | * filters are done, and if so move to the next file. | |
1509 | */ | |
ef416fc2 | 1510 | |
5a4c3fcf MS |
1511 | if (job->state_value >= IPP_JOB_CANCELED) |
1512 | { | |
1513 | /* | |
1514 | * Remove the job from the active list if there are no processes still | |
1515 | * running for it... | |
1516 | */ | |
1517 | ||
1518 | for (i = 0; job->filters[i] < 0; i++); | |
1519 | ||
1520 | if (!job->filters[i] && job->backend <= 0) | |
1521 | cupsArrayRemove(ActiveJobs, job); | |
1522 | } | |
1523 | else if (job->current_file < job->num_files && job->printer) | |
b9faaae1 MS |
1524 | { |
1525 | for (i = 0; job->filters[i] < 0; i ++); | |
ef416fc2 | 1526 | |
82f97232 MS |
1527 | if (!job->filters[i] && |
1528 | (!job->printer->pc || !job->printer->pc->single_file || | |
1529 | job->backend <= 0)) | |
b9faaae1 MS |
1530 | { |
1531 | /* | |
1532 | * Process the next file... | |
1533 | */ | |
ef416fc2 | 1534 | |
b9faaae1 | 1535 | cupsdContinueJob(job); |
ef416fc2 | 1536 | } |
ef416fc2 | 1537 | } |
1538 | } | |
b9faaae1 | 1539 | } |
3dfe78b3 MS |
1540 | |
1541 | /* | |
745129be MS |
1542 | * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors |
1543 | * since they come when we kill/end a process... | |
3dfe78b3 MS |
1544 | */ |
1545 | ||
745129be MS |
1546 | if (status == SIGTERM || status == SIGKILL) |
1547 | { | |
dcb445bc MS |
1548 | cupsdLogJob(job, CUPSD_LOG_DEBUG, |
1549 | "PID %d (%s) was terminated normally with signal %d.", pid, | |
1550 | name, status); | |
745129be | 1551 | } |
5a6b583a MS |
1552 | else if (status == SIGPIPE) |
1553 | { | |
dcb445bc MS |
1554 | cupsdLogJob(job, CUPSD_LOG_DEBUG, |
1555 | "PID %d (%s) did not catch or ignore signal %d.", pid, name, | |
1556 | status); | |
5a6b583a | 1557 | } |
745129be | 1558 | else if (status) |
3dfe78b3 MS |
1559 | { |
1560 | if (WIFEXITED(status)) | |
88f9aafc MS |
1561 | { |
1562 | int code = WEXITSTATUS(status); /* Exit code */ | |
1563 | ||
1564 | if (code > 100) | |
dcb445bc MS |
1565 | cupsdLogJob(job, CUPSD_LOG_DEBUG, |
1566 | "PID %d (%s) stopped with status %d (%s)", pid, name, | |
1567 | code, strerror(code - 100)); | |
88f9aafc | 1568 | else |
dcb445bc MS |
1569 | cupsdLogJob(job, CUPSD_LOG_DEBUG, |
1570 | "PID %d (%s) stopped with status %d.", pid, name, code); | |
88f9aafc | 1571 | } |
3dfe78b3 | 1572 | else |
dcb445bc MS |
1573 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "PID %d (%s) crashed on signal %d.", |
1574 | pid, name, WTERMSIG(status)); | |
3dfe78b3 MS |
1575 | |
1576 | if (LogLevel < CUPSD_LOG_DEBUG) | |
dcb445bc MS |
1577 | cupsdLogJob(job, CUPSD_LOG_INFO, |
1578 | "Hint: Try setting the LogLevel to \"debug\" to find out " | |
1579 | "more."); | |
3dfe78b3 MS |
1580 | } |
1581 | else | |
dcb445bc MS |
1582 | cupsdLogJob(job, CUPSD_LOG_DEBUG, "PID %d (%s) exited with no errors.", |
1583 | pid, name); | |
ef416fc2 | 1584 | } |
dfd5680b MS |
1585 | |
1586 | /* | |
1587 | * If wait*() is interrupted by a signal, tell main() to call us again... | |
1588 | */ | |
1589 | ||
1590 | if (pid < 0 && errno == EINTR) | |
1591 | dead_children = 1; | |
ef416fc2 | 1592 | } |
1593 | ||
1594 | ||
ef416fc2 | 1595 | /* |
1596 | * 'select_timeout()' - Calculate the select timeout value. | |
1597 | * | |
1598 | */ | |
1599 | ||
1600 | static long /* O - Number of seconds */ | |
bd7854cb | 1601 | select_timeout(int fds) /* I - Number of descriptors returned */ |
ef416fc2 | 1602 | { |
ef416fc2 | 1603 | long timeout; /* Timeout for select */ |
1604 | time_t now; /* Current time */ | |
1605 | cupsd_client_t *con; /* Client information */ | |
ef416fc2 | 1606 | cupsd_job_t *job; /* Job information */ |
e1bb4a6e | 1607 | cupsd_printer_t *printer; /* Printer information */ |
ef416fc2 | 1608 | const char *why; /* Debugging aid */ |
1609 | ||
1610 | ||
82cc1f9a MS |
1611 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: JobHistoryUpdate=%ld", |
1612 | (long)JobHistoryUpdate); | |
1613 | ||
ef416fc2 | 1614 | /* |
1615 | * Check to see if any of the clients have pending data to be | |
1616 | * processed; if so, the timeout should be 0... | |
1617 | */ | |
1618 | ||
bd7854cb | 1619 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
1620 | con; | |
1621 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
1720786e | 1622 | if (httpGetReady(con->http)) |
ef416fc2 | 1623 | return (0); |
1624 | ||
1625 | /* | |
f7deaa1a | 1626 | * If select has been active in the last second (fds > 0) or we have |
ef416fc2 | 1627 | * many resources in use then don't bother trying to optimize the |
1628 | * timeout, just make it 1 second. | |
1629 | */ | |
1630 | ||
f7deaa1a | 1631 | if (fds > 0 || cupsArrayCount(Clients) > 50) |
ef416fc2 | 1632 | return (1); |
1633 | ||
1634 | /* | |
1635 | * Otherwise, check all of the possible events that we need to wake for... | |
1636 | */ | |
1637 | ||
1638 | now = time(NULL); | |
1639 | timeout = now + 86400; /* 86400 == 1 day */ | |
1640 | why = "do nothing"; | |
1641 | ||
e6013cfa MS |
1642 | #ifdef __APPLE__ |
1643 | /* | |
b4974142 | 1644 | * When going to sleep, wake up to abort jobs that don't complete in time. |
e6013cfa MS |
1645 | */ |
1646 | ||
1647 | if (SleepJobs > 0 && SleepJobs < timeout) | |
1648 | { | |
1649 | timeout = SleepJobs; | |
b4974142 | 1650 | why = "abort jobs before sleeping"; |
e6013cfa MS |
1651 | } |
1652 | #endif /* __APPLE__ */ | |
1653 | ||
76cd9e37 MS |
1654 | /* |
1655 | * Check whether we are accepting new connections... | |
1656 | */ | |
1657 | ||
1658 | if (ListeningPaused > 0 && cupsArrayCount(Clients) < MaxClients && | |
1659 | ListeningPaused < timeout) | |
1660 | { | |
1661 | if (ListeningPaused <= now) | |
1662 | timeout = now; | |
1663 | else | |
1664 | timeout = ListeningPaused; | |
1665 | ||
1666 | why = "resume listening"; | |
1667 | } | |
1668 | ||
ef416fc2 | 1669 | /* |
1670 | * Check the activity and close old clients... | |
1671 | */ | |
1672 | ||
bd7854cb | 1673 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
1674 | con; | |
1675 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
1720786e | 1676 | if ((httpGetActivity(con->http) + Timeout) < timeout) |
ef416fc2 | 1677 | { |
1720786e | 1678 | timeout = httpGetActivity(con->http) + Timeout; |
ef416fc2 | 1679 | why = "timeout a client connection"; |
1680 | } | |
1681 | ||
3dfe78b3 | 1682 | /* |
bf3816c7 | 1683 | * Write out changes to configuration and state files... |
3dfe78b3 MS |
1684 | */ |
1685 | ||
1686 | if (DirtyCleanTime && timeout > DirtyCleanTime) | |
1687 | { | |
1688 | timeout = DirtyCleanTime; | |
1689 | why = "write dirty config/state files"; | |
1690 | } | |
1691 | ||
ef416fc2 | 1692 | /* |
82cc1f9a | 1693 | * Check for any job activity... |
ef416fc2 | 1694 | */ |
1695 | ||
82cc1f9a MS |
1696 | if (JobHistoryUpdate && timeout > JobHistoryUpdate) |
1697 | { | |
1698 | timeout = JobHistoryUpdate; | |
1699 | why = "update job history"; | |
1700 | } | |
1701 | ||
238c3832 MS |
1702 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
1703 | job; | |
1704 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
ef416fc2 | 1705 | { |
82cc1f9a MS |
1706 | if (job->cancel_time && job->cancel_time < timeout) |
1707 | { | |
1708 | timeout = job->cancel_time; | |
1709 | why = "cancel stuck jobs"; | |
1710 | } | |
1711 | ||
238c3832 MS |
1712 | if (job->kill_time && job->kill_time < timeout) |
1713 | { | |
1714 | timeout = job->kill_time; | |
1715 | why = "kill unresponsive jobs"; | |
1716 | } | |
82cc1f9a MS |
1717 | |
1718 | if (job->state_value == IPP_JOB_HELD && job->hold_until < timeout) | |
238c3832 MS |
1719 | { |
1720 | timeout = job->hold_until; | |
1721 | why = "release held jobs"; | |
1722 | } | |
82cc1f9a MS |
1723 | |
1724 | if (job->state_value == IPP_JOB_PENDING && timeout > (now + 10)) | |
238c3832 MS |
1725 | { |
1726 | timeout = now + 10; | |
1727 | why = "start pending jobs"; | |
1728 | break; | |
1729 | } | |
ef416fc2 | 1730 | } |
1731 | ||
e1bb4a6e MS |
1732 | /* |
1733 | * Check for temporary printers that need to be deleted... | |
1734 | */ | |
1735 | ||
1736 | for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers); printer; printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1737 | { | |
123cfe02 | 1738 | if (printer->temporary && !printer->job && (!local_timeout || local_timeout > (printer->state_time + 60))) |
e1bb4a6e MS |
1739 | local_timeout = printer->state_time + 60; |
1740 | } | |
1741 | ||
123cfe02 | 1742 | if (timeout > local_timeout && local_timeout) |
e1bb4a6e MS |
1743 | { |
1744 | timeout = local_timeout; | |
1745 | why = "delete stale local printers"; | |
1746 | } | |
1747 | ||
ef416fc2 | 1748 | /* |
dcb445bc MS |
1749 | * Adjust from absolute to relative time. We add 1 second to the timeout since |
1750 | * events occur after the timeout expires, and limit the timeout to 86400 | |
1751 | * seconds (1 day) to avoid select() timeout limits present on some operating | |
ef416fc2 | 1752 | * systems... |
1753 | */ | |
1754 | ||
1755 | timeout = timeout - now + 1; | |
1756 | ||
1757 | if (timeout < 1) | |
1758 | timeout = 1; | |
1759 | else if (timeout > 86400) | |
1760 | timeout = 86400; | |
1761 | ||
1762 | /* | |
1763 | * Log and return the timeout value... | |
1764 | */ | |
1765 | ||
f7deaa1a | 1766 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout(%d): %ld seconds to %s", |
1767 | fds, timeout, why); | |
ef416fc2 | 1768 | |
1769 | return (timeout); | |
1770 | } | |
1771 | ||
1772 | ||
e6013cfa MS |
1773 | /* |
1774 | * 'sigchld_handler()' - Handle 'child' signals from old processes. | |
1775 | */ | |
1776 | ||
1777 | static void | |
1778 | sigchld_handler(int sig) /* I - Signal number */ | |
1779 | { | |
1780 | (void)sig; | |
1781 | ||
1782 | /* | |
1783 | * Flag that we have dead children... | |
1784 | */ | |
1785 | ||
1786 | dead_children = 1; | |
1787 | ||
1788 | /* | |
1789 | * Reset the signal handler as needed... | |
1790 | */ | |
1791 | ||
1792 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1793 | signal(SIGCLD, sigchld_handler); | |
1794 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1795 | } | |
1796 | ||
1797 | ||
1798 | /* | |
1799 | * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler. | |
1800 | */ | |
1801 | ||
1802 | static void | |
1803 | sighup_handler(int sig) /* I - Signal number */ | |
1804 | { | |
1805 | (void)sig; | |
1806 | ||
1807 | NeedReload = RELOAD_ALL; | |
1808 | ReloadTime = time(NULL); | |
1809 | ||
1810 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1811 | signal(SIGHUP, sighup_handler); | |
1812 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1813 | } | |
1814 | ||
1815 | ||
1816 | /* | |
1817 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler. | |
1818 | */ | |
1819 | ||
1820 | static void | |
1821 | sigterm_handler(int sig) /* I - Signal number */ | |
1822 | { | |
1823 | (void)sig; /* remove compiler warnings... */ | |
1824 | ||
1825 | /* | |
1826 | * Flag that we should stop and return... | |
1827 | */ | |
1828 | ||
1829 | stop_scheduler = 1; | |
1830 | } | |
1831 | ||
1832 | ||
1166bf58 | 1833 | #ifdef HAVE_ONDEMAND |
26c14fa6 | 1834 | /* |
1166bf58 | 1835 | * 'service_add_listener()' - Bind an open fd as a Listener. |
26c14fa6 BN |
1836 | */ |
1837 | ||
1838 | static void | |
1166bf58 MS |
1839 | service_add_listener(int fd, /* I - Socket file descriptor */ |
1840 | int idx) /* I - Listener number, for logging */ | |
26c14fa6 BN |
1841 | { |
1842 | cupsd_listener_t *lis; /* Listeners array */ | |
1843 | http_addr_t addr; /* Address variable */ | |
1844 | socklen_t addrlen; /* Length of address */ | |
1845 | char s[256]; /* String addresss */ | |
1846 | ||
1166bf58 | 1847 | |
26c14fa6 BN |
1848 | addrlen = sizeof(addr); |
1849 | ||
1850 | if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) | |
1851 | { | |
1166bf58 | 1852 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_add_listener: Unable to get local address for listener #%d: %s", idx + 1, strerror(errno)); |
26c14fa6 BN |
1853 | return; |
1854 | } | |
1855 | ||
1166bf58 | 1856 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_add_listener: Listener #%d at fd %d, \"%s\".", idx + 1, fd, httpAddrString(&addr, s, sizeof(s))); |
26c14fa6 | 1857 | |
1166bf58 MS |
1858 | /* |
1859 | * Try to match the on-demand socket address to one of the listeners... | |
1860 | */ | |
26c14fa6 BN |
1861 | |
1862 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); | |
1863 | lis; | |
1864 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
1865 | if (httpAddrEqual(&lis->address, &addr)) | |
1866 | break; | |
1867 | ||
1868 | /* | |
1869 | * Add a new listener If there's no match... | |
1870 | */ | |
1871 | ||
1872 | if (lis) | |
1873 | { | |
1166bf58 | 1874 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_add_listener: Matched existing listener #%d to %s.", idx + 1, httpAddrString(&(lis->address), s, sizeof(s))); |
26c14fa6 BN |
1875 | } |
1876 | else | |
1877 | { | |
1166bf58 | 1878 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_add_listener: Adding new listener #%d for %s.", idx + 1, httpAddrString(&addr, s, sizeof(s))); |
26c14fa6 BN |
1879 | |
1880 | if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) | |
1881 | { | |
1166bf58 | 1882 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_add_listener: Unable to allocate listener: %s.", strerror(errno)); |
26c14fa6 BN |
1883 | exit(EXIT_FAILURE); |
1884 | return; | |
1885 | } | |
1886 | ||
1887 | cupsArrayAdd(Listeners, lis); | |
1888 | ||
1889 | memcpy(&lis->address, &addr, sizeof(lis->address)); | |
1890 | } | |
1891 | ||
1892 | lis->fd = fd; | |
1893 | lis->on_demand = 1; | |
1894 | ||
1895 | # ifdef HAVE_SSL | |
1896 | if (httpAddrPort(&(lis->address)) == 443) | |
1897 | lis->encryption = HTTP_ENCRYPT_ALWAYS; | |
1898 | # endif /* HAVE_SSL */ | |
1899 | } | |
03bc3ef4 | 1900 | #endif /* HAVE_ONDEMAND */ |
26c14fa6 | 1901 | |
1166bf58 | 1902 | |
1720786e MS |
1903 | /* |
1904 | * 'service_checkin()' - Check-in with launchd and collect the listening fds. | |
1905 | */ | |
1906 | ||
1907 | static void | |
1908 | service_checkin(void) | |
1909 | { | |
1720786e MS |
1910 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: pid=%d", (int)getpid()); |
1911 | ||
03bc3ef4 MS |
1912 | #ifdef HAVE_LAUNCHD |
1913 | if (OnDemand) | |
1720786e | 1914 | { |
03bc3ef4 MS |
1915 | int error; /* Check-in error, if any */ |
1916 | size_t i, /* Looping var */ | |
1917 | count; /* Number of listeners */ | |
1918 | int *ld_sockets; /* Listener sockets */ | |
1720786e | 1919 | |
03bc3ef4 MS |
1920 | /* |
1921 | * Check-in with launchd... | |
1922 | */ | |
117466cd | 1923 | |
03bc3ef4 MS |
1924 | if ((error = launch_activate_socket("Listeners", &ld_sockets, &count)) != 0) |
1925 | { | |
1926 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(error)); | |
1927 | exit(EXIT_FAILURE); | |
1928 | return; /* anti-compiler-warning */ | |
1929 | } | |
1720786e | 1930 | |
03bc3ef4 MS |
1931 | /* |
1932 | * Try to match the launchd sockets to the cupsd listeners... | |
1933 | */ | |
1720786e | 1934 | |
03bc3ef4 | 1935 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", (int)count); |
1720786e | 1936 | |
03bc3ef4 MS |
1937 | for (i = 0; i < count; i ++) |
1938 | service_add_listener(ld_sockets[i], (int)i); | |
1720786e | 1939 | |
03bc3ef4 | 1940 | free(ld_sockets); |
1720786e MS |
1941 | } |
1942 | ||
03bc3ef4 MS |
1943 | #elif defined(HAVE_SYSTEMD) |
1944 | if (OnDemand) | |
1945 | { | |
1946 | int i, /* Looping var */ | |
1947 | count; /* Number of listeners */ | |
1720786e | 1948 | |
03bc3ef4 MS |
1949 | /* |
1950 | * Check-in with systemd... | |
1951 | */ | |
1720786e | 1952 | |
03bc3ef4 MS |
1953 | if ((count = sd_listen_fds(0)) < 0) |
1954 | { | |
1955 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets: %s", strerror(-count)); | |
1956 | exit(EXIT_FAILURE); | |
1957 | return; /* anti-compiler-warning */ | |
1958 | } | |
1166bf58 | 1959 | |
03bc3ef4 MS |
1960 | /* |
1961 | * Try to match the systemd sockets to the cupsd listeners... | |
1962 | */ | |
1720786e | 1963 | |
03bc3ef4 | 1964 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: %d listeners.", count); |
1166bf58 | 1965 | |
03bc3ef4 MS |
1966 | for (i = 0; i < count; i ++) |
1967 | service_add_listener(SD_LISTEN_FDS_START + i, i); | |
441de8b2 | 1968 | } |
1720786e | 1969 | |
03bc3ef4 MS |
1970 | #elif defined(HAVE_UPSTART) |
1971 | if (OnDemand) | |
441de8b2 | 1972 | { |
03bc3ef4 MS |
1973 | const char *e; /* Environment var */ |
1974 | int fd; /* File descriptor */ | |
1720786e | 1975 | |
1720786e | 1976 | |
03bc3ef4 MS |
1977 | if (!(e = getenv("UPSTART_EVENTS"))) |
1978 | { | |
1979 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get started via Upstart."); | |
1980 | exit(EXIT_FAILURE); | |
1981 | return; | |
1982 | } | |
1720786e | 1983 | |
03bc3ef4 MS |
1984 | if (strcasecmp(e, "socket")) |
1985 | { | |
1986 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: We did not get triggered via an Upstart socket event."); | |
1987 | exit(EXIT_FAILURE); | |
1988 | return; | |
1989 | } | |
1720786e | 1990 | |
03bc3ef4 MS |
1991 | if ((e = getenv("UPSTART_FDS")) == NULL) |
1992 | { | |
1993 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Unable to get listener sockets from UPSTART_FDS."); | |
1994 | exit(EXIT_FAILURE); | |
1995 | return; | |
1996 | } | |
1166bf58 | 1997 | |
03bc3ef4 | 1998 | cupsdLogMessage(CUPSD_LOG_DEBUG, "service_checkin: UPSTART_FDS=%s", e); |
1720786e | 1999 | |
03bc3ef4 MS |
2000 | fd = (int)strtol(e, NULL, 10); |
2001 | if (fd < 0) | |
2002 | { | |
2003 | cupsdLogMessage(CUPSD_LOG_ERROR, "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno)); | |
2004 | exit(EXIT_FAILURE); | |
2005 | return; | |
2006 | } | |
2007 | ||
2008 | /* | |
2009 | * Upstart only supportst a single on-demand socket file descriptor... | |
2010 | */ | |
2011 | ||
2012 | service_add_listener(fd, 0); | |
2013 | } | |
2014 | #endif /* HAVE_LAUNCHD */ | |
1720786e MS |
2015 | } |
2016 | ||
2017 | ||
2018 | /* | |
03bc3ef4 | 2019 | * 'service_checkout()' - Update the KeepAlive/PID file as needed. |
1720786e MS |
2020 | */ |
2021 | ||
2022 | static void | |
03bc3ef4 | 2023 | service_checkout(int shutdown) /* I - Shutting down? */ |
1720786e | 2024 | { |
e9b2d701 MS |
2025 | cups_file_t *fp; /* File */ |
2026 | char pidfile[1024]; /* PID/KeepAlive file */ | |
03bc3ef4 MS |
2027 | |
2028 | ||
2029 | /* | |
2030 | * When running on-demand, use the KeepAlive file, otherwise write a PID file | |
2031 | * to StateDir... | |
2032 | */ | |
1720786e | 2033 | |
03bc3ef4 MS |
2034 | #ifdef HAVE_ONDEMAND |
2035 | if (OnDemand) | |
e9b2d701 | 2036 | { |
03bc3ef4 | 2037 | strlcpy(pidfile, CUPS_KEEPALIVE, sizeof(pidfile)); |
e9b2d701 MS |
2038 | |
2039 | if (cupsArrayCount(ActiveJobs) || /* Active jobs */ | |
2040 | WebInterface || /* Web interface enabled */ | |
2041 | NeedReload || /* Doing a reload */ | |
2042 | (Browsing && BrowseLocalProtocols && cupsArrayCount(Printers))) | |
2043 | /* Printers being shared */ | |
2044 | { | |
2045 | /* | |
2046 | * Create or remove the "keep-alive" file based on whether there are active | |
2047 | * jobs or shared printers to advertise... | |
2048 | */ | |
2049 | ||
2050 | shutdown = 0; | |
2051 | } | |
2052 | } | |
03bc3ef4 MS |
2053 | else |
2054 | #endif /* HAVE_ONDEMAND */ | |
2055 | snprintf(pidfile, sizeof(pidfile), "%s/cupsd.pid", StateDir); | |
1720786e | 2056 | |
e9b2d701 MS |
2057 | if (shutdown) |
2058 | { | |
2059 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Removing KeepAlive/PID file \"%s\".", pidfile); | |
1720786e | 2060 | |
e9b2d701 MS |
2061 | unlink(pidfile); |
2062 | } | |
2063 | else | |
1720786e | 2064 | { |
03bc3ef4 | 2065 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Creating KeepAlive/PID file \"%s\".", pidfile); |
1720786e | 2066 | |
03bc3ef4 MS |
2067 | if ((fp = cupsFileOpen(pidfile, "w")) != NULL) |
2068 | { | |
2069 | /* | |
2070 | * Save the PID in the file... | |
2071 | */ | |
2072 | ||
2073 | cupsFilePrintf(fp, "%d\n", (int)getpid()); | |
2074 | cupsFileClose(fp); | |
2075 | } | |
2076 | else | |
2077 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create KeepAlive/PID file \"%s\": %s", pidfile, strerror(errno)); | |
1720786e | 2078 | } |
1720786e | 2079 | } |
1720786e MS |
2080 | |
2081 | ||
ef416fc2 | 2082 | /* |
2083 | * 'usage()' - Show scheduler usage. | |
2084 | */ | |
2085 | ||
2086 | static void | |
a4d04587 | 2087 | usage(int status) /* O - Exit status */ |
ef416fc2 | 2088 | { |
0837b7e8 MS |
2089 | FILE *fp = status ? stderr : stdout; /* Output file */ |
2090 | ||
2091 | ||
2092 | _cupsLangPuts(fp, _("Usage: cupsd [options]")); | |
2093 | _cupsLangPuts(fp, _("Options:")); | |
f3c17241 | 2094 | _cupsLangPuts(fp, _(" -c cupsd.conf Set cupsd.conf file to use.")); |
84315f46 | 2095 | _cupsLangPuts(fp, _(" -f Run in the foreground.")); |
3b433010 | 2096 | _cupsLangPuts(fp, _(" -F Run in the foreground but detach from console.")); |
84315f46 | 2097 | _cupsLangPuts(fp, _(" -h Show this usage message.")); |
1166bf58 | 2098 | #ifdef HAVE_ONDEMAND |
1720786e | 2099 | _cupsLangPuts(fp, _(" -l Run cupsd on demand.")); |
1166bf58 | 2100 | #endif /* HAVE_ONDEMAND */ |
3b433010 MS |
2101 | _cupsLangPuts(fp, _(" -s cups-files.conf Set cups-files.conf file to use.")); |
2102 | _cupsLangPuts(fp, _(" -t Test the configuration file.")); | |
0837b7e8 | 2103 | |
a4d04587 | 2104 | exit(status); |
ef416fc2 | 2105 | } |