]> 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 5094 2006-02-09 01:00:26Z 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 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
150 {
151 /*
152 * Yes, this is a symlink to the actual program, nul-terminate and
153 * use it...
154 */
155
156 linkpath[linkbytes] = '\0';
157
158 if (linkpath[0] == '/')
159 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
160 linkpath);
161 else
162 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
163 dirname(command), linkpath);
164 }
165 else
166 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
167
168 envp[0] = processPath; /* Replace <CFProcessPath> string */
169 #endif /* __APPLE__ && __GNUC__ > 3 */
170
171 /*
172 * Block signals before forking...
173 */
174
175 cupsdHoldSignals();
176
177 if ((*pid = fork()) == 0)
178 {
179 /*
180 * Child process goes here...
181 *
182 * Update stdin/stdout/stderr as needed...
183 */
184
185 if (infd != 0)
186 {
187 close(0);
188 if (infd > 0)
189 dup(infd);
190 else
191 open("/dev/null", O_RDONLY);
192 }
193 if (outfd != 1)
194 {
195 close(1);
196 if (outfd > 0)
197 dup(outfd);
198 else
199 open("/dev/null", O_WRONLY);
200 }
201 if (errfd != 2)
202 {
203 close(2);
204 if (errfd > 0)
205 dup(errfd);
206 else
207 open("/dev/null", O_WRONLY);
208 }
209 if (backfd != 3)
210 {
211 close(3);
212 if (backfd > 0)
213 dup(backfd);
214 else
215 open("/dev/null", O_RDWR);
216 fcntl(3, F_SETFL, O_NDELAY);
217 }
218
219 /*
220 * Change the priority of the process based on the FilterNice setting.
221 * (this is not done for backends...)
222 */
223
224 if (!root)
225 nice(FilterNice);
226
227 /*
228 * Change user to something "safe"...
229 */
230
231 if (!root && !RunUser)
232 {
233 /*
234 * Running as root, so change to non-priviledged user...
235 */
236
237 if (setgid(Group))
238 exit(errno);
239
240 if (setgroups(1, &Group))
241 exit(errno);
242
243 if (setuid(User))
244 exit(errno);
245 }
246 else
247 {
248 /*
249 * Reset group membership to just the main one we belong to.
250 */
251
252 setgid(Group);
253 setgroups(1, &Group);
254 }
255
256 /*
257 * Change umask to restrict permissions on created files...
258 */
259
260 umask(077);
261
262 /*
263 * Unblock signals before doing the exec...
264 */
265
266 #ifdef HAVE_SIGSET
267 sigset(SIGTERM, SIG_DFL);
268 sigset(SIGCHLD, SIG_DFL);
269 #elif defined(HAVE_SIGACTION)
270 memset(&action, 0, sizeof(action));
271
272 sigemptyset(&action.sa_mask);
273 action.sa_handler = SIG_DFL;
274
275 sigaction(SIGTERM, &action, NULL);
276 sigaction(SIGCHLD, &action, NULL);
277 #else
278 signal(SIGTERM, SIG_DFL);
279 signal(SIGCHLD, SIG_DFL);
280 #endif /* HAVE_SIGSET */
281
282 cupsdReleaseSignals();
283
284 /*
285 * Execute the command; if for some reason this doesn't work,
286 * return the error code...
287 */
288
289 if (envp)
290 execve(command, argv, envp);
291 else
292 execv(command, argv);
293
294 perror(command);
295
296 exit(errno);
297 }
298 else if (*pid < 0)
299 {
300 /*
301 * Error - couldn't fork a new process!
302 */
303
304 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
305 strerror(errno));
306
307 *pid = 0;
308 }
309 else
310 {
311 if (!process_array)
312 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
313
314 if (process_array)
315 {
316 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
317 {
318 proc->pid = *pid;
319 strcpy(proc->name, command);
320
321 cupsArrayAdd(process_array, proc);
322 }
323 }
324 }
325
326 cupsdReleaseSignals();
327
328 return (*pid);
329 }
330
331
332 /*
333 * 'compare_procs()' - Compare two processes.
334 */
335
336 static int /* O - Result of comparison */
337 compare_procs(cupsd_proc_t *a, /* I - First process */
338 cupsd_proc_t *b) /* I - Second process */
339 {
340 return (a->pid - b->pid);
341 }
342
343
344 /*
345 * End of "$Id: process.c 5094 2006-02-09 01:00:26Z mike $".
346 */