]> 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/*
f7deaa1a 2 * "$Id: process.c 6326 2007-03-11 17:50:18Z mike $"
ef416fc2 3 *
4 * Process management routines for the Common UNIX Printing System (CUPS).
5 *
f7deaa1a 6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 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 *
e00b005a 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.
ef416fc2 30 */
31
32/*
33 * Include necessary headers...
34 */
35
36#include "cupsd.h"
37#include <grp.h>
e53920b9 38#if defined(__APPLE__)
4400e98d 39# include <libgen.h>
e53920b9 40#endif /* __APPLE__ */
ef416fc2 41
42
e00b005a 43/*
44 * Process structure...
45 */
46
47typedef 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
58static cups_array_t *process_array = NULL;
59
60
61/*
62 * Local functions...
63 */
64
65static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
66
67
ef416fc2 68/*
69 * 'cupsdEndProcess()' - End a process.
70 */
71
72int /* O - 0 on success, -1 on failure */
73cupsdEndProcess(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
e00b005a 83/*
84 * 'cupsdFinishProcess()' - Finish a process and get its name.
85 */
86
87const char * /* O - Process name */
88cupsdFinishProcess(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
ef416fc2 111/*
112 * 'cupsdStartProcess()' - Start a process.
113 */
114
115int /* O - Process ID or 0 */
116cupsdStartProcess(
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 */
f7deaa1a 124 int sidefd, /* I - Sidechannel file descriptor */
ef416fc2 125 int root, /* I - Run as root? */
126 int *pid) /* O - Process ID */
127{
e00b005a 128 cupsd_proc_t *proc; /* New process record */
ef416fc2 129#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
e00b005a 130 struct sigaction action; /* POSIX signal handler */
ef416fc2 131#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
e53920b9 132#if defined(__APPLE__)
e00b005a 133 char processPath[1024], /* CFProcessPath environment variable */
134 linkpath[1024]; /* Link path for symlinks... */
135 int linkbytes; /* Bytes for link path */
e53920b9 136#endif /* __APPLE__ */
ef416fc2 137
138
139 cupsdLogMessage(CUPSD_LOG_DEBUG2,
140 "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
141 command, argv, envp, infd, outfd, errfd);
142
e53920b9 143#if defined(__APPLE__)
144 if (envp)
e00b005a 145 {
146 /*
e53920b9 147 * Add special voodoo magic for MacOS X - this allows MacOS X
148 * programs to access their bundle resources properly...
e00b005a 149 */
150
e53920b9 151 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
152 {
153 /*
154 * Yes, this is a symlink to the actual program, nul-terminate and
155 * use it...
156 */
157
158 linkpath[linkbytes] = '\0';
e00b005a 159
e53920b9 160 if (linkpath[0] == '/')
161 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
162 linkpath);
163 else
164 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
f7deaa1a 165 dirname((char *)command), linkpath);
e53920b9 166 }
e00b005a 167 else
e53920b9 168 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
bd7854cb 169
e53920b9 170 envp[0] = processPath; /* Replace <CFProcessPath> string */
171 }
172#endif /* __APPLE__ */
e00b005a 173
ef416fc2 174 /*
175 * Block signals before forking...
176 */
177
178 cupsdHoldSignals();
179
180 if ((*pid = fork()) == 0)
181 {
182 /*
183 * Child process goes here...
184 *
185 * Update stdin/stdout/stderr as needed...
186 */
187
188 if (infd != 0)
189 {
190 close(0);
191 if (infd > 0)
192 dup(infd);
193 else
194 open("/dev/null", O_RDONLY);
195 }
196 if (outfd != 1)
197 {
198 close(1);
199 if (outfd > 0)
200 dup(outfd);
201 else
202 open("/dev/null", O_WRONLY);
203 }
204 if (errfd != 2)
205 {
206 close(2);
207 if (errfd > 0)
208 dup(errfd);
209 else
210 open("/dev/null", O_WRONLY);
211 }
212 if (backfd != 3)
213 {
214 close(3);
215 if (backfd > 0)
216 dup(backfd);
217 else
218 open("/dev/null", O_RDWR);
219 fcntl(3, F_SETFL, O_NDELAY);
220 }
f7deaa1a 221 if (sidefd != 4 && sidefd > 0)
222 {
223 close(4);
224 dup(sidefd);
225 fcntl(4, F_SETFL, O_NDELAY);
226 }
ef416fc2 227
228 /*
229 * Change the priority of the process based on the FilterNice setting.
230 * (this is not done for backends...)
231 */
232
233 if (!root)
234 nice(FilterNice);
235
236 /*
237 * Change user to something "safe"...
238 */
239
240 if (!root && !RunUser)
241 {
242 /*
243 * Running as root, so change to non-priviledged user...
244 */
245
246 if (setgid(Group))
e00b005a 247 exit(errno);
ef416fc2 248
249 if (setgroups(1, &Group))
e00b005a 250 exit(errno);
ef416fc2 251
252 if (setuid(User))
253 exit(errno);
254 }
255 else
256 {
257 /*
258 * Reset group membership to just the main one we belong to.
259 */
260
e00b005a 261 setgid(Group);
ef416fc2 262 setgroups(1, &Group);
263 }
264
265 /*
266 * Change umask to restrict permissions on created files...
267 */
268
269 umask(077);
270
271 /*
272 * Unblock signals before doing the exec...
273 */
274
275#ifdef HAVE_SIGSET
276 sigset(SIGTERM, SIG_DFL);
277 sigset(SIGCHLD, SIG_DFL);
278#elif defined(HAVE_SIGACTION)
279 memset(&action, 0, sizeof(action));
280
281 sigemptyset(&action.sa_mask);
282 action.sa_handler = SIG_DFL;
283
284 sigaction(SIGTERM, &action, NULL);
285 sigaction(SIGCHLD, &action, NULL);
286#else
287 signal(SIGTERM, SIG_DFL);
288 signal(SIGCHLD, SIG_DFL);
289#endif /* HAVE_SIGSET */
290
291 cupsdReleaseSignals();
292
293 /*
294 * Execute the command; if for some reason this doesn't work,
295 * return the error code...
296 */
297
298 if (envp)
299 execve(command, argv, envp);
300 else
301 execv(command, argv);
302
303 perror(command);
304
305 exit(errno);
306 }
307 else if (*pid < 0)
308 {
309 /*
310 * Error - couldn't fork a new process!
311 */
312
313 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
314 strerror(errno));
315
316 *pid = 0;
317 }
e00b005a 318 else
319 {
320 if (!process_array)
321 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
322
323 if (process_array)
324 {
325 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
326 {
327 proc->pid = *pid;
328 strcpy(proc->name, command);
329
330 cupsArrayAdd(process_array, proc);
331 }
332 }
333 }
ef416fc2 334
335 cupsdReleaseSignals();
336
337 return (*pid);
338}
339
340
341/*
e00b005a 342 * 'compare_procs()' - Compare two processes.
343 */
344
345static int /* O - Result of comparison */
346compare_procs(cupsd_proc_t *a, /* I - First process */
347 cupsd_proc_t *b) /* I - Second process */
348{
349 return (a->pid - b->pid);
350}
351
352
353/*
f7deaa1a 354 * End of "$Id: process.c 6326 2007-03-11 17:50:18Z mike $".
ef416fc2 355 */