~ubuntu-branches/debian/wheezy/upstart/wheezy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* upstart
 *
 * process.c - job process handling
 *
 * Copyright © 2006 Canonical Ltd.
 * Author: Scott James Remnant <scott@ubuntu.com>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */


#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/resource.h>

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/signal.h>
#include <nih/logging.h>
#include <nih/error.h>

#include "job.h"
#include "process.h"


/* Prototypes for static functions */
static int process_setup_limits      (Job *job);
static int process_setup_environment (Job *job);


/**
 * process_spawn:
 * @job: job context for process to be spawned in,
 * @argv: %NULL-terminated list of arguments for the process.
 *
 * This function spawns a new process using the @job details to set up the
 * environment for it; the process is always a session and process group
 * leader as we never want anything in our own group.
 *
 * The process to be executed is given in the @argv array which is passed
 * directly to #execvp, so should be in the same %NULL-terminated form with
 * the first argument containing the path or filename of the binary.  The
 * PATH environment for the job will be searched.
 *
 * This function only spawns the process, it is up to the caller to ensure
 * that the information is saved into the job and the process is watched, etc.
 *
 * Note that the only error raised within this function is a failure of the
 * #fork syscall as the environment is set up within the child process.
 *
 * Returns: process id of new process on success, -1 on raised error
 **/
pid_t
process_spawn (Job          *job,
	       char * const  argv[])
{
	NihError *err;
	sigset_t  child_set, orig_set;
	pid_t     pid;
	int       i;

	nih_assert (job != NULL);

	/* Block SIGCHLD while we fork to avoid surprises */
	sigemptyset (&child_set);
	sigaddset (&child_set, SIGCHLD);
	sigprocmask (SIG_BLOCK, &child_set, &orig_set);

	/* Fork the child process, and return either the id or failure
	 * back to the caller.
	 */
	pid = fork ();
	if (pid > 0) {
		sigprocmask (SIG_SETMASK, &orig_set, NULL);

		nih_debug ("Spawned process %d for %s", pid, job->name);
		return pid;
	} else if (pid < 0) {
		sigprocmask (SIG_SETMASK, &orig_set, NULL);

		nih_return_system_error (-1);
	}


	/* We're now in the child process.
	 *
	 * First we close the standard file descriptors so we don't
	 * inherit them directly from init but get to pick them ourselves
	 */
	for (i = 0; i < 3; i++)
		close (i);

	/* Reset the signal mask, and put all signal handlers back to their
	 * default handling so the child isn't unexpectantly ignoring them
	 */
	sigprocmask (SIG_SETMASK, &orig_set, NULL);
	nih_signal_reset ();

	/* Become the leader of a new session and process group */
	setsid ();

	/* The job defines what the process's standard input, output and
	 * error file descriptors should look like; set those up.
	 */
	if (process_setup_console (job->console) < 0)
		goto error;

	/* The job also gives us a consistent world to run all processes
	 * in, including resource limits and suchlike.  Set that all up.
	 */
	if (process_setup_limits (job) < 0)
		goto error;

	/* And finally set up the environment variables for the process.
	 */
	if (process_setup_environment (job) < 0)
		goto error;

	/* Execute the process, if we escape from here it failed */
	if (execvp (argv[0], argv) < 0)
		nih_error_raise_system ();

error:
	err = nih_error_get ();

	nih_error (_("Unable to execute \"%s\" for %s: %s"),
		   argv[0], job->name, err->message);
	nih_free (err);

	exit (1);
}

/**
 * process_setup_limits:
 * @job: job details.
 *
 * Set up the boundaries for the current process such as the file creation
 * mask, priority, limits, working directory, etc. from the details stored
 * in the @job given.
 *
 * Returns: zero on success, negative value on raised error.
 **/
