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