~ubuntu-branches/ubuntu/trusty/cups/trusty-proposed

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
Description: Add support for creating pid files.
Author: Martin Pitt <mpitt@debian.org>
Bug: http://www.cups.org/str.php?L2465

Last-Update: 2012-11-29

--- a/scheduler/conf.c
+++ b/scheduler/conf.c
@@ -178,7 +178,8 @@
 #ifdef HAVE_AUTHORIZATION_H
   { "SystemGroupAuthKey",	&SystemGroupAuthKey,	CUPSD_VARTYPE_STRING },
 #endif /* HAVE_AUTHORIZATION_H */
-  { "TempDir",			&TempDir,		CUPSD_VARTYPE_PATHNAME }
+  { "TempDir",			&TempDir,		CUPSD_VARTYPE_PATHNAME },
+  { "PidFile",			&PidFile,		CUPSD_VARTYPE_STRING }
 };
 
 static int		default_auth_type = CUPSD_AUTH_AUTO;
@@ -594,6 +595,7 @@
   cupsdSetStringf(&ServerHeader, "CUPS/%d.%d IPP/2.1", CUPS_VERSION_MAJOR,
                   CUPS_VERSION_MINOR);
   cupsdSetString(&StateDir, CUPS_STATEDIR);
+  cupsdSetString(&PidFile, "/var/run/cups/cupsd.pid");
 
   if (!strcmp(CUPS_DEFAULT_PRINTCAP, "/etc/printers.conf"))
     PrintcapFormat = PRINTCAP_SOLARIS;
@@ -3349,6 +3351,7 @@
              !_cups_strcasecmp(line, "SystemGroup") ||
              !_cups_strcasecmp(line, "SystemGroupAuthKey") ||
              !_cups_strcasecmp(line, "TempDir") ||
+             !_cups_strcasecmp(line, "PidFile") ||
 	     !_cups_strcasecmp(line, "User"))
     {
       cupsdLogMessage(CUPSD_LOG_INFO,
--- a/scheduler/conf.h
+++ b/scheduler/conf.h
@@ -245,6 +245,8 @@
 VAR int			SSLOptions		VALUE(CUPSD_SSL_NONE);
 					/* SSL/TLS options */
 #endif /* HAVE_SSL */
+VAR char		*PidFile		VALUE(NULL);
+					/* Debian CUPS pid file */
 
 #ifdef HAVE_LAUNCHD
 VAR int			LaunchdTimeout		VALUE(10);
--- a/scheduler/main.c
+++ b/scheduler/main.c
@@ -90,6 +90,8 @@
 static void		sigterm_handler(int sig);
 static long		select_timeout(int fds);
 static void		usage(int status) __attribute__((noreturn));
+int			write_pid(void);
+int			remove_pid(void);
 
 
 /*
@@ -645,6 +647,11 @@
     cupsdStartSystemMonitor();
 #endif /* __APPLE__ */
 
+  if (write_pid() == 0) {
+    cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to write pid file");
+    return (1);
+  }
+
  /*
   * Send server-started event...
   */
@@ -1140,10 +1147,41 @@
 
   cupsdStopSelect();
 
+  remove_pid();
+
   return (!stop_scheduler);
 }
 
 
+/* 'write_pid()' - Write PID file.
+   'remove_pid()' - Delete PID file.
+*/
+int
+write_pid()
+{
+  FILE *f;
+  int fd;
+  int pid;
+  if (((fd = open(PidFile, O_RDWR|O_CREAT, 0644)) == -1)
+      || ((f = fdopen(fd, "r+")) == NULL) ) {
+    return 0;
+  }
+  pid = getpid();
+  if (!fprintf(f, "%d\n", pid)) {
+    close(fd);
+    return 0;
+  }
+  fflush(f);
+  close(fd);
+
+  return pid;
+}
+
+int
+remove_pid() {
+  return unlink(PidFile);
+}
+
 /*
  * 'cupsdAddString()' - Copy and add a string to an array.
  */
--- a/test/run-stp-tests.sh
+++ b/test/run-stp-tests.sh
@@ -427,6 +427,7 @@
 DocumentRoot $root/doc
 RequestRoot /tmp/cups-$user/spool
 TempDir /tmp/cups-$user/spool/temp
+PidFile /tmp/cups-$user/cupsd.pid
 AccessLog /tmp/cups-$user/log/access_log
 ErrorLog /tmp/cups-$user/log/error_log
 PageLog /tmp/cups-$user/log/page_log