static int
process_setup_limits (Job *job)
{
	int i;

	nih_assert (job != NULL);

	umask (job->umask);

	if (setpriority (PRIO_PROCESS, 0, job->nice) < 0)
		nih_return_system_error (-1);

	for (i = 0; i < RLIMIT_NLIMITS; i++) {
		if (! job->limits[i])
			continue;

		if (setrlimit (i, job->limits[i]) < 0)
			nih_return_system_error (-1);
	}

	if (job->chroot) {
		if (chroot (job->chroot) < 0)
			nih_return_system_error (-1);
		if (chdir ("/") < 0)
			nih_return_system_error (-1);
	}

	if (job->chdir) {
		if (chdir (job->chdir) < 0)
			nih_return_system_error (-1);
	}

	return 0;
}

/**
 * process_setup_environment:
 * @job: job details.
 *
 * Set up the environment variables for the current process based on the
 * the details in @job.
 *
 * Returns: zero on success, negative value on raised error.
 **/
static int
process_setup_environment (Job *job)
{
	char **env;

	nih_assert (job != NULL);

	if (clearenv () < 0)
		nih_return_system_error (-1);

	for (env = job->env; env && *env; env++)
		if (putenv (*env) < 0)
			nih_return_system_error (-1);

	return 0;
}


/**
 * process_kill:
 * @job: job context of process to be killed,
 * @pid: process id of process,
 * @force: force the death.
 *
 * This function only sends the process a TERM signal (KILL if @force is
 * TRUE), it is up to the caller to ensure that the state change is saved
 * into the job and the process is watched; one may also wish to send
 * further signals later.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
process_kill (Job   *job,
	      pid_t  pid,
	      int    force)
{
	int signal;

	nih_assert (job != NULL);
	nih_assert (pid > 0);

	signal = (force ? SIGKILL : SIGTERM);

	if (kill (pid, signal) < 0)
		nih_return_system_error (-1);

	return 0;
}


/**
 * process_setup_console:
 * @type: console type.
 *
 * Set up the standard input, output and error file descriptors for the
 * current process based on the console @type given.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
process_setup_console (ConsoleType type)
{
	int fd;

	switch (type) {
	case CONSOLE_OUTPUT:
	case CONSOLE_OWNER:
		/* Open the console itself */
		fd = open (CONSOLE, O_RDWR|O_NOCTTY);
		if (fd >= 0) {
			struct termios tty;

			/* Take ownership of the console */
			if (type == CONSOLE_OWNER)
				ioctl (fd, TIOCSCTTY, 1);

			/* Set up the console flags to something sensible
			 * (cribbed from sysvinit)
			 */
			tcgetattr (fd, &tty);

			tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB
					| PARENB | PARODD);
			tty.c_cflag |= (HUPCL | CLOCAL | CREAD);

			/* Set up usual keys */
			tty.c_cc[VINTR]  = 3;   /* ^C */
			tty.c_cc[VQUIT]  = 28;  /* ^\ */
			tty.c_cc[VERASE] = 127;
			tty.c_cc[VKILL]  = 24;  /* ^X */
			tty.c_cc[VEOF]   = 4;   /* ^D */
			tty.c_cc[VTIME]  = 0;
			tty.c_cc[VMIN]   = 1;
			tty.c_cc[VSTART] = 17;  /* ^Q */
			tty.c_cc[VSTOP]  = 19;  /* ^S */
			tty.c_cc[VSUSP]  = 26;  /* ^Z */

			/* Pre and post processing */
			tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
			tty.c_oflag = (OPOST | ONLCR);
			tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL
				       | ECHOPRT | ECHOKE);

			/* Set the terminal line */
			tcsetattr (fd, TCSANOW, &tty);
			tcflush (fd, TCIOFLUSH);
			break;
		}

		/* Open failed, fall through to CONSOLE_NONE handling */
		nih_warn (_("Unable to open console: %s"), strerror (errno));

	case CONSOLE_NONE:
	case CONSOLE_LOGGED:
		fd = open (DEV_NULL, O_RDWR);
		if (fd < 0)
			nih_return_system_error (-1);

		break;
	}

	/* Copy to standard output and standard error */
	dup (fd);
	dup (fd);

	return 0;
}