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