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