2 * Main loop for the CUPS scheduler.
4 * Copyright 2007-2017 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 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
21 # define SD_JOURNAL_SUPPRESS_LOCATION
22 # include <systemd/sd-journal.h>
23 #endif /* HAVE_ASL_H */
29 #endif /* HAVE_LAUNCH_H */
32 # include <systemd/sd-daemon.h>
33 #endif /* HAVE_SYSTEMD */
36 # define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
37 /* Name of the KeepAlive file */
38 #endif /* HAVE_ONDEMAND */
40 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
42 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
46 #endif /* HAVE_NOTIFY_H */
49 # include <dbus/dbus.h>
50 #endif /* HAVE_DBUS */
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
54 #endif /* HAVE_SYS_PARAM_H */
61 static void parent_handler(int sig
);
62 static void process_children(void);
63 static void sigchld_handler(int sig
);
64 static void sighup_handler(int sig
);
65 static void sigterm_handler(int sig
);
66 static long select_timeout(int fds
);
67 static void service_checkin(void);
68 static void service_checkout(int shutdown
);
69 static void usage(int status
) __attribute__((noreturn
));
76 static int parent_signal
= 0;
77 /* Set to signal number from child */
78 static int holdcount
= 0; /* Number of times "hold" was called */
79 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
80 static sigset_t holdmask
; /* Old POSIX signal mask */
81 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
82 static int dead_children
= 0;
84 static int stop_scheduler
= 0;
85 /* Should the scheduler stop? */
86 static time_t local_timeout
= 0;
87 /* Next local printer timeout */
91 * 'main()' - Main entry for the CUPS scheduler.
94 int /* O - Exit status */
95 main(int argc
, /* I - Number of command-line args */
96 char *argv
[]) /* I - Command-line arguments */
98 int i
; /* Looping var */
99 char *opt
; /* Option character */
100 int close_all
= 1, /* Close all file descriptors? */
101 disconnect
= 1, /* Disconnect from controlling terminal? */
102 fg
= 0, /* Run in foreground? */
104 /* Running as child process? */
106 /* Print the sandbox profile to stdout? */
107 int fds
; /* Number of ready descriptors */
108 cupsd_client_t
*con
; /* Current client */
109 cupsd_job_t
*job
; /* Current job */
110 cupsd_listener_t
*lis
; /* Current listener */
111 time_t current_time
, /* Current time */
112 activity
, /* Client activity timer */
113 senddoc_time
, /* Send-Document time */
114 expire_time
, /* Subscription expire time */
115 report_time
, /* Malloc/client/job report time */
116 event_time
; /* Last event notification time */
117 long timeout
; /* Timeout for cupsdDoSelect() */
118 struct rlimit limit
; /* Runtime limit */
119 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
120 struct sigaction action
; /* Actions for POSIX signals */
121 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
123 int use_sysman
= 1; /* Use system management functions? */
125 time_t netif_time
= 0; /* Time since last network update */
126 #endif /* __APPLE__ */
127 #if defined(HAVE_ONDEMAND)
128 int service_idle_exit
;
129 /* Idle exit on select timeout? */
130 #endif /* HAVE_ONDEMAND */
135 * Check for setuid invocation, which we do not support!
138 if (getuid() != geteuid())
140 fputs("cupsd: Cannot run as a setuid program.\n", stderr
);
143 #endif /* HAVE_GETEUID */
146 * Check for command-line arguments...
152 if (getenv("CUPSD_LAUNCHD"))
159 #endif /* HAVE_LAUNCHD */
161 for (i
= 1; i
< argc
; i
++)
162 if (argv
[i
][0] == '-')
163 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
166 case 'C' : /* Run as child with config file */
171 case 'c' : /* Configuration file */
175 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
176 "after \"-c\" option."));
180 if (argv
[i
][0] == '/')
183 * Absolute directory...
186 cupsdSetString(&ConfigurationFile
, argv
[i
]);
191 * Relative directory...
194 char *current
; /* Current directory */
197 * Allocate a buffer for the current working directory to
198 * reduce run-time stack usage; this approximates the
199 * behavior of some implementations of getcwd() when they
200 * are passed a NULL pointer.
203 if ((current
= malloc(1024)) == NULL
)
205 _cupsLangPuts(stderr
,
206 _("cupsd: Unable to get current directory."));
210 if (!getcwd(current
, 1024))
212 _cupsLangPuts(stderr
,
213 _("cupsd: Unable to get current directory."));
218 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
223 case 'f' : /* Run in foreground... */
229 case 'F' : /* Run in foreground, but disconnect from terminal... */
234 case 'h' : /* Show usage/help */
238 case 'l' : /* Started by launchd/systemd/upstart... */
245 _cupsLangPuts(stderr
, _("cupsd: On-demand support not compiled "
246 "in, running in normal mode."));
250 #endif /* HAVE_ONDEMAND */
253 case 'p' : /* Stop immediately for profiling */
254 fputs("cupsd: -p (startup profiling) is for internal testing "
255 "use only!\n", stderr
);
262 case 'P' : /* Disable security profiles */
263 fputs("cupsd: -P (disable sandboxing) is for internal testing use only.\n", stderr
);
267 case 's' : /* Set cups-files.conf location */
271 _cupsLangPuts(stderr
, _("cupsd: Expected cups-files.conf "
272 "filename after \"-s\" option."));
276 if (argv
[i
][0] != '/')
279 * Relative filename not allowed...
282 _cupsLangPuts(stderr
, _("cupsd: Relative cups-files.conf "
283 "filename not allowed."));
287 cupsdSetString(&CupsFilesFile
, argv
[i
]);
291 case 'S' : /* Disable system management functions */
292 fputs("cupsd: -S (disable system management) for internal "
293 "testing use only!\n", stderr
);
296 #endif /* __APPLE__ */
298 case 't' : /* Test the cupsd.conf file... */
305 case 'T' : /* Print security profile */
312 default : /* Unknown option */
313 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
320 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting."),
325 if (!ConfigurationFile
)
326 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
330 char *filename
, /* Copy of cupsd.conf filename */
331 *slash
; /* Final slash in cupsd.conf filename */
332 size_t len
; /* Size of buffer */
334 len
= strlen(ConfigurationFile
) + 15;
335 if ((filename
= malloc(len
)) == NULL
)
337 _cupsLangPrintf(stderr
,
338 _("cupsd: Unable to get path to "
339 "cups-files.conf file."));
343 strlcpy(filename
, ConfigurationFile
, len
);
344 if ((slash
= strrchr(filename
, '/')) == NULL
)
346 _cupsLangPrintf(stderr
,
347 _("cupsd: Unable to get path to "
348 "cups-files.conf file."));
352 strlcpy(slash
, "/cups-files.conf", len
- (size_t)(slash
- filename
));
353 cupsdSetString(&CupsFilesFile
, filename
);
360 * Make sure we aren't tying up any filesystems...
366 * Disconnect from the controlling terminal...
375 * Close all open files...
378 getrlimit(RLIMIT_NOFILE
, &limit
);
380 for (i
= 0; i
< (int)limit
.rlim_cur
&& i
< 1024; i
++)
384 * Redirect stdin/out/err to /dev/null...
387 if ((i
= open("/dev/null", O_RDONLY
)) != 0)
393 if ((i
= open("/dev/null", O_WRONLY
)) != 1)
399 if ((i
= open("/dev/null", O_WRONLY
)) != 2)
406 LogStderr
= cupsFileStderr();
409 * Run in the background as needed...
415 * Setup signal handlers for the parent...
418 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
419 sigset(SIGUSR1
, parent_handler
);
420 sigset(SIGCHLD
, parent_handler
);
422 sigset(SIGHUP
, SIG_IGN
);
423 #elif defined(HAVE_SIGACTION)
424 memset(&action
, 0, sizeof(action
));
425 sigemptyset(&action
.sa_mask
);
426 sigaddset(&action
.sa_mask
, SIGUSR1
);
427 action
.sa_handler
= parent_handler
;
428 sigaction(SIGUSR1
, &action
, NULL
);
429 sigaction(SIGCHLD
, &action
, NULL
);
431 sigemptyset(&action
.sa_mask
);
432 action
.sa_handler
= SIG_IGN
;
433 sigaction(SIGHUP
, &action
, NULL
);
435 signal(SIGUSR1
, parent_handler
);
436 signal(SIGCLD
, parent_handler
);
438 signal(SIGHUP
, SIG_IGN
);
439 #endif /* HAVE_SIGSET */
444 * OK, wait for the child to startup and send us SIGUSR1 or to crash
445 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
446 * might be sent by the init script to restart the scheduler...
449 for (; parent_signal
== 0;)
452 if (parent_signal
== SIGUSR1
)
460 else if (WIFEXITED(i
))
462 fprintf(stderr
, "cupsd: Child exited with status %d\n",
468 fprintf(stderr
, "cupsd: Child exited on signal %d\n", WTERMSIG(i
));
473 #if defined(__OpenBSD__) && OpenBSD < 201211
475 * Call _thread_sys_closefrom() so the child process doesn't reset the
476 * parent's file descriptors to be blocking. This is a workaround for a
477 * limitation of userland libpthread on older versions of OpenBSD.
480 _thread_sys_closefrom(0);
481 #endif /* __OpenBSD__ && OpenBSD < 201211 */
484 * Since many system libraries create fork-unsafe data on execution of a
485 * program, we need to re-execute the background cupsd with the "-C" and "-s"
486 * options to avoid problems. Unfortunately, we also have to assume that
487 * argv[0] contains the name of the cupsd executable - there is no portable
488 * way to get the real pathname...
491 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, "-s", CupsFilesFile
, (char *)0);
496 * Let the system know we are busy while we bring up cupsd...
499 cupsdSetBusyState(1);
502 * Set the timezone info...
508 setlocale(LC_TIME
, "");
511 #ifdef HAVE_DBUS_THREADS_INIT
513 * Enable threading support for D-BUS...
516 dbus_threads_init_default();
517 #endif /* HAVE_DBUS_THREADS_INIT */
520 * Set the maximum number of files...
523 getrlimit(RLIMIT_NOFILE
, &limit
);
525 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
526 if (limit
.rlim_max
> FD_SETSIZE
)
529 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
531 if (limit
.rlim_max
== RLIM_INFINITY
)
534 #endif /* RLIM_INFINITY */
535 MaxFDs
= limit
.rlim_max
;
537 limit
.rlim_cur
= (rlim_t
)MaxFDs
;
539 setrlimit(RLIMIT_NOFILE
, &limit
);
544 * Read configuration...
547 if (!cupsdReadConfiguration())
549 else if (TestConfigFile
)
551 printf("\"%s\" is OK.\n", CupsFilesFile
);
552 printf("\"%s\" is OK.\n", ConfigurationFile
);
555 else if (print_profile
)
557 cups_file_t
*fp
; /* File pointer */
558 const char *profile
= cupsdCreateProfile(42, 0);
560 char line
[1024]; /* Line from file */
563 if ((fp
= cupsFileOpen(profile
, "r")) == NULL
)
565 printf("Unable to open profile file \"%s\": %s\n", profile
? profile
: "(null)", strerror(errno
));
569 while (cupsFileGets(fp
, line
, sizeof(line
)))
578 * Clean out old temp files and printer cache data.
581 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
582 cupsdCleanFiles(TempDir
, NULL
);
584 cupsdCleanFiles(CacheDir
, "*.ipp");
587 * If we were started on demand by launchd or systemd get the listen sockets
588 * file descriptors...
595 * Startup the server...
603 * Catch hangup and child signals and ignore broken pipes...
606 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
607 sigset(SIGCHLD
, sigchld_handler
);
608 sigset(SIGHUP
, sighup_handler
);
609 sigset(SIGPIPE
, SIG_IGN
);
610 sigset(SIGTERM
, sigterm_handler
);
611 #elif defined(HAVE_SIGACTION)
612 memset(&action
, 0, sizeof(action
));
614 sigemptyset(&action
.sa_mask
);
615 sigaddset(&action
.sa_mask
, SIGTERM
);
616 sigaddset(&action
.sa_mask
, SIGCHLD
);
617 action
.sa_handler
= sigchld_handler
;
618 sigaction(SIGCHLD
, &action
, NULL
);
620 sigemptyset(&action
.sa_mask
);
621 sigaddset(&action
.sa_mask
, SIGHUP
);
622 action
.sa_handler
= sighup_handler
;
623 sigaction(SIGHUP
, &action
, NULL
);
625 sigemptyset(&action
.sa_mask
);
626 action
.sa_handler
= SIG_IGN
;
627 sigaction(SIGPIPE
, &action
, NULL
);
629 sigemptyset(&action
.sa_mask
);
630 sigaddset(&action
.sa_mask
, SIGTERM
);
631 sigaddset(&action
.sa_mask
, SIGCHLD
);
632 action
.sa_handler
= sigterm_handler
;
633 sigaction(SIGTERM
, &action
, NULL
);
635 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
636 signal(SIGHUP
, sighup_handler
);
637 signal(SIGPIPE
, SIG_IGN
);
638 signal(SIGTERM
, sigterm_handler
);
639 #endif /* HAVE_SIGSET */
642 * Initialize authentication certificates...
648 * If we are running in the background, signal the parent process that
649 * we are up and running...
652 if (!fg
|| run_as_child
)
655 * Send a signal to the parent process, but only if the parent is
656 * not PID 1 (init). This avoids accidentally shutting down the
657 * system on OpenBSD if you CTRL-C the server before it is up...
660 i
= getppid(); /* Save parent PID to avoid race condition */
668 * Start power management framework...
672 cupsdStartSystemMonitor();
673 #endif /* __APPLE__ */
676 * Send server-started event...
681 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started on demand.");
683 #endif /* HAVE_ONDEMAND */
685 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started in foreground.");
687 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started in background.");
689 cupsdSetBusyState(0);
692 * Start any pending print jobs...
701 current_time
= time(NULL
);
702 event_time
= current_time
;
703 expire_time
= current_time
;
707 senddoc_time
= current_time
;
709 while (!stop_scheduler
)
712 * Check if there are dead children to handle...
719 * Check if we need to load the server configuration file...
725 * Close any idle clients...
728 if (cupsArrayCount(Clients
) > 0)
730 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
732 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
733 if (httpGetState(con
->http
) == HTTP_WAITING
)
734 cupsdCloseClient(con
);
736 con
->http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
738 cupsdPauseListening();
742 * Restart if all clients are closed and all jobs finished, or
743 * if the reload timeout has elapsed...
746 if ((cupsArrayCount(Clients
) == 0 &&
747 (cupsArrayCount(PrintingJobs
) == 0 || NeedReload
!= RELOAD_ALL
)) ||
748 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
751 * Shutdown the server...
757 #endif /* HAVE_ONDEMAND */
764 * Read configuration...
767 if (!cupsdReadConfiguration())
769 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
770 sd_journal_print(LOG_ERR
, "Unable to read configuration file \"%s\" - exiting.", ConfigurationFile
);
772 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting.", ConfigurationFile
);
773 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
779 * Startup the server...
787 * Send a server-restarted event...
790 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
791 "Scheduler restarted.");
796 * Check for available input or ready output. If cupsdDoSelect()
797 * returns 0 or -1, something bad happened and we should exit
800 * Note that we at least have one listening socket open at all
804 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
809 * If no other work is scheduled and we're being controlled by launchd,
810 * systemd, etc. then timeout after 'IdleExitTimeout' seconds of
814 if (timeout
== 86400 && OnDemand
&& IdleExitTimeout
&&
817 # endif /* HAVE_SYSTEMD */
818 !cupsArrayCount(ActiveJobs
))
820 cupsd_printer_t
*p
= NULL
; /* Current printer */
822 if (Browsing
&& BrowseLocalProtocols
)
824 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
); p
; p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
831 timeout
= IdleExitTimeout
;
832 service_idle_exit
= 1;
836 service_idle_exit
= 0;
837 #endif /* HAVE_ONDEMAND */
839 if ((fds
= cupsdDoSelect(timeout
)) < 0)
842 * Got an error from select!
845 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
846 cupsd_printer_t
*p
; /* Current printer */
847 #endif /* HAVE_DNSSD || HAVE_AVAHI */
849 if (errno
== EINTR
) /* Just interrupted by a signal */
853 * Log all sorts of debug info to help track down the problem.
856 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
859 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
861 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
862 cupsdLogMessage(CUPSD_LOG_EMERG
,
863 "Clients[%d] = %d, file = %d, state = %d",
864 i
, con
->number
, con
->file
, httpGetState(con
->http
));
866 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
868 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
869 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
871 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
874 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
876 #endif /* __APPLE__ */
878 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
880 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
881 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
883 job
->status_buffer
? job
->status_buffer
->fd
: -1,
884 job
->print_pipes
[0], job
->print_pipes
[1],
885 job
->back_pipes
[0], job
->back_pipes
[1]);
887 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
888 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
890 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
891 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
892 p
->reg_name
? p
->reg_name
: "(null)");
893 #endif /* HAVE_DNSSD || HAVE_AVAHI */
898 current_time
= time(NULL
);
901 * Write dirty config/state files...
904 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
909 * If we are going to sleep and still have pending jobs, stop them after
910 * a period of time...
913 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
914 cupsArrayCount(PrintingJobs
) > 0)
917 cupsdStopAllJobs(CUPSD_JOB_DEFAULT
, 5);
919 #endif /* __APPLE__ */
923 * Update the network interfaces once a minute...
926 if ((current_time
- netif_time
) >= 60)
928 netif_time
= current_time
;
931 #endif /* !__APPLE__ */
935 * If no other work was scheduled and we're being controlled by launchd,
936 * systemd, or upstart then timeout after 'LaunchdTimeout' seconds of
940 if (!fds
&& service_idle_exit
)
942 cupsdLogMessage(CUPSD_LOG_INFO
,
943 "Printer sharing is off and there are no jobs pending, "
944 "will restart on demand.");
948 #endif /* HAVE_ONDEMAND */
951 * Resume listening for new connections as needed...
954 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
955 cupsArrayCount(Clients
) < MaxClients
)
956 cupsdResumeListening();
959 * Expire subscriptions and unload completed jobs as needed...
962 if (current_time
> expire_time
)
964 if (cupsArrayCount(Subscriptions
) > 0)
965 cupsdExpireSubscriptions(NULL
, NULL
);
967 cupsdUnloadCompletedJobs();
969 expire_time
= current_time
;
973 * Delete stale local printers...
976 if (current_time
>= local_timeout
)
978 cupsdDeleteTemporaryPrinters(0);
982 #ifndef HAVE_AUTHORIZATION_H
984 * Update the root certificate once every 5 minutes if we have client
988 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
989 !RunUser
&& cupsArrayCount(Clients
))
992 * Update the root certificate...
996 cupsdAddCert(0, "root", cupsdDefaultAuthType());
998 #endif /* !HAVE_AUTHORIZATION_H */
1001 * Check for new data on the client sockets...
1004 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1006 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1009 * Process pending data in the input buffer...
1012 if (httpGetReady(con
->http
))
1014 cupsdReadClient(con
);
1019 * Check the activity and close old clients...
1022 activity
= current_time
- Timeout
;
1023 if (httpGetActivity(con
->http
) < activity
&& !con
->pipe_pid
)
1025 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Closing client %d after %d seconds of inactivity.", con
->number
, Timeout
);
1027 cupsdCloseClient(con
);
1033 * Update any pending multi-file documents...
1036 if ((current_time
- senddoc_time
) >= 10)
1039 senddoc_time
= current_time
;
1043 * Clean job history...
1046 if (JobHistoryUpdate
&& current_time
>= JobHistoryUpdate
)
1050 * Log statistics at most once a minute when in debug mode...
1053 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
1055 size_t string_count
, /* String count */
1056 alloc_bytes
, /* Allocated string bytes */
1057 total_bytes
; /* Total string bytes */
1058 #ifdef HAVE_MALLINFO
1059 struct mallinfo mem
; /* Malloc information */
1063 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1064 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1065 mem
.usmblks
+ mem
.uordblks
);
1066 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1067 mem
.fsmblks
+ mem
.fordblks
);
1068 #endif /* HAVE_MALLINFO */
1070 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1071 cupsArrayCount(Clients
));
1072 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1073 cupsArrayCount(Jobs
));
1074 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1075 cupsArrayCount(ActiveJobs
));
1076 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1077 cupsArrayCount(Printers
));
1079 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1080 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1081 "Report: stringpool-string-count=" CUPS_LLFMT
,
1082 CUPS_LLCAST string_count
);
1083 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1084 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1085 CUPS_LLCAST alloc_bytes
);
1086 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1087 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1088 CUPS_LLCAST total_bytes
);
1090 report_time
= current_time
;
1094 * Handle OS-specific event notification for any events that have
1095 * accumulated. Don't send these more than once a second...
1098 if (LastEvent
&& (current_time
- event_time
) >= 1)
1100 #ifdef HAVE_NOTIFY_POST
1101 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1102 CUPSD_EVENT_PRINTER_DELETED
|
1103 CUPSD_EVENT_PRINTER_MODIFIED
))
1105 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1106 "notify_post(\"com.apple.printerListChange\")");
1107 notify_post("com.apple.printerListChange");
1110 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1112 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1113 "notify_post(\"com.apple.printerHistoryChange\")");
1114 notify_post("com.apple.printerHistoryChange");
1117 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1118 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1119 CUPSD_EVENT_JOB_PROGRESS
))
1121 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1122 "notify_post(\"com.apple.jobChange\")");
1123 notify_post("com.apple.jobChange");
1125 #endif /* HAVE_NOTIFY_POST */
1128 * Reset the accumulated events...
1131 LastEvent
= CUPSD_EVENT_NONE
;
1132 event_time
= current_time
;
1137 * Log a message based on what happened...
1142 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1143 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1144 "Scheduler shutting down normally.");
1148 cupsdLogMessage(CUPSD_LOG_ERROR
,
1149 "Scheduler shutting down due to program error.");
1150 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1151 "Scheduler shutting down due to program error.");
1155 * Close all network clients...
1163 * Update the KeepAlive/PID file as needed...
1166 service_checkout(1);
1175 * Delete all temporary printers...
1178 cupsdDeleteTemporaryPrinters(1);
1182 * Stop monitoring system event monitoring...
1186 cupsdStopSystemMonitor();
1187 #endif /* __APPLE__ */
1191 return (!stop_scheduler
);
1196 * 'cupsdAddString()' - Copy and add a string to an array.
1199 int /* O - 1 on success, 0 on failure */
1200 cupsdAddString(cups_array_t
**a
, /* IO - String array */
1201 const char *s
) /* I - String to copy and add */
1204 *a
= cupsArrayNew3((cups_array_func_t
)strcmp
, NULL
,
1205 (cups_ahash_func_t
)NULL
, 0,
1206 (cups_acopy_func_t
)strdup
,
1207 (cups_afree_func_t
)free
);
1209 return (cupsArrayAdd(*a
, (char *)s
));
1214 * 'cupsdCheckProcess()' - Tell the main loop to check for dead children.
1218 cupsdCheckProcess(void)
1221 * Flag that we have dead children...
1229 * 'cupsdClearString()' - Clear a string.
1233 cupsdClearString(char **s
) /* O - String value */
1244 * 'cupsdFreeStrings()' - Free an array of strings.
1248 cupsdFreeStrings(cups_array_t
**a
) /* IO - String array */
1252 cupsArrayDelete(*a
);
1259 * 'cupsdHoldSignals()' - Hold child and termination signals.
1263 cupsdHoldSignals(void)
1265 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1266 sigset_t newmask
; /* New POSIX signal mask */
1267 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1277 #elif defined(HAVE_SIGACTION)
1278 sigemptyset(&newmask
);
1279 sigaddset(&newmask
, SIGTERM
);
1280 sigaddset(&newmask
, SIGCHLD
);
1281 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1282 #endif /* HAVE_SIGSET */
1287 * 'cupsdReleaseSignals()' - Release signals for delivery.
1291 cupsdReleaseSignals(void)
1300 #elif defined(HAVE_SIGACTION)
1301 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1302 #endif /* HAVE_SIGSET */
1307 * 'cupsdSetString()' - Set a string value.
1311 cupsdSetString(char **s
, /* O - New string */
1312 const char *v
) /* I - String value */
1328 * 'cupsdSetStringf()' - Set a formatted string value.
1332 cupsdSetStringf(char **s
, /* O - New string */
1333 const char *f
, /* I - Printf-style format string */
1334 ...) /* I - Additional args as needed */
1336 char v
[65536 + 64]; /* Formatting string value */
1337 va_list ap
; /* Argument pointer */
1338 char *olds
; /* Old string */
1349 vsnprintf(v
, sizeof(v
), f
, ap
);
1363 * 'parent_handler()' - Catch USR1/CHLD signals...
1367 parent_handler(int sig
) /* I - Signal */
1370 * Store the signal we got from the OS and return...
1373 parent_signal
= sig
;
1378 * 'process_children()' - Process all dead children...
1382 process_children(void)
1384 int status
; /* Exit status of child */
1385 int pid
, /* Process ID of child */
1386 job_id
; /* Job ID of child */
1387 cupsd_job_t
*job
; /* Current job */
1388 int i
; /* Looping var */
1389 char name
[1024]; /* Process name */
1390 const char *type
; /* Type of program */
1393 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1396 * Reset the dead_children flag...
1402 * Collect the exit status of some children...
1406 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1407 #elif defined(HAVE_WAIT3)
1408 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1410 if ((pid
= wait(&status
)) > 0)
1411 #endif /* HAVE_WAITPID */
1414 * Collect the name of the process that finished...
1417 cupsdFinishProcess(pid
, name
, sizeof(name
), &job_id
);
1420 * Delete certificates for CGI processes...
1424 cupsdDeleteCert(pid
);
1427 * Handle completed job filters...
1431 job
= cupsdFindJob(job_id
);
1437 for (i
= 0; job
->filters
[i
]; i
++)
1438 if (job
->filters
[i
] == pid
)
1441 if (job
->filters
[i
] || job
->backend
== pid
)
1444 * OK, this process has gone away; what's left?
1447 if (job
->filters
[i
])
1449 job
->filters
[i
] = -pid
;
1454 job
->backend
= -pid
;
1458 if (status
&& status
!= SIGTERM
&& status
!= SIGKILL
&&
1462 * An error occurred; save the exit status so we know to stop
1463 * the printer or cancel the job when all of the filters finish...
1465 * A negative status indicates that the backend failed and the
1466 * printer needs to be stopped.
1468 * In order to preserve the most serious status, we always log
1469 * when a process dies due to a signal (e.g. SIGABRT, SIGSEGV,
1470 * and SIGBUS) and prefer to log the backend exit status over a
1474 int old_status
= abs(job
->status
);
1476 if (WIFSIGNALED(status
) || /* This process crashed, or */
1477 !job
->status
|| /* No process had a status, or */
1478 (!job
->filters
[i
] && WIFEXITED(old_status
)))
1479 { /* Backend and filter didn't crash */
1480 if (job
->filters
[i
])
1481 job
->status
= status
; /* Filter failed */
1483 job
->status
= -status
; /* Backend failed */
1486 if (job
->state_value
== IPP_JOB_PROCESSING
&&
1487 job
->status_level
> CUPSD_LOG_ERROR
&&
1488 (job
->filters
[i
] || !WIFEXITED(status
)))
1490 char message
[1024]; /* New printer-state-message */
1493 job
->status_level
= CUPSD_LOG_ERROR
;
1495 snprintf(message
, sizeof(message
), "%s failed", type
);
1499 strlcpy(job
->printer
->state_message
, message
,
1500 sizeof(job
->printer
->state_message
));
1506 if (!job
->printer_message
&& job
->attrs
)
1508 if ((job
->printer_message
=
1509 ippFindAttribute(job
->attrs
, "job-printer-state-message",
1510 IPP_TAG_TEXT
)) == NULL
)
1511 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
,
1513 "job-printer-state-message",
1517 if (job
->printer_message
)
1518 ippSetString(job
->attrs
, &job
->printer_message
, 0, message
);
1523 * If this is not the last file in a job, see if all of the
1524 * filters are done, and if so move to the next file.
1527 if (job
->state_value
>= IPP_JOB_CANCELED
)
1530 * Remove the job from the active list if there are no processes still
1534 for (i
= 0; job
->filters
[i
] < 0; i
++);
1536 if (!job
->filters
[i
] && job
->backend
<= 0)
1537 cupsArrayRemove(ActiveJobs
, job
);
1539 else if (job
->current_file
< job
->num_files
&& job
->printer
)
1541 for (i
= 0; job
->filters
[i
] < 0; i
++);
1543 if (!job
->filters
[i
] &&
1544 (!job
->printer
->pc
|| !job
->printer
->pc
->single_file
||
1548 * Process the next file...
1551 cupsdContinueJob(job
);
1558 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1559 * since they come when we kill/end a process...
1562 if (status
== SIGTERM
|| status
== SIGKILL
)
1564 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1565 "PID %d (%s) was terminated normally with signal %d.", pid
,
1568 else if (status
== SIGPIPE
)
1570 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1571 "PID %d (%s) did not catch or ignore signal %d.", pid
, name
,
1576 if (WIFEXITED(status
))
1578 int code
= WEXITSTATUS(status
); /* Exit code */
1581 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1582 "PID %d (%s) stopped with status %d (%s)", pid
, name
,
1583 code
, strerror(code
- 100));
1585 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1586 "PID %d (%s) stopped with status %d.", pid
, name
, code
);
1589 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) crashed on signal %d.",
1590 pid
, name
, WTERMSIG(status
));
1592 if (LogLevel
< CUPSD_LOG_DEBUG
)
1593 cupsdLogJob(job
, CUPSD_LOG_INFO
,
1594 "Hint: Try setting the LogLevel to \"debug\" to find out "
1598 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1603 * If wait*() is interrupted by a signal, tell main() to call us again...
1606 if (pid
< 0 && errno
== EINTR
)
1612 * 'select_timeout()' - Calculate the select timeout value.
1616 static long /* O - Number of seconds */
1617 select_timeout(int fds
) /* I - Number of descriptors returned */
1619 long timeout
; /* Timeout for select */
1620 time_t now
; /* Current time */
1621 cupsd_client_t
*con
; /* Client information */
1622 cupsd_job_t
*job
; /* Job information */
1623 cupsd_printer_t
*printer
; /* Printer information */
1624 const char *why
; /* Debugging aid */
1627 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout: JobHistoryUpdate=%ld",
1628 (long)JobHistoryUpdate
);
1631 * Check to see if any of the clients have pending data to be
1632 * processed; if so, the timeout should be 0...
1635 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1637 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1638 if (httpGetReady(con
->http
))
1642 * If select has been active in the last second (fds > 0) or we have
1643 * many resources in use then don't bother trying to optimize the
1644 * timeout, just make it 1 second.
1647 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1651 * Otherwise, check all of the possible events that we need to wake for...
1655 timeout
= now
+ 86400; /* 86400 == 1 day */
1660 * When going to sleep, wake up to abort jobs that don't complete in time.
1663 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1665 timeout
= SleepJobs
;
1666 why
= "abort jobs before sleeping";
1668 #endif /* __APPLE__ */
1671 * Check whether we are accepting new connections...
1674 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1675 ListeningPaused
< timeout
)
1677 if (ListeningPaused
<= now
)
1680 timeout
= ListeningPaused
;
1682 why
= "resume listening";
1686 * Check the activity and close old clients...
1689 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1691 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1692 if ((httpGetActivity(con
->http
) + Timeout
) < timeout
)
1694 timeout
= httpGetActivity(con
->http
) + Timeout
;
1695 why
= "timeout a client connection";
1699 * Write out changes to configuration and state files...
1702 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1704 timeout
= DirtyCleanTime
;
1705 why
= "write dirty config/state files";
1709 * Check for any job activity...
1712 if (JobHistoryUpdate
&& timeout
> JobHistoryUpdate
)
1714 timeout
= JobHistoryUpdate
;
1715 why
= "update job history";
1718 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1720 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1722 if (job
->cancel_time
&& job
->cancel_time
< timeout
)
1724 timeout
= job
->cancel_time
;
1725 why
= "cancel stuck jobs";
1728 if (job
->kill_time
&& job
->kill_time
< timeout
)
1730 timeout
= job
->kill_time
;
1731 why
= "kill unresponsive jobs";
1734 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1736 timeout
= job
->hold_until
;
1737 why
= "release held jobs";
1740 if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1743 why
= "start pending jobs";
1749 * Check for temporary printers that need to be deleted...
1752 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
); printer
; printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1754 if (printer
->temporary
&& !printer
->job
&& (!local_timeout
|| local_timeout
> (printer
->state_time
+ 60)))
1755 local_timeout
= printer
->state_time
+ 60;
1758 if (timeout
> local_timeout
&& local_timeout
)
1760 timeout
= local_timeout
;
1761 why
= "delete stale local printers";
1765 * Adjust from absolute to relative time. We add 1 second to the timeout since
1766 * events occur after the timeout expires, and limit the timeout to 86400
1767 * seconds (1 day) to avoid select() timeout limits present on some operating
1771 timeout
= timeout
- now
+ 1;
1775 else if (timeout
> 86400)
1779 * Log and return the timeout value...
1782 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
1790 * 'sigchld_handler()' - Handle 'child' signals from old processes.
1794 sigchld_handler(int sig
) /* I - Signal number */
1799 * Flag that we have dead children...
1805 * Reset the signal handler as needed...
1808 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1809 signal(SIGCLD
, sigchld_handler
);
1810 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1815 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
1819 sighup_handler(int sig
) /* I - Signal number */
1823 NeedReload
= RELOAD_ALL
;
1824 ReloadTime
= time(NULL
);
1826 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1827 signal(SIGHUP
, sighup_handler
);
1828 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1833 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
1837 sigterm_handler(int sig
) /* I - Signal number */
1839 (void)sig
; /* remove compiler warnings... */
1842 * Flag that we should stop and return...
1849 #ifdef HAVE_ONDEMAND
1851 * 'service_add_listener()' - Bind an open fd as a Listener.
1855 service_add_listener(int fd
, /* I - Socket file descriptor */
1856 int idx
) /* I - Listener number, for logging */
1858 cupsd_listener_t
*lis
; /* Listeners array */
1859 http_addr_t addr
; /* Address variable */
1860 socklen_t addrlen
; /* Length of address */
1861 char s
[256]; /* String addresss */
1864 addrlen
= sizeof(addr
);
1866 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1868 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_add_listener: Unable to get local address for listener #%d: %s", idx
+ 1, strerror(errno
));
1872 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Listener #%d at fd %d, \"%s\".", idx
+ 1, fd
, httpAddrString(&addr
, s
, sizeof(s
)));
1875 * Try to match the on-demand socket address to one of the listeners...
1878 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1880 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1881 if (httpAddrEqual(&lis
->address
, &addr
))
1885 * Add a new listener If there's no match...
1890 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Matched existing listener #%d to %s.", idx
+ 1, httpAddrString(&(lis
->address
), s
, sizeof(s
)));
1894 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Adding new listener #%d for %s.", idx
+ 1, httpAddrString(&addr
, s
, sizeof(s
)));
1896 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1898 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_add_listener: Unable to allocate listener: %s.", strerror(errno
));
1903 cupsArrayAdd(Listeners
, lis
);
1905 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1912 if (httpAddrPort(&(lis
->address
)) == 443)
1913 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1914 # endif /* HAVE_SSL */
1916 #endif /* HAVE_ONDEMAND */
1920 * 'service_checkin()' - Check-in with launchd and collect the listening fds.
1924 service_checkin(void)
1926 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: pid=%d", (int)getpid());
1931 int error
; /* Check-in error, if any */
1932 size_t i
, /* Looping var */
1933 count
; /* Number of listeners */
1934 int *ld_sockets
; /* Listener sockets */
1937 * Check-in with launchd...
1940 if ((error
= launch_activate_socket("Listeners", &ld_sockets
, &count
)) != 0)
1942 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets: %s", strerror(error
));
1944 return; /* anti-compiler-warning */
1948 * Try to match the launchd sockets to the cupsd listeners...
1951 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: %d listeners.", (int)count
);
1953 for (i
= 0; i
< count
; i
++)
1954 service_add_listener(ld_sockets
[i
], (int)i
);
1959 #elif defined(HAVE_SYSTEMD)
1962 int i
, /* Looping var */
1963 count
; /* Number of listeners */
1966 * Check-in with systemd...
1969 if ((count
= sd_listen_fds(0)) < 0)
1971 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets: %s", strerror(-count
));
1973 return; /* anti-compiler-warning */
1977 * Try to match the systemd sockets to the cupsd listeners...
1980 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: %d listeners.", count
);
1982 for (i
= 0; i
< count
; i
++)
1983 service_add_listener(SD_LISTEN_FDS_START
+ i
, i
);
1986 #elif defined(HAVE_UPSTART)
1989 const char *e
; /* Environment var */
1990 int fd
; /* File descriptor */
1993 if (!(e
= getenv("UPSTART_EVENTS")))
1995 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: We did not get started via Upstart.");
2000 if (strcasecmp(e
, "socket"))
2002 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: We did not get triggered via an Upstart socket event.");
2007 if ((e
= getenv("UPSTART_FDS")) == NULL
)
2009 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets from UPSTART_FDS.");
2014 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: UPSTART_FDS=%s", e
);
2016 fd
= (int)strtol(e
, NULL
, 10);
2019 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno
));
2025 * Upstart only supportst a single on-demand socket file descriptor...
2028 service_add_listener(fd
, 0);
2030 #endif /* HAVE_LAUNCHD */
2035 * 'service_checkout()' - Update the KeepAlive/PID file as needed.
2039 service_checkout(int shutdown
) /* I - Shutting down? */
2041 cups_file_t
*fp
; /* File */
2042 char pidfile
[1024]; /* PID/KeepAlive file */
2046 * When running on-demand, use the KeepAlive file, otherwise write a PID file
2050 #ifdef HAVE_ONDEMAND
2053 strlcpy(pidfile
, CUPS_KEEPALIVE
, sizeof(pidfile
));
2055 if (cupsArrayCount(ActiveJobs
) || /* Active jobs */
2056 WebInterface
|| /* Web interface enabled */
2057 NeedReload
|| /* Doing a reload */
2058 (Browsing
&& BrowseLocalProtocols
&& cupsArrayCount(Printers
)))
2059 /* Printers being shared */
2062 * Create or remove the "keep-alive" file based on whether there are active
2063 * jobs or shared printers to advertise...
2070 #endif /* HAVE_ONDEMAND */
2071 snprintf(pidfile
, sizeof(pidfile
), "%s/cupsd.pid", StateDir
);
2075 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Removing KeepAlive/PID file \"%s\".", pidfile
);
2081 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Creating KeepAlive/PID file \"%s\".", pidfile
);
2083 if ((fp
= cupsFileOpen(pidfile
, "w")) != NULL
)
2086 * Save the PID in the file...
2089 cupsFilePrintf(fp
, "%d\n", (int)getpid());
2093 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create KeepAlive/PID file \"%s\": %s", pidfile
, strerror(errno
));
2099 * 'usage()' - Show scheduler usage.
2103 usage(int status
) /* O - Exit status */
2105 FILE *fp
= status
? stderr
: stdout
; /* Output file */
2108 _cupsLangPuts(fp
, _("Usage: cupsd [options]"));
2109 _cupsLangPuts(fp
, _("Options:"));
2110 _cupsLangPuts(fp
, _(" -c cupsd.conf Set cupsd.conf file to use."));
2111 _cupsLangPuts(fp
, _(" -f Run in the foreground."));
2112 _cupsLangPuts(fp
, _(" -F Run in the foreground but detach from console."));
2113 _cupsLangPuts(fp
, _(" -h Show this usage message."));
2114 #ifdef HAVE_ONDEMAND
2115 _cupsLangPuts(fp
, _(" -l Run cupsd on demand."));
2116 #endif /* HAVE_ONDEMAND */
2117 _cupsLangPuts(fp
, _(" -s cups-files.conf Set cups-files.conf file to use."));
2118 _cupsLangPuts(fp
, _(" -t Test the configuration file."));