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