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 # include <pthread/qos.h>
21 #endif /* __APPLE__ */
24 #elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
25 # define SD_JOURNAL_SUPPRESS_LOCATION
26 # include <systemd/sd-journal.h>
27 #endif /* HAVE_ASL_H */
33 #endif /* HAVE_LAUNCH_H */
36 # include <systemd/sd-daemon.h>
37 #endif /* HAVE_SYSTEMD */
40 # define CUPS_KEEPALIVE CUPS_CACHEDIR "/org.cups.cupsd"
41 /* Name of the KeepAlive file */
42 #endif /* HAVE_ONDEMAND */
44 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
46 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
50 #endif /* HAVE_NOTIFY_H */
53 # include <dbus/dbus.h>
54 #endif /* HAVE_DBUS */
56 #ifdef HAVE_SYS_PARAM_H
57 # include <sys/param.h>
58 #endif /* HAVE_SYS_PARAM_H */
65 static void parent_handler(int sig
);
66 static void process_children(void);
67 static void sigchld_handler(int sig
);
68 static void sighup_handler(int sig
);
69 static void sigterm_handler(int sig
);
70 static long select_timeout(int fds
);
71 static void service_checkin(void);
72 static void service_checkout(int shutdown
);
73 static void usage(int status
) _CUPS_NORETURN
;
80 static int parent_signal
= 0;
81 /* Set to signal number from child */
82 static int holdcount
= 0; /* Number of times "hold" was called */
83 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
84 static sigset_t holdmask
; /* Old POSIX signal mask */
85 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
86 static int dead_children
= 0;
88 static int stop_scheduler
= 0;
89 /* Should the scheduler stop? */
90 static time_t local_timeout
= 0;
91 /* Next local printer timeout */
95 * 'main()' - Main entry for the CUPS scheduler.
98 int /* O - Exit status */
99 main(int argc
, /* I - Number of command-line args */
100 char *argv
[]) /* I - Command-line arguments */
102 int i
; /* Looping var */
103 char *opt
; /* Option character */
104 int close_all
= 1, /* Close all file descriptors? */
105 disconnect
= 1, /* Disconnect from controlling terminal? */
106 fg
= 0, /* Run in foreground? */
108 /* Running as child process? */
110 /* Print the sandbox profile to stdout? */
111 int fds
; /* Number of ready descriptors */
112 cupsd_client_t
*con
; /* Current client */
113 cupsd_job_t
*job
; /* Current job */
114 cupsd_listener_t
*lis
; /* Current listener */
115 time_t current_time
, /* Current time */
116 activity
, /* Client activity timer */
117 senddoc_time
, /* Send-Document time */
118 expire_time
, /* Subscription expire time */
119 report_time
, /* Malloc/client/job report time */
120 event_time
; /* Last event notification time */
121 long timeout
; /* Timeout for cupsdDoSelect() */
122 struct rlimit limit
; /* Runtime limit */
123 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
124 struct sigaction action
; /* Actions for POSIX signals */
125 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
127 int use_sysman
= 1; /* Use system management functions? */
129 time_t netif_time
= 0; /* Time since last network update */
130 #endif /* __APPLE__ */
131 #if defined(HAVE_ONDEMAND)
132 int service_idle_exit
;
133 /* Idle exit on select timeout? */
134 #endif /* HAVE_ONDEMAND */
139 * Check for setuid invocation, which we do not support!
142 if (getuid() != geteuid())
144 fputs("cupsd: Cannot run as a setuid program.\n", stderr
);
147 #endif /* HAVE_GETEUID */
150 * Check for command-line arguments...
155 for (i
= 1; i
< argc
; i
++)
156 if (argv
[i
][0] == '-')
157 for (opt
= argv
[i
] + 1; *opt
!= '\0'; opt
++)
160 case 'C' : /* Run as child with config file */
165 case 'c' : /* Configuration file */
169 _cupsLangPuts(stderr
, _("cupsd: Expected config filename "
170 "after \"-c\" option."));
174 if (argv
[i
][0] == '/')
177 * Absolute directory...
180 cupsdSetString(&ConfigurationFile
, argv
[i
]);
185 * Relative directory...
188 char *current
; /* Current directory */
191 * Allocate a buffer for the current working directory to
192 * reduce run-time stack usage; this approximates the
193 * behavior of some implementations of getcwd() when they
194 * are passed a NULL pointer.
197 if ((current
= malloc(1024)) == NULL
)
199 _cupsLangPuts(stderr
,
200 _("cupsd: Unable to get current directory."));
204 if (!getcwd(current
, 1024))
206 _cupsLangPuts(stderr
,
207 _("cupsd: Unable to get current directory."));
212 cupsdSetStringf(&ConfigurationFile
, "%s/%s", current
, argv
[i
]);
217 case 'f' : /* Run in foreground... */
223 case 'F' : /* Run in foreground, but disconnect from terminal... */
228 case 'h' : /* Show usage/help */
232 case 'l' : /* Started by launchd/systemd/upstart... */
239 _cupsLangPuts(stderr
, _("cupsd: On-demand support not compiled "
240 "in, running in normal mode."));
244 #endif /* HAVE_ONDEMAND */
247 case 'p' : /* Stop immediately for profiling */
248 fputs("cupsd: -p (startup profiling) is for internal testing "
249 "use only!\n", stderr
);
256 case 'P' : /* Disable security profiles */
257 fputs("cupsd: -P (disable sandboxing) is for internal testing use only.\n", stderr
);
261 case 's' : /* Set cups-files.conf location */
265 _cupsLangPuts(stderr
, _("cupsd: Expected cups-files.conf "
266 "filename after \"-s\" option."));
270 if (argv
[i
][0] != '/')
273 * Relative filename not allowed...
276 _cupsLangPuts(stderr
, _("cupsd: Relative cups-files.conf "
277 "filename not allowed."));
281 cupsdSetString(&CupsFilesFile
, argv
[i
]);
285 case 'S' : /* Disable system management functions */
286 fputs("cupsd: -S (disable system management) for internal "
287 "testing use only!\n", stderr
);
290 #endif /* __APPLE__ */
292 case 't' : /* Test the cupsd.conf file... */
299 case 'T' : /* Print security profile */
306 default : /* Unknown option */
307 _cupsLangPrintf(stderr
, _("cupsd: Unknown option \"%c\" - "
314 _cupsLangPrintf(stderr
, _("cupsd: Unknown argument \"%s\" - aborting."),
319 if (!ConfigurationFile
)
320 cupsdSetString(&ConfigurationFile
, CUPS_SERVERROOT
"/cupsd.conf");
324 char *filename
, /* Copy of cupsd.conf filename */
325 *slash
; /* Final slash in cupsd.conf filename */
326 size_t len
; /* Size of buffer */
328 len
= strlen(ConfigurationFile
) + 15;
329 if ((filename
= malloc(len
)) == NULL
)
331 _cupsLangPrintf(stderr
,
332 _("cupsd: Unable to get path to "
333 "cups-files.conf file."));
337 strlcpy(filename
, ConfigurationFile
, len
);
338 if ((slash
= strrchr(filename
, '/')) == NULL
)
340 _cupsLangPrintf(stderr
,
341 _("cupsd: Unable to get path to "
342 "cups-files.conf file."));
346 strlcpy(slash
, "/cups-files.conf", len
- (size_t)(slash
- filename
));
347 cupsdSetString(&CupsFilesFile
, filename
);
354 * Make sure we aren't tying up any filesystems...
360 * Disconnect from the controlling terminal...
369 * Close all open files...
372 getrlimit(RLIMIT_NOFILE
, &limit
);
374 for (i
= 0; i
< (int)limit
.rlim_cur
&& i
< 1024; i
++)
378 * Redirect stdin/out/err to /dev/null...
381 if ((i
= open("/dev/null", O_RDONLY
)) != 0)
387 if ((i
= open("/dev/null", O_WRONLY
)) != 1)
393 if ((i
= open("/dev/null", O_WRONLY
)) != 2)
400 LogStderr
= cupsFileStderr();
403 * Run in the background as needed...
409 * Setup signal handlers for the parent...
412 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
413 sigset(SIGUSR1
, parent_handler
);
414 sigset(SIGCHLD
, parent_handler
);
416 sigset(SIGHUP
, SIG_IGN
);
417 #elif defined(HAVE_SIGACTION)
418 memset(&action
, 0, sizeof(action
));
419 sigemptyset(&action
.sa_mask
);
420 sigaddset(&action
.sa_mask
, SIGUSR1
);
421 action
.sa_handler
= parent_handler
;
422 sigaction(SIGUSR1
, &action
, NULL
);
423 sigaction(SIGCHLD
, &action
, NULL
);
425 sigemptyset(&action
.sa_mask
);
426 action
.sa_handler
= SIG_IGN
;
427 sigaction(SIGHUP
, &action
, NULL
);
429 signal(SIGUSR1
, parent_handler
);
430 signal(SIGCLD
, parent_handler
);
432 signal(SIGHUP
, SIG_IGN
);
433 #endif /* HAVE_SIGSET */
438 * OK, wait for the child to startup and send us SIGUSR1 or to crash
439 * and the OS send us SIGCHLD... We also need to ignore SIGHUP which
440 * might be sent by the init script to restart the scheduler...
443 for (; parent_signal
== 0;)
446 if (parent_signal
== SIGUSR1
)
454 else if (WIFEXITED(i
))
456 fprintf(stderr
, "cupsd: Child exited with status %d\n",
462 fprintf(stderr
, "cupsd: Child exited on signal %d\n", WTERMSIG(i
));
467 #if defined(__OpenBSD__) && OpenBSD < 201211
469 * Call _thread_sys_closefrom() so the child process doesn't reset the
470 * parent's file descriptors to be blocking. This is a workaround for a
471 * limitation of userland libpthread on older versions of OpenBSD.
474 _thread_sys_closefrom(0);
475 #endif /* __OpenBSD__ && OpenBSD < 201211 */
478 * Since many system libraries create fork-unsafe data on execution of a
479 * program, we need to re-execute the background cupsd with the "-C" and "-s"
480 * options to avoid problems. Unfortunately, we also have to assume that
481 * argv[0] contains the name of the cupsd executable - there is no portable
482 * way to get the real pathname...
485 execlp(argv
[0], argv
[0], "-C", ConfigurationFile
, "-s", CupsFilesFile
, (char *)0);
490 * Let the system know we are busy while we bring up cupsd...
493 cupsdSetBusyState(1);
496 * Set the timezone info...
502 setlocale(LC_TIME
, "");
505 #ifdef HAVE_DBUS_THREADS_INIT
507 * Enable threading support for D-BUS...
510 dbus_threads_init_default();
511 #endif /* HAVE_DBUS_THREADS_INIT */
514 * Set the maximum number of files...
517 getrlimit(RLIMIT_NOFILE
, &limit
);
519 #if !defined(HAVE_POLL) && !defined(HAVE_EPOLL) && !defined(HAVE_KQUEUE)
520 if (limit
.rlim_max
> FD_SETSIZE
)
523 #endif /* !HAVE_POLL && !HAVE_EPOLL && !HAVE_KQUEUE */
525 if (limit
.rlim_max
== RLIM_INFINITY
)
528 #endif /* RLIM_INFINITY */
529 MaxFDs
= limit
.rlim_max
;
531 limit
.rlim_cur
= (rlim_t
)MaxFDs
;
533 setrlimit(RLIMIT_NOFILE
, &limit
);
538 * Read configuration...
541 if (!cupsdReadConfiguration())
543 else if (TestConfigFile
)
545 printf("\"%s\" is OK.\n", CupsFilesFile
);
546 printf("\"%s\" is OK.\n", ConfigurationFile
);
549 else if (print_profile
)
551 cups_file_t
*fp
; /* File pointer */
552 const char *profile
= cupsdCreateProfile(42, 0);
554 char line
[1024]; /* Line from file */
557 if ((fp
= cupsFileOpen(profile
, "r")) == NULL
)
559 printf("Unable to open profile file \"%s\": %s\n", profile
? profile
: "(null)", strerror(errno
));
563 while (cupsFileGets(fp
, line
, sizeof(line
)))
572 * Clean out old temp files and printer cache data.
575 if (!strncmp(TempDir
, RequestRoot
, strlen(RequestRoot
)))
576 cupsdCleanFiles(TempDir
, NULL
);
578 cupsdCleanFiles(CacheDir
, "*.ipp");
581 * If we were started on demand by launchd or systemd get the listen sockets
582 * file descriptors...
589 * Startup the server...
597 * Catch hangup and child signals and ignore broken pipes...
600 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
601 sigset(SIGCHLD
, sigchld_handler
);
602 sigset(SIGHUP
, sighup_handler
);
603 sigset(SIGPIPE
, SIG_IGN
);
604 sigset(SIGTERM
, sigterm_handler
);
605 #elif defined(HAVE_SIGACTION)
606 memset(&action
, 0, sizeof(action
));
608 sigemptyset(&action
.sa_mask
);
609 sigaddset(&action
.sa_mask
, SIGTERM
);
610 sigaddset(&action
.sa_mask
, SIGCHLD
);
611 action
.sa_handler
= sigchld_handler
;
612 sigaction(SIGCHLD
, &action
, NULL
);
614 sigemptyset(&action
.sa_mask
);
615 sigaddset(&action
.sa_mask
, SIGHUP
);
616 action
.sa_handler
= sighup_handler
;
617 sigaction(SIGHUP
, &action
, NULL
);
619 sigemptyset(&action
.sa_mask
);
620 action
.sa_handler
= SIG_IGN
;
621 sigaction(SIGPIPE
, &action
, NULL
);
623 sigemptyset(&action
.sa_mask
);
624 sigaddset(&action
.sa_mask
, SIGTERM
);
625 sigaddset(&action
.sa_mask
, SIGCHLD
);
626 action
.sa_handler
= sigterm_handler
;
627 sigaction(SIGTERM
, &action
, NULL
);
629 signal(SIGCLD
, sigchld_handler
); /* No, SIGCLD isn't a typo... */
630 signal(SIGHUP
, sighup_handler
);
631 signal(SIGPIPE
, SIG_IGN
);
632 signal(SIGTERM
, sigterm_handler
);
633 #endif /* HAVE_SIGSET */
636 * Initialize authentication certificates...
642 * If we are running in the background, signal the parent process that
643 * we are up and running...
646 if (!fg
|| run_as_child
)
649 * Send a signal to the parent process, but only if the parent is
650 * not PID 1 (init). This avoids accidentally shutting down the
651 * system on OpenBSD if you CTRL-C the server before it is up...
654 i
= getppid(); /* Save parent PID to avoid race condition */
662 * Start power management framework...
666 cupsdStartSystemMonitor();
667 #endif /* __APPLE__ */
670 * Send server-started event...
675 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started on demand.");
677 #endif /* HAVE_ONDEMAND */
679 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started in foreground.");
681 cupsdAddEvent(CUPSD_EVENT_SERVER_STARTED
, NULL
, NULL
, "Scheduler started in background.");
683 cupsdSetBusyState(0);
686 * Start any pending print jobs...
695 current_time
= time(NULL
);
696 event_time
= current_time
;
697 expire_time
= current_time
;
701 senddoc_time
= current_time
;
703 while (!stop_scheduler
)
706 * Check if there are dead children to handle...
713 * Check if we need to load the server configuration file...
719 * Close any idle clients...
722 if (cupsArrayCount(Clients
) > 0)
724 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
726 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
727 if (httpGetState(con
->http
) == HTTP_WAITING
)
728 cupsdCloseClient(con
);
730 con
->http
->keep_alive
= HTTP_KEEPALIVE_OFF
;
732 cupsdPauseListening();
736 * Restart if all clients are closed and all jobs finished, or
737 * if the reload timeout has elapsed...
740 if ((cupsArrayCount(Clients
) == 0 &&
741 (cupsArrayCount(PrintingJobs
) == 0 || NeedReload
!= RELOAD_ALL
)) ||
742 (time(NULL
) - ReloadTime
) >= ReloadTimeout
)
745 * Shutdown the server...
751 #endif /* HAVE_ONDEMAND */
758 * Read configuration...
761 if (!cupsdReadConfiguration())
763 #ifdef HAVE_SYSTEMD_SD_JOURNAL_H
764 sd_journal_print(LOG_ERR
, "Unable to read configuration file \"%s\" - exiting.", ConfigurationFile
);
766 syslog(LOG_LPR
, "Unable to read configuration file \'%s\' - exiting.", ConfigurationFile
);
767 #endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
773 * Startup the server...
781 * Send a server-restarted event...
784 cupsdAddEvent(CUPSD_EVENT_SERVER_RESTARTED
, NULL
, NULL
,
785 "Scheduler restarted.");
790 * Check for available input or ready output. If cupsdDoSelect()
791 * returns 0 or -1, something bad happened and we should exit
794 * Note that we at least have one listening socket open at all
798 if ((timeout
= select_timeout(fds
)) > 1 && LastEvent
)
803 * If no other work is scheduled and we're being controlled by launchd,
804 * systemd, etc. then timeout after 'IdleExitTimeout' seconds of
808 if (timeout
== 86400 && OnDemand
&& IdleExitTimeout
&&
811 # endif /* HAVE_SYSTEMD */
812 !cupsArrayCount(ActiveJobs
))
814 cupsd_printer_t
*p
= NULL
; /* Current printer */
816 if (Browsing
&& BrowseLocalProtocols
)
818 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
); p
; p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
825 timeout
= IdleExitTimeout
;
826 service_idle_exit
= 1;
830 service_idle_exit
= 0;
831 #endif /* HAVE_ONDEMAND */
833 if ((fds
= cupsdDoSelect(timeout
)) < 0)
836 * Got an error from select!
839 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
840 cupsd_printer_t
*p
; /* Current printer */
841 #endif /* HAVE_DNSSD || HAVE_AVAHI */
843 if (errno
== EINTR
) /* Just interrupted by a signal */
847 * Log all sorts of debug info to help track down the problem.
850 cupsdLogMessage(CUPSD_LOG_EMERG
, "cupsdDoSelect() failed - %s!",
853 for (i
= 0, con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
855 i
++, con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
856 cupsdLogMessage(CUPSD_LOG_EMERG
,
857 "Clients[%d] = %d, file = %d, state = %d",
858 i
, con
->number
, con
->file
, httpGetState(con
->http
));
860 for (i
= 0, lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
862 i
++, lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
863 cupsdLogMessage(CUPSD_LOG_EMERG
, "Listeners[%d] = %d", i
, lis
->fd
);
865 cupsdLogMessage(CUPSD_LOG_EMERG
, "CGIPipes[0] = %d", CGIPipes
[0]);
868 cupsdLogMessage(CUPSD_LOG_EMERG
, "SysEventPipes[0] = %d",
870 #endif /* __APPLE__ */
872 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
874 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
875 cupsdLogMessage(CUPSD_LOG_EMERG
, "Jobs[%d] = %d < [%d %d] > [%d %d]",
877 job
->status_buffer
? job
->status_buffer
->fd
: -1,
878 job
->print_pipes
[0], job
->print_pipes
[1],
879 job
->back_pipes
[0], job
->back_pipes
[1]);
881 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
882 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
884 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
885 cupsdLogMessage(CUPSD_LOG_EMERG
, "printer[%s] reg_name=\"%s\"", p
->name
,
886 p
->reg_name
? p
->reg_name
: "(null)");
887 #endif /* HAVE_DNSSD || HAVE_AVAHI */
892 current_time
= time(NULL
);
895 * Write dirty config/state files...
898 if (DirtyCleanTime
&& current_time
>= DirtyCleanTime
)
903 * If we are going to sleep and still have pending jobs, stop them after
904 * a period of time...
907 if (SleepJobs
> 0 && current_time
>= SleepJobs
&&
908 cupsArrayCount(PrintingJobs
) > 0)
911 cupsdStopAllJobs(CUPSD_JOB_DEFAULT
, 5);
913 #endif /* __APPLE__ */
917 * Update the network interfaces once a minute...
920 if ((current_time
- netif_time
) >= 60)
922 netif_time
= current_time
;
925 #endif /* !__APPLE__ */
929 * If no other work was scheduled and we're being controlled by launchd,
930 * systemd, or upstart then timeout after 'LaunchdTimeout' seconds of
934 if (!fds
&& service_idle_exit
)
936 cupsdLogMessage(CUPSD_LOG_INFO
,
937 "Printer sharing is off and there are no jobs pending, "
938 "will restart on demand.");
942 #endif /* HAVE_ONDEMAND */
945 * Resume listening for new connections as needed...
948 if (ListeningPaused
&& ListeningPaused
<= current_time
&&
949 cupsArrayCount(Clients
) < MaxClients
)
950 cupsdResumeListening();
953 * Expire subscriptions and unload completed jobs as needed...
956 if (current_time
> expire_time
)
958 if (cupsArrayCount(Subscriptions
) > 0)
959 cupsdExpireSubscriptions(NULL
, NULL
);
961 cupsdUnloadCompletedJobs();
963 expire_time
= current_time
;
967 * Delete stale local printers...
970 if (current_time
>= local_timeout
)
972 cupsdDeleteTemporaryPrinters(0);
976 #ifndef HAVE_AUTHORIZATION_H
978 * Update the root certificate once every 5 minutes if we have client
982 if ((current_time
- RootCertTime
) >= RootCertDuration
&& RootCertDuration
&&
983 !RunUser
&& cupsArrayCount(Clients
))
986 * Update the root certificate...
990 cupsdAddCert(0, "root", cupsdDefaultAuthType());
992 #endif /* !HAVE_AUTHORIZATION_H */
995 * Check for new data on the client sockets...
998 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1000 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1003 * Process pending data in the input buffer...
1006 if (httpGetReady(con
->http
))
1008 cupsdReadClient(con
);
1013 * Check the activity and close old clients...
1016 activity
= current_time
- Timeout
;
1017 if (httpGetActivity(con
->http
) < activity
&& !con
->pipe_pid
)
1019 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Closing client %d after %d seconds of inactivity.", con
->number
, Timeout
);
1021 cupsdCloseClient(con
);
1027 * Update any pending multi-file documents...
1030 if ((current_time
- senddoc_time
) >= 10)
1033 senddoc_time
= current_time
;
1037 * Clean job history...
1040 if (JobHistoryUpdate
&& current_time
>= JobHistoryUpdate
)
1044 * Log statistics at most once a minute when in debug mode...
1047 if ((current_time
- report_time
) >= 60 && LogLevel
>= CUPSD_LOG_DEBUG
)
1049 size_t string_count
, /* String count */
1050 alloc_bytes
, /* Allocated string bytes */
1051 total_bytes
; /* Total string bytes */
1052 #ifdef HAVE_MALLINFO
1053 struct mallinfo mem
; /* Malloc information */
1057 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-arena=%lu", mem
.arena
);
1058 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-used=%lu",
1059 mem
.usmblks
+ mem
.uordblks
);
1060 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: malloc-free=%lu",
1061 mem
.fsmblks
+ mem
.fordblks
);
1062 #endif /* HAVE_MALLINFO */
1064 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: clients=%d",
1065 cupsArrayCount(Clients
));
1066 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs=%d",
1067 cupsArrayCount(Jobs
));
1068 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: jobs-active=%d",
1069 cupsArrayCount(ActiveJobs
));
1070 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Report: printers=%d",
1071 cupsArrayCount(Printers
));
1073 string_count
= _cupsStrStatistics(&alloc_bytes
, &total_bytes
);
1074 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1075 "Report: stringpool-string-count=" CUPS_LLFMT
,
1076 CUPS_LLCAST string_count
);
1077 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1078 "Report: stringpool-alloc-bytes=" CUPS_LLFMT
,
1079 CUPS_LLCAST alloc_bytes
);
1080 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1081 "Report: stringpool-total-bytes=" CUPS_LLFMT
,
1082 CUPS_LLCAST total_bytes
);
1084 report_time
= current_time
;
1088 * Handle OS-specific event notification for any events that have
1089 * accumulated. Don't send these more than once a second...
1092 if (LastEvent
&& (current_time
- event_time
) >= 1)
1094 #ifdef HAVE_NOTIFY_POST
1095 if (LastEvent
& (CUPSD_EVENT_PRINTER_ADDED
|
1096 CUPSD_EVENT_PRINTER_DELETED
|
1097 CUPSD_EVENT_PRINTER_MODIFIED
))
1099 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1100 "notify_post(\"com.apple.printerListChange\")");
1101 notify_post("com.apple.printerListChange");
1104 if (LastEvent
& CUPSD_EVENT_PRINTER_STATE_CHANGED
)
1106 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1107 "notify_post(\"com.apple.printerHistoryChange\")");
1108 notify_post("com.apple.printerHistoryChange");
1111 if (LastEvent
& (CUPSD_EVENT_JOB_STATE_CHANGED
|
1112 CUPSD_EVENT_JOB_CONFIG_CHANGED
|
1113 CUPSD_EVENT_JOB_PROGRESS
))
1115 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1116 "notify_post(\"com.apple.jobChange\")");
1117 notify_post("com.apple.jobChange");
1119 #endif /* HAVE_NOTIFY_POST */
1122 * Reset the accumulated events...
1125 LastEvent
= CUPSD_EVENT_NONE
;
1126 event_time
= current_time
;
1131 * Log a message based on what happened...
1136 cupsdLogMessage(CUPSD_LOG_INFO
, "Scheduler shutting down normally.");
1137 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1138 "Scheduler shutting down normally.");
1142 cupsdLogMessage(CUPSD_LOG_ERROR
,
1143 "Scheduler shutting down due to program error.");
1144 cupsdAddEvent(CUPSD_EVENT_SERVER_STOPPED
, NULL
, NULL
,
1145 "Scheduler shutting down due to program error.");
1149 * Close all network clients...
1157 * Update the KeepAlive/PID file as needed...
1160 service_checkout(1);
1169 * Delete all temporary printers...
1172 cupsdDeleteTemporaryPrinters(1);
1176 * Stop monitoring system event monitoring...
1180 cupsdStopSystemMonitor();
1181 #endif /* __APPLE__ */
1185 return (!stop_scheduler
);
1190 * 'cupsdAddString()' - Copy and add a string to an array.
1193 int /* O - 1 on success, 0 on failure */
1194 cupsdAddString(cups_array_t
**a
, /* IO - String array */
1195 const char *s
) /* I - String to copy and add */
1198 *a
= cupsArrayNew3((cups_array_func_t
)strcmp
, NULL
,
1199 (cups_ahash_func_t
)NULL
, 0,
1200 (cups_acopy_func_t
)strdup
,
1201 (cups_afree_func_t
)free
);
1203 return (cupsArrayAdd(*a
, (char *)s
));
1208 * 'cupsdCheckProcess()' - Tell the main loop to check for dead children.
1212 cupsdCheckProcess(void)
1215 * Flag that we have dead children...
1223 * 'cupsdClearString()' - Clear a string.
1227 cupsdClearString(char **s
) /* O - String value */
1238 * 'cupsdFreeStrings()' - Free an array of strings.
1242 cupsdFreeStrings(cups_array_t
**a
) /* IO - String array */
1246 cupsArrayDelete(*a
);
1253 * 'cupsdHoldSignals()' - Hold child and termination signals.
1257 cupsdHoldSignals(void)
1259 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1260 sigset_t newmask
; /* New POSIX signal mask */
1261 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
1271 #elif defined(HAVE_SIGACTION)
1272 sigemptyset(&newmask
);
1273 sigaddset(&newmask
, SIGTERM
);
1274 sigaddset(&newmask
, SIGCHLD
);
1275 sigprocmask(SIG_BLOCK
, &newmask
, &holdmask
);
1276 #endif /* HAVE_SIGSET */
1281 * 'cupsdReleaseSignals()' - Release signals for delivery.
1285 cupsdReleaseSignals(void)
1294 #elif defined(HAVE_SIGACTION)
1295 sigprocmask(SIG_SETMASK
, &holdmask
, NULL
);
1296 #endif /* HAVE_SIGSET */
1301 * 'cupsdSetString()' - Set a string value.
1305 cupsdSetString(char **s
, /* O - New string */
1306 const char *v
) /* I - String value */
1322 * 'cupsdSetStringf()' - Set a formatted string value.
1326 cupsdSetStringf(char **s
, /* O - New string */
1327 const char *f
, /* I - Printf-style format string */
1328 ...) /* I - Additional args as needed */
1330 char v
[65536 + 64]; /* Formatting string value */
1331 va_list ap
; /* Argument pointer */
1332 char *olds
; /* Old string */
1343 vsnprintf(v
, sizeof(v
), f
, ap
);
1357 * 'parent_handler()' - Catch USR1/CHLD signals...
1361 parent_handler(int sig
) /* I - Signal */
1364 * Store the signal we got from the OS and return...
1367 parent_signal
= sig
;
1372 * 'process_children()' - Process all dead children...
1376 process_children(void)
1378 int status
; /* Exit status of child */
1379 int pid
, /* Process ID of child */
1380 job_id
; /* Job ID of child */
1381 cupsd_job_t
*job
; /* Current job */
1382 int i
; /* Looping var */
1383 char name
[1024]; /* Process name */
1384 const char *type
; /* Type of program */
1387 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_children()");
1390 * Reset the dead_children flag...
1396 * Collect the exit status of some children...
1400 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
1401 #elif defined(HAVE_WAIT3)
1402 while ((pid
= wait3(&status
, WNOHANG
, NULL
)) > 0)
1404 if ((pid
= wait(&status
)) > 0)
1405 #endif /* HAVE_WAITPID */
1408 * Collect the name of the process that finished...
1411 cupsdFinishProcess(pid
, name
, sizeof(name
), &job_id
);
1414 * Delete certificates for CGI processes...
1418 cupsdDeleteCert(pid
);
1421 * Handle completed job filters...
1425 job
= cupsdFindJob(job_id
);
1431 for (i
= 0; job
->filters
[i
]; i
++)
1432 if (job
->filters
[i
] == pid
)
1435 if (job
->filters
[i
] || job
->backend
== pid
)
1438 * OK, this process has gone away; what's left?
1441 if (job
->filters
[i
])
1443 job
->filters
[i
] = -pid
;
1448 job
->backend
= -pid
;
1452 if (status
&& status
!= SIGTERM
&& status
!= SIGKILL
&&
1456 * An error occurred; save the exit status so we know to stop
1457 * the printer or cancel the job when all of the filters finish...
1459 * A negative status indicates that the backend failed and the
1460 * printer needs to be stopped.
1462 * In order to preserve the most serious status, we always log
1463 * when a process dies due to a signal (e.g. SIGABRT, SIGSEGV,
1464 * and SIGBUS) and prefer to log the backend exit status over a
1468 int old_status
= abs(job
->status
);
1470 if (WIFSIGNALED(status
) || /* This process crashed, or */
1471 !job
->status
|| /* No process had a status, or */
1472 (!job
->filters
[i
] && WIFEXITED(old_status
)))
1473 { /* Backend and filter didn't crash */
1474 if (job
->filters
[i
])
1476 job
->status
= status
; /* Filter failed */
1480 job
->status
= -status
; /* Backend failed */
1482 if (job
->current_file
< job
->num_files
)
1483 cupsdSetJobState(job
, IPP_JOB_ABORTED
, CUPSD_JOB_FORCE
, "Canceling multi-file job due to backend failure.");
1487 if (job
->state_value
== IPP_JOB_PROCESSING
&&
1488 job
->status_level
> CUPSD_LOG_ERROR
&&
1489 (job
->filters
[i
] || !WIFEXITED(status
)))
1491 char message
[1024]; /* New printer-state-message */
1494 job
->status_level
= CUPSD_LOG_ERROR
;
1496 snprintf(message
, sizeof(message
), "%s failed", type
);
1500 strlcpy(job
->printer
->state_message
, message
,
1501 sizeof(job
->printer
->state_message
));
1507 if (!job
->printer_message
&& job
->attrs
)
1509 if ((job
->printer_message
=
1510 ippFindAttribute(job
->attrs
, "job-printer-state-message",
1511 IPP_TAG_TEXT
)) == NULL
)
1512 job
->printer_message
= ippAddString(job
->attrs
, IPP_TAG_JOB
,
1514 "job-printer-state-message",
1518 if (job
->printer_message
)
1519 ippSetString(job
->attrs
, &job
->printer_message
, 0, message
);
1524 * If this is not the last file in a job, see if all of the
1525 * filters are done, and if so move to the next file.
1528 if (job
->state_value
>= IPP_JOB_CANCELED
)
1531 * Remove the job from the active list if there are no processes still
1535 for (i
= 0; job
->filters
[i
] < 0; i
++);
1537 if (!job
->filters
[i
] && job
->backend
<= 0)
1538 cupsArrayRemove(ActiveJobs
, job
);
1540 else if (job
->current_file
< job
->num_files
&& job
->printer
)
1542 for (i
= 0; job
->filters
[i
] < 0; i
++);
1544 if (!job
->filters
[i
] &&
1545 (!job
->printer
->pc
|| !job
->printer
->pc
->single_file
||
1549 * Process the next file...
1552 cupsdContinueJob(job
);
1559 * Show the exit status as needed, ignoring SIGTERM and SIGKILL errors
1560 * since they come when we kill/end a process...
1563 if (status
== SIGTERM
|| status
== SIGKILL
)
1565 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1566 "PID %d (%s) was terminated normally with signal %d.", pid
,
1569 else if (status
== SIGPIPE
)
1571 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1572 "PID %d (%s) did not catch or ignore signal %d.", pid
, name
,
1577 if (WIFEXITED(status
))
1579 int code
= WEXITSTATUS(status
); /* Exit code */
1582 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1583 "PID %d (%s) stopped with status %d (%s)", pid
, name
,
1584 code
, strerror(code
- 100));
1586 cupsdLogJob(job
, CUPSD_LOG_DEBUG
,
1587 "PID %d (%s) stopped with status %d.", pid
, name
, code
);
1590 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) crashed on signal %d.",
1591 pid
, name
, WTERMSIG(status
));
1593 if (LogLevel
< CUPSD_LOG_DEBUG
)
1594 cupsdLogJob(job
, CUPSD_LOG_INFO
,
1595 "Hint: Try setting the LogLevel to \"debug\" to find out "
1599 cupsdLogJob(job
, CUPSD_LOG_DEBUG
, "PID %d (%s) exited with no errors.",
1604 * If wait*() is interrupted by a signal, tell main() to call us again...
1607 if (pid
< 0 && errno
== EINTR
)
1613 * 'select_timeout()' - Calculate the select timeout value.
1617 static long /* O - Number of seconds */
1618 select_timeout(int fds
) /* I - Number of descriptors returned */
1620 long timeout
; /* Timeout for select */
1621 time_t now
; /* Current time */
1622 cupsd_client_t
*con
; /* Client information */
1623 cupsd_job_t
*job
; /* Job information */
1624 cupsd_printer_t
*printer
; /* Printer information */
1625 const char *why
; /* Debugging aid */
1628 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout: JobHistoryUpdate=%ld",
1629 (long)JobHistoryUpdate
);
1632 * Check to see if any of the clients have pending data to be
1633 * processed; if so, the timeout should be 0...
1636 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1638 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1639 if (httpGetReady(con
->http
))
1643 * If select has been active in the last second (fds > 0) or we have
1644 * many resources in use then don't bother trying to optimize the
1645 * timeout, just make it 1 second.
1648 if (fds
> 0 || cupsArrayCount(Clients
) > 50)
1652 * Otherwise, check all of the possible events that we need to wake for...
1656 timeout
= now
+ 86400; /* 86400 == 1 day */
1661 * When going to sleep, wake up to abort jobs that don't complete in time.
1664 if (SleepJobs
> 0 && SleepJobs
< timeout
)
1666 timeout
= SleepJobs
;
1667 why
= "abort jobs before sleeping";
1669 #endif /* __APPLE__ */
1672 * Check whether we are accepting new connections...
1675 if (ListeningPaused
> 0 && cupsArrayCount(Clients
) < MaxClients
&&
1676 ListeningPaused
< timeout
)
1678 if (ListeningPaused
<= now
)
1681 timeout
= ListeningPaused
;
1683 why
= "resume listening";
1687 * Check the activity and close old clients...
1690 for (con
= (cupsd_client_t
*)cupsArrayFirst(Clients
);
1692 con
= (cupsd_client_t
*)cupsArrayNext(Clients
))
1693 if ((httpGetActivity(con
->http
) + Timeout
) < timeout
)
1695 timeout
= httpGetActivity(con
->http
) + Timeout
;
1696 why
= "timeout a client connection";
1700 * Write out changes to configuration and state files...
1703 if (DirtyCleanTime
&& timeout
> DirtyCleanTime
)
1705 timeout
= DirtyCleanTime
;
1706 why
= "write dirty config/state files";
1710 * Check for any job activity...
1713 if (JobHistoryUpdate
&& timeout
> JobHistoryUpdate
)
1715 timeout
= JobHistoryUpdate
;
1716 why
= "update job history";
1719 for (job
= (cupsd_job_t
*)cupsArrayFirst(ActiveJobs
);
1721 job
= (cupsd_job_t
*)cupsArrayNext(ActiveJobs
))
1723 if (job
->cancel_time
&& job
->cancel_time
< timeout
)
1725 timeout
= job
->cancel_time
;
1726 why
= "cancel stuck jobs";
1729 if (job
->kill_time
&& job
->kill_time
< timeout
)
1731 timeout
= job
->kill_time
;
1732 why
= "kill unresponsive jobs";
1735 if (job
->state_value
== IPP_JOB_HELD
&& job
->hold_until
< timeout
)
1737 timeout
= job
->hold_until
;
1738 why
= "release held jobs";
1741 if (job
->state_value
== IPP_JOB_PENDING
&& timeout
> (now
+ 10))
1744 why
= "start pending jobs";
1750 * Check for temporary printers that need to be deleted...
1753 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
); printer
; printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1755 if (printer
->temporary
&& !printer
->job
&& (!local_timeout
|| local_timeout
> (printer
->state_time
+ 60)))
1756 local_timeout
= printer
->state_time
+ 60;
1759 if (timeout
> local_timeout
&& local_timeout
)
1761 timeout
= local_timeout
;
1762 why
= "delete stale local printers";
1766 * Adjust from absolute to relative time. We add 1 second to the timeout since
1767 * events occur after the timeout expires, and limit the timeout to 86400
1768 * seconds (1 day) to avoid select() timeout limits present on some operating
1772 timeout
= timeout
- now
+ 1;
1776 else if (timeout
> 86400)
1780 * Log and return the timeout value...
1783 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "select_timeout(%d): %ld seconds to %s",
1791 * 'sigchld_handler()' - Handle 'child' signals from old processes.
1795 sigchld_handler(int sig
) /* I - Signal number */
1800 * Flag that we have dead children...
1806 * Reset the signal handler as needed...
1809 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1810 signal(SIGCLD
, sigchld_handler
);
1811 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1816 * 'sighup_handler()' - Handle 'hangup' signals to reconfigure the scheduler.
1820 sighup_handler(int sig
) /* I - Signal number */
1824 NeedReload
= RELOAD_ALL
;
1825 ReloadTime
= time(NULL
);
1827 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1828 signal(SIGHUP
, sighup_handler
);
1829 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1834 * 'sigterm_handler()' - Handle 'terminate' signals that stop the scheduler.
1838 sigterm_handler(int sig
) /* I - Signal number */
1840 (void)sig
; /* remove compiler warnings... */
1843 * Flag that we should stop and return...
1850 #ifdef HAVE_ONDEMAND
1852 * 'service_add_listener()' - Bind an open fd as a Listener.
1856 service_add_listener(int fd
, /* I - Socket file descriptor */
1857 int idx
) /* I - Listener number, for logging */
1859 cupsd_listener_t
*lis
; /* Listeners array */
1860 http_addr_t addr
; /* Address variable */
1861 socklen_t addrlen
; /* Length of address */
1862 char s
[256]; /* String addresss */
1865 addrlen
= sizeof(addr
);
1867 if (getsockname(fd
, (struct sockaddr
*)&addr
, &addrlen
))
1869 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_add_listener: Unable to get local address for listener #%d: %s", idx
+ 1, strerror(errno
));
1873 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Listener #%d at fd %d, \"%s\".", idx
+ 1, fd
, httpAddrString(&addr
, s
, sizeof(s
)));
1876 * Try to match the on-demand socket address to one of the listeners...
1879 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1881 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1882 if (httpAddrEqual(&lis
->address
, &addr
))
1886 * Add a new listener If there's no match...
1891 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Matched existing listener #%d to %s.", idx
+ 1, httpAddrString(&(lis
->address
), s
, sizeof(s
)));
1895 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_add_listener: Adding new listener #%d for %s.", idx
+ 1, httpAddrString(&addr
, s
, sizeof(s
)));
1897 if ((lis
= calloc(1, sizeof(cupsd_listener_t
))) == NULL
)
1899 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_add_listener: Unable to allocate listener: %s.", strerror(errno
));
1904 cupsArrayAdd(Listeners
, lis
);
1906 memcpy(&lis
->address
, &addr
, sizeof(lis
->address
));
1913 if (httpAddrPort(&(lis
->address
)) == 443)
1914 lis
->encryption
= HTTP_ENCRYPT_ALWAYS
;
1915 # endif /* HAVE_SSL */
1917 #endif /* HAVE_ONDEMAND */
1921 * 'service_checkin()' - Check-in with launchd and collect the listening fds.
1925 service_checkin(void)
1927 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: pid=%d", (int)getpid());
1932 int error
; /* Check-in error, if any */
1933 size_t i
, /* Looping var */
1934 count
; /* Number of listeners */
1935 int *ld_sockets
; /* Listener sockets */
1939 * Force "user initiated" priority for the main thread...
1942 pthread_set_qos_class_self_np(QOS_CLASS_USER_INITIATED
, 0);
1943 # endif /* __APPLE__ */
1946 * Check-in with launchd...
1949 if ((error
= launch_activate_socket("Listeners", &ld_sockets
, &count
)) != 0)
1951 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets: %s", strerror(error
));
1953 return; /* anti-compiler-warning */
1957 * Try to match the launchd sockets to the cupsd listeners...
1960 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: %d listeners.", (int)count
);
1962 for (i
= 0; i
< count
; i
++)
1963 service_add_listener(ld_sockets
[i
], (int)i
);
1968 xpc_transaction_begin();
1969 # endif /* __APPLE__ */
1972 #elif defined(HAVE_SYSTEMD)
1975 int i
, /* Looping var */
1976 count
; /* Number of listeners */
1979 * Check-in with systemd...
1982 if ((count
= sd_listen_fds(0)) < 0)
1984 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets: %s", strerror(-count
));
1986 return; /* anti-compiler-warning */
1990 * Try to match the systemd sockets to the cupsd listeners...
1993 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: %d listeners.", count
);
1995 for (i
= 0; i
< count
; i
++)
1996 service_add_listener(SD_LISTEN_FDS_START
+ i
, i
);
1999 #elif defined(HAVE_UPSTART)
2002 const char *e
; /* Environment var */
2003 int fd
; /* File descriptor */
2006 if (!(e
= getenv("UPSTART_EVENTS")))
2008 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: We did not get started via Upstart.");
2013 if (strcasecmp(e
, "socket"))
2015 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: We did not get triggered via an Upstart socket event.");
2020 if ((e
= getenv("UPSTART_FDS")) == NULL
)
2022 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Unable to get listener sockets from UPSTART_FDS.");
2027 cupsdLogMessage(CUPSD_LOG_DEBUG
, "service_checkin: UPSTART_FDS=%s", e
);
2029 fd
= (int)strtol(e
, NULL
, 10);
2032 cupsdLogMessage(CUPSD_LOG_ERROR
, "service_checkin: Could not parse UPSTART_FDS: %s", strerror(errno
));
2038 * Upstart only supportst a single on-demand socket file descriptor...
2041 service_add_listener(fd
, 0);
2043 #endif /* HAVE_LAUNCHD */
2048 * 'service_checkout()' - Update the KeepAlive/PID file as needed.
2052 service_checkout(int shutdown
) /* I - Shutting down? */
2054 cups_file_t
*fp
; /* File */
2055 char pidfile
[1024]; /* PID/KeepAlive file */
2059 * When running on-demand, use the KeepAlive file, otherwise write a PID file
2063 #ifdef HAVE_ONDEMAND
2066 strlcpy(pidfile
, CUPS_KEEPALIVE
, sizeof(pidfile
));
2068 if (cupsArrayCount(ActiveJobs
) || /* Active jobs */
2069 WebInterface
|| /* Web interface enabled */
2070 NeedReload
|| /* Doing a reload */
2071 (Browsing
&& BrowseLocalProtocols
&& cupsArrayCount(Printers
)))
2072 /* Printers being shared */
2075 * Create or remove the "keep-alive" file based on whether there are active
2076 * jobs or shared printers to advertise...
2083 #endif /* HAVE_ONDEMAND */
2084 snprintf(pidfile
, sizeof(pidfile
), "%s/cupsd.pid", StateDir
);
2088 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Removing KeepAlive/PID file \"%s\".", pidfile
);
2094 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Creating KeepAlive/PID file \"%s\".", pidfile
);
2096 if ((fp
= cupsFileOpen(pidfile
, "w")) != NULL
)
2099 * Save the PID in the file...
2102 cupsFilePrintf(fp
, "%d\n", (int)getpid());
2106 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create KeepAlive/PID file \"%s\": %s", pidfile
, strerror(errno
));
2110 if (OnDemand
&& shutdown
)
2111 xpc_transaction_end();
2112 # endif /* __APPLE__ */
2117 * 'usage()' - Show scheduler usage.
2121 usage(int status
) /* O - Exit status */
2123 FILE *fp
= status
? stderr
: stdout
; /* Output file */
2126 _cupsLangPuts(fp
, _("Usage: cupsd [options]"));
2127 _cupsLangPuts(fp
, _("Options:"));
2128 _cupsLangPuts(fp
, _(" -c cupsd.conf Set cupsd.conf file to use."));
2129 _cupsLangPuts(fp
, _(" -f Run in the foreground."));
2130 _cupsLangPuts(fp
, _(" -F Run in the foreground but detach from console."));
2131 _cupsLangPuts(fp
, _(" -h Show this usage message."));
2132 #ifdef HAVE_ONDEMAND
2133 _cupsLangPuts(fp
, _(" -l Run cupsd on demand."));
2134 #endif /* HAVE_ONDEMAND */
2135 _cupsLangPuts(fp
, _(" -s cups-files.conf Set cups-files.conf file to use."));
2136 _cupsLangPuts(fp
, _(" -t Test the configuration file."));