summaryrefslogtreecommitdiff
path: root/debian/patches/cupsd-idleexittimeout.patch
blob: c799b3cf14e0d35de2166d4153750cdec9a93dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 <till.kamppeter@gmail.com>
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.