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.
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.
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]
23 * Copyright (c) 2013 Gary Mills
25 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
42 * init(1M) is the general process spawning program. Its primary job is to
43 * start and restart svc.startd for smf(5). For backwards-compatibility it also
44 * spawns and respawns processes according to /etc/inittab and the current
45 * run-level. It reads /etc/default/inittab for general configuration.
47 * To change run-levels the system administrator runs init from the command
48 * line with a level name. init signals svc.startd via libscf and directs the
49 * zone's init (pid 1 in the global zone) what to do by sending it a signal;
50 * these signal numbers are commonly refered to in the code as 'states'. Valid
51 * run-levels are [sS0123456]. Additionally, init can be given directives
52 * [qQabc], which indicate actions to be taken pertaining to /etc/inittab.
54 * When init processes inittab entries, it finds processes that are to be
55 * spawned at various run-levels. inittab contains the set of the levels for
56 * which each inittab entry is valid.
58 * State File and Restartability
59 * Premature exit by init(1M) is handled as a special case by the kernel:
60 * init(1M) will be immediately re-executed, retaining its original PID. (PID
61 * 1 in the global zone.) To track the processes it has previously spawned,
62 * as well as other mutable state, init(1M) regularly updates a state file
63 * such that its subsequent invocations have knowledge of its various
64 * dependent processes and duties.
67 * We start svc.startd(1M) in a contract and transfer inherited contracts when
68 * restarting it. Everything else is started using the legacy contract
69 * template, and the created contracts are abandoned when they become empty.
71 * utmpx Entry Handling
72 * Because init(1M) no longer governs the startup process, its knowledge of
73 * when utmpx becomes writable is indirect. However, spawned processes
74 * expect to be constructed with valid utmpx entries. As a result, attempts
75 * to write normal entries will be retried until successful.
78 * In certain failure scenarios, init(1M) will enter a maintenance mode, in
79 * which it invokes sulogin(1M) to allow the operator an opportunity to
80 * repair the system. Normally, this operation is performed as a
81 * fork(2)-exec(2)-waitpid(3C) sequence with the parent waiting for repair or
82 * diagnosis to be completed. In the cases that fork(2) requests themselves
83 * fail, init(1M) will directly execute sulogin(1M), and allow the kernel to
84 * restart init(1M) on exit from the operator session.
86 * One scenario where init(1M) enters its maintenance mode is when
87 * svc.startd(1M) begins to fail rapidly, defined as when the average time
88 * between recent failures drops below a given threshold.
91 #include <sys/contract/process.h>
94 #include <sys/statvfs.h>
95 #include <sys/stropts.h>
96 #include <sys/systeminfo.h>
98 #include <sys/termios.h>
100 #include <sys/types.h>
101 #include <sys/utsname.h>
103 #include <bsm/adt_event.h>
104 #include <bsm/libbsm.h>
105 #include <security/pam_appl.h>
112 #include <libcontract.h>
113 #include <libcontract_priv.h>
116 #include <libscf_priv.h>
122 #include <stdio_ext.h>
133 #include <ucontext.h>
137 #define fioctl(p, sptr, cmd) ioctl(fileno(p), sptr, cmd)
138 #define min(a, b) (((a) < (b)) ? (a) : (b))
144 #define UT_USER_SZ 32 /* Size of a utmpx ut_user field */
145 #define UT_LINE_SZ 32 /* Size of a utmpx ut_line field */
148 * SLEEPTIME The number of seconds "init" sleeps between wakeups if
149 * nothing else requires this "init" wakeup.
151 #define SLEEPTIME (5 * 60)
154 * MAXCMDL The maximum length of a command string in inittab.
159 * EXEC The length of the prefix string added to all comamnds
162 #define EXEC (sizeof ("exec ") - 1)
165 * TWARN The amount of time between warning signal, SIGTERM,
166 * and the fatal kill signal, SIGKILL.
170 #define id_eq(x, y) ((x[0] == y[0] && x[1] == y[1] && x[2] == y[2] &&\
171 x[3] == y[3]) ? TRUE : FALSE)
174 * The kernel's default umask is 022 these days; since some processes inherit
175 * their umask from init, init will set it from CMASK in /etc/default/init.
176 * init gets the default umask from the kernel, it sets it to 022 whenever
177 * it wants to create a file and reverts to CMASK afterwards.
183 * The following definitions, concluding with the 'lvls' array, provide a
184 * common mapping between level-name (like 'S'), signal number (state),
185 * run-level mask, and specific properties associated with a run-level.
186 * This array should be accessed using the routines lvlname_to_state(),
187 * lvlname_to_mask(), state_to_mask(), and state_to_flags().
191 * Correspondence of signals to init actions.
201 #define SINGLE_USER SIGBUS
207 * Bit Mask for each level. Used to determine legal levels.
216 #define MASKSU 0x0080
221 #define MASK_NUMERIC (MASK0 | MASK1 | MASK2 | MASK3 | MASK4 | MASK5 | MASK6)
222 #define MASK_abc (MASKa | MASKb | MASKc)
225 * Flags to indicate properties of various states.
227 #define LSEL_RUNLEVEL 0x0001 /* runlevels you can transition to */
236 static lvl_t lvls
[] = {
239 { LVL0
, MASK0
, '0', LSEL_RUNLEVEL
},
240 { LVL1
, MASK1
, '1', LSEL_RUNLEVEL
},
241 { LVL2
, MASK2
, '2', LSEL_RUNLEVEL
},
242 { LVL3
, MASK3
, '3', LSEL_RUNLEVEL
},
243 { LVL4
, MASK4
, '4', LSEL_RUNLEVEL
},
244 { LVL5
, MASK5
, '5', LSEL_RUNLEVEL
},
245 { LVL6
, MASK6
, '6', LSEL_RUNLEVEL
},
246 { SINGLE_USER
, MASKSU
, 'S', LSEL_RUNLEVEL
},
247 { SINGLE_USER
, MASKSU
, 's', LSEL_RUNLEVEL
},
248 { LVLa
, MASKa
, 'a', 0 },
249 { LVLb
, MASKb
, 'b', 0 },
250 { LVLc
, MASKc
, 'c', 0 }
253 #define LVL_NELEMS (sizeof (lvls) / sizeof (lvl_t))
256 * Legal action field values.
258 #define OFF 0 /* Kill process if on, else ignore */
259 #define RESPAWN 1 /* Continuously restart process when it dies */
260 #define ONDEMAND RESPAWN /* Respawn for a, b, c type processes */
261 #define ONCE 2 /* Start process, do not respawn when dead */
262 #define WAIT 3 /* Perform once and wait to complete */
263 #define BOOT 4 /* Start at boot time only */
264 #define BOOTWAIT 5 /* Start at boot time and wait to complete */
265 #define POWERFAIL 6 /* Start on powerfail */
266 #define POWERWAIT 7 /* Start and wait for complete on powerfail */
267 #define INITDEFAULT 8 /* Default level "init" should start at */
268 #define SYSINIT 9 /* Actions performed before init speaks */
271 #define M_RESPAWN 0002
272 #define M_ONDEMAND M_RESPAWN
276 #define M_BOOTWAIT 0040
279 #define M_INITDEFAULT 0400
280 #define M_SYSINIT 01000
282 /* States for the inittab parser in getcmd(). */
290 * inittab entry id constants
292 #define INITTAB_ENTRY_ID_SIZE 4
293 #define INITTAB_ENTRY_ID_STR_FORMAT "%.4s" /* if INITTAB_ENTRY_ID_SIZE */
294 /* changes, this should */
295 /* change accordingly */
298 * Init can be in any of three main states, "normal" mode where it is
299 * processing entries for the lines file in a normal fashion, "boot" mode,
300 * where it is only interested in the boot actions, and "powerfail" mode,
301 * where it is only interested in powerfail related actions. The following
302 * masks declare the legal actions for each mode.
304 #define NORMAL_MODES (M_OFF | M_RESPAWN | M_ONCE | M_WAIT)
305 #define BOOT_MODES (M_BOOT | M_BOOTWAIT)
306 #define PF_MODES (M_PF | M_PWAIT)
309 char p_id
[INITTAB_ENTRY_ID_SIZE
]; /* Four letter unique id of */
311 pid_t p_pid
; /* Process id */
312 short p_count
; /* How many respawns of this command in */
313 /* the current series */
314 long p_time
; /* Start time for a series of respawns */
316 short p_exit
; /* Exit status of a process which died */
320 * Flags for the "p_flags" word of a PROC_TABLE entry:
322 * OCCUPIED This slot in init's proc table is in use.
324 * LIVING Process is alive.
326 * NOCLEANUP efork() is not allowed to cleanup this entry even
327 * if process is dead.
329 * NAMED This process has a name, i.e. came from inittab.
331 * DEMANDREQUEST Process started by a "telinit [abc]" command. Processes
332 * formed this way are respawnable and immune to level
333 * changes as long as their entry exists in inittab.
335 * TOUCHED Flag used by remv() to determine whether it has looked
336 * at an entry while checking for processes to be killed.
338 * WARNED Flag used by remv() to mark processes that have been
339 * sent the SIGTERM signal. If they don't die in 5
340 * seconds, they are sent the SIGKILL signal.
342 * KILLED Flag used by remv() to mark procs that have been sent
343 * the SIGTERM and SIGKILL signals.
345 * PF_MASK Bitwise or of legal flags, for sanity checking.
351 #define DEMANDREQUEST 020
358 * Respawn limits for processes that are to be respawned:
360 * SPAWN_INTERVAL The number of seconds over which "init" will try to
361 * respawn a process SPAWN_LIMIT times before it gets mad.
363 * SPAWN_LIMIT The number of respawns "init" will attempt in
364 * SPAWN_INTERVAL seconds before it generates an
365 * error message and inhibits further tries for
368 * INHIBIT The number of seconds "init" ignores an entry it had
369 * trouble spawning unless a "telinit Q" is received.
372 #define SPAWN_INTERVAL (2*60)
373 #define SPAWN_LIMIT 10
374 #define INHIBIT (5*60)
377 * The maximum number of decimal digits for an id_t. (ceil(log10 (max_id)))
379 #define ID_MAX_STR_LEN 10
381 #define NULLPROC ((struct PROC_TABLE *)(0))
382 #define NO_ROOM ((struct PROC_TABLE *)(FAILURE))
385 char c_id
[INITTAB_ENTRY_ID_SIZE
]; /* Four letter unique id of */
386 /* process to be affected by */
388 short c_levels
; /* Mask of legal levels for process */
389 short c_action
; /* Mask for type of action required */
390 char *c_command
; /* Pointer to init command */
394 int pd_type
; /* Command type */
395 pid_t pd_pid
; /* pid to add or remove */
404 static struct pidlist
{
405 pid_t pl_pid
; /* pid to watch for */
406 int pl_dflag
; /* Flag indicating SIGCLD from this pid */
407 short pl_exit
; /* Exit status of proc */
408 struct pidlist
*pl_next
; /* Next in list */
412 * The following structure contains a set of modes for /dev/syscon
413 * and should match the default contents of /etc/ioctl.syscon. It should also
414 * be kept in-sync with base_termios in uts/common/io/ttcompat.c.
416 static struct termios dflt_termios
= {
417 BRKINT
|ICRNL
|IXON
|IMAXBEL
, /* iflag */
418 OPOST
|ONLCR
|TAB3
, /* oflag */
419 CS8
|CREAD
|B9600
, /* cflag */
420 ISIG
|ICANON
|ECHO
|ECHOE
|ECHOK
|ECHOCTL
|ECHOKE
|IEXTEN
, /* lflag */
421 CINTR
, CQUIT
, CERASE
, CKILL
, CEOF
, 0, 0, 0,
422 0, 0, 0, 0, 0, 0, 0, 0,
426 static struct termios stored_syscon_termios
;
427 static int write_ioctl
= 0; /* Rewrite /etc/ioctl.syscon */
429 static union WAKEUP
{
431 unsigned w_usersignal
: 1; /* User sent signal to "init" */
432 unsigned w_childdeath
: 1; /* An "init" child died */
433 unsigned w_powerhit
: 1; /* OS experienced powerfail */
443 struct PROC_TABLE ist_proc_table
[1];
446 #define cur_state (g_state->ist_runlevel)
447 #define num_proc (g_state->ist_num_proc)
448 #define proc_table (g_state->ist_proc_table)
449 #define utmpx_ok (g_state->ist_utmpx_ok)
451 /* Contract cookies. */
452 #define ORDINARY_COOKIE 0
453 #define STARTD_COOKIE 1
457 #define bad_error(func, err) { \
458 (void) fprintf(stderr, "%s:%d: %s() failed with unexpected " \
459 "error %d. Aborting.\n", __FILE__, __LINE__, (func), (err)); \
463 #define bad_error(func, err) abort()
468 * Useful file and device names.
470 static char *CONSOLE
= "/dev/console"; /* Real system console */
471 static char *INITPIPE_DIR
= "/var/run";
472 static char *INITPIPE
= "/var/run/initpipe";
474 #define INIT_STATE_DIR "/etc/svc/volatile"
475 static const char * const init_state_file
= INIT_STATE_DIR
"/init.state";
476 static const char * const init_next_state_file
=
477 INIT_STATE_DIR
"/init-next.state";
479 static const int init_num_proc
= 20; /* Initial size of process table. */
481 static char *UTMPX
= UTMPX_FILE
; /* Snapshot record file */
482 static char *WTMPX
= WTMPX_FILE
; /* Long term record file */
483 static char *INITTAB
= "/etc/inittab"; /* Script file for "init" */
484 static char *SYSTTY
= "/dev/systty"; /* System Console */
485 static char *SYSCON
= "/dev/syscon"; /* Virtual System console */
486 static char *IOCTLSYSCON
= "/etc/ioctl.syscon"; /* Last syscon modes */
487 static char *ENVFILE
= "/etc/default/init"; /* Default env. */
488 static char *SU
= "/etc/sulogin"; /* Super-user program for single user */
489 static char *SH
= "/sbin/sh"; /* Standard shell */
492 * Default Path. /sbin is included in path only during sysinit phase
494 #define DEF_PATH "PATH=/usr/sbin:/usr/bin"
495 #define INIT_PATH "PATH=/sbin:/usr/sbin:/usr/bin"
497 static int prior_state
;
498 static int prev_state
; /* State "init" was in last time it woke */
499 static int new_state
; /* State user wants "init" to go to. */
500 static int lvlq_received
; /* Explicit request to examine state */
501 static int op_modes
= BOOT_MODES
; /* Current state of "init" */
502 static int Gchild
= 0; /* Flag to indicate "godchild" died, set in */
503 /* childeath() and cleared in cleanaux() */
504 static int Pfd
= -1; /* fd to receive pids thru */
505 static unsigned int spawncnt
, pausecnt
;
506 static int rsflag
; /* Set if a respawn has taken place */
507 static volatile int time_up
; /* Flag set to TRUE by the alarm interrupt */
508 /* routine each time an alarm interrupt */
510 static int sflg
= 0; /* Set if we were booted -s to single user */
511 static int rflg
= 0; /* Set if booted -r, reconfigure devices */
512 static int bflg
= 0; /* Set if booted -b, don't run rc scripts */
513 static pid_t init_pid
; /* PID of "one true" init for current zone */
515 static struct init_state
*g_state
= NULL
;
516 static size_t g_state_sz
;
517 static int booting
= 1; /* Set while we're booting. */
520 * Array for default global environment.
522 #define MAXENVENT 24 /* Max number of default env variables + 1 */
523 /* init can use three itself, so this leaves */
524 /* 20 for the administrator in ENVFILE. */
525 static char *glob_envp
[MAXENVENT
]; /* Array of environment strings */
526 static int glob_envn
; /* Number of environment strings */
529 static struct pollfd poll_fds
[1];
530 static int poll_nfds
= 0; /* poll_fds is uninitialized */
533 * Contracts constants
535 #define SVC_INIT_PREFIX "init:/"
536 #define SVC_AUX_SIZE (INITTAB_ENTRY_ID_SIZE + 1)
537 #define SVC_FMRI_SIZE (sizeof (SVC_INIT_PREFIX) + INITTAB_ENTRY_ID_SIZE)
539 static int legacy_tmpl
= -1; /* fd for legacy contract template */
540 static int startd_tmpl
= -1; /* fd for svc.startd's template */
541 static char startd_svc_aux
[SVC_AUX_SIZE
];
543 static char startd_cline
[256] = ""; /* svc.startd's command line */
544 static int do_restart_startd
= 1; /* Whether to restart svc.startd. */
545 static char *smf_options
= NULL
; /* Options to give to startd. */
546 static int smf_debug
= 0; /* Messages for debugging smf(5) */
547 static time_t init_boot_time
; /* Substitute for kernel boot time. */
549 #define NSTARTD_FAILURE_TIMES 3 /* trigger after 3 failures */
550 #define STARTD_FAILURE_RATE_NS 5000000000LL /* 1 failure/5 seconds */
552 static hrtime_t startd_failure_time
[NSTARTD_FAILURE_TIMES
];
553 static uint_t startd_failure_index
;
556 static char *prog_name(char *);
557 static int state_to_mask(int);
558 static int lvlname_to_mask(char, int *);
559 static void lscf_set_runlevel(char);
560 static int state_to_flags(int);
561 static char state_to_name(int);
562 static int lvlname_to_state(char);
563 static int getcmd(struct CMD_LINE
*, char *);
564 static int realcon();
565 static int spawn_processes();
566 static int get_ioctl_syscon();
567 static int account(short, struct PROC_TABLE
*, char *);
568 static void alarmclk();
569 static void childeath(int);
570 static void cleanaux();
571 static void clearent(pid_t
, short);
572 static void console(boolean_t
, char *, ...);
573 static void init_signals(void);
574 static void setup_pipe();
575 static void killproc(pid_t
);
576 static void init_env();
577 static void boot_init();
578 static void powerfail();
580 static void write_ioctl_syscon();
581 static void spawn(struct PROC_TABLE
*, struct CMD_LINE
*);
582 static void setimer(int);
583 static void siglvl(int, siginfo_t
*, ucontext_t
*);
584 static void sigpoll(int);
585 static void enter_maintenance(void);
586 static void timer(int);
587 static void userinit(int, char **);
588 static void notify_pam_dead(struct utmpx
*);
589 static long waitproc(struct PROC_TABLE
*);
590 static struct PROC_TABLE
*efork(int, struct PROC_TABLE
*, int);
591 static struct PROC_TABLE
*findpslot(struct CMD_LINE
*);
592 static void increase_proc_table_size();
593 static void st_init();
594 static void st_write();
595 static void contracts_init();
596 static void contract_event(struct pollfd
*);
597 static int startd_run(const char *, int, ctid_t
);
598 static void startd_record_failure();
599 static int startd_failure_rate_critical();
600 static char *audit_boot_msg();
601 static int audit_put_record(int, int, char *);
602 static void update_boot_archive(int new_state
);
605 main(int argc
, char *argv
[])
607 int chg_lvl_flag
= FALSE
, print_banner
= FALSE
;
608 int may_need_audit
= 1;
612 /* Get a timestamp for use as boot time, if needed. */
613 (void) time(&init_boot_time
);
615 /* Get the default umask */
619 /* Parse the arguments to init. Check for single user */
621 while ((c
= getopt(argc
, argv
, "brsm:")) != EOF
) {
638 smf_options
= optarg
;
639 smf_debug
= (strstr(smf_options
, "debug") != NULL
);
645 * Determine if we are the main init, or a user invoked init, whose job
646 * it is to inform init to change levels or perform some other action.
648 if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID
, &init_pid
,
649 sizeof (init_pid
)) != sizeof (init_pid
)) {
650 (void) fprintf(stderr
, "could not get pid for init\n");
655 * If this PID is not the same as the "true" init for the zone, then we
656 * must be in 'user' mode.
658 if (getpid() != init_pid
) {
659 userinit(argc
, argv
);
662 if (getzoneid() != GLOBAL_ZONEID
) {
667 * Initialize state (and set "booting").
671 if (booting
&& print_banner
) {
673 char buf
[BUFSIZ
], *isa
;
678 * We want to print the boot banner as soon as
679 * possible. In the global zone, the kernel does it,
680 * but we do not have that luxury in non-global zones,
681 * so we will print it here.
684 ret
= sysinfo(SI_ISALIST
, buf
, sizeof (buf
));
685 if (ret
!= -1L && ret
<= sizeof (buf
)) {
686 for (isa
= strtok(buf
, " "); isa
;
687 isa
= strtok(NULL
, " ")) {
688 if (strcmp(isa
, "sparcv9") == 0 ||
689 strcmp(isa
, "amd64") == 0) {
697 "\n\n%s Release %s Version %s %d-bit\r\n",
698 un
.sysname
, un
.release
, un
.version
, bits
);
700 "Copyright (c) 1983, 2010, Oracle and/or its affiliates."
701 " All rights reserved.\r\n");
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
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.
713 write_ioctl
= get_ioctl_syscon();
716 * Set up all signals to be caught or ignored as appropriate.
720 /* Load glob_envp from ENVFILE. */
726 /* cur_state should have been read in. */
728 op_modes
= NORMAL_MODES
;
730 /* Rewrite the ioctl file if it was bad. */
732 write_ioctl_syscon();
735 * It's fine to boot up with state as zero, because
736 * startd will later tell us the real state.
739 op_modes
= BOOT_MODES
;
744 prev_state
= prior_state
= cur_state
;
749 * Here is the beginning of the main process loop.
754 lvlq_received
= B_FALSE
;
758 * Clean up any accounting records for dead "godchildren".
764 * If in "normal" mode, check all living processes and initiate
765 * kill sequence on those that should not be there anymore.
767 if (op_modes
== NORMAL_MODES
&& cur_state
!= LVLa
&&
768 cur_state
!= LVLb
&& cur_state
!= LVLc
)
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.
777 chg_lvl_flag
= FALSE
;
779 if (state_to_flags(cur_state
) & LSEL_RUNLEVEL
) {
780 char rl
= state_to_name(cur_state
);
783 lscf_set_runlevel(rl
);
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.
794 if (spawn_processes() == FAILURE
) {
795 prior_state
= prev_state
;
796 cur_state
= SINGLE_USER
;
799 /* If any respawns occurred, take note. */
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
819 if (wakeup
.w_flags
.w_powerhit
) {
822 * Make sure that cur_state != prev_state so that
823 * ONCE and WAIT types work.
826 } else if (op_modes
!= NORMAL_MODES
) {
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.
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
||
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.
846 cur_state
= prior_state
;
847 prior_state
= prev_state
;
848 prev_state
= cur_state
;
850 prev_state
= cur_state
;
852 if (wakeup
.w_mask
== 0) {
855 if (may_need_audit
&& (cur_state
== LVL3
)) {
856 msg
= audit_boot_msg();
859 (void) audit_put_record(ADT_SUCCESS
,
865 * "init" is finished with all actions for
866 * the current wakeup.
868 ret
= poll(poll_fds
, poll_nfds
,
869 SLEEPTIME
* MILLISEC
);
872 contract_event(&poll_fds
[0]);
873 else if (ret
< 0 && errno
!= EINTR
)
874 console(B_TRUE
, "poll() error: %s\n",
878 if (wakeup
.w_flags
.w_usersignal
) {
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
885 if (new_state
!= cur_state
) {
886 if (new_state
== LVLa
||
889 prev_state
= prior_state
;
890 prior_state
= cur_state
;
891 cur_state
= new_state
;
893 prev_state
= cur_state
;
895 prior_state
= cur_state
;
896 cur_state
= new_state
;
904 if (wakeup
.w_flags
.w_powerhit
)
908 * Clear all wakeup reasons.
918 update_boot_archive(int new_state
)
920 if (new_state
!= LVL0
&& new_state
!= LVL5
&& new_state
!= LVL6
)
923 if (getzoneid() != GLOBAL_ZONEID
)
926 (void) system("/sbin/bootadm -a update_all");
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.
938 struct PROC_TABLE
*su_process
;
940 console(B_FALSE
, "Requesting maintenance mode\n"
941 "(See /lib/svc/share/README for additional information.)\n");
942 (void) sighold(SIGCLD
);
943 while ((su_process
= efork(M_OFF
, NULLPROC
, NOCLEANUP
)) == NO_ROOM
)
945 (void) sigrelse(SIGCLD
);
946 if (su_process
== NULLPROC
) {
949 (void) fclose(stdin
);
950 (void) fclose(stdout
);
951 (void) fclose(stderr
);
954 fd
= open(SYSCON
, O_RDWR
| O_NOCTTY
);
960 * Need to issue an error message somewhere.
962 syslog(LOG_CRIT
, "init[%d]: cannot open %s; %s\n",
963 getpid(), SYSCON
, strerror(errno
));
967 * Execute the "su" program.
969 (void) execle(SU
, SU
, "-", (char *)0, glob_envp
);
970 console(B_TRUE
, "execle of %s failed: %s\n", SU
,
977 * If we are the parent, wait around for the child to die
978 * or for "init" to be signaled to change levels.
980 while (waitproc(su_process
) == FAILURE
) {
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().
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.
998 struct PROC_TABLE
*process
;
1000 char cmd_string
[MAXCMDL
];
1003 change_level
= (cur_state
!= prev_state
? TRUE
: FALSE
);
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.
1010 for (process
= proc_table
;
1011 (process
< proc_table
+ num_proc
); process
++) {
1012 process
->p_flags
&= ~TOUCHED
;
1016 * Scan all inittab entries.
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
))
1027 * This slot contains the process we are looking for.
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?
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
) {
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
1050 process
->p_flags
|= TOUCHED
;
1052 if ((process
->p_flags
& KILLED
) == 0) {
1061 * Fork a killing proc
1064 * having to pause for
1070 process
->p_flags
|= KILLED
;
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
1083 if (process
->p_flags
&
1084 (LIVING
|NOCLEANUP
|DEMANDREQUEST
))
1085 process
->p_flags
|= TOUCHED
;
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.
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.
1111 * Scan for processes which should be dying. We hope they
1112 * will die without having to be sent a SIGKILL signal.
1114 for (process
= proc_table
;
1115 (process
< proc_table
+ num_proc
); process
++) {
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.
1121 while (time_up
== FALSE
&&
1122 (process
->p_flags
& (WARNED
|LIVING
|OCCUPIED
)) ==
1123 (WARNED
|LIVING
|OCCUPIED
))
1126 if (time_up
== TRUE
)
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.
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
);
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.
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
)) ==
1164 (void) account(DEAD_PROCESS
, process
, NULL
);
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.
1171 if ((process
->p_flags
& TOUCHED
) == 0)
1172 process
->p_flags
= 0;
1180 * Extract the svc.startd command line and whether to restart it from its
1185 process_startd_line(struct CMD_LINE
*cmd
, char *cmd_string
)
1189 /* Save the command line. */
1191 /* Also append -r or -s. */
1192 (void) strlcpy(startd_cline
, cmd_string
, sizeof (startd_cline
));
1193 (void) strlcat(startd_cline
, " -", sizeof (startd_cline
));
1195 sz
= strlcat(startd_cline
, "s", sizeof (startd_cline
));
1197 sz
= strlcat(startd_cline
, "r", sizeof (startd_cline
));
1199 sz
= strlcpy(startd_cline
, cmd_string
, sizeof (startd_cline
));
1202 if (sz
>= sizeof (startd_cline
)) {
1204 "svc.startd command line too long. Ignoring.\n");
1205 startd_cline
[0] = '\0';
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.
1217 struct PROC_TABLE
*pp
;
1218 struct CMD_LINE cmd
;
1219 char cmd_string
[MAXCMDL
];
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.
1229 if (wakeup
.w_flags
.w_powerhit
) {
1230 wakeup
.w_flags
.w_powerhit
= 0;
1231 op_modes
= PF_MODES
;
1233 lvl_mask
= state_to_mask(cur_state
);
1236 * Scan through all the entries in inittab.
1238 while ((status
= getcmd(&cmd
, &cmd_string
[0])) == TRUE
) {
1239 if (id_eq(cmd
.c_id
, "smf")) {
1240 process_startd_line(&cmd
, cmd_string
);
1244 retry_for_proc_slot
:
1247 * Find out if there is a process slot for this entry already.
1249 if ((pp
= findpslot(&cmd
)) == NULLPROC
) {
1251 * we've run out of proc table entries
1252 * increase proc_table.
1254 increase_proc_table_size();
1257 * Retry now as we have an empty proc slot.
1258 * In case increase_proc_table_size() fails,
1259 * we will keep retrying.
1261 goto retry_for_proc_slot
;
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.
1270 if (((pp
->p_flags
& (LIVING
|DEMANDREQUEST
)) == DEMANDREQUEST
) &&
1271 (cmd
.c_levels
& MASK_abc
) &&
1272 (cmd
.c_action
& op_modes
) == M_ONDEMAND
) {
1278 * If the action is not an action we are interested in,
1281 if ((cmd
.c_action
& op_modes
) == 0 || pp
->p_flags
& LIVING
||
1282 (cmd
.c_levels
& lvl_mask
) == 0)
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.
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
))
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.
1306 if (cmd
.c_action
& (M_ONCE
| M_RESPAWN
| M_PF
| M_BOOT
)) {
1311 while (waitproc(pp
) == FAILURE
)
1313 (void) account(DEAD_PROCESS
, pp
, NULL
);
1321 * spawn() spawns a shell, inserts the information about the process
1322 * process into the proc_table, and does the startup accounting.
1325 spawn(struct PROC_TABLE
*process
, struct CMD_LINE
*cmd
)
1328 int modes
, maxfiles
;
1330 struct PROC_TABLE tmproc
, *oprocess
;
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.
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)
1345 * If this is a respawnable process, check the threshold
1346 * information to avoid excessive respawns.
1348 if (cmd
->c_action
& M_RESPAWN
) {
1350 * Add NOCLEANUP to all respawnable commands so that the
1351 * information about the frequency of respawns isn't lost.
1357 * If no time is assigned, then this is the first time
1358 * this command is being processed in this series. Assign
1361 if (process
->p_time
== 0L)
1362 process
->p_time
= now
;
1364 if (process
->p_count
++ == SPAWN_LIMIT
) {
1366 if ((now
- process
->p_time
) < SPAWN_INTERVAL
) {
1368 * Process is respawning too rapidly. Print
1369 * message and refuse to respawn it for now.
1371 console(B_TRUE
, "Command is respawning too "
1372 "rapidly. Check for possible errors.\n"
1374 &cmd
->c_id
[0], &cmd
->c_command
[EXEC
]);
1377 process
->p_time
= now
;
1378 process
->p_count
= 0;
1380 } else if (process
->p_count
> SPAWN_LIMIT
) {
1382 * If process has been respawning too rapidly and
1383 * the inhibit time limit hasn't expired yet, we
1384 * refuse to respawn.
1386 if (now
- process
->p_time
< SPAWN_INTERVAL
+ INHIBIT
)
1388 process
->p_time
= now
;
1389 process
->p_count
= 0;
1395 * Spawn a child process to execute this command.
1397 (void) sighold(SIGCLD
);
1399 while ((process
= efork(cmd
->c_action
, oprocess
, modes
)) == NO_ROOM
)
1402 if (process
== NULLPROC
) {
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.
1412 * Perform the accounting for the beginning of a process.
1413 * Note that all processes are initially "INIT_PROCESS"es.
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();
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
);
1428 * Now exec a shell with the -c option and the command
1431 (void) execle(SH
, "INITSH", "-c", cmd
->c_command
, (char *)0,
1433 console(B_TRUE
, "Command\n\"%s\"\n failed to execute. errno "
1434 "= %d (exec of shell failed)\n", cmd
->c_command
, errno
);
1437 * Don't come back so quickly that "init" doesn't have a
1438 * chance to finish putting this child in "proc_table".
1446 * We are the parent. Insert the necessary
1447 * information in the proc_table.
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];
1456 (void) sigrelse(SIGCLD
);
1460 * findpslot() finds the old slot in the process table for the
1461 * command with the same id, or it finds an empty slot.
1463 static struct PROC_TABLE
*
1464 findpslot(struct CMD_LINE
*cmd
)
1466 struct PROC_TABLE
*process
;
1467 struct PROC_TABLE
*empty
= NULLPROC
;
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
))
1476 * If the entry is totally empty and "empty" is still 0,
1477 * remember where this hole is and make sure the slot is
1480 if (empty
== NULLPROC
&& (process
->p_flags
& OCCUPIED
) == 0) {
1482 process
->p_id
[0] = '\0';
1483 process
->p_id
[1] = '\0';
1484 process
->p_id
[2] = '\0';
1485 process
->p_id
[3] = '\0';
1487 process
->p_time
= 0L;
1488 process
->p_count
= 0;
1489 process
->p_flags
= 0;
1490 process
->p_exit
= 0;
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.
1500 if (process
== (proc_table
+ num_proc
))
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.
1513 static FILE *fp_inittab
= NULL
;
1516 getcmd(struct CMD_LINE
*cmd
, char *shcmd
)
1519 int c
, lastc
, state
;
1521 int answer
, i
, proceed
;
1523 static char *actions
[] = {
1524 "off", "respawn", "ondemand", "once", "wait", "boot",
1525 "bootwait", "powerfail", "powerwait", "initdefault",
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
,
1533 * Only these actions will be allowed for entries which
1534 * are specified for single-user mode.
1536 short su_acts
= M_INITDEFAULT
| M_PF
| M_PWAIT
| M_WAIT
;
1538 if (fp_inittab
== NULL
) {
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.
1545 for (i
= 0; i
< 3; i
++) {
1546 if (stat(INITTAB
, &sbuf
) == -1) {
1549 "Cannot stat %s, errno: %d\n",
1555 } else if (sbuf
.st_size
< 10) {
1558 "%s truncated or corrupted\n",
1570 * If unable to open inittab, print error message and
1571 * return FAILURE to caller.
1573 if ((fp_inittab
= fopen(INITTAB
, "r")) == NULL
) {
1574 console(B_TRUE
, "Cannot open %s errno: %d\n", INITTAB
,
1581 * Keep getting commands from inittab until you find a
1582 * good one or run out of file.
1584 for (answer
= FALSE
; answer
== FALSE
; ) {
1586 * Zero out the cmd itself before trying next line.
1588 bzero(cmd
, sizeof (struct CMD_LINE
));
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.
1599 if ((c
= fgetc(fp_inittab
)) == EOF
) {
1601 (void) fclose(fp_inittab
);
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 */
1613 if (state
!= FAILURE
&& state
!= COMMAND
) {
1615 * Squeeze out spaces and tabs.
1617 if (c
== ' ' || c
== '\t')
1621 * Ignore characters in a comment, except for the \n.
1623 if (state
== COMMENT
) {
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.
1638 if (state
== ID
&& c
== '#' && ptr
== shcmd
) {
1644 * If the character is a ':', then check the
1645 * previous field for correctness and advance
1646 * to the next field.
1653 * Check to see that there are only
1654 * 1 to 4 characters for the id.
1656 if ((i
= ptr
- shcmd
) < 1 || i
> 4) {
1659 bcopy(shcmd
, &cmd
->c_id
[0], i
);
1667 * Build a mask for all the levels for
1668 * which this command will be legal.
1670 for (cmd
->c_levels
= 0, ptr1
= shcmd
;
1671 ptr1
< ptr
; ptr1
++) {
1673 if (lvlname_to_mask(*ptr1
,
1678 cmd
->c_levels
|= mask
;
1680 if (state
!= FAILURE
) {
1682 ptr
= shcmd
; /* Reset the buffer */
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".
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
;
1704 cmd
->c_action
= M_OFF
;
1706 for (cmd
->c_action
= 0, i
= 0,
1709 sizeof (actions
)/sizeof (char *);
1711 if (strcmp(shcmd
, actions
[i
]) == 0) {
1712 if ((cmd
->c_levels
& MASKSU
) &&
1713 !(act_masks
[i
] & su_acts
))
1724 * If the action didn't match any legal action,
1725 * set state to FAILURE.
1727 if (cmd
->c_action
== 0) {
1731 (void) strcpy(shcmd
, "exec ");
1740 /* If the character is a '\n', then this is the end of a */
1741 /* line. If the '\n' wasn't preceded by a backslash, */
1742 /* it is also the end of an inittab command. If it was */
1743 /* preceded by a backslash then the next line is a */
1744 /* continuation. Note that the continuation '\n' falls */
1745 /* through and is treated like other characters and is */
1746 /* stored in the shell command line. */
1747 if (c
== '\n' && lastc
!= '\\') {
1753 /* For all other characters just stuff them into the */
1754 /* command as long as there aren't too many of them. */
1755 /* Make sure there is room for a terminating '\0' also. */
1756 if (ptr
>= shcmd
+ MAXCMDL
- 1)
1761 /* If the character we just stored was a quoted */
1762 /* backslash, then change "c" to '\0', so that this */
1763 /* backslash will not cause a subsequent '\n' to appear */
1764 /* quoted. In otherwords '\' '\' '\n' is the real end */
1765 /* of a command, while '\' '\n' is a continuation. */
1766 if (c
== '\\' && lastc
== '\\')
1771 * Make sure all the fields are properly specified
1772 * for a good command line.
1774 if (state
== COMMAND
) {
1776 cmd
->c_command
= shcmd
;
1779 * If no default level was supplied, insert
1780 * all numerical levels.
1782 if (cmd
->c_levels
== 0)
1783 cmd
->c_levels
= MASK_NUMERIC
;
1786 * If no action has been supplied, declare this
1789 if (cmd
->c_action
== 0)
1790 cmd
->c_action
= M_OFF
;
1793 * If no shell command has been supplied, make sure
1794 * there is a null string in the command field.
1796 if (ptr
== shcmd
+ EXEC
)
1802 * If we have reached the end of inittab, then close it
1803 * and quit trying to find a good command line.
1806 (void) fclose(fp_inittab
);
1815 * lvlname_to_state(): convert the character name of a state to its level
1816 * (its corresponding signal number).
1819 lvlname_to_state(char name
)
1822 for (i
= 0; i
< LVL_NELEMS
; i
++) {
1823 if (lvls
[i
].lvl_name
== name
)
1824 return (lvls
[i
].lvl_state
);
1830 * state_to_name(): convert the level to the character name.
1833 state_to_name(int state
)
1836 for (i
= 0; i
< LVL_NELEMS
; i
++) {
1837 if (lvls
[i
].lvl_state
== state
)
1838 return (lvls
[i
].lvl_name
);
1844 * state_to_mask(): return the mask corresponding to a signal number
1847 state_to_mask(int state
)
1850 for (i
= 0; i
< LVL_NELEMS
; i
++) {
1851 if (lvls
[i
].lvl_state
== state
)
1852 return (lvls
[i
].lvl_mask
);
1854 return (0); /* return 0, since that represents an empty mask */
1858 * lvlname_to_mask(): return the mask corresponding to a levels character name
1861 lvlname_to_mask(char name
, int *mask
)
1864 for (i
= 0; i
< LVL_NELEMS
; i
++) {
1865 if (lvls
[i
].lvl_name
== name
) {
1866 *mask
= lvls
[i
].lvl_mask
;
1874 * state_to_flags(): return the flags corresponding to a runlevel. These
1875 * indicate properties of that runlevel.
1878 state_to_flags(int state
)
1881 for (i
= 0; i
< LVL_NELEMS
; i
++) {
1882 if (lvls
[i
].lvl_state
== state
)
1883 return (lvls
[i
].lvl_flags
);
1889 * killproc() creates a child which kills the process specified by pid.
1894 struct PROC_TABLE
*process
;
1896 (void) sighold(SIGCLD
);
1897 while ((process
= efork(M_OFF
, NULLPROC
, 0)) == NO_ROOM
)
1899 (void) sigrelse(SIGCLD
);
1901 if (process
== NULLPROC
) {
1903 * efork() sets all signal handlers to the default, so reset
1904 * the ALRM handler to make timer() work as expected.
1906 (void) sigset(SIGALRM
, alarmclk
);
1909 * We are the child. Try to terminate the process nicely
1910 * first using SIGTERM and if it refuses to die in TWARN
1911 * seconds kill it with SIGKILL.
1913 (void) kill(pid
, SIGTERM
);
1914 (void) timer(TWARN
);
1915 (void) kill(pid
, SIGKILL
);
1921 * Set up the default environment for all procs to be forked from init.
1922 * Read the values from the /etc/default/init file, except for PATH. If
1923 * there's not enough room in the environment array, the environment
1924 * lines that don't fit are silently discarded.
1931 int inquotes
, length
, wslength
;
1932 char *tokp
, *cp1
, *cp2
;
1934 glob_envp
[0] = malloc((unsigned)(strlen(DEF_PATH
)+2));
1935 (void) strcpy(glob_envp
[0], DEF_PATH
);
1940 malloc((unsigned)(strlen("_DVFS_RECONFIG=YES")+2));
1941 (void) strcpy(glob_envp
[1], "_DVFS_RECONFIG=YES");
1943 } else if (bflg
== 1) {
1945 malloc((unsigned)(strlen("RB_NOBOOTRC=YES")+2));
1946 (void) strcpy(glob_envp
[1], "RB_NOBOOTRC=YES");
1950 if ((fp
= fopen(ENVFILE
, "r")) == NULL
) {
1952 "Cannot open %s. Environment not initialized.\n",
1955 while (fgets(line
, MAXCMDL
- 1, fp
) != NULL
&&
1956 glob_envn
< MAXENVENT
- 2) {
1960 length
= strlen(line
);
1961 if (line
[length
- 1] == '\n')
1962 line
[length
- 1] = '\0';
1965 * Ignore blank or comment lines.
1967 if (line
[0] == '#' || line
[0] == '\0' ||
1968 (wslength
= strspn(line
, " \t\n")) ==
1970 strchr(line
, '#') == line
+ wslength
)
1974 * First make a pass through the line and change
1975 * any non-quoted semi-colons to blanks so they
1976 * will be treated as token separators below.
1979 for (cp1
= line
; *cp1
!= '\0'; cp1
++) {
1985 } else if (*cp1
== ';') {
1992 * Tokens within the line are separated by blanks
1993 * and tabs. For each token in the line which
1994 * contains a '=' we strip out any quotes and then
1995 * stick the token in the environment array.
1997 if ((tokp
= strtok(line
, " \t")) == NULL
)
2000 if (strchr(tokp
, '=') == NULL
)
2002 length
= strlen(tokp
);
2003 while ((cp1
= strpbrk(tokp
, "\"\'")) != NULL
) {
2005 cp2
< &tokp
[length
]; cp2
++)
2010 if (strncmp(tokp
, "CMASK=",
2011 sizeof ("CMASK=") - 1) == 0) {
2014 /* We know there's an = */
2015 t
= strtol(strchr(tokp
, '=') + 1, NULL
,
2019 if (t
<= 077 && t
>= 0)
2021 (void) umask(cmask
);
2024 glob_envp
[glob_envn
] =
2025 malloc((unsigned)(length
+ 1));
2026 (void) strcpy(glob_envp
[glob_envn
], tokp
);
2027 if (++glob_envn
>= MAXENVENT
- 1)
2029 } while ((tokp
= strtok(NULL
, " \t")) != NULL
);
2033 * Append a null pointer to the environment array
2036 glob_envp
[glob_envn
] = NULL
;
2042 * boot_init(): Do initialization things that should be done at boot.
2048 struct PROC_TABLE
*process
, *oprocess
;
2049 struct CMD_LINE cmd
;
2051 char svc_aux
[SVC_AUX_SIZE
];
2052 char init_svc_fmri
[SVC_FMRI_SIZE
];
2056 /* Use INIT_PATH for sysinit cmds */
2057 old_path
= glob_envp
[0];
2058 glob_envp
[0] = malloc((unsigned)(strlen(INIT_PATH
)+2));
2059 (void) strcpy(glob_envp
[0], INIT_PATH
);
2062 * Scan inittab(4) and process the special svc.startd entry, initdefault
2063 * and sysinit entries.
2065 while (getcmd(&cmd
, &line
[0]) == TRUE
) {
2066 if (startd_tmpl
>= 0 && id_eq(cmd
.c_id
, "smf")) {
2067 process_startd_line(&cmd
, line
);
2068 (void) snprintf(startd_svc_aux
, SVC_AUX_SIZE
,
2069 INITTAB_ENTRY_ID_STR_FORMAT
, cmd
.c_id
);
2070 } else if (cmd
.c_action
== M_INITDEFAULT
) {
2072 * initdefault is no longer meaningful, as the SMF
2073 * milestone controls what (legacy) run level we
2077 "Ignoring legacy \"initdefault\" entry.\n");
2078 } else if (cmd
.c_action
== M_SYSINIT
) {
2080 * Execute the "sysinit" entry and wait for it to
2081 * complete. No bookkeeping is performed on these
2082 * entries because we avoid writing to the file system
2083 * until after there has been an chance to check it.
2085 if (process
= findpslot(&cmd
)) {
2086 (void) sighold(SIGCLD
);
2087 (void) snprintf(svc_aux
, SVC_AUX_SIZE
,
2088 INITTAB_ENTRY_ID_STR_FORMAT
, cmd
.c_id
);
2089 (void) snprintf(init_svc_fmri
, SVC_FMRI_SIZE
,
2090 SVC_INIT_PREFIX INITTAB_ENTRY_ID_STR_FORMAT
,
2092 if (legacy_tmpl
>= 0) {
2093 (void) ct_pr_tmpl_set_svc_fmri(
2094 legacy_tmpl
, init_svc_fmri
);
2095 (void) ct_pr_tmpl_set_svc_aux(
2096 legacy_tmpl
, svc_aux
);
2099 for (oprocess
= process
;
2100 (process
= efork(M_OFF
, oprocess
,
2101 (NAMED
|NOCLEANUP
))) == NO_ROOM
;
2104 (void) sigrelse(SIGCLD
);
2106 if (process
== NULLPROC
) {
2107 maxfiles
= ulimit(UL_GDESLIM
, 0);
2109 for (i
= 0; i
< maxfiles
; i
++)
2110 (void) fcntl(i
, F_SETFD
,
2112 (void) execle(SH
, "INITSH", "-c",
2114 (char *)0, glob_envp
);
2116 "Command\n\"%s\"\n failed to execute. errno = %d (exec of shell failed)\n",
2117 cmd
.c_command
, errno
);
2120 while (waitproc(process
) == FAILURE
)
2122 process
->p_flags
= 0;
2128 /* Restore the path. */
2130 glob_envp
[0] = old_path
;
2133 * This will enable st_write() to complain about init_state_file.
2138 * If the /etc/ioctl.syscon didn't exist or had invalid contents write
2139 * out a correct version.
2142 write_ioctl_syscon();
2145 * Start svc.startd(1M), which does most of the work.
2147 if (startd_cline
[0] != '\0' && startd_tmpl
>= 0) {
2148 /* Start svc.startd. */
2149 if (startd_run(startd_cline
, startd_tmpl
, 0) == -1)
2150 cur_state
= SINGLE_USER
;
2152 console(B_TRUE
, "Absent svc.startd entry or bad "
2153 "contract template. Not starting svc.startd.\n");
2154 enter_maintenance();
2159 * init_signals(): Initialize all signals to either be caught or ignored.
2164 struct sigaction act
;
2168 * Start by ignoring all signals, then selectively re-enable some.
2169 * The SIG_IGN disposition will only affect asynchronous signals:
2170 * any signal that we trigger synchronously that doesn't end up
2171 * being handled by siglvl() will be forcibly delivered by the kernel.
2173 for (i
= SIGHUP
; i
<= SIGRTMAX
; i
++)
2174 (void) sigset(i
, SIG_IGN
);
2177 * Handle all level-changing signals using siglvl() and set sa_mask so
2178 * that all level-changing signals are blocked while in siglvl().
2180 act
.sa_handler
= siglvl
;
2181 act
.sa_flags
= SA_SIGINFO
;
2182 (void) sigemptyset(&act
.sa_mask
);
2184 (void) sigaddset(&act
.sa_mask
, LVLQ
);
2185 (void) sigaddset(&act
.sa_mask
, LVL0
);
2186 (void) sigaddset(&act
.sa_mask
, LVL1
);
2187 (void) sigaddset(&act
.sa_mask
, LVL2
);
2188 (void) sigaddset(&act
.sa_mask
, LVL3
);
2189 (void) sigaddset(&act
.sa_mask
, LVL4
);
2190 (void) sigaddset(&act
.sa_mask
, LVL5
);
2191 (void) sigaddset(&act
.sa_mask
, LVL6
);
2192 (void) sigaddset(&act
.sa_mask
, SINGLE_USER
);
2193 (void) sigaddset(&act
.sa_mask
, LVLa
);
2194 (void) sigaddset(&act
.sa_mask
, LVLb
);
2195 (void) sigaddset(&act
.sa_mask
, LVLc
);
2197 (void) sigaction(LVLQ
, &act
, NULL
);
2198 (void) sigaction(LVL0
, &act
, NULL
);
2199 (void) sigaction(LVL1
, &act
, NULL
);
2200 (void) sigaction(LVL2
, &act
, NULL
);
2201 (void) sigaction(LVL3
, &act
, NULL
);
2202 (void) sigaction(LVL4
, &act
, NULL
);
2203 (void) sigaction(LVL5
, &act
, NULL
);
2204 (void) sigaction(LVL6
, &act
, NULL
);
2205 (void) sigaction(SINGLE_USER
, &act
, NULL
);
2206 (void) sigaction(LVLa
, &act
, NULL
);
2207 (void) sigaction(LVLb
, &act
, NULL
);
2208 (void) sigaction(LVLc
, &act
, NULL
);
2210 (void) sigset(SIGALRM
, alarmclk
);
2213 (void) sigset(SIGCLD
, childeath
);
2214 (void) sigset(SIGPWR
, powerfail
);
2218 * Set up pipe for "godchildren". If the file exists and is a pipe just open
2219 * it. Else, if the file system is r/w create it. Otherwise, defer its
2220 * creation and open until after /var/run has been mounted. This function is
2221 * only called on startup and when explicitly requested via LVLQ.
2226 struct stat stat_buf
;
2227 struct statvfs statvfs_buf
;
2228 struct sigaction act
;
2231 * Always close the previous pipe descriptor as the mounted filesystems
2237 if ((stat(INITPIPE
, &stat_buf
) == 0) &&
2238 ((stat_buf
.st_mode
& (S_IFMT
|S_IRUSR
)) == (S_IFIFO
|S_IRUSR
)))
2239 Pfd
= open(INITPIPE
, O_RDWR
| O_NDELAY
);
2241 if ((statvfs(INITPIPE_DIR
, &statvfs_buf
) == 0) &&
2242 ((statvfs_buf
.f_flag
& ST_RDONLY
) == 0)) {
2243 (void) unlink(INITPIPE
);
2244 (void) mknod(INITPIPE
, S_IFIFO
| 0600, 0);
2245 Pfd
= open(INITPIPE
, O_RDWR
| O_NDELAY
);
2249 (void) ioctl(Pfd
, I_SETSIG
, S_INPUT
);
2251 * Read pipe in message discard mode.
2253 (void) ioctl(Pfd
, I_SRDOPT
, RMSGD
);
2255 act
.sa_handler
= sigpoll
;
2257 (void) sigemptyset(&act
.sa_mask
);
2258 (void) sigaddset(&act
.sa_mask
, SIGCLD
);
2259 (void) sigaction(SIGPOLL
, &act
, NULL
);
2264 * siglvl - handle an asynchronous signal from init(1M) telling us that we
2265 * should change the current run level. We set new_state accordingly.
2268 siglvl(int sig
, siginfo_t
*sip
, ucontext_t
*ucp
)
2270 struct PROC_TABLE
*process
;
2271 struct sigaction act
;
2274 * If the signal was from the kernel (rather than init(1M)) then init
2275 * itself tripped the signal. That is, we might have a bug and tripped
2276 * a real SIGSEGV instead of receiving it as an alias for SIGLVLa. In
2277 * such a case we reset the disposition to SIG_DFL, block all signals
2278 * in uc_mask but the current one, and return to the interrupted ucp
2279 * to effect an appropriate death. The kernel will then restart us.
2281 * The one exception to SI_FROMKERNEL() is SIGFPE (a.k.a. LVL6), which
2282 * the kernel can send us when it wants to effect an orderly reboot.
2283 * For this case we must also verify si_code is zero, rather than a
2284 * code such as FPE_INTDIV which a bug might have triggered.
2286 if (sip
!= NULL
&& SI_FROMKERNEL(sip
) &&
2287 (sig
!= SIGFPE
|| sip
->si_code
== 0)) {
2289 (void) sigemptyset(&act
.sa_mask
);
2290 act
.sa_handler
= SIG_DFL
;
2292 (void) sigaction(sig
, &act
, NULL
);
2294 (void) sigfillset(&ucp
->uc_sigmask
);
2295 (void) sigdelset(&ucp
->uc_sigmask
, sig
);
2296 ucp
->uc_flags
|= UC_SIGMASK
;
2298 (void) setcontext(ucp
);
2302 * If the signal received is a LVLQ signal, do not really
2303 * change levels, just restate the current level. If the
2304 * signal is not a LVLQ, set the new level to the signal
2308 new_state
= cur_state
;
2309 lvlq_received
= B_TRUE
;
2315 * Clear all times and repeat counts in the process table
2316 * since either the level is changing or the user has editted
2317 * the inittab file and wants us to look at it again.
2318 * If the user has fixed a typo, we don't want residual timing
2319 * data preventing the fixed command line from executing.
2321 for (process
= proc_table
;
2322 (process
< proc_table
+ num_proc
); process
++) {
2323 process
->p_time
= 0L;
2324 process
->p_count
= 0;
2328 * Set the flag to indicate that a "user signal" was received.
2330 wakeup
.w_flags
.w_usersignal
= 1;
2344 * childeath_single():
2346 * This used to be the SIGCLD handler and it was set with signal()
2347 * (as opposed to sigset()). When a child exited we'd come to the
2348 * handler, wait for the child, and reenable the handler with
2349 * signal() just before returning. The implementation of signal()
2350 * checks with waitid() for waitable children and sends a SIGCLD
2351 * if there are some. If children are exiting faster than the
2352 * handler can run we keep sending signals and the handler never
2353 * gets to return and eventually the stack runs out and init dies.
2354 * To prevent that we set the handler with sigset() so the handler
2355 * doesn't need to be reset, and in childeath() (see below) we
2356 * call childeath_single() as long as there are children to be
2357 * waited for. If a child exits while init is in the handler a
2358 * SIGCLD will be pending and delivered on return from the handler.
2359 * If the child was already waited for the handler will have nothing
2360 * to do and return, otherwise the child will be waited for.
2363 childeath_single(pid_t pid
, int status
)
2365 struct PROC_TABLE
*process
;
2369 * Scan the process table to see if we are interested in this process.
2371 for (process
= proc_table
;
2372 (process
< proc_table
+ num_proc
); process
++) {
2373 if ((process
->p_flags
& (LIVING
|OCCUPIED
)) ==
2374 (LIVING
|OCCUPIED
) && process
->p_pid
== pid
) {
2377 * Mark this process as having died and store the exit
2378 * status. Also set the wakeup flag for a dead child
2379 * and break out of the loop.
2381 process
->p_flags
&= ~LIVING
;
2382 process
->p_exit
= (short)status
;
2383 wakeup
.w_flags
.w_childdeath
= 1;
2390 * No process was found above, look through auxiliary list.
2392 (void) sighold(SIGPOLL
);
2395 if (pid
> pp
->pl_pid
) {
2401 } else if (pid
< pp
->pl_pid
) {
2408 * This is a dead "godchild".
2411 pp
->pl_exit
= (short)status
;
2412 wakeup
.w_flags
.w_childdeath
= 1;
2413 Gchild
= 1; /* Notice to call cleanaux(). */
2418 (void) sigrelse(SIGPOLL
);
2423 childeath(int signo
)
2428 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0)
2429 childeath_single(pid
, status
);
2436 wakeup
.w_flags
.w_powerhit
= 1;
2440 * efork() forks a child and the parent inserts the process in its table
2441 * of processes that are directly a result of forks that it has performed.
2442 * The child just changes the "global" with the process id for this process
2443 * to it's new value.
2444 * If efork() is called with a pointer into the proc_table it uses that slot,
2445 * otherwise it searches for a free slot. Regardless of how it was called,
2446 * it returns the pointer to the proc_table entry
2448 * The SIGCLD signal is blocked (held) before calling efork()
2449 * and is unblocked (released) after efork() returns.
2451 * Ideally, this should be rewritten to use modern signal semantics.
2453 static struct PROC_TABLE
*
2454 efork(int action
, struct PROC_TABLE
*process
, int modes
)
2457 struct PROC_TABLE
*proc
;
2460 * Freshen up the proc_table, removing any entries for dead processes
2461 * that don't have NOCLEANUP set. Perform the necessary accounting.
2463 for (proc
= proc_table
; (proc
< proc_table
+ num_proc
); proc
++) {
2464 if ((proc
->p_flags
& (OCCUPIED
|LIVING
|NOCLEANUP
)) ==
2467 * Is this a named process?
2468 * If so, do the necessary bookkeeping.
2470 if (proc
->p_flags
& NAMED
)
2471 (void) account(DEAD_PROCESS
, proc
, NULL
);
2474 * Free this entry for new usage.
2480 while ((childpid
= fork()) == FAILURE
) {
2482 * Shorten the alarm timer in case someone else's child dies
2483 * and free up a slot in the process table.
2488 * Wait for some children to die. Since efork()
2489 * is always called with SIGCLD blocked, unblock
2490 * it here so that child death signals can come in.
2492 (void) sigrelse(SIGCLD
);
2494 (void) sighold(SIGCLD
);
2498 if (childpid
!= 0) {
2500 if (process
== NULLPROC
) {
2502 * No proc table pointer specified so search
2505 for (process
= proc_table
; process
->p_flags
!= 0 &&
2506 (process
< proc_table
+ num_proc
); process
++)
2509 if (process
== (proc_table
+ num_proc
)) {
2510 int old_proc_table_size
= num_proc
;
2512 /* Increase the process table size */
2513 increase_proc_table_size();
2514 if (old_proc_table_size
== num_proc
) {
2515 /* didn't grow: memory failure */
2519 proc_table
+ old_proc_table_size
;
2523 process
->p_time
= 0L;
2524 process
->p_count
= 0;
2526 process
->p_id
[0] = '\0';
2527 process
->p_id
[1] = '\0';
2528 process
->p_id
[2] = '\0';
2529 process
->p_id
[3] = '\0';
2530 process
->p_pid
= childpid
;
2531 process
->p_flags
= (LIVING
| OCCUPIED
| modes
);
2532 process
->p_exit
= 0;
2536 if ((action
& (M_WAIT
| M_BOOTWAIT
)) == 0)
2542 * Reset all signals to the system defaults.
2544 for (i
= SIGHUP
; i
<= SIGRTMAX
; i
++)
2545 (void) sigset(i
, SIG_DFL
);
2548 * POSIX B.2.2.2 advises that init should set SIGTTOU,
2549 * SIGTTIN, and SIGTSTP to SIG_IGN.
2551 * Make sure that SIGXCPU and SIGXFSZ also remain ignored,
2552 * for backward compatibility.
2554 (void) sigset(SIGTTIN
, SIG_IGN
);
2555 (void) sigset(SIGTTOU
, SIG_IGN
);
2556 (void) sigset(SIGTSTP
, SIG_IGN
);
2557 (void) sigset(SIGXCPU
, SIG_IGN
);
2558 (void) sigset(SIGXFSZ
, SIG_IGN
);
2565 * waitproc() waits for a specified process to die. For this function to
2566 * work, the specified process must already in the proc_table. waitproc()
2567 * returns the exit status of the specified process when it dies.
2570 waitproc(struct PROC_TABLE
*process
)
2573 sigset_t oldmask
, newmask
, zeromask
;
2575 (void) sigemptyset(&zeromask
);
2576 (void) sigemptyset(&newmask
);
2578 (void) sigaddset(&newmask
, SIGCLD
);
2580 /* Block SIGCLD and save the current signal mask */
2581 if (sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
) < 0)
2582 perror("SIG_BLOCK error");
2585 * Wait around until the process dies.
2587 if (process
->p_flags
& LIVING
)
2588 (void) sigsuspend(&zeromask
);
2590 /* Reset signal mask to unblock SIGCLD */
2591 if (sigprocmask(SIG_SETMASK
, &oldmask
, NULL
) < 0)
2592 perror("SIG_SETMASK error");
2594 if (process
->p_flags
& LIVING
)
2598 * Make sure to only return 16 bits so that answer will always
2599 * be positive whenever the process of interest really died.
2601 answer
= (process
->p_exit
& 0xffff);
2604 * Free the slot in the proc_table.
2606 process
->p_flags
= 0;
2611 * notify_pam_dead(): calls into the PAM framework to close the given session.
2614 notify_pam_dead(struct utmpx
*up
)
2617 char user
[sizeof (up
->ut_user
) + 1];
2618 char ttyn
[sizeof (up
->ut_line
) + 1];
2619 char host
[sizeof (up
->ut_host
) + 1];
2622 * PAM does not take care of updating utmpx/wtmpx.
2624 (void) snprintf(user
, sizeof (user
), "%s", up
->ut_user
);
2625 (void) snprintf(ttyn
, sizeof (ttyn
), "%s", up
->ut_line
);
2626 (void) snprintf(host
, sizeof (host
), "%s", up
->ut_host
);
2628 if (pam_start("init", user
, NULL
, &pamh
) == PAM_SUCCESS
) {
2629 (void) pam_set_item(pamh
, PAM_TTY
, ttyn
);
2630 (void) pam_set_item(pamh
, PAM_RHOST
, host
);
2631 (void) pam_close_session(pamh
, 0);
2632 (void) pam_end(pamh
, PAM_SUCCESS
);
2637 * Check you can access utmpx (As / may be read-only and
2638 * /var may not be mounted yet).
2644 utmpx_ok
= (access(UTMPX
, R_OK
|W_OK
) == 0);
2645 } while (!utmpx_ok
&& errno
== EINTR
);
2651 * account() updates entries in utmpx and appends new entries to the end of
2652 * wtmpx (assuming they exist). The program argument indicates the name of
2653 * program if INIT_PROCESS, otherwise should be NULL.
2655 * account() only blocks for INIT_PROCESS requests.
2657 * Returns non-zero if write failed.
2660 account(short state
, struct PROC_TABLE
*process
, char *program
)
2662 struct utmpx utmpbuf
, *u
, *oldu
;
2664 char fail_buf
[UT_LINE_SZ
];
2665 sigset_t block
, unblock
;
2667 if (!utmpx_ok
&& !access_utmpx()) {
2672 * Set up the prototype for the utmp structure we want to write.
2675 (void) memset(u
, 0, sizeof (struct utmpx
));
2678 * Fill in the various fields of the utmp structure.
2680 u
->ut_id
[0] = process
->p_id
[0];
2681 u
->ut_id
[1] = process
->p_id
[1];
2682 u
->ut_id
[2] = process
->p_id
[2];
2683 u
->ut_id
[3] = process
->p_id
[3];
2684 u
->ut_pid
= process
->p_pid
;
2687 * Fill the "ut_exit" structure.
2689 u
->ut_exit
.e_termination
= WTERMSIG(process
->p_exit
);
2690 u
->ut_exit
.e_exit
= WEXITSTATUS(process
->p_exit
);
2693 (void) time(&u
->ut_tv
.tv_sec
);
2696 * Block signals for utmp update.
2698 (void) sigfillset(&block
);
2699 (void) sigprocmask(SIG_BLOCK
, &block
, &unblock
);
2702 * See if there already is such an entry in the "utmpx" file.
2704 setutxent(); /* Start at beginning of utmpx file. */
2706 if ((oldu
= getutxid(u
)) != NULL
) {
2708 * Copy in the old "user", "line" and "host" fields
2709 * to our new structure.
2711 bcopy(oldu
->ut_user
, u
->ut_user
, sizeof (u
->ut_user
));
2712 bcopy(oldu
->ut_line
, u
->ut_line
, sizeof (u
->ut_line
));
2713 bcopy(oldu
->ut_host
, u
->ut_host
, sizeof (u
->ut_host
));
2714 u
->ut_syslen
= (tmplen
= strlen(u
->ut_host
)) ?
2715 min(tmplen
+ 1, sizeof (u
->ut_host
)) : 0;
2717 if (oldu
->ut_type
== USER_PROCESS
&& state
== DEAD_PROCESS
) {
2718 notify_pam_dead(oldu
);
2723 * Perform special accounting. Insert the special string into the
2724 * ut_line array. For INIT_PROCESSes put in the name of the
2725 * program in the "ut_user" field.
2729 (void) strncpy(u
->ut_user
, program
, sizeof (u
->ut_user
));
2730 (void) strcpy(fail_buf
, "INIT_PROCESS");
2734 (void) strlcpy(fail_buf
, u
->ut_id
, sizeof (u
->ut_id
) + 1);
2739 * Write out the updated entry to utmpx file.
2741 if (pututxline(u
) == NULL
) {
2742 console(B_TRUE
, "Failed write of utmpx entry: \"%s\": %s\n",
2743 fail_buf
, strerror(errno
));
2745 (void) sigprocmask(SIG_SETMASK
, &unblock
, NULL
);
2750 * If we're able to write to utmpx, then attempt to add to the
2751 * end of the wtmpx file.
2757 (void) sigprocmask(SIG_SETMASK
, &unblock
, NULL
);
2763 clearent(pid_t pid
, short status
)
2766 sigset_t block
, unblock
;
2769 * Block signals for utmp update.
2771 (void) sigfillset(&block
);
2772 (void) sigprocmask(SIG_BLOCK
, &block
, &unblock
);
2775 * No error checking for now.
2779 while (up
= getutxent()) {
2780 if (up
->ut_pid
== pid
) {
2781 if (up
->ut_type
== DEAD_PROCESS
) {
2783 * Cleaned up elsewhere.
2788 notify_pam_dead(up
);
2790 up
->ut_type
= DEAD_PROCESS
;
2791 up
->ut_exit
.e_termination
= WTERMSIG(status
);
2792 up
->ut_exit
.e_exit
= WEXITSTATUS(status
);
2793 (void) time(&up
->ut_tv
.tv_sec
);
2795 (void) pututxline(up
);
2797 * Now attempt to add to the end of the
2798 * wtmp and wtmpx files. Do not create
2799 * if they don't already exist.
2801 updwtmpx(WTMPX
, up
);
2808 (void) sigprocmask(SIG_SETMASK
, &unblock
, NULL
);
2812 * prog_name() searches for the word or unix path name and
2813 * returns a pointer to the last element of the pathname.
2816 prog_name(char *string
)
2819 static char word
[UT_USER_SZ
+ 1];
2822 * Search for the first word skipping leading spaces and tabs.
2824 while (*string
== ' ' || *string
== '\t')
2828 * If the first non-space non-tab character is not one allowed in
2829 * a word, return a pointer to a null string, otherwise parse the
2832 if (*string
!= '.' && *string
!= '/' && *string
!= '_' &&
2833 (*string
< 'a' || *string
> 'z') &&
2834 (*string
< 'A' || * string
> 'Z') &&
2835 (*string
< '0' || *string
> '9'))
2839 * Parse the pathname looking forward for '/', ' ', '\t', '\n' or
2840 * '\0'. Each time a '/' is found, move "ptr" to one past the
2841 * '/', thus when a ' ', '\t', '\n', or '\0' is found, "ptr" will
2842 * point to the last element of the pathname.
2844 for (ptr
= string
; *string
!= ' ' && *string
!= '\t' &&
2845 *string
!= '\n' && *string
!= '\0'; string
++) {
2851 * Copy out up to the size of the "ut_user" array into "word",
2852 * null terminate it and return a pointer to it.
2854 for (ptr2
= &word
[0]; ptr2
< &word
[UT_USER_SZ
] &&
2855 ptr
< string
; /* CSTYLED */)
2864 * realcon() returns a nonzero value if there is a character device
2865 * associated with SYSCON that has the same device number as CONSOLE.
2870 struct stat sconbuf
, conbuf
;
2872 if (stat(SYSCON
, &sconbuf
) != -1 &&
2873 stat(CONSOLE
, &conbuf
) != -1 &&
2874 S_ISCHR(sconbuf
.st_mode
) &&
2875 S_ISCHR(conbuf
.st_mode
) &&
2876 sconbuf
.st_rdev
== conbuf
.st_rdev
) {
2885 * get_ioctl_syscon() retrieves the SYSCON settings from the IOCTLSYSCON file.
2886 * Returns true if the IOCTLSYSCON file needs to be written (with
2887 * write_ioctl_syscon() below)
2893 unsigned int iflags
, oflags
, cflags
, lflags
, ldisc
, cc
[18];
2894 int i
, valid_format
= 0;
2897 * Read in the previous modes for SYSCON from IOCTLSYSCON.
2899 if ((fp
= fopen(IOCTLSYSCON
, "r")) == NULL
) {
2900 stored_syscon_termios
= dflt_termios
;
2902 "warning:%s does not exist, default settings assumed\n",
2907 "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x",
2908 &iflags
, &oflags
, &cflags
, &lflags
,
2909 &cc
[0], &cc
[1], &cc
[2], &cc
[3], &cc
[4], &cc
[5], &cc
[6],
2910 &cc
[7], &cc
[8], &cc
[9], &cc
[10], &cc
[11], &cc
[12], &cc
[13],
2911 &cc
[14], &cc
[15], &cc
[16], &cc
[17]);
2914 stored_syscon_termios
.c_iflag
= iflags
;
2915 stored_syscon_termios
.c_oflag
= oflags
;
2916 stored_syscon_termios
.c_cflag
= cflags
;
2917 stored_syscon_termios
.c_lflag
= lflags
;
2918 for (i
= 0; i
< 18; i
++)
2919 stored_syscon_termios
.c_cc
[i
] = (char)cc
[i
];
2921 } else if (i
== 13) {
2923 i
= fscanf(fp
, "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x",
2924 &iflags
, &oflags
, &cflags
, &lflags
, &ldisc
, &cc
[0], &cc
[1],
2925 &cc
[2], &cc
[3], &cc
[4], &cc
[5], &cc
[6], &cc
[7]);
2928 * If the file is formatted properly, use the values to
2929 * initialize the console terminal condition.
2931 stored_syscon_termios
.c_iflag
= (ushort_t
)iflags
;
2932 stored_syscon_termios
.c_oflag
= (ushort_t
)oflags
;
2933 stored_syscon_termios
.c_cflag
= (ushort_t
)cflags
;
2934 stored_syscon_termios
.c_lflag
= (ushort_t
)lflags
;
2935 for (i
= 0; i
< 8; i
++)
2936 stored_syscon_termios
.c_cc
[i
] = (char)cc
[i
];
2941 /* If the file is badly formatted, use the default settings. */
2943 stored_syscon_termios
= dflt_termios
;
2946 /* If the file had a bad format, rewrite it later. */
2947 return (!valid_format
);
2952 write_ioctl_syscon()
2957 (void) unlink(SYSCON
);
2958 (void) link(SYSTTY
, SYSCON
);
2960 fp
= fopen(IOCTLSYSCON
, "w");
2962 (void) fprintf(fp
, "%x:%x:%x:%x:0", stored_syscon_termios
.c_iflag
,
2963 stored_syscon_termios
.c_oflag
, stored_syscon_termios
.c_cflag
,
2964 stored_syscon_termios
.c_lflag
);
2965 for (i
= 0; i
< 8; ++i
)
2966 (void) fprintf(fp
, ":%x", stored_syscon_termios
.c_cc
[i
]);
2967 (void) putc('\n', fp
);
2970 (void) fsync(fileno(fp
));
2972 (void) umask(cmask
);
2977 * void console(boolean_t, char *, ...)
2978 * Outputs the requested message to the system console. Note that the number
2979 * of arguments passed to console() should be determined by the print format.
2981 * The "prefix" parameter indicates whether or not "INIT: " should precede the
2984 * To make sure we write to the console in a sane fashion, we use the modes
2985 * we keep in stored_syscon_termios (which we read out of /etc/ioctl.syscon).
2986 * Afterwards we restore whatever modes were already there.
2990 console(boolean_t prefix
, char *format
, ...)
2992 char outbuf
[BUFSIZ
];
2995 struct termios old_syscon_termios
;
2999 * We open SYSCON anew each time in case it has changed (see
3002 if ((fd
= open(SYSCON
, O_RDWR
| O_NOCTTY
)) < 0 ||
3003 (f
= fdopen(fd
, "r+")) == NULL
) {
3005 syslog(LOG_WARNING
, "INIT: ");
3006 va_start(args
, format
);
3007 vsyslog(LOG_WARNING
, format
, args
);
3013 setbuf(f
, &outbuf
[0]);
3015 getret
= tcgetattr(fd
, &old_syscon_termios
);
3016 old_syscon_termios
.c_cflag
&= ~HUPCL
;
3018 /* Don't overwrite cflag of real console. */
3019 stored_syscon_termios
.c_cflag
= old_syscon_termios
.c_cflag
;
3021 stored_syscon_termios
.c_cflag
&= ~HUPCL
;
3023 (void) tcsetattr(fd
, TCSANOW
, &stored_syscon_termios
);
3026 (void) fprintf(f
, "\nINIT: ");
3027 va_start(args
, format
);
3028 (void) vfprintf(f
, format
, args
);
3032 (void) tcsetattr(fd
, TCSADRAIN
, &old_syscon_termios
);
3038 * timer() is a substitute for sleep() which uses alarm() and pause().
3044 while (time_up
== FALSE
)
3049 setimer(int timelimit
)
3052 (void) alarm(timelimit
);
3053 time_up
= (timelimit
? FALSE
: TRUE
);
3058 * ENOMEM - out of memory
3059 * ECONNABORTED - repository connection broken
3060 * EPERM - permission denied
3061 * EACCES - backend access denied
3062 * EROFS - backend readonly
3065 get_or_add_startd(scf_instance_t
*inst
)
3068 scf_scope_t
*scope
= NULL
;
3069 scf_service_t
*svc
= NULL
;
3072 h
= scf_instance_handle(inst
);
3074 if (scf_handle_decode_fmri(h
, SCF_SERVICE_STARTD
, NULL
, NULL
, inst
,
3075 NULL
, NULL
, SCF_DECODE_FMRI_EXACT
) == 0)
3078 switch (scf_error()) {
3079 case SCF_ERROR_CONNECTION_BROKEN
:
3080 return (ECONNABORTED
);
3082 case SCF_ERROR_NOT_FOUND
:
3085 case SCF_ERROR_HANDLE_MISMATCH
:
3086 case SCF_ERROR_INVALID_ARGUMENT
:
3087 case SCF_ERROR_CONSTRAINT_VIOLATED
:
3089 bad_error("scf_handle_decode_fmri", scf_error());
3092 /* Make sure we're right, since we're adding piece-by-piece. */
3093 assert(strcmp(SCF_SERVICE_STARTD
,
3094 "svc:/system/svc/restarter:default") == 0);
3096 if ((scope
= scf_scope_create(h
)) == NULL
||
3097 (svc
= scf_service_create(h
)) == NULL
) {
3103 if (scf_handle_get_scope(h
, SCF_SCOPE_LOCAL
, scope
) != 0) {
3104 switch (scf_error()) {
3105 case SCF_ERROR_CONNECTION_BROKEN
:
3109 case SCF_ERROR_NOT_FOUND
:
3110 (void) fputs(gettext(
3111 "smf(5) repository missing local scope.\n"),
3116 case SCF_ERROR_HANDLE_MISMATCH
:
3117 case SCF_ERROR_INVALID_ARGUMENT
:
3119 bad_error("scf_handle_get_scope", scf_error());
3124 if (scf_scope_get_service(scope
, "system/svc/restarter", svc
) != 0) {
3125 switch (scf_error()) {
3126 case SCF_ERROR_CONNECTION_BROKEN
:
3130 case SCF_ERROR_DELETED
:
3133 case SCF_ERROR_NOT_FOUND
:
3136 case SCF_ERROR_HANDLE_MISMATCH
:
3137 case SCF_ERROR_INVALID_ARGUMENT
:
3138 case SCF_ERROR_NOT_SET
:
3140 bad_error("scf_scope_get_service", scf_error());
3144 if (scf_scope_add_service(scope
, "system/svc/restarter", svc
) !=
3146 switch (scf_error()) {
3147 case SCF_ERROR_CONNECTION_BROKEN
:
3151 case SCF_ERROR_EXISTS
:
3154 case SCF_ERROR_PERMISSION_DENIED
:
3158 case SCF_ERROR_BACKEND_ACCESS
:
3162 case SCF_ERROR_BACKEND_READONLY
:
3166 case SCF_ERROR_HANDLE_MISMATCH
:
3167 case SCF_ERROR_INVALID_ARGUMENT
:
3168 case SCF_ERROR_NOT_SET
:
3170 bad_error("scf_scope_add_service", scf_error());
3176 if (scf_service_get_instance(svc
, "default", inst
) != 0) {
3177 switch (scf_error()) {
3178 case SCF_ERROR_CONNECTION_BROKEN
:
3182 case SCF_ERROR_DELETED
:
3185 case SCF_ERROR_NOT_FOUND
:
3188 case SCF_ERROR_HANDLE_MISMATCH
:
3189 case SCF_ERROR_INVALID_ARGUMENT
:
3190 case SCF_ERROR_NOT_SET
:
3192 bad_error("scf_service_get_instance", scf_error());
3195 if (scf_service_add_instance(svc
, "default", inst
) !=
3197 switch (scf_error()) {
3198 case SCF_ERROR_CONNECTION_BROKEN
:
3202 case SCF_ERROR_DELETED
:
3205 case SCF_ERROR_EXISTS
:
3208 case SCF_ERROR_PERMISSION_DENIED
:
3212 case SCF_ERROR_BACKEND_ACCESS
:
3216 case SCF_ERROR_BACKEND_READONLY
:
3220 case SCF_ERROR_HANDLE_MISMATCH
:
3221 case SCF_ERROR_INVALID_ARGUMENT
:
3222 case SCF_ERROR_NOT_SET
:
3224 bad_error("scf_service_add_instance",
3233 scf_service_destroy(svc
);
3234 scf_scope_destroy(scope
);
3240 * ECONNABORTED - repository connection broken
3241 * ECANCELED - the transaction's property group was deleted
3244 transaction_add_set(scf_transaction_t
*tx
, scf_transaction_entry_t
*ent
,
3245 const char *pname
, scf_type_t type
)
3248 if (scf_transaction_property_change_type(tx
, ent
, pname
, type
) == 0)
3251 switch (scf_error()) {
3252 case SCF_ERROR_CONNECTION_BROKEN
:
3253 return (ECONNABORTED
);
3255 case SCF_ERROR_DELETED
:
3258 case SCF_ERROR_NOT_FOUND
:
3261 case SCF_ERROR_HANDLE_MISMATCH
:
3262 case SCF_ERROR_INVALID_ARGUMENT
:
3263 case SCF_ERROR_NOT_BOUND
:
3264 case SCF_ERROR_NOT_SET
:
3266 bad_error("scf_transaction_property_change_type", scf_error());
3270 if (scf_transaction_property_new(tx
, ent
, pname
, type
) == 0)
3273 switch (scf_error()) {
3274 case SCF_ERROR_CONNECTION_BROKEN
:
3275 return (ECONNABORTED
);
3277 case SCF_ERROR_DELETED
:
3280 case SCF_ERROR_EXISTS
:
3283 case SCF_ERROR_HANDLE_MISMATCH
:
3284 case SCF_ERROR_INVALID_ARGUMENT
:
3285 case SCF_ERROR_NOT_BOUND
:
3286 case SCF_ERROR_NOT_SET
:
3288 bad_error("scf_transaction_property_new", scf_error());
3296 switch (scf_error()) {
3297 case SCF_ERROR_NO_MEMORY
:
3298 console(B_TRUE
, gettext("Out of memory.\n"));
3301 case SCF_ERROR_CONNECTION_BROKEN
:
3302 console(B_TRUE
, gettext(
3303 "Connection to smf(5) repository server broken.\n"));
3306 case SCF_ERROR_NO_RESOURCES
:
3307 console(B_TRUE
, gettext(
3308 "smf(5) repository server is out of memory.\n"));
3311 case SCF_ERROR_PERMISSION_DENIED
:
3312 console(B_TRUE
, gettext("Insufficient privileges.\n"));
3316 console(B_TRUE
, gettext("libscf error: %s\n"),
3317 scf_strerror(scf_error()));
3322 lscf_set_runlevel(char rl
)
3325 scf_instance_t
*inst
= NULL
;
3326 scf_propertygroup_t
*pg
= NULL
;
3327 scf_transaction_t
*tx
= NULL
;
3328 scf_transaction_entry_t
*ent
= NULL
;
3329 scf_value_t
*val
= NULL
;
3333 h
= scf_handle_create(SCF_VERSION
);
3339 if (scf_handle_bind(h
) != 0) {
3340 switch (scf_error()) {
3341 case SCF_ERROR_NO_SERVER
:
3343 gettext("smf(5) repository server not running.\n"));
3352 if ((inst
= scf_instance_create(h
)) == NULL
||
3353 (pg
= scf_pg_create(h
)) == NULL
||
3354 (val
= scf_value_create(h
)) == NULL
||
3355 (tx
= scf_transaction_create(h
)) == NULL
||
3356 (ent
= scf_entry_create(h
)) == NULL
) {
3362 r
= get_or_add_startd(inst
);
3375 bad_error("get_or_add_startd", r
);
3379 if (scf_instance_get_pg(inst
, SCF_PG_OPTIONS_OVR
, pg
) != 0) {
3380 switch (scf_error()) {
3381 case SCF_ERROR_CONNECTION_BROKEN
:
3385 case SCF_ERROR_DELETED
:
3388 case SCF_ERROR_NOT_FOUND
:
3391 case SCF_ERROR_HANDLE_MISMATCH
:
3392 case SCF_ERROR_INVALID_ARGUMENT
:
3393 case SCF_ERROR_NOT_SET
:
3395 bad_error("scf_instance_get_pg", scf_error());
3399 if (scf_instance_add_pg(inst
, SCF_PG_OPTIONS_OVR
,
3400 SCF_PG_OPTIONS_OVR_TYPE
, SCF_PG_OPTIONS_OVR_FLAGS
, pg
) !=
3402 switch (scf_error()) {
3403 case SCF_ERROR_CONNECTION_BROKEN
:
3404 case SCF_ERROR_PERMISSION_DENIED
:
3405 case SCF_ERROR_BACKEND_ACCESS
:
3409 case SCF_ERROR_DELETED
:
3412 case SCF_ERROR_EXISTS
:
3415 case SCF_ERROR_HANDLE_MISMATCH
:
3416 case SCF_ERROR_INVALID_ARGUMENT
:
3417 case SCF_ERROR_NOT_SET
:
3419 bad_error("scf_instance_add_pg", scf_error());
3426 r
= scf_value_set_astring(val
, buf
);
3430 if (scf_transaction_start(tx
, pg
) != 0) {
3431 switch (scf_error()) {
3432 case SCF_ERROR_CONNECTION_BROKEN
:
3433 case SCF_ERROR_PERMISSION_DENIED
:
3434 case SCF_ERROR_BACKEND_ACCESS
:
3438 case SCF_ERROR_DELETED
:
3441 case SCF_ERROR_HANDLE_MISMATCH
:
3442 case SCF_ERROR_NOT_BOUND
:
3443 case SCF_ERROR_IN_USE
:
3444 case SCF_ERROR_NOT_SET
:
3446 bad_error("scf_transaction_start", scf_error());
3450 r
= transaction_add_set(tx
, ent
, "runlevel", SCF_TYPE_ASTRING
);
3460 scf_transaction_reset(tx
);
3464 bad_error("transaction_add_set", r
);
3467 r
= scf_entry_add_value(ent
, val
);
3470 r
= scf_transaction_commit(tx
);
3475 switch (scf_error()) {
3476 case SCF_ERROR_CONNECTION_BROKEN
:
3477 case SCF_ERROR_PERMISSION_DENIED
:
3478 case SCF_ERROR_BACKEND_ACCESS
:
3479 case SCF_ERROR_BACKEND_READONLY
:
3483 case SCF_ERROR_DELETED
:
3484 scf_transaction_reset(tx
);
3487 case SCF_ERROR_INVALID_ARGUMENT
:
3488 case SCF_ERROR_NOT_BOUND
:
3489 case SCF_ERROR_NOT_SET
:
3491 bad_error("scf_transaction_commit",
3496 scf_transaction_reset(tx
);
3497 (void) scf_pg_update(pg
);
3501 scf_transaction_destroy(tx
);
3502 scf_entry_destroy(ent
);
3503 scf_value_destroy(val
);
3505 scf_instance_destroy(inst
);
3507 (void) scf_handle_unbind(h
);
3508 scf_handle_destroy(h
);
3512 * Function to handle requests from users to main init running as process 1.
3515 userinit(int argc
, char **argv
)
3520 struct stat sconbuf
, conbuf
;
3521 const char *usage_msg
= "Usage: init [0123456SsQqabc]\n";
3524 * We are a user invoked init. Is there an argument and is it
3525 * a single character? If not, print usage message and quit.
3527 if (argc
!= 2 || argv
[1][1] != '\0') {
3528 (void) fprintf(stderr
, usage_msg
);
3532 if ((init_signal
= lvlname_to_state((char)argv
[1][0])) == -1) {
3533 (void) fprintf(stderr
, usage_msg
);
3534 (void) audit_put_record(ADT_FAILURE
, ADT_FAIL_VALUE_BAD_CMD
,
3539 if (init_signal
== SINGLE_USER
) {
3541 * Make sure this process is talking to a legal tty line
3542 * and that /dev/syscon is linked to this line.
3544 ln
= ttyname(0); /* Get the name of tty */
3546 (void) fprintf(stderr
,
3547 "Standard input not a tty line\n");
3548 (void) audit_put_record(ADT_FAILURE
,
3549 ADT_FAIL_VALUE_BAD_TTY
, argv
[1]);
3553 if ((stat(ln
, &sconbuf
) != -1) &&
3554 (stat(SYSCON
, &conbuf
) == -1 ||
3555 sconbuf
.st_rdev
!= conbuf
.st_rdev
)) {
3557 * /dev/syscon needs to change.
3558 * Unlink /dev/syscon and relink it to the current line.
3560 if (lstat(SYSCON
, &conbuf
) != -1 &&
3561 unlink(SYSCON
) == FAILURE
) {
3562 perror("Can't unlink /dev/syscon");
3563 (void) fprintf(stderr
,
3564 "Run command on the system console.\n");
3565 (void) audit_put_record(ADT_FAILURE
,
3566 ADT_FAIL_VALUE_PROGRAM
, argv
[1]);
3569 if (symlink(ln
, SYSCON
) == FAILURE
) {
3570 (void) fprintf(stderr
,
3571 "Can't symlink /dev/syscon to %s: %s", ln
,
3574 /* Try to leave a syscon */
3575 (void) link(SYSTTY
, SYSCON
);
3576 (void) audit_put_record(ADT_FAILURE
,
3577 ADT_FAIL_VALUE_PROGRAM
, argv
[1]);
3582 * Try to leave a message on system console saying where
3583 * /dev/syscon is currently connected.
3585 if ((fp
= fopen(SYSTTY
, "r+")) != NULL
) {
3587 "\n**** SYSCON CHANGED TO %s ****\n",
3594 update_boot_archive(init_signal
);
3596 (void) audit_put_record(ADT_SUCCESS
, ADT_SUCCESS
, argv
[1]);
3599 * Signal init; init will take care of telling svc.startd.
3601 if (kill(init_pid
, init_signal
) == FAILURE
) {
3602 (void) fprintf(stderr
, "Must be super-user\n");
3603 (void) audit_put_record(ADT_FAILURE
,
3604 ADT_FAIL_VALUE_AUTH
, argv
[1]);
3612 #define DELTA 25 /* Number of pidlist elements to allocate at a time */
3619 struct pidrec
*p
= &prec
;
3620 struct pidlist
*plp
;
3621 struct pidlist
*tp
, *savetp
;
3630 * Important Note: Either read will really fail (in which case
3631 * return is all we can do) or will get EAGAIN (Pfd was opened
3632 * O_NDELAY), in which case we also want to return.
3633 * Always return from here!
3635 if (read(Pfd
, p
, sizeof (struct pidrec
)) !=
3636 sizeof (struct pidrec
)) {
3639 switch (p
->pd_type
) {
3643 * New "godchild", add to list.
3645 if (Plfree
== NULL
) {
3646 plp
= (struct pidlist
*)calloc(DELTA
,
3647 sizeof (struct pidlist
));
3649 /* Can't save pid */
3653 * Point at 2nd record allocated, we'll use plp.
3657 * Link them into a chain.
3660 for (i
= 0; i
< DELTA
- 2; i
++) {
3661 tp
->pl_next
= tp
+ 1;
3666 Plfree
= plp
->pl_next
;
3668 plp
->pl_pid
= p
->pd_pid
;
3670 plp
->pl_next
= NULL
;
3672 * Note - pid list is kept in increasing order of pids.
3674 if (Plhead
== NULL
) {
3676 /* Back up to read next record */
3679 savetp
= tp
= Plhead
;
3681 if (plp
->pl_pid
> tp
->pl_pid
) {
3685 } else if (plp
->pl_pid
< tp
->pl_pid
) {
3687 plp
->pl_next
= Plhead
;
3692 savetp
->pl_next
= plp
;
3696 /* Already in list! */
3697 plp
->pl_next
= Plfree
;
3703 /* Add to end of list */
3704 savetp
->pl_next
= plp
;
3707 /* Back up to read next record. */
3712 * This one was handled by someone else,
3713 * purge it from the list.
3715 if (Plhead
== NULL
) {
3716 /* Back up to read next record. */
3719 savetp
= tp
= Plhead
;
3721 if (p
->pd_pid
> tp
->pl_pid
) {
3722 /* Keep on looking. */
3726 } else if (p
->pd_pid
< tp
->pl_pid
) {
3732 Plhead
= tp
->pl_next
;
3734 savetp
->pl_next
= tp
->pl_next
;
3735 tp
->pl_next
= Plfree
;
3740 /* Back up to read next record. */
3743 console(B_TRUE
, "Bad message on initpipe\n");
3753 struct pidlist
*savep
, *p
;
3757 (void) sighold(SIGCLD
);
3758 Gchild
= 0; /* Note - Safe to do this here since no SIGCLDs */
3759 (void) sighold(SIGPOLL
);
3764 * Found an entry to delete,
3765 * remove it from list first.
3768 status
= p
->pl_exit
;
3770 Plhead
= p
->pl_next
;
3771 p
->pl_next
= Plfree
;
3775 savep
->pl_next
= p
->pl_next
;
3776 p
->pl_next
= Plfree
;
3780 clearent(pid
, status
);
3786 (void) sigrelse(SIGPOLL
);
3787 (void) sigrelse(SIGCLD
);
3792 * /etc/inittab has more entries and we have run out of room in the proc_table
3793 * array. Double the size of proc_table to accomodate the extra entries.
3796 increase_proc_table_size()
3798 sigset_t block
, unblock
;
3800 size_t delta
= num_proc
* sizeof (struct PROC_TABLE
);
3804 * Block signals for realloc.
3806 (void) sigfillset(&block
);
3807 (void) sigprocmask(SIG_BLOCK
, &block
, &unblock
);
3811 * On failure we just return because callers of this function check
3815 ptr
= realloc(g_state
, g_state_sz
+ delta
);
3816 while (ptr
== NULL
&& errno
== EAGAIN
)
3820 /* ensure that the new part is initialized to zero */
3821 bzero((caddr_t
)ptr
+ g_state_sz
, delta
);
3824 g_state_sz
+= delta
;
3829 /* unblock our signals before returning */
3830 (void) sigprocmask(SIG_SETMASK
, &unblock
, NULL
);
3836 * Sanity check g_state.
3842 struct PROC_TABLE
*ptp
;
3845 /* Note: cur_state is encoded as a signal number */
3846 if (cur_state
< 1 || cur_state
== 9 || cur_state
> 13)
3849 /* Check num_proc */
3850 if (g_state_sz
!= sizeof (struct init_state
) + (num_proc
- 1) *
3851 sizeof (struct PROC_TABLE
))
3854 /* Check proc_table */
3855 for (i
= 0, ptp
= proc_table
; i
< num_proc
; ++i
, ++ptp
) {
3856 /* skip unoccupied entries */
3857 if (!(ptp
->p_flags
& OCCUPIED
))
3860 /* p_flags has no bits outside of PF_MASK */
3861 if (ptp
->p_flags
& ~(PF_MASK
))
3864 /* 5 <= pid <= MAXPID */
3865 if (ptp
->p_pid
< 5 || ptp
->p_pid
> MAXPID
)
3869 if (ptp
->p_count
< 0)
3873 if (ptp
->p_time
< 0)
3881 * Initialize our state.
3883 * If the system just booted, then init_state_file, which is located on an
3884 * everpresent tmpfs filesystem, should not exist.
3886 * If we were restarted, then init_state_file should exist, in
3887 * which case we'll read it in, sanity check it, and use it.
3889 * Note: You can't call console() until proc_table is ready.
3895 int ret
, st_fd
, insane
= 0;
3904 * If we can exclusively create the file, then we're the
3905 * initial invocation of init(1M).
3907 st_fd
= open(init_state_file
, O_RDWR
| O_CREAT
| O_EXCL
,
3909 } while (st_fd
== -1 && errno
== EINTR
);
3916 st_fd
= open(init_state_file
, O_RDWR
, S_IRUSR
| S_IWUSR
);
3917 } while (st_fd
== -1 && errno
== EINTR
);
3921 /* Get the size of the file. */
3923 ret
= fstat(st_fd
, &stb
);
3924 while (ret
== -1 && errno
== EINTR
)
3930 g_state
= malloc(stb
.st_size
);
3931 while (g_state
== NULL
&& errno
== EAGAIN
)
3933 if (g_state
== NULL
)
3936 to_be_read
= stb
.st_size
;
3937 ptr
= (char *)g_state
;
3938 while (to_be_read
> 0) {
3941 read_ret
= read(st_fd
, ptr
, to_be_read
);
3949 to_be_read
-= read_ret
;
3953 (void) close(st_fd
);
3955 g_state_sz
= stb
.st_size
;
3958 console(B_TRUE
, "Restarting.\n");
3966 (void) close(st_fd
);
3968 (void) unlink(init_state_file
);
3972 /* Something went wrong, so allocate new state. */
3973 g_state_sz
= sizeof (struct init_state
) +
3974 ((init_num_proc
- 1) * sizeof (struct PROC_TABLE
));
3976 g_state
= calloc(1, g_state_sz
);
3977 while (g_state
== NULL
&& errno
== EAGAIN
)
3979 if (g_state
== NULL
) {
3984 g_state
->ist_runlevel
= -1;
3985 num_proc
= init_num_proc
;
3988 console(B_TRUE
, "Restarting.\n");
3990 /* Overwrite the bad state file. */
3995 "Error accessing persistent state file `%s'. "
3996 "Ignored.\n", init_state_file
);
3999 "Persistent state file `%s' is invalid and was "
4000 "ignored.\n", init_state_file
);
4006 * Write g_state out to the state file.
4011 static int complained
= 0;
4020 st_fd
= open(init_next_state_file
,
4021 O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
4022 } while (st_fd
< 0 && errno
== EINTR
);
4026 cp
= (char *)g_state
;
4029 ret
= write(st_fd
, cp
, sz
);
4041 (void) close(st_fd
);
4043 if (rename(init_next_state_file
, init_state_file
)) {
4044 (void) unlink(init_next_state_file
);
4053 (void) close(st_fd
);
4055 if (!booting
&& !complained
) {
4057 * Only complain after the filesystem should have come up.
4058 * And only do it once so we don't loop between console()
4063 console(B_TRUE
, "Couldn't write persistent state "
4064 "file `%s'.\n", init_state_file
);
4066 console(B_TRUE
, "Couldn't move persistent state "
4067 "file `%s' to `%s'.\n", init_next_state_file
,
4073 * Create a contract with these parameters.
4076 contract_make_template(uint_t info
, uint_t critical
, uint_t fatal
,
4081 char *ioctl_tset_emsg
=
4082 "Couldn't set \"%s\" contract template parameter: %s.\n";
4085 fd
= open64(CTFS_ROOT
"/process/template", O_RDWR
);
4086 while (fd
< 0 && errno
== EINTR
)
4089 console(B_TRUE
, "Couldn't create process template: %s.\n",
4094 if (err
= ct_pr_tmpl_set_param(fd
, CT_PR_INHERIT
| CT_PR_REGENT
))
4095 console(B_TRUE
, "Contract set template inherit, regent "
4096 "failed: %s.\n", strerror(err
));
4099 * These errors result in a misconfigured template, which is better
4100 * than no template at all, so warn but don't abort.
4102 if (err
= ct_tmpl_set_informative(fd
, info
))
4103 console(B_TRUE
, ioctl_tset_emsg
, "informative", strerror(err
));
4105 if (err
= ct_tmpl_set_critical(fd
, critical
))
4106 console(B_TRUE
, ioctl_tset_emsg
, "critical", strerror(err
));
4108 if (err
= ct_pr_tmpl_set_fatal(fd
, fatal
))
4109 console(B_TRUE
, ioctl_tset_emsg
, "fatal", strerror(err
));
4111 if (err
= ct_tmpl_set_cookie(fd
, cookie
))
4112 console(B_TRUE
, ioctl_tset_emsg
, "cookie", strerror(err
));
4114 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
4120 * Create the templates and open an event file descriptor. We use dup2(2) to
4121 * get these descriptors away from the stdin/stdout/stderr group.
4129 * Create & configure a legacy template. We only want empty events so
4130 * we know when to abandon them.
4132 legacy_tmpl
= contract_make_template(0, CT_PR_EV_EMPTY
, CT_PR_EV_HWERR
,
4134 if (legacy_tmpl
>= 0) {
4135 err
= ct_tmpl_activate(legacy_tmpl
);
4137 (void) close(legacy_tmpl
);
4140 "Couldn't activate legacy template (%s); "
4141 "legacy services will be in init's contract.\n",
4146 "Legacy services will be in init's contract.\n");
4148 if (dup2(legacy_tmpl
, 255) == -1) {
4149 console(B_TRUE
, "Could not duplicate legacy template: %s.\n",
4152 (void) close(legacy_tmpl
);
4156 (void) fcntl(legacy_tmpl
, F_SETFD
, FD_CLOEXEC
);
4158 startd_tmpl
= contract_make_template(0, CT_PR_EV_EMPTY
,
4159 CT_PR_EV_HWERR
| CT_PR_EV_SIGNAL
| CT_PR_EV_CORE
, STARTD_COOKIE
);
4161 if (dup2(startd_tmpl
, 254) == -1) {
4162 console(B_TRUE
, "Could not duplicate startd template: %s.\n",
4165 (void) close(startd_tmpl
);
4169 (void) fcntl(startd_tmpl
, F_SETFD
, FD_CLOEXEC
);
4171 if (legacy_tmpl
< 0 && startd_tmpl
< 0) {
4172 /* The creation errors have already been reported. */
4174 "Ignoring contract events. Core smf(5) services will not "
4180 * Open an event endpoint.
4183 fd
= open64(CTFS_ROOT
"/process/pbundle", O_RDONLY
);
4184 while (fd
< 0 && errno
== EINTR
)
4188 "Couldn't open process pbundle: %s. Core smf(5) services "
4189 "will not be restarted.\n", strerror(errno
));
4193 if (dup2(fd
, 253) == -1) {
4194 console(B_TRUE
, "Could not duplicate process bundle: %s.\n",
4201 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
4203 /* Reset in case we've been restarted. */
4204 (void) ct_event_reset(fd
);
4206 poll_fds
[0].fd
= fd
;
4207 poll_fds
[0].events
= POLLIN
;
4212 contract_getfile(ctid_t id
, const char *name
, int oflag
)
4217 fd
= contract_open(id
, "process", name
, oflag
);
4218 while (fd
< 0 && errno
== EINTR
)
4222 console(B_TRUE
, "Couldn't open %s for contract %ld: %s.\n",
4223 name
, id
, strerror(errno
));
4229 contract_cookie(ctid_t id
, uint64_t *cp
)
4234 fd
= contract_getfile(id
, "status", O_RDONLY
);
4238 err
= ct_status_read(fd
, CTD_COMMON
, &sh
);
4240 console(B_TRUE
, "Couldn't read status of contract %ld: %s.\n",
4248 *cp
= ct_status_get_cookie(sh
);
4255 contract_ack(ct_evthdl_t e
)
4259 if (ct_event_get_flags(e
) & CTE_INFO
)
4262 fd
= contract_getfile(ct_event_get_ctid(e
), "ctl", O_WRONLY
);
4266 (void) ct_ctl_ack(fd
, ct_event_get_evid(e
));
4271 * Process a contract event.
4274 contract_event(struct pollfd
*poll
)
4280 if (!(poll
->revents
& POLLIN
)) {
4281 if (poll
->revents
& POLLERR
)
4283 "Unknown poll error on my process contract "
4288 err
= ct_event_read(poll
->fd
, &e
);
4290 console(B_TRUE
, "Error retrieving contract event: %s.\n",
4295 ctid
= ct_event_get_ctid(e
);
4297 if (ct_event_get_type(e
) == CT_PR_EV_EMPTY
) {
4299 int ret
, abandon
= 1;
4301 /* If it's svc.startd, restart it. Else, abandon. */
4302 ret
= contract_cookie(ctid
, &cookie
);
4305 if (cookie
== STARTD_COOKIE
&&
4306 do_restart_startd
) {
4308 console(B_TRUE
, "Restarting "
4312 * Account for the failure. If the failure rate
4313 * exceeds a threshold, then drop to maintenance
4316 startd_record_failure();
4317 if (startd_failure_rate_critical())
4318 enter_maintenance();
4320 if (startd_tmpl
< 0)
4322 "Restarting svc.startd in "
4323 "improper contract (bad "
4326 (void) startd_run(startd_cline
, startd_tmpl
,
4333 if (abandon
&& (err
= contract_abandon_id(ctid
))) {
4334 console(B_TRUE
, "Couldn't abandon contract %ld: %s.\n",
4335 ctid
, strerror(err
));
4339 * No need to acknowledge the event since either way the
4340 * originating contract should be abandoned.
4344 "Received contract event of unexpected type %d from "
4345 "contract %ld.\n", ct_event_get_type(e
), ctid
);
4347 if ((ct_event_get_flags(e
) & (CTE_INFO
| CTE_ACK
)) == 0)
4348 /* Allow unexpected critical events to be released. */
4356 * svc.startd(1M) Management
4360 * (Re)start svc.startd(1M). old_ctid should be the contract ID of the old
4361 * contract, or 0 if we're starting it for the first time. If wait is true
4362 * we'll wait for and return the exit value of the child.
4365 startd_run(const char *cline
, int tmpl
, ctid_t old_ctid
)
4367 int err
, i
, ret
, did_activate
;
4371 if (cline
[0] == '\0')
4375 * Don't restart startd if the system is rebooting or shutting down.
4378 ret
= stat("/etc/svc/volatile/resetting", &sb
);
4379 } while (ret
== -1 && errno
== EINTR
);
4383 console(B_TRUE
, "Quiescing for reboot.\n");
4388 err
= ct_pr_tmpl_set_transfer(tmpl
, old_ctid
);
4389 if (err
== EINVAL
) {
4390 console(B_TRUE
, "Remake startd_tmpl; reattempt transfer.\n");
4391 tmpl
= startd_tmpl
= contract_make_template(0, CT_PR_EV_EMPTY
,
4392 CT_PR_EV_HWERR
, STARTD_COOKIE
);
4394 err
= ct_pr_tmpl_set_transfer(tmpl
, old_ctid
);
4398 "Couldn't set transfer parameter of contract template: "
4399 "%s.\n", strerror(err
));
4402 if ((err
= ct_pr_tmpl_set_svc_fmri(startd_tmpl
,
4403 SCF_SERVICE_STARTD
)) != 0)
4405 "Can not set svc_fmri in contract template: %s\n",
4407 if ((err
= ct_pr_tmpl_set_svc_aux(startd_tmpl
,
4408 startd_svc_aux
)) != 0)
4410 "Can not set svc_aux in contract template: %s\n",
4412 did_activate
= !(ct_tmpl_activate(tmpl
));
4415 "Template activation failed; not starting \"%s\" in "
4416 "proper contract.\n", cline
);
4418 /* Hold SIGCLD so we can wait if necessary. */
4419 (void) sighold(SIGCLD
);
4421 while ((pid
= fork()) < 0) {
4422 if (errno
== EPERM
) {
4423 console(B_TRUE
, "Insufficient permission to fork.\n");
4425 /* Now that's a doozy. */
4430 "fork() for svc.startd failed: %s. Will retry in 1 "
4431 "second...\n", strerror(errno
));
4435 /* Eventually give up? */
4441 /* See the comment in efork() */
4442 for (i
= SIGHUP
; i
<= SIGRTMAX
; ++i
) {
4443 if (i
== SIGTTOU
|| i
== SIGTTIN
|| i
== SIGTSTP
)
4444 (void) sigset(i
, SIG_IGN
);
4446 (void) sigset(i
, SIG_DFL
);
4449 if (smf_options
!= NULL
) {
4450 /* Put smf_options in the environment. */
4451 glob_envp
[glob_envn
] =
4452 malloc(sizeof ("SMF_OPTIONS=") - 1 +
4453 strlen(smf_options
) + 1);
4455 if (glob_envp
[glob_envn
] != NULL
) {
4457 (void) sprintf(glob_envp
[glob_envn
],
4458 "SMF_OPTIONS=%s", smf_options
);
4459 glob_envp
[glob_envn
+1] = NULL
;
4462 "Could not set SMF_OPTIONS (%s).\n",
4468 console(B_TRUE
, "Executing svc.startd\n");
4470 (void) execle(SH
, "INITSH", "-c", cline
, NULL
, glob_envp
);
4472 console(B_TRUE
, "Could not exec \"%s\" (%s).\n", SH
,
4481 if (legacy_tmpl
< 0 || ct_tmpl_activate(legacy_tmpl
) != 0)
4482 (void) ct_tmpl_clear(tmpl
);
4485 /* Clear the old_ctid reference so the kernel can reclaim it. */
4487 (void) ct_pr_tmpl_set_transfer(tmpl
, 0);
4489 (void) sigrelse(SIGCLD
);
4495 * void startd_record_failure(void)
4496 * Place the current time in our circular array of svc.startd failures.
4499 startd_record_failure()
4501 int index
= startd_failure_index
++ % NSTARTD_FAILURE_TIMES
;
4503 startd_failure_time
[index
] = gethrtime();
4507 * int startd_failure_rate_critical(void)
4508 * Return true if the average failure interval is less than the permitted
4509 * interval. Implicit success if insufficient measurements for an average
4513 startd_failure_rate_critical()
4515 int n
= startd_failure_index
;
4516 hrtime_t avg_ns
= 0;
4518 if (startd_failure_index
< NSTARTD_FAILURE_TIMES
)
4522 (startd_failure_time
[(n
- 1) % NSTARTD_FAILURE_TIMES
] -
4523 startd_failure_time
[n
% NSTARTD_FAILURE_TIMES
]) /
4524 NSTARTD_FAILURE_TIMES
;
4526 return (avg_ns
< STARTD_FAILURE_RATE_NS
);
4530 * returns string that must be free'd
4537 char desc
[] = "booted";
4538 zoneid_t zid
= getzoneid();
4540 b
= malloc(sizeof (desc
) + MAXNAMELEN
+ 3);
4545 p
+= strlcpy(p
, desc
, sizeof (desc
));
4546 if (zid
!= GLOBAL_ZONEID
) {
4547 p
+= strlcpy(p
, ": ", 3);
4548 (void) getzonenamebyid(zid
, p
, MAXNAMELEN
);
4554 * Generate AUE_init_solaris audit record. Return 1 if
4555 * auditing is enabled in case the caller cares.
4557 * In the case of userint() or a local zone invocation of
4558 * one_true_init, the process initially contains the audit
4559 * characteristics of the process that invoked init. The first pass
4560 * through here uses those characteristics then for the case of
4561 * one_true_init in a local zone, clears them so subsequent system
4562 * state changes won't be attributed to the person who booted the
4566 audit_put_record(int pass_fail
, int status
, char *msg
)
4568 adt_session_data_t
*ah
;
4569 adt_event_data_t
*event
;
4571 if (!adt_audit_enabled())
4575 * the PROC_DATA picks up the context to tell whether this is
4576 * an attributed record (auid = -2 is unattributed)
4578 if (adt_start_session(&ah
, NULL
, ADT_USE_PROC_DATA
)) {
4579 console(B_TRUE
, "audit failure: %s\n", strerror(errno
));
4582 event
= adt_alloc_event(ah
, ADT_init_solaris
);
4583 if (event
== NULL
) {
4584 console(B_TRUE
, "audit failure: %s\n", strerror(errno
));
4585 (void) adt_end_session(ah
);
4588 event
->adt_init_solaris
.info
= msg
; /* NULL is ok here */
4590 if (adt_put_event(event
, pass_fail
, status
)) {
4591 console(B_TRUE
, "audit failure: %s\n", strerror(errno
));
4592 (void) adt_end_session(ah
);
4595 adt_free_event(event
);
4597 (void) adt_end_session(ah
);