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