]> 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 5376 2006-04-06 20:32:07Z 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__)
39 # include <libgen.h>
40 #endif /* __APPLE__ */
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__)
132 char processPath[1024], /* CFProcessPath environment variable */
133 linkpath[1024]; /* Link path for symlinks... */
134 int linkbytes; /* Bytes for link path */
135 #endif /* __APPLE__ */
136
137
138 cupsdLogMessage(CUPSD_LOG_DEBUG2,
139 "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
140 command, argv, envp, infd, outfd, errfd);
141
142 #if defined(__APPLE__)
143 if (envp)
144 {
145 /*
146 * Add special voodoo magic for MacOS X - this allows MacOS X
147 * programs to access their bundle resources properly...
148 */
149
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';
158
159 if (linkpath[0] == '/')
160 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
161 linkpath);
162 else
163 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
164 dirname(command), linkpath);
165 }
166 else
167 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
168
169 envp[0] = processPath; /* Replace <CFProcessPath> string */
170 }
171 #endif /* __APPLE__ */
172
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 }
220
221 /*
222 * Change the priority of the process based on the FilterNice setting.
223 * (this is not done for backends...)
224 */
225
226 if (!root)
227 nice(FilterNice);
228
229 /*
230 * Change user to something "safe"...
231 */
232
233 if (!root && !RunUser)
234 {
235 /*
236 * Running as root, so change to non-priviledged user...
237 */
238
239 if (setgid(Group))
240 exit(errno);
241
242 if (setgroups(1, &Group))
243 exit(errno);
244
245 if (setuid(User))
246 exit(errno);
247 }
248 else
249 {
250 /*
251 * Reset group membership to just the main one we belong to.
252 */
253
254 setgid(Group);
255 setgroups(1, &Group);
256 }
257
258 /*
259 * Change umask to restrict permissions on created files...
260 */
261
262 umask(077);
263
264 /*
265 * Unblock signals before doing the exec...
266 */
267
268 #ifdef HAVE_SIGSET
269 sigset(SIGTERM, SIG_DFL);
270 sigset(SIGCHLD, SIG_DFL);
271 #elif defined(HAVE_SIGACTION)
272 memset(&action, 0, sizeof(action));
273
274 sigemptyset(&action.sa_mask);
275 action.sa_handler = SIG_DFL;
276
277 sigaction(SIGTERM, &action, NULL);
278 sigaction(SIGCHLD, &action, NULL);
279 #else
280 signal(SIGTERM, SIG_DFL);
281 signal(SIGCHLD, SIG_DFL);
282 #endif /* HAVE_SIGSET */
283
284 cupsdReleaseSignals();
285
286 /*
287 * Execute the command; if for some reason this doesn't work,
288 * return the error code...
289 */
290
291 if (envp)
292 execve(command, argv, envp);
293 else
294 execv(command, argv);
295
296 perror(command);
297
298 exit(errno);
299 }
300 else if (*pid < 0)
301 {
302 /*
303 * Error - couldn't fork a new process!
304 */
305
306 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
307 strerror(errno));
308
309 *pid = 0;
310 }
311 else
312 {
313 if (!process_array)
314 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
315
316 if (process_array)
317 {
318 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
319 {
320 proc->pid = *pid;
321 strcpy(proc->name, command);
322
323 cupsArrayAdd(process_array, proc);
324 }
325 }
326 }
327
328 cupsdReleaseSignals();
329
330 return (*pid);
331 }
332
333
334 /*
335 * 'compare_procs()' - Compare two processes.
336 */
337
338 static int /* O - Result of comparison */
339 compare_procs(cupsd_proc_t *a, /* I - First process */
340 cupsd_proc_t *b) /* I - Second process */
341 {
342 return (a->pid - b->pid);
343 }
344
345
346 /*
347 * End of "$Id: process.c 5376 2006-04-06 20:32:07Z mike $".
348 */