]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/process.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / process.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: process.c 6649 2007-07-11 21:46:42Z 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
e53920b9 134#if defined(__APPLE__)
135 if (envp)
e00b005a 136 {
137 /*
e53920b9 138 * Add special voodoo magic for MacOS X - this allows MacOS X
139 * programs to access their bundle resources properly...
e00b005a 140 */
141
e53920b9 142 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
143 {
144 /*
145 * Yes, this is a symlink to the actual program, nul-terminate and
146 * use it...
147 */
148
149 linkpath[linkbytes] = '\0';
e00b005a 150
e53920b9 151 if (linkpath[0] == '/')
152 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
153 linkpath);
154 else
155 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
f7deaa1a 156 dirname((char *)command), linkpath);
e53920b9 157 }
e00b005a 158 else
e53920b9 159 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
bd7854cb 160
e53920b9 161 envp[0] = processPath; /* Replace <CFProcessPath> string */
162 }
163#endif /* __APPLE__ */
e00b005a 164
ef416fc2 165 /*
166 * Block signals before forking...
167 */
168
169 cupsdHoldSignals();
170
171 if ((*pid = fork()) == 0)
172 {
173 /*
174 * Child process goes here...
175 *
176 * Update stdin/stdout/stderr as needed...
177 */
178
179 if (infd != 0)
180 {
181 close(0);
182 if (infd > 0)
183 dup(infd);
184 else
185 open("/dev/null", O_RDONLY);
186 }
187 if (outfd != 1)
188 {
189 close(1);
190 if (outfd > 0)
191 dup(outfd);
192 else
193 open("/dev/null", O_WRONLY);
194 }
195 if (errfd != 2)
196 {
197 close(2);
198 if (errfd > 0)
199 dup(errfd);
200 else
201 open("/dev/null", O_WRONLY);
202 }
203 if (backfd != 3)
204 {
205 close(3);
206 if (backfd > 0)
207 dup(backfd);
208 else
209 open("/dev/null", O_RDWR);
210 fcntl(3, F_SETFL, O_NDELAY);
211 }
f7deaa1a 212 if (sidefd != 4 && sidefd > 0)
213 {
214 close(4);
215 dup(sidefd);
216 fcntl(4, F_SETFL, O_NDELAY);
217 }
ef416fc2 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))
e00b005a 238 exit(errno);
ef416fc2 239
240 if (setgroups(1, &Group))
e00b005a 241 exit(errno);
ef416fc2 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
e00b005a 252 setgid(Group);
ef416fc2 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 }
e00b005a 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 }
ef416fc2 325
326 cupsdReleaseSignals();
327
328 return (*pid);
329}
330
331
332/*
e00b005a 333 * 'compare_procs()' - Compare two processes.
334 */
335
336static int /* O - Result of comparison */
337compare_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/*
bc44d920 345 * End of "$Id: process.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 346 */