2 * Main loop for the CUPS scheduler.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
17 #include <sys/resource.h>
20 #endif /* __APPLE__ */
23 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
24 # define SD_JOURNAL_SUPPRESS_LOCATION
25 # include <systemd/sd-journal.h>
26 #endif /* HAVE_ASL_H */
32 #endif /* HAVE_LAUNCH_H */
35 # include <systemd/sd-daemon.h>
36 #endif /* HAVE_SYSTEMD */
39 # define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
40 /* Name of the KeepAlive file */
41 #endif /* HAVE_ONDEMAND */
43 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
45 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
49 #endif /* HAVE_NOTIFY_H */
52 # include <dbus/dbus.h>
53 #endif /* HAVE_DBUS */
55 #ifdef HAVE_SYS_PARAM_H
56 # include <sys/param.h>
57 #endif /* HAVE_SYS_PARAM_H */
64 static void parent_handler(int sig
);
65 static void process_children(void);
66 static void sigchld_handler(int sig
);
67 static void sighup_handler(int sig
);
68 static void sigterm_handler(int sig
);
69 static long select_timeout(int fds
);
70 static void service_checkin(void);
71 static void service_checkout(int shutdown
);
72 static void usage(int status
) _CUPS_NORETURN
;
79 static int parent_signal
= 0;
80 /* Set to signal number from child */
81 static int holdcount
= 0; /* Number of times "hold" was called */
82 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
83 static sigset_t holdmask
; /* Old POSIX signal mask */
84 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
85 static int dead_children
= 0;
87 static int stop_scheduler
= 0;
88 /* Should the scheduler stop? */
89 static time_t local_timeout
= 0;
90 /* Next local printer timeout */
94 * 'main()' - Main entry for the CUPS scheduler.
97 int /* O - Exit status */
98 main(int argc
, /* I - Number of command-line args */
99 char *argv
[]) /* I - Command-line arguments */
101 int i
; /* Looping var */
102 char *opt
; /* Option character */
103 int close_all
= 1, /* Close all file descriptors? */
104 disconnect
= 1, /* Disconnect from controlling terminal? */
105 fg
= 0, /* Run in foreground? */
107 /* Running as child process? */
109 /* Print the sandbox profile to stdout? */
110 int fds
; /* Number of ready descriptors */
111 cupsd_client_t
*con
; /* Current client */
112 cupsd_job_t
*job
; /* Current job */
113 cupsd_listener_t
*lis
; /* Current listener */
114 time_t current_time
, /* Current time */
115 activity
, /* Client activity timer */
116 senddoc_time
, /* Send-Document time */
117 expire_time
, /* Subscription expire time */
118 report_time
, /* Malloc/client/job report time */
119 event_time
; /* Last event notification time */
120 long timeout
; /* Timeout for cupsdDoSelect() */
121 struct rlimit limit
; /* Runtime limit */
122 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
123 struct sigaction action
; /* Actions for POSIX signals */
124 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
126 int use_sysman
= 1; /* Use system management functions? */
128 time_t netif_time
= 0; /* Time since last network update */
129 #endif /* __APPLE__ */
130 #if defined(HAVE_ONDEMAND)
131 int service_idle_exit
;
132 /* Idle exit on select timeout? */
133 #endif /* HAVE_ONDEMAND */
138 * Check for setuid invocation, which we do not support!
141 if (getuid() != geteuid())
143 fputs("cupsd: Cannot run as a setuid program.\n", stderr
);
146 #endif /* HAVE_GETEUID */
149 * Check for command-line arguments...
154 for (i
= 1; i
< argc
; i
++)
155 if (argv
[i
][0] == '-')
156 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
159 case 'C' : /* Run as child with config file */
164 case 'c' : /* Configuration file */
168 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
169 "after \"-c\" option."));
173 if (argv
[i
][0] == '/')
176 * Absolute directory...
179 cupsdSetString(&ConfigurationFile
, argv
[i
]);
184 * Relative directory...
187 char *current
; /* Current directory */
190 * Allocate a buffer for the current working directory to
191 * reduce run-time stack usage; this approximates the
192 * behavior of some implementations of getcwd() when they
193 * are passed a NULL pointer.
196 if ((current
= malloc(1024)) == NULL
)
198 _cupsLangPuts(stderr
,
199 _("cupsd: Unable to get current directory."));
203 if (!getcwd(current
, 1024))
205 _cupsLangPuts(stderr
,
206 _("cupsd: Unable to get current directory."));
211 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
216 case 'f' : /* Run in foreground... */
222 case 'F' : /* Run in foreground, but disconnect from terminal... */
227 case 'h' : /* Show usage/help */
231 case 'l' : /* Started by launchd/systemd/upstart... */
238 _cupsLangPuts(stderr
, _("cupsd: On-demand support not compiled "
239 "in, running in normal mode."));
243 #endif /* HAVE_ONDEMAND */
246 case 'p' : /* Stop immediately for profiling */
247 fputs("cupsd: -p (startup profiling) is for internal testing "
248 "use only!\n", stderr
);
255 case 'P' : /* Disable security profiles */
256 fputs("cupsd: -P (disable sandboxing) is for internal testing use only.\n", stderr
);
260 case 's' : /* Set cups-files.conf location */
264 _cupsLangPuts(stderr
, _("cupsd: Expected cups-files.conf "
265 "filename after \"-s\" option."));
269 if (argv
[i
][0] != '/')
272 * Relative filename not allowed...
275 _cupsLangPuts(stderr
, _("cupsd: Relative cups-files.conf "
276 "filename not allowed."));
280 cupsdSetString(&CupsFilesFile
, argv
[i
]);
284 case 'S' : /* Disable system management functions */
285 fputs("cupsd: -S (disable system management) for internal "
286 "testing use only!\n", stderr
);
289 #endif /* __APPLE__ */
291 case 't' : /* Test the cupsd.conf file... */
298 case 'T' : /* Print security profile */
305 default : /* Unknown option */
306 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
313 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting."),
318 if (!ConfigurationFile
)
319 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
323 char *filename
, /* Copy of cupsd.conf filename */
324 *slash
; /* Final slash in cupsd.conf filename */
325 size_t len
; /* Size of buffer */
327 len
= strlen(ConfigurationFile
) + 15;
328 if ((filename
= malloc(len
)) == NULL
)
330 _cupsLangPrintf(stderr
,
331 _("cupsd: Unable to get path to "
332 "cups-files.conf file."));
336 strlcpy(filename
, ConfigurationFile
, len
);
337 if ((slash
= strrchr(filename
, '/')) == NULL
)
339 _cupsLangPrintf(stderr
,
340 _("cupsd: Unable to get path to "
341 "cups-files.conf file."));
345 strlcpy(slash
, "/cups-files.conf", len
- (size_t)(slash
- filename
));
346 cupsdSetString(&CupsFilesFile
, filename
);
353 * Make sure we aren't tying up any filesystems...
359 * Disconnect from the controlling terminal...
368 * Close all open files...
371 getrlimit(RLIMIT_NOFILE
, &limit
);
373 for (i
= 0; i
< (int)limit
.rlim_cur
&& i
< 1024; i
++)
377 * Redirect stdin/out/err to /dev/null...
380 if ((i
= open("/dev/null", O_RDONLY
)) != 0)
386 if ((i
= open("/dev/null", O_WRONLY
)) != 1)
392 if ((i
= open("/dev/null", O_WRONLY
)) != 2)
399 LogStderr
= cupsFileStderr();
402 * Run in the background as needed...
408 * Setup signal handlers for the parent...
411 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
412 sigset(SIGUSR1
, parent_handler
);
413 sigset(SIGCHLD
, parent_handler
);
415 sigset(SIGHUP
, SIG_IGN
);
416 #elif defined(HAVE_SIGACTION)
417 memset(&action
, 0, sizeof(action
));
418 sigemptyset(&action
.sa_mask
);
419 sigaddset(&action
.sa_mask
, SIGUSR1
);
420 action
.sa_handler
= parent_handler
;
421 sigaction(SIGUSR1
, &action
, NULL
);
422 sigaction(SIGCHLD
, &action
, NULL
);
424 sigemptyset(&action
.sa_mask
);
425 action
.sa_handler
= SIG_IGN
;
426 sigaction(SIGHUP
, &action
, NULL
);
428 signal(SIGUSR1
, parent_handler
);
429 signal(SIGCLD
, parent_handler
);
431 signal(SIGHUP
, SIG_IGN
);
432 #endif /* HAVE_SIGSET */
437 * OK, wait for the child to startup and send us SIGUSR1 or to crash
438 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
439 * might be sent by the init script to restart the scheduler...
442 for (; parent_signal
== 0;)
445 if (parent_signal
== SIGUSR1
)
453 else if (WIFEXITED(i
))
455 fprintf(stderr
, "cupsd: Child exited with status %d\n",
461 fprintf(stderr
, "cupsd: Child exited on signal %d\n", WTERMSIG(i
));
466 #if defined(__OpenBSD__) && OpenBSD < 201211
468 * Call _thread_sys_closefrom() so the child process doesn't reset the
469 * parent's file descriptors to be blocking. This is a workaround for a
470 * limitation of userland libpthread on older versions of OpenBSD.
473 _thread_sys_closefrom(0);
474 #endif /* __OpenBSD__ && OpenBSD < 201211 */
477 * Since many system libraries create fork-unsafe data on execution of a
478 * program, we need to re-execute the background cupsd with the "-C" and "-s"
479 * options to avoid problems. Unfortunately, we also have to assume that
480 * argv[0] contains the name of the cupsd executable - there is no portable
481 * way to get the real pathname...
484 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, "-s", CupsFilesFile
, (char *)0);
489 * Let the system know we are busy while we bring up cupsd...
492 cupsdSetBusyState(1);
495 * Set the timezone info...
501 setlocale(LC_TIME
, "");
504 #ifdef HAVE_DBUS_THREADS_INIT
506 * Enable threading support for D-BUS...
509 dbus_threads_init_default();
510 #endif /* HAVE_DBUS_THREADS_INIT */
513 * Set the maximum number of files...
516 getrlimit(RLIMIT_NOFILE
, &limit
);
518 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
519 if (limit
.rlim_max
> FD_SETSIZE
)
522 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
524 if (limit
.rlim_max
== RLIM_INFINITY
)
527 #endif /* RLIM_INFINITY */
528 MaxFDs
= limit
.rlim_max
;
530 limit
.rlim_cur
= (rlim_t
)MaxFDs
;
532 setrlimit(RLIMIT_NOFILE
, &limit
);
537 * Read configuration...
540 if (!cupsdReadConfiguration())
542 else if (TestConfigFile
)
544 printf("\"%s\" is OK.\n", CupsFilesFile
);
545 printf("\"%s\" is OK.\n", ConfigurationFile
);
548 else if (print_profile
)
550 cups_file_t
*fp
; /* File pointer */
551 const char *profile
= cupsdCreateProfile(42, 0);
553 char line
[1024]; /* Line from file */
556 if ((fp
= cupsFileOpen(profile
, "r")) == NULL
)
558 printf("Unable to open profile file \"%s\": %s\n", profile
? profile
: "(null)", strerror(errno
));
562 while (cupsFileGets(fp
, line
, sizeof(line
)))
571 * Clean out old temp files and printer cache data.
574 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
575 cupsdCleanFiles(TempDir
, NULL
);
577 cupsdCleanFiles(CacheDir
, "*.ipp");
580 * If we were started on demand by launchd or systemd get the listen sockets
581 * file descriptors...
588 * Startup the server...
596 * Catch hangup and child signals and ignore broken pipes...
599 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
600 sigset(SIGCHLD
, sigchld_handler
);
601 sigset(SIGHUP
, sighup_handler
);
602 sigset(SIGPIPE
, SIG_IGN
);
603 sigset(SIGTERM
, sigterm_handler
);
604 #elif defined(HAVE_SIGACTION)
605 memset(&action
, 0, sizeof(action
));
607 sigemptyset(&action
.sa_mask
);
608 sigaddset(&action
.sa_mask
, SIGTERM
);
609 sigaddset(&action
.sa_mask
, SIGCHLD
);
610 action
.sa_handler
= sigchld_handler
;
611 sigaction(SIGCHLD
, &action
, NULL
);
613 sigemptyset(&action
.sa_mask
);
614 sigaddset(&action
.sa_mask
, SIGHUP
);
615 action
.sa_handler
= sighup_handler
;
616 sigaction(SIGHUP
, &action
, NULL
);
618 sigemptyset(&action
.sa_mask
);
619 action
.sa_handler
= SIG_IGN
;
620 sigaction(SIGPIPE
, &action
, NULL
);
622 sigemptyset(&action
.sa_mask
);
623 sigaddset(&action
.sa_mask
, SIGTERM
);
624 sigaddset(&action
.sa_mask
, SIGCHLD
);
625 action
.sa_handler
= sigterm_handler
;
626 sigaction(SIGTERM
, &action
, NULL
);
628 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
629 signal(SIGHUP
, sighup_handler
);
630 signal(SIGPIPE
, SIG_IGN
);
631 signal(SIGTERM
, sigterm_handler
);
632 #endif /* HAVE_SIGSET */
635 * Initialize authentication certificates...
641 * If we are running in the background, signal the parent process that
642 * we are up and running...
645 if (!fg
|| run_as_child
)
648 * Send a signal to the parent process, but only if the parent is
649 * not PID 1 (init). This avoids accidentally shutting down the
650 * system on OpenBSD if you CTRL-C the server before it is up...
653 i
= getppid(); /* Save parent PID to avoid race condition */
661 * Start power management framework...
665 cupsdStartSystemMonitor();
666 #endif /* __APPLE__ */
669 * Send server-started event...
674 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started on demand.");
676 #endif /* HAVE_ONDEMAND */
678 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started in foreground.");
680 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started in background.");
682 cupsdSetBusyState(0);
685 * Start any pending print jobs...
694 current_time
= time(NULL
);
695 event_time
= current_time
;
696 expire_time
= current_time
;
700 senddoc_time
= current_time
;
702 while (!stop_scheduler
)
705 * Check if there are dead children to handle...
712 * Check if we need to load the server configuration file...
718 * Close any idle clients...
721 if (cupsArrayCount(Clients
) > 0)
723 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
725 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
726 if (httpGetState(con
->http
) == HTTP_WAITING
)
727 cupsdCloseClient(con
);
729 con
->http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
731 cupsdPauseListening();
735 * Restart if all clients are closed and all jobs finished, or
736 * if the reload timeout has elapsed...
739 if ((cupsArrayCount(Clients
) == 0 &&
740 (cupsArrayCount(PrintingJobs
) == 0 || NeedReload
!= RELOAD_ALL
)) ||
741 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
744 * Shutdown the server...
750 #endif /* HAVE_ONDEMAND */
757 * Read configuration...
760 if (!cupsdReadConfiguration())
762 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
763 sd_journal_print(LOG_ERR
, "Unable to read configuration file \"%s\" - exiting.", ConfigurationFile
);
765 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting.", ConfigurationFile
);
766 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
772 * Startup the server...
780 * Send a server-restarted event...
783 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
784 "Scheduler restarted.");
789 * Check for available input or ready output. If cupsdDoSelect()
790 * returns 0 or -1, something bad happened and we should exit
793 * Note that we at least have one listening socket open at all
797 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
802 * If no other work is scheduled and we're being controlled by launchd,
803 * systemd, etc. then timeout after 'IdleExitTimeout' seconds of
807 if (timeout
== 86400 && OnDemand
&& IdleExitTimeout
&&
810 # endif /* HAVE_SYSTEMD */
811 !cupsArrayCount(ActiveJobs
))
813 cupsd_printer_t
*p
= NULL
; /* Current printer */
815 if (Browsing
&& BrowseLocalProtocols
)
817 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
); p
; p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
824 timeout
= IdleExitTimeout
;
825 service_idle_exit
= 1;
829 service_idle_exit
= 0;
830 #endif /* HAVE_ONDEMAND */
832 if ((fds
= cupsdDoSelect(timeout
)) < 0)
835 * Got an error from select!
838 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
839 cupsd_printer_t
*p
; /* Current printer */
840 #endif /* HAVE_DNSSD || HAVE_AVAHI */
842 if (errno
== EINTR
) /* Just interrupted by a signal */
846 * Log all sorts of debug info to help track down the problem.
849 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
852 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
854 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
855 cupsdLogMessage(CUPSD_LOG_EMERG
,
856 "Clients[%d] = %d, file = %d, state = %d",
857 i
, con
->number
, con
->file
, httpGetState(con
->http
));
859 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
861 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
862 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
864 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
867 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
869 #endif /* __APPLE__ */
871 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
873 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
874 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
876 job
->status_buffer
? job
->status_buffer
->fd
: -1,
877 job
->print_pipes
[0], job
->print_pipes
[1],
878 job
->back_pipes
[0], job
->back_pipes
[1]);
880 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
881 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
883 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
884 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
885 p
->reg_name
? p
->reg_name
: "(null)");
886 #endif /* HAVE_DNSSD || HAVE_AVAHI */
891 current_time
= time(NULL
);
894 * Write dirty config/state files...
897 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
902 * If we are going to sleep and still have pending jobs, stop them after
903 * a period of time...
906 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
907 cupsArrayCount(PrintingJobs
) > 0)
910 cupsdStopAllJobs(CUPSD_JOB_DEFAULT
, 5);
912 #endif /* __APPLE__ */
916 * Update the network interfaces once a minute...
919 if ((current_time
- netif_time
) >= 60)
921 netif_time
= current_time
;
924 #endif /* !__APPLE__ */
928 * If no other work was scheduled and we're being controlled by launchd,
929 * systemd, or upstart then timeout after 'LaunchdTimeout' seconds of
933 if (!fds
&& service_idle_exit
)
935 cupsdLogMessage(CUPSD_LOG_INFO
,
936 "Printer sharing is off and there are no jobs pending, "
937 "will restart on demand.");
941 #endif /* HAVE_ONDEMAND */
944 * Resume listening for new connections as needed...
947 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
948 cupsArrayCount(Clients
) < MaxClients
)
949 cupsdResumeListening();
952 * Expire subscriptions and unload completed jobs as needed...
955 if (current_time
> expire_time
)
957 if (cupsArrayCount(Subscriptions
) > 0)
958 cupsdExpireSubscriptions(NULL
, NULL
);
960 cupsdUnloadCompletedJobs();
962 expire_time
= current_time
;
966 * Delete stale local printers...
969 if (current_time
>= local_timeout
)
971 cupsdDeleteTemporaryPrinters(0);
975 #ifndef HAVE_AUTHORIZATION_H
977 * Update the root certificate once every 5 minutes if we have client
981 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
982 !RunUser
&& cupsArrayCount(Clients
))
985 * Update the root certificate...
989 cupsdAddCert(0, "root", cupsdDefaultAuthType());
991 #endif /* !HAVE_AUTHORIZATION_H */
994 * Check for new data on the client sockets...
997 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
999 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1002 * Process pending data in the input buffer...
1005 if (httpGetReady(con
->http
))
1007 cupsdReadClient(con
);
1012 * Check the activity and close old clients...
1015 activity
= current_time
- Timeout
;
1016 if (httpGetActivity(con
->http
) < activity
&& !con
->pipe_pid
)
1018 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Closing client %d after %d seconds of inactivity.", con
->number
, Timeout
);
1020 cupsdCloseClient(con
);
1026 * Update any pending multi-file documents...
1029 if ((current_time
- senddoc_time
) >= 10)
1032 senddoc_time
= current_time
;
1036 * Clean job history...
1039 if (JobHistoryUpdate
&& current_time
>= JobHistoryUpdate
)
1043 * Log statistics at most once a minute when in debug mode...
1046 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
1048 size_t string_count
, /* String count */
1049 alloc_bytes
, /* Allocated string bytes */
1050 total_bytes
; /* Total string bytes */
1051 #ifdef HAVE_MALLINFO
1052 struct mallinfo mem
; /* Malloc information */
1056 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1057 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1058 mem
.usmblks
+ mem
.uordblks
);
1059 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1060 mem
.fsmblks
+ mem
.fordblks
);
1061 #endif /* HAVE_MALLINFO */
1063 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1064 cupsArrayCount(Clients
));
1065 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1066 cupsArrayCount(Jobs
));
1067 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1068 cupsArrayCount(ActiveJobs
));
1069 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1070 cupsArrayCount(Printers
));
1072 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1073 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1074 "Report: stringpool-string-count=" CUPS_LLFMT
,
1075 CUPS_LLCAST string_count
);
1076 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1077 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1078 CUPS_LLCAST alloc_bytes
);
1079 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1080 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1081 CUPS_LLCAST total_bytes
);
1083 report_time
= current_time
;
1087 * Handle OS-specific event notification for any events that have
1088 * accumulated. Don't send these more than once a second...
1091 if (LastEvent
&& (current_time
- event_time
) >= 1)
1093 #ifdef HAVE_NOTIFY_POST
1094 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1095 CUPSD_EVENT_PRINTER_DELETED
|
1096 CUPSD_EVENT_PRINTER_MODIFIED
))
1098 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1099 "notify_post(\"com.apple.printerListChange\")");
1100 notify_post("com.apple.printerListChange");
1103 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1105 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1106 "notify_post(\"com.apple.printerHistoryChange\")");
1107 notify_post("com.apple.printerHistoryChange");
1110 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1111 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1112 CUPSD_EVENT_JOB_PROGRESS
))
1114 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1115 "notify_post(\"com.apple.jobChange\")");
1116 notify_post("com.apple.jobChange");
1118 #endif /* HAVE_NOTIFY_POST */
1121 * Reset the accumulated events...
1124 LastEvent
= CUPSD_EVENT_NONE
;
1125 event_time
= current_time
;
1130 * Log a message based on what happened...
1135 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1136 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1137 "Scheduler shutting down normally.");
1141 cupsdLogMessage(CUPSD_LOG_ERROR
,
1142 "Scheduler shutting down due to program error.");
1143 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1144 "Scheduler shutting down due to program error.");
1148 * Close all network clients...
1156 * Update the KeepAlive/PID file as needed...
1159 service_checkout(1);
1168 * Delete all temporary printers...
1171 cupsdDeleteTemporaryPrinters(1);
1175 * Stop monitoring system event monitoring...
1179 cupsdStopSystemMonitor();
1180 #endif /* __APPLE__ */
1184 return (!stop_scheduler
);
1189 * 'cupsdAddString()' - Copy and add a string to an array.
1192 int /* O - 1 on success, 0 on failure */
1193 cupsdAddString(cups_array_t
**a
, /* IO - String array */
1194 const char *s
) /* I - String to copy and add */
1197 *a
= cupsArrayNew3((cups_array_func_t
)strcmp
, NULL
,
1198 (cups_ahash_func_t
)NULL
, 0,
1199 (cups_acopy_func_t
)strdup
,
1200 (cups_afree_func_t
)free
);
1202 return (cupsArrayAdd(*a
, (char *)s
));
1207 * 'cupsdCheckProcess()' - Tell the main loop to check for dead children.
1211 cupsdCheckProcess(void)
1214 * Flag that we have dead children...
1222 * 'cupsdClearString()' - Clear a string.
1226 cupsdClearString(char **s
) /* O - String value */
1237 * 'cupsdFreeStrings()' - Free an array of strings.
1241 cupsdFreeStrings(cups_array_t
**a
) /* IO - String array */
1245 cupsArrayDelete(*a
);
1252 * 'cupsdHoldSignals()' - Hold child and termination signals.
1256 cupsdHoldSignals(void)
1258 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1259 sigset_t newmask
; /* New POSIX signal mask */
1260 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1270 #elif defined(HAVE_SIGACTION)
1271 sigemptyset(&newmask
);
1272 sigaddset(&newmask
, SIGTERM
);
1273 sigaddset(&newmask
, SIGCHLD
);
1274 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1275 #endif /* HAVE_SIGSET */
1280 * 'cupsdReleaseSignals()' - Release signals for delivery.
1284 cupsdReleaseSignals(void)
1293 #elif defined(HAVE_SIGACTION)
1294 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1295 #endif /* HAVE_SIGSET */
1300 * 'cupsdSetString()' - Set a string value.
1304 cupsdSetString(char **s
, /* O - New string */
1305 const char *v
) /* I - String value */
1321 * 'cupsdSetStringf()' - Set a formatted string value.
1325 cupsdSetStringf(char **s
, /* O - New string */
1326 const char *f
, /* I - Printf-style format string */
1327 ...) /* I - Additional args as needed */
1329 char v
[65536 + 64]; /* Formatting string value */
1330 va_list ap
; /* Argument pointer */
1331 char *olds
; /* Old string */
1342 vsnprintf(v
, sizeof(v
), f
, ap
);
1356 * 'parent_handler()' - Catch USR1/CHLD signals...
1360 parent_handler(int sig
) /* I - Signal */
1363 * Store the signal we got from the OS and return...
1366 parent_signal
= sig
;
1371 * 'process_children()' - Process all dead children...
1375 process_children(void)
1377 int status
; /* Exit status of child */
1378 int pid
, /* Process ID of child */
1379 job_id
; /* Job ID of child */
1380 cupsd_job_t
*job
; /* Current job */
1381 int i
; /* Looping var */
1382 char name
[1024]; /* Process name */
1383 const char *type
; /* Type of program */
1386 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1389 * Reset the dead_children flag...
1395 * Collect the exit status of some children...
1399 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1400 #elif defined(HAVE_WAIT3)
1401 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1403 if ((pid
= wait(&status
)) > 0)
1404 #endif /* HAVE_WAITPID */
1407 * Collect the name of the process that finished...
1410 cupsdFinishProcess(pid
, name
, sizeof(name
), &job_id
);
1413 * Delete certificates for CGI processes...
1417 cupsdDeleteCert(pid
);
1420 * Handle completed job filters...
1424 job
= cupsdFindJob(job_id
);
1430 for (i
= 0; job
->filters
[i
]; i
++)
1431 if (job
->filters
[i
] == pid
)
1434 if (job
->filters
[i
] || job
->backend
== pid
)
1437 * OK, this process has gone away; what's left?
1440 if (job
->filters
[i
])
1442 job
->filters
[i
] = -pid
;
1447 job
->backend
= -pid
;
1451 if (status
&& status
!= SIGTERM
&& status
!= SIGKILL
&&
1455 * An error occurred; save the exit status so we know to stop
1456 * the printer or cancel the job when all of the filters finish...
1458 * A negative status indicates that the backend failed and the
1459 * printer needs to be stopped.
1461 * In order to preserve the most serious status, we always log
1462 * when a process dies due to a signal (e.g. SIGABRT, SIGSEGV,
1463 * and SIGBUS) and prefer to log the backend exit status over a
1467 int old_status
= abs(job
->status
);
1469 if (WIFSIGNALED(status
) || /* This process crashed, or */
1470 !job
->status
|| /* No process had a status, or */
1471 (!job
->filters
[i
] && WIFEXITED(old_status
)))
1472 { /* Backend and filter didn't crash */
1473 if (job
->filters
[i
])
1474 job
->status
= status
; /* Filter failed */
1476 job
->status
= -status
; /* Backend failed */
1479 if (job
->state_value
== IPP_JOB_PROCESSING
&&
1480 job
->status_level
> CUPSD_LOG_ERROR
&&
1481 (job
->filters
[i
] || !WIFEXITED(status
)))
1483 char message
[1024]; /* New printer-state-message */
1486 job
->status_level
= CUPSD_LOG_ERROR
;
1488 snprintf(message
, sizeof(message
), "%s failed", type
);
1492 strlcpy(job
->printer
->state_message
, message
,
1493 sizeof(job
->printer
->state_message
));
1499 if (!job
->printer_message
&& job
->attrs
)
1501 if ((job
->printer_message
=
1502 ippFindAttribute(job
->attrs
, "job-printer-state-message",
1503 IPP_TAG_TEXT
)) == NULL
)
1504 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
,
1506 "job-printer-state-message",
1510 if (job
->printer_message
)
1511 ippSetString(job
->attrs
, &job
->printer_message
, 0, message
);
1516 * If this is not the last file in a job, see if all of the
1517 * filters are done, and if so move to the next file.
1520 if (job
->state_value
>= IPP_JOB_CANCELED
)
1523 * Remove the job from the active list if there are no processes still
1527 for (i
= 0; job
->filters
[i
] < 0; i
++);
1529 if (!job
->filters
[i
] && job
->backend
<= 0)
1530 cupsArrayRemove(ActiveJobs
, job
);
1532 else if (job
->current_file
< job
->num_files
&& job
->printer
)
1534 for (i
= 0; job
->filters
[i
] < 0; i
++);
1536 if (!job
->filters
[i
] &&
1537 (!job
->printer
->pc
|| !job
->printer
->pc
->single_file
||
1541 * Process the next file...
1544 cupsdContinueJob(job
);
1551 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1552 * since they come when we kill/end a process...
1555 if (status
== SIGTERM
|| status
== SIGKILL
)
1557 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1558 "PID %d (%s) was terminated normally with signal %d.", pid
,
1561 else if (status
== SIGPIPE
)
1563 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1564 "PID %d (%s) did not catch or ignore signal %d.", pid
, name
,
1569 if (WIFEXITED(status
))
1571 int code
= WEXITSTATUS(status
); /* Exit code */
1574 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1575 "PID %d (%s) stopped with status %d (%s)", pid
, name
,
1576 code
, strerror(code
- 100));
1578 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1579 "PID %d (%s) stopped with status %d.", pid
, name
, code
);
1582 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) crashed on signal %d.",
1583 pid
, name
, WTERMSIG(status
));
1585 if (LogLevel
< CUPSD_LOG_DEBUG
)
1586 cupsdLogJob(job
, CUPSD_LOG_INFO
,
1587 "Hint: Try setting the LogLevel to \"debug\" to find out "
1591 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1596 * If wait*() is interrupted by a signal, tell main() to call us again...
1599 if (pid
< 0 && errno
== EINTR
)
1605 * 'select_timeout()' - Calculate the select timeout value.
1609 static long /* O - Number of seconds */
1610 select_timeout(int fds
) /* I - Number of descriptors returned */
1612 long timeout
; /* Timeout for select */
1613 time_t now
; /* Current time */
1614 cupsd_client_t
*con
; /* Client information */
1615 cupsd_job_t
*job
; /* Job information */
1616 cupsd_printer_t
*printer
; /* Printer information */
1617 const char *why
; /* Debugging aid */
1620 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout: JobHistoryUpdate=%ld",
1621 (long)JobHistoryUpdate
);
1624 * Check to see if any of the clients have pending data to be
1625 * processed; if so, the timeout should be 0...
1628 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1630 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1631 if (httpGetReady(con
->http
))
1635 * If select has been active in the last second (fds > 0) or we have
1636 * many resources in use then don't bother trying to optimize the
1637 * timeout, just make it 1 second.
1640 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1644 * Otherwise, check all of the possible events that we need to wake for...
1648 timeout
= now
+ 86400; /* 86400 == 1 day */
1653 * When going to sleep, wake up to abort jobs that don't complete in time.
1656 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1658 timeout
= SleepJobs
;
1659 why
= "abort jobs before sleeping";
1661 #endif /* __APPLE__ */
1664 * Check whether we are accepting new connections...
1667 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1668 ListeningPaused
< timeout
)
1670 if (ListeningPaused
<= now
)
1673 timeout
= ListeningPaused
;
1675 why
= "resume listening";
1679 * Check the activity and close old clients...
1682 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1684 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1685 if ((httpGetActivity(con
->http
) + Timeout
) < timeout
)
1687 timeout
= httpGetActivity(con
->http
) + Timeout
;
1688 why
= "timeout a client connection";
1692 * Write out changes to configuration and state files...
1695 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1697 timeout
= DirtyCleanTime
;
1698 why
= "write dirty config/state files";
1702 * Check for any job activity...
1705 if (JobHistoryUpdate
&& timeout
> JobHistoryUpdate
)
1707 timeout
= JobHistoryUpdate
;
1708 why
= "update job history";
1711 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1713 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1715 if (job
->cancel_time
&& job
->cancel_time
< timeout
)
1717 timeout
= job
->cancel_time
;
1718 why
= "cancel stuck jobs";
1721 if (job
->kill_time
&& job
->kill_time
< timeout
)
1723 timeout
= job
->kill_time
;
1724 why
= "kill unresponsive jobs";
1727 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1729 timeout
= job
->hold_until
;
1730 why
= "release held jobs";
1733 if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1736 why
= "start pending jobs";
1742 * Check for temporary printers that need to be deleted...
1745 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
); printer
; printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1747 if (printer
->temporary
&& !printer
->job
&& (!local_timeout
|| local_timeout
> (printer
->state_time
+ 60)))
1748 local_timeout
= printer
->state_time
+ 60;
1751 if (timeout
> local_timeout
&& local_timeout
)
1753 timeout
= local_timeout
;
1754 why
= "delete stale local printers";
1758 * Adjust from absolute to relative time. We add 1 second to the timeout since
1759 * events occur after the timeout expires, and limit the timeout to 86400
1760 * seconds (1 day) to avoid select() timeout limits present on some operating
1764 timeout
= timeout
- now
+ 1;
1768 else if (timeout
> 86400)
1772 * Log and return the timeout value...
1775 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
1783 * 'sigchld_handler()' - Handle 'child' signals from old processes.
1787 sigchld_handler(int sig
) /* I - Signal number */
1792 * Flag that we have dead children...
1798 * Reset the signal handler as needed...
1801 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1802 signal(SIGCLD
, sigchld_handler
);
1803 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1808 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
1812 sighup_handler(int sig
) /* I - Signal number */
1816 NeedReload
= RELOAD_ALL
;
1817 ReloadTime
= time(NULL
);
1819 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1820 signal(SIGHUP
, sighup_handler
);
1821 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1826 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
1830 sigterm_handler(int sig
) /* I - Signal number */
1832 (void)sig
; /* remove compiler warnings... */
1835 * Flag that we should stop and return...
1842 #ifdef HAVE_ONDEMAND
1844 * 'service_add_listener()' - Bind an open fd as a Listener.
1848 service_add_listener(int fd
, /* I - Socket file descriptor */
1849 int idx
) /* I - Listener number, for logging */
1851 cupsd_listener_t
*lis
; /* Listeners array */
1852 http_addr_t addr
; /* Address variable */
1853 socklen_t addrlen
; /* Length of address */
1854 char s
[256]; /* String addresss */
1857 addrlen
= sizeof(addr
);
1859 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1861 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_add_listener: Unable to get local address for listener #%d: %s", idx
+ 1, strerror(errno
));
1865 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Listener #%d at fd %d, \"%s\".", idx
+ 1, fd
, httpAddrString(&addr
, s
, sizeof(s
)));
1868 * Try to match the on-demand socket address to one of the listeners...
1871 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1873 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1874 if (httpAddrEqual(&lis
->address
, &addr
))
1878 * Add a new listener If there's no match...
1883 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Matched existing listener #%d to %s.", idx
+ 1, httpAddrString(&(lis
->address
), s
, sizeof(s
)));
1887 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Adding new listener #%d for %s.", idx
+ 1, httpAddrString(&addr
, s
, sizeof(s
)));
1889 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1891 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_add_listener: Unable to allocate listener: %s.", strerror(errno
));
1896 cupsArrayAdd(Listeners
, lis
);
1898 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1905 if (httpAddrPort(&(lis
->address
)) == 443)
1906 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1907 # endif /* HAVE_SSL */
1909 #endif /* HAVE_ONDEMAND */
1913 * 'service_checkin()' - Check-in with launchd and collect the listening fds.
1917 service_checkin(void)
1919 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: pid=%d", (int)getpid());
1924 int error
; /* Check-in error, if any */
1925 size_t i
, /* Looping var */
1926 count
; /* Number of listeners */
1927 int *ld_sockets
; /* Listener sockets */
1930 * Check-in with launchd...
1933 if ((error
= launch_activate_socket("Listeners", &ld_sockets
, &count
)) != 0)
1935 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets: %s", strerror(error
));
1937 return; /* anti-compiler-warning */
1941 * Try to match the launchd sockets to the cupsd listeners...
1944 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: %d listeners.", (int)count
);
1946 for (i
= 0; i
< count
; i
++)
1947 service_add_listener(ld_sockets
[i
], (int)i
);
1952 xpc_transaction_begin();
1953 # endif /* __APPLE__ */
1956 #elif defined(HAVE_SYSTEMD)
1959 int i
, /* Looping var */
1960 count
; /* Number of listeners */
1963 * Check-in with systemd...
1966 if ((count
= sd_listen_fds(0)) < 0)
1968 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets: %s", strerror(-count
));
1970 return; /* anti-compiler-warning */
1974 * Try to match the systemd sockets to the cupsd listeners...
1977 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: %d listeners.", count
);
1979 for (i
= 0; i
< count
; i
++)
1980 service_add_listener(SD_LISTEN_FDS_START
+ i
, i
);
1983 #elif defined(HAVE_UPSTART)
1986 const char *e
; /* Environment var */
1987 int fd
; /* File descriptor */
1990 if (!(e
= getenv("UPSTART_EVENTS")))
1992 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: We did not get started via Upstart.");
1997 if (strcasecmp(e
, "socket"))
1999 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: We did not get triggered via an Upstart socket event.");
2004 if ((e
= getenv("UPSTART_FDS")) == NULL
)
2006 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets from UPSTART_FDS.");
2011 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: UPSTART_FDS=%s", e
);
2013 fd
= (int)strtol(e
, NULL
, 10);
2016 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno
));
2022 * Upstart only supportst a single on-demand socket file descriptor...
2025 service_add_listener(fd
, 0);
2027 #endif /* HAVE_LAUNCHD */
2032 * 'service_checkout()' - Update the KeepAlive/PID file as needed.
2036 service_checkout(int shutdown
) /* I - Shutting down? */
2038 cups_file_t
*fp
; /* File */
2039 char pidfile
[1024]; /* PID/KeepAlive file */
2043 * When running on-demand, use the KeepAlive file, otherwise write a PID file
2047 #ifdef HAVE_ONDEMAND
2050 strlcpy(pidfile
, CUPS_KEEPALIVE
, sizeof(pidfile
));
2052 if (cupsArrayCount(ActiveJobs
) || /* Active jobs */
2053 WebInterface
|| /* Web interface enabled */
2054 NeedReload
|| /* Doing a reload */
2055 (Browsing
&& BrowseLocalProtocols
&& cupsArrayCount(Printers
)))
2056 /* Printers being shared */
2059 * Create or remove the "keep-alive" file based on whether there are active
2060 * jobs or shared printers to advertise...
2067 #endif /* HAVE_ONDEMAND */
2068 snprintf(pidfile
, sizeof(pidfile
), "%s/cupsd.pid", StateDir
);
2072 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Removing KeepAlive/PID file \"%s\".", pidfile
);
2078 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Creating KeepAlive/PID file \"%s\".", pidfile
);
2080 if ((fp
= cupsFileOpen(pidfile
, "w")) != NULL
)
2083 * Save the PID in the file...
2086 cupsFilePrintf(fp
, "%d\n", (int)getpid());
2090 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create KeepAlive/PID file \"%s\": %s", pidfile
, strerror(errno
));
2094 if (OnDemand
&& shutdown
)
2095 xpc_transaction_end();
2096 # endif /* __APPLE__ */
2101 * 'usage()' - Show scheduler usage.
2105 usage(int status
) /* O - Exit status */
2107 FILE *fp
= status
? stderr
: stdout
; /* Output file */
2110 _cupsLangPuts(fp
, _("Usage: cupsd [options]"));
2111 _cupsLangPuts(fp
, _("Options:"));
2112 _cupsLangPuts(fp
, _(" -c cupsd.conf Set cupsd.conf file to use."));
2113 _cupsLangPuts(fp
, _(" -f Run in the foreground."));
2114 _cupsLangPuts(fp
, _(" -F Run in the foreground but detach from console."));
2115 _cupsLangPuts(fp
, _(" -h Show this usage message."));
2116 #ifdef HAVE_ONDEMAND
2117 _cupsLangPuts(fp
, _(" -l Run cupsd on demand."));
2118 #endif /* HAVE_ONDEMAND */
2119 _cupsLangPuts(fp
, _(" -s cups-files.conf Set cups-files.conf file to use."));
2120 _cupsLangPuts(fp
, _(" -t Test the configuration file."));