xref: /illumos-gate/usr/src/cmd/svc/startd/fork.c (revision 437220cd296f6d8b6654d6d52508b40b1e2d1ac7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * fork.c - safe forking for svc.startd
30  *
31  * fork_configd() and fork_sulogin() are related, special cases that handle the
32  * spawning of specific client processes for svc.startd.
33  */
34 
35 #include <sys/contract/process.h>
36 #include <sys/corectl.h>
37 #include <sys/ctfs.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <sys/uio.h>
41 #include <sys/wait.h>
42 #include <assert.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <libcontract.h>
46 #include <libcontract_priv.h>
47 #include <limits.h>
48 #include <port.h>
49 #include <signal.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <utmpx.h>
56 
57 #include "configd_exit.h"
58 #include "protocol.h"
59 #include "startd.h"
60 
61 static	struct	utmpx	*utmpp;	/* pointer for getutxent() */
62 
63 pid_t
64 startd_fork1(int *forkerr)
65 {
66 	pid_t p;
67 
68 	/*
69 	 * prefork stack
70 	 */
71 	wait_prefork();
72 
73 	p = fork1();
74 
75 	if (p == -1 && forkerr != NULL)
76 		*forkerr = errno;
77 
78 	/*
79 	 * postfork stack
80 	 */
81 	wait_postfork(p);
82 
83 	return (p);
84 }
85 
86 /*
87  * void fork_mount(char *, char *)
88  *   Run mount(1M) with the given options and mount point.  (mount(1M) has much
89  *   hidden knowledge; it's much less correct to reimplement that logic here to
90  *   save a fork(2)/exec(2) invocation.)
91  */
92 int
93 fork_mount(char *path, char *opts)
94 {
95 	pid_t pid;
96 	uint_t tries = 0;
97 	int status;
98 
99 	for (pid = fork1(); pid == -1; pid = fork1()) {
100 		if (++tries > MAX_MOUNT_RETRIES)
101 			return (-1);
102 
103 		(void) sleep(tries);
104 	}
105 
106 	if (pid != 0) {
107 		(void) waitpid(pid, &status, 0);
108 
109 		/*
110 		 * If our mount(1M) invocation exited by peculiar means, or with
111 		 * a non-zero status, our mount likelihood is low.
112 		 */
113 		if (!WIFEXITED(status) ||
114 		    WEXITSTATUS(status) != 0)
115 			return (-1);
116 
117 		return (0);
118 	}
119 
120 	(void) execl("/sbin/mount", "mount", "-o", opts, path, NULL);
121 
122 	return (-1);
123 }
124 
125 /*
126  * pid_t fork_common(...)
127  *   Common routine used by fork_sulogin and fork_configd to fork a
128  *   process in a contract with the provided terms.  Invokes
129  *   fork_sulogin (with its no-fork argument set) on errors.
130  */
131 static pid_t
132 fork_common(const char *name, int retries, ctid_t *ctidp,
133     uint_t inf, uint_t crit, uint_t fatal, uint_t param, uint64_t cookie)
134 {
135 	uint_t tries = 0;
136 	int ctfd, err;
137 	pid_t pid;
138 
139 	/*
140 	 * Establish process contract terms.
141 	 */
142 	if ((ctfd = open64(CTFS_ROOT "/process/template", O_RDWR)) == -1) {
143 		fork_sulogin(B_TRUE, "Could not open process contract template "
144 		    "for %s: %s\n", name, strerror(errno));
145 		/* NOTREACHED */
146 	}
147 
148 	err = ct_tmpl_set_critical(ctfd, crit);
149 	err |= ct_pr_tmpl_set_fatal(ctfd, fatal);
150 	err |= ct_tmpl_set_informative(ctfd, inf);
151 	err |= ct_pr_tmpl_set_param(ctfd, param);
152 	err |= ct_tmpl_set_cookie(ctfd, cookie);
153 	if (err) {
154 		(void) close(ctfd);
155 		fork_sulogin(B_TRUE, "Could not set %s process contract "
156 		    "terms\n", name);
157 		/* NOTREACHED */
158 	}
159 
160 	if (err = ct_tmpl_activate(ctfd)) {
161 		(void) close(ctfd);
162 		fork_sulogin(B_TRUE, "Could not activate %s process contract "
163 		    "template: %s\n", name, strerror(err));
164 		/* NOTREACHED */
165 	}
166 
167 	/*
168 	 * Attempt to fork "retries" times.
169 	 */
170 	for (pid = fork1(); pid == -1; pid = fork1()) {
171 		if (++tries > retries) {
172 			/*
173 			 * When we exit the sulogin session, init(1M)
174 			 * will restart svc.startd(1M).
175 			 */
176 			err = errno;
177 			(void) ct_tmpl_clear(ctfd);
178 			(void) close(ctfd);
179 			fork_sulogin(B_TRUE, "Could not fork to start %s: %s\n",
180 			    name, strerror(err));
181 			/* NOTREACHED */
182 		}
183 		(void) sleep(tries);
184 	}
185 
186 	/*
187 	 * Clean up, return pid and ctid.
188 	 */
189 	if (pid != 0 && (errno = contract_latest(ctidp)) != 0)
190 		uu_die("Could not get new contract id for %s\n", name);
191 	(void) ct_tmpl_clear(ctfd);
192 	(void) close(ctfd);
193 
194 	return (pid);
195 }
196 
197 /*
198  * void fork_sulogin(boolean_t, const char *, ...)
199  *   When we are invoked with the -s flag from boot (or run into an unfixable
200  *   situation), we run a private copy of sulogin.  When the sulogin session
201  *   is ended, we continue.  This is the last fallback action for system
202  *   maintenance.
203  *
204  *   If immediate is true, fork_sulogin() executes sulogin(1M) directly, without
205  *   forking.
206  *
207  *   Because fork_sulogin() is needed potentially before we daemonize, we leave
208  *   it outside the wait_register() framework.
209  */
210 /*PRINTFLIKE2*/
211 void
212 fork_sulogin(boolean_t immediate, const char *format, ...)
213 {
214 	va_list args;
215 	int i, fd_console;
216 
217 	(void) printf("Requesting System Maintenance Mode\n");
218 
219 	if (!booting_to_single_user)
220 		(void) printf("(See /lib/svc/share/README for more "
221 		    "information.)\n");
222 
223 	va_start(args, format);
224 	(void) vprintf(format, args);
225 	va_end(args);
226 
227 	if (!immediate) {
228 		ctid_t	ctid;
229 		pid_t	pid;
230 
231 		pid = fork_common("sulogin", MAX_SULOGIN_RETRIES, &ctid,
232 		    CT_PR_EV_HWERR, 0, CT_PR_EV_HWERR, CT_PR_PGRPONLY,
233 		    SULOGIN_COOKIE);
234 
235 		if (pid != 0) {
236 			(void) waitpid(pid, NULL, 0);
237 			contract_abandon(ctid);
238 			return;
239 		}
240 		/* close all inherited fds */
241 		closefrom(0);
242 	} else {
243 		(void) printf("Directly executing sulogin.\n");
244 		/*
245 		 * Can't call closefrom() in this MT section
246 		 * so safely close a minimum set of fds.
247 		 */
248 		for (i = 0; i < 3; i++)
249 			(void) close(i);
250 	}
251 
252 	(void) setpgrp();
253 
254 	/* open the console for sulogin */
255 	if ((fd_console = open("/dev/console", O_RDWR)) >= 0) {
256 		if (fd_console != STDIN_FILENO)
257 			while (dup2(fd_console, STDIN_FILENO) < 0 &&
258 			    errno == EINTR)
259 				;
260 		if (fd_console != STDOUT_FILENO)
261 			while (dup2(fd_console, STDOUT_FILENO) < 0 &&
262 			    errno == EINTR)
263 				;
264 		if (fd_console != STDERR_FILENO)
265 			while (dup2(fd_console, STDERR_FILENO) < 0 &&
266 			    errno == EINTR)
267 				;
268 		if (fd_console > 2)
269 			(void) close(fd_console);
270 	}
271 
272 	setutxent();
273 	while ((utmpp = getutxent()) != NULL) {
274 		if (strcmp(utmpp->ut_user, "LOGIN") != 0) {
275 			if (strcmp(utmpp->ut_line, "console") == 0) {
276 				(void) kill(utmpp->ut_pid, 9);
277 				break;
278 			}
279 		}
280 	}
281 
282 	(void) execl("/sbin/sulogin", "sulogin", NULL);
283 
284 	uu_warn("Could not exec() sulogin");
285 
286 	exit(1);
287 }
288 
289 #define	CONFIGD_PATH	"/lib/svc/bin/svc.configd"
290 
291 /*
292  * void fork_configd(int status)
293  *   We are interested in exit events (since the parent's exiting means configd
294  *   is ready to run and since the child's exiting indicates an error case) and
295  *   in empty events.  This means we have a unique template for initiating
296  *   configd.
297  */
298 /*ARGSUSED*/
299 void
300 fork_configd(int exitstatus)
301 {
302 	pid_t pid;
303 	ctid_t ctid = -1;
304 	int err;
305 	char path[PATH_MAX];
306 
307 retry:
308 	log_framework(LOG_DEBUG, "fork_configd trying to start svc.configd\n");
309 
310 	/*
311 	 * If we're retrying, we will have an old contract lying around
312 	 * from the failure.  Since we're going to be creating a new
313 	 * contract shortly, we abandon the old one now.
314 	 */
315 	if (ctid != -1)
316 		contract_abandon(ctid);
317 	ctid = -1;
318 
319 	pid = fork_common("svc.configd", MAX_CONFIGD_RETRIES, &ctid,
320 	    0, CT_PR_EV_EXIT, 0, CT_PR_INHERIT | CT_PR_REGENT, CONFIGD_COOKIE);
321 
322 	if (pid != 0) {
323 		int exitstatus;
324 
325 		st->st_configd_pid = pid;
326 
327 		if (waitpid(pid, &exitstatus, 0) == -1) {
328 			fork_sulogin(B_FALSE, "waitpid on svc.configd "
329 			    "failed: %s\n", strerror(errno));
330 		} else if (WIFEXITED(exitstatus)) {
331 			char *errstr;
332 
333 			/*
334 			 * Examine exitstatus.  This will eventually get more
335 			 * complicated, as we will want to teach startd how to
336 			 * invoke configd with alternate repositories, etc.
337 			 *
338 			 * Note that exec(2) failure results in an exit status
339 			 * of 1, resulting in the default clause below.
340 			 */
341 
342 			/*
343 			 * Assign readable strings to cases we don't handle, or
344 			 * have error outcomes that cannot be eliminated.
345 			 */
346 			switch (WEXITSTATUS(exitstatus)) {
347 			case CONFIGD_EXIT_BAD_ARGS:
348 				errstr = "bad arguments";
349 				break;
350 
351 			case CONFIGD_EXIT_DATABASE_BAD:
352 				errstr = "database corrupt";
353 				break;
354 
355 			case CONFIGD_EXIT_DATABASE_LOCKED:
356 				errstr = "database locked";
357 				break;
358 			case CONFIGD_EXIT_INIT_FAILED:
359 				errstr = "initialization failure";
360 				break;
361 			case CONFIGD_EXIT_DOOR_INIT_FAILED:
362 				errstr = "door initialization failure";
363 				break;
364 			case CONFIGD_EXIT_DATABASE_INIT_FAILED:
365 				errstr = "database initialization failure";
366 				break;
367 			case CONFIGD_EXIT_NO_THREADS:
368 				errstr = "no threads available";
369 				break;
370 			case CONFIGD_EXIT_LOST_MAIN_DOOR:
371 				errstr = "lost door server attachment";
372 				break;
373 			case 1:
374 				errstr = "execution failure";
375 				break;
376 			default:
377 				errstr = "unknown error";
378 				break;
379 			}
380 
381 			/*
382 			 * Remedial actions for various configd failures.
383 			 */
384 			switch (WEXITSTATUS(exitstatus)) {
385 			case CONFIGD_EXIT_OKAY:
386 				break;
387 
388 			case CONFIGD_EXIT_DATABASE_LOCKED:
389 				/* attempt remount of / read-write */
390 				if (fs_is_read_only("/", NULL) == 1) {
391 					if (fs_remount("/") == -1)
392 						fork_sulogin(B_FALSE,
393 						    "remount of root "
394 						    "filesystem failed\n");
395 
396 					goto retry;
397 				}
398 				break;
399 
400 			default:
401 				fork_sulogin(B_FALSE, "svc.configd exited "
402 				    "with status %d (%s)\n",
403 				    WEXITSTATUS(exitstatus), errstr);
404 				goto retry;
405 			}
406 		} else if (WIFSIGNALED(exitstatus)) {
407 			char signame[SIG2STR_MAX];
408 
409 			if (sig2str(WTERMSIG(exitstatus), signame))
410 				(void) snprintf(signame, SIG2STR_MAX,
411 				    "signum %d", WTERMSIG(exitstatus));
412 
413 			fork_sulogin(B_FALSE, "svc.configd signalled:"
414 			    " %s\n", signame);
415 
416 			goto retry;
417 		} else {
418 			fork_sulogin(B_FALSE, "svc.configd non-exit "
419 			    "condition: 0x%x\n", exitstatus);
420 
421 			goto retry;
422 		}
423 
424 		/*
425 		 * Announce that we have a valid svc.configd status.
426 		 */
427 		MUTEX_LOCK(&st->st_configd_live_lock);
428 		st->st_configd_lives = 1;
429 		err = pthread_cond_broadcast(&st->st_configd_live_cv);
430 		assert(err == 0);
431 		MUTEX_UNLOCK(&st->st_configd_live_lock);
432 
433 		log_framework(LOG_DEBUG, "fork_configd broadcasts configd is "
434 		    "live\n");
435 		return;
436 	}
437 
438 	/*
439 	 * Set our per-process core file path to leave core files in
440 	 * /etc/svc/volatile directory, named after the PID to aid in debugging.
441 	 */
442 	(void) snprintf(path, sizeof (path),
443 	    "/etc/svc/volatile/core.configd.%%p");
444 
445 	(void) core_set_process_path(path, strlen(path) + 1, getpid());
446 
447 	log_framework(LOG_DEBUG, "executing svc.configd\n");
448 
449 	(void) execl(CONFIGD_PATH, CONFIGD_PATH, NULL);
450 
451 	/*
452 	 * Status code is used above to identify configd exec failure.
453 	 */
454 	exit(1);
455 }
456 
457 void *
458 fork_configd_thread(void *vctid)
459 {
460 	int fd, err;
461 	ctid_t configd_ctid = (ctid_t)vctid;
462 
463 	if (configd_ctid == -1) {
464 		log_framework(LOG_DEBUG,
465 		    "fork_configd_thread starting svc.configd\n");
466 		fork_configd(0);
467 	} else {
468 		/*
469 		 * configd_ctid is known:  we broadcast and continue.
470 		 * test contract for appropriate state by verifying that
471 		 * there is one or more processes within it?
472 		 */
473 		log_framework(LOG_DEBUG,
474 		    "fork_configd_thread accepting svc.configd with CTID %ld\n",
475 		    configd_ctid);
476 		MUTEX_LOCK(&st->st_configd_live_lock);
477 		st->st_configd_lives = 1;
478 		(void) pthread_cond_broadcast(&st->st_configd_live_cv);
479 		MUTEX_UNLOCK(&st->st_configd_live_lock);
480 	}
481 
482 	fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
483 	if (fd == -1)
484 		uu_die("process bundle open failed");
485 
486 	/*
487 	 * Make sure we get all events (including those generated by configd
488 	 * before this thread was started).
489 	 */
490 	err = ct_event_reset(fd);
491 	assert(err == 0);
492 
493 	for (;;) {
494 		int efd, sfd;
495 		ct_evthdl_t ev;
496 		uint32_t type;
497 		ctevid_t evid;
498 		ct_stathdl_t status;
499 		ctid_t ctid;
500 		uint64_t cookie;
501 		pid_t pid;
502 
503 		if (err = ct_event_read_critical(fd, &ev)) {
504 			assert(err != EINVAL && err != EAGAIN);
505 			log_error(LOG_WARNING,
506 			    "Error reading next contract event: %s",
507 			    strerror(err));
508 			continue;
509 		}
510 
511 		evid = ct_event_get_evid(ev);
512 		ctid = ct_event_get_ctid(ev);
513 		type = ct_event_get_type(ev);
514 
515 		/* Fetch cookie. */
516 		sfd = contract_open(ctid, "process", "status", O_RDONLY);
517 		if (sfd < 0) {
518 			ct_event_free(ev);
519 			continue;
520 		}
521 
522 		if (err = ct_status_read(sfd, CTD_COMMON, &status)) {
523 			log_framework(LOG_WARNING, "Could not get status for "
524 			    "contract %ld: %s\n", ctid, strerror(err));
525 
526 			ct_event_free(ev);
527 			startd_close(sfd);
528 			continue;
529 		}
530 
531 		cookie = ct_status_get_cookie(status);
532 
533 		ct_status_free(status);
534 
535 		startd_close(sfd);
536 
537 		/*
538 		 * Don't process events from contracts we aren't interested in.
539 		 */
540 		if (cookie != CONFIGD_COOKIE) {
541 			ct_event_free(ev);
542 			continue;
543 		}
544 
545 		if (type == CT_PR_EV_EXIT) {
546 			int exitstatus;
547 
548 			(void) ct_pr_event_get_pid(ev, &pid);
549 			(void) ct_pr_event_get_exitstatus(ev,
550 			    &exitstatus);
551 
552 			if (st->st_configd_pid != pid) {
553 				/*
554 				 * This is the child exiting, so we
555 				 * abandon the contract and restart
556 				 * configd.
557 				 */
558 				contract_abandon(ctid);
559 				fork_configd(exitstatus);
560 			}
561 		}
562 
563 		efd = contract_open(ctid, "process", "ctl", O_WRONLY);
564 		if (efd != -1) {
565 			(void) ct_ctl_ack(efd, evid);
566 			startd_close(efd);
567 		}
568 
569 		ct_event_free(ev);
570 
571 	}
572 
573 	/*NOTREACHED*/
574 	return (NULL);
575 }
576 
577 void
578 fork_rc_script(char rl, const char *arg, boolean_t wait)
579 {
580 	pid_t pid;
581 	int tmpl, err, stat;
582 	char path[20] = "/sbin/rc.", log[20] = "rc..log", timebuf[20];
583 	time_t now;
584 	struct tm ltime;
585 	size_t sz;
586 	char *pathenv;
587 	char **nenv;
588 
589 	path[8] = rl;
590 
591 	tmpl = open64(CTFS_ROOT "/process/template", O_RDWR);
592 	if (tmpl >= 0) {
593 		err = ct_tmpl_set_critical(tmpl, 0);
594 		assert(err == 0);
595 
596 		err = ct_tmpl_set_informative(tmpl, 0);
597 		assert(err == 0);
598 
599 		err = ct_pr_tmpl_set_fatal(tmpl, 0);
600 		assert(err == 0);
601 
602 		err = ct_tmpl_activate(tmpl);
603 		assert(err == 0);
604 
605 		err = close(tmpl);
606 		assert(err == 0);
607 	} else {
608 		uu_warn("Could not create contract template for %s.\n", path);
609 	}
610 
611 	pid = startd_fork1(NULL);
612 	if (pid < 0) {
613 		return;
614 	} else if (pid != 0) {
615 		/* parent */
616 		if (wait) {
617 			do
618 				err = waitpid(pid, &stat, 0);
619 			while (err != 0 && errno == EINTR)
620 				;
621 
622 			if (!WIFEXITED(stat)) {
623 				log_framework(LOG_INFO,
624 				    "%s terminated with waitpid() status %d.\n",
625 				    path, stat);
626 			} else if (WEXITSTATUS(stat) != 0) {
627 				log_framework(LOG_INFO,
628 				    "%s failed with status %d.\n", path,
629 				    WEXITSTATUS(stat));
630 			}
631 		}
632 
633 		return;
634 	}
635 
636 	/* child */
637 
638 	log[2] = rl;
639 
640 	setlog(log);
641 
642 	now = time(NULL);
643 	sz = strftime(timebuf, sizeof (timebuf), "%b %e %T",
644 	    localtime_r(&now, &ltime));
645 	assert(sz != 0);
646 
647 	(void) fprintf(stderr, "%s Executing %s %s\n", timebuf, path, arg);
648 
649 	if (rl == 'S')
650 		pathenv = "PATH=/sbin:/usr/sbin:/usr/bin";
651 	else
652 		pathenv = "PATH=/usr/sbin:/usr/bin";
653 
654 	nenv = set_smf_env(NULL, 0, pathenv, NULL, NULL);
655 
656 	(void) execle(path, path, arg, 0, nenv);
657 
658 	perror("exec");
659 	exit(0);
660 }
661