]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/process.c
ba4a8dc3b08b9be7b38cbcef6efb552159e2bd3a
[thirdparty/cups.git] / scheduler / process.c
1 /*
2 * "$Id$"
3 *
4 * Process management routines for the CUPS scheduler.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 */
15
16 /*
17 * Include necessary headers...
18 */
19
20 #include "cupsd.h"
21 #include <grp.h>
22 #ifdef __APPLE__
23 # include <libgen.h>
24 #endif /* __APPLE__ */
25 #ifdef HAVE_POSIX_SPAWN
26 # include <spawn.h>
27 extern char **environ;
28 #endif /* HAVE_POSIX_SPAWN */
29
30
31 /*
32 * Process structure...
33 */
34
35 typedef struct
36 {
37 int pid, /* Process ID */
38 job_id; /* Job associated with process */
39 char name[1]; /* Name of process */
40 } cupsd_proc_t;
41
42
43 /*
44 * Local globals...
45 */
46
47 static cups_array_t *process_array = NULL;
48
49
50 /*
51 * Local functions...
52 */
53
54 static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
55 #ifdef HAVE_SANDBOX_H
56 static char *cupsd_requote(char *dst, const char *src, size_t dstsize);
57 #endif /* HAVE_SANDBOX_H */
58
59
60 /*
61 * 'cupsdCreateProfile()' - Create an execution profile for a subprocess.
62 */
63
64 void * /* O - Profile or NULL on error */
65 cupsdCreateProfile(int job_id, /* I - Job ID or 0 for none */
66 int allow_networking)/* I - Allow networking off machine? */
67 {
68 #ifdef HAVE_SANDBOX_H
69 cups_file_t *fp; /* File pointer */
70 char profile[1024], /* File containing the profile */
71 bin[1024], /* Quoted ServerBin */
72 cache[1024], /* Quoted CacheDir */
73 domain[1024], /* Domain socket, if any */
74 request[1024], /* Quoted RequestRoot */
75 root[1024], /* Quoted ServerRoot */
76 temp[1024]; /* Quoted TempDir */
77 const char *nodebug; /* " (with no-log)" for no debug */
78 cupsd_listener_t *lis; /* Current listening socket */
79
80
81 if (!UseSandboxing || Sandboxing == CUPSD_SANDBOXING_OFF)
82 {
83 /*
84 * Only use sandbox profiles as root...
85 */
86
87 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
88
89 return (NULL);
90 }
91
92 if ((fp = cupsTempFile2(profile, sizeof(profile))) == NULL)
93 {
94 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
95 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create security profile: %s",
96 strerror(errno));
97 return (NULL);
98 }
99
100 fchown(cupsFileNumber(fp), RunUser, Group);
101 fchmod(cupsFileNumber(fp), 0640);
102
103 cupsd_requote(bin, ServerBin, sizeof(bin));
104 cupsd_requote(cache, CacheDir, sizeof(cache));
105 cupsd_requote(request, RequestRoot, sizeof(request));
106 cupsd_requote(root, ServerRoot, sizeof(root));
107 cupsd_requote(temp, TempDir, sizeof(temp));
108
109 nodebug = LogLevel < CUPSD_LOG_DEBUG ? " (with no-log)" : "";
110
111 cupsFilePuts(fp, "(version 1)\n");
112 if (Sandboxing == CUPSD_SANDBOXING_STRICT)
113 cupsFilePuts(fp, "(deny default)\n");
114 else
115 cupsFilePuts(fp, "(allow default)\n");
116 if (LogLevel >= CUPSD_LOG_DEBUG)
117 cupsFilePuts(fp, "(debug deny)\n");
118 cupsFilePuts(fp, "(import \"system.sb\")\n");
119 cupsFilePuts(fp, "(system-network)\n");
120 cupsFilePuts(fp, "(allow mach-per-user-lookup)\n");
121 cupsFilePuts(fp, "(allow ipc-posix-sem)\n");
122 cupsFilePuts(fp, "(allow ipc-posix-shm)\n");
123 cupsFilePuts(fp, "(allow ipc-sysv-shm)\n");
124 cupsFilePuts(fp, "(allow mach-lookup)\n");
125 cupsFilePrintf(fp,
126 "(deny file-write* file-read-data file-read-metadata\n"
127 " (regex"
128 " #\"^%s$\"" /* RequestRoot */
129 " #\"^%s/\"" /* RequestRoot/... */
130 ")%s)\n",
131 request, request, nodebug);
132 if (!RunUser)
133 cupsFilePrintf(fp,
134 "(deny file-write* file-read-data file-read-metadata\n"
135 " (regex"
136 " #\"^/Users$\""
137 " #\"^/Users/\""
138 ")%s)\n", nodebug);
139 cupsFilePrintf(fp,
140 "(deny file-write*\n"
141 " (regex"
142 " #\"^%s$\"" /* ServerRoot */
143 " #\"^%s/\"" /* ServerRoot/... */
144 " #\"^/private/etc$\""
145 " #\"^/private/etc/\""
146 " #\"^/usr/local/etc$\""
147 " #\"^/usr/local/etc/\""
148 " #\"^/Library$\""
149 " #\"^/Library/\""
150 " #\"^/System$\""
151 " #\"^/System/\""
152 ")%s)\n",
153 root, root, nodebug);
154 /* Specifically allow applications to stat RequestRoot and some other system folders */
155 cupsFilePrintf(fp,
156 "(allow file-read-metadata\n"
157 " (regex"
158 " #\"^/$\"" /* / */
159 " #\"^/usr$\"" /* /usr */
160 " #\"^/Library$\"" /* /Library */
161 " #\"^/Library/Printers$\"" /* /Library/Printers */
162 " #\"^%s$\"" /* RequestRoot */
163 "))\n",
164 request);
165 /* Read and write TempDir, CacheDir, and other common folders */
166 cupsFilePrintf(fp,
167 "(allow file-write* file-read-data file-read-metadata\n"
168 " (regex"
169 " #\"^%s$\"" /* TempDir */
170 " #\"^%s/\"" /* TempDir/... */
171 " #\"^%s$\"" /* CacheDir */
172 " #\"^%s/\"" /* CacheDir/... */
173 " #\"^/private/var/db/\""
174 " #\"^/private/var/folders/\""
175 " #\"^/private/var/run/\""
176 " #\"^/Library/Application Support/\""
177 " #\"^/Library/Caches/\""
178 " #\"^/Library/Preferences/\""
179 " #\"^/Users/Shared/\""
180 "))\n",
181 temp, temp, cache, cache);
182 /* Read common folders */
183 cupsFilePrintf(fp,
184 "(allow file-read-data file-read-metadata\n"
185 " (regex"
186 " #\"^/AppleInternal$\""
187 " #\"^/AppleInternal/\""
188 " #\"^/bin$\"" /* /bin */
189 " #\"^/bin/\"" /* /bin/... */
190 " #\"^/private$\""
191 " #\"^/private/etc$\""
192 " #\"^/private/etc/\""
193 " #\"^/private/var$\""
194 " #\"^/private/var/db$\""
195 " #\"^/private/var/folders$\""
196 " #\"^/private/var/spool$\""
197 " #\"^/usr/bin$\"" /* /usr/bin */
198 " #\"^/usr/bin/\"" /* /usr/bin/... */
199 " #\"^/usr/libexec/cups$\"" /* /usr/libexec/cups */
200 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
201 " #\"^/usr/sbin$\"" /* /usr/sbin */
202 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
203 " #\"^/Library/Caches$\""
204 " #\"^/Library/Fonts$\""
205 " #\"^/Library/Fonts/\""
206 " #\"^/Library/Frameworks$\""
207 " #\"^/Library/Frameworks/\""
208 " #\"^/Library/Keychains$\""
209 " #\"^/Library/Keychains/\""
210 " #\"^/Library/Printers$\""
211 " #\"^/Library/Printers/\""
212 " #\"^%s/Library$\"" /* RequestRoot/Library */
213 " #\"^%s/Library/\"" /* RequestRoot/Library/... */
214 " #\"^%s$\"" /* ServerBin */
215 " #\"^%s/\"" /* ServerBin/... */
216 " #\"^%s$\"" /* ServerRoot */
217 " #\"^%s/\"" /* ServerRoot/... */
218 "))\n",
219 request, request, bin, bin, root, root);
220 if (Sandboxing == CUPSD_SANDBOXING_RELAXED)
221 {
222 /* Limited write access to /Library/Printers/... */
223 cupsFilePuts(fp,
224 "(allow file-write*\n"
225 " (regex"
226 " #\"^/Library/Printers/.*/\""
227 "))\n");
228 cupsFilePrintf(fp,
229 "(deny file-write*\n"
230 " (regex"
231 " #\"^/Library/Printers/PPDs$\""
232 " #\"^/Library/Printers/PPDs/\""
233 " #\"^/Library/Printers/PPD Plugins$\""
234 " #\"^/Library/Printers/PPD Plugins/\""
235 ")%s)\n", nodebug);
236 }
237 /* Allow execution of child processes */
238 cupsFilePuts(fp, "(allow process-fork)\n");
239 cupsFilePrintf(fp,
240 "(allow process-exec\n"
241 " (regex"
242 " #\"^/bin/\"" /* /bin/... */
243 " #\"^/usr/bin/\"" /* /usr/bin/... */
244 " #\"^/usr/libexec/cups/\"" /* /usr/libexec/cups/... */
245 " #\"^/usr/libexec/fax/\"" /* /usr/libexec/fax/... */
246 " #\"^/usr/sbin/\"" /* /usr/sbin/... */
247 " #\"^%s/\"" /* ServerBin/... */
248 " #\"^/Library/Printers/.*/\""
249 "))\n",
250 bin);
251 if (RunUser && getenv("CUPS_TESTROOT"))
252 {
253 /* Allow source directory access in "make test" environment */
254 char testroot[1024]; /* Root directory of test files */
255
256 cupsd_requote(testroot, getenv("CUPS_TESTROOT"), sizeof(testroot));
257
258 cupsFilePrintf(fp,
259 "(allow file-write* file-read-data file-read-metadata\n"
260 " (regex"
261 " #\"^%s$\"" /* CUPS_TESTROOT */
262 " #\"^%s/\"" /* CUPS_TESTROOT/... */
263 "))\n",
264 testroot, testroot);
265 cupsFilePrintf(fp,
266 "(allow process-exec\n"
267 " (regex"
268 " #\"^%s/\"" /* CUPS_TESTROOT/... */
269 "))\n",
270 testroot);
271 }
272 if (job_id)
273 {
274 /* Allow job filters to read the current job files... */
275 cupsFilePrintf(fp,
276 "(allow file-read-data file-read-metadata\n"
277 " (regex #\"^%s/([ac]%05d|d%05d-[0-9][0-9][0-9])$\"))\n",
278 request, job_id, job_id);
279 }
280 else
281 {
282 /* Allow email notifications from notifiers... */
283 cupsFilePuts(fp,
284 "(allow process-exec\n"
285 " (literal \"/usr/sbin/sendmail\")\n"
286 " (with no-sandbox))\n");
287 }
288 /* Allow outbound networking to local services */
289 cupsFilePuts(fp, "(allow network-outbound"
290 "\n (regex #\"^/private/var/run/\")");
291 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
292 lis;
293 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
294 {
295 if (httpAddrFamily(&(lis->address)) == AF_LOCAL)
296 {
297 httpAddrString(&(lis->address), domain, sizeof(domain));
298 cupsFilePrintf(fp, "\n (literal \"%s\")", domain);
299 }
300 }
301 if (allow_networking)
302 {
303 /* Allow TCP and UDP networking off the machine... */
304 cupsFilePuts(fp, "\n (remote tcp))\n");
305 cupsFilePuts(fp, "(allow network-bind)\n"); /* for LPD resvport */
306 cupsFilePuts(fp, "(allow network*\n"
307 " (local udp \"*:*\")\n"
308 " (remote udp \"*:*\"))\n");
309
310 /* Also allow access to Bluetooth, USB, device files, etc. */
311 cupsFilePuts(fp, "(allow iokit-open)\n");
312 cupsFilePuts(fp, "(allow file-write* file-read-data file-read-metadata file-ioctl\n"
313 " (regex #\"^/dev/\"))\n");
314 cupsFilePuts(fp, "(allow distributed-notification-post)\n");
315 }
316 else
317 {
318 /* Only allow SNMP (UDP) off the machine... */
319 cupsFilePuts(fp, ")\n");
320 cupsFilePuts(fp, "(allow network-outbound\n"
321 " (remote udp \"*:161\"))\n");
322 cupsFilePuts(fp, "(allow network-inbound\n"
323 " (local udp \"localhost:*\"))\n");
324 }
325 cupsFileClose(fp);
326
327 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d,allow_networking=%d) = \"%s\"", job_id, allow_networking, profile);
328 return ((void *)strdup(profile));
329
330 #else
331 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdCreateProfile(job_id=%d, allow_networking=%d) = NULL", job_id, allow_networking);
332
333 return (NULL);
334 #endif /* HAVE_SANDBOX_H */
335 }
336
337
338 /*
339 * 'cupsdDestroyProfile()' - Delete an execution profile.
340 */
341
342 void
343 cupsdDestroyProfile(void *profile) /* I - Profile */
344 {
345 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeleteProfile(profile=\"%s\")",
346 profile ? (char *)profile : "(null)");
347
348 #ifdef HAVE_SANDBOX_H
349 if (profile)
350 {
351 unlink((char *)profile);
352 free(profile);
353 }
354 #endif /* HAVE_SANDBOX_H */
355 }
356
357
358 /*
359 * 'cupsdEndProcess()' - End a process.
360 */
361
362 int /* O - 0 on success, -1 on failure */
363 cupsdEndProcess(int pid, /* I - Process ID */
364 int force) /* I - Force child to die */
365 {
366 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdEndProcess(pid=%d, force=%d)", pid,
367 force);
368
369 if (!pid)
370 return (0);
371
372 if (!RunUser)
373 {
374 /*
375 * When running as root, cupsd puts child processes in their own process
376 * group. Using "-pid" sends a signal to all processes in the group.
377 */
378
379 pid = -pid;
380 }
381
382 if (force)
383 return (kill(pid, SIGKILL));
384 else
385 return (kill(pid, SIGTERM));
386 }
387
388
389 /*
390 * 'cupsdFinishProcess()' - Finish a process and get its name.
391 */
392
393 const char * /* O - Process name */
394 cupsdFinishProcess(int pid, /* I - Process ID */
395 char *name, /* I - Name buffer */
396 size_t namelen, /* I - Size of name buffer */
397 int *job_id) /* O - Job ID pointer or NULL */
398 {
399 cupsd_proc_t key, /* Search key */
400 *proc; /* Matching process */
401
402
403 key.pid = pid;
404
405 if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL)
406 {
407 if (job_id)
408 *job_id = proc->job_id;
409
410 strlcpy(name, proc->name, namelen);
411 cupsArrayRemove(process_array, proc);
412 free(proc);
413 }
414 else
415 {
416 if (job_id)
417 *job_id = 0;
418
419 strlcpy(name, "unknown", namelen);
420 }
421
422 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdFinishProcess(pid=%d, name=%p, namelen=" CUPS_LLFMT ", job_id=%p(%d)) = \"%s\"", pid, name, CUPS_LLCAST namelen, job_id, job_id ? *job_id : 0, name);
423
424 return (name);
425 }
426
427
428 /*
429 * 'cupsdStartProcess()' - Start a process.
430 */
431
432 int /* O - Process ID or 0 */
433 cupsdStartProcess(
434 const char *command, /* I - Full path to command */
435 char *argv[], /* I - Command-line arguments */
436 char *envp[], /* I - Environment */
437 int infd, /* I - Standard input file descriptor */
438 int outfd, /* I - Standard output file descriptor */
439 int errfd, /* I - Standard error file descriptor */
440 int backfd, /* I - Backchannel file descriptor */
441 int sidefd, /* I - Sidechannel file descriptor */
442 int root, /* I - Run as root? */
443 void *profile, /* I - Security profile to use */
444 cupsd_job_t *job, /* I - Job associated with process */
445 int *pid) /* O - Process ID */
446 {
447 int i; /* Looping var */
448 const char *exec_path = command; /* Command to be exec'd */
449 char *real_argv[110], /* Real command-line arguments */
450 cups_exec[1024]; /* Path to "cups-exec" program */
451 uid_t user; /* Command UID */
452 cupsd_proc_t *proc; /* New process record */
453 #ifdef HAVE_POSIX_SPAWN
454 posix_spawn_file_actions_t actions; /* Spawn file actions */
455 posix_spawnattr_t attrs; /* Spawn attributes */
456 char user_str[16], /* User string */
457 group_str[16], /* Group string */
458 nice_str[16]; /* FilterNice string */
459 #elif defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
460 struct sigaction action; /* POSIX signal handler */
461 #endif /* HAVE_POSIX_SPAWN */
462 #if defined(__APPLE__)
463 char processPath[1024], /* CFProcessPath environment variable */
464 linkpath[1024]; /* Link path for symlinks... */
465 int linkbytes; /* Bytes for link path */
466 #endif /* __APPLE__ */
467
468
469 *pid = 0;
470
471 /*
472 * Figure out the UID for the child process...
473 */
474
475 if (RunUser)
476 user = RunUser;
477 else if (root)
478 user = 0;
479 else
480 user = User;
481
482 /*
483 * Check the permissions of the command we are running...
484 */
485
486 if (_cupsFileCheck(command, _CUPS_FILE_CHECK_PROGRAM, !RunUser,
487 cupsdLogFCMessage, job ? job->printer : NULL))
488 return (0);
489
490 #if defined(__APPLE__)
491 if (envp)
492 {
493 /*
494 * Add special voodoo magic for OS X - this allows OS X programs to access
495 * their bundle resources properly...
496 */
497
498 if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
499 {
500 /*
501 * Yes, this is a symlink to the actual program, nul-terminate and
502 * use it...
503 */
504
505 linkpath[linkbytes] = '\0';
506
507 if (linkpath[0] == '/')
508 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
509 linkpath);
510 else
511 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
512 dirname((char *)command), linkpath);
513 }
514 else
515 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
516
517 envp[0] = processPath; /* Replace <CFProcessPath> string */
518 }
519 #endif /* __APPLE__ */
520
521 /*
522 * Use helper program when we have a sandbox profile...
523 */
524
525 #ifndef HAVE_POSIX_SPAWN
526 if (profile)
527 #endif /* !HAVE_POSIX_SPAWN */
528 {
529 snprintf(cups_exec, sizeof(cups_exec), "%s/daemon/cups-exec", ServerBin);
530 snprintf(user_str, sizeof(user_str), "%d", user);
531 snprintf(group_str, sizeof(group_str), "%d", Group);
532 snprintf(nice_str, sizeof(nice_str), "%d", FilterNice);
533
534 real_argv[0] = cups_exec;
535 real_argv[1] = (char *)"-g";
536 real_argv[2] = group_str;
537 real_argv[3] = (char *)"-n";
538 real_argv[4] = nice_str;
539 real_argv[5] = (char *)"-u";
540 real_argv[6] = user_str;
541 real_argv[7] = profile;
542 real_argv[8] = (char *)command;
543
544 for (i = 0;
545 i < (int)(sizeof(real_argv) / sizeof(real_argv[0]) - 10) && argv[i];
546 i ++)
547 real_argv[i + 9] = argv[i];
548
549 real_argv[i + 9] = NULL;
550
551 argv = real_argv;
552 exec_path = cups_exec;
553 }
554
555 if (LogLevel == CUPSD_LOG_DEBUG2)
556 {
557 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Preparing to start \"%s\", arguments:", command);
558
559 for (i = 0; argv[i]; i ++)
560 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: argv[%d] = \"%s\"", i, argv[i]);
561 }
562
563 #ifdef HAVE_POSIX_SPAWN
564 /*
565 * Setup attributes and file actions for the spawn...
566 */
567
568 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Setting spawn attributes.");
569 posix_spawnattr_init(&attrs);
570 posix_spawnattr_setflags(&attrs, POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF);
571
572 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Setting file actions.");
573 posix_spawn_file_actions_init(&actions);
574 if (infd != 0)
575 {
576 if (infd < 0)
577 posix_spawn_file_actions_addopen(&actions, 0, "/dev/null", O_WRONLY, 0);
578 else
579 posix_spawn_file_actions_adddup2(&actions, infd, 0);
580 }
581
582 if (outfd != 1)
583 {
584 if (outfd < 0)
585 posix_spawn_file_actions_addopen(&actions, 1, "/dev/null", O_WRONLY, 0);
586 else
587 posix_spawn_file_actions_adddup2(&actions, outfd, 1);
588 }
589
590 if (errfd != 2)
591 {
592 if (errfd < 0)
593 posix_spawn_file_actions_addopen(&actions, 2, "/dev/null", O_WRONLY, 0);
594 else
595 posix_spawn_file_actions_adddup2(&actions, errfd, 2);
596 }
597
598 if (backfd != 3 && backfd >= 0)
599 posix_spawn_file_actions_adddup2(&actions, backfd, 3);
600
601 if (sidefd != 4 && sidefd >= 0)
602 posix_spawn_file_actions_adddup2(&actions, sidefd, 4);
603
604 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: Calling posix_spawn.");
605
606 if (posix_spawn(pid, exec_path, &actions, &attrs, argv, envp ? envp : environ))
607 {
608 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command, strerror(errno));
609
610 *pid = 0;
611 }
612 else
613 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdStartProcess: pid=%d", (int)*pid);
614
615 posix_spawn_file_actions_destroy(&actions);
616 posix_spawnattr_destroy(&attrs);
617
618 #else
619 /*
620 * Block signals before forking...
621 */
622
623 cupsdHoldSignals();
624
625 if ((*pid = fork()) == 0)
626 {
627 /*
628 * Child process goes here; update stderr as needed...
629 */
630
631 if (errfd != 2)
632 {
633 if (errfd < 0)
634 errfd = open("/dev/null", O_WRONLY);
635
636 if (errfd != 2)
637 {
638 dup2(errfd, 2);
639 close(errfd);
640 }
641 }
642
643 /*
644 * Put this process in its own process group so that we can kill any child
645 * processes it creates.
646 */
647
648 # ifdef HAVE_SETPGID
649 if (!RunUser && setpgid(0, 0))
650 exit(errno + 100);
651 # else
652 if (!RunUser && setpgrp())
653 exit(errno + 100);
654 # endif /* HAVE_SETPGID */
655
656 /*
657 * Update the remaining file descriptors as needed...
658 */
659
660 if (infd != 0)
661 {
662 if (infd < 0)
663 infd = open("/dev/null", O_RDONLY);
664
665 if (infd != 0)
666 {
667 dup2(infd, 0);
668 close(infd);
669 }
670 }
671
672 if (outfd != 1)
673 {
674 if (outfd < 0)
675 outfd = open("/dev/null", O_WRONLY);
676
677 if (outfd != 1)
678 {
679 dup2(outfd, 1);
680 close(outfd);
681 }
682 }
683
684 if (backfd != 3 && backfd >= 0)
685 {
686 dup2(backfd, 3);
687 close(backfd);
688 fcntl(3, F_SETFL, O_NDELAY);
689 }
690
691 if (sidefd != 4 && sidefd >= 0)
692 {
693 dup2(sidefd, 4);
694 close(sidefd);
695 fcntl(4, F_SETFL, O_NDELAY);
696 }
697
698 /*
699 * Change the priority of the process based on the FilterNice setting.
700 * (this is not done for root processes...)
701 */
702
703 if (!root)
704 nice(FilterNice);
705
706 /*
707 * Reset group membership to just the main one we belong to.
708 */
709
710 if (!RunUser && setgid(Group))
711 exit(errno + 100);
712
713 if (!RunUser && setgroups(1, &Group))
714 exit(errno + 100);
715
716 /*
717 * Change user to something "safe"...
718 */
719
720 if (!RunUser && user && setuid(user))
721 exit(errno + 100);
722
723 /*
724 * Change umask to restrict permissions on created files...
725 */
726
727 umask(077);
728
729 /*
730 * Unblock signals before doing the exec...
731 */
732
733 # ifdef HAVE_SIGSET
734 sigset(SIGTERM, SIG_DFL);
735 sigset(SIGCHLD, SIG_DFL);
736 sigset(SIGPIPE, SIG_DFL);
737 # elif defined(HAVE_SIGACTION)
738 memset(&action, 0, sizeof(action));
739
740 sigemptyset(&action.sa_mask);
741 action.sa_handler = SIG_DFL;
742
743 sigaction(SIGTERM, &action, NULL);
744 sigaction(SIGCHLD, &action, NULL);
745 sigaction(SIGPIPE, &action, NULL);
746 # else
747 signal(SIGTERM, SIG_DFL);
748 signal(SIGCHLD, SIG_DFL);
749 signal(SIGPIPE, SIG_DFL);
750 # endif /* HAVE_SIGSET */
751
752 cupsdReleaseSignals();
753
754 /*
755 * Execute the command; if for some reason this doesn't work, log an error
756 * exit with a non-zero value...
757 */
758
759 if (envp)
760 execve(exec_path, argv, envp);
761 else
762 execv(exec_path, argv);
763
764 exit(errno + 100);
765 }
766 else if (*pid < 0)
767 {
768 /*
769 * Error - couldn't fork a new process!
770 */
771
772 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
773 strerror(errno));
774
775 *pid = 0;
776 }
777
778 cupsdReleaseSignals();
779 #endif /* HAVE_POSIX_SPAWN */
780
781 if (*pid)
782 {
783 if (!process_array)
784 process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
785
786 if (process_array)
787 {
788 if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
789 {
790 proc->pid = *pid;
791 proc->job_id = job ? job->id : 0;
792 _cups_strcpy(proc->name, command);
793
794 cupsArrayAdd(process_array, proc);
795 }
796 }
797 }
798
799 cupsdLogMessage(CUPSD_LOG_DEBUG2,
800 "cupsdStartProcess(command=\"%s\", argv=%p, envp=%p, "
801 "infd=%d, outfd=%d, errfd=%d, backfd=%d, sidefd=%d, root=%d, "
802 "profile=%p, job=%p(%d), pid=%p) = %d",
803 command, argv, envp, infd, outfd, errfd, backfd, sidefd,
804 root, profile, job, job ? job->id : 0, pid, *pid);
805
806 return (*pid);
807 }
808
809
810 /*
811 * 'compare_procs()' - Compare two processes.
812 */
813
814 static int /* O - Result of comparison */
815 compare_procs(cupsd_proc_t *a, /* I - First process */
816 cupsd_proc_t *b) /* I - Second process */
817 {
818 return (a->pid - b->pid);
819 }
820
821
822 #ifdef HAVE_SANDBOX_H
823 /*
824 * 'cupsd_requote()' - Make a regular-expression version of a string.
825 */
826
827 static char * /* O - Quoted string */
828 cupsd_requote(char *dst, /* I - Destination buffer */
829 const char *src, /* I - Source string */
830 size_t dstsize) /* I - Size of destination buffer */
831 {
832 int ch; /* Current character */
833 char *dstptr, /* Current position in buffer */
834 *dstend; /* End of destination buffer */
835
836
837 dstptr = dst;
838 dstend = dst + dstsize - 2;
839
840 while (*src && dstptr < dstend)
841 {
842 ch = *src++;
843
844 if (ch == '/' && !*src)
845 break; /* Don't add trailing slash */
846
847 if (strchr(".?*()[]^$\\", ch))
848 *dstptr++ = '\\';
849
850 *dstptr++ = (char)ch;
851 }
852
853 *dstptr = '\0';
854
855 return (dst);
856 }
857 #endif /* HAVE_SANDBOX_H */
858
859
860 /*
861 * End of "$Id$".
862 */