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