xref: /illumos-gate/usr/src/cmd/init/init.c (revision 5c51f124)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*
41  * init(1M) is the general process spawning program.  Its primary job is to
42  * start and restart svc.startd for smf(5).  For backwards-compatibility it also
43  * spawns and respawns processes according to /etc/inittab and the current
44  * run-level.  It reads /etc/default/inittab for general configuration.
45  *
46  * To change run-levels the system administrator runs init from the command
47  * line with a level name.  init signals svc.startd via libscf and directs the
48  * zone's init (pid 1 in the global zone) what to do by sending it a signal;
49  * these signal numbers are commonly refered to in the code as 'states'.  Valid
50  * run-levels are [sS0123456].  Additionally, init can be given directives
51  * [qQabc], which indicate actions to be taken pertaining to /etc/inittab.
52  *
53  * When init processes inittab entries, it finds processes that are to be
54  * spawned at various run-levels.  inittab contains the set of the levels for
55  * which each inittab entry is valid.
56  *
57  * State File and Restartability
58  *   Premature exit by init(1M) is handled as a special case by the kernel:
59  *   init(1M) will be immediately re-executed, retaining its original PID.  (PID
60  *   1 in the global zone.)  To track the processes it has previously spawned,
61  *   as well as other mutable state, init(1M) regularly updates a state file
62  *   such that its subsequent invocations have knowledge of its various
63  *   dependent processes and duties.
64  *
65  * Process Contracts
66  *   We start svc.startd(1M) in a contract and transfer inherited contracts when
67  *   restarting it.  Everything else is started using the legacy contract
68  *   template, and the created contracts are abandoned when they become empty.
69  *
70  * utmpx Entry Handling
71  *   Because init(1M) no longer governs the startup process, its knowledge of
72  *   when utmpx becomes writable is indirect.  However, spawned processes
73  *   expect to be constructed with valid utmpx entries.  As a result, attempts
74  *   to write normal entries will be retried until successful.
75  *
76  * Maintenance Mode
77  *   In certain failure scenarios, init(1M) will enter a maintenance mode, in
78  *   which it invokes sulogin(1M) to allow the operator an opportunity to
79  *   repair the system.  Normally, this operation is performed as a
80  *   fork(2)-exec(2)-waitpid(3C) sequence with the parent waiting for repair or
81  *   diagnosis to be completed.  In the cases that fork(2) requests themselves
82  *   fail, init(1M) will directly execute sulogin(1M), and allow the kernel to
83  *   restart init(1M) on exit from the operator session.
84  *
85  *   One scenario where init(1M) enters its maintenance mode is when
86  *   svc.startd(1M) begins to fail rapidly, defined as when the average time
87  *   between recent failures drops below a given threshold.
88  */
89 
90 #include <sys/contract/process.h>
91 #include <sys/ctfs.h>
92 #include <sys/stat.h>
93 #include <sys/statvfs.h>
94 #include <sys/stropts.h>
95 #include <sys/systeminfo.h>
96 #include <sys/time.h>
97 #include <sys/termios.h>
98 #include <sys/tty.h>
99 #include <sys/types.h>
100 #include <sys/utsname.h>
101 
102 #include <bsm/adt_event.h>
103 #include <bsm/libbsm.h>
104 #include <security/pam_appl.h>
105 
106 #include <assert.h>
107 #include <ctype.h>
108 #include <dirent.h>
109 #include <errno.h>
110 #include <fcntl.h>
111 #include <libcontract.h>
112 #include <libcontract_priv.h>
113 #include <libintl.h>
114 #include <libscf.h>
115 #include <libscf_priv.h>
116 #include <poll.h>
117 #include <procfs.h>
118 #include <signal.h>
119 #include <stdarg.h>
120 #include <stdio.h>
121 #include <stdio_ext.h>
122 #include <stdlib.h>
123 #include <string.h>
124 #include <strings.h>
125 #include <syslog.h>
126 #include <time.h>
127 #include <ulimit.h>
128 #include <unistd.h>
129 #include <utmpx.h>
130 #include <wait.h>
131 #include <zone.h>
132 #include <ucontext.h>
133 
134 #undef	sleep
135 
136 #define	fioctl(p, sptr, cmd)	ioctl(fileno(p), sptr, cmd)
137 #define	min(a, b)		(((a) < (b)) ? (a) : (b))
138 
139 #define	TRUE	1
140 #define	FALSE	0
141 #define	FAILURE	-1
142 
143 #define	UT_LINE_SZ	32	/* Size of a utmpx ut_line field */
144 
145 /*
146  * SLEEPTIME	The number of seconds "init" sleeps between wakeups if
147  *		nothing else requires this "init" wakeup.
148  */
149 #define	SLEEPTIME	(5 * 60)
150 
151 /*
152  * MAXCMDL	The maximum length of a command string in inittab.
153  */
154 #define	MAXCMDL	512
155 
156 /*
157  * EXEC		The length of the prefix string added to all comamnds
158  *		found in inittab.
159  */
160 #define	EXEC	(sizeof ("exec ") - 1)
161 
162 /*
163  * TWARN	The amount of time between warning signal, SIGTERM,
164  *		and the fatal kill signal, SIGKILL.
165  */
166 #define	TWARN	5
167 
168 #define	id_eq(x, y)	((x[0] == y[0] && x[1] == y[1] && x[2] == y[2] &&\
169 			x[3] == y[3]) ? TRUE : FALSE)
170 
171 /*
172  * The kernel's default umask is 022 these days; since some processes inherit
173  * their umask from init, init will set it from CMASK in /etc/default/init.
174  * init gets the default umask from the kernel, it sets it to 022 whenever
175  * it wants to create a file and reverts to CMASK afterwards.
176  */
177 
178 static int cmask;
179 
180 /*
181  * The following definitions, concluding with the 'lvls' array, provide a
182  * common mapping between level-name (like 'S'), signal number (state),
183  * run-level mask, and specific properties associated with a run-level.
184  * This array should be accessed using the routines lvlname_to_state(),
185  * lvlname_to_mask(), state_to_mask(), and state_to_flags().
186  */
187 
188 /*
189  * Correspondence of signals to init actions.
190  */
191 #define	LVLQ		SIGHUP
192 #define	LVL0		SIGINT
193 #define	LVL1		SIGQUIT
194 #define	LVL2		SIGILL
195 #define	LVL3		SIGTRAP
196 #define	LVL4		SIGIOT
197 #define	LVL5		SIGEMT
198 #define	LVL6		SIGFPE
199 #define	SINGLE_USER	SIGBUS
200 #define	LVLa		SIGSEGV
201 #define	LVLb		SIGSYS
202 #define	LVLc		SIGPIPE
203 
204 /*
205  * Bit Mask for each level.  Used to determine legal levels.
206  */
207 #define	MASK0	0x0001
208 #define	MASK1	0x0002
209 #define	MASK2	0x0004
210 #define	MASK3	0x0008
211 #define	MASK4	0x0010
212 #define	MASK5	0x0020
213 #define	MASK6	0x0040
214 #define	MASKSU	0x0080
215 #define	MASKa	0x0100
216 #define	MASKb	0x0200
217 #define	MASKc	0x0400
218 
219 #define	MASK_NUMERIC (MASK0 | MASK1 | MASK2 | MASK3 | MASK4 | MASK5 | MASK6)
220 #define	MASK_abc (MASKa | MASKb | MASKc)
221 
222 /*
223  * Flags to indicate properties of various states.
224  */
225 #define	LSEL_RUNLEVEL	0x0001	/* runlevels you can transition to */
226 
227 typedef struct lvl {
228 	int	lvl_state;
229 	int	lvl_mask;
230 	char	lvl_name;
231 	int	lvl_flags;
232 } lvl_t;
233 
234 static lvl_t lvls[] = {
235 	{ LVLQ,		0,	'Q', 0					},
236 	{ LVLQ,		0,	'q', 0					},
237 	{ LVL0,		MASK0,	'0', LSEL_RUNLEVEL			},
238 	{ LVL1, 	MASK1,	'1', LSEL_RUNLEVEL			},
239 	{ LVL2, 	MASK2,	'2', LSEL_RUNLEVEL			},
240 	{ LVL3, 	MASK3,	'3', LSEL_RUNLEVEL			},
241 	{ LVL4, 	MASK4,	'4', LSEL_RUNLEVEL			},
242 	{ LVL5, 	MASK5,	'5', LSEL_RUNLEVEL			},
243 	{ LVL6, 	MASK6, 	'6', LSEL_RUNLEVEL			},
244 	{ SINGLE_USER, 	MASKSU, 'S', LSEL_RUNLEVEL			},
245 	{ SINGLE_USER, 	MASKSU, 's', LSEL_RUNLEVEL			},
246 	{ LVLa,		MASKa,	'a', 0					},
247 	{ LVLb,		MASKb,	'b', 0					},
248 	{ LVLc,		MASKc,	'c', 0					}
249 };
250 
251 #define	LVL_NELEMS (sizeof (lvls) / sizeof (lvl_t))
252 
253 /*
254  * Legal action field values.
255  */
256 #define	OFF		0	/* Kill process if on, else ignore */
257 #define	RESPAWN		1	/* Continuously restart process when it dies */
258 #define	ONDEMAND	RESPAWN	/* Respawn for a, b, c type processes */
259 #define	ONCE		2	/* Start process, do not respawn when dead */
260 #define	WAIT		3	/* Perform once and wait to complete */
261 #define	BOOT		4	/* Start at boot time only */
262 #define	BOOTWAIT	5	/* Start at boot time and wait to complete */
263 #define	POWERFAIL	6	/* Start on powerfail */
264 #define	POWERWAIT	7	/* Start and wait for complete on powerfail */
265 #define	INITDEFAULT	8	/* Default level "init" should start at */
266 #define	SYSINIT		9	/* Actions performed before init speaks */
267 
268 #define	M_OFF		0001
269 #define	M_RESPAWN	0002
270 #define	M_ONDEMAND	M_RESPAWN
271 #define	M_ONCE		0004
272 #define	M_WAIT		0010
273 #define	M_BOOT		0020
274 #define	M_BOOTWAIT	0040
275 #define	M_PF		0100
276 #define	M_PWAIT		0200
277 #define	M_INITDEFAULT	0400
278 #define	M_SYSINIT	01000
279 
280 /* States for the inittab parser in getcmd(). */
281 #define	ID	1
282 #define	LEVELS	2
283 #define	ACTION	3
284 #define	COMMAND	4
285 #define	COMMENT	5
286 
287 /*
288  * inittab entry id constants
289  */
290 #define	INITTAB_ENTRY_ID_SIZE 4
291 #define	INITTAB_ENTRY_ID_STR_FORMAT "%.4s"	/* if INITTAB_ENTRY_ID_SIZE */
292 						/* changes, this should */
293 						/* change accordingly */
294 
295 /*
296  * Init can be in any of three main states, "normal" mode where it is
297  * processing entries for the lines file in a normal fashion, "boot" mode,
298  * where it is only interested in the boot actions, and "powerfail" mode,
299  * where it is only interested in powerfail related actions. The following
300  * masks declare the legal actions for each mode.
301  */
302 #define	NORMAL_MODES	(M_OFF | M_RESPAWN | M_ONCE | M_WAIT)
303 #define	BOOT_MODES	(M_BOOT | M_BOOTWAIT)
304 #define	PF_MODES	(M_PF | M_PWAIT)
305 
306 struct PROC_TABLE {
307 	char	p_id[INITTAB_ENTRY_ID_SIZE];	/* Four letter unique id of */
308 						/* process */
309 	pid_t	p_pid;		/* Process id */
310 	short	p_count;	/* How many respawns of this command in */
311 				/*   the current series */
312 	long	p_time;		/* Start time for a series of respawns */
313 	short	p_flags;
314 	short	p_exit;		/* Exit status of a process which died */
315 };
316 
317 /*
318  * Flags for the "p_flags" word of a PROC_TABLE entry:
319  *
320  *	OCCUPIED	This slot in init's proc table is in use.
321  *
322  *	LIVING		Process is alive.
323  *
324  *	NOCLEANUP	efork() is not allowed to cleanup this entry even
325  *			if process is dead.
326  *
327  *	NAMED		This process has a name, i.e. came from inittab.
328  *
329  *	DEMANDREQUEST	Process started by a "telinit [abc]" command.  Processes
330  *			formed this way are respawnable and immune to level
331  *			changes as long as their entry exists in inittab.
332  *
333  *	TOUCHED		Flag used by remv() to determine whether it has looked
334  *			at an entry while checking for processes to be killed.
335  *
336  *	WARNED		Flag used by remv() to mark processes that have been
337  *			sent the SIGTERM signal.  If they don't die in 5
338  *			seconds, they are sent the SIGKILL signal.
339  *
340  *	KILLED		Flag used by remv() to mark procs that have been sent
341  *			the SIGTERM and SIGKILL signals.
342  *
343  *	PF_MASK		Bitwise or of legal flags, for sanity checking.
344  */
345 #define	OCCUPIED	01
346 #define	LIVING		02
347 #define	NOCLEANUP	04
348 #define	NAMED		010
349 #define	DEMANDREQUEST	020
350 #define	TOUCHED		040
351 #define	WARNED		0100
352 #define	KILLED		0200
353 #define	PF_MASK		0377
354 
355 /*
356  * Respawn limits for processes that are to be respawned:
357  *
358  *	SPAWN_INTERVAL	The number of seconds over which "init" will try to
359  *			respawn a process SPAWN_LIMIT times before it gets mad.
360  *
361  *	SPAWN_LIMIT	The number of respawns "init" will attempt in
362  *			SPAWN_INTERVAL seconds before it generates an
363  *			error message and inhibits further tries for
364  *			INHIBIT seconds.
365  *
366  *	INHIBIT		The number of seconds "init" ignores an entry it had
367  *			trouble spawning unless a "telinit Q" is received.
368  */
369 
370 #define	SPAWN_INTERVAL	(2*60)
371 #define	SPAWN_LIMIT	10
372 #define	INHIBIT		(5*60)
373 
374 /*
375  * The maximum number of decimal digits for an id_t.  (ceil(log10 (max_id)))
376  */
377 #define	ID_MAX_STR_LEN	10
378 
379 #define	NULLPROC	((struct PROC_TABLE *)(0))
380 #define	NO_ROOM		((struct PROC_TABLE *)(FAILURE))
381 
382 struct CMD_LINE {
383 	char c_id[INITTAB_ENTRY_ID_SIZE];	/* Four letter unique id of */
384 						/* process to be affected by */
385 						/* action */
386 	short c_levels;	/* Mask of legal levels for process */
387 	short c_action;	/* Mask for type of action required */
388 	char *c_command; /* Pointer to init command */
389 };
390 
391 struct	pidrec {
392 	int	pd_type;	/* Command type */
393 	pid_t	pd_pid;		/* pid to add or remove */
394 };
395 
396 /*
397  * pd_type's
398  */
399 #define	ADDPID	1
400 #define	REMPID	2
401 
402 static struct	pidlist {
403 	pid_t	pl_pid;		/* pid to watch for */
404 	int	pl_dflag;	/* Flag indicating SIGCLD from this pid */
405 	short	pl_exit;	/* Exit status of proc */
406 	struct	pidlist	*pl_next; /* Next in list */
407 } *Plhead, *Plfree;
408 
409 /*
410  * The following structure contains a set of modes for /dev/syscon
411  * and should match the default contents of /etc/ioctl.syscon.  It should also
412  * be kept in-sync with base_termios in uts/common/io/ttcompat.c.
413  */
414 static struct termios	dflt_termios = {
415 	BRKINT|ICRNL|IXON|IMAXBEL,			/* iflag */
416 	OPOST|ONLCR|TAB3,				/* oflag */
417 	CS8|CREAD|B9600,				/* cflag */
418 	ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN, /* lflag */
419 	CINTR, CQUIT, CERASE, CKILL, CEOF, 0, 0, 0,
420 	0, 0, 0, 0, 0, 0, 0, 0,
421 	0, 0, 0
422 };
423 
424 static struct termios	stored_syscon_termios;
425 static int		write_ioctl = 0;	/* Rewrite /etc/ioctl.syscon */
426 
427 static union WAKEUP {
428 	struct WAKEFLAGS {
429 		unsigned w_usersignal : 1;	/* User sent signal to "init" */
430 		unsigned w_childdeath : 1;	/* An "init" child died */
431 		unsigned w_powerhit : 1;	/* OS experienced powerfail */
432 	}	w_flags;
433 	int w_mask;
434 } wakeup;
435 
436 
437 struct init_state {
438 	int			ist_runlevel;
439 	int			ist_num_proc;
440 	int			ist_utmpx_ok;
441 	struct PROC_TABLE	ist_proc_table[1];
442 };
443 
444 #define	cur_state	(g_state->ist_runlevel)
445 #define	num_proc	(g_state->ist_num_proc)
446 #define	proc_table	(g_state->ist_proc_table)
447 #define	utmpx_ok	(g_state->ist_utmpx_ok)
448 
449 /* Contract cookies. */
450 #define	ORDINARY_COOKIE		0
451 #define	STARTD_COOKIE		1
452 
453 
454 #ifndef NDEBUG
455 #define	bad_error(func, err)	{					\
456 	(void) fprintf(stderr, "%s:%d: %s() failed with unexpected "	\
457 	    "error %d.  Aborting.\n", __FILE__, __LINE__, (func), (err)); \
458 	abort();							\
459 }
460 #else
461 #define	bad_error(func, err)	abort()
462 #endif
463 
464 
465 /*
466  * Useful file and device names.
467  */
468 static char *CONSOLE	  = "/dev/console";	/* Real system console */
469 static char *INITPIPE_DIR = "/var/run";
470 static char *INITPIPE	  = "/var/run/initpipe";
471 
472 #define	INIT_STATE_DIR "/etc/svc/volatile"
473 static const char * const init_state_file = INIT_STATE_DIR "/init.state";
474 static const char * const init_next_state_file =
475 	INIT_STATE_DIR "/init-next.state";
476 
477 static const int init_num_proc = 20;	/* Initial size of process table. */
478 
479 static char *UTMPX	 = UTMPX_FILE;		/* Snapshot record file */
480 static char *WTMPX	 = WTMPX_FILE;		/* Long term record file */
481 static char *INITTAB	 = "/etc/inittab";	/* Script file for "init" */
482 static char *SYSTTY	 = "/dev/systty";	/* System Console */
483 static char *SYSCON	 = "/dev/syscon";	/* Virtual System console */
484 static char *IOCTLSYSCON = "/etc/ioctl.syscon";	/* Last syscon modes */
485 static char *ENVFILE	 = "/etc/default/init";	/* Default env. */
486 static char *SU	= "/etc/sulogin";	/* Super-user program for single user */
487 static char *SH	= "/sbin/sh";		/* Standard shell */
488 
489 /*
490  * Default Path.  /sbin is included in path only during sysinit phase
491  */
492 #define	DEF_PATH	"PATH=/usr/sbin:/usr/bin"
493 #define	INIT_PATH	"PATH=/sbin:/usr/sbin:/usr/bin"
494 
495 static int	prior_state;
496 static int	prev_state;	/* State "init" was in last time it woke */
497 static int	new_state;	/* State user wants "init" to go to. */
498 static int	lvlq_received;	/* Explicit request to examine state */
499 static int	op_modes = BOOT_MODES; /* Current state of "init" */
500 static int	Gchild = 0;	/* Flag to indicate "godchild" died, set in */
501 				/*   childeath() and cleared in cleanaux() */
502 static int	Pfd = -1;	/* fd to receive pids thru */
503 static unsigned int	spawncnt, pausecnt;
504 static int	rsflag;		/* Set if a respawn has taken place */
505 static volatile int time_up;	/* Flag set to TRUE by the alarm interrupt */
506 				/* routine each time an alarm interrupt */
507 				/* takes place. */
508 static int	sflg = 0;	/* Set if we were booted -s to single user */
509 static int	rflg = 0;	/* Set if booted -r, reconfigure devices */
510 static int	bflg = 0;	/* Set if booted -b, don't run rc scripts */
511 static pid_t	init_pid;	/* PID of "one true" init for current zone */
512 
513 static struct init_state *g_state = NULL;
514 static size_t	g_state_sz;
515 static int	booting = 1;	/* Set while we're booting. */
516 
517 /*
518  * Array for default global environment.
519  */
520 #define	MAXENVENT	24	/* Max number of default env variables + 1 */
521 				/* init can use three itself, so this leaves */
522 				/* 20 for the administrator in ENVFILE. */
523 static char	*glob_envp[MAXENVENT];	/* Array of environment strings */
524 static int	glob_envn;		/* Number of environment strings */
525 
526 
527 static struct pollfd	poll_fds[1];
528 static int		poll_nfds = 0;	/* poll_fds is uninitialized */
529 
530 /*
531  * Contracts constants
532  */
533 #define	SVC_INIT_PREFIX "init:/"
534 #define	SVC_AUX_SIZE (INITTAB_ENTRY_ID_SIZE + 1)
535 #define	SVC_FMRI_SIZE (sizeof (SVC_INIT_PREFIX) + INITTAB_ENTRY_ID_SIZE)
536 
537 static int	legacy_tmpl = -1;	/* fd for legacy contract template */
538 static int	startd_tmpl = -1;	/* fd for svc.startd's template */
539 static char	startd_svc_aux[SVC_AUX_SIZE];
540 
541 static char	startd_cline[256] = "";	/* svc.startd's command line */
542 static int	do_restart_startd = 1;	/* Whether to restart svc.startd. */
543 static char	*smf_options = NULL;	/* Options to give to startd. */
544 static int	smf_debug = 0;		/* Messages for debugging smf(5) */
545 static time_t	init_boot_time;		/* Substitute for kernel boot time. */
546 
547 #define	NSTARTD_FAILURE_TIMES	3		/* trigger after 3 failures */
548 #define	STARTD_FAILURE_RATE_NS	5000000000LL	/* 1 failure/5 seconds */
549 
550 static hrtime_t	startd_failure_time[NSTARTD_FAILURE_TIMES];
551 static uint_t	startd_failure_index;
552 
553 
554 static char	*prog_name(char *);
555 static int	state_to_mask(int);
556 static int	lvlname_to_mask(char, int *);
557 static void	lscf_set_runlevel(char);
558 static int	state_to_flags(int);
559 static char	state_to_name(int);
560 static int	lvlname_to_state(char);
561 static int	getcmd(struct CMD_LINE *, char *);
562 static int	realcon();
563 static int	spawn_processes();
564 static int	get_ioctl_syscon();
565 static int	account(short, struct PROC_TABLE *, char *);
566 static void	alarmclk();
567 static void	childeath(int);
568 static void	cleanaux();
569 static void	clearent(pid_t, short);
570 static void	console(boolean_t, char *, ...);
571 static void	init_signals(void);
572 static void	setup_pipe();
573 static void	killproc(pid_t);
574 static void	init_env();
575 static void	boot_init();
576 static void	powerfail();
577 static void	remv();
578 static void	write_ioctl_syscon();
579 static void	spawn(struct PROC_TABLE *, struct CMD_LINE *);
580 static void	setimer(int);
581 static void	siglvl(int, siginfo_t *, ucontext_t *);
582 static void	sigpoll(int);
583 static void	enter_maintenance(void);
584 static void	timer(int);
585 static void	userinit(int, char **);
586 static void	notify_pam_dead(struct utmpx *);
587 static long	waitproc(struct PROC_TABLE *);
588 static struct PROC_TABLE *efork(int, struct PROC_TABLE *, int);
589 static struct PROC_TABLE *findpslot(struct CMD_LINE *);
590 static void	increase_proc_table_size();
591 static void	st_init();
592 static void	st_write();
593 static void	contracts_init();
594 static void	contract_event(struct pollfd *);
595 static int	startd_run(const char *, int, ctid_t);
596 static void	startd_record_failure();
597 static int	startd_failure_rate_critical();
598 static char	*audit_boot_msg();
599 static int	audit_put_record(int, int, char *);
600 static void	update_boot_archive(int new_state);
601 
602 int
603 main(int argc, char *argv[])
604 {
605 	int	chg_lvl_flag = FALSE, print_banner = FALSE;
606 	int	may_need_audit = 1;
607 	int	c;
608 	char	*msg;
609 
610 	/* Get a timestamp for use as boot time, if needed. */
611 	(void) time(&init_boot_time);
612 
613 	/* Get the default umask */
614 	cmask = umask(022);
615 	(void) umask(cmask);
616 
617 	/* Parse the arguments to init. Check for single user */
618 	opterr = 0;
619 	while ((c = getopt(argc, argv, "brsm:")) != EOF) {
620 		switch (c) {
621 		case 'b':
622 			rflg = 0;
623 			bflg = 1;
624 			if (!sflg)
625 				sflg++;
626 			break;
627 		case 'r':
628 			bflg = 0;
629 			rflg++;
630 			break;
631 		case 's':
632 			if (!bflg)
633 				sflg++;
634 			break;
635 		case 'm':
636 			smf_options = optarg;
637 			smf_debug = (strstr(smf_options, "debug") != NULL);
638 			break;
639 		}
640 	}
641 
642 	/*
643 	 * Determine if we are the main init, or a user invoked init, whose job
644 	 * it is to inform init to change levels or perform some other action.
645 	 */
646 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
647 	    sizeof (init_pid)) != sizeof (init_pid)) {
648 		(void) fprintf(stderr, "could not get pid for init\n");
649 		return (1);
650 	}
651 
652 	/*
653 	 * If this PID is not the same as the "true" init for the zone, then we
654 	 * must be in 'user' mode.
655 	 */
656 	if (getpid() != init_pid) {
657 		userinit(argc, argv);
658 	}
659 
660 	if (getzoneid() != GLOBAL_ZONEID) {
661 		print_banner = TRUE;
662 	}
663 
664 	/*
665 	 * Initialize state (and set "booting").
666 	 */
667 	st_init();
668 
669 	if (booting && print_banner) {
670 		struct utsname un;
671 		char buf[BUFSIZ], *isa;
672 		long ret;
673 		int bits = 32;
674 
675 		/*
676 		 * We want to print the boot banner as soon as
677 		 * possible.  In the global zone, the kernel does it,
678 		 * but we do not have that luxury in non-global zones,
679 		 * so we will print it here.
680 		 */
681 		(void) uname(&un);
682 		ret = sysinfo(SI_ISALIST, buf, sizeof (buf));
683 		if (ret != -1L && ret <= sizeof (buf)) {
684 			for (isa = strtok(buf, " "); isa;
685 			    isa = strtok(NULL, " ")) {
686 				if (strcmp(isa, "sparcv9") == 0 ||
687 				    strcmp(isa, "amd64") == 0) {
688 					bits = 64;
689 					break;
690 				}
691 			}
692 		}
693 
694 		console(B_FALSE,
695 		    "\n\n%s Release %s Version %s %d-bit\r\n",
696 		    un.sysname, un.release, un.version, bits);
697 		console(B_FALSE,
698 		    "Copyright 1983-2009 Sun Microsystems, Inc. "
699 		    " All rights reserved.\r\n");
700 		console(B_FALSE,
701 		    "Use is subject to license terms.\r\n");
702 	}
703 
704 	/*
705 	 * Get the ioctl settings for /dev/syscon from /etc/ioctl.syscon
706 	 * so that it can be brought up in the state it was in when the
707 	 * system went down; or set to defaults if ioctl.syscon isn't
708 	 * valid.
709 	 *
710 	 * This needs to be done even if we're restarting so reset_modes()
711 	 * will work in case we need to go down to single user mode.
712 	 */
713 	write_ioctl = get_ioctl_syscon();
714 
715 	/*
716 	 * Set up all signals to be caught or ignored as appropriate.
717 	 */
718 	init_signals();
719 
720 	/* Load glob_envp from ENVFILE. */
721 	init_env();
722 
723 	contracts_init();
724 
725 	if (!booting) {
726 		/* cur_state should have been read in. */
727 
728 		op_modes = NORMAL_MODES;
729 
730 		/* Rewrite the ioctl file if it was bad. */
731 		if (write_ioctl)
732 			write_ioctl_syscon();
733 	} else {
734 		/*
735 		 * It's fine to boot up with state as zero, because
736 		 * startd will later tell us the real state.
737 		 */
738 		cur_state = 0;
739 		op_modes = BOOT_MODES;
740 
741 		boot_init();
742 	}
743 
744 	prev_state = prior_state = cur_state;
745 
746 	setup_pipe();
747 
748 	/*
749 	 * Here is the beginning of the main process loop.
750 	 */
751 	for (;;) {
752 		if (lvlq_received) {
753 			setup_pipe();
754 			lvlq_received = B_FALSE;
755 		}
756 
757 		/*
758 		 * Clean up any accounting records for dead "godchildren".
759 		 */
760 		if (Gchild)
761 			cleanaux();
762 
763 		/*
764 		 * If in "normal" mode, check all living processes and initiate
765 		 * kill sequence on those that should not be there anymore.
766 		 */
767 		if (op_modes == NORMAL_MODES && cur_state != LVLa &&
768 		    cur_state != LVLb && cur_state != LVLc)
769 			remv();
770 
771 		/*
772 		 * If a change in run levels is the reason we awoke, now do
773 		 * the accounting to report the change in the utmp file.
774 		 * Also report the change on the system console.
775 		 */
776 		if (chg_lvl_flag) {
777 			chg_lvl_flag = FALSE;
778 
779 			if (state_to_flags(cur_state) & LSEL_RUNLEVEL) {
780 				char rl = state_to_name(cur_state);
781 
782 				if (rl != -1)
783 					lscf_set_runlevel(rl);
784 			}
785 
786 			may_need_audit = 1;
787 		}
788 
789 		/*
790 		 * Scan the inittab file and spawn and respawn processes that
791 		 * should be alive in the current state. If inittab does not
792 		 * exist default to  single user mode.
793 		 */
794 		if (spawn_processes() == FAILURE) {
795 			prior_state = prev_state;
796 			cur_state = SINGLE_USER;
797 		}
798 
799 		/* If any respawns occurred, take note. */
800 		if (rsflag) {
801 			rsflag = 0;
802 			spawncnt++;
803 		}
804 
805 		/*
806 		 * If a powerfail signal was received during the last
807 		 * sequence, set mode to powerfail.  When spawn_processes() is
808 		 * entered the first thing it does is to check "powerhit".  If
809 		 * it is in PF_MODES then it clears "powerhit" and does
810 		 * a powerfail sequence.  If it is not in PF_MODES, then it
811 		 * puts itself in PF_MODES and then clears "powerhit".  Should
812 		 * "powerhit" get set again while spawn_processes() is working
813 		 * on a powerfail sequence, the following code  will see that
814 		 * spawn_processes() tries to execute the powerfail sequence
815 		 * again.  This guarantees that the powerfail sequence will be
816 		 * successfully completed before further processing takes
817 		 * place.
818 		 */
819 		if (wakeup.w_flags.w_powerhit) {
820 			op_modes = PF_MODES;
821 			/*
822 			 * Make sure that cur_state != prev_state so that
823 			 * ONCE and WAIT types work.
824 			 */
825 			prev_state = 0;
826 		} else if (op_modes != NORMAL_MODES) {
827 			/*
828 			 * If spawn_processes() was not just called while in
829 			 * normal mode, we set the mode to normal and it will
830 			 * be called again to check normal modes.  If we have
831 			 * just finished a powerfail sequence with prev_state
832 			 * equal to zero, we set prev_state equal to cur_state
833 			 * before the next pass through.
834 			 */
835 			if (op_modes == PF_MODES)
836 				prev_state = cur_state;
837 			op_modes = NORMAL_MODES;
838 		} else if (cur_state == LVLa || cur_state == LVLb ||
839 		    cur_state == LVLc) {
840 			/*
841 			 * If it was a change of levels that awakened us and the
842 			 * new level is one of the demand levels then reset
843 			 * cur_state to the previous state and do another scan
844 			 * to take care of the usual respawn actions.
845 			 */
846 			cur_state = prior_state;
847 			prior_state = prev_state;
848 			prev_state = cur_state;
849 		} else {
850 			prev_state = cur_state;
851 
852 			if (wakeup.w_mask == 0) {
853 				int ret;
854 
855 				if (may_need_audit && (cur_state == LVL3)) {
856 					msg = audit_boot_msg();
857 
858 					may_need_audit = 0;
859 					(void) audit_put_record(ADT_SUCCESS,
860 					    ADT_SUCCESS, msg);
861 					free(msg);
862 				}
863 
864 				/*
865 				 * "init" is finished with all actions for
866 				 * the current wakeup.
867 				 */
868 				ret = poll(poll_fds, poll_nfds,
869 				    SLEEPTIME * MILLISEC);
870 				pausecnt++;
871 				if (ret > 0)
872 					contract_event(&poll_fds[0]);
873 				else if (ret < 0 && errno != EINTR)
874 					console(B_TRUE, "poll() error: %s\n",
875 					    strerror(errno));
876 			}
877 
878 			if (wakeup.w_flags.w_usersignal) {
879 				/*
880 				 * Install the new level.  This could be a real
881 				 * change in levels  or a telinit [Q|a|b|c] or
882 				 * just a telinit to the same level at which
883 				 * we are running.
884 				 */
885 				if (new_state != cur_state) {
886 					if (new_state == LVLa ||
887 					    new_state == LVLb ||
888 					    new_state == LVLc) {
889 						prev_state = prior_state;
890 						prior_state = cur_state;
891 						cur_state = new_state;
892 					} else {
893 						prev_state = cur_state;
894 						if (cur_state >= 0)
895 							prior_state = cur_state;
896 						cur_state = new_state;
897 						chg_lvl_flag = TRUE;
898 					}
899 				}
900 
901 				new_state = 0;
902 			}
903 
904 			if (wakeup.w_flags.w_powerhit)
905 				op_modes = PF_MODES;
906 
907 			/*
908 			 * Clear all wakeup reasons.
909 			 */
910 			wakeup.w_mask = 0;
911 		}
912 	}
913 
914 	/*NOTREACHED*/
915 }
916 
917 static void
918 update_boot_archive(int new_state)
919 {
920 	if (new_state != LVL0 && new_state != LVL5 && new_state != LVL6)
921 		return;
922 
923 	if (getzoneid() != GLOBAL_ZONEID)
924 		return;
925 
926 	(void) system("/sbin/bootadm -ea update_all");
927 }
928 
929 /*
930  * void enter_maintenance()
931  *   A simple invocation of sulogin(1M), with no baggage, in the case that we
932  *   are unable to activate svc.startd(1M).  We fork; the child runs sulogin;
933  *   we wait for it to exit.
934  */
935 static void
936 enter_maintenance()
937 {
938 	struct PROC_TABLE	*su_process;
939 
940 	console(B_FALSE, "Requesting maintenance mode\n"
941 	    "(See /lib/svc/share/README for additional information.)\n");
942 	(void) sigset(SIGCLD, SIG_DFL);
943 	while ((su_process = efork(M_OFF, NULLPROC, NOCLEANUP)) == NO_ROOM)
944 		(void) pause();
945 	(void) sigset(SIGCLD, childeath);
946 	if (su_process == NULLPROC) {
947 		int fd;
948 
949 		(void) fclose(stdin);
950 		(void) fclose(stdout);
951 		(void) fclose(stderr);
952 		closefrom(0);
953 
954 		fd = open(SYSCON, O_RDWR | O_NOCTTY);
955 		if (fd >= 0) {
956 			(void) dup2(fd, 1);
957 			(void) dup2(fd, 2);
958 		} else {
959 			/*
960 			 * Need to issue an error message somewhere.
961 			 */
962 			syslog(LOG_CRIT, "init[%d]: cannot open %s; %s\n",
963 			    getpid(), SYSCON, strerror(errno));
964 		}
965 
966 		/*
967 		 * Execute the "su" program.
968 		 */
969 		(void) execle(SU, SU, "-", (char *)0, glob_envp);
970 		console(B_TRUE, "execle of %s failed: %s\n", SU,
971 		    strerror(errno));
972 		timer(5);
973 		exit(1);
974 	}
975 
976 	/*
977 	 * If we are the parent, wait around for the child to die
978 	 * or for "init" to be signaled to change levels.
979 	 */
980 	while (waitproc(su_process) == FAILURE) {
981 		/*
982 		 * All other reasons for waking are ignored when in
983 		 * single-user mode.  The only child we are interested
984 		 * in is being waited for explicitly by waitproc().
985 		 */
986 		wakeup.w_mask = 0;
987 	}
988 }
989 
990 /*
991  * remv() scans through "proc_table" and performs cleanup.  If
992  * there is a process in the table, which shouldn't be here at
993  * the current run level, then remv() kills the process.
994  */
995 static void
996 remv()
997 {
998 	struct PROC_TABLE	*process;
999 	struct CMD_LINE		cmd;
1000 	char			cmd_string[MAXCMDL];
1001 	int			change_level;
1002 
1003 	change_level = (cur_state != prev_state ? TRUE : FALSE);
1004 
1005 	/*
1006 	 * Clear the TOUCHED flag on all entries so that when we have
1007 	 * finished scanning inittab, we will be able to tell if we
1008 	 * have any processes for which there is no entry in inittab.
1009 	 */
1010 	for (process = proc_table;
1011 	    (process < proc_table + num_proc); process++) {
1012 		process->p_flags &= ~TOUCHED;
1013 	}
1014 
1015 	/*
1016 	 * Scan all inittab entries.
1017 	 */
1018 	while (getcmd(&cmd, &cmd_string[0]) == TRUE) {
1019 		/* Scan for process which goes with this entry in inittab. */
1020 		for (process = proc_table;
1021 		    (process < proc_table + num_proc); process++) {
1022 			if ((process->p_flags & OCCUPIED) == 0 ||
1023 			    !id_eq(process->p_id, cmd.c_id))
1024 				continue;
1025 
1026 			/*
1027 			 * This slot contains the process we are looking for.
1028 			 */
1029 
1030 			/*
1031 			 * Is the cur_state SINGLE_USER or is this process
1032 			 * marked as "off" or was this proc started by some
1033 			 * mechanism other than LVL{a|b|c} and the current level
1034 			 * does not support this process?
1035 			 */
1036 			if (cur_state == SINGLE_USER ||
1037 			    cmd.c_action == M_OFF ||
1038 			    ((cmd.c_levels & state_to_mask(cur_state)) == 0 &&
1039 			    (process->p_flags & DEMANDREQUEST) == 0)) {
1040 				if (process->p_flags & LIVING) {
1041 					/*
1042 					 * Touch this entry so we know we have
1043 					 * treated it.  Note that procs which
1044 					 * are already dead at this point and
1045 					 * should not be restarted are left
1046 					 * untouched.  This causes their slot to
1047 					 * be freed later after dead accounting
1048 					 * is done.
1049 					 */
1050 					process->p_flags |= TOUCHED;
1051 
1052 					if ((process->p_flags & KILLED) == 0) {
1053 						if (change_level) {
1054 							process->p_flags
1055 							    |= WARNED;
1056 							(void) kill(
1057 							    process->p_pid,
1058 							    SIGTERM);
1059 						} else {
1060 							/*
1061 							 * Fork a killing proc
1062 							 * so "init" can
1063 							 * continue without
1064 							 * having to pause for
1065 							 * TWARN seconds.
1066 							 */
1067 							killproc(
1068 							    process->p_pid);
1069 						}
1070 						process->p_flags |= KILLED;
1071 					}
1072 				}
1073 			} else {
1074 				/*
1075 				 * Process can exist at current level.  If it is
1076 				 * still alive or a DEMANDREQUEST we touch it so
1077 				 * it will be left alone.  Otherwise we leave it
1078 				 * untouched so it will be accounted for and
1079 				 * cleaned up later in remv().  Dead
1080 				 * DEMANDREQUESTs will be accounted but not
1081 				 * freed.
1082 				 */
1083 				if (process->p_flags &
1084 				    (LIVING|NOCLEANUP|DEMANDREQUEST))
1085 					process->p_flags |= TOUCHED;
1086 			}
1087 
1088 			break;
1089 		}
1090 	}
1091 
1092 	st_write();
1093 
1094 	/*
1095 	 * If this was a change of levels call, scan through the
1096 	 * process table for processes that were warned to die.  If any
1097 	 * are found that haven't left yet, sleep for TWARN seconds and
1098 	 * then send final terminations to any that haven't died yet.
1099 	 */
1100 	if (change_level) {
1101 
1102 		/*
1103 		 * Set the alarm for TWARN seconds on the assumption
1104 		 * that there will be some that need to be waited for.
1105 		 * This won't harm anything except we are guaranteed to
1106 		 * wakeup in TWARN seconds whether we need to or not.
1107 		 */
1108 		setimer(TWARN);
1109 
1110 		/*
1111 		 * Scan for processes which should be dying.  We hope they
1112 		 * will die without having to be sent a SIGKILL signal.
1113 		 */
1114 		for (process = proc_table;
1115 		    (process < proc_table + num_proc); process++) {
1116 			/*
1117 			 * If this process should die, hasn't yet, and the
1118 			 * TWARN time hasn't expired yet, wait for process
1119 			 * to die or for timer to expire.
1120 			 */
1121 			while (time_up == FALSE &&
1122 			    (process->p_flags & (WARNED|LIVING|OCCUPIED)) ==
1123 			    (WARNED|LIVING|OCCUPIED))
1124 				(void) pause();
1125 
1126 			if (time_up == TRUE)
1127 				break;
1128 		}
1129 
1130 		/*
1131 		 * If we reached the end of the table without the timer
1132 		 * expiring, then there are no procs which will have to be
1133 		 * sent the SIGKILL signal.  If the timer has expired, then
1134 		 * it is necessary to scan the table again and send signals
1135 		 * to all processes which aren't going away nicely.
1136 		 */
1137 		if (time_up == TRUE) {
1138 			for (process = proc_table;
1139 			    (process < proc_table + num_proc); process++) {
1140 				if ((process->p_flags &
1141 				    (WARNED|LIVING|OCCUPIED)) ==
1142 				    (WARNED|LIVING|OCCUPIED))
1143 					(void) kill(process->p_pid, SIGKILL);
1144 			}
1145 		}
1146 		setimer(0);
1147 	}
1148 
1149 	/*
1150 	 * Rescan the proc_table for two kinds of entry, those marked LIVING,
1151 	 * NAMED, which don't have an entry in inittab (haven't been TOUCHED
1152 	 * by the above scanning), and haven't been sent kill signals, and
1153 	 * those entries marked not LIVING, NAMED.  The former procs are killed.
1154 	 * The latter have DEAD_PROCESS accounting done and the slot cleared.
1155 	 */
1156 	for (process = proc_table;
1157 	    (process < proc_table + num_proc); process++) {
1158 		if ((process->p_flags & (LIVING|NAMED|TOUCHED|KILLED|OCCUPIED))
1159 		    == (LIVING|NAMED|OCCUPIED)) {
1160 			killproc(process->p_pid);
1161 			process->p_flags |= KILLED;
1162 		} else if ((process->p_flags & (LIVING|NAMED|OCCUPIED)) ==
1163 		    (NAMED|OCCUPIED)) {
1164 			(void) account(DEAD_PROCESS, process, NULL);
1165 			/*
1166 			 * If this named proc hasn't been TOUCHED, then free the
1167 			 * space. It has either died of it's own accord, but
1168 			 * isn't respawnable or it was killed because it
1169 			 * shouldn't exist at this level.
1170 			 */
1171 			if ((process->p_flags & TOUCHED) == 0)
1172 				process->p_flags = 0;
1173 		}
1174 	}
1175 
1176 	st_write();
1177 }
1178 
1179 /*
1180  * Extract the svc.startd command line and whether to restart it from its
1181  * inittab entry.
1182  */
1183 /*ARGSUSED*/
1184 static void
1185 process_startd_line(struct CMD_LINE *cmd, char *cmd_string)
1186 {
1187 	size_t sz;
1188 
1189 	/* Save the command line. */
1190 	if (sflg || rflg) {
1191 		/* Also append -r or -s. */
1192 		(void) strlcpy(startd_cline, cmd_string, sizeof (startd_cline));
1193 		(void) strlcat(startd_cline, " -", sizeof (startd_cline));
1194 		if (sflg)
1195 			sz = strlcat(startd_cline, "s", sizeof (startd_cline));
1196 		if (rflg)
1197 			sz = strlcat(startd_cline, "r", sizeof (startd_cline));
1198 	} else {
1199 		sz = strlcpy(startd_cline, cmd_string, sizeof (startd_cline));
1200 	}
1201 
1202 	if (sz >= sizeof (startd_cline)) {
1203 		console(B_TRUE,
1204 		    "svc.startd command line too long.  Ignoring.\n");
1205 		startd_cline[0] = '\0';
1206 		return;
1207 	}
1208 }
1209 
1210 /*
1211  * spawn_processes() scans inittab for entries which should be run at this
1212  * mode.  Processes which should be running but are not, are started.
1213  */
1214 static int
1215 spawn_processes()
1216 {
1217 	struct PROC_TABLE		*pp;
1218 	struct CMD_LINE			cmd;
1219 	char				cmd_string[MAXCMDL];
1220 	short				lvl_mask;
1221 	int				status;
1222 
1223 	/*
1224 	 * First check the "powerhit" flag.  If it is set, make sure the modes
1225 	 * are PF_MODES and clear the "powerhit" flag.  Avoid the possible race
1226 	 * on the "powerhit" flag by disallowing a new powerfail interrupt
1227 	 * between the test of the powerhit flag and the clearing of it.
1228 	 */
1229 	if (wakeup.w_flags.w_powerhit) {
1230 		wakeup.w_flags.w_powerhit = 0;
1231 		op_modes = PF_MODES;
1232 	}
1233 	lvl_mask = state_to_mask(cur_state);
1234 
1235 	/*
1236 	 * Scan through all the entries in inittab.
1237 	 */
1238 	while ((status = getcmd(&cmd, &cmd_string[0])) == TRUE) {
1239 		if (id_eq(cmd.c_id, "smf")) {
1240 			process_startd_line(&cmd, cmd_string);
1241 			continue;
1242 		}
1243 
1244 retry_for_proc_slot:
1245 
1246 		/*
1247 		 * Find out if there is a process slot for this entry already.
1248 		 */
1249 		if ((pp = findpslot(&cmd)) == NULLPROC) {
1250 			/*
1251 			 * we've run out of proc table entries
1252 			 * increase proc_table.
1253 			 */
1254 			increase_proc_table_size();
1255 
1256 			/*
1257 			 * Retry now as we have an empty proc slot.
1258 			 * In case increase_proc_table_size() fails,
1259 			 * we will keep retrying.
1260 			 */
1261 			goto retry_for_proc_slot;
1262 		}
1263 
1264 		/*
1265 		 * If there is an entry, and it is marked as DEMANDREQUEST,
1266 		 * one of the levels a, b, or c is in its levels mask, and
1267 		 * the action field is ONDEMAND and ONDEMAND is a permissable
1268 		 * mode, and the process is dead, then respawn it.
1269 		 */
1270 		if (((pp->p_flags & (LIVING|DEMANDREQUEST)) == DEMANDREQUEST) &&
1271 		    (cmd.c_levels & MASK_abc) &&
1272 		    (cmd.c_action & op_modes) == M_ONDEMAND) {
1273 			spawn(pp, &cmd);
1274 			continue;
1275 		}
1276 
1277 		/*
1278 		 * If the action is not an action we are interested in,
1279 		 * skip the entry.
1280 		 */
1281 		if ((cmd.c_action & op_modes) == 0 || pp->p_flags & LIVING ||
1282 		    (cmd.c_levels & lvl_mask) == 0)
1283 			continue;
1284 
1285 		/*
1286 		 * If the modes are the normal modes (ONCE, WAIT, RESPAWN, OFF,
1287 		 * ONDEMAND) and the action field is either OFF or the action
1288 		 * field is ONCE or WAIT and the current level is the same as
1289 		 * the last level, then skip this entry.  ONCE and WAIT only
1290 		 * get run when the level changes.
1291 		 */
1292 		if (op_modes == NORMAL_MODES &&
1293 		    (cmd.c_action == M_OFF ||
1294 		    (cmd.c_action & (M_ONCE|M_WAIT)) &&
1295 		    cur_state == prev_state))
1296 			continue;
1297 
1298 		/*
1299 		 * At this point we are interested in performing the action for
1300 		 * this entry.  Actions fall into two categories, spinning off
1301 		 * a process and not waiting, and spinning off a process and
1302 		 * waiting for it to die.  If the action is ONCE, RESPAWN,
1303 		 * ONDEMAND, POWERFAIL, or BOOT we don't wait for the process
1304 		 * to die, for all other actions we do wait.
1305 		 */
1306 		if (cmd.c_action & (M_ONCE | M_RESPAWN | M_PF | M_BOOT)) {
1307 			spawn(pp, &cmd);
1308 
1309 		} else {
1310 			spawn(pp, &cmd);
1311 			while (waitproc(pp) == FAILURE)
1312 				;
1313 			(void) account(DEAD_PROCESS, pp, NULL);
1314 			pp->p_flags = 0;
1315 		}
1316 	}
1317 	return (status);
1318 }
1319 
1320 /*
1321  * spawn() spawns a shell, inserts the information about the process
1322  * process into the proc_table, and does the startup accounting.
1323  */
1324 static void
1325 spawn(struct PROC_TABLE *process, struct CMD_LINE *cmd)
1326 {
1327 	int		i;
1328 	int		modes, maxfiles;
1329 	time_t		now;
1330 	struct PROC_TABLE tmproc, *oprocess;
1331 
1332 	/*
1333 	 * The modes to be sent to efork() are 0 unless we are
1334 	 * spawning a LVLa, LVLb, or LVLc entry or we will be
1335 	 * waiting for the death of the child before continuing.
1336 	 */
1337 	modes = NAMED;
1338 	if (process->p_flags & DEMANDREQUEST || cur_state == LVLa ||
1339 	    cur_state == LVLb || cur_state == LVLc)
1340 		modes |= DEMANDREQUEST;
1341 	if ((cmd->c_action & (M_SYSINIT | M_WAIT | M_BOOTWAIT | M_PWAIT)) != 0)
1342 		modes |= NOCLEANUP;
1343 
1344 	/*
1345 	 * If this is a respawnable process, check the threshold
1346 	 * information to avoid excessive respawns.
1347 	 */
1348 	if (cmd->c_action & M_RESPAWN) {
1349 		/*
1350 		 * Add NOCLEANUP to all respawnable commands so that the
1351 		 * information about the frequency of respawns isn't lost.
1352 		 */
1353 		modes |= NOCLEANUP;
1354 		(void) time(&now);
1355 
1356 		/*
1357 		 * If no time is assigned, then this is the first time
1358 		 * this command is being processed in this series.  Assign
1359 		 * the current time.
1360 		 */
1361 		if (process->p_time == 0L)
1362 			process->p_time = now;
1363 
1364 		if (process->p_count++ == SPAWN_LIMIT) {
1365 
1366 			if ((now - process->p_time) < SPAWN_INTERVAL) {
1367 				/*
1368 				 * Process is respawning too rapidly.  Print
1369 				 * message and refuse to respawn it for now.
1370 				 */
1371 				console(B_TRUE, "Command is respawning too "
1372 				    "rapidly. Check for possible errors.\n"
1373 				    "id:%4s \"%s\"\n",
1374 				    &cmd->c_id[0], &cmd->c_command[EXEC]);
1375 				return;
1376 			}
1377 			process->p_time = now;
1378 			process->p_count = 0;
1379 
1380 		} else if (process->p_count > SPAWN_LIMIT) {
1381 			/*
1382 			 * If process has been respawning too rapidly and
1383 			 * the inhibit time limit hasn't expired yet, we
1384 			 * refuse to respawn.
1385 			 */
1386 			if (now - process->p_time < SPAWN_INTERVAL + INHIBIT)
1387 				return;
1388 			process->p_time = now;
1389 			process->p_count = 0;
1390 		}
1391 		rsflag = TRUE;
1392 	}
1393 
1394 	/*
1395 	 * Spawn a child process to execute this command.
1396 	 */
1397 	(void) sigset(SIGCLD, SIG_DFL);
1398 	oprocess = process;
1399 	while ((process = efork(cmd->c_action, oprocess, modes)) == NO_ROOM)
1400 		(void) pause();
1401 
1402 	if (process == NULLPROC) {
1403 
1404 		/*
1405 		 * We are the child.  We must make sure we get a different
1406 		 * file pointer for our references to utmpx.  Otherwise our
1407 		 * seeks and reads will compete with those of the parent.
1408 		 */
1409 		endutxent();
1410 
1411 		/*
1412 		 * Perform the accounting for the beginning of a process.
1413 		 * Note that all processes are initially "INIT_PROCESS"es.
1414 		 */
1415 		tmproc.p_id[0] = cmd->c_id[0];
1416 		tmproc.p_id[1] = cmd->c_id[1];
1417 		tmproc.p_id[2] = cmd->c_id[2];
1418 		tmproc.p_id[3] = cmd->c_id[3];
1419 		tmproc.p_pid = getpid();
1420 		tmproc.p_exit = 0;
1421 		(void) account(INIT_PROCESS, &tmproc,
1422 		    prog_name(&cmd->c_command[EXEC]));
1423 		maxfiles = ulimit(UL_GDESLIM, 0);
1424 		for (i = 0; i < maxfiles; i++)
1425 			(void) fcntl(i, F_SETFD, FD_CLOEXEC);
1426 
1427 		/*
1428 		 * Now exec a shell with the -c option and the command
1429 		 * from inittab.
1430 		 */
1431 		(void) execle(SH, "INITSH", "-c", cmd->c_command, (char *)0,
1432 		    glob_envp);
1433 		console(B_TRUE, "Command\n\"%s\"\n failed to execute.  errno "
1434 		    "= %d (exec of shell failed)\n", cmd->c_command, errno);
1435 
1436 		/*
1437 		 * Don't come back so quickly that "init" doesn't have a
1438 		 * chance to finish putting this child in "proc_table".
1439 		 */
1440 		timer(20);
1441 		exit(1);
1442 
1443 	}
1444 
1445 	/*
1446 	 * We are the parent.  Insert the necessary
1447 	 * information in the proc_table.
1448 	 */
1449 	process->p_id[0] = cmd->c_id[0];
1450 	process->p_id[1] = cmd->c_id[1];
1451 	process->p_id[2] = cmd->c_id[2];
1452 	process->p_id[3] = cmd->c_id[3];
1453 
1454 	st_write();
1455 
1456 	(void) sigset(SIGCLD, childeath);
1457 }
1458 
1459 /*
1460  * findpslot() finds the old slot in the process table for the
1461  * command with the same id, or it finds an empty slot.
1462  */
1463 static struct PROC_TABLE *
1464 findpslot(struct CMD_LINE *cmd)
1465 {
1466 	struct PROC_TABLE	*process;
1467 	struct PROC_TABLE	*empty = NULLPROC;
1468 
1469 	for (process = proc_table;
1470 	    (process < proc_table + num_proc); process++) {
1471 		if (process->p_flags & OCCUPIED &&
1472 		    id_eq(process->p_id, cmd->c_id))
1473 			break;
1474 
1475 		/*
1476 		 * If the entry is totally empty and "empty" is still 0,
1477 		 * remember where this hole is and make sure the slot is
1478 		 * zeroed out.
1479 		 */
1480 		if (empty == NULLPROC && (process->p_flags & OCCUPIED) == 0) {
1481 			empty = process;
1482 			process->p_id[0] = '\0';
1483 			process->p_id[1] = '\0';
1484 			process->p_id[2] = '\0';
1485 			process->p_id[3] = '\0';
1486 			process->p_pid = 0;
1487 			process->p_time = 0L;
1488 			process->p_count = 0;
1489 			process->p_flags = 0;
1490 			process->p_exit = 0;
1491 		}
1492 	}
1493 
1494 	/*
1495 	 * If there is no entry for this slot, then there should be an
1496 	 * empty slot.  If there is no empty slot, then we've run out
1497 	 * of proc_table space.  If the latter is true, empty will be
1498 	 * NULL and the caller will have to complain.
1499 	 */
1500 	if (process == (proc_table + num_proc))
1501 		process = empty;
1502 
1503 	return (process);
1504 }
1505 
1506 /*
1507  * getcmd() parses lines from inittab.  Each time it finds a command line
1508  * it will return TRUE as well as fill the passed CMD_LINE structure and
1509  * the shell command string.  When the end of inittab is reached, FALSE
1510  * is returned inittab is automatically opened if it is not currently open
1511  * and is closed when the end of the file is reached.
1512  */
1513 static FILE *fp_inittab = NULL;
1514 
1515 static int
1516 getcmd(struct CMD_LINE *cmd, char *shcmd)
1517 {
1518 	char	*ptr;
1519 	int	c, lastc, state;
1520 	char 	*ptr1;
1521 	int	answer, i, proceed;
1522 	struct	stat	sbuf;
1523 	static char *actions[] = {
1524 		"off", "respawn", "ondemand", "once", "wait", "boot",
1525 		"bootwait", "powerfail", "powerwait", "initdefault",
1526 		"sysinit",
1527 	};
1528 	static short act_masks[] = {
1529 		M_OFF, M_RESPAWN, M_ONDEMAND, M_ONCE, M_WAIT, M_BOOT,
1530 		M_BOOTWAIT, M_PF, M_PWAIT, M_INITDEFAULT, M_SYSINIT,
1531 	};
1532 	/*
1533 	 * Only these actions will be allowed for entries which
1534 	 * are specified for single-user mode.
1535 	 */
1536 	short su_acts = M_INITDEFAULT | M_PF | M_PWAIT | M_WAIT;
1537 
1538 	if (fp_inittab == NULL) {
1539 		/*
1540 		 * Before attempting to open inittab we stat it to make
1541 		 * sure it currently exists and is not empty.  We try
1542 		 * several times because someone may have temporarily
1543 		 * unlinked or truncated the file.
1544 		 */
1545 		for (i = 0; i < 3; i++) {
1546 			if (stat(INITTAB, &sbuf) == -1) {
1547 				if (i == 2) {
1548 					console(B_TRUE,
1549 					    "Cannot stat %s, errno: %d\n",
1550 					    INITTAB, errno);
1551 					return (FAILURE);
1552 				} else {
1553 					timer(3);
1554 				}
1555 			} else if (sbuf.st_size < 10) {
1556 				if (i == 2) {
1557 					console(B_TRUE,
1558 					    "%s truncated or corrupted\n",
1559 					    INITTAB);
1560 					return (FAILURE);
1561 				} else {
1562 					timer(3);
1563 				}
1564 			} else {
1565 				break;
1566 			}
1567 		}
1568 
1569 		/*
1570 		 * If unable to open inittab, print error message and
1571 		 * return FAILURE to caller.
1572 		 */
1573 		if ((fp_inittab = fopen(INITTAB, "r")) == NULL) {
1574 			console(B_TRUE, "Cannot open %s errno: %d\n", INITTAB,
1575 			    errno);
1576 			return (FAILURE);
1577 		}
1578 	}
1579 
1580 	/*
1581 	 * Keep getting commands from inittab until you find a
1582 	 * good one or run out of file.
1583 	 */
1584 	for (answer = FALSE; answer == FALSE; ) {
1585 		/*
1586 		 * Zero out the cmd itself before trying next line.
1587 		 */
1588 		bzero(cmd, sizeof (struct CMD_LINE));
1589 
1590 		/*
1591 		 * Read in lines of inittab, parsing at colons, until a line is
1592 		 * read in which doesn't end with a backslash.  Do not start if
1593 		 * the first character read is an EOF.  Note that this means
1594 		 * that lines which don't end in a newline are still processed,
1595 		 * since the "for" will terminate normally once started,
1596 		 * regardless of whether line terminates with a newline or EOF.
1597 		 */
1598 		state = FAILURE;
1599 		if ((c = fgetc(fp_inittab)) == EOF) {
1600 			answer = FALSE;
1601 			(void) fclose(fp_inittab);
1602 			fp_inittab = NULL;
1603 			break;
1604 		}
1605 
1606 		for (proceed = TRUE, ptr = shcmd, state = ID, lastc = '\0';
1607 		    proceed && c != EOF;
1608 		    lastc = c, c = fgetc(fp_inittab)) {
1609 		    /* If we're not in the FAILURE state and haven't	*/
1610 		    /* yet reached the shell command field, process	*/
1611 		    /* the line, otherwise just look for a real end	*/
1612 		    /* of line.						*/
1613 		    if (state != FAILURE && state != COMMAND) {
1614 			/*
1615 			 * Squeeze out spaces and tabs.
1616 			 */
1617 			if (c == ' ' || c == '\t')
1618 				continue;
1619 
1620 			/*
1621 			 * Ignore characters in a comment, except for the \n.
1622 			 */
1623 			if (state == COMMENT) {
1624 				if (c == '\n') {
1625 					lastc = ' ';
1626 					break;
1627 				} else {
1628 					continue;
1629 				}
1630 			}
1631 
1632 			/*
1633 			 * Detect comments (lines whose first non-whitespace
1634 			 * character is '#') by checking that we're at the
1635 			 * beginning of a line, have seen a '#', and haven't
1636 			 * yet accumulated any characters.
1637 			 */
1638 			if (state == ID && c == '#' && ptr == shcmd) {
1639 				state = COMMENT;
1640 				continue;
1641 			}
1642 
1643 			/*
1644 			 * If the character is a ':', then check the
1645 			 * previous field for correctness and advance
1646 			 * to the next field.
1647 			 */
1648 			if (c == ':') {
1649 			    switch (state) {
1650 
1651 			    case ID :
1652 				/*
1653 				 * Check to see that there are only
1654 				 * 1 to 4 characters for the id.
1655 				 */
1656 				if ((i = ptr - shcmd) < 1 || i > 4) {
1657 					state = FAILURE;
1658 				} else {
1659 					bcopy(shcmd, &cmd->c_id[0], i);
1660 					ptr = shcmd;
1661 					state = LEVELS;
1662 				}
1663 				break;
1664 
1665 			    case LEVELS :
1666 				/*
1667 				 * Build a mask for all the levels for
1668 				 * which this command will be legal.
1669 				 */
1670 				for (cmd->c_levels = 0, ptr1 = shcmd;
1671 				    ptr1 < ptr; ptr1++) {
1672 					int mask;
1673 					if (lvlname_to_mask(*ptr1,
1674 					    &mask) == -1) {
1675 						state = FAILURE;
1676 						break;
1677 					}
1678 					cmd->c_levels |= mask;
1679 				}
1680 				if (state != FAILURE) {
1681 					state = ACTION;
1682 					ptr = shcmd;	/* Reset the buffer */
1683 				}
1684 				break;
1685 
1686 			    case ACTION :
1687 				/*
1688 				 * Null terminate the string in shcmd buffer and
1689 				 * then try to match against legal actions.  If
1690 				 * the field is of length 0, then the default of
1691 				 * "RESPAWN" is used if the id is numeric,
1692 				 * otherwise the default is "OFF".
1693 				 */
1694 				if (ptr == shcmd) {
1695 					if (isdigit(cmd->c_id[0]) &&
1696 					    (cmd->c_id[1] == '\0' ||
1697 						isdigit(cmd->c_id[1])) &&
1698 					    (cmd->c_id[2] == '\0' ||
1699 						isdigit(cmd->c_id[2])) &&
1700 					    (cmd->c_id[3] == '\0' ||
1701 						isdigit(cmd->c_id[3])))
1702 						    cmd->c_action = M_RESPAWN;
1703 					else
1704 						    cmd->c_action = M_OFF;
1705 				} else {
1706 				    for (cmd->c_action = 0, i = 0, *ptr = '\0';
1707 				    i < sizeof (actions)/sizeof (char *);
1708 				    i++) {
1709 					if (strcmp(shcmd, actions[i]) == 0) {
1710 					    if ((cmd->c_levels & MASKSU) &&
1711 						!(act_masks[i] & su_acts))
1712 						    cmd->c_action = 0;
1713 					    else
1714 						cmd->c_action = act_masks[i];
1715 					    break;
1716 					}
1717 				    }
1718 				}
1719 
1720 				/*
1721 				 * If the action didn't match any legal action,
1722 				 * set state to FAILURE.
1723 				 */
1724 				if (cmd->c_action == 0) {
1725 					state = FAILURE;
1726 				} else {
1727 					state = COMMAND;
1728 					(void) strcpy(shcmd, "exec ");
1729 				}
1730 				ptr = shcmd + EXEC;
1731 				break;
1732 			    }
1733 			    continue;
1734 			}
1735 		    }
1736 
1737 		    /* If the character is a '\n', then this is the end of a */
1738 		    /* line.  If the '\n' wasn't preceded by a backslash, */
1739 		    /* it is also the end of an inittab command.  If it was */
1740 		    /* preceded by a backslash then the next line is a */
1741 		    /* continuation.  Note that the continuation '\n' falls */
1742 		    /* through and is treated like other characters and is */
1743 		    /* stored in the shell command line. */
1744 		    if (c == '\n' && lastc != '\\') {
1745 				proceed = FALSE;
1746 				*ptr = '\0';
1747 				break;
1748 		    }
1749 
1750 		    /* For all other characters just stuff them into the */
1751 		    /* command as long as there aren't too many of them. */
1752 		    /* Make sure there is room for a terminating '\0' also. */
1753 		    if (ptr >= shcmd + MAXCMDL - 1)
1754 			state = FAILURE;
1755 		    else
1756 			*ptr++ = (char)c;
1757 
1758 		    /* If the character we just stored was a quoted	*/
1759 		    /* backslash, then change "c" to '\0', so that this	*/
1760 		    /* backslash will not cause a subsequent '\n' to appear */
1761 		    /* quoted.  In otherwords '\' '\' '\n' is the real end */
1762 		    /* of a command, while '\' '\n' is a continuation. */
1763 		    if (c == '\\' && lastc == '\\')
1764 			c = '\0';
1765 		}
1766 
1767 		/*
1768 		 * Make sure all the fields are properly specified
1769 		 * for a good command line.
1770 		 */
1771 		if (state == COMMAND) {
1772 			answer = TRUE;
1773 			cmd->c_command = shcmd;
1774 
1775 			/*
1776 			 * If no default level was supplied, insert
1777 			 * all numerical levels.
1778 			 */
1779 			if (cmd->c_levels == 0)
1780 				cmd->c_levels = MASK_NUMERIC;
1781 
1782 			/*
1783 			 * If no action has been supplied, declare this
1784 			 * entry to be OFF.
1785 			 */
1786 			if (cmd->c_action == 0)
1787 				cmd->c_action = M_OFF;
1788 
1789 			/*
1790 			 * If no shell command has been supplied, make sure
1791 			 * there is a null string in the command field.
1792 			 */
1793 			if (ptr == shcmd + EXEC)
1794 				*shcmd = '\0';
1795 		} else
1796 			answer = FALSE;
1797 
1798 		/*
1799 		 * If we have reached the end of inittab, then close it
1800 		 * and quit trying to find a good command line.
1801 		 */
1802 		if (c == EOF) {
1803 			(void) fclose(fp_inittab);
1804 			fp_inittab = NULL;
1805 			break;
1806 		}
1807 	}
1808 	return (answer);
1809 }
1810 
1811 /*
1812  * lvlname_to_state(): convert the character name of a state to its level
1813  * (its corresponding signal number).
1814  */
1815 static int
1816 lvlname_to_state(char name)
1817 {
1818 	int i;
1819 	for (i = 0; i < LVL_NELEMS; i++) {
1820 		if (lvls[i].lvl_name == name)
1821 			return (lvls[i].lvl_state);
1822 	}
1823 	return (-1);
1824 }
1825 
1826 /*
1827  * state_to_name(): convert the level to the character name.
1828  */
1829 static char
1830 state_to_name(int state)
1831 {
1832 	int i;
1833 	for (i = 0; i < LVL_NELEMS; i++) {
1834 		if (lvls[i].lvl_state == state)
1835 			return (lvls[i].lvl_name);
1836 	}
1837 	return (-1);
1838 }
1839 
1840 /*
1841  * state_to_mask(): return the mask corresponding to a signal number
1842  */
1843 static int
1844 state_to_mask(int state)
1845 {
1846 	int i;
1847 	for (i = 0; i < LVL_NELEMS; i++) {
1848 		if (lvls[i].lvl_state == state)
1849 			return (lvls[i].lvl_mask);
1850 	}
1851 	return (0);	/* return 0, since that represents an empty mask */
1852 }
1853 
1854 /*
1855  * lvlname_to_mask(): return the mask corresponding to a levels character name
1856  */
1857 static int
1858 lvlname_to_mask(char name, int *mask)
1859 {
1860 	int i;
1861 	for (i = 0; i < LVL_NELEMS; i++) {
1862 		if (lvls[i].lvl_name == name) {
1863 			*mask = lvls[i].lvl_mask;
1864 			return (0);
1865 		}
1866 	}
1867 	return (-1);
1868 }
1869 
1870 /*
1871  * state_to_flags(): return the flags corresponding to a runlevel.  These
1872  * indicate properties of that runlevel.
1873  */
1874 static int
1875 state_to_flags(int state)
1876 {
1877 	int i;
1878 	for (i = 0; i < LVL_NELEMS; i++) {
1879 		if (lvls[i].lvl_state == state)
1880 			return (lvls[i].lvl_flags);
1881 	}
1882 	return (0);
1883 }
1884 
1885 /*
1886  * killproc() creates a child which kills the process specified by pid.
1887  */
1888 void
1889 killproc(pid_t pid)
1890 {
1891 	struct PROC_TABLE	*process;
1892 
1893 	(void) sigset(SIGCLD, SIG_DFL);
1894 	while ((process = efork(M_OFF, NULLPROC, 0)) == NO_ROOM)
1895 		(void) pause();
1896 	(void) sigset(SIGCLD, childeath);
1897 
1898 	if (process == NULLPROC) {
1899 		/*
1900 		 * efork() sets all signal handlers to the default, so reset
1901 		 * the ALRM handler to make timer() work as expected.
1902 		 */
1903 		(void) sigset(SIGALRM, alarmclk);
1904 
1905 		/*
1906 		 * We are the child.  Try to terminate the process nicely
1907 		 * first using SIGTERM and if it refuses to die in TWARN
1908 		 * seconds kill it with SIGKILL.
1909 		 */
1910 		(void) kill(pid, SIGTERM);
1911 		(void) timer(TWARN);
1912 		(void) kill(pid, SIGKILL);
1913 		(void) exit(0);
1914 	}
1915 }
1916 
1917 /*
1918  * Set up the default environment for all procs to be forked from init.
1919  * Read the values from the /etc/default/init file, except for PATH.  If
1920  * there's not enough room in the environment array, the environment
1921  * lines that don't fit are silently discarded.
1922  */
1923 void
1924 init_env()
1925 {
1926 	char	line[MAXCMDL];
1927 	FILE	*fp;
1928 	int	inquotes, length, wslength;
1929 	char	*tokp, *cp1, *cp2;
1930 
1931 	glob_envp[0] = malloc((unsigned)(strlen(DEF_PATH)+2));
1932 	(void) strcpy(glob_envp[0], DEF_PATH);
1933 	glob_envn = 1;
1934 
1935 	if (rflg) {
1936 		glob_envp[1] =
1937 		    malloc((unsigned)(strlen("_DVFS_RECONFIG=YES")+2));
1938 		(void) strcpy(glob_envp[1], "_DVFS_RECONFIG=YES");
1939 		++glob_envn;
1940 	} else if (bflg == 1) {
1941 		glob_envp[1] =
1942 		    malloc((unsigned)(strlen("RB_NOBOOTRC=YES")+2));
1943 		(void) strcpy(glob_envp[1], "RB_NOBOOTRC=YES");
1944 		++glob_envn;
1945 	}
1946 
1947 	if ((fp = fopen(ENVFILE, "r")) == NULL) {
1948 		console(B_TRUE,
1949 		    "Cannot open %s. Environment not initialized.\n",
1950 		    ENVFILE);
1951 	} else {
1952 		while (fgets(line, MAXCMDL - 1, fp) != NULL &&
1953 		    glob_envn < MAXENVENT - 2) {
1954 			/*
1955 			 * Toss newline
1956 			 */
1957 			length = strlen(line);
1958 			if (line[length - 1] == '\n')
1959 				line[length - 1] = '\0';
1960 
1961 			/*
1962 			 * Ignore blank or comment lines.
1963 			 */
1964 			if (line[0] == '#' || line[0] == '\0' ||
1965 			    (wslength = strspn(line, " \t\n")) ==
1966 			    strlen(line) ||
1967 			    strchr(line, '#') == line + wslength)
1968 				continue;
1969 
1970 			/*
1971 			 * First make a pass through the line and change
1972 			 * any non-quoted semi-colons to blanks so they
1973 			 * will be treated as token separators below.
1974 			 */
1975 			inquotes = 0;
1976 			for (cp1 = line; *cp1 != '\0'; cp1++) {
1977 				if (*cp1 == '"') {
1978 					if (inquotes == 0)
1979 						inquotes = 1;
1980 					else
1981 						inquotes = 0;
1982 				} else if (*cp1 == ';') {
1983 					if (inquotes == 0)
1984 						*cp1 = ' ';
1985 				}
1986 			}
1987 
1988 			/*
1989 			 * Tokens within the line are separated by blanks
1990 			 *  and tabs.  For each token in the line which
1991 			 * contains a '=' we strip out any quotes and then
1992 			 * stick the token in the environment array.
1993 			 */
1994 			if ((tokp = strtok(line, " \t")) == NULL)
1995 				continue;
1996 			do {
1997 				if (strchr(tokp, '=') == NULL)
1998 					continue;
1999 				length = strlen(tokp);
2000 				while ((cp1 = strpbrk(tokp, "\"\'")) != NULL) {
2001 					for (cp2 = cp1;
2002 					    cp2 < &tokp[length]; cp2++)
2003 						*cp2 = *(cp2 + 1);
2004 					length--;
2005 				}
2006 
2007 				if (strncmp(tokp, "CMASK=",
2008 				    sizeof ("CMASK=") - 1) == 0) {
2009 					long t;
2010 
2011 					/* We know there's an = */
2012 					t = strtol(strchr(tokp, '=') + 1, NULL,
2013 					    8);
2014 
2015 					/* Sanity */
2016 					if (t <= 077 && t >= 0)
2017 						cmask = (int)t;
2018 					(void) umask(cmask);
2019 					continue;
2020 				}
2021 				glob_envp[glob_envn] =
2022 				    malloc((unsigned)(length + 1));
2023 				(void) strcpy(glob_envp[glob_envn], tokp);
2024 				if (++glob_envn >= MAXENVENT - 1)
2025 					break;
2026 			} while ((tokp = strtok(NULL, " \t")) != NULL);
2027 		}
2028 
2029 		/*
2030 		 * Append a null pointer to the environment array
2031 		 * to mark its end.
2032 		 */
2033 		glob_envp[glob_envn] = NULL;
2034 		(void) fclose(fp);
2035 	}
2036 }
2037 
2038 /*
2039  * boot_init(): Do initialization things that should be done at boot.
2040  */
2041 void
2042 boot_init()
2043 {
2044 	int i;
2045 	struct PROC_TABLE *process, *oprocess;
2046 	struct CMD_LINE	cmd;
2047 	char	line[MAXCMDL];
2048 	char	svc_aux[SVC_AUX_SIZE];
2049 	char	init_svc_fmri[SVC_FMRI_SIZE];
2050 	char *old_path;
2051 	int maxfiles;
2052 
2053 	/* Use INIT_PATH for sysinit cmds */
2054 	old_path = glob_envp[0];
2055 	glob_envp[0] = malloc((unsigned)(strlen(INIT_PATH)+2));
2056 	(void) strcpy(glob_envp[0], INIT_PATH);
2057 
2058 	/*
2059 	 * Scan inittab(4) and process the special svc.startd entry, initdefault
2060 	 * and sysinit entries.
2061 	 */
2062 	while (getcmd(&cmd, &line[0]) == TRUE) {
2063 		if (startd_tmpl >= 0 && id_eq(cmd.c_id, "smf")) {
2064 			process_startd_line(&cmd, line);
2065 			(void) snprintf(startd_svc_aux, SVC_AUX_SIZE,
2066 			    INITTAB_ENTRY_ID_STR_FORMAT, cmd.c_id);
2067 		} else if (cmd.c_action == M_INITDEFAULT) {
2068 			/*
2069 			 * initdefault is no longer meaningful, as the SMF
2070 			 * milestone controls what (legacy) run level we
2071 			 * boot to.
2072 			 */
2073 			console(B_TRUE,
2074 			    "Ignoring legacy \"initdefault\" entry.\n");
2075 		} else if (cmd.c_action == M_SYSINIT) {
2076 			/*
2077 			 * Execute the "sysinit" entry and wait for it to
2078 			 * complete.  No bookkeeping is performed on these
2079 			 * entries because we avoid writing to the file system
2080 			 * until after there has been an chance to check it.
2081 			 */
2082 			if (process = findpslot(&cmd)) {
2083 				(void) sigset(SIGCLD, SIG_DFL);
2084 				(void) snprintf(svc_aux, SVC_AUX_SIZE,
2085 				    INITTAB_ENTRY_ID_STR_FORMAT, cmd.c_id);
2086 				(void) snprintf(init_svc_fmri, SVC_FMRI_SIZE,
2087 				    SVC_INIT_PREFIX INITTAB_ENTRY_ID_STR_FORMAT,
2088 				    cmd.c_id);
2089 				if (legacy_tmpl >= 0) {
2090 					(void) ct_pr_tmpl_set_svc_fmri(
2091 					    legacy_tmpl, init_svc_fmri);
2092 					(void) ct_pr_tmpl_set_svc_aux(
2093 					    legacy_tmpl, svc_aux);
2094 				}
2095 
2096 				for (oprocess = process;
2097 				    (process = efork(M_OFF, oprocess,
2098 				    (NAMED|NOCLEANUP))) == NO_ROOM;
2099 				    /* CSTYLED */)
2100 					;
2101 				(void) sigset(SIGCLD, childeath);
2102 
2103 				if (process == NULLPROC) {
2104 					maxfiles = ulimit(UL_GDESLIM, 0);
2105 
2106 					for (i = 0; i < maxfiles; i++)
2107 						(void) fcntl(i, F_SETFD,
2108 						    FD_CLOEXEC);
2109 					(void) execle(SH, "INITSH", "-c",
2110 					    cmd.c_command,
2111 					    (char *)0, glob_envp);
2112 					console(B_TRUE,
2113 "Command\n\"%s\"\n failed to execute.  errno = %d (exec of shell failed)\n",
2114 					    cmd.c_command, errno);
2115 					exit(1);
2116 				} else while (waitproc(process) == FAILURE);
2117 				process->p_flags = 0;
2118 				st_write();
2119 			}
2120 		}
2121 	}
2122 
2123 	/* Restore the path. */
2124 	free(glob_envp[0]);
2125 	glob_envp[0] = old_path;
2126 
2127 	/*
2128 	 * This will enable st_write() to complain about init_state_file.
2129 	 */
2130 	booting = 0;
2131 
2132 	/*
2133 	 * If the /etc/ioctl.syscon didn't exist or had invalid contents write
2134 	 * out a correct version.
2135 	 */
2136 	if (write_ioctl)
2137 		write_ioctl_syscon();
2138 
2139 	/*
2140 	 * Start svc.startd(1M), which does most of the work.
2141 	 */
2142 	if (startd_cline[0] != '\0' && startd_tmpl >= 0) {
2143 		/* Start svc.startd. */
2144 		if (startd_run(startd_cline, startd_tmpl, 0) == -1)
2145 			cur_state = SINGLE_USER;
2146 	} else {
2147 		console(B_TRUE, "Absent svc.startd entry or bad "
2148 		    "contract template.  Not starting svc.startd.\n");
2149 		enter_maintenance();
2150 	}
2151 }
2152 
2153 /*
2154  * init_signals(): Initialize all signals to either be caught or ignored.
2155  */
2156 void
2157 init_signals(void)
2158 {
2159 	struct sigaction act;
2160 	int i;
2161 
2162 	/*
2163 	 * Start by ignoring all signals, then selectively re-enable some.
2164 	 * The SIG_IGN disposition will only affect asynchronous signals:
2165 	 * any signal that we trigger synchronously that doesn't end up
2166 	 * being handled by siglvl() will be forcibly delivered by the kernel.
2167 	 */
2168 	for (i = SIGHUP; i <= SIGRTMAX; i++)
2169 		(void) sigset(i, SIG_IGN);
2170 
2171 	/*
2172 	 * Handle all level-changing signals using siglvl() and set sa_mask so
2173 	 * that all level-changing signals are blocked while in siglvl().
2174 	 */
2175 	act.sa_handler = siglvl;
2176 	act.sa_flags = SA_SIGINFO;
2177 	(void) sigemptyset(&act.sa_mask);
2178 
2179 	(void) sigaddset(&act.sa_mask, LVLQ);
2180 	(void) sigaddset(&act.sa_mask, LVL0);
2181 	(void) sigaddset(&act.sa_mask, LVL1);
2182 	(void) sigaddset(&act.sa_mask, LVL2);
2183 	(void) sigaddset(&act.sa_mask, LVL3);
2184 	(void) sigaddset(&act.sa_mask, LVL4);
2185 	(void) sigaddset(&act.sa_mask, LVL5);
2186 	(void) sigaddset(&act.sa_mask, LVL6);
2187 	(void) sigaddset(&act.sa_mask, SINGLE_USER);
2188 	(void) sigaddset(&act.sa_mask, LVLa);
2189 	(void) sigaddset(&act.sa_mask, LVLb);
2190 	(void) sigaddset(&act.sa_mask, LVLc);
2191 
2192 	(void) sigaction(LVLQ, &act, NULL);
2193 	(void) sigaction(LVL0, &act, NULL);
2194 	(void) sigaction(LVL1, &act, NULL);
2195 	(void) sigaction(LVL2, &act, NULL);
2196 	(void) sigaction(LVL3, &act, NULL);
2197 	(void) sigaction(LVL4, &act, NULL);
2198 	(void) sigaction(LVL5, &act, NULL);
2199 	(void) sigaction(LVL6, &act, NULL);
2200 	(void) sigaction(SINGLE_USER, &act, NULL);
2201 	(void) sigaction(LVLa, &act, NULL);
2202 	(void) sigaction(LVLb, &act, NULL);
2203 	(void) sigaction(LVLc, &act, NULL);
2204 
2205 	(void) sigset(SIGALRM, alarmclk);
2206 	alarmclk();
2207 
2208 	(void) sigset(SIGCLD, childeath);
2209 	(void) sigset(SIGPWR, powerfail);
2210 }
2211 
2212 /*
2213  * Set up pipe for "godchildren". If the file exists and is a pipe just open
2214  * it. Else, if the file system is r/w create it.  Otherwise, defer its
2215  * creation and open until after /var/run has been mounted.  This function is
2216  * only called on startup and when explicitly requested via LVLQ.
2217  */
2218 void
2219 setup_pipe()
2220 {
2221 	struct stat stat_buf;
2222 	struct statvfs statvfs_buf;
2223 	struct sigaction act;
2224 
2225 	/*
2226 	 * Always close the previous pipe descriptor as the mounted filesystems
2227 	 * may have changed.
2228 	 */
2229 	if (Pfd >= 0)
2230 		(void) close(Pfd);
2231 
2232 	if ((stat(INITPIPE, &stat_buf) == 0) &&
2233 	    ((stat_buf.st_mode & (S_IFMT|S_IRUSR)) == (S_IFIFO|S_IRUSR)))
2234 		Pfd = open(INITPIPE, O_RDWR | O_NDELAY);
2235 	else
2236 		if ((statvfs(INITPIPE_DIR, &statvfs_buf) == 0) &&
2237 		    ((statvfs_buf.f_flag & ST_RDONLY) == 0)) {
2238 			(void) unlink(INITPIPE);
2239 			(void) mknod(INITPIPE, S_IFIFO | 0600, 0);
2240 			Pfd = open(INITPIPE, O_RDWR | O_NDELAY);
2241 		}
2242 
2243 	if (Pfd >= 0) {
2244 		(void) ioctl(Pfd, I_SETSIG, S_INPUT);
2245 		/*
2246 		 * Read pipe in message discard mode.
2247 		 */
2248 		(void) ioctl(Pfd, I_SRDOPT, RMSGD);
2249 
2250 		act.sa_handler = sigpoll;
2251 		act.sa_flags = 0;
2252 		(void) sigemptyset(&act.sa_mask);
2253 		(void) sigaddset(&act.sa_mask, SIGCLD);
2254 		(void) sigaction(SIGPOLL, &act, NULL);
2255 	}
2256 }
2257 
2258 /*
2259  * siglvl - handle an asynchronous signal from init(1M) telling us that we
2260  * should change the current run level.  We set new_state accordingly.
2261  */
2262 void
2263 siglvl(int sig, siginfo_t *sip, ucontext_t *ucp)
2264 {
2265 	struct PROC_TABLE *process;
2266 	struct sigaction act;
2267 
2268 	/*
2269 	 * If the signal was from the kernel (rather than init(1M)) then init
2270 	 * itself tripped the signal.  That is, we might have a bug and tripped
2271 	 * a real SIGSEGV instead of receiving it as an alias for SIGLVLa.  In
2272 	 * such a case we reset the disposition to SIG_DFL, block all signals
2273 	 * in uc_mask but the current one, and return to the interrupted ucp
2274 	 * to effect an appropriate death.  The kernel will then restart us.
2275 	 *
2276 	 * The one exception to SI_FROMKERNEL() is SIGFPE (a.k.a. LVL6), which
2277 	 * the kernel can send us when it wants to effect an orderly reboot.
2278 	 * For this case we must also verify si_code is zero, rather than a
2279 	 * code such as FPE_INTDIV which a bug might have triggered.
2280 	 */
2281 	if (sip != NULL && SI_FROMKERNEL(sip) &&
2282 	    (sig != SIGFPE || sip->si_code == 0)) {
2283 
2284 		(void) sigemptyset(&act.sa_mask);
2285 		act.sa_handler = SIG_DFL;
2286 		act.sa_flags = 0;
2287 		(void) sigaction(sig, &act, NULL);
2288 
2289 		(void) sigfillset(&ucp->uc_sigmask);
2290 		(void) sigdelset(&ucp->uc_sigmask, sig);
2291 		ucp->uc_flags |= UC_SIGMASK;
2292 
2293 		(void) setcontext(ucp);
2294 	}
2295 
2296 	/*
2297 	 * If the signal received is a LVLQ signal, do not really
2298 	 * change levels, just restate the current level.  If the
2299 	 * signal is not a LVLQ, set the new level to the signal
2300 	 * received.
2301 	 */
2302 	if (sig == LVLQ) {
2303 		new_state = cur_state;
2304 		lvlq_received = B_TRUE;
2305 	} else {
2306 		new_state = sig;
2307 	}
2308 
2309 	/*
2310 	 * Clear all times and repeat counts in the process table
2311 	 * since either the level is changing or the user has editted
2312 	 * the inittab file and wants us to look at it again.
2313 	 * If the user has fixed a typo, we don't want residual timing
2314 	 * data preventing the fixed command line from executing.
2315 	 */
2316 	for (process = proc_table;
2317 	    (process < proc_table + num_proc); process++) {
2318 		process->p_time = 0L;
2319 		process->p_count = 0;
2320 	}
2321 
2322 	/*
2323 	 * Set the flag to indicate that a "user signal" was received.
2324 	 */
2325 	wakeup.w_flags.w_usersignal = 1;
2326 }
2327 
2328 
2329 /*
2330  * alarmclk
2331  */
2332 static void
2333 alarmclk()
2334 {
2335 	time_up = TRUE;
2336 }
2337 
2338 /*
2339  * childeath_single():
2340  *
2341  * This used to be the SIGCLD handler and it was set with signal()
2342  * (as opposed to sigset()).  When a child exited we'd come to the
2343  * handler, wait for the child, and reenable the handler with
2344  * signal() just before returning.  The implementation of signal()
2345  * checks with waitid() for waitable children and sends a SIGCLD
2346  * if there are some.  If children are exiting faster than the
2347  * handler can run we keep sending signals and the handler never
2348  * gets to return and eventually the stack runs out and init dies.
2349  * To prevent that we set the handler with sigset() so the handler
2350  * doesn't need to be reset, and in childeath() (see below) we
2351  * call childeath_single() as long as there are children to be
2352  * waited for.  If a child exits while init is in the handler a
2353  * SIGCLD will be pending and delivered on return from the handler.
2354  * If the child was already waited for the handler will have nothing
2355  * to do and return, otherwise the child will be waited for.
2356  */
2357 static void
2358 childeath_single()
2359 {
2360 	struct PROC_TABLE	*process;
2361 	struct pidlist		*pp;
2362 	pid_t			pid;
2363 	int			status;
2364 
2365 	/*
2366 	 * Perform wait to get the process id of the child that died and
2367 	 * then scan the process table to see if we are interested in
2368 	 * this process. NOTE: if a super-user sends the SIGCLD signal
2369 	 * to init, the following wait will not immediately return and
2370 	 * init will be inoperative until one of its child really does die.
2371 	 */
2372 	pid = wait(&status);
2373 
2374 	for (process = proc_table;
2375 	    (process < proc_table + num_proc); process++) {
2376 		if ((process->p_flags & (LIVING|OCCUPIED)) ==
2377 		    (LIVING|OCCUPIED) && process->p_pid == pid) {
2378 
2379 			/*
2380 			 * Mark this process as having died and store the exit
2381 			 * status.  Also set the wakeup flag for a dead child
2382 			 * and break out of the loop.
2383 			 */
2384 			process->p_flags &= ~LIVING;
2385 			process->p_exit = (short)status;
2386 			wakeup.w_flags.w_childdeath = 1;
2387 
2388 			return;
2389 		}
2390 	}
2391 
2392 	/*
2393 	 * No process was found above, look through auxiliary list.
2394 	 */
2395 	(void) sighold(SIGPOLL);
2396 	pp = Plhead;
2397 	while (pp) {
2398 		if (pid > pp->pl_pid) {
2399 			/*
2400 			 * Keep on looking.
2401 			 */
2402 			pp = pp->pl_next;
2403 			continue;
2404 		} else if (pid < pp->pl_pid) {
2405 			/*
2406 			 * Not in the list.
2407 			 */
2408 			break;
2409 		} else {
2410 			/*
2411 			 * This is a dead "godchild".
2412 			 */
2413 			pp->pl_dflag = 1;
2414 			pp->pl_exit = (short)status;
2415 			wakeup.w_flags.w_childdeath = 1;
2416 			Gchild = 1;	/* Notice to call cleanaux(). */
2417 			break;
2418 		}
2419 	}
2420 
2421 	(void) sigrelse(SIGPOLL);
2422 }
2423 
2424 /* ARGSUSED */
2425 static void
2426 childeath(int signo)
2427 {
2428 	siginfo_t info;
2429 
2430 	while ((waitid(P_ALL, (id_t)0, &info, WEXITED|WNOHANG|WNOWAIT) == 0) &&
2431 	    info.si_pid != 0)
2432 		childeath_single();
2433 }
2434 
2435 static void
2436 powerfail()
2437 {
2438 	(void) nice(-19);
2439 	wakeup.w_flags.w_powerhit = 1;
2440 }
2441 
2442 /*
2443  * efork() forks a child and the parent inserts the process in its table
2444  * of processes that are directly a result of forks that it has performed.
2445  * The child just changes the "global" with the process id for this process
2446  * to it's new value.
2447  * If efork() is called with a pointer into the proc_table it uses that slot,
2448  * otherwise it searches for a free slot.  Regardless of how it was called,
2449  * it returns the pointer to the proc_table entry
2450  *
2451  * The SIGCLD handler is set to default (SIG_DFL) before calling efork().
2452  * This relies on the somewhat obscure SVR2 SIGCLD/SIG_DFL semantic
2453  * implied by the use of signal(3c).  While the meaning of SIG_DFL for
2454  * SIGCLD is nominally to ignore the signal, once the signal disposition
2455  * is set to childeath(), the kernel will post a SIGCLD if a child
2456  * exited during the period the disposition was SIG_DFL.  It acts more
2457  * like a signal block.
2458  *
2459  * Ideally, this should be rewritten to use modern signal semantics.
2460  */
2461 static struct PROC_TABLE *
2462 efork(int action, struct PROC_TABLE *process, int modes)
2463 {
2464 	pid_t	childpid;
2465 	struct PROC_TABLE *proc;
2466 	int		i;
2467 	void (*oldroutine)();
2468 	/*
2469 	 * Freshen up the proc_table, removing any entries for dead processes
2470 	 * that don't have NOCLEANUP set.  Perform the necessary accounting.
2471 	 */
2472 	for (proc = proc_table; (proc < proc_table + num_proc); proc++) {
2473 		if ((proc->p_flags & (OCCUPIED|LIVING|NOCLEANUP)) ==
2474 		    (OCCUPIED)) {
2475 			/*
2476 			 * Is this a named process?
2477 			 * If so, do the necessary bookkeeping.
2478 			 */
2479 			if (proc->p_flags & NAMED)
2480 				(void) account(DEAD_PROCESS, proc, NULL);
2481 
2482 			/*
2483 			 * Free this entry for new usage.
2484 			 */
2485 			proc->p_flags = 0;
2486 		}
2487 	}
2488 
2489 	while ((childpid = fork()) == FAILURE) {
2490 		/*
2491 		 * Shorten the alarm timer in case someone else's child dies
2492 		 * and free up a slot in the process table.
2493 		 */
2494 		setimer(5);
2495 
2496 		/*
2497 		 * Wait for some children to die.  Since efork() is normally
2498 		 * called with SIGCLD in the default state, reset it to catch
2499 		 * so that child death signals can come in.
2500 		 */
2501 		oldroutine = sigset(SIGCLD, childeath);
2502 		(void) pause();
2503 		(void) sigset(SIGCLD, oldroutine);
2504 		setimer(0);
2505 	}
2506 
2507 	if (childpid != 0) {
2508 
2509 		if (process == NULLPROC) {
2510 			/*
2511 			 * No proc table pointer specified so search
2512 			 * for a free slot.
2513 			 */
2514 			for (process = proc_table;  process->p_flags != 0 &&
2515 			    (process < proc_table + num_proc); process++)
2516 					;
2517 
2518 			if (process == (proc_table + num_proc)) {
2519 				int old_proc_table_size = num_proc;
2520 
2521 				/* Increase the process table size */
2522 				increase_proc_table_size();
2523 				if (old_proc_table_size == num_proc) {
2524 					/* didn't grow: memory failure */
2525 					return (NO_ROOM);
2526 				} else {
2527 					process =
2528 					    proc_table + old_proc_table_size;
2529 				}
2530 			}
2531 
2532 			process->p_time = 0L;
2533 			process->p_count = 0;
2534 		}
2535 		process->p_id[0] = '\0';
2536 		process->p_id[1] = '\0';
2537 		process->p_id[2] = '\0';
2538 		process->p_id[3] = '\0';
2539 		process->p_pid = childpid;
2540 		process->p_flags = (LIVING | OCCUPIED | modes);
2541 		process->p_exit = 0;
2542 
2543 		st_write();
2544 	} else {
2545 		if ((action & (M_WAIT | M_BOOTWAIT)) == 0)
2546 			(void) setpgrp();
2547 
2548 		process = NULLPROC;
2549 
2550 		/*
2551 		 * Reset all signals to the system defaults.
2552 		 */
2553 		for (i = SIGHUP; i <= SIGRTMAX; i++)
2554 			(void) sigset(i, SIG_DFL);
2555 
2556 		/*
2557 		 * POSIX B.2.2.2 advises that init should set SIGTTOU,
2558 		 * SIGTTIN, and SIGTSTP to SIG_IGN.
2559 		 *
2560 		 * Make sure that SIGXCPU and SIGXFSZ also remain ignored,
2561 		 * for backward compatibility.
2562 		 */
2563 		(void) sigset(SIGTTIN, SIG_IGN);
2564 		(void) sigset(SIGTTOU, SIG_IGN);
2565 		(void) sigset(SIGTSTP, SIG_IGN);
2566 		(void) sigset(SIGXCPU, SIG_IGN);
2567 		(void) sigset(SIGXFSZ, SIG_IGN);
2568 	}
2569 	return (process);
2570 }
2571 
2572 
2573 /*
2574  * waitproc() waits for a specified process to die.  For this function to
2575  * work, the specified process must already in the proc_table.  waitproc()
2576  * returns the exit status of the specified process when it dies.
2577  */
2578 static long
2579 waitproc(struct PROC_TABLE *process)
2580 {
2581 	int		answer;
2582 	sigset_t	oldmask, newmask, zeromask;
2583 
2584 	(void) sigemptyset(&zeromask);
2585 	(void) sigemptyset(&newmask);
2586 
2587 	(void) sigaddset(&newmask, SIGCLD);
2588 
2589 	/* Block SIGCLD and save the current signal mask */
2590 	if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
2591 		perror("SIG_BLOCK error");
2592 
2593 	/*
2594 	 * Wait around until the process dies.
2595 	 */
2596 	if (process->p_flags & LIVING)
2597 		(void) sigsuspend(&zeromask);
2598 
2599 	/* Reset signal mask to unblock SIGCLD */
2600 	if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
2601 		perror("SIG_SETMASK error");
2602 
2603 	if (process->p_flags & LIVING)
2604 		return (FAILURE);
2605 
2606 	/*
2607 	 * Make sure to only return 16 bits so that answer will always
2608 	 * be positive whenever the process of interest really died.
2609 	 */
2610 	answer = (process->p_exit & 0xffff);
2611 
2612 	/*
2613 	 * Free the slot in the proc_table.
2614 	 */
2615 	process->p_flags = 0;
2616 	return (answer);
2617 }
2618 
2619 /*
2620  * notify_pam_dead(): calls into the PAM framework to close the given session.
2621  */
2622 static void
2623 notify_pam_dead(struct utmpx *up)
2624 {
2625 	pam_handle_t *pamh;
2626 	char user[sizeof (up->ut_user) + 1];
2627 	char ttyn[sizeof (up->ut_line) + 1];
2628 	char host[sizeof (up->ut_host) + 1];
2629 
2630 	/*
2631 	 * PAM does not take care of updating utmpx/wtmpx.
2632 	 */
2633 	(void) snprintf(user, sizeof (user), "%s", up->ut_user);
2634 	(void) snprintf(ttyn, sizeof (ttyn), "%s", up->ut_line);
2635 	(void) snprintf(host, sizeof (host), "%s", up->ut_host);
2636 
2637 	if (pam_start("init", user, NULL, &pamh) == PAM_SUCCESS)  {
2638 		(void) pam_set_item(pamh, PAM_TTY, ttyn);
2639 		(void) pam_set_item(pamh, PAM_RHOST, host);
2640 		(void) pam_close_session(pamh, 0);
2641 		(void) pam_end(pamh, PAM_SUCCESS);
2642 	}
2643 }
2644 
2645 /*
2646  * Check you can access utmpx (As / may be read-only and
2647  * /var may not be mounted yet).
2648  */
2649 static int
2650 access_utmpx(void)
2651 {
2652 	do {
2653 		utmpx_ok = (access(UTMPX, R_OK|W_OK) == 0);
2654 	} while (!utmpx_ok && errno == EINTR);
2655 
2656 	return (utmpx_ok);
2657 }
2658 
2659 /*
2660  * account() updates entries in utmpx and appends new entries to the end of
2661  * wtmpx (assuming they exist).  The program argument indicates the name of
2662  * program if INIT_PROCESS, otherwise should be NULL.
2663  *
2664  * account() only blocks for INIT_PROCESS requests.
2665  *
2666  * Returns non-zero if write failed.
2667  */
2668 static int
2669 account(short state, struct PROC_TABLE *process, char *program)
2670 {
2671 	struct utmpx utmpbuf, *u, *oldu;
2672 	int tmplen;
2673 	char fail_buf[UT_LINE_SZ];
2674 	sigset_t block, unblock;
2675 
2676 	if (!utmpx_ok && !access_utmpx()) {
2677 		return (-1);
2678 	}
2679 
2680 	/*
2681 	 * Set up the prototype for the utmp structure we want to write.
2682 	 */
2683 	u = &utmpbuf;
2684 	(void) memset(u, 0, sizeof (struct utmpx));
2685 
2686 	/*
2687 	 * Fill in the various fields of the utmp structure.
2688 	 */
2689 	u->ut_id[0] = process->p_id[0];
2690 	u->ut_id[1] = process->p_id[1];
2691 	u->ut_id[2] = process->p_id[2];
2692 	u->ut_id[3] = process->p_id[3];
2693 	u->ut_pid = process->p_pid;
2694 
2695 	/*
2696 	 * Fill the "ut_exit" structure.
2697 	 */
2698 	u->ut_exit.e_termination = WTERMSIG(process->p_exit);
2699 	u->ut_exit.e_exit = WEXITSTATUS(process->p_exit);
2700 	u->ut_type = state;
2701 
2702 	(void) time(&u->ut_tv.tv_sec);
2703 
2704 	/*
2705 	 * Block signals for utmp update.
2706 	 */
2707 	(void) sigfillset(&block);
2708 	(void) sigprocmask(SIG_BLOCK, &block, &unblock);
2709 
2710 	/*
2711 	 * See if there already is such an entry in the "utmpx" file.
2712 	 */
2713 	setutxent();	/* Start at beginning of utmpx file. */
2714 
2715 	if ((oldu = getutxid(u)) != NULL) {
2716 		/*
2717 		 * Copy in the old "user", "line" and "host" fields
2718 		 * to our new structure.
2719 		 */
2720 		bcopy(oldu->ut_user, u->ut_user, sizeof (u->ut_user));
2721 		bcopy(oldu->ut_line, u->ut_line, sizeof (u->ut_line));
2722 		bcopy(oldu->ut_host, u->ut_host, sizeof (u->ut_host));
2723 		u->ut_syslen = (tmplen = strlen(u->ut_host)) ?
2724 		    min(tmplen + 1, sizeof (u->ut_host)) : 0;
2725 
2726 		if (oldu->ut_type == USER_PROCESS && state == DEAD_PROCESS) {
2727 			notify_pam_dead(oldu);
2728 		}
2729 	}
2730 
2731 	/*
2732 	 * Perform special accounting. Insert the special string into the
2733 	 * ut_line array. For INIT_PROCESSes put in the name of the
2734 	 * program in the "ut_user" field.
2735 	 */
2736 	switch (state) {
2737 	case INIT_PROCESS:
2738 		(void) strncpy(u->ut_user, program, sizeof (u->ut_user));
2739 		(void) strcpy(fail_buf, "INIT_PROCESS");
2740 		break;
2741 
2742 	default:
2743 		(void) strlcpy(fail_buf, u->ut_id, sizeof (u->ut_id) + 1);
2744 		break;
2745 	}
2746 
2747 	/*
2748 	 * Write out the updated entry to utmpx file.
2749 	 */
2750 	if (pututxline(u) == NULL) {
2751 		console(B_TRUE, "Failed write of utmpx entry: \"%s\": %s\n",
2752 		    fail_buf, strerror(errno));
2753 		endutxent();
2754 		(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
2755 		return (-1);
2756 	}
2757 
2758 	/*
2759 	 * If we're able to write to utmpx, then attempt to add to the
2760 	 * end of the wtmpx file.
2761 	 */
2762 	updwtmpx(WTMPX, u);
2763 
2764 	endutxent();
2765 
2766 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
2767 
2768 	return (0);
2769 }
2770 
2771 static void
2772 clearent(pid_t pid, short status)
2773 {
2774 	struct utmpx *up;
2775 	sigset_t block, unblock;
2776 
2777 	/*
2778 	 * Block signals for utmp update.
2779 	 */
2780 	(void) sigfillset(&block);
2781 	(void) sigprocmask(SIG_BLOCK, &block, &unblock);
2782 
2783 	/*
2784 	 * No error checking for now.
2785 	 */
2786 
2787 	setutxent();
2788 	while (up = getutxent()) {
2789 		if (up->ut_pid == pid) {
2790 			if (up->ut_type == DEAD_PROCESS) {
2791 				/*
2792 				 * Cleaned up elsewhere.
2793 				 */
2794 				continue;
2795 			}
2796 
2797 			notify_pam_dead(up);
2798 
2799 			up->ut_type = DEAD_PROCESS;
2800 			up->ut_exit.e_termination = WTERMSIG(status);
2801 			up->ut_exit.e_exit = WEXITSTATUS(status);
2802 			(void) time(&up->ut_tv.tv_sec);
2803 
2804 			(void) pututxline(up);
2805 			/*
2806 			 * Now attempt to add to the end of the
2807 			 * wtmp and wtmpx files.  Do not create
2808 			 * if they don't already exist.
2809 			 */
2810 			updwtmpx(WTMPX, up);
2811 
2812 			break;
2813 		}
2814 	}
2815 
2816 	endutxent();
2817 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
2818 }
2819 
2820 /*
2821  * prog_name() searches for the word or unix path name and
2822  * returns a pointer to the last element of the pathname.
2823  */
2824 static char *
2825 prog_name(char *string)
2826 {
2827 	char	*ptr, *ptr2;
2828 	/* XXX - utmp - fix name length */
2829 	static char word[_POSIX_LOGIN_NAME_MAX];
2830 
2831 	/*
2832 	 * Search for the first word skipping leading spaces and tabs.
2833 	 */
2834 	while (*string == ' ' || *string == '\t')
2835 		string++;
2836 
2837 	/*
2838 	 * If the first non-space non-tab character is not one allowed in
2839 	 * a word, return a pointer to a null string, otherwise parse the
2840 	 * pathname.
2841 	 */
2842 	if (*string != '.' && *string != '/' && *string != '_' &&
2843 	    (*string < 'a' || *string > 'z') &&
2844 	    (*string < 'A' || * string > 'Z') &&
2845 	    (*string < '0' || *string > '9'))
2846 		return ("");
2847 
2848 	/*
2849 	 * Parse the pathname looking forward for '/', ' ', '\t', '\n' or
2850 	 * '\0'.  Each time a '/' is found, move "ptr" to one past the
2851 	 * '/', thus when a ' ', '\t', '\n', or '\0' is found, "ptr" will
2852 	 * point to the last element of the pathname.
2853 	 */
2854 	for (ptr = string; *string != ' ' && *string != '\t' &&
2855 	    *string != '\n' && *string != '\0'; string++) {
2856 		if (*string == '/')
2857 			ptr = string+1;
2858 	}
2859 
2860 	/*
2861 	 * Copy out up to the size of the "ut_user" array into "word",
2862 	 * null terminate it and return a pointer to it.
2863 	 */
2864 	/* XXX - utmp - fix name length */
2865 	for (ptr2 = &word[0]; ptr2 < &word[_POSIX_LOGIN_NAME_MAX - 1] &&
2866 	    ptr < string; /* CSTYLED */)
2867 		*ptr2++ = *ptr++;
2868 
2869 	*ptr2 = '\0';
2870 	return (&word[0]);
2871 }
2872 
2873 
2874 /*
2875  * realcon() returns a nonzero value if there is a character device
2876  * associated with SYSCON that has the same device number as CONSOLE.
2877  */
2878 static int
2879 realcon()
2880 {
2881 	struct stat sconbuf, conbuf;
2882 
2883 	if (stat(SYSCON, &sconbuf) != -1 &&
2884 	    stat(CONSOLE, &conbuf) != -1 &&
2885 	    S_ISCHR(sconbuf.st_mode) &&
2886 	    S_ISCHR(conbuf.st_mode) &&
2887 	    sconbuf.st_rdev == conbuf.st_rdev) {
2888 		return (1);
2889 	} else {
2890 		return (0);
2891 	}
2892 }
2893 
2894 
2895 /*
2896  * get_ioctl_syscon() retrieves the SYSCON settings from the IOCTLSYSCON file.
2897  * Returns true if the IOCTLSYSCON file needs to be written (with
2898  * write_ioctl_syscon() below)
2899  */
2900 static int
2901 get_ioctl_syscon()
2902 {
2903 	FILE	*fp;
2904 	unsigned int	iflags, oflags, cflags, lflags, ldisc, cc[18];
2905 	int		i, valid_format = 0;
2906 
2907 	/*
2908 	 * Read in the previous modes for SYSCON from IOCTLSYSCON.
2909 	 */
2910 	if ((fp = fopen(IOCTLSYSCON, "r")) == NULL) {
2911 		stored_syscon_termios = dflt_termios;
2912 		console(B_TRUE,
2913 		    "warning:%s does not exist, default settings assumed\n",
2914 		    IOCTLSYSCON);
2915 	} else {
2916 
2917 	    i = fscanf(fp,
2918 	    "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x",
2919 		&iflags, &oflags, &cflags, &lflags,
2920 		&cc[0], &cc[1], &cc[2], &cc[3], &cc[4], &cc[5], &cc[6],
2921 		&cc[7], &cc[8], &cc[9], &cc[10], &cc[11], &cc[12], &cc[13],
2922 		&cc[14], &cc[15], &cc[16], &cc[17]);
2923 
2924 	    if (i == 22) {
2925 		stored_syscon_termios.c_iflag = iflags;
2926 		stored_syscon_termios.c_oflag = oflags;
2927 		stored_syscon_termios.c_cflag = cflags;
2928 		stored_syscon_termios.c_lflag = lflags;
2929 		for (i = 0; i < 18; i++)
2930 			stored_syscon_termios.c_cc[i] = (char)cc[i];
2931 		valid_format = 1;
2932 	    } else if (i == 13) {
2933 		rewind(fp);
2934 		i = fscanf(fp, "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x",
2935 		    &iflags, &oflags, &cflags, &lflags, &ldisc, &cc[0], &cc[1],
2936 		    &cc[2], &cc[3], &cc[4], &cc[5], &cc[6], &cc[7]);
2937 
2938 		/*
2939 		 * If the file is formatted properly, use the values to
2940 		 * initialize the console terminal condition.
2941 		 */
2942 		stored_syscon_termios.c_iflag = (ushort_t)iflags;
2943 		stored_syscon_termios.c_oflag = (ushort_t)oflags;
2944 		stored_syscon_termios.c_cflag = (ushort_t)cflags;
2945 		stored_syscon_termios.c_lflag = (ushort_t)lflags;
2946 		for (i = 0; i < 8; i++)
2947 			stored_syscon_termios.c_cc[i] = (char)cc[i];
2948 		valid_format = 1;
2949 	    }
2950 	    (void) fclose(fp);
2951 
2952 	    /* If the file is badly formatted, use the default settings. */
2953 	    if (!valid_format)
2954 		stored_syscon_termios = dflt_termios;
2955 	}
2956 
2957 	/* If the file had a bad format, rewrite it later. */
2958 	return (!valid_format);
2959 }
2960 
2961 
2962 static void
2963 write_ioctl_syscon()
2964 {
2965 	FILE *fp;
2966 	int i;
2967 
2968 	(void) unlink(SYSCON);
2969 	(void) link(SYSTTY, SYSCON);
2970 	(void) umask(022);
2971 	fp = fopen(IOCTLSYSCON, "w");
2972 
2973 	(void) fprintf(fp, "%x:%x:%x:%x:0", stored_syscon_termios.c_iflag,
2974 	    stored_syscon_termios.c_oflag, stored_syscon_termios.c_cflag,
2975 	    stored_syscon_termios.c_lflag);
2976 	for (i = 0; i < 8; ++i)
2977 		(void) fprintf(fp, ":%x", stored_syscon_termios.c_cc[i]);
2978 	(void) putc('\n', fp);
2979 
2980 	(void) fflush(fp);
2981 	(void) fsync(fileno(fp));
2982 	(void) fclose(fp);
2983 	(void) umask(cmask);
2984 }
2985 
2986 
2987 /*
2988  * void console(boolean_t, char *, ...)
2989  *   Outputs the requested message to the system console.  Note that the number
2990  *   of arguments passed to console() should be determined by the print format.
2991  *
2992  *   The "prefix" parameter indicates whether or not "INIT: " should precede the
2993  *   message.
2994  *
2995  *   To make sure we write to the console in a sane fashion, we use the modes
2996  *   we keep in stored_syscon_termios (which we read out of /etc/ioctl.syscon).
2997  *   Afterwards we restore whatever modes were already there.
2998  */
2999 /* PRINTFLIKE2 */
3000 static void
3001 console(boolean_t prefix, char *format, ...)
3002 {
3003 	char	outbuf[BUFSIZ];
3004 	va_list	args;
3005 	int fd, getret;
3006 	struct termios old_syscon_termios;
3007 	FILE *f;
3008 
3009 	/*
3010 	 * We open SYSCON anew each time in case it has changed (see
3011 	 * userinit()).
3012 	 */
3013 	if ((fd = open(SYSCON, O_RDWR | O_NOCTTY)) < 0 ||
3014 	    (f = fdopen(fd, "r+")) == NULL) {
3015 		if (prefix)
3016 			syslog(LOG_WARNING, "INIT: ");
3017 		va_start(args, format);
3018 		vsyslog(LOG_WARNING, format, args);
3019 		va_end(args);
3020 		if (fd >= 0)
3021 			(void) close(fd);
3022 		return;
3023 	}
3024 	setbuf(f, &outbuf[0]);
3025 
3026 	getret = tcgetattr(fd, &old_syscon_termios);
3027 	old_syscon_termios.c_cflag &= ~HUPCL;
3028 	if (realcon())
3029 		/* Don't overwrite cflag of real console. */
3030 		stored_syscon_termios.c_cflag = old_syscon_termios.c_cflag;
3031 
3032 	stored_syscon_termios.c_cflag &= ~HUPCL;
3033 
3034 	(void) tcsetattr(fd, TCSANOW, &stored_syscon_termios);
3035 
3036 	if (prefix)
3037 		(void) fprintf(f, "\nINIT: ");
3038 	va_start(args, format);
3039 	(void) vfprintf(f, format, args);
3040 	va_end(args);
3041 
3042 	if (getret == 0)
3043 		(void) tcsetattr(fd, TCSADRAIN, &old_syscon_termios);
3044 
3045 	(void) fclose(f);
3046 }
3047 
3048 /*
3049  * timer() is a substitute for sleep() which uses alarm() and pause().
3050  */
3051 static void
3052 timer(int waitime)
3053 {
3054 	setimer(waitime);
3055 	while (time_up == FALSE)
3056 		(void) pause();
3057 }
3058 
3059 static void
3060 setimer(int timelimit)
3061 {
3062 	alarmclk();
3063 	(void) alarm(timelimit);
3064 	time_up = (timelimit ? FALSE : TRUE);
3065 }
3066 
3067 /*
3068  * Fails with
3069  *   ENOMEM - out of memory
3070  *   ECONNABORTED - repository connection broken
3071  *   EPERM - permission denied
3072  *   EACCES - backend access denied
3073  *   EROFS - backend readonly
3074  */
3075 static int
3076 get_or_add_startd(scf_instance_t *inst)
3077 {
3078 	scf_handle_t *h;
3079 	scf_scope_t *scope = NULL;
3080 	scf_service_t *svc = NULL;
3081 	int ret = 0;
3082 
3083 	h = scf_instance_handle(inst);
3084 
3085 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
3086 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0)
3087 		return (0);
3088 
3089 	switch (scf_error()) {
3090 	case SCF_ERROR_CONNECTION_BROKEN:
3091 		return (ECONNABORTED);
3092 
3093 	case SCF_ERROR_NOT_FOUND:
3094 		break;
3095 
3096 	case SCF_ERROR_HANDLE_MISMATCH:
3097 	case SCF_ERROR_INVALID_ARGUMENT:
3098 	case SCF_ERROR_CONSTRAINT_VIOLATED:
3099 	default:
3100 		bad_error("scf_handle_decode_fmri", scf_error());
3101 	}
3102 
3103 	/* Make sure we're right, since we're adding piece-by-piece. */
3104 	assert(strcmp(SCF_SERVICE_STARTD,
3105 	    "svc:/system/svc/restarter:default") == 0);
3106 
3107 	if ((scope = scf_scope_create(h)) == NULL ||
3108 	    (svc = scf_service_create(h)) == NULL) {
3109 		ret = ENOMEM;
3110 		goto out;
3111 	}
3112 
3113 get_scope:
3114 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, scope) != 0) {
3115 		switch (scf_error()) {
3116 		case SCF_ERROR_CONNECTION_BROKEN:
3117 			ret = ECONNABORTED;
3118 			goto out;
3119 
3120 		case SCF_ERROR_NOT_FOUND:
3121 			(void) fputs(gettext(
3122 			    "smf(5) repository missing local scope.\n"),
3123 			    stderr);
3124 			exit(1);
3125 			/* NOTREACHED */
3126 
3127 		case SCF_ERROR_HANDLE_MISMATCH:
3128 		case SCF_ERROR_INVALID_ARGUMENT:
3129 		default:
3130 			bad_error("scf_handle_get_scope", scf_error());
3131 		}
3132 	}
3133 
3134 get_svc:
3135 	if (scf_scope_get_service(scope, "system/svc/restarter", svc) != 0) {
3136 		switch (scf_error()) {
3137 		case SCF_ERROR_CONNECTION_BROKEN:
3138 			ret = ECONNABORTED;
3139 			goto out;
3140 
3141 		case SCF_ERROR_DELETED:
3142 			goto get_scope;
3143 
3144 		case SCF_ERROR_NOT_FOUND:
3145 			break;
3146 
3147 		case SCF_ERROR_HANDLE_MISMATCH:
3148 		case SCF_ERROR_INVALID_ARGUMENT:
3149 		case SCF_ERROR_NOT_SET:
3150 		default:
3151 			bad_error("scf_scope_get_service", scf_error());
3152 		}
3153 
3154 add_svc:
3155 		if (scf_scope_add_service(scope, "system/svc/restarter", svc) !=
3156 		    0) {
3157 			switch (scf_error()) {
3158 			case SCF_ERROR_CONNECTION_BROKEN:
3159 				ret = ECONNABORTED;
3160 				goto out;
3161 
3162 			case SCF_ERROR_EXISTS:
3163 				goto get_svc;
3164 
3165 			case SCF_ERROR_PERMISSION_DENIED:
3166 				ret = EPERM;
3167 				goto out;
3168 
3169 			case SCF_ERROR_BACKEND_ACCESS:
3170 				ret = EACCES;
3171 				goto out;
3172 
3173 			case SCF_ERROR_BACKEND_READONLY:
3174 				ret = EROFS;
3175 				goto out;
3176 
3177 			case SCF_ERROR_HANDLE_MISMATCH:
3178 			case SCF_ERROR_INVALID_ARGUMENT:
3179 			case SCF_ERROR_NOT_SET:
3180 			default:
3181 				bad_error("scf_scope_add_service", scf_error());
3182 			}
3183 		}
3184 	}
3185 
3186 get_inst:
3187 	if (scf_service_get_instance(svc, "default", inst) != 0) {
3188 		switch (scf_error()) {
3189 		case SCF_ERROR_CONNECTION_BROKEN:
3190 			ret = ECONNABORTED;
3191 			goto out;
3192 
3193 		case SCF_ERROR_DELETED:
3194 			goto add_svc;
3195 
3196 		case SCF_ERROR_NOT_FOUND:
3197 			break;
3198 
3199 		case SCF_ERROR_HANDLE_MISMATCH:
3200 		case SCF_ERROR_INVALID_ARGUMENT:
3201 		case SCF_ERROR_NOT_SET:
3202 		default:
3203 			bad_error("scf_service_get_instance", scf_error());
3204 		}
3205 
3206 		if (scf_service_add_instance(svc, "default", inst) !=
3207 		    0) {
3208 			switch (scf_error()) {
3209 			case SCF_ERROR_CONNECTION_BROKEN:
3210 				ret = ECONNABORTED;
3211 				goto out;
3212 
3213 			case SCF_ERROR_DELETED:
3214 				goto add_svc;
3215 
3216 			case SCF_ERROR_EXISTS:
3217 				goto get_inst;
3218 
3219 			case SCF_ERROR_PERMISSION_DENIED:
3220 				ret = EPERM;
3221 				goto out;
3222 
3223 			case SCF_ERROR_BACKEND_ACCESS:
3224 				ret = EACCES;
3225 				goto out;
3226 
3227 			case SCF_ERROR_BACKEND_READONLY:
3228 				ret = EROFS;
3229 				goto out;
3230 
3231 			case SCF_ERROR_HANDLE_MISMATCH:
3232 			case SCF_ERROR_INVALID_ARGUMENT:
3233 			case SCF_ERROR_NOT_SET:
3234 			default:
3235 				bad_error("scf_service_add_instance",
3236 				    scf_error());
3237 			}
3238 		}
3239 	}
3240 
3241 	ret = 0;
3242 
3243 out:
3244 	scf_service_destroy(svc);
3245 	scf_scope_destroy(scope);
3246 	return (ret);
3247 }
3248 
3249 /*
3250  * Fails with
3251  *   ECONNABORTED - repository connection broken
3252  *   ECANCELED - the transaction's property group was deleted
3253  */
3254 static int
3255 transaction_add_set(scf_transaction_t *tx, scf_transaction_entry_t *ent,
3256     const char *pname, scf_type_t type)
3257 {
3258 change_type:
3259 	if (scf_transaction_property_change_type(tx, ent, pname, type) == 0)
3260 		return (0);
3261 
3262 	switch (scf_error()) {
3263 	case SCF_ERROR_CONNECTION_BROKEN:
3264 		return (ECONNABORTED);
3265 
3266 	case SCF_ERROR_DELETED:
3267 		return (ECANCELED);
3268 
3269 	case SCF_ERROR_NOT_FOUND:
3270 		goto new;
3271 
3272 	case SCF_ERROR_HANDLE_MISMATCH:
3273 	case SCF_ERROR_INVALID_ARGUMENT:
3274 	case SCF_ERROR_NOT_BOUND:
3275 	case SCF_ERROR_NOT_SET:
3276 	default:
3277 		bad_error("scf_transaction_property_change_type", scf_error());
3278 	}
3279 
3280 new:
3281 	if (scf_transaction_property_new(tx, ent, pname, type) == 0)
3282 		return (0);
3283 
3284 	switch (scf_error()) {
3285 	case SCF_ERROR_CONNECTION_BROKEN:
3286 		return (ECONNABORTED);
3287 
3288 	case SCF_ERROR_DELETED:
3289 		return (ECANCELED);
3290 
3291 	case SCF_ERROR_EXISTS:
3292 		goto change_type;
3293 
3294 	case SCF_ERROR_HANDLE_MISMATCH:
3295 	case SCF_ERROR_INVALID_ARGUMENT:
3296 	case SCF_ERROR_NOT_BOUND:
3297 	case SCF_ERROR_NOT_SET:
3298 	default:
3299 		bad_error("scf_transaction_property_new", scf_error());
3300 		/* NOTREACHED */
3301 	}
3302 }
3303 
3304 static void
3305 scferr(void)
3306 {
3307 	switch (scf_error()) {
3308 	case SCF_ERROR_NO_MEMORY:
3309 		console(B_TRUE, gettext("Out of memory.\n"));
3310 		break;
3311 
3312 	case SCF_ERROR_CONNECTION_BROKEN:
3313 		console(B_TRUE, gettext(
3314 		    "Connection to smf(5) repository server broken.\n"));
3315 		break;
3316 
3317 	case SCF_ERROR_NO_RESOURCES:
3318 		console(B_TRUE, gettext(
3319 		    "smf(5) repository server is out of memory.\n"));
3320 		break;
3321 
3322 	case SCF_ERROR_PERMISSION_DENIED:
3323 		console(B_TRUE, gettext("Insufficient privileges.\n"));
3324 		break;
3325 
3326 	default:
3327 		console(B_TRUE, gettext("libscf error: %s\n"),
3328 		    scf_strerror(scf_error()));
3329 	}
3330 }
3331 
3332 static void
3333 lscf_set_runlevel(char rl)
3334 {
3335 	scf_handle_t *h;
3336 	scf_instance_t *inst = NULL;
3337 	scf_propertygroup_t *pg = NULL;
3338 	scf_transaction_t *tx = NULL;
3339 	scf_transaction_entry_t *ent = NULL;
3340 	scf_value_t *val = NULL;
3341 	char buf[2];
3342 	int r;
3343 
3344 	h = scf_handle_create(SCF_VERSION);
3345 	if (h == NULL) {
3346 		scferr();
3347 		return;
3348 	}
3349 
3350 	if (scf_handle_bind(h) != 0) {
3351 		switch (scf_error()) {
3352 		case SCF_ERROR_NO_SERVER:
3353 			console(B_TRUE,
3354 			    gettext("smf(5) repository server not running.\n"));
3355 			goto bail;
3356 
3357 		default:
3358 			scferr();
3359 			goto bail;
3360 		}
3361 	}
3362 
3363 	if ((inst = scf_instance_create(h)) == NULL ||
3364 	    (pg = scf_pg_create(h)) == NULL ||
3365 	    (val = scf_value_create(h)) == NULL ||
3366 	    (tx = scf_transaction_create(h)) == NULL ||
3367 	    (ent = scf_entry_create(h)) == NULL) {
3368 		scferr();
3369 		goto bail;
3370 	}
3371 
3372 get_inst:
3373 	r = get_or_add_startd(inst);
3374 	switch (r) {
3375 	case 0:
3376 		break;
3377 
3378 	case ENOMEM:
3379 	case ECONNABORTED:
3380 	case EPERM:
3381 	case EACCES:
3382 	case EROFS:
3383 		scferr();
3384 		goto bail;
3385 	default:
3386 		bad_error("get_or_add_startd", r);
3387 	}
3388 
3389 get_pg:
3390 	if (scf_instance_get_pg(inst, SCF_PG_OPTIONS_OVR, pg) != 0) {
3391 		switch (scf_error()) {
3392 		case SCF_ERROR_CONNECTION_BROKEN:
3393 			scferr();
3394 			goto bail;
3395 
3396 		case SCF_ERROR_DELETED:
3397 			goto get_inst;
3398 
3399 		case SCF_ERROR_NOT_FOUND:
3400 			break;
3401 
3402 		case SCF_ERROR_HANDLE_MISMATCH:
3403 		case SCF_ERROR_INVALID_ARGUMENT:
3404 		case SCF_ERROR_NOT_SET:
3405 		default:
3406 			bad_error("scf_instance_get_pg", scf_error());
3407 		}
3408 
3409 add_pg:
3410 		if (scf_instance_add_pg(inst, SCF_PG_OPTIONS_OVR,
3411 		    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS, pg) !=
3412 		    0) {
3413 			switch (scf_error()) {
3414 			case SCF_ERROR_CONNECTION_BROKEN:
3415 			case SCF_ERROR_PERMISSION_DENIED:
3416 			case SCF_ERROR_BACKEND_ACCESS:
3417 				scferr();
3418 				goto bail;
3419 
3420 			case SCF_ERROR_DELETED:
3421 				goto get_inst;
3422 
3423 			case SCF_ERROR_EXISTS:
3424 				goto get_pg;
3425 
3426 			case SCF_ERROR_HANDLE_MISMATCH:
3427 			case SCF_ERROR_INVALID_ARGUMENT:
3428 			case SCF_ERROR_NOT_SET:
3429 			default:
3430 				bad_error("scf_instance_add_pg", scf_error());
3431 			}
3432 		}
3433 	}
3434 
3435 	buf[0] = rl;
3436 	buf[1] = '\0';
3437 	r = scf_value_set_astring(val, buf);
3438 	assert(r == 0);
3439 
3440 	for (;;) {
3441 		if (scf_transaction_start(tx, pg) != 0) {
3442 			switch (scf_error()) {
3443 			case SCF_ERROR_CONNECTION_BROKEN:
3444 			case SCF_ERROR_PERMISSION_DENIED:
3445 			case SCF_ERROR_BACKEND_ACCESS:
3446 				scferr();
3447 				goto bail;
3448 
3449 			case SCF_ERROR_DELETED:
3450 				goto add_pg;
3451 
3452 			case SCF_ERROR_HANDLE_MISMATCH:
3453 			case SCF_ERROR_NOT_BOUND:
3454 			case SCF_ERROR_IN_USE:
3455 			case SCF_ERROR_NOT_SET:
3456 			default:
3457 				bad_error("scf_transaction_start", scf_error());
3458 			}
3459 		}
3460 
3461 		r = transaction_add_set(tx, ent, "runlevel", SCF_TYPE_ASTRING);
3462 		switch (r) {
3463 		case 0:
3464 			break;
3465 
3466 		case ECONNABORTED:
3467 			scferr();
3468 			goto bail;
3469 
3470 		case ECANCELED:
3471 			scf_transaction_reset(tx);
3472 			goto add_pg;
3473 
3474 		default:
3475 			bad_error("transaction_add_set", r);
3476 		}
3477 
3478 		r = scf_entry_add_value(ent, val);
3479 		assert(r == 0);
3480 
3481 		r = scf_transaction_commit(tx);
3482 		if (r == 1)
3483 			break;
3484 
3485 		if (r != 0) {
3486 			switch (scf_error()) {
3487 			case SCF_ERROR_CONNECTION_BROKEN:
3488 			case SCF_ERROR_PERMISSION_DENIED:
3489 			case SCF_ERROR_BACKEND_ACCESS:
3490 			case SCF_ERROR_BACKEND_READONLY:
3491 				scferr();
3492 				goto bail;
3493 
3494 			case SCF_ERROR_DELETED:
3495 				scf_transaction_reset(tx);
3496 				goto add_pg;
3497 
3498 			case SCF_ERROR_INVALID_ARGUMENT:
3499 			case SCF_ERROR_NOT_BOUND:
3500 			case SCF_ERROR_NOT_SET:
3501 			default:
3502 				bad_error("scf_transaction_commit",
3503 				    scf_error());
3504 			}
3505 		}
3506 
3507 		scf_transaction_reset(tx);
3508 		(void) scf_pg_update(pg);
3509 	}
3510 
3511 bail:
3512 	scf_transaction_destroy(tx);
3513 	scf_entry_destroy(ent);
3514 	scf_value_destroy(val);
3515 	scf_pg_destroy(pg);
3516 	scf_instance_destroy(inst);
3517 
3518 	(void) scf_handle_unbind(h);
3519 	scf_handle_destroy(h);
3520 }
3521 
3522 /*
3523  * Function to handle requests from users to main init running as process 1.
3524  */
3525 static void
3526 userinit(int argc, char **argv)
3527 {
3528 	FILE	*fp;
3529 	char	*ln;
3530 	int	init_signal;
3531 	struct stat	sconbuf, conbuf;
3532 	const char *usage_msg = "Usage: init [0123456SsQqabc]\n";
3533 
3534 	/*
3535 	 * We are a user invoked init.  Is there an argument and is it
3536 	 * a single character?  If not, print usage message and quit.
3537 	 */
3538 	if (argc != 2 || argv[1][1] != '\0') {
3539 		(void) fprintf(stderr, usage_msg);
3540 		exit(0);
3541 	}
3542 
3543 	if ((init_signal = lvlname_to_state((char)argv[1][0])) == -1) {
3544 		(void) fprintf(stderr, usage_msg);
3545 		(void) audit_put_record(ADT_FAILURE, ADT_FAIL_VALUE_BAD_CMD,
3546 		    argv[1]);
3547 		exit(1);
3548 	}
3549 
3550 	if (init_signal == SINGLE_USER) {
3551 		/*
3552 		 * Make sure this process is talking to a legal tty line
3553 		 * and that /dev/syscon is linked to this line.
3554 		 */
3555 		ln = ttyname(0);	/* Get the name of tty */
3556 		if (ln == NULL) {
3557 			(void) fprintf(stderr,
3558 			    "Standard input not a tty line\n");
3559 			(void) audit_put_record(ADT_FAILURE,
3560 			    ADT_FAIL_VALUE_BAD_TTY, argv[1]);
3561 			exit(1);
3562 		}
3563 
3564 		if ((stat(ln, &sconbuf) != -1) &&
3565 		    (stat(SYSCON, &conbuf) == -1 ||
3566 		    sconbuf.st_rdev != conbuf.st_rdev)) {
3567 			/*
3568 			 * /dev/syscon needs to change.
3569 			 * Unlink /dev/syscon and relink it to the current line.
3570 			 */
3571 			if (lstat(SYSCON, &conbuf) != -1 &&
3572 			    unlink(SYSCON) == FAILURE) {
3573 				perror("Can't unlink /dev/syscon");
3574 				(void) fprintf(stderr,
3575 				    "Run command on the system console.\n");
3576 				(void) audit_put_record(ADT_FAILURE,
3577 				    ADT_FAIL_VALUE_PROGRAM, argv[1]);
3578 				exit(1);
3579 			}
3580 			if (symlink(ln, SYSCON) == FAILURE) {
3581 				(void) fprintf(stderr,
3582 				    "Can't symlink /dev/syscon to %s: %s", ln,
3583 				    strerror(errno));
3584 
3585 				/* Try to leave a syscon */
3586 				(void) link(SYSTTY, SYSCON);
3587 				(void) audit_put_record(ADT_FAILURE,
3588 				    ADT_FAIL_VALUE_PROGRAM, argv[1]);
3589 				exit(1);
3590 			}
3591 
3592 			/*
3593 			 * Try to leave a message on system console saying where
3594 			 * /dev/syscon is currently connected.
3595 			 */
3596 			if ((fp = fopen(SYSTTY, "r+")) != NULL) {
3597 				(void) fprintf(fp,
3598 				    "\n****	SYSCON CHANGED TO %s	****\n",
3599 				    ln);
3600 				(void) fclose(fp);
3601 			}
3602 		}
3603 	}
3604 
3605 	update_boot_archive(init_signal);
3606 
3607 	(void) audit_put_record(ADT_SUCCESS, ADT_SUCCESS, argv[1]);
3608 
3609 	/*
3610 	 * Signal init; init will take care of telling svc.startd.
3611 	 */
3612 	if (kill(init_pid, init_signal) == FAILURE) {
3613 		(void) fprintf(stderr, "Must be super-user\n");
3614 		(void) audit_put_record(ADT_FAILURE,
3615 		    ADT_FAIL_VALUE_AUTH, argv[1]);
3616 		exit(1);
3617 	}
3618 
3619 	exit(0);
3620 }
3621 
3622 
3623 #define	DELTA	25	/* Number of pidlist elements to allocate at a time */
3624 
3625 /* ARGSUSED */
3626 void
3627 sigpoll(int n)
3628 {
3629 	struct pidrec prec;
3630 	struct pidrec *p = &prec;
3631 	struct pidlist *plp;
3632 	struct pidlist *tp, *savetp;
3633 	int i;
3634 
3635 	if (Pfd < 0) {
3636 		return;
3637 	}
3638 
3639 	for (;;) {
3640 		/*
3641 		 * Important Note: Either read will really fail (in which case
3642 		 * return is all we can do) or will get EAGAIN (Pfd was opened
3643 		 * O_NDELAY), in which case we also want to return.
3644 		 * Always return from here!
3645 		 */
3646 		if (read(Pfd, p, sizeof (struct pidrec)) !=
3647 						sizeof (struct pidrec)) {
3648 			return;
3649 		}
3650 		switch (p->pd_type) {
3651 
3652 		case ADDPID:
3653 			/*
3654 			 * New "godchild", add to list.
3655 			 */
3656 			if (Plfree == NULL) {
3657 				plp = (struct pidlist *)calloc(DELTA,
3658 				    sizeof (struct pidlist));
3659 				if (plp == NULL) {
3660 					/* Can't save pid */
3661 					break;
3662 				}
3663 				/*
3664 				 * Point at 2nd record allocated, we'll use plp.
3665 				 */
3666 				tp = plp + 1;
3667 				/*
3668 				 * Link them into a chain.
3669 				 */
3670 				Plfree = tp;
3671 				for (i = 0; i < DELTA - 2; i++) {
3672 					tp->pl_next = tp + 1;
3673 					tp++;
3674 				}
3675 			} else {
3676 				plp = Plfree;
3677 				Plfree = plp->pl_next;
3678 			}
3679 			plp->pl_pid = p->pd_pid;
3680 			plp->pl_dflag = 0;
3681 			plp->pl_next = NULL;
3682 			/*
3683 			 * Note - pid list is kept in increasing order of pids.
3684 			 */
3685 			if (Plhead == NULL) {
3686 				Plhead = plp;
3687 				/* Back up to read next record */
3688 				break;
3689 			} else {
3690 				savetp = tp = Plhead;
3691 				while (tp) {
3692 					if (plp->pl_pid > tp->pl_pid) {
3693 						savetp = tp;
3694 						tp = tp->pl_next;
3695 						continue;
3696 					} else if (plp->pl_pid < tp->pl_pid) {
3697 						if (tp == Plhead) {
3698 							plp->pl_next = Plhead;
3699 							Plhead = plp;
3700 						} else {
3701 							plp->pl_next =
3702 							    savetp->pl_next;
3703 							savetp->pl_next = plp;
3704 						}
3705 						break;
3706 					} else {
3707 						/* Already in list! */
3708 						plp->pl_next = Plfree;
3709 						Plfree = plp;
3710 						break;
3711 					}
3712 				}
3713 				if (tp == NULL) {
3714 					/* Add to end of list */
3715 					savetp->pl_next = plp;
3716 				}
3717 			}
3718 			/* Back up to read next record. */
3719 			break;
3720 
3721 		case REMPID:
3722 			/*
3723 			 * This one was handled by someone else,
3724 			 * purge it from the list.
3725 			 */
3726 			if (Plhead == NULL) {
3727 				/* Back up to read next record. */
3728 				break;
3729 			}
3730 			savetp = tp = Plhead;
3731 			while (tp) {
3732 				if (p->pd_pid > tp->pl_pid) {
3733 					/* Keep on looking. */
3734 					savetp = tp;
3735 					tp = tp->pl_next;
3736 					continue;
3737 				} else if (p->pd_pid < tp->pl_pid) {
3738 					/* Not in list. */
3739 					break;
3740 				} else {
3741 					/* Found it. */
3742 					if (tp == Plhead)
3743 						Plhead = tp->pl_next;
3744 					else
3745 						savetp->pl_next = tp->pl_next;
3746 					tp->pl_next = Plfree;
3747 					Plfree = tp;
3748 					break;
3749 				}
3750 			}
3751 			/* Back up to read next record. */
3752 			break;
3753 		default:
3754 			console(B_TRUE, "Bad message on initpipe\n");
3755 			break;
3756 		}
3757 	}
3758 }
3759 
3760 
3761 static void
3762 cleanaux()
3763 {
3764 	struct pidlist *savep, *p;
3765 	pid_t	pid;
3766 	short	status;
3767 
3768 	(void) sigset(SIGCLD, SIG_DFL);
3769 	Gchild = 0;	/* Note - Safe to do this here since no SIGCLDs */
3770 	(void) sighold(SIGPOLL);
3771 	savep = p = Plhead;
3772 	while (p) {
3773 		if (p->pl_dflag) {
3774 			/*
3775 			 * Found an entry to delete,
3776 			 * remove it from list first.
3777 			 */
3778 			pid = p->pl_pid;
3779 			status = p->pl_exit;
3780 			if (p == Plhead) {
3781 				Plhead = p->pl_next;
3782 				p->pl_next = Plfree;
3783 				Plfree = p;
3784 				savep = p = Plhead;
3785 			} else {
3786 				savep->pl_next = p->pl_next;
3787 				p->pl_next = Plfree;
3788 				Plfree = p;
3789 				p = savep->pl_next;
3790 			}
3791 			clearent(pid, status);
3792 			continue;
3793 		}
3794 		savep = p;
3795 		p = p->pl_next;
3796 	}
3797 	(void) sigrelse(SIGPOLL);
3798 	(void) sigset(SIGCLD, childeath);
3799 }
3800 
3801 
3802 /*
3803  * /etc/inittab has more entries and we have run out of room in the proc_table
3804  * array. Double the size of proc_table to accomodate the extra entries.
3805  */
3806 static void
3807 increase_proc_table_size()
3808 {
3809 	sigset_t block, unblock;
3810 	void *ptr;
3811 	size_t delta = num_proc * sizeof (struct PROC_TABLE);
3812 
3813 
3814 	/*
3815 	 * Block signals for realloc.
3816 	 */
3817 	(void) sigfillset(&block);
3818 	(void) sigprocmask(SIG_BLOCK, &block, &unblock);
3819 
3820 
3821 	/*
3822 	 * On failure we just return because callers of this function check
3823 	 * for failure.
3824 	 */
3825 	do
3826 		ptr = realloc(g_state, g_state_sz + delta);
3827 	while (ptr == NULL && errno == EAGAIN);
3828 
3829 	if (ptr != NULL) {
3830 		/* ensure that the new part is initialized to zero */
3831 		bzero((caddr_t)ptr + g_state_sz, delta);
3832 
3833 		g_state = ptr;
3834 		g_state_sz += delta;
3835 		num_proc <<= 1;
3836 	}
3837 
3838 
3839 	/* unblock our signals before returning */
3840 	(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
3841 }
3842 
3843 
3844 
3845 /*
3846  * Sanity check g_state.
3847  */
3848 static int
3849 st_sane()
3850 {
3851 	int i;
3852 	struct PROC_TABLE *ptp;
3853 
3854 
3855 	/* Note: cur_state is encoded as a signal number */
3856 	if (cur_state < 1 || cur_state == 9 || cur_state > 13)
3857 		return (0);
3858 
3859 	/* Check num_proc */
3860 	if (g_state_sz != sizeof (struct init_state) + (num_proc - 1) *
3861 	    sizeof (struct PROC_TABLE))
3862 		return (0);
3863 
3864 	/* Check proc_table */
3865 	for (i = 0, ptp = proc_table; i < num_proc; ++i, ++ptp) {
3866 		/* skip unoccupied entries */
3867 		if (!(ptp->p_flags & OCCUPIED))
3868 			continue;
3869 
3870 		/* p_flags has no bits outside of PF_MASK */
3871 		if (ptp->p_flags & ~(PF_MASK))
3872 			return (0);
3873 
3874 		/* 5 <= pid <= MAXPID */
3875 		if (ptp->p_pid < 5 || ptp->p_pid > MAXPID)
3876 			return (0);
3877 
3878 		/* p_count >= 0 */
3879 		if (ptp->p_count < 0)
3880 			return (0);
3881 
3882 		/* p_time >= 0 */
3883 		if (ptp->p_time < 0)
3884 			return (0);
3885 	}
3886 
3887 	return (1);
3888 }
3889 
3890 /*
3891  * Initialize our state.
3892  *
3893  * If the system just booted, then init_state_file, which is located on an
3894  * everpresent tmpfs filesystem, should not exist.
3895  *
3896  * If we were restarted, then init_state_file should exist, in
3897  * which case we'll read it in, sanity check it, and use it.
3898  *
3899  * Note: You can't call console() until proc_table is ready.
3900  */
3901 void
3902 st_init()
3903 {
3904 	struct stat stb;
3905 	int ret, st_fd, insane = 0;
3906 	size_t to_be_read;
3907 	char *ptr;
3908 
3909 
3910 	booting = 1;
3911 
3912 	do {
3913 		/*
3914 		 * If we can exclusively create the file, then we're the
3915 		 * initial invocation of init(1M).
3916 		 */
3917 		st_fd = open(init_state_file, O_RDWR | O_CREAT | O_EXCL,
3918 		    S_IRUSR | S_IWUSR);
3919 	} while (st_fd == -1 && errno == EINTR);
3920 	if (st_fd != -1)
3921 		goto new_state;
3922 
3923 	booting = 0;
3924 
3925 	do {
3926 		st_fd = open(init_state_file, O_RDWR, S_IRUSR | S_IWUSR);
3927 	} while (st_fd == -1 && errno == EINTR);
3928 	if (st_fd == -1)
3929 		goto new_state;
3930 
3931 	/* Get the size of the file. */
3932 	do
3933 		ret = fstat(st_fd, &stb);
3934 	while (ret == -1 && errno == EINTR);
3935 	if (ret == -1)
3936 		goto new_state;
3937 
3938 	do
3939 		g_state = malloc(stb.st_size);
3940 	while (g_state == NULL && errno == EAGAIN);
3941 	if (g_state == NULL)
3942 		goto new_state;
3943 
3944 	to_be_read = stb.st_size;
3945 	ptr = (char *)g_state;
3946 	while (to_be_read > 0) {
3947 		ssize_t read_ret;
3948 
3949 		read_ret = read(st_fd, ptr, to_be_read);
3950 		if (read_ret < 0) {
3951 			if (errno == EINTR)
3952 				continue;
3953 
3954 			goto new_state;
3955 		}
3956 
3957 		to_be_read -= read_ret;
3958 		ptr += read_ret;
3959 	}
3960 
3961 	(void) close(st_fd);
3962 
3963 	g_state_sz = stb.st_size;
3964 
3965 	if (st_sane()) {
3966 		console(B_TRUE, "Restarting.\n");
3967 		return;
3968 	}
3969 
3970 	insane = 1;
3971 
3972 new_state:
3973 	if (st_fd >= 0)
3974 		(void) close(st_fd);
3975 	else
3976 		(void) unlink(init_state_file);
3977 
3978 	if (g_state != NULL)
3979 		free(g_state);
3980 
3981 	/* Something went wrong, so allocate new state. */
3982 	g_state_sz = sizeof (struct init_state) +
3983 	    ((init_num_proc - 1) * sizeof (struct PROC_TABLE));
3984 	do
3985 		g_state = calloc(1, g_state_sz);
3986 	while (g_state == NULL && errno == EAGAIN);
3987 	if (g_state == NULL) {
3988 		/* Fatal error! */
3989 		exit(errno);
3990 	}
3991 
3992 	g_state->ist_runlevel = -1;
3993 	num_proc = init_num_proc;
3994 
3995 	if (!booting) {
3996 		console(B_TRUE, "Restarting.\n");
3997 
3998 		/* Overwrite the bad state file. */
3999 		st_write();
4000 
4001 		if (!insane) {
4002 			console(B_TRUE,
4003 			    "Error accessing persistent state file `%s'.  "
4004 			    "Ignored.\n", init_state_file);
4005 		} else {
4006 			console(B_TRUE,
4007 			    "Persistent state file `%s' is invalid and was "
4008 			    "ignored.\n", init_state_file);
4009 		}
4010 	}
4011 }
4012 
4013 /*
4014  * Write g_state out to the state file.
4015  */
4016 void
4017 st_write()
4018 {
4019 	static int complained = 0;
4020 
4021 	int st_fd;
4022 	char *cp;
4023 	size_t sz;
4024 	ssize_t ret;
4025 
4026 
4027 	do {
4028 		st_fd = open(init_next_state_file,
4029 		    O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
4030 	} while (st_fd < 0 && errno == EINTR);
4031 	if (st_fd < 0)
4032 		goto err;
4033 
4034 	cp = (char *)g_state;
4035 	sz = g_state_sz;
4036 	while (sz > 0) {
4037 		ret = write(st_fd, cp, sz);
4038 		if (ret < 0) {
4039 			if (errno == EINTR)
4040 				continue;
4041 
4042 			goto err;
4043 		}
4044 
4045 		sz -= ret;
4046 		cp += ret;
4047 	}
4048 
4049 	(void) close(st_fd);
4050 	st_fd = -1;
4051 	if (rename(init_next_state_file, init_state_file)) {
4052 		(void) unlink(init_next_state_file);
4053 		goto err;
4054 	}
4055 	complained = 0;
4056 
4057 	return;
4058 
4059 err:
4060 	if (st_fd >= 0)
4061 		(void) close(st_fd);
4062 
4063 	if (!booting && !complained) {
4064 		/*
4065 		 * Only complain after the filesystem should have come up.
4066 		 * And only do it once so we don't loop between console()
4067 		 * & efork().
4068 		 */
4069 		complained = 1;
4070 		if (st_fd)
4071 			console(B_TRUE, "Couldn't write persistent state "
4072 			    "file `%s'.\n", init_state_file);
4073 		else
4074 			console(B_TRUE, "Couldn't move persistent state "
4075 			    "file `%s' to `%s'.\n", init_next_state_file,
4076 			    init_state_file);
4077 	}
4078 }
4079 
4080 /*
4081  * Create a contract with these parameters.
4082  */
4083 static int
4084 contract_make_template(uint_t info, uint_t critical, uint_t fatal,
4085     uint64_t cookie)
4086 {
4087 	int fd, err;
4088 
4089 	char *ioctl_tset_emsg =
4090 	    "Couldn't set \"%s\" contract template parameter: %s.\n";
4091 
4092 	do
4093 		fd = open64(CTFS_ROOT "/process/template", O_RDWR);
4094 	while (fd < 0 && errno == EINTR);
4095 	if (fd < 0) {
4096 		console(B_TRUE, "Couldn't create process template: %s.\n",
4097 		    strerror(errno));
4098 		return (-1);
4099 	}
4100 
4101 	if (err = ct_pr_tmpl_set_param(fd, CT_PR_INHERIT | CT_PR_REGENT))
4102 		console(B_TRUE, "Contract set template inherit, regent "
4103 		    "failed: %s.\n", strerror(err));
4104 
4105 	/*
4106 	 * These errors result in a misconfigured template, which is better
4107 	 * than no template at all, so warn but don't abort.
4108 	 */
4109 	if (err = ct_tmpl_set_informative(fd, info))
4110 		console(B_TRUE, ioctl_tset_emsg, "informative", strerror(err));
4111 
4112 	if (err = ct_tmpl_set_critical(fd, critical))
4113 		console(B_TRUE, ioctl_tset_emsg, "critical", strerror(err));
4114 
4115 	if (err = ct_pr_tmpl_set_fatal(fd, fatal))
4116 		console(B_TRUE, ioctl_tset_emsg, "fatal", strerror(err));
4117 
4118 	if (err = ct_tmpl_set_cookie(fd, cookie))
4119 		console(B_TRUE, ioctl_tset_emsg, "cookie", strerror(err));
4120 
4121 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
4122 
4123 	return (fd);
4124 }
4125 
4126 /*
4127  * Create the templates and open an event file descriptor.  We use dup2(2) to
4128  * get these descriptors away from the stdin/stdout/stderr group.
4129  */
4130 static void
4131 contracts_init()
4132 {
4133 	int err, fd;
4134 
4135 	/*
4136 	 * Create & configure a legacy template.  We only want empty events so
4137 	 * we know when to abandon them.
4138 	 */
4139 	legacy_tmpl = contract_make_template(0, CT_PR_EV_EMPTY, CT_PR_EV_HWERR,
4140 	    ORDINARY_COOKIE);
4141 	if (legacy_tmpl >= 0) {
4142 		err = ct_tmpl_activate(legacy_tmpl);
4143 		if (err != 0) {
4144 			(void) close(legacy_tmpl);
4145 			legacy_tmpl = -1;
4146 			console(B_TRUE,
4147 			    "Couldn't activate legacy template (%s); "
4148 			    "legacy services will be in init's contract.\n",
4149 			    strerror(err));
4150 		}
4151 	} else
4152 		console(B_TRUE,
4153 		    "Legacy services will be in init's contract.\n");
4154 
4155 	if (dup2(legacy_tmpl, 255) == -1) {
4156 		console(B_TRUE, "Could not duplicate legacy template: %s.\n",
4157 		    strerror(errno));
4158 	} else {
4159 		(void) close(legacy_tmpl);
4160 		legacy_tmpl = 255;
4161 	}
4162 
4163 	(void) fcntl(legacy_tmpl, F_SETFD, FD_CLOEXEC);
4164 
4165 	startd_tmpl = contract_make_template(0, CT_PR_EV_EMPTY,
4166 	    CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE, STARTD_COOKIE);
4167 
4168 	if (dup2(startd_tmpl, 254) == -1) {
4169 		console(B_TRUE, "Could not duplicate startd template: %s.\n",
4170 		    strerror(errno));
4171 	} else {
4172 		(void) close(startd_tmpl);
4173 		startd_tmpl = 254;
4174 	}
4175 
4176 	(void) fcntl(startd_tmpl, F_SETFD, FD_CLOEXEC);
4177 
4178 	if (legacy_tmpl < 0 && startd_tmpl < 0) {
4179 		/* The creation errors have already been reported. */
4180 		console(B_TRUE,
4181 		    "Ignoring contract events.  Core smf(5) services will not "
4182 		    "be restarted.\n");
4183 		return;
4184 	}
4185 
4186 	/*
4187 	 * Open an event endpoint.
4188 	 */
4189 	do
4190 		fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
4191 	while (fd < 0 && errno == EINTR);
4192 	if (fd < 0) {
4193 		console(B_TRUE,
4194 		    "Couldn't open process pbundle: %s.  Core smf(5) services "
4195 		    "will not be restarted.\n", strerror(errno));
4196 		return;
4197 	}
4198 
4199 	if (dup2(fd, 253) == -1) {
4200 		console(B_TRUE, "Could not duplicate process bundle: %s.\n",
4201 		    strerror(errno));
4202 	} else {
4203 		(void) close(fd);
4204 		fd = 253;
4205 	}
4206 
4207 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
4208 
4209 	/* Reset in case we've been restarted. */
4210 	(void) ct_event_reset(fd);
4211 
4212 	poll_fds[0].fd = fd;
4213 	poll_fds[0].events = POLLIN;
4214 	poll_nfds = 1;
4215 }
4216 
4217 static int
4218 contract_getfile(ctid_t id, const char *name, int oflag)
4219 {
4220 	int fd;
4221 
4222 	do
4223 		fd = contract_open(id, "process", name, oflag);
4224 	while (fd < 0 && errno == EINTR);
4225 
4226 	if (fd < 0)
4227 		console(B_TRUE, "Couldn't open %s for contract %ld: %s.\n",
4228 		    name, id, strerror(errno));
4229 
4230 	return (fd);
4231 }
4232 
4233 static int
4234 contract_cookie(ctid_t id, uint64_t *cp)
4235 {
4236 	int fd, err;
4237 	ct_stathdl_t sh;
4238 
4239 	fd = contract_getfile(id, "status", O_RDONLY);
4240 	if (fd < 0)
4241 		return (-1);
4242 
4243 	err = ct_status_read(fd, CTD_COMMON, &sh);
4244 	if (err != 0) {
4245 		console(B_TRUE, "Couldn't read status of contract %ld: %s.\n",
4246 		    id, strerror(err));
4247 		(void) close(fd);
4248 		return (-1);
4249 	}
4250 
4251 	(void) close(fd);
4252 
4253 	*cp = ct_status_get_cookie(sh);
4254 
4255 	ct_status_free(sh);
4256 	return (0);
4257 }
4258 
4259 static void
4260 contract_ack(ct_evthdl_t e)
4261 {
4262 	int fd;
4263 
4264 	if (ct_event_get_flags(e) & CTE_INFO)
4265 		return;
4266 
4267 	fd = contract_getfile(ct_event_get_ctid(e), "ctl", O_WRONLY);
4268 	if (fd < 0)
4269 		return;
4270 
4271 	(void) ct_ctl_ack(fd, ct_event_get_evid(e));
4272 	(void) close(fd);
4273 }
4274 
4275 /*
4276  * Process a contract event.
4277  */
4278 static void
4279 contract_event(struct pollfd *poll)
4280 {
4281 	ct_evthdl_t e;
4282 	int err;
4283 	ctid_t ctid;
4284 
4285 	if (!(poll->revents & POLLIN)) {
4286 		if (poll->revents & POLLERR)
4287 			console(B_TRUE,
4288 			    "Unknown poll error on my process contract "
4289 			    "pbundle.\n");
4290 		return;
4291 	}
4292 
4293 	err = ct_event_read(poll->fd, &e);
4294 	if (err != 0) {
4295 		console(B_TRUE, "Error retrieving contract event: %s.\n",
4296 		    strerror(err));
4297 		return;
4298 	}
4299 
4300 	ctid = ct_event_get_ctid(e);
4301 
4302 	if (ct_event_get_type(e) == CT_PR_EV_EMPTY) {
4303 		uint64_t cookie;
4304 		int ret, abandon = 1;
4305 
4306 		/* If it's svc.startd, restart it.  Else, abandon. */
4307 		ret = contract_cookie(ctid, &cookie);
4308 
4309 		if (ret == 0) {
4310 			if (cookie == STARTD_COOKIE &&
4311 			    do_restart_startd) {
4312 				if (smf_debug)
4313 					console(B_TRUE, "Restarting "
4314 					    "svc.startd.\n");
4315 
4316 				/*
4317 				 * Account for the failure.  If the failure rate
4318 				 * exceeds a threshold, then drop to maintenance
4319 				 * mode.
4320 				 */
4321 				startd_record_failure();
4322 				if (startd_failure_rate_critical())
4323 					enter_maintenance();
4324 
4325 				if (startd_tmpl < 0)
4326 					console(B_TRUE,
4327 					    "Restarting svc.startd in "
4328 					    "improper contract (bad "
4329 					    "template).\n");
4330 
4331 				(void) startd_run(startd_cline, startd_tmpl,
4332 				    ctid);
4333 
4334 				abandon = 0;
4335 			}
4336 		}
4337 
4338 		if (abandon && (err = contract_abandon_id(ctid))) {
4339 			console(B_TRUE, "Couldn't abandon contract %ld: %s.\n",
4340 			    ctid, strerror(err));
4341 		}
4342 
4343 		/*
4344 		 * No need to acknowledge the event since either way the
4345 		 * originating contract should be abandoned.
4346 		 */
4347 	} else {
4348 		console(B_TRUE,
4349 		    "Received contract event of unexpected type %d from "
4350 		    "contract %ld.\n", ct_event_get_type(e), ctid);
4351 
4352 		if ((ct_event_get_flags(e) & (CTE_INFO | CTE_ACK)) == 0)
4353 			/* Allow unexpected critical events to be released. */
4354 			contract_ack(e);
4355 	}
4356 
4357 	ct_event_free(e);
4358 }
4359 
4360 /*
4361  * svc.startd(1M) Management
4362  */
4363 
4364 /*
4365  * (Re)start svc.startd(1M).  old_ctid should be the contract ID of the old
4366  * contract, or 0 if we're starting it for the first time.  If wait is true
4367  * we'll wait for and return the exit value of the child.
4368  */
4369 static int
4370 startd_run(const char *cline, int tmpl, ctid_t old_ctid)
4371 {
4372 	int err, i, ret, did_activate;
4373 	pid_t pid;
4374 	struct stat sb;
4375 
4376 	if (cline[0] == '\0')
4377 		return (-1);
4378 
4379 	/*
4380 	 * Don't restart startd if the system is rebooting or shutting down.
4381 	 */
4382 	do {
4383 		ret = stat("/etc/svc/volatile/resetting", &sb);
4384 	} while (ret == -1 && errno == EINTR);
4385 
4386 	if (ret == 0) {
4387 		if (smf_debug)
4388 			console(B_TRUE, "Quiescing for reboot.\n");
4389 		(void) pause();
4390 		return (-1);
4391 	}
4392 
4393 	err = ct_pr_tmpl_set_transfer(tmpl, old_ctid);
4394 	if (err == EINVAL) {
4395 		console(B_TRUE, "Remake startd_tmpl; reattempt transfer.\n");
4396 		tmpl = startd_tmpl = contract_make_template(0, CT_PR_EV_EMPTY,
4397 		    CT_PR_EV_HWERR, STARTD_COOKIE);
4398 
4399 		err = ct_pr_tmpl_set_transfer(tmpl, old_ctid);
4400 	}
4401 	if (err != 0) {
4402 		console(B_TRUE,
4403 		    "Couldn't set transfer parameter of contract template: "
4404 		    "%s.\n", strerror(err));
4405 	}
4406 
4407 	if ((err = ct_pr_tmpl_set_svc_fmri(startd_tmpl,
4408 	    SCF_SERVICE_STARTD)) != 0)
4409 		console(B_TRUE,
4410 		    "Can not set svc_fmri in contract template: %s\n",
4411 		    strerror(err));
4412 	if ((err = ct_pr_tmpl_set_svc_aux(startd_tmpl,
4413 	    startd_svc_aux)) != 0)
4414 		console(B_TRUE,
4415 		    "Can not set svc_aux in contract template: %s\n",
4416 		    strerror(err));
4417 	did_activate = !(ct_tmpl_activate(tmpl));
4418 	if (!did_activate)
4419 		console(B_TRUE,
4420 		    "Template activation failed; not starting \"%s\" in "
4421 		    "proper contract.\n", cline);
4422 
4423 	/* Hold SIGCHLD so we can wait if necessary. */
4424 	(void) sighold(SIGCHLD);
4425 
4426 	while ((pid = fork()) < 0) {
4427 		if (errno == EPERM) {
4428 			console(B_TRUE, "Insufficient permission to fork.\n");
4429 
4430 			/* Now that's a doozy. */
4431 			exit(1);
4432 		}
4433 
4434 		console(B_TRUE,
4435 		    "fork() for svc.startd failed: %s.  Will retry in 1 "
4436 		    "second...\n", strerror(errno));
4437 
4438 		(void) sleep(1);
4439 
4440 		/* Eventually give up? */
4441 	}
4442 
4443 	if (pid == 0) {
4444 		/* child */
4445 
4446 		/* See the comment in efork() */
4447 		for (i = SIGHUP; i <= SIGRTMAX; ++i) {
4448 			if (i == SIGTTOU || i == SIGTTIN || i == SIGTSTP)
4449 				(void) sigset(i, SIG_IGN);
4450 			else
4451 				(void) sigset(i, SIG_DFL);
4452 		}
4453 
4454 		if (smf_options != NULL) {
4455 			/* Put smf_options in the environment. */
4456 			glob_envp[glob_envn] =
4457 			    malloc(sizeof ("SMF_OPTIONS=") - 1 +
4458 			    strlen(smf_options) + 1);
4459 
4460 			if (glob_envp[glob_envn] != NULL) {
4461 				/* LINTED */
4462 				(void) sprintf(glob_envp[glob_envn],
4463 				    "SMF_OPTIONS=%s", smf_options);
4464 				glob_envp[glob_envn+1] = NULL;
4465 			} else {
4466 				console(B_TRUE,
4467 				    "Could not set SMF_OPTIONS (%s).\n",
4468 				    strerror(errno));
4469 			}
4470 		}
4471 
4472 		if (smf_debug)
4473 			console(B_TRUE, "Executing svc.startd\n");
4474 
4475 		(void) execle(SH, "INITSH", "-c", cline, NULL, glob_envp);
4476 
4477 		console(B_TRUE, "Could not exec \"%s\" (%s).\n", SH,
4478 		    strerror(errno));
4479 
4480 		exit(1);
4481 	}
4482 
4483 	/* parent */
4484 
4485 	if (did_activate) {
4486 		if (legacy_tmpl < 0 || ct_tmpl_activate(legacy_tmpl) != 0)
4487 			(void) ct_tmpl_clear(tmpl);
4488 	}
4489 
4490 	/* Clear the old_ctid reference so the kernel can reclaim it. */
4491 	if (old_ctid != 0)
4492 		(void) ct_pr_tmpl_set_transfer(tmpl, 0);
4493 
4494 	(void) sigrelse(SIGCHLD);
4495 
4496 	return (0);
4497 }
4498 
4499 /*
4500  * void startd_record_failure(void)
4501  *   Place the current time in our circular array of svc.startd failures.
4502  */
4503 void
4504 startd_record_failure()
4505 {
4506 	int index = startd_failure_index++ % NSTARTD_FAILURE_TIMES;
4507 
4508 	startd_failure_time[index] = gethrtime();
4509 }
4510 
4511 /*
4512  * int startd_failure_rate_critical(void)
4513  *   Return true if the average failure interval is less than the permitted
4514  *   interval.  Implicit success if insufficient measurements for an average
4515  *   exist.
4516  */
4517 int
4518 startd_failure_rate_critical()
4519 {
4520 	int n = startd_failure_index;
4521 	hrtime_t avg_ns = 0;
4522 
4523 	if (startd_failure_index < NSTARTD_FAILURE_TIMES)
4524 		return (0);
4525 
4526 	avg_ns =
4527 	    (startd_failure_time[(n - 1) % NSTARTD_FAILURE_TIMES] -
4528 	    startd_failure_time[n % NSTARTD_FAILURE_TIMES]) /
4529 	    NSTARTD_FAILURE_TIMES;
4530 
4531 	return (avg_ns < STARTD_FAILURE_RATE_NS);
4532 }
4533 
4534 /*
4535  * returns string that must be free'd
4536  */
4537 
4538 static char
4539 *audit_boot_msg()
4540 {
4541 	char		*b, *p;
4542 	char		desc[] = "booted";
4543 	zoneid_t	zid = getzoneid();
4544 
4545 	b = malloc(sizeof (desc) + MAXNAMELEN + 3);
4546 	if (b == NULL)
4547 		return (b);
4548 
4549 	p = b;
4550 	p += strlcpy(p, desc, sizeof (desc));
4551 	if (zid != GLOBAL_ZONEID) {
4552 		p += strlcpy(p, ": ", 3);
4553 		(void) getzonenamebyid(zid, p, MAXNAMELEN);
4554 	}
4555 	return (b);
4556 }
4557 
4558 /*
4559  * Generate AUE_init_solaris audit record.  Return 1 if
4560  * auditing is enabled in case the caller cares.
4561  *
4562  * In the case of userint() or a local zone invocation of
4563  * one_true_init, the process initially contains the audit
4564  * characteristics of the process that invoked init.  The first pass
4565  * through here uses those characteristics then for the case of
4566  * one_true_init in a local zone, clears them so subsequent system
4567  * state changes won't be attributed to the person who booted the
4568  * zone.
4569  */
4570 static int
4571 audit_put_record(int pass_fail, int status, char *msg)
4572 {
4573 	adt_session_data_t	*ah;
4574 	adt_event_data_t	*event;
4575 
4576 	if (!adt_audit_enabled())
4577 		return (0);
4578 
4579 	/*
4580 	 * the PROC_DATA picks up the context to tell whether this is
4581 	 * an attributed record (auid = -2 is unattributed)
4582 	 */
4583 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA)) {
4584 		console(B_TRUE, "audit failure:  %s\n", strerror(errno));
4585 		return (1);
4586 	}
4587 	event = adt_alloc_event(ah, ADT_init_solaris);
4588 	if (event == NULL) {
4589 		console(B_TRUE, "audit failure:  %s\n", strerror(errno));
4590 		(void) adt_end_session(ah);
4591 		return (1);
4592 	}
4593 	event->adt_init_solaris.info = msg;	/* NULL is ok here */
4594 
4595 	if (adt_put_event(event, pass_fail, status)) {
4596 		console(B_TRUE, "audit failure:  %s\n", strerror(errno));
4597 		(void) adt_end_session(ah);
4598 		return (1);
4599 	}
4600 	adt_free_event(event);
4601 
4602 	(void) adt_end_session(ah);
4603 
4604 	return (1);
4605 }
4606