]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/process.c
Import CUPS 1.4svn r7023 into easysw/current.
[thirdparty/cups.git] / scheduler / process.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: process.c 6987 2007-09-25 15:43:44Z 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));
7dfedb92 138 *pid = 0;
76cd9e37
MS
139 return (0);
140 }
141
e53920b9 142#if defined(__APPLE__)
143 if (envp)
e00b005a 144 {
145 /*
e53920b9 146 * Add special voodoo magic for MacOS X - this allows MacOS X
147 * programs to access their bundle resources properly...
e00b005a 148 */
149
e53920b9 150 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
151 {
152 /*
153 * Yes, this is a symlink to the actual program, nul-terminate and
154 * use it...
155 */
156
157 linkpath[linkbytes] = '\0';
e00b005a 158
e53920b9 159 if (linkpath[0] == '/')
160 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
161 linkpath);
162 else
163 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
f7deaa1a 164 dirname((char *)command), linkpath);
e53920b9 165 }
e00b005a 166 else
e53920b9 167 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
bd7854cb 168
e53920b9 169 envp[0] = processPath; /* Replace <CFProcessPath> string */
170 }
171#endif /* __APPLE__ */
e00b005a 172
ef416fc2 173 /*
174 * Block signals before forking...
175 */
176
177 cupsdHoldSignals();
178
179 if ((*pid = fork()) == 0)
180 {
181 /*
182 * Child process goes here...
183 *
184 * Update stdin/stdout/stderr as needed...
185 */
186
187 if (infd != 0)
188 {
189 close(0);
190 if (infd > 0)
191 dup(infd);
192 else
193 open("/dev/null", O_RDONLY);
194 }
195 if (outfd != 1)
196 {
197 close(1);
198 if (outfd > 0)
199 dup(outfd);
200 else
201 open("/dev/null", O_WRONLY);
202 }
203 if (errfd != 2)
204 {
205 close(2);
206 if (errfd > 0)
207 dup(errfd);
208 else
209 open("/dev/null", O_WRONLY);
210 }
211 if (backfd != 3)
212 {
213 close(3);
214 if (backfd > 0)
215 dup(backfd);
216 else
217 open("/dev/null", O_RDWR);
218 fcntl(3, F_SETFL, O_NDELAY);
219 }
f7deaa1a 220 if (sidefd != 4 && sidefd > 0)
221 {
222 close(4);
223 dup(sidefd);
224 fcntl(4, F_SETFL, O_NDELAY);
225 }
ef416fc2 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))
e00b005a 246 exit(errno);
ef416fc2 247
248 if (setgroups(1, &Group))
e00b005a 249 exit(errno);
ef416fc2 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
e00b005a 260 setgid(Group);
ef416fc2 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 }
e00b005a 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 }
ef416fc2 333
334 cupsdReleaseSignals();
335
336 return (*pid);
337}
338
339
340/*
e00b005a 341 * 'compare_procs()' - Compare two processes.
342 */
343
344static int /* O - Result of comparison */
345compare_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/*
2e4ff8af 353 * End of "$Id: process.c 6987 2007-09-25 15:43:44Z mike $".
ef416fc2 354 */