]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/process.c
38f6a41f985b9ebe6af12299fc3f724a42cb6163
4 * Process management routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdEndProcess() - End a process.
27 * cupsdStartProcess() - Start a process.
31 * Include necessary headers...
39 * 'cupsdEndProcess()' - End a process.
42 int /* O - 0 on success, -1 on failure */
43 cupsdEndProcess(int pid
, /* I - Process ID */
44 int force
) /* I - Force child to die */
47 return (kill(pid
, SIGKILL
));
49 return (kill(pid
, SIGTERM
));
54 * 'cupsdStartProcess()' - Start a process.
57 int /* O - Process ID or 0 */
59 const char *command
, /* I - Full path to command */
60 char *argv
[], /* I - Command-line arguments */
61 char *envp
[], /* I - Environment */
62 int infd
, /* I - Standard input file descriptor */
63 int outfd
, /* I - Standard output file descriptor */
64 int errfd
, /* I - Standard error file descriptor */
65 int backfd
, /* I - Backchannel file descriptor */
66 int root
, /* I - Run as root? */
67 int *pid
) /* O - Process ID */
69 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
70 struct sigaction action
; /* POSIX signal handler */
71 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
74 cupsdLogMessage(L_DEBUG2
, "cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
75 command
, argv
, envp
, infd
, outfd
, errfd
);
78 * Block signals before forking...
83 if ((*pid
= fork()) == 0)
86 * Child process goes here...
88 * Update stdin/stdout/stderr as needed...
97 open("/dev/null", O_RDONLY
);
105 open("/dev/null", O_WRONLY
);
113 open("/dev/null", O_WRONLY
);
121 open("/dev/null", O_RDWR
);
122 fcntl(3, F_SETFL
, O_NDELAY
);
126 * Change the priority of the process based on the FilterNice setting.
127 * (this is not done for backends...)
134 * Change user to something "safe"...
137 if (!root
&& !RunUser
)
140 * Running as root, so change to non-priviledged user...
146 if (setgroups(1, &Group
))
155 * Reset group membership to just the main one we belong to.
158 setgroups(1, &Group
);
162 * Change umask to restrict permissions on created files...
168 * Unblock signals before doing the exec...
172 sigset(SIGTERM
, SIG_DFL
);
173 sigset(SIGCHLD
, SIG_DFL
);
174 #elif defined(HAVE_SIGACTION)
175 memset(&action
, 0, sizeof(action
));
177 sigemptyset(&action
.sa_mask
);
178 action
.sa_handler
= SIG_DFL
;
180 sigaction(SIGTERM
, &action
, NULL
);
181 sigaction(SIGCHLD
, &action
, NULL
);
183 signal(SIGTERM
, SIG_DFL
);
184 signal(SIGCHLD
, SIG_DFL
);
185 #endif /* HAVE_SIGSET */
187 cupsdReleaseSignals();
190 * Execute the command; if for some reason this doesn't work,
191 * return the error code...
195 execve(command
, argv
, envp
);
197 execv(command
, argv
);
206 * Error - couldn't fork a new process!
209 cupsdLogMessage(L_ERROR
, "Unable to fork %s - %s.", command
, strerror(errno
));
214 cupsdReleaseSignals();