Description: Add mode to let cupsd terminate when it is idle, also for running CUPS on-demand. If the mode is activated, either by IdleExitTimeout=TIMEOUT in cupsd.conf, by "-x TIMEOUT" on the cupsd command line (With TIMEOUT in both cases the idle timeout in seconds, 0 turning off the feature, off by default), cupsd terminates when it is idle (no jobs, no shared printers) for the given timeout. Author: Till Kamppeter Bug: https://cups.org/str.php?L4374 Last-Update: 2014-06-04 --- a/scheduler/conf.c +++ b/scheduler/conf.c @@ -79,6 +79,7 @@ { "DefaultShared", &DefaultShared, CUPSD_VARTYPE_BOOLEAN }, { "DirtyCleanInterval", &DirtyCleanInterval, CUPSD_VARTYPE_TIME }, { "ErrorPolicy", &ErrorPolicy, CUPSD_VARTYPE_STRING }, + { "IdleExitTimeout", &IdleExitTimeout, CUPSD_VARTYPE_TIME }, { "FilterLimit", &FilterLimit, CUPSD_VARTYPE_INTEGER }, { "FilterNice", &FilterNice, CUPSD_VARTYPE_INTEGER }, #ifdef HAVE_GSSAPI @@ -754,6 +755,7 @@ MaxSubscriptionsPerUser = 0; DefaultLeaseDuration = 86400; MaxLeaseDuration = 0; + IdleExitTimeout = 0; #ifdef HAVE_LAUNCHD LaunchdTimeout = 10; --- a/scheduler/conf.h +++ b/scheduler/conf.h @@ -246,6 +246,9 @@ /* SSL/TLS options */ #endif /* HAVE_SSL */ +VAR int IdleExitTimeout VALUE(0); + /* Time after which an idle cupsd will exit */ + #ifdef HAVE_LAUNCHD VAR int LaunchdTimeout VALUE(10); /* Time after which an idle cupsd will exit */ --- a/scheduler/main.c +++ b/scheduler/main.c @@ -98,6 +98,7 @@ char *argv[]) /* I - Command-line arguments */ { int i; /* Looping var */ + int t = -1; /* Timeout */ char *opt; /* Option character */ int fg; /* Run in the foreground */ int close_all = 1, /* Close all file descriptors? */ @@ -125,6 +126,8 @@ #else time_t netif_time = 0; /* Time since last network update */ #endif /* __APPLE__ */ + int idle_exit; + /* Idle exit on select timeout? */ #if HAVE_LAUNCHD int launchd_idle_exit; /* Idle exit on select timeout? */ @@ -304,6 +307,21 @@ close_all = 0; break; + case 'x' : /* Exit on idle timeout */ + i ++; + if (i >= argc) + { + _cupsLangPuts(stderr, _("cupsd: Expected exit-on-idle timeout in seconds " + "after \"-x\" option.")); + usage(1); + } + + if ((t = atoi(argv[i])) < 0) + _cupsLangPrintf(stderr, + _("cupsd: Invalid exit-on-idle timeout value \"%s\"."), + argv[i]); + break; + default : /* Unknown option */ _cupsLangPrintf(stderr, _("cupsd: Unknown option \"%c\" - " "aborting."), *opt); @@ -541,6 +559,13 @@ } /* + * Exit-on-idle timeout set by command line or configuration file + */ + + if (t >= 0) + IdleExitTimeout = t; + + /* * Clean out old temp files and printer cache data. */ @@ -778,6 +803,26 @@ if ((timeout = select_timeout(fds)) > 1 && LastEvent) timeout = 1; + /* + * If no other work is scheduled and we've set the exit-on-idle timeout + * then timeout after 'IdleExitTimeout' seconds of inactivity... + */ + + if (IdleExitTimeout && timeout >= IdleExitTimeout && + !cupsArrayCount(ActiveJobs) && + (!Browsing || !BrowseLocalProtocols || !cupsArrayCount(Printers))) + { + cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsd is idle, scheduling shutdown in %d seconds.", + IdleExitTimeout); + timeout = IdleExitTimeout; + idle_exit = 1; + } + else + { + cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsd is not idle any more, canceling shutdown."); + idle_exit = 0; + } + #if HAVE_LAUNCHD /* * If no other work is scheduled and we're being controlled by @@ -891,6 +936,20 @@ } #endif /* !__APPLE__ */ + /* + * If no other work is scheduled and we've set the exit-on-idle timeout + * then timeout after 'IdleExitTimeout' seconds of inactivity... + */ + + if (!fds && idle_exit) + { + cupsdLogMessage(CUPSD_LOG_INFO, + "Printer sharing is off and there are no jobs pending, " + "shutting down for now."); + stop_scheduler = 1; + break; + } + #if HAVE_LAUNCHD /* * If no other work was scheduled and we're being controlled by launchd --- a/man/cupsd.conf.man.in +++ b/man/cupsd.conf.man.in @@ -214,6 +214,11 @@ .br Specifies whether or not to do reverse lookups on client addresses. .TP 5 +IdleExitTimeout seconds +.br +Specifies the number of seconds to wait before exiting on inactivity under +systemd or upstart. +.TP 5 Include filename .br Includes the named file.