]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/process.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / process.c
1 /*
2 * "$Id: process.c 5046 2006-02-01 22:11:58Z mike $"
3 *
4 * Process management routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * cupsdEndProcess() - End a process.
27 * cupsdFinishProcess() - Finish a process and get its name.
28 * cupsdStartProcess() - Start a process.
29 * compare_procs() - Compare two processes.
30 */
31
32 /*
33 * Include necessary headers...
34 */
35
36 #include "cupsd.h"
37 #include <grp.h>
38
39
40 /*
41 * Process structure...
42 */
43
44 typedef struct
45 {
46 int pid; /* Process ID */
47 char name[1]; /* Name of process */
48 } cupsd_proc_t;
49
50
51 /*
52 * Local globals...
53 */
54
55 static cups_array_t *process_array = NULL;
56
57
58 /*
59 * Local functions...
60 */
61
62 static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
63
64
65 /*
66 * 'cupsdEndProcess()' - End a process.
67 */
68
69 int /* O - 0 on success, -1 on failure */
70 cupsdEndProcess(int pid, /* I - Process ID */
71 int force) /* I - Force child to die */
72 {
73 if (force)
74 return (kill(pid, SIGKILL));
75 else
76 return (kill(pid, SIGTERM));
77 }
78
79
80 /*
81 * 'cupsdFinishProcess()' - Finish a process and get its name.
82 */
83
84 const char * /* O - Process name */
85 cupsdFinishProcess(int pid, /* I - Process ID */
86 char *name, /* I - Name buffer */
87 int namelen) /* I - Size of name buffer */
88 {
89 cupsd_proc_t key, /* Search key */
90 *proc; /* Matching process */
91
92
93 key.pid = pid;
94
95 if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL)
96 {
97 strlcpy(name, proc->name, namelen);
98 cupsArrayRemove(process_array, proc);
99 free(proc);
100
101 return (name);
102 }
103 else
104 return ("unknown");
105 }
106
107
108 /*
109 * 'cupsdStartProcess()' - Start a process.
110 */
111
112 int /* O - Process ID or 0 */
113 cupsdStartProcess(
114 const char *command, /* I - Full path to command */
115 char *argv[], /* I - Command-line arguments */
116 char *envp[], /* I - Environment */
117 int infd, /* I - Standard input file descriptor */
118 int outfd, /* I - Standard output file descriptor */
119 int errfd, /* I - Standard error file descriptor */
120 int backfd, /* I - Backchannel file descriptor */
121 int root, /* I - Run as root? */
122 int *pid) /* O - Process ID */
123 {
124 cupsd_proc_t *proc; /* New process record */
125 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
126 struct sigaction action; /* POSIX signal handler */
127 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
128 #if defined(__APPLE__) && __GNUC__ < 4
129 int envc; /* Number of environment variables */
130 char processPath[1024], /* CFProcessPath environment variable */
131 linkpath[1024]; /* Link path for symlinks... */
132 int linkbytes; /* Bytes for link path */
133 #endif /* __APPLE__ && __GNUC__ < 4 */
134
135
136 cupsdLogMessage(CUPSD_LOG_DEBUG2,
137 "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
138 command, argv, envp, infd, outfd, errfd);
139
140 #if defined(__APPLE__) && __GNUC__ < 4
141 /*
142 * Add special voodoo magic for MacOS X 10.3 and earlier - this allows
143 * MacOS X programs to access their bundle resources properly...
144 */
145
146 for (envc = 0; envc < MAX_ENV && envp[envc]; envc ++);
147 /* All callers pass in a MAX_ENV element array of environment strings */
148
149 if (envc < (MAX_ENV - 1))
150 {
151 /*
152 * We have room, try to read the symlink path for this command...
153 */
154
155 if ((linkbytes = readlink(linkpath, sizeof(linkpath) - 1)) > 0)
156 {
157 /*
158 * Yes, this is a symlink to the actual program, nul-terminate and
159 * use it...
160 */
161
162 linkpath[linkbytes] = '\0';
163
164 if (linkpath[0] == '/')
165 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
166 linkpath);
167 else
168 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
169 dirname(command), linkpath);
170 }
171 else
172 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
173
174 envp[envc++] = processPath;
175 envp[envc] = NULL;
176 }
177 #endif /* __APPLE__ && __GNUC__ > 3 */
178
179 /*
180 * Block signals before forking...
181 */
182
183 cupsdHoldSignals();
184
185 if ((*pid = fork()) == 0)
186 {
187 /*
188 * Child process goes here...
189 *
190 * Update stdin/stdout/stderr as needed...
191 */
192
193 if (infd != 0)
194 {
195 close(0);
196 if (infd > 0)
197 dup(infd);
198 else
199 open("/dev/null", O_RDONLY);
200 }
201 if (outfd != 1)
202 {
203 close(1);
204 if (outfd > 0)
205 dup(outfd);
206 else
207 open("/dev/null", O_WRONLY);
208 }
209 if (errfd != 2)
210 {
211 close(2);
212 if (errfd > 0)
213 dup(errfd);
214 else
215 open("/dev/null", O_WRONLY);
216 }
217 if (backfd != 3)
218 {
219 close(3);
220 if (backfd > 0)
221 dup(backfd);
222 else
223 open("/dev/null", O_RDWR);
224 fcntl(3, F_SETFL, O_NDELAY);
225 }
226
227 /*
228 * Change the priority of the process based on the FilterNice setting.
229 * (this is not done for backends...)
230 */
231
232 if (!root)
233 nice(FilterNice);
234
235 /*
236 * Change user to something "safe"...
237 */
238
239 if (!root && !RunUser)
240 {
241 /*
242 * Running as root, so change to non-priviledged user...
243 */
244
245 if (setgid(Group))
246 exit(errno);
247
248 if (setgroups(1, &Group))
249 exit(errno);
250
251 if (setuid(User))
252 exit(errno);
253 }
254 else
255 {
256 /*
257 * Reset group membership to just the main one we belong to.
258 */
259
260 setgid(Group);
261 setgroups(1, &Group);
262 }
263
264 /*
265 * Change umask to restrict permissions on created files...
266 */
267
268 umask(077);
269
270 /*
271 * Unblock signals before doing the exec...
272 */
273
274 #ifdef HAVE_SIGSET
275 sigset(SIGTERM, SIG_DFL);
276 sigset(SIGCHLD, SIG_DFL);
277 #elif defined(HAVE_SIGACTION)
278 memset(&action, 0, sizeof(action));
279
280 sigemptyset(&action.sa_mask);
281 action.sa_handler = SIG_DFL;
282
283 sigaction(SIGTERM, &action, NULL);
284 sigaction(SIGCHLD, &action, NULL);
285 #else
286 signal(SIGTERM, SIG_DFL);
287 signal(SIGCHLD, SIG_DFL);
288 #endif /* HAVE_SIGSET */
289
290 cupsdReleaseSignals();
291
292 /*
293 * Execute the command; if for some reason this doesn't work,
294 * return the error code...
295 */
296
297 if (envp)
298 execve(command, argv, envp);
299 else
300 execv(command, argv);
301
302 perror(command);
303
304 exit(errno);
305 }
306 else if (*pid < 0)
307 {
308 /*
309 * Error - couldn't fork a new process!
310 */
311
312 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
313 strerror(errno));
314
315 *pid = 0;
316 }
317 else
318 {
319 if (!process_array)
320 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
321
322 if (process_array)
323 {
324 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
325 {
326 proc->pid = *pid;
327 strcpy(proc->name, command);
328
329 cupsArrayAdd(process_array, proc);
330 }
331 }
332 }
333
334 cupsdReleaseSignals();
335
336 return (*pid);
337 }
338
339
340 /*
341 * 'compare_procs()' - Compare two processes.
342 */
343
344 static int /* O - Result of comparison */
345 compare_procs(cupsd_proc_t *a, /* I - First process */
346 cupsd_proc_t *b) /* I - Second process */
347 {
348 return (a->pid - b->pid);
349 }
350
351
352 /*
353 * End of "$Id: process.c 5046 2006-02-01 22:11:58Z mike $".
354 */