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