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