]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: main.c 6914 2007-09-05 21:05:04Z mike $" |
ef416fc2 | 3 | * |
4 | * Scheduler main loop for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 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... | |
29 | * sigchld_handler() - Handle 'child' signals from old processes. | |
30 | * sighup_handler() - Handle 'hangup' signals to reconfigure the | |
31 | * scheduler. | |
32 | * sigterm_handler() - Handle 'terminate' signals that stop the | |
33 | * scheduler. | |
34 | * select_timeout() - Calculate the select timeout value. | |
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 | ||
492 | if (cupsdRemoveFile(tempfile)) | |
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(); | |
517 | } | |
518 | #endif /* HAVE_LAUNCHD */ | |
519 | ||
7594b224 | 520 | #if defined(__APPLE__) && defined(HAVE_DLFCN_H) |
521 | /* | |
522 | * Load Print Service quota enforcement library (X Server only) | |
523 | */ | |
524 | ||
525 | PSQLibRef = dlopen(PSQLibPath, RTLD_LAZY); | |
526 | ||
527 | if (PSQLibRef) | |
528 | PSQUpdateQuotaProc = dlsym(PSQLibRef, PSQLibFuncName); | |
529 | #endif /* __APPLE__ && HAVE_DLFCN_H */ | |
530 | ||
c24d2134 MS |
531 | #ifdef HAVE_GSSAPI |
532 | # ifdef __APPLE__ | |
533 | /* | |
534 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
535 | * to use it... | |
536 | */ | |
537 | ||
538 | if (krb5_init_context != NULL) | |
539 | # endif /* __APPLE__ */ | |
540 | ||
541 | /* | |
542 | * Setup a Kerberos context for the scheduler to use... | |
543 | */ | |
544 | ||
545 | if (krb5_init_context(&KerberosContext)) | |
a41f09e2 MS |
546 | { |
547 | KerberosContext = NULL; | |
548 | ||
c24d2134 | 549 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize Kerberos context"); |
a41f09e2 | 550 | } |
c24d2134 MS |
551 | #endif /* HAVE_GSSAPI */ |
552 | ||
a4d04587 | 553 | /* |
554 | * Startup the server... | |
555 | */ | |
556 | ||
557 | cupsdStartServer(); | |
558 | ||
ef416fc2 | 559 | /* |
560 | * Catch hangup and child signals and ignore broken pipes... | |
561 | */ | |
562 | ||
563 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
e1d6a774 | 564 | sigset(SIGCHLD, sigchld_handler); |
4744bd90 | 565 | sigset(SIGHUP, sighup_handler); |
ef416fc2 | 566 | sigset(SIGPIPE, SIG_IGN); |
567 | sigset(SIGTERM, sigterm_handler); | |
568 | #elif defined(HAVE_SIGACTION) | |
569 | memset(&action, 0, sizeof(action)); | |
570 | ||
e1d6a774 | 571 | sigemptyset(&action.sa_mask); |
572 | sigaddset(&action.sa_mask, SIGTERM); | |
573 | sigaddset(&action.sa_mask, SIGCHLD); | |
574 | action.sa_handler = sigchld_handler; | |
575 | sigaction(SIGCHLD, &action, NULL); | |
576 | ||
ef416fc2 | 577 | sigemptyset(&action.sa_mask); |
578 | sigaddset(&action.sa_mask, SIGHUP); | |
4744bd90 | 579 | action.sa_handler = sighup_handler; |
ef416fc2 | 580 | sigaction(SIGHUP, &action, NULL); |
581 | ||
582 | sigemptyset(&action.sa_mask); | |
583 | action.sa_handler = SIG_IGN; | |
584 | sigaction(SIGPIPE, &action, NULL); | |
585 | ||
586 | sigemptyset(&action.sa_mask); | |
587 | sigaddset(&action.sa_mask, SIGTERM); | |
588 | sigaddset(&action.sa_mask, SIGCHLD); | |
589 | action.sa_handler = sigterm_handler; | |
590 | sigaction(SIGTERM, &action, NULL); | |
591 | #else | |
e1d6a774 | 592 | signal(SIGCLD, sigchld_handler); /* No, SIGCLD isn't a typo... */ |
4744bd90 | 593 | signal(SIGHUP, sighup_handler); |
ef416fc2 | 594 | signal(SIGPIPE, SIG_IGN); |
595 | signal(SIGTERM, sigterm_handler); | |
596 | #endif /* HAVE_SIGSET */ | |
597 | ||
598 | #ifdef __sgi | |
599 | /* | |
600 | * Try to create a fake lpsched lock file if one is not already there. | |
601 | * Some Adobe applications need it under IRIX in order to enable | |
602 | * printing... | |
603 | */ | |
604 | ||
605 | if ((fp = cupsFileOpen("/var/spool/lp/SCHEDLOCK", "w")) == NULL) | |
606 | { | |
607 | syslog(LOG_LPR, "Unable to create fake lpsched lock file " | |
608 | "\"/var/spool/lp/SCHEDLOCK\"\' - %s!", | |
609 | strerror(errno)); | |
610 | } | |
611 | else | |
612 | { | |
613 | fchmod(cupsFileNumber(fp), 0644); | |
614 | fchown(cupsFileNumber(fp), User, Group); | |
615 | ||
616 | cupsFileClose(fp); | |
617 | } | |
618 | #endif /* __sgi */ | |
619 | ||
620 | /* | |
621 | * Initialize authentication certificates... | |
622 | */ | |
623 | ||
624 | cupsdInitCerts(); | |
625 | ||
626 | /* | |
627 | * If we are running in the background, signal the parent process that | |
628 | * we are up and running... | |
629 | */ | |
630 | ||
a41f09e2 MS |
631 | #ifdef __APPLE__ |
632 | if (!fg || run_as_child) | |
633 | #else | |
ef416fc2 | 634 | if (!fg) |
a41f09e2 | 635 | #endif /* __APPLE__ */ |
ef416fc2 | 636 | { |
637 | /* | |
638 | * Send a signal to the parent process, but only if the parent is | |
639 | * not PID 1 (init). This avoids accidentally shutting down the | |
640 | * system on OpenBSD if you CTRL-C the server before it is up... | |
641 | */ | |
642 | ||
643 | i = getppid(); /* Save parent PID to avoid race condition */ | |
644 | ||
645 | if (i != 1) | |
646 | kill(i, SIGUSR1); | |
647 | } | |
648 | ||
e1d6a774 | 649 | #ifdef __APPLE__ |
09ec0018 | 650 | /* |
651 | * Start power management framework... | |
652 | */ | |
653 | ||
654 | cupsdStartSystemMonitor(); | |
e1d6a774 | 655 | #endif /* __APPLE__ */ |
ef416fc2 | 656 | |
657 | /* | |
658 | * Start any pending print jobs... | |
659 | */ | |
660 | ||
661 | cupsdCheckJobs(); | |
662 | ||
663 | /* | |
664 | * Loop forever... | |
665 | */ | |
666 | ||
50fe7201 MS |
667 | current_time = time(NULL); |
668 | browse_time = current_time; | |
669 | event_time = current_time; | |
670 | expire_time = current_time; | |
ef416fc2 | 671 | fds = 1; |
5bd77a73 | 672 | report_time = 0; |
50fe7201 | 673 | senddoc_time = current_time; |
ef416fc2 | 674 | |
675 | while (!stop_scheduler) | |
676 | { | |
677 | #ifdef DEBUG | |
678 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
679 | "main: Top of loop, dead_children=%d, NeedReload=%d", | |
680 | dead_children, NeedReload); | |
681 | #endif /* DEBUG */ | |
682 | ||
683 | /* | |
684 | * Check if there are dead children to handle... | |
685 | */ | |
686 | ||
687 | if (dead_children) | |
688 | process_children(); | |
689 | ||
690 | /* | |
691 | * Check if we need to load the server configuration file... | |
692 | */ | |
693 | ||
694 | if (NeedReload) | |
695 | { | |
696 | /* | |
697 | * Close any idle clients... | |
698 | */ | |
699 | ||
bd7854cb | 700 | if (cupsArrayCount(Clients) > 0) |
ef416fc2 | 701 | { |
bd7854cb | 702 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
703 | con; | |
704 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 705 | if (con->http.state == HTTP_WAITING) |
ef416fc2 | 706 | cupsdCloseClient(con); |
ef416fc2 | 707 | else |
708 | con->http.keep_alive = HTTP_KEEPALIVE_OFF; | |
709 | ||
710 | cupsdPauseListening(); | |
711 | } | |
712 | ||
713 | /* | |
714 | * Check for any active jobs... | |
715 | */ | |
716 | ||
717 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
718 | job; | |
719 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
bd7854cb | 720 | if (job->state_value == IPP_JOB_PROCESSING) |
ef416fc2 | 721 | break; |
722 | ||
723 | /* | |
724 | * Restart if all clients are closed and all jobs finished, or | |
725 | * if the reload timeout has elapsed... | |
726 | */ | |
727 | ||
bd7854cb | 728 | if ((cupsArrayCount(Clients) == 0 && |
729 | (!job || NeedReload != RELOAD_ALL)) || | |
ef416fc2 | 730 | (time(NULL) - ReloadTime) >= ReloadTimeout) |
731 | { | |
a4d04587 | 732 | /* |
733 | * Shutdown the server... | |
734 | */ | |
735 | ||
736 | cupsdStopServer(); | |
737 | ||
738 | /* | |
739 | * Read configuration... | |
740 | */ | |
741 | ||
ef416fc2 | 742 | if (!cupsdReadConfiguration()) |
743 | { | |
744 | syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting!", | |
745 | ConfigurationFile); | |
746 | break; | |
747 | } | |
a4d04587 | 748 | |
749 | #if HAVE_LAUNCHD | |
4400e98d | 750 | if (Launchd) |
a4d04587 | 751 | { |
b94498cf | 752 | /* |
753 | * If we were started by launchd get the listen sockets file descriptors... | |
754 | */ | |
755 | ||
a4d04587 | 756 | launchd_checkin(); |
757 | } | |
758 | #endif /* HAVE_LAUNCHD */ | |
759 | ||
760 | /* | |
761 | * Startup the server... | |
762 | */ | |
763 | ||
764 | cupsdStartServer(); | |
ef416fc2 | 765 | } |
766 | } | |
767 | ||
768 | /* | |
f7deaa1a | 769 | * Check for available input or ready output. If cupsdDoSelect() |
770 | * returns 0 or -1, something bad happened and we should exit | |
771 | * immediately. | |
ef416fc2 | 772 | * |
773 | * Note that we at least have one listening socket open at all | |
774 | * times. | |
775 | */ | |
776 | ||
50fe7201 MS |
777 | if ((timeout = select_timeout(fds)) > 1 && LastEvent) |
778 | timeout = 1; | |
ef416fc2 | 779 | |
a4d04587 | 780 | #if HAVE_LAUNCHD |
781 | /* | |
782 | * If no other work is scheduled and we're being controlled by | |
411affcf | 783 | * launchd then timeout after 'LaunchdTimeout' seconds of |
a4d04587 | 784 | * inactivity... |
785 | */ | |
786 | ||
f7deaa1a | 787 | if (timeout == 86400 && Launchd && LaunchdTimeout && !NumPolled && |
bc44d920 | 788 | !cupsArrayCount(ActiveJobs) && |
f7deaa1a | 789 | (!Browsing || |
790 | (!BrowseRemoteProtocols && | |
791 | (!NumBrowsers || !BrowseLocalProtocols || | |
792 | cupsArrayCount(Printers) == 0)))) | |
a4d04587 | 793 | { |
f7deaa1a | 794 | timeout = LaunchdTimeout; |
a4d04587 | 795 | launchd_idle_exit = 1; |
796 | } | |
797 | else | |
798 | launchd_idle_exit = 0; | |
799 | #endif /* HAVE_LAUNCHD */ | |
800 | ||
f7deaa1a | 801 | if ((fds = cupsdDoSelect(timeout)) < 0) |
ef416fc2 | 802 | { |
ef416fc2 | 803 | /* |
804 | * Got an error from select! | |
805 | */ | |
806 | ||
f7deaa1a | 807 | #ifdef HAVE_DNSSD |
808 | cupsd_printer_t *p; /* Current printer */ | |
809 | #endif /* HAVE_DNSSD */ | |
810 | ||
811 | ||
812 | if (errno == EINTR) /* Just interrupted by a signal */ | |
ef416fc2 | 813 | continue; |
814 | ||
815 | /* | |
816 | * Log all sorts of debug info to help track down the problem. | |
817 | */ | |
818 | ||
f7deaa1a | 819 | cupsdLogMessage(CUPSD_LOG_EMERG, "cupsdDoSelect() failed - %s!", |
ef416fc2 | 820 | strerror(errno)); |
821 | ||
bd7854cb | 822 | for (i = 0, con = (cupsd_client_t *)cupsArrayFirst(Clients); |
823 | con; | |
824 | i ++, con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 825 | cupsdLogMessage(CUPSD_LOG_EMERG, |
826 | "Clients[%d] = %d, file = %d, state = %d", | |
827 | i, con->http.fd, con->file, con->http.state); | |
828 | ||
bd7854cb | 829 | for (i = 0, lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); |
830 | lis; | |
831 | i ++, lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
ef416fc2 | 832 | cupsdLogMessage(CUPSD_LOG_EMERG, "Listeners[%d] = %d", i, lis->fd); |
833 | ||
834 | cupsdLogMessage(CUPSD_LOG_EMERG, "BrowseSocket = %d", BrowseSocket); | |
835 | ||
923edb68 | 836 | cupsdLogMessage(CUPSD_LOG_EMERG, "CGIPipes[0] = %d", CGIPipes[0]); |
837 | ||
09ec0018 | 838 | #ifdef __APPLE__ |
839 | cupsdLogMessage(CUPSD_LOG_EMERG, "SysEventPipes[0] = %d", | |
840 | SysEventPipes[0]); | |
841 | #endif /* __APPLE__ */ | |
842 | ||
ef416fc2 | 843 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); |
844 | job; | |
845 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
846 | cupsdLogMessage(CUPSD_LOG_EMERG, "Jobs[%d] = %d < [%d %d] > [%d %d]", | |
847 | job->id, | |
848 | job->status_buffer ? job->status_buffer->fd : -1, | |
849 | job->print_pipes[0], job->print_pipes[1], | |
850 | job->back_pipes[0], job->back_pipes[1]); | |
f7deaa1a | 851 | |
852 | #ifdef HAVE_DNSSD | |
853 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
854 | p; | |
855 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
856 | cupsdLogMessage(CUPSD_LOG_EMERG, "printer[%s] %d", p->name, | |
857 | p->dnssd_ipp_fd); | |
858 | #endif /* HAVE_DNSSD */ | |
859 | ||
ef416fc2 | 860 | break; |
861 | } | |
862 | ||
863 | current_time = time(NULL); | |
864 | ||
a4924f6c MS |
865 | #ifndef __APPLE__ |
866 | /* | |
867 | * Update the network interfaces once a minute... | |
868 | */ | |
869 | ||
870 | if ((current_time - netif_time) >= 60) | |
871 | { | |
872 | netif_time = current_time; | |
873 | NetIFUpdate = 1; | |
874 | } | |
875 | #endif /* !__APPLE__ */ | |
876 | ||
a4d04587 | 877 | #if HAVE_LAUNCHD |
878 | /* | |
411affcf | 879 | * If no other work was scheduled and we're being controlled by launchd |
a4d04587 | 880 | * then timeout after 'LaunchdTimeout' seconds of inactivity... |
881 | */ | |
882 | ||
883 | if (!fds && launchd_idle_exit) | |
884 | { | |
885 | cupsdLogMessage(CUPSD_LOG_INFO, | |
886 | "Printer sharing is off and there are no jobs pending, " | |
887 | "will restart on demand."); | |
888 | stop_scheduler = 1; | |
889 | break; | |
890 | } | |
891 | #endif /* HAVE_LAUNCHD */ | |
892 | ||
76cd9e37 MS |
893 | /* |
894 | * Resume listening for new connections as needed... | |
895 | */ | |
896 | ||
897 | if (ListeningPaused && ListeningPaused <= current_time && | |
898 | cupsArrayCount(Clients) < MaxClients) | |
899 | cupsdResumeListening(); | |
900 | ||
ef416fc2 | 901 | /* |
bd7854cb | 902 | * Expire subscriptions and unload completed jobs as needed... |
ef416fc2 | 903 | */ |
904 | ||
bd7854cb | 905 | if (current_time > expire_time) |
ef416fc2 | 906 | { |
bd7854cb | 907 | if (cupsArrayCount(Subscriptions) > 0) |
908 | cupsdExpireSubscriptions(NULL, NULL); | |
909 | ||
910 | cupsdUnloadCompletedJobs(); | |
ef416fc2 | 911 | |
912 | expire_time = current_time; | |
913 | } | |
914 | ||
915 | /* | |
916 | * Update the browse list as needed... | |
917 | */ | |
918 | ||
f7deaa1a | 919 | if (Browsing) |
ef416fc2 | 920 | { |
ef416fc2 | 921 | #ifdef HAVE_LIBSLP |
b423cd4c | 922 | if ((BrowseRemoteProtocols & BROWSE_SLP) && |
ef416fc2 | 923 | BrowseSLPRefresh <= current_time) |
924 | cupsdUpdateSLPBrowse(); | |
925 | #endif /* HAVE_LIBSLP */ | |
b423cd4c | 926 | |
927 | #ifdef HAVE_LDAP | |
928 | if ((BrowseRemoteProtocols & BROWSE_LDAP) && | |
929 | BrowseLDAPRefresh <= current_time) | |
930 | cupsdUpdateLDAPBrowse(); | |
931 | #endif /* HAVE_LDAP */ | |
fa73b229 | 932 | } |
ef416fc2 | 933 | |
fa73b229 | 934 | if (Browsing && BrowseLocalProtocols && current_time > browse_time) |
935 | { | |
936 | cupsdSendBrowseList(); | |
937 | browse_time = current_time; | |
ef416fc2 | 938 | } |
939 | ||
940 | /* | |
f7deaa1a | 941 | * Update the root certificate once every 5 minutes if we have client |
942 | * connections... | |
ef416fc2 | 943 | */ |
944 | ||
f7deaa1a | 945 | if ((current_time - RootCertTime) >= RootCertDuration && RootCertDuration && |
946 | !RunUser && cupsArrayCount(Clients)) | |
947 | { | |
948 | /* | |
949 | * Update the root certificate... | |
950 | */ | |
951 | ||
952 | cupsdDeleteCert(0); | |
5bd77a73 | 953 | cupsdAddCert(0, "root", NULL); |
f7deaa1a | 954 | } |
ef416fc2 | 955 | |
956 | /* | |
957 | * Check for new data on the client sockets... | |
958 | */ | |
959 | ||
bd7854cb | 960 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
961 | con; | |
962 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 963 | { |
964 | /* | |
f7deaa1a | 965 | * Process pending data in the input buffer... |
ef416fc2 | 966 | */ |
967 | ||
f7deaa1a | 968 | if (con->http.used) |
ef416fc2 | 969 | { |
f7deaa1a | 970 | cupsdReadClient(con); |
971 | continue; | |
ef416fc2 | 972 | } |
973 | ||
974 | /* | |
975 | * Check the activity and close old clients... | |
976 | */ | |
977 | ||
978 | activity = current_time - Timeout; | |
979 | if (con->http.activity < activity && !con->pipe_pid) | |
980 | { | |
981 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
982 | "Closing client %d after %d seconds of inactivity...", | |
983 | con->http.fd, Timeout); | |
984 | ||
985 | cupsdCloseClient(con); | |
ef416fc2 | 986 | continue; |
987 | } | |
988 | } | |
989 | ||
990 | /* | |
991 | * Update any pending multi-file documents... | |
992 | */ | |
993 | ||
994 | if ((current_time - senddoc_time) >= 10) | |
995 | { | |
996 | cupsdCheckJobs(); | |
997 | senddoc_time = current_time; | |
998 | } | |
999 | ||
ef416fc2 | 1000 | /* |
5bd77a73 | 1001 | * Log statistics at most once a minute when in debug mode... |
ef416fc2 | 1002 | */ |
1003 | ||
5bd77a73 | 1004 | if ((current_time - report_time) >= 60 && LogLevel >= CUPSD_LOG_DEBUG) |
ef416fc2 | 1005 | { |
5bd77a73 MS |
1006 | size_t string_count, /* String count */ |
1007 | alloc_bytes, /* Allocated string bytes */ | |
1008 | total_bytes; /* Total string bytes */ | |
4400e98d | 1009 | #ifdef HAVE_MALLINFO |
5bd77a73 | 1010 | struct mallinfo mem; /* Malloc information */ |
ef416fc2 | 1011 | |
1012 | ||
1013 | mem = mallinfo(); | |
5bd77a73 MS |
1014 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-arena=%lu", mem.arena); |
1015 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-used=%lu", | |
1016 | mem.usmblks + mem.uordblks); | |
1017 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: malloc-free=%lu", | |
ef416fc2 | 1018 | mem.fsmblks + mem.fordblks); |
4400e98d | 1019 | #endif /* HAVE_MALLINFO */ |
1020 | ||
5bd77a73 MS |
1021 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: clients=%d", |
1022 | cupsArrayCount(Clients)); | |
1023 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: jobs=%d", | |
1024 | cupsArrayCount(Jobs)); | |
1025 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: jobs-active=%d", | |
1026 | cupsArrayCount(ActiveJobs)); | |
1027 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: printers=%d", | |
1028 | cupsArrayCount(Printers)); | |
1029 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Report: printers-implicit=%d", | |
1030 | cupsArrayCount(ImplicitPrinters)); | |
1031 | ||
757d2cad | 1032 | string_count = _cupsStrStatistics(&alloc_bytes, &total_bytes); |
5bd77a73 MS |
1033 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1034 | "Report: stringpool-string-count=" CUPS_LLFMT, | |
1035 | CUPS_LLCAST string_count); | |
1036 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1037 | "Report: stringpool-alloc-bytes=" CUPS_LLFMT, | |
1038 | CUPS_LLCAST alloc_bytes); | |
1039 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1040 | "Report: stringpool-total-bytes=" CUPS_LLFMT, | |
4400e98d | 1041 | CUPS_LLCAST total_bytes); |
1042 | ||
5bd77a73 | 1043 | report_time = current_time; |
ef416fc2 | 1044 | } |
ef416fc2 | 1045 | |
fa73b229 | 1046 | /* |
1047 | * Handle OS-specific event notification for any events that have | |
1048 | * accumulated. Don't send these more than once a second... | |
1049 | */ | |
1050 | ||
50fe7201 | 1051 | if (LastEvent && (current_time - event_time) >= 1) |
fa73b229 | 1052 | { |
1053 | #ifdef HAVE_NOTIFY_POST | |
a41f09e2 MS |
1054 | if (LastEvent & (CUPSD_EVENT_PRINTER_ADDED | |
1055 | CUPSD_EVENT_PRINTER_DELETED | | |
1056 | CUPSD_EVENT_PRINTER_MODIFIED)) | |
fa73b229 | 1057 | { |
e00b005a | 1058 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
fa73b229 | 1059 | "notify_post(\"com.apple.printerListChange\")"); |
1060 | notify_post("com.apple.printerListChange"); | |
1061 | } | |
1062 | ||
1063 | if (LastEvent & CUPSD_EVENT_PRINTER_STATE_CHANGED) | |
1064 | { | |
e00b005a | 1065 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
fa73b229 | 1066 | "notify_post(\"com.apple.printerHistoryChange\")"); |
1067 | notify_post("com.apple.printerHistoryChange"); | |
1068 | } | |
1069 | ||
1070 | if (LastEvent & (CUPSD_EVENT_JOB_STATE_CHANGED | | |
1071 | CUPSD_EVENT_JOB_CONFIG_CHANGED | | |
1072 | CUPSD_EVENT_JOB_PROGRESS)) | |
1073 | { | |
e00b005a | 1074 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
fa73b229 | 1075 | "notify_post(\"com.apple.jobChange\")"); |
1076 | notify_post("com.apple.jobChange"); | |
1077 | } | |
1078 | #endif /* HAVE_NOTIFY_POST */ | |
1079 | ||
1080 | /* | |
e53920b9 | 1081 | * Reset the accumulated events... |
fa73b229 | 1082 | */ |
1083 | ||
50fe7201 MS |
1084 | LastEvent = CUPSD_EVENT_NONE; |
1085 | event_time = current_time; | |
fa73b229 | 1086 | } |
ef416fc2 | 1087 | } |
1088 | ||
1089 | /* | |
1090 | * Log a message based on what happened... | |
1091 | */ | |
1092 | ||
1093 | if (stop_scheduler) | |
1094 | cupsdLogMessage(CUPSD_LOG_INFO, "Scheduler shutting down normally."); | |
1095 | else | |
1096 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1097 | "Scheduler shutting down due to program error."); | |
1098 | ||
1099 | /* | |
7ff4fea9 | 1100 | * Close all network clients... |
ef416fc2 | 1101 | */ |
1102 | ||
1103 | cupsdStopServer(); | |
1104 | ||
7ff4fea9 MS |
1105 | #ifdef HAVE_LAUNCHD |
1106 | /* | |
1107 | * Update the launchd KeepAlive file as needed... | |
1108 | */ | |
1109 | ||
1110 | if (Launchd) | |
1111 | launchd_checkout(); | |
1112 | #endif /* HAVE_LAUNCHD */ | |
1113 | ||
1114 | /* | |
1115 | * Stop all jobs... | |
1116 | */ | |
1117 | ||
bd7854cb | 1118 | cupsdFreeAllJobs(); |
ef416fc2 | 1119 | |
e1d6a774 | 1120 | #ifdef __APPLE__ |
7ff4fea9 MS |
1121 | /* |
1122 | * Stop monitoring system event monitoring... | |
1123 | */ | |
1124 | ||
09ec0018 | 1125 | cupsdStopSystemMonitor(); |
e1d6a774 | 1126 | #endif /* __APPLE__ */ |
09ec0018 | 1127 | |
c24d2134 | 1128 | #ifdef HAVE_GSSAPI |
7ff4fea9 MS |
1129 | /* |
1130 | * Free the scheduler's Kerberos context... | |
1131 | */ | |
1132 | ||
c24d2134 MS |
1133 | # ifdef __APPLE__ |
1134 | /* | |
1135 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
1136 | * to use it... | |
1137 | */ | |
1138 | ||
1139 | if (krb5_init_context != NULL) | |
1140 | # endif /* __APPLE__ */ | |
a41f09e2 MS |
1141 | if (KerberosContext) |
1142 | krb5_free_context(KerberosContext); | |
c24d2134 MS |
1143 | #endif /* HAVE_GSSAPI */ |
1144 | ||
b94498cf | 1145 | #ifdef __APPLE__ |
1146 | #ifdef HAVE_DLFCN_H | |
1147 | /* | |
1148 | * Unload Print Service quota enforcement library (X Server only) | |
1149 | */ | |
411affcf | 1150 | |
b94498cf | 1151 | PSQUpdateQuotaProc = NULL; |
1152 | if (PSQLibRef) | |
1153 | { | |
1154 | dlclose(PSQLibRef); | |
1155 | PSQLibRef = NULL; | |
f7deaa1a | 1156 | } |
b94498cf | 1157 | #endif /* HAVE_DLFCN_H */ |
1158 | #endif /* __APPLE__ */ | |
a4d04587 | 1159 | |
ef416fc2 | 1160 | #ifdef __sgi |
1161 | /* | |
1162 | * Remove the fake IRIX lpsched lock file, but only if the existing | |
1163 | * file is not a FIFO which indicates that the real IRIX lpsched is | |
1164 | * running... | |
1165 | */ | |
1166 | ||
1167 | if (!stat("/var/spool/lp/FIFO", &statbuf)) | |
1168 | if (!S_ISFIFO(statbuf.st_mode)) | |
1169 | unlink("/var/spool/lp/SCHEDLOCK"); | |
1170 | #endif /* __sgi */ | |
1171 | ||
f7deaa1a | 1172 | cupsdStopSelect(); |
ef416fc2 | 1173 | |
1174 | return (!stop_scheduler); | |
1175 | } | |
1176 | ||
1177 | ||
1178 | /* | |
1179 | * 'cupsdClosePipe()' - Close a pipe as necessary. | |
1180 | */ | |
1181 | ||
1182 | void | |
1183 | cupsdClosePipe(int *fds) /* I - Pipe file descriptors (2) */ | |
1184 | { | |
1185 | /* | |
1186 | * Close file descriptors as needed... | |
1187 | */ | |
1188 | ||
1189 | if (fds[0] >= 0) | |
1190 | { | |
1191 | close(fds[0]); | |
1192 | fds[0] = -1; | |
1193 | } | |
1194 | ||
1195 | if (fds[1] >= 0) | |
1196 | { | |
1197 | close(fds[1]); | |
1198 | fds[1] = -1; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | ||
1203 | /* | |
1204 | * 'cupsdOpenPipe()' - Create a pipe which is closed on exec. | |
1205 | */ | |
1206 | ||
1207 | int /* O - 0 on success, -1 on error */ | |
1208 | cupsdOpenPipe(int *fds) /* O - Pipe file descriptors (2) */ | |
1209 | { | |
1210 | /* | |
1211 | * Create the pipe... | |
1212 | */ | |
1213 | ||
1214 | if (pipe(fds)) | |
e1d6a774 | 1215 | { |
1216 | fds[0] = -1; | |
1217 | fds[1] = -1; | |
1218 | ||
ef416fc2 | 1219 | return (-1); |
e1d6a774 | 1220 | } |
ef416fc2 | 1221 | |
1222 | /* | |
1223 | * Set the "close on exec" flag on each end of the pipe... | |
1224 | */ | |
1225 | ||
1226 | if (fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC)) | |
1227 | { | |
1228 | close(fds[0]); | |
1229 | close(fds[1]); | |
e1d6a774 | 1230 | |
1231 | fds[0] = -1; | |
1232 | fds[1] = -1; | |
1233 | ||
ef416fc2 | 1234 | return (-1); |
1235 | } | |
1236 | ||
1237 | if (fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD) | FD_CLOEXEC)) | |
1238 | { | |
1239 | close(fds[0]); | |
1240 | close(fds[1]); | |
e1d6a774 | 1241 | |
1242 | fds[0] = -1; | |
1243 | fds[1] = -1; | |
1244 | ||
ef416fc2 | 1245 | return (-1); |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * Return 0 indicating success... | |
1250 | */ | |
1251 | ||
1252 | return (0); | |
1253 | } | |
1254 | ||
1255 | ||
ef416fc2 | 1256 | /* |
1257 | * 'cupsdClearString()' - Clear a string. | |
1258 | */ | |
1259 | ||
1260 | void | |
1261 | cupsdClearString(char **s) /* O - String value */ | |
1262 | { | |
1263 | if (s && *s) | |
1264 | { | |
e53920b9 | 1265 | _cupsStrFree(*s); |
ef416fc2 | 1266 | *s = NULL; |
1267 | } | |
1268 | } | |
1269 | ||
1270 | ||
1271 | /* | |
1272 | * 'cupsdHoldSignals()' - Hold child and termination signals. | |
1273 | */ | |
1274 | ||
1275 | void | |
1276 | cupsdHoldSignals(void) | |
1277 | { | |
1278 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) | |
1279 | sigset_t newmask; /* New POSIX signal mask */ | |
1280 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
1281 | ||
1282 | ||
1283 | holdcount ++; | |
1284 | if (holdcount > 1) | |
1285 | return; | |
1286 | ||
1287 | #ifdef HAVE_SIGSET | |
1288 | sighold(SIGTERM); | |
1289 | sighold(SIGCHLD); | |
1290 | #elif defined(HAVE_SIGACTION) | |
1291 | sigemptyset(&newmask); | |
1292 | sigaddset(&newmask, SIGTERM); | |
1293 | sigaddset(&newmask, SIGCHLD); | |
1294 | sigprocmask(SIG_BLOCK, &newmask, &holdmask); | |
1295 | #endif /* HAVE_SIGSET */ | |
1296 | } | |
1297 | ||
1298 | ||
ef416fc2 | 1299 | /* |
1300 | * 'cupsdReleaseSignals()' - Release signals for delivery. | |
1301 | */ | |
1302 | ||
1303 | void | |
1304 | cupsdReleaseSignals(void) | |
1305 | { | |
1306 | holdcount --; | |
1307 | if (holdcount > 0) | |
1308 | return; | |
1309 | ||
1310 | #ifdef HAVE_SIGSET | |
1311 | sigrelse(SIGTERM); | |
1312 | sigrelse(SIGCHLD); | |
1313 | #elif defined(HAVE_SIGACTION) | |
1314 | sigprocmask(SIG_SETMASK, &holdmask, NULL); | |
1315 | #endif /* HAVE_SIGSET */ | |
1316 | } | |
1317 | ||
1318 | ||
1319 | /* | |
1320 | * 'cupsdSetString()' - Set a string value. | |
1321 | */ | |
1322 | ||
1323 | void | |
1324 | cupsdSetString(char **s, /* O - New string */ | |
1325 | const char *v) /* I - String value */ | |
1326 | { | |
1327 | if (!s || *s == v) | |
1328 | return; | |
1329 | ||
1330 | if (*s) | |
e53920b9 | 1331 | _cupsStrFree(*s); |
ef416fc2 | 1332 | |
1333 | if (v) | |
e53920b9 | 1334 | *s = _cupsStrAlloc(v); |
ef416fc2 | 1335 | else |
1336 | *s = NULL; | |
1337 | } | |
1338 | ||
1339 | ||
1340 | /* | |
1341 | * 'cupsdSetStringf()' - Set a formatted string value. | |
1342 | */ | |
1343 | ||
1344 | void | |
1345 | cupsdSetStringf(char **s, /* O - New string */ | |
1346 | const char *f, /* I - Printf-style format string */ | |
1347 | ...) /* I - Additional args as needed */ | |
1348 | { | |
1349 | char v[4096]; /* Formatting string value */ | |
1350 | va_list ap; /* Argument pointer */ | |
1351 | char *olds; /* Old string */ | |
1352 | ||
1353 | ||
1354 | if (!s) | |
1355 | return; | |
1356 | ||
1357 | olds = *s; | |
1358 | ||
1359 | if (f) | |
1360 | { | |
1361 | va_start(ap, f); | |
1362 | vsnprintf(v, sizeof(v), f, ap); | |
1363 | va_end(ap); | |
1364 | ||
e53920b9 | 1365 | *s = _cupsStrAlloc(v); |
ef416fc2 | 1366 | } |
1367 | else | |
1368 | *s = NULL; | |
1369 | ||
1370 | if (olds) | |
e53920b9 | 1371 | _cupsStrFree(olds); |
ef416fc2 | 1372 | } |
1373 | ||
1374 | ||
a4d04587 | 1375 | #ifdef HAVE_LAUNCHD |
1376 | /* | |
1377 | * 'launchd_checkin()' - Check-in with launchd and collect the listening fds. | |
1378 | */ | |
1379 | ||
1380 | static void | |
1381 | launchd_checkin(void) | |
1382 | { | |
1383 | int i, /* Looping var */ | |
bd7854cb | 1384 | count, /* Numebr of listeners */ |
a4d04587 | 1385 | portnum; /* Port number */ |
1386 | launch_data_t ld_msg, /* Launch data message */ | |
1387 | ld_resp, /* Launch data response */ | |
1388 | ld_array, /* Launch data array */ | |
1389 | ld_sockets, /* Launch data sockets dictionary */ | |
a4d04587 | 1390 | tmp; /* Launch data */ |
1391 | cupsd_listener_t *lis; /* Listeners array */ | |
1392 | http_addr_t addr; /* Address variable */ | |
1393 | socklen_t addrlen; /* Length of address */ | |
f7deaa1a | 1394 | int fd; /* File descriptor */ |
1395 | char s[256]; /* String addresss */ | |
a4d04587 | 1396 | |
1397 | ||
1398 | cupsdLogMessage(CUPSD_LOG_DEBUG, "launchd_checkin: pid=%d", (int)getpid()); | |
1399 | ||
1400 | /* | |
1401 | * Check-in with launchd... | |
1402 | */ | |
1403 | ||
1404 | ld_msg = launch_data_new_string(LAUNCH_KEY_CHECKIN); | |
1405 | if ((ld_resp = launch_msg(ld_msg)) == NULL) | |
1406 | { | |
bd7854cb | 1407 | cupsdLogMessage(CUPSD_LOG_ERROR, |
a4d04587 | 1408 | "launchd_checkin: launch_msg(\"" LAUNCH_KEY_CHECKIN |
1409 | "\") IPC failure"); | |
1410 | exit(EXIT_FAILURE); | |
1411 | } | |
bd7854cb | 1412 | |
a4d04587 | 1413 | if (launch_data_get_type(ld_resp) == LAUNCH_DATA_ERRNO) |
1414 | { | |
1415 | errno = launch_data_get_errno(ld_resp); | |
1416 | cupsdLogMessage(CUPSD_LOG_ERROR, "launchd_checkin: Check-in failed: %s", | |
1417 | strerror(errno)); | |
1418 | exit(EXIT_FAILURE); | |
1419 | } | |
1420 | ||
a4d04587 | 1421 | /* |
1422 | * Get the sockets dictionary... | |
1423 | */ | |
1424 | ||
1425 | if (!(ld_sockets = launch_data_dict_lookup(ld_resp, LAUNCH_JOBKEY_SOCKETS))) | |
1426 | { | |
1427 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1428 | "launchd_checkin: No sockets found to answer requests on!"); | |
1429 | exit(EXIT_FAILURE); | |
1430 | } | |
bd7854cb | 1431 | |
a4d04587 | 1432 | /* |
1433 | * Get the array of listener sockets... | |
1434 | */ | |
1435 | ||
1436 | if (!(ld_array = launch_data_dict_lookup(ld_sockets, "Listeners"))) | |
1437 | { | |
1438 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1439 | "launchd_checkin: No sockets found to answer requests on!"); | |
1440 | exit(EXIT_FAILURE); | |
1441 | } | |
1442 | ||
1443 | /* | |
1444 | * Add listening fd(s) to the Listener array... | |
1445 | */ | |
1446 | ||
1447 | if (launch_data_get_type(ld_array) == LAUNCH_DATA_ARRAY) | |
1448 | { | |
f7deaa1a | 1449 | count = launch_data_array_get_count(ld_array); |
bd7854cb | 1450 | |
1451 | for (i = 0; i < count; i ++) | |
a4d04587 | 1452 | { |
1453 | /* | |
f7deaa1a | 1454 | * Get the launchd file descriptor and address... |
a4d04587 | 1455 | */ |
bd7854cb | 1456 | |
f7deaa1a | 1457 | tmp = launch_data_array_get_index(ld_array, i); |
1458 | fd = launch_data_get_fd(tmp); | |
1459 | addrlen = sizeof(addr); | |
1460 | ||
1461 | if (getsockname(fd, (struct sockaddr *)&addr, &addrlen)) | |
bd7854cb | 1462 | { |
1463 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
f7deaa1a | 1464 | "launchd_checkin: Unable to get local address - %s", |
1465 | strerror(errno)); | |
1466 | continue; | |
bd7854cb | 1467 | } |
1468 | ||
f7deaa1a | 1469 | /* |
1470 | * Try to match the launchd socket address to one of the listeners... | |
1471 | */ | |
bd7854cb | 1472 | |
f7deaa1a | 1473 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); |
1474 | lis; | |
1475 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
1476 | if (httpAddrEqual(&lis->address, &addr)) | |
1477 | break; | |
1478 | ||
1479 | /* | |
1480 | * Add a new listener If there's no match... | |
1481 | */ | |
a4d04587 | 1482 | |
f7deaa1a | 1483 | if (lis) |
a4d04587 | 1484 | { |
f7deaa1a | 1485 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1486 | "launchd_checkin: Matched existing listener %s with fd %d...", | |
1487 | httpAddrString(&(lis->address), s, sizeof(s)), fd); | |
1488 | } | |
1489 | else | |
1490 | { | |
1491 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1492 | "launchd_checkin: Adding new listener %s with fd %d...", | |
1493 | httpAddrString(&addr, s, sizeof(s)), fd); | |
1494 | ||
1495 | if ((lis = calloc(1, sizeof(cupsd_listener_t))) == NULL) | |
1496 | { | |
1497 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1498 | "launchd_checkin: Unable to allocate listener - %s.", | |
1499 | strerror(errno)); | |
1500 | exit(EXIT_FAILURE); | |
1501 | } | |
1502 | ||
1503 | cupsArrayAdd(Listeners, lis); | |
1504 | ||
1505 | memcpy(&lis->address, &addr, sizeof(lis->address)); | |
a4d04587 | 1506 | } |
bd7854cb | 1507 | |
f7deaa1a | 1508 | lis->fd = fd; |
1509 | ||
a4d04587 | 1510 | # ifdef HAVE_SSL |
1511 | portnum = 0; | |
bd7854cb | 1512 | |
a4d04587 | 1513 | # ifdef AF_INET6 |
f7deaa1a | 1514 | if (lis->address.addr.sa_family == AF_INET6) |
1515 | portnum = ntohs(lis->address.ipv6.sin6_port); | |
a4d04587 | 1516 | else |
1517 | # endif /* AF_INET6 */ | |
f7deaa1a | 1518 | if (lis->address.addr.sa_family == AF_INET) |
1519 | portnum = ntohs(lis->address.ipv4.sin_port); | |
a4d04587 | 1520 | |
1521 | if (portnum == 443) | |
1522 | lis->encryption = HTTP_ENCRYPT_ALWAYS; | |
1523 | # endif /* HAVE_SSL */ | |
1524 | } | |
1525 | } | |
1526 | ||
a4d04587 | 1527 | launch_data_free(ld_msg); |
1528 | launch_data_free(ld_resp); | |
1529 | } | |
1530 | ||
1531 | ||
f7deaa1a | 1532 | /* |
1533 | * 'launchd_checkout()' - Update the launchd KeepAlive file as needed. | |
1534 | */ | |
1535 | ||
1536 | static void | |
1537 | launchd_checkout(void) | |
1538 | { | |
1539 | int fd; /* File descriptor */ | |
1540 | ||
1541 | ||
1542 | /* | |
1543 | * Create or remove the launchd KeepAlive file based on whether | |
1544 | * there are active jobs, polling, browsing for remote printers or | |
1545 | * shared printers to advertise... | |
1546 | */ | |
1547 | ||
1548 | if ((cupsArrayCount(ActiveJobs) || NumPolled || | |
1549 | (Browsing && | |
1550 | (BrowseRemoteProtocols || | |
1551 | (BrowseLocalProtocols && NumBrowsers && cupsArrayCount(Printers)))))) | |
1552 | { | |
1553 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1554 | "Creating launchd keepalive file \"" CUPS_KEEPALIVE "\"..."); | |
1555 | ||
1556 | if ((fd = open(CUPS_KEEPALIVE, O_RDONLY | O_CREAT | O_EXCL, S_IRUSR)) >= 0) | |
1557 | close(fd); | |
1558 | } | |
1559 | else | |
1560 | { | |
1561 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1562 | "Removing launchd keepalive file \"" CUPS_KEEPALIVE "\"..."); | |
1563 | ||
1564 | unlink(CUPS_KEEPALIVE); | |
1565 | } | |
1566 | } | |
a4d04587 | 1567 | #endif /* HAVE_LAUNCHD */ |
1568 | ||
1569 | ||
ef416fc2 | 1570 | /* |
1571 | * 'parent_handler()' - Catch USR1/CHLD signals... | |
1572 | */ | |
1573 | ||
1574 | static void | |
1575 | parent_handler(int sig) /* I - Signal */ | |
1576 | { | |
1577 | /* | |
1578 | * Store the signal we got from the OS and return... | |
1579 | */ | |
1580 | ||
1581 | parent_signal = sig; | |
1582 | } | |
1583 | ||
1584 | ||
1585 | /* | |
1586 | * 'process_children()' - Process all dead children... | |
1587 | */ | |
1588 | ||
1589 | static void | |
1590 | process_children(void) | |
1591 | { | |
1592 | int status; /* Exit status of child */ | |
1593 | int pid; /* Process ID of child */ | |
1594 | cupsd_job_t *job; /* Current job */ | |
1595 | int i; /* Looping var */ | |
e00b005a | 1596 | char name[1024]; /* Process name */ |
ef416fc2 | 1597 | |
1598 | ||
1599 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_children()"); | |
1600 | ||
1601 | /* | |
1602 | * Reset the dead_children flag... | |
1603 | */ | |
1604 | ||
1605 | dead_children = 0; | |
1606 | ||
1607 | /* | |
1608 | * Collect the exit status of some children... | |
1609 | */ | |
1610 | ||
1611 | #ifdef HAVE_WAITPID | |
1612 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) | |
1613 | #elif defined(HAVE_WAIT3) | |
1614 | while ((pid = wait3(&status, WNOHANG, NULL)) > 0) | |
1615 | #else | |
1616 | if ((pid = wait(&status)) > 0) | |
1617 | #endif /* HAVE_WAITPID */ | |
1618 | { | |
ef416fc2 | 1619 | /* |
d09495fa | 1620 | * Ignore SIGTERM errors - that comes when a job is canceled... |
ef416fc2 | 1621 | */ |
1622 | ||
e00b005a | 1623 | cupsdFinishProcess(pid, name, sizeof(name)); |
1624 | ||
ef416fc2 | 1625 | if (status == SIGTERM) |
1626 | status = 0; | |
1627 | ||
1628 | if (status) | |
1629 | { | |
1630 | if (WIFEXITED(status)) | |
e00b005a | 1631 | cupsdLogMessage(CUPSD_LOG_ERROR, "PID %d (%s) stopped with status %d!", |
1632 | pid, name, WEXITSTATUS(status)); | |
ef416fc2 | 1633 | else |
e00b005a | 1634 | cupsdLogMessage(CUPSD_LOG_ERROR, "PID %d (%s) crashed on signal %d!", |
1635 | pid, name, WTERMSIG(status)); | |
ef416fc2 | 1636 | |
1637 | if (LogLevel < CUPSD_LOG_DEBUG) | |
1638 | cupsdLogMessage(CUPSD_LOG_INFO, | |
bd7854cb | 1639 | "Hint: Try setting the LogLevel to \"debug\" to find " |
1640 | "out more."); | |
ef416fc2 | 1641 | } |
1642 | else | |
e00b005a | 1643 | cupsdLogMessage(CUPSD_LOG_DEBUG, "PID %d (%s) exited with no errors.", |
1644 | pid, name); | |
ef416fc2 | 1645 | |
1646 | /* | |
1647 | * Delete certificates for CGI processes... | |
1648 | */ | |
1649 | ||
1650 | if (pid) | |
1651 | cupsdDeleteCert(pid); | |
1652 | ||
1653 | /* | |
1654 | * Lookup the PID in the jobs list... | |
1655 | */ | |
1656 | ||
1657 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
1658 | job; | |
1659 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
bd7854cb | 1660 | if (job->state_value == IPP_JOB_PROCESSING) |
ef416fc2 | 1661 | { |
1662 | for (i = 0; job->filters[i]; i ++) | |
1663 | if (job->filters[i] == pid) | |
1664 | break; | |
1665 | ||
1666 | if (job->filters[i] || job->backend == pid) | |
1667 | { | |
1668 | /* | |
1669 | * OK, this process has gone away; what's left? | |
1670 | */ | |
1671 | ||
1672 | if (job->filters[i]) | |
1673 | job->filters[i] = -pid; | |
1674 | else | |
1675 | job->backend = -pid; | |
1676 | ||
1677 | if (status && job->status >= 0) | |
1678 | { | |
1679 | /* | |
1680 | * An error occurred; save the exit status so we know to stop | |
1681 | * the printer or cancel the job when all of the filters finish... | |
1682 | * | |
1683 | * A negative status indicates that the backend failed and the | |
1684 | * printer needs to be stopped. | |
1685 | */ | |
1686 | ||
1687 | if (job->filters[i]) | |
1688 | job->status = status; /* Filter failed */ | |
1689 | else | |
1690 | job->status = -status; /* Backend failed */ | |
e00b005a | 1691 | |
ac884b6a MS |
1692 | if (job->printer && !(job->printer->type & CUPS_PRINTER_FAX) && |
1693 | job->status_level > CUPSD_LOG_ERROR) | |
e00b005a | 1694 | { |
ac884b6a MS |
1695 | job->status_level = CUPSD_LOG_ERROR; |
1696 | ||
e00b005a | 1697 | snprintf(job->printer->state_message, |
1698 | sizeof(job->printer->state_message), "%s failed", name); | |
1699 | cupsdAddPrinterHistory(job->printer); | |
1700 | } | |
ef416fc2 | 1701 | } |
1702 | ||
1703 | /* | |
1704 | * If this is not the last file in a job, see if all of the | |
1705 | * filters are done, and if so move to the next file. | |
1706 | */ | |
1707 | ||
1708 | if (job->current_file < job->num_files) | |
1709 | { | |
1710 | for (i = 0; job->filters[i] < 0; i ++); | |
1711 | ||
1712 | if (!job->filters[i]) | |
1713 | { | |
1714 | /* | |
1715 | * Process the next file... | |
1716 | */ | |
1717 | ||
1718 | cupsdFinishJob(job); | |
1719 | } | |
1720 | } | |
1721 | break; | |
1722 | } | |
1723 | } | |
1724 | } | |
1725 | } | |
1726 | ||
1727 | ||
1728 | /* | |
1729 | * 'sigchld_handler()' - Handle 'child' signals from old processes. | |
1730 | */ | |
1731 | ||
1732 | static void | |
1733 | sigchld_handler(int sig) /* I - Signal number */ | |
1734 | { | |
1735 | (void)sig; | |
1736 | ||
1737 | /* | |
1738 | * Flag that we have dead children... | |
1739 | */ | |
1740 | ||
1741 | dead_children = 1; | |
1742 | ||
1743 | /* | |
1744 | * Reset the signal handler as needed... | |
1745 | */ | |
1746 | ||
1747 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1748 | signal(SIGCLD, sigchld_handler); | |
1749 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1750 | } | |
1751 | ||
1752 | ||
1753 | /* | |
1754 | * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler. | |
1755 | */ | |
1756 | ||
1757 | static void | |
1758 | sighup_handler(int sig) /* I - Signal number */ | |
1759 | { | |
1760 | (void)sig; | |
1761 | ||
1762 | NeedReload = RELOAD_ALL; | |
1763 | ReloadTime = time(NULL); | |
1764 | ||
1765 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1766 | signal(SIGHUP, sighup_handler); | |
1767 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1768 | } | |
1769 | ||
1770 | ||
1771 | /* | |
1772 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler. | |
1773 | */ | |
1774 | ||
1775 | static void | |
1776 | sigterm_handler(int sig) /* I - Signal */ | |
1777 | { | |
1778 | (void)sig; /* remove compiler warnings... */ | |
1779 | ||
1780 | /* | |
1781 | * Flag that we should stop and return... | |
1782 | */ | |
1783 | ||
1784 | stop_scheduler = 1; | |
1785 | } | |
1786 | ||
1787 | ||
1788 | /* | |
1789 | * 'select_timeout()' - Calculate the select timeout value. | |
1790 | * | |
1791 | */ | |
1792 | ||
1793 | static long /* O - Number of seconds */ | |
bd7854cb | 1794 | select_timeout(int fds) /* I - Number of descriptors returned */ |
ef416fc2 | 1795 | { |
ef416fc2 | 1796 | long timeout; /* Timeout for select */ |
1797 | time_t now; /* Current time */ | |
1798 | cupsd_client_t *con; /* Client information */ | |
1799 | cupsd_printer_t *p; /* Printer information */ | |
1800 | cupsd_job_t *job; /* Job information */ | |
1801 | cupsd_subscription_t *sub; /* Subscription information */ | |
1802 | const char *why; /* Debugging aid */ | |
1803 | ||
1804 | ||
1805 | /* | |
1806 | * Check to see if any of the clients have pending data to be | |
1807 | * processed; if so, the timeout should be 0... | |
1808 | */ | |
1809 | ||
bd7854cb | 1810 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
1811 | con; | |
1812 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 1813 | if (con->http.used > 0) |
1814 | return (0); | |
1815 | ||
1816 | /* | |
f7deaa1a | 1817 | * If select has been active in the last second (fds > 0) or we have |
ef416fc2 | 1818 | * many resources in use then don't bother trying to optimize the |
1819 | * timeout, just make it 1 second. | |
1820 | */ | |
1821 | ||
f7deaa1a | 1822 | if (fds > 0 || cupsArrayCount(Clients) > 50) |
ef416fc2 | 1823 | return (1); |
1824 | ||
1825 | /* | |
1826 | * Otherwise, check all of the possible events that we need to wake for... | |
1827 | */ | |
1828 | ||
1829 | now = time(NULL); | |
1830 | timeout = now + 86400; /* 86400 == 1 day */ | |
1831 | why = "do nothing"; | |
1832 | ||
76cd9e37 MS |
1833 | /* |
1834 | * Check whether we are accepting new connections... | |
1835 | */ | |
1836 | ||
1837 | if (ListeningPaused > 0 && cupsArrayCount(Clients) < MaxClients && | |
1838 | ListeningPaused < timeout) | |
1839 | { | |
1840 | if (ListeningPaused <= now) | |
1841 | timeout = now; | |
1842 | else | |
1843 | timeout = ListeningPaused; | |
1844 | ||
1845 | why = "resume listening"; | |
1846 | } | |
1847 | ||
ef416fc2 | 1848 | /* |
1849 | * Check the activity and close old clients... | |
1850 | */ | |
1851 | ||
bd7854cb | 1852 | for (con = (cupsd_client_t *)cupsArrayFirst(Clients); |
1853 | con; | |
1854 | con = (cupsd_client_t *)cupsArrayNext(Clients)) | |
ef416fc2 | 1855 | if ((con->http.activity + Timeout) < timeout) |
1856 | { | |
1857 | timeout = con->http.activity + Timeout; | |
1858 | why = "timeout a client connection"; | |
1859 | } | |
1860 | ||
1861 | /* | |
1862 | * Update the browse list as needed... | |
1863 | */ | |
1864 | ||
1865 | if (Browsing && BrowseLocalProtocols) | |
1866 | { | |
1867 | #ifdef HAVE_LIBSLP | |
1868 | if ((BrowseLocalProtocols & BROWSE_SLP) && (BrowseSLPRefresh < timeout)) | |
1869 | { | |
1870 | timeout = BrowseSLPRefresh; | |
1871 | why = "update SLP browsing"; | |
1872 | } | |
1873 | #endif /* HAVE_LIBSLP */ | |
1874 | ||
b423cd4c | 1875 | #ifdef HAVE_LDAP |
1876 | if ((BrowseLocalProtocols & BROWSE_LDAP) && (BrowseLDAPRefresh < timeout)) | |
1877 | { | |
1878 | timeout = BrowseLDAPRefresh; | |
1879 | why = "update LDAP browsing"; | |
1880 | } | |
1881 | #endif /* HAVE_LDAP */ | |
1882 | ||
f7deaa1a | 1883 | if ((BrowseLocalProtocols & BROWSE_CUPS) && NumBrowsers) |
ef416fc2 | 1884 | { |
1885 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
1886 | p; | |
1887 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1888 | { | |
1889 | if (p->type & CUPS_PRINTER_REMOTE) | |
1890 | { | |
1891 | if ((p->browse_time + BrowseTimeout) < timeout) | |
1892 | { | |
1893 | timeout = p->browse_time + BrowseTimeout; | |
1894 | why = "browse timeout a printer"; | |
1895 | } | |
1896 | } | |
f7deaa1a | 1897 | else if (p->shared && !(p->type & CUPS_PRINTER_IMPLICIT)) |
ef416fc2 | 1898 | { |
1899 | if (BrowseInterval && (p->browse_time + BrowseInterval) < timeout) | |
1900 | { | |
1901 | timeout = p->browse_time + BrowseInterval; | |
1902 | why = "send browse update"; | |
1903 | } | |
1904 | } | |
1905 | } | |
1906 | } | |
1907 | } | |
1908 | ||
1909 | /* | |
1910 | * Check for any active jobs... | |
1911 | */ | |
1912 | ||
1913 | if (timeout > (now + 10) && ActiveJobs) | |
1914 | { | |
1915 | for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs); | |
1916 | job; | |
1917 | job = (cupsd_job_t *)cupsArrayNext(ActiveJobs)) | |
bd7854cb | 1918 | if (job->state_value <= IPP_JOB_PROCESSING) |
ef416fc2 | 1919 | { |
1920 | timeout = now + 10; | |
1921 | why = "process active jobs"; | |
1922 | break; | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | #ifdef HAVE_MALLINFO | |
1927 | /* | |
1928 | * Log memory usage every minute... | |
1929 | */ | |
1930 | ||
1931 | if (LogLevel >= CUPSD_LOG_DEBUG && (mallinfo_time + 60) < timeout) | |
1932 | { | |
1933 | timeout = mallinfo_time + 60; | |
1934 | why = "display memory usage"; | |
1935 | } | |
1936 | #endif /* HAVE_MALLINFO */ | |
1937 | ||
ef416fc2 | 1938 | /* |
1939 | * Expire subscriptions as needed... | |
1940 | */ | |
1941 | ||
1942 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
1943 | sub; | |
1944 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
bd7854cb | 1945 | if (!sub->job && sub->expire && sub->expire < timeout) |
ef416fc2 | 1946 | { |
1947 | timeout = sub->expire; | |
1948 | why = "expire subscription"; | |
1949 | } | |
1950 | ||
1951 | /* | |
1952 | * Adjust from absolute to relative time. If p->browse_time above | |
1953 | * was 0 then we can end up with a negative value here, so check. | |
1954 | * We add 1 second to the timeout since events occur after the | |
1955 | * timeout expires, and limit the timeout to 86400 seconds (1 day) | |
1956 | * to avoid select() timeout limits present on some operating | |
1957 | * systems... | |
1958 | */ | |
1959 | ||
1960 | timeout = timeout - now + 1; | |
1961 | ||
1962 | if (timeout < 1) | |
1963 | timeout = 1; | |
1964 | else if (timeout > 86400) | |
1965 | timeout = 86400; | |
1966 | ||
1967 | /* | |
1968 | * Log and return the timeout value... | |
1969 | */ | |
1970 | ||
f7deaa1a | 1971 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout(%d): %ld seconds to %s", |
1972 | fds, timeout, why); | |
ef416fc2 | 1973 | |
1974 | return (timeout); | |
1975 | } | |
1976 | ||
1977 | ||
1978 | /* | |
1979 | * 'usage()' - Show scheduler usage. | |
1980 | */ | |
1981 | ||
1982 | static void | |
a4d04587 | 1983 | usage(int status) /* O - Exit status */ |
ef416fc2 | 1984 | { |
a4d04587 | 1985 | _cupsLangPuts(status ? stderr : stdout, |
1986 | _("Usage: cupsd [-c config-file] [-f] [-F] [-h] [-l]\n" | |
1987 | "\n" | |
1988 | "-c config-file Load alternate configuration file\n" | |
1989 | "-f Run in the foreground\n" | |
1990 | "-F Run in the foreground but detach\n" | |
1991 | "-h Show this usage message\n" | |
1992 | "-l Run cupsd from launchd(8)\n")); | |
1993 | exit(status); | |
ef416fc2 | 1994 | } |
1995 | ||
1996 | ||
1997 | /* | |
2e4ff8af | 1998 | * End of "$Id: main.c 6914 2007-09-05 21:05:04Z mike $". |
ef416fc2 | 1999 | */ |