From 3f66072e3026c697c1d8b5c44d9eb2de561d51b5 Mon Sep 17 00:00:00 2001 From: gdr Date: Sat, 4 Oct 1997 05:24:01 +0000 Subject: [PATCH] initial checkin --- sbin/init/Makefile | 11 + sbin/init/NOTES | 112 +++ sbin/init/init.8 | 296 +++++++ sbin/init/init.c | 1422 ++++++++++++++++++++++++++++++++ sbin/init/pathnames.h | 42 + sbin/reboot/Makefile | 26 + sbin/reboot/boot.hp300.8 | 117 +++ sbin/reboot/boot.i386.8 | 169 ++++ sbin/reboot/boot.sparc.8 | 89 ++ sbin/reboot/boot.tahoe.8 | 152 ++++ sbin/reboot/boot.vax.8 | 322 ++++++++ sbin/reboot/reboot.8 | 97 +++ sbin/reboot/reboot.c | 204 +++++ sbin/shutdown/Makefile | 9 + sbin/shutdown/pathnames.h | 41 + sbin/shutdown/shutdown.8 | 148 ++++ sbin/shutdown/shutdown.c | 468 +++++++++++ usr.sbin/syslogd/Makefile | 9 + usr.sbin/syslogd/pathnames.h | 40 + usr.sbin/syslogd/syslog.conf.5 | 251 ++++++ usr.sbin/syslogd/syslogd.8 | 127 +++ usr.sbin/syslogd/syslogd.c | 1194 +++++++++++++++++++++++++++ 22 files changed, 5346 insertions(+) create mode 100644 sbin/init/Makefile create mode 100644 sbin/init/NOTES create mode 100644 sbin/init/init.8 create mode 100644 sbin/init/init.c create mode 100644 sbin/init/pathnames.h create mode 100644 sbin/reboot/Makefile create mode 100644 sbin/reboot/boot.hp300.8 create mode 100644 sbin/reboot/boot.i386.8 create mode 100644 sbin/reboot/boot.sparc.8 create mode 100644 sbin/reboot/boot.tahoe.8 create mode 100644 sbin/reboot/boot.vax.8 create mode 100644 sbin/reboot/reboot.8 create mode 100644 sbin/reboot/reboot.c create mode 100644 sbin/shutdown/Makefile create mode 100644 sbin/shutdown/pathnames.h create mode 100644 sbin/shutdown/shutdown.8 create mode 100644 sbin/shutdown/shutdown.c create mode 100644 usr.sbin/syslogd/Makefile create mode 100644 usr.sbin/syslogd/pathnames.h create mode 100644 usr.sbin/syslogd/syslog.conf.5 create mode 100644 usr.sbin/syslogd/syslogd.8 create mode 100644 usr.sbin/syslogd/syslogd.c diff --git a/sbin/init/Makefile b/sbin/init/Makefile new file mode 100644 index 0000000..7acd065 --- /dev/null +++ b/sbin/init/Makefile @@ -0,0 +1,11 @@ +# @(#)Makefile 8.1 (Berkeley) 7/19/93 + +PROG= init +MAN8= init.8 +DPADD= ${LIBUTIL} +LDADD= -lutil -lcrypt +BINMODE=500 +INSTALLFLAGS=-fschg +CFLAGS+=-DDEBUGSHELL -DSECURE + +.include diff --git a/sbin/init/NOTES b/sbin/init/NOTES new file mode 100644 index 0000000..bf75101 --- /dev/null +++ b/sbin/init/NOTES @@ -0,0 +1,112 @@ +POSIX and init: +-------------- + +POSIX.1 does not define 'init' but it mentions it in a few places. + +B.2.2.2, p205 line 873: + + This is part of the extensive 'job control' glossary entry. + This specific reference says that 'init' must by default provide + protection from job control signals to jobs it starts -- + it sets SIGTSTP, SIGTTIN and SIGTTOU to SIG_IGN. + +B.2.2.2, p206 line 889: + + Here is a reference to 'vhangup'. It says, 'POSIX.1 does + not specify how controlling terminal access is affected by + a user logging out (that is, by a controlling process + terminating).' vhangup() is recognized as one way to handle + the problem. I'm not clear what happens in Reno; I have + the impression that when the controlling process terminates, + references to the controlling terminal are converted to + references to a 'dead' vnode. I don't know whether vhangup() + is required. + +B.2.2.2, p206 line 921: + + Orphaned process groups bear indirectly on this issue. A + session leader's process group is considered to be orphaned; + that is, it's immune to job control signals from the terminal. + +B.2.2.2, p233 line 2055: + + 'Historically, the implementation-dependent process that + inherits children whose parents have terminated without + waiting on them is called "init" and has a process ID of 1.' + + It goes on to note that it used to be the case that 'init' + was responsible for sending SIGHUP to the foreground process + group of a tty whose controlling process has exited, using + vhangup(). It is now the responsibility of the kernel to + do this when the controlling process calls _exit(). The + kernel is also responsible for sending SIGCONT to stopped + process groups that become orphaned. This is like old BSD + but entire process groups are signaled instead of individual + processes. + + In general it appears that the kernel now automatically + takes care of orphans, relieving 'init' of any responsibility. + Specifics are listed on the _exit() page (p50). + +On setsid(): +----------- + +It appears that neither getty nor login call setsid(), so init must +do this -- seems reasonable. B.4.3.2 p 248 implies that this is the +way that 'init' should work; it says that setsid() should be called +after forking. + +Process group leaders cannot call setsid() -- another reason to +fork! Of course setsid() causes the current process to become a +process group leader, so we can only call setsid() once. Note that +the controlling terminal acquires the session leader's process +group when opened. + +Controlling terminals: +--------------------- + +B.7.1.1.3 p276: 'POSIX.1 does not specify a mechanism by which to +allocate a controlling terminal. This is normally done by a system +utility (such as 'getty') and is considered ... outside the scope +of POSIX.1.' It goes on to say that historically the first open() +of a tty in a session sets the controlling terminal. P130 has the +full details; nothing particularly surprising. + +The glossary p12 describes a 'controlling process' as the first +process in a session that acquires a controlling terminal. Access +to the terminal from the session is revoked if the controlling +process exits (see p50, in the discussion of process termination). + +Design notes: +------------ + +your generic finite state machine +we are fascist about which signals we elect to receive, + even signals purportedly generated by hardware +handle fatal errors gracefully if possible (we reboot if we goof!!) + if we get a segmentation fault etc., print a message on the console + and spin for a while before rebooting + (this at least decreases the amount of paper consumed :-) +apply hysteresis to rapidly exiting gettys +check wait status of children we reap + don't wait for stopped children +don't use SIGCHILD, it's too expensive + but it may close windows and avoid races, sigh +look for EINTR in case we need to change state +init is responsible for utmp and wtmp maintenance (ick) + maybe now we can consider replacements? maintain them in parallel + init only removes utmp and closes out wtmp entries... + +necessary states and state transitions (gleaned from the man page): + 1: single user shell (with password checking?); on exit, go to 2 + 2: rc script: on exit 0, go to 3; on exit N (error), go to 1 + 3: read ttys file: on completion, go to 4 + 4: multi-user operation: on SIGTERM, go to 7; on SIGHUP, go to 5; + on SIGTSTP, go to 6 + 5: clean up mode (re-read ttys file, killing off controlling processes + on lines that are now 'off', starting them on lines newly 'on') + on completion, go to 4 + 6: boring mode (no new sessions); signals as in 4 + 7: death: send SIGHUP to all controlling processes, reap for 30 seconds, + then go to 1 (warn if not all processes died, i.e. wait blocks) +Given the -s flag, we start at state 1; otherwise state 2 diff --git a/sbin/init/init.8 b/sbin/init/init.8 new file mode 100644 index 0000000..017f2ac --- /dev/null +++ b/sbin/init/init.8 @@ -0,0 +1,296 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Donn Seeley at Berkeley Software Design, Inc. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)init.8 8.3 (Berkeley) 4/18/94 +.\" +.\" $Id: init.8,v 1.1 1997/10/04 05:23:51 gdr Exp $ +.\" +.TH INIT 8 "4 October 1997" GNO "System Administration" +.SH NAME +.BR init +\- process control initialization +.SH SYNOPSIS +.BR init +.SH DESCRIPTION +The +.BR init +program +is the last stage of the boot process. +It normally runs the automatic reboot sequence as described in +.BR reboot (8), +and if this succeeds, begins multi-user operation. +If the reboot scripts fail, +.BR init +commences single user operation by giving +the super-user a shell on the console. +The +.BR init +program may be passed parameters +from the boot program to +prevent the system from going multi-user and to instead execute +a single user shell without starting the normal daemons. +The system is then quiescent for maintenance work and may +later be made to go to multi-user by exiting the +single-user shell (with ^D). +This +causes +.BR init +to run the +.BR /etc/rc +start up command file in fastboot mode (skipping disk checks). +.LP +If the +.BR console +entry in the +.BR ttys (5) +file is marked ``insecure'', +then +.BR init +will require that the superuser password be +entered before the system will start a single-user shell. +The password check is skipped if the +.BR console +is marked as ``secure''. +.LP +The kernel runs with four different levels of security. +Any superuser process can raise the security level, but only +.BR init +can lower it. +Security levels are defined as follows: +.RS +.IP \fB-1\fR +Permanently insecure mode \- always run system in level 0 mode. +.IP \fB0\fR +Insecure mode \- immutable and append-only flags may be turned off. +All devices may be read or written subject to their permissions. +.IP \fB1\fR +Secure mode \- immutable and append-only flags may not be changed; +disks for mounted filesystems, +.BR /dev/mem , +and +.BR /dev/kmem +are read-only. +.IP \fB2\fR +Highly secure mode \- same as secure mode, plus disks are always +read-only whether mounted or not. +This level precludes tampering with filesystems by unmounting them, +but also inhibits running +.BR newfs (8) +while the system is multi-user. +.RE +.LP +Normally, the system runs in level 0 mode while single user +and in level 1 mode while multiuser. +If the level 2 mode is desired while running multiuser, +it can be set in the startup script +.BR /etc/rc +using +.BR sysctl (8). +If it is desired to run the system in level 0 mode while multiuser, +the administrator must build a kernel with the variable +.BR securelevel +defined in the file +.BR /sys/compile/MACHINE/param.c +and initialize it to -1. +.LP +In multi-user operation, +.BR init +maintains +processes for the terminal ports found in the file +.BR ttys (5). +.BR Init +reads this file, and executes the command found in the second field. +This command is usually +.BR getty (8); +.BR getty +opens and initializes the tty line +and +executes the +.BR login +program. +The +.BR login +program, when a valid user logs in, +executes a shell for that user. When this shell +dies, either because the user logged out +or an abnormal termination occurred (a signal), +the +.BR init +program wakes up, deletes the user +from the +.BR utmp (5) +file of current users and records the logout in the +.BR wtmp +file. +The cycle is +then restarted by +.BR init +executing a new +.BR getty +for the line. +.LP +Line status (on, off, secure, getty, or window information) +may be changed in the +.BR ttys +file without a reboot by sending the signal +.BR SIGHUP +to +.BR init +with the command +.B "kill -HUP 1" . +On receipt of this signal, +.BR init +re-reads the +.BR ttys +file. +When a line is turned off in +.BR ttys , +.BR init +will send a SIGHUP signal to the controlling process +for the session associated with the line. +For any lines that were previously turned off in the +.BR ttys +file and are now on, +.BR init +executes a new +.BR getty +to enable a new login. +If the getty or window field for a line is changed, +the change takes effect at the end of the current +login session (e.g., the next time +.BR init +starts a process on the line). +If a line is commented out or deleted from +.BR ttys , +.BR init +will not do anything at all to that line. +However, it will complain that the relationship between lines +in the +.BR ttys +file and records in the +.BR utmp +file is out of sync, +so this practice is not recommended. +.LP +.BR Init +will terminate multi-user operations and resume single-user mode +if sent a terminate TERM +signal, for example, +.BR "kill \-TERM 1" . +If there are processes outstanding that are deadlocked (because of +hardware or software failure), +.BR init +will not wait for them all to die (which might take forever), but +will time out after 30 seconds and print a warning message. +.LP +.BR Init +will cease creating new +.BR getty Ns's +and allow the system to slowly die away, if it is sent a terminal stop +TSTP signal, i.e. +.BR "kill \-TSTP 1" . +A later hangup will resume full +multi-user operations, or a terminate will start a single user shell. +This hook is used by +.BR reboot (8) +and +.BR halt (8). +.LP +.BR Init +will terminate all possible processes (again, it will not wait +for deadlocked processes) and reboot the machine if sent the interrupt +INT signal, i.e. +.BR "kill \-INT 1" . +This is useful for shutting the machine down cleanly from inside the kernel +or from X when the machines appears to be hung. +.LP +The role of +.BR init +is so critical that if it dies, the system will reboot itself +automatically. +If, at bootstrap time, the +.BR init +process cannot be located, the system will panic with the message +``panic: "init died (signal %d, exit %d)''. +.SH DIAGNOSTICS +.RS +.IP "\fIgetty repeating too quickly on port %s, sleeping\fR" +A process being started to service a line is exiting quickly +each time it is started. +This is often caused by a ringing or noisy terminal line. +.IR "Init will sleep for 10 seconds" , +.IR "then continue trying to start the process" . +.LP +.IP "\fIsome processes would not die; ps axl advised.\fR" +A process +is hung and could not be killed when the system was shutting down. +This condition is usually caused by a process +that is stuck in a device driver because of +a persistent device error condition. +.RE +.SH FILES +.RS +.IP \fB/dev/console\fR +System console device. +.IP \fB/dev/tty*\fR +Terminal ports found in +.BR ttys . +.IP \fB/var/run/utmp\fR +Record of Current users on the system. +.IP \fB/var/log/wtmp\fR +Record of all logins and logouts. +.IP \fB/etc/ttys\fR +The terminal initialization information file. +.IP \fB/etc/rc\fR +System startup commands. +.RE +.SH SEE ALSO +.BR login (1), +.BR kill (1), +.BR sh (1), +.BR ttys (5), +.BR crash (8), +.BR getty (8), +.BR rc (8), +.BR reboot (8), +.BR halt (8), +.BR shutdown (8) +.SH HISTORY +A +.BR init +command appeared in Version 6 AT&T UNIX. +.SH BUGS +Systems without +.BR sysctl +behave as though they have security level \-1. diff --git a/sbin/init/init.c b/sbin/init/init.c new file mode 100644 index 0000000..f06b2dc --- /dev/null +++ b/sbin/init/init.c @@ -0,0 +1,1422 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Donn Seeley at Berkeley Software Design, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1991, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 7/15/93"; +#endif /* not lint */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __STDC__ +#include +#else +#include +#endif + +#ifdef SECURE +#include +#endif + +#include "pathnames.h" + +/* + * Until the mythical util.h arrives... + */ +extern int login_tty __P((int)); +extern int logout __P((const char *)); +extern void logwtmp __P((const char *, const char *, const char *)); + +/* + * Sleep times; used to prevent thrashing. + */ +#define GETTY_SPACING 5 /* N secs minimum getty spacing */ +#define GETTY_SLEEP 30 /* sleep N secs after spacing problem */ +#define GETTY_NSPACE 3 /* max. spacing count to bring reaction */ +#define WINDOW_WAIT 3 /* wait N secs after starting window */ +#define STALL_TIMEOUT 30 /* wait N secs after warning */ +#define DEATH_WATCH 10 /* wait N secs for procs to die */ + +void handle __P((sig_t, ...)); +void delset __P((sigset_t *, ...)); + +void stall __P((char *, ...)); +void warning __P((char *, ...)); +void emergency __P((char *, ...)); +void disaster __P((int)); +void badsys __P((int)); + +/* + * We really need a recursive typedef... + * The following at least guarantees that the return type of (*state_t)() + * is sufficiently wide to hold a function pointer. + */ +typedef long (*state_func_t) __P((void)); +typedef state_func_t (*state_t) __P((void)); + +state_func_t single_user __P((void)); +state_func_t runcom __P((void)); +state_func_t read_ttys __P((void)); +state_func_t multi_user __P((void)); +state_func_t clean_ttys __P((void)); +state_func_t catatonia __P((void)); +state_func_t death __P((void)); + +enum { AUTOBOOT, FASTBOOT } runcom_mode = AUTOBOOT; +int Reboot = FALSE; + +void transition __P((state_t)); +state_t requested_transition = runcom; + +void setctty __P((char *)); + +typedef struct init_session { + int se_index; /* index of entry in ttys file */ + pid_t se_process; /* controlling process */ + time_t se_started; /* used to avoid thrashing */ + int se_flags; /* status of session */ +#define SE_SHUTDOWN 0x1 /* session won't be restarted */ + int se_nspace; /* spacing count */ + char *se_device; /* filename of port */ + char *se_getty; /* what to run on that port */ + char *se_getty_argv_space; /* pre-parsed argument array space */ + char **se_getty_argv; /* pre-parsed argument array */ + char *se_window; /* window system (started only once) */ + char *se_window_argv_space; /* pre-parsed argument array space */ + char **se_window_argv; /* pre-parsed argument array */ + char *se_type; /* default terminal type */ + struct init_session *se_prev; + struct init_session *se_next; +} session_t; + +void free_session __P((session_t *)); +session_t *new_session __P((session_t *, int, struct ttyent *)); +session_t *sessions; + +char **construct_argv __P((char *)); +void start_window_system __P((session_t *)); +void collect_child __P((pid_t)); +pid_t start_getty __P((session_t *)); +void transition_handler __P((int)); +void alrm_handler __P((int)); +void setsecuritylevel __P((int)); +int getsecuritylevel __P((void)); +int setupargv __P((session_t *, struct ttyent *)); +int clang; + +void clear_session_logs __P((session_t *)); + +int start_session_db __P((void)); +void add_session __P((session_t *)); +void del_session __P((session_t *)); +session_t *find_session __P((pid_t)); +DB *session_db; + +/* + * The mother of all processes. + */ +int +main(argc, argv) + int argc; + char **argv; +{ + int c; + struct sigaction sa; + sigset_t mask; + + + /* Dispose of random users. */ + if (getuid() != 0) { + (void)fprintf(stderr, "init: %s\n", strerror(EPERM)); + exit (1); + } + + /* System V users like to reexec init. */ + if (getpid() != 1) { + (void)fprintf(stderr, "init: already running\n"); + exit (1); + } + + /* + * Note that this does NOT open a file... + * Does 'init' deserve its own facility number? + */ + openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH); + + /* + * Create an initial session. + */ + if (setsid() < 0) + warning("initial setsid() failed: %m"); + + /* + * Establish an initial user so that programs running + * single user do not freak out and die (like passwd). + */ + if (setlogin("root") < 0) + warning("setlogin() failed: %m"); + + /* + * This code assumes that we always get arguments through flags, + * never through bits set in some random machine register. + */ + while ((c = getopt(argc, argv, "sf")) != -1) + switch (c) { + case 's': + requested_transition = single_user; + break; + case 'f': + runcom_mode = FASTBOOT; + break; + default: + warning("unrecognized flag '-%c'", c); + break; + } + + if (optind != argc) + warning("ignoring excess arguments"); + + /* + * We catch or block signals rather than ignore them, + * so that they get reset on exec. + */ + handle(badsys, SIGSYS, 0); + handle(disaster, SIGABRT, SIGFPE, SIGILL, SIGSEGV, + SIGBUS, SIGXCPU, SIGXFSZ, 0); + handle(transition_handler, SIGHUP, SIGINT, SIGTERM, SIGTSTP, 0); + handle(alrm_handler, SIGALRM, 0); + sigfillset(&mask); + delset(&mask, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGSYS, + SIGXCPU, SIGXFSZ, SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGALRM, 0); + sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_IGN; + (void) sigaction(SIGTTIN, &sa, (struct sigaction *)0); + (void) sigaction(SIGTTOU, &sa, (struct sigaction *)0); + + /* + * Paranoia. + */ + close(0); + close(1); + close(2); + + /* + * Start the state machine. + */ + transition(requested_transition); + + /* + * Should never reach here. + */ + return 1; +} + +/* + * Associate a function with a signal handler. + */ +void +#ifdef __STDC__ +handle(sig_t handler, ...) +#else +handle(va_alist) + va_dcl +#endif +{ + int sig; + struct sigaction sa; + int mask_everything; + va_list ap; +#ifndef __STDC__ + sig_t handler; + + va_start(ap); + handler = va_arg(ap, sig_t); +#else + va_start(ap, handler); +#endif + + sa.sa_handler = handler; + sigfillset(&mask_everything); + + while (sig = va_arg(ap, int)) { + sa.sa_mask = mask_everything; + /* XXX SA_RESTART? */ + sa.sa_flags = sig == SIGCHLD ? SA_NOCLDSTOP : 0; + sigaction(sig, &sa, (struct sigaction *) 0); + } + va_end(ap); +} + +/* + * Delete a set of signals from a mask. + */ +void +#ifdef __STDC__ +delset(sigset_t *maskp, ...) +#else +delset(va_alist) + va_dcl +#endif +{ + int sig; + va_list ap; +#ifndef __STDC__ + sigset_t *maskp; + + va_start(ap); + maskp = va_arg(ap, sigset_t *); +#else + va_start(ap, maskp); +#endif + + while (sig = va_arg(ap, int)) + sigdelset(maskp, sig); + va_end(ap); +} + +/* + * Log a message and sleep for a while (to give someone an opportunity + * to read it and to save log or hardcopy output if the problem is chronic). + * NB: should send a message to the session logger to avoid blocking. + */ +void +#ifdef __STDC__ +stall(char *message, ...) +#else +stall(va_alist) + va_dcl +#endif +{ + va_list ap; +#ifndef __STDC__ + char *message; + + va_start(ap); + message = va_arg(ap, char *); +#else + va_start(ap, message); +#endif + + vsyslog(LOG_ALERT, message, ap); + va_end(ap); + sleep(STALL_TIMEOUT); +} + +/* + * Like stall(), but doesn't sleep. + * If cpp had variadic macros, the two functions could be #defines for another. + * NB: should send a message to the session logger to avoid blocking. + */ +void +#ifdef __STDC__ +warning(char *message, ...) +#else +warning(va_alist) + va_dcl +#endif +{ + va_list ap; +#ifndef __STDC__ + char *message; + + va_start(ap); + message = va_arg(ap, char *); +#else + va_start(ap, message); +#endif + + vsyslog(LOG_ALERT, message, ap); + va_end(ap); +} + +/* + * Log an emergency message. + * NB: should send a message to the session logger to avoid blocking. + */ +void +#ifdef __STDC__ +emergency(char *message, ...) +#else +emergency(va_alist) + va_dcl +#endif +{ + va_list ap; +#ifndef __STDC__ + char *message; + + va_start(ap); + message = va_arg(ap, char *); +#else + va_start(ap, message); +#endif + + vsyslog(LOG_EMERG, message, ap); + va_end(ap); +} + +/* + * Catch a SIGSYS signal. + * + * These may arise if a system does not support sysctl. + * We tolerate up to 25 of these, then throw in the towel. + */ +void +badsys(sig) + int sig; +{ + static int badcount = 0; + + if (badcount++ < 25) + return; + disaster(sig); +} + +/* + * Catch an unexpected signal. + */ +void +disaster(sig) + int sig; +{ + emergency("fatal signal: %s", + sig < (unsigned) NSIG ? sys_siglist[sig] : "unknown signal"); + + sleep(STALL_TIMEOUT); + _exit(sig); /* reboot */ +} + +/* + * Get the security level of the kernel. + */ +int +getsecuritylevel() +{ +#ifdef KERN_SECURELVL + int name[2], curlevel; + size_t len; + extern int errno; + + name[0] = CTL_KERN; + name[1] = KERN_SECURELVL; + len = sizeof curlevel; + if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) { + emergency("cannot get kernel security level: %s", + strerror(errno)); + return (-1); + } + return (curlevel); +#else + return (-1); +#endif +} + +/* + * Set the security level of the kernel. + */ +void +setsecuritylevel(newlevel) + int newlevel; +{ +#ifdef KERN_SECURELVL + int name[2], curlevel; + extern int errno; + + curlevel = getsecuritylevel(); + if (newlevel == curlevel) + return; + name[0] = CTL_KERN; + name[1] = KERN_SECURELVL; + if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) { + emergency( + "cannot change kernel security level from %d to %d: %s", + curlevel, newlevel, strerror(errno)); + return; + } +#ifdef SECURE + warning("kernel security level changed from %d to %d", + curlevel, newlevel); +#endif +#endif +} + +/* + * Change states in the finite state machine. + * The initial state is passed as an argument. + */ +void +transition(s) + state_t s; +{ + for (;;) + s = (state_t) (*s)(); +} + +/* + * Close out the accounting files for a login session. + * NB: should send a message to the session logger to avoid blocking. + */ +void +clear_session_logs(sp) + session_t *sp; +{ + char *line = sp->se_device + sizeof(_PATH_DEV) - 1; + + if (logout(line)) + logwtmp(line, "", ""); +} + +/* + * Start a session and allocate a controlling terminal. + * Only called by children of init after forking. + */ +void +setctty(name) + char *name; +{ + int fd; + + (void) revoke(name); + if ((fd = open(name, O_RDWR)) == -1) { + stall("can't open %s: %m", name); + _exit(1); + } + if (login_tty(fd) == -1) { + stall("can't get %s for controlling terminal: %m", name); + _exit(1); + } +} + +/* + * Bring the system up single user. + */ +state_func_t +single_user() +{ + pid_t pid, wpid; + int status; + sigset_t mask; + char *shell = _PATH_BSHELL; + char *argv[2]; +#ifdef SECURE + struct ttyent *typ; + struct passwd *pp; + static const char banner[] = + "Enter root password, or ^D to go multi-user\n"; + char *clear, *password; +#endif + + /* + * If the kernel is in secure mode, downgrade it to insecure mode. + */ + if (getsecuritylevel() > 0) + setsecuritylevel(0); + + if (Reboot) { + /* Instead of going single user, let's halt the machine */ + sync(); + alarm(2); + pause(); + reboot(RB_AUTOBOOT); + _exit(0); + } + + if ((pid = fork()) == 0) { + /* + * Start the single user session. + */ + setctty(_PATH_CONSOLE); + +#ifdef SECURE + /* + * Check the root password. + * We don't care if the console is 'on' by default; + * it's the only tty that can be 'off' and 'secure'. + */ + typ = getttynam("console"); + pp = getpwnam("root"); + if (typ && (typ->ty_status & TTY_SECURE) == 0 && pp) { + write(2, banner, sizeof banner - 1); + for (;;) { + clear = getpass("Password:"); + if (clear == 0 || *clear == '\0') + _exit(0); + password = crypt(clear, pp->pw_passwd); + bzero(clear, _PASSWORD_LEN); + if (strcmp(password, pp->pw_passwd) == 0) + break; + warning("single-user login failed\n"); + } + } + endttyent(); + endpwent(); +#endif /* SECURE */ + +#ifdef DEBUGSHELL + { + char altshell[128], *cp = altshell; + int num; + +#define SHREQUEST \ + "Enter pathname of shell or RETURN for sh: " + (void)write(STDERR_FILENO, + SHREQUEST, sizeof(SHREQUEST) - 1); + while ((num = read(STDIN_FILENO, cp, 1)) != -1 && + num != 0 && *cp != '\n' && cp < &altshell[127]) + cp++; + *cp = '\0'; + if (altshell[0] != '\0') + shell = altshell; + } +#endif /* DEBUGSHELL */ + + /* + * Unblock signals. + * We catch all the interesting ones, + * and those are reset to SIG_DFL on exec. + */ + sigemptyset(&mask); + sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); + + /* + * Fire off a shell. + * If the default one doesn't work, try the Bourne shell. + */ + argv[0] = "-sh"; + argv[1] = 0; + execv(shell, argv); + emergency("can't exec %s for single user: %m", shell); + execv(_PATH_BSHELL, argv); + emergency("can't exec %s for single user: %m", _PATH_BSHELL); + sleep(STALL_TIMEOUT); + _exit(1); + } + + if (pid == -1) { + /* + * We are seriously hosed. Do our best. + */ + emergency("can't fork single-user shell, trying again"); + while (waitpid(-1, (int *) 0, WNOHANG) > 0) + continue; + return (state_func_t) single_user; + } + + requested_transition = 0; + do { + if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) + collect_child(wpid); + if (wpid == -1) { + if (errno == EINTR) + continue; + warning("wait for single-user shell failed: %m; restarting"); + return (state_func_t) single_user; + } + if (wpid == pid && WIFSTOPPED(status)) { + warning("init: shell stopped, restarting\n"); + kill(pid, SIGCONT); + wpid = -1; + } + } while (wpid != pid && !requested_transition); + + if (requested_transition) + return (state_func_t) requested_transition; + + if (!WIFEXITED(status)) { + if (WTERMSIG(status) == SIGKILL) { + /* + * reboot(8) killed shell? + */ + warning("single user shell terminated."); + sleep(STALL_TIMEOUT); + _exit(0); + } else { + warning("single user shell terminated, restarting"); + return (state_func_t) single_user; + } + } + + runcom_mode = FASTBOOT; + return (state_func_t) runcom; +} + +/* + * Run the system startup script. + */ +state_func_t +runcom() +{ + pid_t pid, wpid; + int status; + char *argv[4]; + struct sigaction sa; + + if ((pid = fork()) == 0) { + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_IGN; + (void) sigaction(SIGTSTP, &sa, (struct sigaction *)0); + (void) sigaction(SIGHUP, &sa, (struct sigaction *)0); + + setctty(_PATH_CONSOLE); + + argv[0] = "sh"; + argv[1] = _PATH_RUNCOM; + argv[2] = runcom_mode == AUTOBOOT ? "autoboot" : 0; + argv[3] = 0; + + sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) 0); + + execv(_PATH_BSHELL, argv); + stall("can't exec %s for %s: %m", _PATH_BSHELL, _PATH_RUNCOM); + _exit(1); /* force single user mode */ + } + + if (pid == -1) { + emergency("can't fork for %s on %s: %m", + _PATH_BSHELL, _PATH_RUNCOM); + while (waitpid(-1, (int *) 0, WNOHANG) > 0) + continue; + sleep(STALL_TIMEOUT); + return (state_func_t) single_user; + } + + /* + * Copied from single_user(). This is a bit paranoid. + */ + do { + if ((wpid = waitpid(-1, &status, WUNTRACED)) != -1) + collect_child(wpid); + if (wpid == -1) { + if (errno == EINTR) + continue; + warning("wait for %s on %s failed: %m; going to single user mode", + _PATH_BSHELL, _PATH_RUNCOM); + return (state_func_t) single_user; + } + if (wpid == pid && WIFSTOPPED(status)) { + warning("init: %s on %s stopped, restarting\n", + _PATH_BSHELL, _PATH_RUNCOM); + kill(pid, SIGCONT); + wpid = -1; + } + } while (wpid != pid); + + if (WIFSIGNALED(status) && WTERMSIG(status) == SIGTERM && + requested_transition == catatonia) { + /* /etc/rc executed /sbin/reboot; wait for the end quietly */ + sigset_t s; + + sigfillset(&s); + for (;;) + sigsuspend(&s); + } + + if (!WIFEXITED(status)) { + warning("%s on %s terminated abnormally, going to single user mode", + _PATH_BSHELL, _PATH_RUNCOM); + return (state_func_t) single_user; + } + + if (WEXITSTATUS(status)) + return (state_func_t) single_user; + + runcom_mode = AUTOBOOT; /* the default */ + /* NB: should send a message to the session logger to avoid blocking. */ + logwtmp("~", "reboot", ""); + return (state_func_t) read_ttys; +} + +/* + * Open the session database. + * + * NB: We could pass in the size here; is it necessary? + */ +int +start_session_db() +{ + if (session_db && (*session_db->close)(session_db)) + emergency("session database close: %s", strerror(errno)); + if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == 0) { + emergency("session database open: %s", strerror(errno)); + return (1); + } + return (0); + +} + +/* + * Add a new login session. + */ +void +add_session(sp) + session_t *sp; +{ + DBT key; + DBT data; + + key.data = &sp->se_process; + key.size = sizeof sp->se_process; + data.data = &sp; + data.size = sizeof sp; + + if ((*session_db->put)(session_db, &key, &data, 0)) + emergency("insert %d: %s", sp->se_process, strerror(errno)); +} + +/* + * Delete an old login session. + */ +void +del_session(sp) + session_t *sp; +{ + DBT key; + + key.data = &sp->se_process; + key.size = sizeof sp->se_process; + + if ((*session_db->del)(session_db, &key, 0)) + emergency("delete %d: %s", sp->se_process, strerror(errno)); +} + +/* + * Look up a login session by pid. + */ +session_t * +#ifdef __STDC__ +find_session(pid_t pid) +#else +find_session(pid) + pid_t pid; +#endif +{ + DBT key; + DBT data; + session_t *ret; + + key.data = &pid; + key.size = sizeof pid; + if ((*session_db->get)(session_db, &key, &data, 0) != 0) + return 0; + bcopy(data.data, (char *)&ret, sizeof(ret)); + return ret; +} + +/* + * Construct an argument vector from a command line. + */ +char ** +construct_argv(command) + char *command; +{ + char *strk (char *); + register int argc = 0; + register char **argv = (char **) malloc(((strlen(command) + 1) / 2 + 1) + * sizeof (char *)); + + if ((argv[argc++] = strk(command)) == 0) + return 0; + while (argv[argc++] = strk((char *) 0)) + continue; + return argv; +} + +/* + * Deallocate a session descriptor. + */ +void +free_session(sp) + register session_t *sp; +{ + free(sp->se_device); + if (sp->se_getty) { + free(sp->se_getty); + free(sp->se_getty_argv_space); + free(sp->se_getty_argv); + } + if (sp->se_window) { + free(sp->se_window); + free(sp->se_window_argv_space); + free(sp->se_window_argv); + } + if (sp->se_type) + free(sp->se_type); + free(sp); +} + +/* + * Allocate a new session descriptor. + */ +session_t * +new_session(sprev, session_index, typ) + session_t *sprev; + int session_index; + register struct ttyent *typ; +{ + register session_t *sp; + + if ((typ->ty_status & TTY_ON) == 0 || + typ->ty_name == 0 || + typ->ty_getty == 0) + return 0; + + sp = (session_t *) malloc(sizeof (session_t)); + bzero(sp, sizeof *sp); + + sp->se_index = session_index; + + sp->se_device = malloc(sizeof(_PATH_DEV) + strlen(typ->ty_name)); + (void) sprintf(sp->se_device, "%s%s", _PATH_DEV, typ->ty_name); + + if (setupargv(sp, typ) == 0) { + free_session(sp); + return (0); + } + + sp->se_next = 0; + if (sprev == 0) { + sessions = sp; + sp->se_prev = 0; + } else { + sprev->se_next = sp; + sp->se_prev = sprev; + } + + return sp; +} + +/* + * Calculate getty and if useful window argv vectors. + */ +int +setupargv(sp, typ) + session_t *sp; + struct ttyent *typ; +{ + + if (sp->se_getty) { + free(sp->se_getty); + free(sp->se_getty_argv_space); + free(sp->se_getty_argv); + } + sp->se_getty = malloc(strlen(typ->ty_getty) + strlen(typ->ty_name) + 2); + (void) sprintf(sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name); + sp->se_getty_argv_space = strdup(sp->se_getty); + sp->se_getty_argv = construct_argv(sp->se_getty_argv_space); + if (sp->se_getty_argv == 0) { + warning("can't parse getty for port %s", sp->se_device); + free(sp->se_getty); + free(sp->se_getty_argv_space); + sp->se_getty = sp->se_getty_argv_space = 0; + return (0); + } + if (sp->se_window) { + free(sp->se_window); + free(sp->se_window_argv_space); + free(sp->se_window_argv); + } + sp->se_window = sp->se_window_argv_space = 0; + sp->se_window_argv = 0; + if (typ->ty_window) { + sp->se_window = strdup(typ->ty_window); + sp->se_window_argv_space = strdup(sp->se_window); + sp->se_window_argv = construct_argv(sp->se_window_argv_space); + if (sp->se_window_argv == 0) { + warning("can't parse window for port %s", + sp->se_device); + free(sp->se_window_argv_space); + free(sp->se_window); + sp->se_window = sp->se_window_argv_space = 0; + return (0); + } + } + if (sp->se_type) + free(sp->se_type); + sp->se_type = typ->ty_type ? strdup(typ->ty_type) : 0; + return (1); +} + +/* + * Walk the list of ttys and create sessions for each active line. + */ +state_func_t +read_ttys() +{ + int session_index = 0; + register session_t *sp, *snext; + register struct ttyent *typ; + + /* + * Destroy any previous session state. + * There shouldn't be any, but just in case... + */ + for (sp = sessions; sp; sp = snext) { + if (sp->se_process) + clear_session_logs(sp); + snext = sp->se_next; + free_session(sp); + } + sessions = 0; + if (start_session_db()) + return (state_func_t) single_user; + + /* + * Allocate a session entry for each active port. + * Note that sp starts at 0. + */ + while (typ = getttyent()) + if (snext = new_session(sp, ++session_index, typ)) + sp = snext; + + endttyent(); + + return (state_func_t) multi_user; +} + +/* + * Start a window system running. + */ +void +start_window_system(sp) + session_t *sp; +{ + pid_t pid; + sigset_t mask; + char term[64], *env[2]; + + if ((pid = fork()) == -1) { + emergency("can't fork for window system on port %s: %m", + sp->se_device); + /* hope that getty fails and we can try again */ + return; + } + + if (pid) + return; + + sigemptyset(&mask); + sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); + + if (setsid() < 0) + emergency("setsid failed (window) %m"); + + if (sp->se_type) { + /* Don't use malloc after fork */ + strcpy(term, "TERM="); + strcat(term, sp->se_type); + env[0] = term; + env[1] = 0; + } + else + env[0] = 0; + execve(sp->se_window_argv[0], sp->se_window_argv, env); + stall("can't exec window system '%s' for port %s: %m", + sp->se_window_argv[0], sp->se_device); + _exit(1); +} + +/* + * Start a login session running. + */ +pid_t +start_getty(sp) + session_t *sp; +{ + pid_t pid; + sigset_t mask; + time_t current_time = time((time_t *) 0); + int too_quick = 0; + char term[64], *env[2]; + + if (current_time >= sp->se_started && + current_time - sp->se_started < GETTY_SPACING) { + if (++sp->se_nspace > GETTY_NSPACE) { + sp->se_nspace = 0; + too_quick = 1; + } + } else + sp->se_nspace = 0; + + /* + * fork(), not vfork() -- we can't afford to block. + */ + if ((pid = fork()) == -1) { + emergency("can't fork for getty on port %s: %m", sp->se_device); + return -1; + } + + if (pid) + return pid; + + if (too_quick) { + warning("getty repeating too quickly on port %s, sleeping %d secs", + sp->se_device, GETTY_SLEEP); + sleep((unsigned) GETTY_SLEEP); + } + + if (sp->se_window) { + start_window_system(sp); + sleep(WINDOW_WAIT); + } + + sigemptyset(&mask); + sigprocmask(SIG_SETMASK, &mask, (sigset_t *) 0); + + if (sp->se_type) { + /* Don't use malloc after fork */ + strcpy(term, "TERM="); + strcat(term, sp->se_type); + env[0] = term; + env[1] = 0; + } + else + env[0] = 0; + execve(sp->se_getty_argv[0], sp->se_getty_argv, env); + stall("can't exec getty '%s' for port %s: %m", + sp->se_getty_argv[0], sp->se_device); + _exit(1); +} + +/* + * Collect exit status for a child. + * If an exiting login, start a new login running. + */ +void +#ifdef __STDC__ +collect_child(pid_t pid) +#else +collect_child(pid) + pid_t pid; +#endif +{ + register session_t *sp, *sprev, *snext; + + if (! sessions) + return; + + if (! (sp = find_session(pid))) + return; + + clear_session_logs(sp); + del_session(sp); + sp->se_process = 0; + + if (sp->se_flags & SE_SHUTDOWN) { + if (sprev = sp->se_prev) + sprev->se_next = sp->se_next; + else + sessions = sp->se_next; + if (snext = sp->se_next) + snext->se_prev = sp->se_prev; + free_session(sp); + return; + } + + if ((pid = start_getty(sp)) == -1) { + /* serious trouble */ + requested_transition = clean_ttys; + return; + } + + sp->se_process = pid; + sp->se_started = time((time_t *) 0); + add_session(sp); +} + +/* + * Catch a signal and request a state transition. + */ +void +transition_handler(sig) + int sig; +{ + + switch (sig) { + case SIGHUP: + requested_transition = clean_ttys; + break; + case SIGINT: + Reboot = TRUE; + case SIGTERM: + requested_transition = death; + break; + case SIGTSTP: + requested_transition = catatonia; + break; + default: + requested_transition = 0; + break; + } +} + +/* + * Take the system multiuser. + */ +state_func_t +multi_user() +{ + pid_t pid; + register session_t *sp; + + requested_transition = 0; + + /* + * If the administrator has not set the security level to -1 + * to indicate that the kernel should not run multiuser in secure + * mode, and the run script has not set a higher level of security + * than level 1, then put the kernel into secure mode. + */ + if (getsecuritylevel() == 0) + setsecuritylevel(1); + + for (sp = sessions; sp; sp = sp->se_next) { + if (sp->se_process) + continue; + if ((pid = start_getty(sp)) == -1) { + /* serious trouble */ + requested_transition = clean_ttys; + break; + } + sp->se_process = pid; + sp->se_started = time((time_t *) 0); + add_session(sp); + } + + while (!requested_transition) + if ((pid = waitpid(-1, (int *) 0, 0)) != -1) + collect_child(pid); + + return (state_func_t) requested_transition; +} + +/* + * This is an n-squared algorithm. We hope it isn't run often... + */ +state_func_t +clean_ttys() +{ + register session_t *sp, *sprev; + register struct ttyent *typ; + register int session_index = 0; + register int devlen; + char *old_getty, *old_window, *old_type; + + if (! sessions) + return (state_func_t) multi_user; + + devlen = sizeof(_PATH_DEV) - 1; + while (typ = getttyent()) { + ++session_index; + + for (sprev = 0, sp = sessions; sp; sprev = sp, sp = sp->se_next) + if (strcmp(typ->ty_name, sp->se_device + devlen) == 0) + break; + + if (sp) { + if (sp->se_index != session_index) { + warning("port %s changed utmp index from %d to %d", + sp->se_device, sp->se_index, + session_index); + sp->se_index = session_index; + } + if ((typ->ty_status & TTY_ON) == 0 || + typ->ty_getty == 0) { + sp->se_flags |= SE_SHUTDOWN; + kill(sp->se_process, SIGHUP); + continue; + } + sp->se_flags &= ~SE_SHUTDOWN; + old_getty = sp->se_getty ? strdup(sp->se_getty) : 0; + old_window = sp->se_window ? strdup(sp->se_window) : 0; + old_type = sp->se_type ? strdup(sp->se_type) : 0; + if (setupargv(sp, typ) == 0) { + warning("can't parse getty for port %s", + sp->se_device); + sp->se_flags |= SE_SHUTDOWN; + kill(sp->se_process, SIGHUP); + } + else if ( !old_getty + || !old_type && sp->se_type + || old_type && !sp->se_type + || !old_window && sp->se_window + || old_window && !sp->se_window + || strcmp(old_getty, sp->se_getty) != 0 + || old_window && strcmp(old_window, sp->se_window) != 0 + || old_type && strcmp(old_type, sp->se_type) != 0 + ) { + /* Don't set SE_SHUTDOWN here */ + sp->se_nspace = 0; + sp->se_started = 0; + kill(sp->se_process, SIGHUP); + } + if (old_getty) + free(old_getty); + if (old_getty) + free(old_window); + if (old_type) + free(old_type); + continue; + } + + new_session(sprev, session_index, typ); + } + + endttyent(); + + return (state_func_t) multi_user; +} + +/* + * Block further logins. + */ +state_func_t +catatonia() +{ + register session_t *sp; + + for (sp = sessions; sp; sp = sp->se_next) + sp->se_flags |= SE_SHUTDOWN; + + return (state_func_t) multi_user; +} + +/* + * Note SIGALRM. + */ +void +alrm_handler(sig) + int sig; +{ + clang = 1; +} + +/* + * Bring the system down to single user. + */ +state_func_t +death() +{ + register session_t *sp; + register int i; + pid_t pid; + static const int death_sigs[3] = { SIGHUP, SIGTERM, SIGKILL }; + + for (sp = sessions; sp; sp = sp->se_next) + sp->se_flags |= SE_SHUTDOWN; + + /* NB: should send a message to the session logger to avoid blocking. */ + logwtmp("~", "shutdown", ""); + + for (i = 0; i < 3; ++i) { + if (kill(-1, death_sigs[i]) == -1 && errno == ESRCH) + return (state_func_t) single_user; + + clang = 0; + alarm(DEATH_WATCH); + do + if ((pid = waitpid(-1, (int *)0, 0)) != -1) + collect_child(pid); + while (clang == 0 && errno != ECHILD); + + if (errno == ECHILD) + return (state_func_t) single_user; + } + + warning("some processes would not die; ps axl advised"); + + return (state_func_t) single_user; +} +char * +strk (char *p) +{ + static char *t; + char *q; + int c; + + if (p) + t = p; + if (!t) + return 0; + + c = *t; + while (c == ' ' || c == '\t' ) + c = *++t; + if (!c) { + t = 0; + return 0; + } + q = t; + if (c == '\'') { + c = *++t; + q = t; + while (c && c != '\'') + c = *++t; + if (!c) /* unterminated string */ + q = t = 0; + else + *t++ = 0; + } else { + while (c && c != ' ' && c != '\t' ) + c = *++t; + *t++ = 0; + if (!c) + t = 0; + } + return q; +} diff --git a/sbin/init/pathnames.h b/sbin/init/pathnames.h new file mode 100644 index 0000000..abb874a --- /dev/null +++ b/sbin/init/pathnames.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Donn Seeley at Berkeley Software Design, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)pathnames.h 8.1 (Berkeley) 6/5/93 + */ + +#include + +#define _PATH_SLOGGER "/sbin/session_logger" +#define _PATH_RUNCOM "/etc/rc" diff --git a/sbin/reboot/Makefile b/sbin/reboot/Makefile new file mode 100644 index 0000000..ab92afc --- /dev/null +++ b/sbin/reboot/Makefile @@ -0,0 +1,26 @@ +# @(#)Makefile 8.1 (Berkeley) 6/5/93 + +PROG= reboot +DPADD= ${LIBUTIL} +LDADD= -lutil +MAN8= reboot.8 boot_hp300.8 boot_i386.8 boot_sparc.8 boot_tahoe.8 boot_vax.8 +MLINKS= reboot.8 halt.8 reboot.8 fastboot.8 reboot.8 fasthalt.8 + +ARCH!= uname -m + +.if exists (${.CURDIR}/boot_${ARCH}.8) +MLINKS+= boot_${ARCH}.8 boot.8 +.endif + +LINKS= ${BINDIR}/reboot ${BINDIR}/halt ${BINDIR}/reboot ${BINDIR}/fastboot \ + ${BINDIR}/reboot ${BINDIR}/fasthalt + +XXXBROKENafterinstall: + ${MINSTALL} boot_hp300.8 ${DESTDIR}${MANDIR}8/hp300/boot.8 + ${MINSTALL} boot_i386.8 ${DESTDIR}${MANDIR}8/i386/boot.8 + ${MINSTALL} boot_sparc.8 ${DESTDIR}${MANDIR}8/sparc/boot.8 + ${MINSTALL} boot_tahoe.8 ${DESTDIR}${MANDIR}8/tahoe/boot.8 + ${MINSTALL} boot_vax.8 ${DESTDIR}${MANDIR}8/vax/boot.8 + +.include +.include diff --git a/sbin/reboot/boot.hp300.8 b/sbin/reboot/boot.hp300.8 new file mode 100644 index 0000000..1d3a4f6 --- /dev/null +++ b/sbin/reboot/boot.hp300.8 @@ -0,0 +1,117 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Systems Programming Group of the University of Utah Computer +.\" Science Department. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)boot_hp300.8 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt BOOT_HP300 8 hp300 +.Os +.Sh NAME +.Nm boot +.Nd +system bootstrapping procedures +.Sh DESCRIPTION +.Sy Power fail and crash recovery. +Normally, the system will reboot itself at power-up or after crashes. +An automatic consistency check of the file systems will be performed, +and unless this fails, the system will resume multi-user operations. +.Pp +.Sy Cold starts. +On an HP300, the boot procedure uses the boot ROM to load a boot program +from an +.Tn LIF +format directory at the beginning of an attached disk. +The +.Pa /usr/mdec +directory contains a disk boot programs which should be placed in a +new pack automatically by +.Xr newfs 8 +when the ``a'' partition file system on the pack is created. +.Pp +This +.Em boot +program +finds the corresponding file on the given device +.Pf ( Ar kernel +by default), +loads that file into memory, +and starts the program at the entry address specified in the program header. +.Pp +The boot program can be interrupted by typing `^C' (ctrl-C). +This will force the boot program to interactively prompt for a system to boot. +If not interrupted, it will boot from the device from which the boot +program itself was loaded. +.Pp +The file specifications used for an interactive boot are of the form: +.Pp +.Dl device(unit, minor) +.Pp +where +.Ar device +is the type of the device to be searched, +.Ar unit +is 8 * the hpib number plus the unit number of the disk or tape, +and +.Ar minor +is the disk partition or tape file number. +Normal line editing characters can be used when typing the file specification. +Currently, ``rd'' and ``sd'' are the only valid +.Ar device +specifiers. +.Pp +For example, +to boot from the `a' file system of unit 0 on HP-IB 2, +type +.Ql rd(16, 0)kernel +to the boot prompt. +For tapes, the minor device number gives a file offset. +.Pp +In an emergency, the bootstrap methods described in the paper +.%T Installing 4.3bsd on the HP300 +can be used to boot from a distribution tape. +.Sh FILES +.Bl -tag -width /usr/mdec/installboot -compact +.It Pa /kernel +system code +.It Pa /usr/mdec/bootrd +.Tn LIF +format boot block +.It Pa /usr/mdec/installboot +program to install boot blocks +.El +.Sh SEE ALSO +.Xr halt 8 , +.Xr reboot 8 , +.Xr shutdown 8 diff --git a/sbin/reboot/boot.i386.8 b/sbin/reboot/boot.i386.8 new file mode 100644 index 0000000..d426473 --- /dev/null +++ b/sbin/reboot/boot.i386.8 @@ -0,0 +1,169 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software written and contributed +.\" to Berkeley by William Jolitz. +.\" +.\" Almost completely rewritten for FreeBSD 2.1 by Joerg Wunsch. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)boot_i386.8 8.2 (Berkeley) 4/19/94 +.\" +.\" $Id: boot.i386.8,v 1.1 1997/10/04 05:23:54 gdr Exp $ +.\" +.Dd April 19, 1994 +.Dt BOOT 8 i386 +.Os +.Sh NAME +.Nm boot +.Nd +system bootstrapping procedures +.Sh DESCRIPTION +.Sy Power fail and crash recovery. +Normally, the system will reboot itself at power-up or after crashes. +An automatic consistency check of the file systems will be performed, +and unless this fails, the system will resume multi-user operations. +.Pp +.Sy Cold starts. +Most 386 +.Tn "PC AT" +clones attempt to boot the floppy disk drive 0 (otherwise known as +drive A:) first, and failing that, attempt to boot the hard disk +controller 1, drive 0 (otherwise known as drive C:, or drive 0x80 in +the BIOS). The automatic boot will attempt to load +.Pa /kernel +from partition +.Ql a +of either the floppy or the hard disk. +This boot may be aborted by typing any character on the keyboard +at the +.Ql Boot: +prompt. At this time, the following input will be accepted: +.Bl -tag -offset indent -width 10x +.It \&? +Give a short listing of the files in the root directory of the default +boot device, as a hint about available boot files. +.It Op ctrlr(unit,part) Op /filename Op Fl abcCdhrsv +Specify boot file and flags. +.Bl -tag -offset indent -width 10x -compact +.It ctrlr +The controller to boot from. Note that the controller is required +to have BIOS support since the BIOS services are used to load the +boot file image. +.Pp +Common controller names are: +.Bl -tag -offset indent -width "wdXX" -compact +.It wd +ST506, IDE, ESDI, RLL disks on a WD100[2367] or lookalike +controller +.It fd +5 1/4" or 3 1/2" High density floppies +.It sd +SCSI disk on any supported SCSI controller +.It cd +boot from CDROM +.It hd +Pseudo-controller, must be used to specify that unit number +1 (known to the BIOS as drive 0x81) is on a different controller +than unit number 0. This can happen for the wd vs. sd case. +.El +.It unit +The unit number of the drive on the controller being used. Either 0 +or 1 for the wd and fd and most sd controllers, between 0 and 6 for +some newer sd controllers. +.It part +The partition letter inside the BSD portion of the disk. See +.Xr disklabel 8 . +By convention, only partition +.Ql a +contains a bootable image. If sliced disks are used +.Pq Dq fdisk partitions , +only the first BSD slice can be used to boot from. The partition +letter does always refer to this slice then. +.It /filename +The pathname of the file to boot; must be inside the root directory +of the specified partition. Defaults to +.Pa /kernel . +Symbolic links are not supported (hard links are). +.It Fl abcCdhrsv +Boot flags: +.Bl -tag -offset indent -width "-CXX" -compact +.It Fl a +ask for the device to install as root file system during kernel +initialization +.It Fl b +do not automatically reboot after shutdown or crash +.It Fl c +run UserConfig to modify hardware parameters for the loaded +kernel +.It Fl C +boot from CDROM +.It Fl d +enter the DDB kernel debugger +.Pq see Xr ddb 4 +before configuring any device (except the system's console) +.It Fl h +toggle serial/graphics console +.It Fl r +do not establish the root directory of the file system +hierarchy on the device where the boot file is being loaded +from +.It Fl s +boot into single-user mode; if the console is marked as +.Dq insecure +.Pq see Xr ttys 5 , +the root password must be entered +.It Fl v +be verbose during device probing +.El +.El +.El +.Sh FILES +.Bl -tag -width /kernelxx -compact +.It Pa /kernel +system code +.\" .It Pa /boot +.\" system bootstrap +.El +.Sh SEE ALSO +.Xr ddb 4 , +.Xr ttys 5 , +.Xr halt 8 , +.Xr reboot 8 , +.Xr shutdown 8 , +.Xr disklabel 8 . +.Sh BUGS +The disklabel format used by this version of +.Bx +is quite +different from that of other architectures. +.Pp +The boot flags are not very self-explanatory, and the alphabet has +too few characters to implement every potentially useful boot option. diff --git a/sbin/reboot/boot.sparc.8 b/sbin/reboot/boot.sparc.8 new file mode 100644 index 0000000..c3499bc --- /dev/null +++ b/sbin/reboot/boot.sparc.8 @@ -0,0 +1,89 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)boot_sparc.8 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt REBOOT 8 sparc +.Os +.Sh NAME +.Nm reboot +.Nd +.Tn UNIX +bootstrapping procedures +.Sh SYNOPSIS +.Nm reboot +.Op Fl n +.Op Fl q +.Sh DESCRIPTION +.Sy Power fail and crash recovery. +Normally, the system will reboot itself at power-up or after crashes. +An automatic consistency check of the file systems will be performed +as described in +.Xr fsck 8 . +and unless this fails, the system will resume multi-user operations. +.Pp +.Sy Cold starts +The SPARC system currently uses the SunOS bootstrap loaders. +This will be changed in a future version of the system. +The SunOS boot will attempt to load +.Pa kernel +from partition A of the boot device, +which must currently be an ``sd'' disk. +.Pp +The +.Op Fl s +flag to the SunOS boot loader will being the system up in single-user mode. +The +.Op Fl d +flag to the SunOS boot loader will bring the system up in debug mode. +Here it waits for a kernel debugger connect; see +.Xr kgdb 8 . +Other flags are currently ignored. +.Sh FILES +.Bl -tag -width /kernelxx -compact +.It Pa /kernel +system code +.It Pa /boot +system bootstrap +.El +.Sh SEE ALSO +.Xr crash 8 , +.Xr disklabel 8 , +.Xr fsck 8 , +.Xr halt 8 , +.Xr init 8 , +.Xr rc 8 , +.Xr shutdown 8 , +.Xr syslogd 8 +.Sh BUGS +The use of Sun disk labels, without the ability to write them, +is problematic. diff --git a/sbin/reboot/boot.tahoe.8 b/sbin/reboot/boot.tahoe.8 new file mode 100644 index 0000000..52b2047 --- /dev/null +++ b/sbin/reboot/boot.tahoe.8 @@ -0,0 +1,152 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)boot_tahoe.8 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt BOOT 8 tahoe +.Os +.Sh NAME +.Nm boot +.Nd +system bootstrapping procedures +.Sh DESCRIPTION +.Sy Power fail and crash recovery. +Normally, the system will reboot itself at power-up or after crashes. +An automatic consistency check of the file systems will be performed, +and unless this fails, the system will resume multi-user operations. +.Pp +.Sy Cold starts. +These are processor-type dependent. +On the +.Tn CCI +Power 6/32 and related processors, +the system will do a standard autoboot from drive 0 +upon power-up or reset. +This automatic boot may be cancelled by typing a +.Ql \&# +in the first few seconds after reset. +This enters console mode; the console prompt is +.Ql > +or +.Ql \&# . +The boot flags can be set to any hexadecimal value +.Fl n +with the command +.Pp +.Bd -filled -offset indent -compact +.Li \&#> p23 +.Ar n . +.Ed +.Pp +The default device may be examined or set; see the Diagnostics and Debugging +manual for the processor for details on device naming and syntax. +After setting the boot flags and/or device, +a bootstrap sequence can be initiated with +.Pp +.Dl #> fb +.Pp +A specific device or bootstrap file may be used; for example, +.Pp +.Dl \&#> \&fb xfd(1,0) +.Pp +would boot from the `a' partition on +.Tn XFD +drive 1. +.Pp +The file specifications used for the boostrap +when loaded with the +.Dq askme +flag +(register 23 set to 1 or 3) +are of the form: +.Pp +.Dl device(adaptor,controller,unit,minor) +.Pp +where +.Ar device +is the type of the device to be searched, +.Ar adaptor +is number of the +.Tn VERSAbus +(or +.Tn VMEbus ) +to which the device is attached, +.Ar controller +is the unit number of the controller on that buss, +.Ar unit +is the unit number of the disk or tape, +and +.Ar minor +is the disk partition or tape file number. +Leading adaptor or controller numbers default to 0. +Normal line editing characters can be used when typing the file specification. +The following list of supported devices may vary from installation to +installation: +.Pp +.Bd -unfilled -offset indent -compact +dk SMD or ESDI disks on VDDC or SMD-E +cy tape on Ciprico Tapemaster controller +.Ed +.Pp +For example, +to boot from a file system which starts at cylinder 0 +of unit 0 of an +.Tn SMD-E +disk, type +.Ql dk(0,0)kernel +to the boot prompt; +.Ql dk(2,1,0)kernel +would specify drive 1 on +.Tn SMD-E +controller 2. +.Pp +In an emergency, the bootstrap methods described in the paper +.%T "Installing and Operating 4.3 BSD-tahoe UNIX on the Tahoe" +can be used +to boot from a distribution tape. +.Sh FILES +.Bl -tag -width /kernel -compact +.It Pa /kernel +system code +.It Pa /boot +system bootstrap +.El +.Sh SEE ALSO +.Xr halt 8 , +.Xr reboot 8 , +.Xr shutdown 8 +.Sh BUGS +The disklabel format used by some versions of the console processor +is different than the format used by +.Tn UNIX +and the bootstrap. +.Sh HISTORY diff --git a/sbin/reboot/boot.vax.8 b/sbin/reboot/boot.vax.8 new file mode 100644 index 0000000..37e9cbe --- /dev/null +++ b/sbin/reboot/boot.vax.8 @@ -0,0 +1,322 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)boot_vax.8 8.2 (Berkeley) 4/19/94 +.\" +.Dd April 19, 1994 +.Dt BOOT 8 vax +.Os +.Sh NAME +.Nm boot +.Nd +system bootstrapping procedures +.Sh DESCRIPTION +.Sy Power fail and crash recovery. +Normally, the system will reboot itself at power-up or after crashes. +Provided the auto-restart is enabled on the machine front panel, +an automatic consistency check of the file systems will be performed, +and unless this fails, the system will resume multi-user operations. +.Pp +.Sy Cold starts. +These are processor-type dependent. +On an 11/780, there are two floppy files for each disk controller, +both of which cause boots from unit 0 of the root file system +of a controller located on mba0 or uba0. +One gives a single user shell, while the other invokes the multi-user +automatic reboot. +Thus these files are +.Tn HPS +and +.Tn HPM +for the single +and multi-user boot from +.Tn MASSBUS +RP06/RM03/RM05 disks, +.Tn UPS +and +.Tn UPM +for +.Tn UNIBUS +storage module controller and disks +such as the +.Tn EMULEX +SC-21 +and +.Tn AMPEX +9300 pair, +.Tn RAS +and +.Tn RAM +to boot from +.Tn MSCP +controllers and disks such as the RA81, +or +.Tn HKS +and +.Tn HKM +for RK07 disks. +There is also a script for booting from the default device, +which is normally a copy of one of the standard multi-user boot scripts, +but which may be modified to perform other actions +or to boot from a different unit. +The situation on the 8600 is similar, with scripts loaded from the console RL02. +.Pp +Giving the command +.Pp +.Dl >>>BOOT HPM +.Pp +would boot the system from (e.g.) an RP06 and run the automatic consistency +check as described in +.Xr fsck 8 . +(Note that it may +be necessary to type control-P +and halt the processor +to gain the attention of the +.Tn LSI-11 +before getting the >>> prompt.) +The command +.Pp +.Dl >>>BOOT ANY +.Pp +invokes a version of the boot program in a way which allows you to +specify any system as the system to be booted. +It reads from the console a device specification (see below) followed +immediately by a pathname. +.Pp +The scripts may be modified for local configuration if necessary. +The flags are placed in register 11 (as defined in +.Aq Pa sys/reboot.h ) . +The boot device is specified in register 10. +The encoding of this register is also defined in +.Aq Pa sys/reboot.h . +The current encoding has a historical basis, and is shown in the following +table: +.Pp +.Bd -unfilled -offset indent -compact +bits usage +0-7 boot device type (the device major number) +8-15 disk partition +16-19 drive unit +20-23 controller number +24-27 adaptor number (UNIBUS or MASSBUS as appropriate) +.Ed +.Pp +The adaptor number corresponds to the normal configuration on the 11/750, +and to the order in which adaptors are found on the 11/780 and 8600 +(generally the same as the numbers used by +.Tn UNIX ) . +.Pp +On an 11/750, the reset button will boot from the device +selected by the front panel boot device switch. In systems +with RK07's, position B normally selects the RK07 for boot. +This will boot multi-user. To boot from RK07 with boot flags you +may specify +.Pp +.Bd -unfilled -offset indent -compact +.Li \&>>>B/ Ns Fl n No DMA0 +.Ed +.Pp +where, giving a +.Ar n +of 1 causes the boot program +to ask for the name of the system to be bootstrapped, +giving a +.Ar n +of 2 causes the boot program to come up single +user, and a +.Ar n +of 3 causes both of these actions to occur. +The ``DM'' specifies RK07, the ``A'' represents the adaptor number +.Pf ( Tn UNIBUS +or +.Tn MASSBUS ) , +and the ``0'' is the drive unit number. +Other disk types which may be used are DB +.Pq Tn MASSBUS , +DD (TU58), +and DU +.Pf ( Tn UDA-50/RA +disk). +A non-zero disk partition can be used by adding (partition times 1000 hex) +to +.Ar n . +.Pp +The boot procedure on the Micro +.Tn VAX +II +is similar. +A switch on the back panel sets the power-up action +to autoboot or to halt. +When halted, the processor may be booted using the same syntax +as on the 11/750. +.Pp +The 11/750 boot procedure uses the boot roms to load block 0 off of +the specified device. The /usr/mdec directory contains a number +of bootstrap programs for the various disks which should be placed +in a new pack by +.Xr disklabel 8 . +Similarly, the Micro +.Tn VAX +II boot procedure loads a boot parameter block +from block 0 of the disk. +The +.Xr rdboot +.Dq bootstrap +contains the correct parameters for an +.Tn MSCP +disk such +as the RD53. +.Pp +On any processor, the +.Em boot +program +finds the corresponding file on the given device +.Pf ( Pa kernel +by default), loads that file +into memory location zero, and starts the program at the entry address +specified in the program header (after clearing off the high bit +of the specified entry address). +.Pp +The file specifications used with +.Dq BOOT ANY +or +.Dq \&B/3 +are of the form: +.Pp +.Dl device(adaptor,controller,unit,minor) +.Pp +where +.Ar device +is the type of the device to be searched, +.Ar adaptor +is the +.Tn UNIBUS +or +.Tn MASSBUS +number of the adaptor to which the device is attached, +.Ar controller +is the unit number of the controller or +.Tn MASSBUS +tape formatter on that adaptor, +.Ar unit +is the unit number of the disk or transport slave unit of the tape, +and +.Ar minor +is the disk partition or tape file number. +Leading adaptor or controller numbers default to 0. +Normal line editing characters can be used when typing the file specification. +The following list of supported devices may vary from installation to +installation: +.Pp +.Bd -unfilled -offset indent -compact +hp MASSBUS disk drive +up UNIBUS storage module drive +ht TE16,TU45,TU77 on MASSBUS +kra storage module on a KDB50 +mt TU78 on MASSBUS +hk RK07 on UNIBUS +ra storage module on a MSCP-compatible UNIBUS controller +rb storage module on a 730 IDC +rl RL02 on UNIBUS +tm TM11 emulation tape drives on UNIBUS +tms TMSCP-compatible tape +ts TS11 on UNIBUS +ut UNIBUS TU45 emulator +.Ed +.Pp +For example, +to boot from a file system which starts at cylinder 0 +of unit 0 of a +.Tn MASSBUS +disk, type +.Ql hp(0,0)kernel +to the boot prompt; +.Ql hp(2,0,1,0)kernel +would specify drive 1 on +.Tn MASSBUS +adaptor 2; +.Ql up(0,0)kernel +would specify a +.Tn UNIBUS +drive, +.Ql hk(0,0)kernel +would specify +an RK07 disk drive, +.Ql ra(1,0,0,0)kernel +would specify a +.Tn UDA50 +disk drive on a second +.Tn UNIBUS , +and +.Ql rb(0,0)kernel +would specify a +disk on a 730 +.Tn IDC . +For tapes, the minor device number gives a file offset; +.Ql mt(1,2,3,4) +would specify the fifth file on slave 3 of the formatter +at +.Ql drive +2 on mba 1. +.Pp +On an 11/750 with patchable control store, +microcode patches will be installed by +.Em boot +if the file +.Pa psc750.bin +exists in the root of the filesystem from which the system is booted. +.Pp +In an emergency, the bootstrap methods described in the paper +.%T Installing and Operating 4.3bsd +can be used to boot from a distribution tape. +.Sh FILES +.Bl -tag -width /usr/mdec/xxboot -compact +.It Pa /kernel +system code +.It Pa /boot +system bootstrap +.It Pa /usr/mdec/xxboot +sector-0 boot block for 750, xx is disk type +.It Pa /usr/mdec/bootxx +second-stage boot for 750, xx is disk type +.It Pa /pcs750.bin +microcode patch file on 750 +.El +.Sh SEE ALSO +.Xr arff 8 , +.Xr halt 8 , +.Xr reboot 8 , +.Xr shutdown 8 +.Sh HISTORY +The +.Nm +command appeared in +.Bx 4.0 . diff --git a/sbin/reboot/reboot.8 b/sbin/reboot/reboot.8 new file mode 100644 index 0000000..3b7b76f --- /dev/null +++ b/sbin/reboot/reboot.8 @@ -0,0 +1,97 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)reboot.8 8.1 (Berkeley) 6/9/93 +.\" +.TH REBOOT 8 "4 October 1997" GNO "System Administration" +.SH NAME +.BR reboot , +.BR halt +\- stopping and restarting the system +.SH SYNOPSIS +.BR "halt -nq" +.br +.BR "reboot -nq" +.br +.BR "fasthalt -nq" +.br +.BR "fastboot -nq" +.SH DESCRIPTION +The +.BR halt +and +.BR reboot +utilities flush the file system cache to disk, send all running processes +a SIGTERM (and subsequently a SIGKILL) and, respectively, halt or restart +the system. +The action is logged, including entering a shutdown record into the login +accounting file. +.LP +The options are as follows: +.RS +.IP \fBn\fR +If the +.BR n +option is specified, +the file system cache is not flushed. +This option should probably not be used. +.IP \fBq\fR +If the +.B q +option is specified, +the system is halted or restarted quickly and ungracefully, and only +the flushing of the file system cache is performed. +This option should probably not be used. +.RE +.LP +The +.BR fasthalt +and +.BR fastboot +utilities are nothing more than aliases for the +.BR halt +and +.BR reboot +utilities. +.LP +Normally, the +.BR shutdown (8) +utility is used when the system needs to be halted or restarted, giving +users advance warning of their impending doom. +.SH SEE ALSO +.BR utmp (5), +.BR boot (8), +.BR shutdown (8), +.BR sync (8) +.SH HISTORY +A +.BR reboot +command appeared in Version 6 AT&T UNIX. diff --git a/sbin/reboot/reboot.c b/sbin/reboot/reboot.c new file mode 100644 index 0000000..6d0fee4 --- /dev/null +++ b/sbin/reboot/reboot.c @@ -0,0 +1,204 @@ +/* + * Copyright (c) 1980, 1986, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1980, 1986, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)reboot.c 8.1 (Berkeley) 6/5/93"; +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void err __P((const char *fmt, ...)); +void usage __P((void)); + +int dohalt; + +int +main(argc, argv) + int argc; + char *argv[]; +{ + register int i; + struct passwd *pw; + int ch, howto, lflag, nflag, qflag, sverrno; + char *p, *user; + + if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) { + dohalt = 1; + howto = RB_HALT; + } else + howto = 0; + lflag = nflag = qflag = 0; + while ((ch = getopt(argc, argv, "lnq")) != EOF) + switch(ch) { + case 'l': /* Undocumented; used by shutdown. */ + lflag = 1; + break; + case 'n': + nflag = 1; + howto |= RB_NOSYNC; + break; + case 'q': + qflag = 1; + break; + case '?': + default: + usage(); + } + argc -= optind; + argv += optind; + + if (geteuid()) + err("%s", strerror(EPERM)); + + if (qflag) { + reboot(howto); + err("%s", strerror(errno)); + } + + /* Log the reboot. */ + if (!lflag) { + if ((user = getlogin()) == NULL) + user = (pw = getpwuid(getuid())) ? + pw->pw_name : "???"; + if (dohalt) { + openlog("halt", 0, LOG_AUTH | LOG_CONS); + syslog(LOG_CRIT, "halted by %s", user); + } else { + openlog("reboot", 0, LOG_AUTH | LOG_CONS); + syslog(LOG_CRIT, "rebooted by %s", user); + } + } + logwtmp("~", "shutdown", ""); + + /* + * Do a sync early on, so disks start transfers while we're off + * killing processes. Don't worry about writes done before the + * processes die, the reboot system call syncs the disks. + */ + if (!nflag) + sync(); + + /* Just stop init -- if we fail, we'll restart it. */ + if (kill(1, SIGTSTP) == -1) + err("SIGTSTP init: %s", strerror(errno)); + + /* Ignore the SIGHUP we get when our parent shell dies. */ + (void)signal(SIGHUP, SIG_IGN); + + /* Send a SIGTERM first, a chance to save the buffers. */ + if (kill(-1, SIGTERM) == -1) + err("SIGTERM processes: %s", strerror(errno)); + + /* + * After the processes receive the signal, start the rest of the + * buffers on their way. Wait 5 seconds between the SIGTERM and + * the SIGKILL to give everybody a chance. + */ + sleep(2); + if (!nflag) + sync(); + sleep(3); + + for (i = 1;; ++i) { + if (kill(-1, SIGKILL) == -1) { + if (errno == ESRCH) + break; + goto restart; + } + if (i > 5) { + (void)fprintf(stderr, + "WARNING: some process(es) wouldn't die\n"); + break; + } + (void)sleep(2 * i); + } + + reboot(howto); + /* FALLTHROUGH */ + +restart: + sverrno = errno; + err("%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "", + strerror(sverrno)); + /* NOTREACHED */ +} + +void +usage() +{ + (void)fprintf(stderr, "usage: %s [-nq]\n", dohalt ? "halt" : "reboot"); + exit(1); +} + +#if __STDC__ +#include +#else +#include +#endif + +void +#if __STDC__ +err(const char *fmt, ...) +#else +err(fmt, va_alist) + char *fmt; + va_dcl +#endif +{ + va_list ap; +#if __STDC__ + va_start(ap, fmt); +#else + va_start(ap); +#endif + (void)fprintf(stderr, "%s: ", dohalt ? "halt" : "reboot"); + (void)vfprintf(stderr, fmt, ap); + va_end(ap); + (void)fprintf(stderr, "\n"); + exit(1); + /* NOTREACHED */ +} diff --git a/sbin/shutdown/Makefile b/sbin/shutdown/Makefile new file mode 100644 index 0000000..48847e4 --- /dev/null +++ b/sbin/shutdown/Makefile @@ -0,0 +1,9 @@ +# @(#)Makefile 8.1 (Berkeley) 6/5/93 + +PROG= shutdown +MAN8= shutdown.8 +BINOWN= root +BINGRP= operator +BINMODE=4550 + +.include diff --git a/sbin/shutdown/pathnames.h b/sbin/shutdown/pathnames.h new file mode 100644 index 0000000..9d05838 --- /dev/null +++ b/sbin/shutdown/pathnames.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)pathnames.h 8.1 (Berkeley) 6/5/93 + */ + +#include + +#define _PATH_FASTBOOT "/fastboot" +#define _PATH_HALT "/sbin/halt" +#define _PATH_REBOOT "/sbin/reboot" +#define _PATH_WALL "/usr/bin/wall" diff --git a/sbin/shutdown/shutdown.8 b/sbin/shutdown/shutdown.8 new file mode 100644 index 0000000..44a612a --- /dev/null +++ b/sbin/shutdown/shutdown.8 @@ -0,0 +1,148 @@ +.\" Copyright (c) 1988, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)shutdown.8 8.1 (Berkeley) 6/5/93 +.\" +.TH SHUTDOWN 8 "4 October 1997" GNO "System Administration" +.SH NAME +.BR shutdown +\- close down the system at a given time +.SH SYNOPSIS +.BR shutdown +[ +.B - +] [ +.BR -hkrn +] +.I time +.I "warning-message ..." +.SH DESCRIPTION +.BR Shutdown +provides an automated shutdown procedure for super-users +to nicely notify users when the system is shutting down, +saving them from system administrators, hackers, and gurus, who +would otherwise not bother with such niceties. +.LP +Available friendlinesses: +.RS +.IP \fBh\fR +The system is halted at the specified +.BR time +when +.BR shutdown +execs +.BR halt (8). +.IP \fBk\fR +Kick every body off. +The +.B k +option +does not actually halt the system, but leaves the +system multi-user with logins disabled (for all but super-user). +.IP \fBn\fR +Prevent the normal +.BR sync (2) +before stopping. +.IP \fBr\fR +.BR Shutdown +execs +.BR reboot (8) +at the specified +.BR time . +.IP \fItime\fR +.BR Time +is the time at which +.BR shutdown +will bring the system down and +may be the word +.BR now +(indicating an immediate shutdown) or +specify a future time in one of two formats: +.BR +number , +or +.BR yymmddhhmm , +where the year, month, and day may be defaulted +to the current system values. The first form brings the system down in +.BR number +minutes and the second at the absolute time specified. +.IP \fIwarning-message\fR +Any other arguments comprise the warning message that is broadcast +to users currently logged into the system. +.IP \fB-\fR +If +.BR - +is supplied as an option, the warning message is read from the standard +input. +.RE +.LP +At intervals, becoming more frequent as apocalypse approaches +and starting at ten hours before shutdown, warning messages are displayed +on the terminals of all users logged in. Five minutes before +shutdown, or immediately if shutdown is in less than 5 minutes, +logins are disabled by creating +.B /etc/nologin +and copying the +warning message there. If this file exists when a user attempts to +log in, +.BR login (1) +prints its contents and exits. The file is +removed just before +.BR shutdown +exits. +.LP +At shutdown time a message is written in the system log, containing the +time of shutdown, who initiated the shutdown and the reason. +A terminate +signal is then sent to +.BR init +to bring the system down to single-user state (depending on above +options). +The time of the shutdown and the warning message +are placed in +.B /etc/nologin +and should be used to +inform the users about when the system will be back up +and why it is going down (or anything else). +.SH FILES +.IP \fB/etc/nologin\fR +tells login not to let anyone log in +.SH SEE ALSO +.BR login (1), +.BR wall (1), +.BR halt (8), +.BR reboot (8) +.SH BACKWARD COMPATIBILITY +The hours and minutes in the second time format may be separated by +a colon (``:'') for backward compatibility. +.SH HISTORY +The +.BR shutdown +command appeared in 4.0BSD. diff --git a/sbin/shutdown/shutdown.c b/sbin/shutdown/shutdown.c new file mode 100644 index 0000000..e94e096 --- /dev/null +++ b/sbin/shutdown/shutdown.c @@ -0,0 +1,468 @@ +/* + * Copyright (c) 1988, 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1988, 1990, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)shutdown.c 8.2 (Berkeley) 2/16/94"; +#endif /* not lint */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pathnames.h" + +#ifdef DEBUG +#undef _PATH_NOLOGIN +#define _PATH_NOLOGIN "./nologin" +#endif + +#define H *60*60 +#define M *60 +#define S *1 +#define NOLOG_TIME 5*60 +struct interval { + int timeleft, timetowait; +} tlist[] = { + 10 H, 5 H, 5 H, 3 H, 2 H, 1 H, 1 H, 30 M, + 30 M, 10 M, 20 M, 10 M, 10 M, 5 M, 5 M, 3 M, + 2 M, 1 M, 1 M, 30 S, 30 S, 30 S, + 0, 0, +}; +#undef H +#undef M +#undef S + +static time_t offset, shuttime; +static int dohalt, doreboot, killflg, mbuflen; +static char *nosync, *whom, mbuf[BUFSIZ]; + +void badtime __P((void)); +void die_you_gravy_sucking_pig_dog __P((void)); +void finish __P((int)); +void getoffset __P((char *)); +void loop __P((void)); +void nolog __P((void)); +void timeout __P((int)); +void timewarn __P((int)); +void usage __P((void)); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + extern int optind; + register char *p, *endp; + struct passwd *pw; + int arglen, ch, len, readstdin; + +#ifndef DEBUG + if (geteuid()) { + (void)fprintf(stderr, "shutdown: NOT super-user\n"); + exit(1); + } +#endif + nosync = NULL; + readstdin = 0; + while ((ch = getopt(argc, argv, "-hknr")) != EOF) + switch (ch) { + case '-': + readstdin = 1; + break; + case 'h': + dohalt = 1; + break; + case 'k': + killflg = 1; + break; + case 'n': + nosync = "-n"; + break; + case 'r': + doreboot = 1; + break; + case '?': + default: + usage(); + } + argc -= optind; + argv += optind; + + if (argc < 1) + usage(); + + if (nosync) { + (void)fprintf(stderr, + "shutdown: incompatible switches -f and -n.\n"); + usage(); + } + if (doreboot && dohalt) { + (void)fprintf(stderr, + "shutdown: incompatible switches -h and -r.\n"); + usage(); + } + getoffset(*argv++); + + if (*argv) { + for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) { + arglen = strlen(*argv); + if ((len -= arglen) <= 2) + break; + if (p != mbuf) + *p++ = ' '; + bcopy(*argv, p, arglen); + p += arglen; + } + *p = '\n'; + *++p = '\0'; + } + + if (readstdin) { + p = mbuf; + endp = mbuf + sizeof(mbuf) - 2; + for (;;) { + if (!fgets(p, endp - p + 1, stdin)) + break; + for (; *p && p < endp; ++p); + if (p == endp) { + *p = '\n'; + *++p = '\0'; + break; + } + } + } + mbuflen = strlen(mbuf); + + if (offset) + (void)printf("Shutdown at %.24s.\n", ctime(&shuttime)); + else + (void)printf("Shutdown NOW!\n"); + + if (!(whom = getlogin())) + whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; + +#ifdef DEBUG + (void)putc('\n', stdout); +#else + (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN); + { + int forkpid; + + forkpid = fork(); + if (forkpid == -1) { + perror("shutdown: fork"); + exit(1); + } + if (forkpid) { + (void)printf("shutdown: [pid %d]\n", forkpid); + exit(0); + } + } +#endif + openlog("shutdown", LOG_CONS, LOG_AUTH); + loop(); + /* NOTREACHED */ +} + +void +loop() +{ + struct interval *tp; + u_int sltime; + int logged; + + if (offset <= NOLOG_TIME) { + logged = 1; + nolog(); + } + else + logged = 0; + tp = tlist; + if (tp->timeleft < offset) + (void)sleep((u_int)(offset - tp->timeleft)); + else { + while (offset < tp->timeleft) + ++tp; + /* + * Warn now, if going to sleep more than a fifth of + * the next wait time. + */ + if (sltime = offset - tp->timeleft) { + if (sltime > tp->timetowait / 5) + timewarn(offset); + (void)sleep(sltime); + } + } + for (;; ++tp) { + timewarn(tp->timeleft); + if (!logged && tp->timeleft <= NOLOG_TIME) { + logged = 1; + nolog(); + } + (void)sleep((u_int)tp->timetowait); + if (!tp->timeleft) + break; + } + die_you_gravy_sucking_pig_dog(); +} + +static jmp_buf alarmbuf; + +void +timewarn(timeleft) + int timeleft; +{ + static int first; + static char hostname[MAXHOSTNAMELEN + 1]; + FILE *pf; + char wcmd[MAXPATHLEN + 4]; + + if (!first++) + (void)gethostname(hostname, sizeof(hostname)); + + /* undoc -n option to wall suppresses normal wall banner */ + (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL); + if (!(pf = popen(wcmd, "w"))) { + syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL); + return; + } + + (void)fprintf(pf, + "\007*** %sSystem shutdown message from %s@%s ***\007\n", + timeleft ? "": "FINAL ", whom, hostname); + + if (timeleft > 10*60) + (void)fprintf(pf, "System going down at %5.5s\n\n", + ctime(&shuttime) + 11); + else if (timeleft > 59) + (void)fprintf(pf, "System going down in %d minute%s\n\n", + timeleft / 60, (timeleft > 60) ? "s" : ""); + else if (timeleft) + (void)fprintf(pf, "System going down in 30 seconds\n\n"); + else + (void)fprintf(pf, "System going down IMMEDIATELY\n\n"); + + if (mbuflen) + (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf); + + /* + * play some games, just in case wall doesn't come back + * probably unecessary, given that wall is careful. + */ + if (!setjmp(alarmbuf)) { + (void)signal(SIGALRM, timeout); + (void)alarm((u_int)30); + (void)pclose(pf); + (void)alarm((u_int)0); + (void)signal(SIGALRM, SIG_DFL); + } +} + +void +timeout(signo) + int signo; +{ + longjmp(alarmbuf, 1); +} + +void +die_you_gravy_sucking_pig_dog() +{ + + syslog(LOG_NOTICE, "%s by %s: %s", + doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf); + (void)sleep(2); + + (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); + if (killflg) { + (void)printf("\rbut you'll have to do it yourself\r\n"); + exit(0); + } +#ifdef DEBUG + if (doreboot) + (void)printf("reboot"); + else if (dohalt) + (void)printf("halt"); + if (nosync) + (void)printf(" no sync"); + (void)printf("\nkill -HUP 1\n"); +#else + if (doreboot) { + execle(_PATH_REBOOT, "reboot", "-l", nosync, 0); + syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_REBOOT); + perror("shutdown"); + } + else if (dohalt) { + execle(_PATH_HALT, "halt", "-l", nosync, 0); + syslog(LOG_ERR, "shutdown: can't exec %s: %m.", _PATH_HALT); + perror("shutdown"); + } + (void)kill(1, SIGTERM); /* to single user */ +#endif + finish(0); +} + +#define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2; + +void +getoffset(timearg) + register char *timearg; +{ + register struct tm *lt; + register char *p; + time_t now; + + if (!strcasecmp(timearg, "now")) { /* now */ + offset = 0; + return; + } + + (void)time(&now); + if (*timearg == '+') { /* +minutes */ + if (!isdigit(*++timearg)) + badtime(); + offset = atoi(timearg) * 60; + shuttime = now + offset; + return; + } + + /* handle hh:mm by getting rid of the colon */ + for (p = timearg; *p; ++p) + if (!isascii(*p) || !isdigit(*p)) + if (*p == ':' && strlen(p) == 3) { + p[0] = p[1]; + p[1] = p[2]; + p[2] = '\0'; + } + else + badtime(); + + unsetenv("TZ"); /* OUR timezone */ + lt = localtime(&now); /* current time val */ + + switch(strlen(timearg)) { + case 10: + lt->tm_year = ATOI2(timearg); + /* FALLTHROUGH */ + case 8: + lt->tm_mon = ATOI2(timearg); + if (--lt->tm_mon < 0 || lt->tm_mon > 11) + badtime(); + /* FALLTHROUGH */ + case 6: + lt->tm_mday = ATOI2(timearg); + if (lt->tm_mday < 1 || lt->tm_mday > 31) + badtime(); + /* FALLTHROUGH */ + case 4: + lt->tm_hour = ATOI2(timearg); + if (lt->tm_hour < 0 || lt->tm_hour > 23) + badtime(); + lt->tm_min = ATOI2(timearg); + if (lt->tm_min < 0 || lt->tm_min > 59) + badtime(); + lt->tm_sec = 0; + if ((shuttime = mktime(lt)) == -1) + badtime(); + if ((offset = shuttime - now) < 0) { + (void)fprintf(stderr, + "shutdown: that time is already past.\n"); + exit(1); + } + break; + default: + badtime(); + } +} + +#define NOMSG "\n\nNO LOGINS: System going down at " +void +nolog() +{ + int logfd; + char *ct; + + (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */ + (void)signal(SIGINT, finish); + (void)signal(SIGHUP, finish); + (void)signal(SIGQUIT, finish); + (void)signal(SIGTERM, finish); + if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC, + 0664)) >= 0) { + (void)write(logfd, NOMSG, sizeof(NOMSG) - 1); + ct = ctime(&shuttime); + (void)write(logfd, ct + 11, 5); + (void)write(logfd, "\n\n", 2); + (void)write(logfd, mbuf, strlen(mbuf)); + (void)close(logfd); + } +} + +void +finish(signo) + int signo; +{ + (void)unlink(_PATH_NOLOGIN); + exit(0); +} + +void +badtime() +{ + (void)fprintf(stderr, "shutdown: bad time format.\n"); + exit(1); +} + +void +usage() +{ + fprintf(stderr, "usage: shutdown [-hknr] shutdowntime [ message ]\n"); + exit(1); +} diff --git a/usr.sbin/syslogd/Makefile b/usr.sbin/syslogd/Makefile new file mode 100644 index 0000000..5112bd4 --- /dev/null +++ b/usr.sbin/syslogd/Makefile @@ -0,0 +1,9 @@ +# @(#)Makefile 8.1 (Berkeley) 6/6/93 + +PROG= syslogd +SRCS= syslogd.c ttymsg.c +.PATH: ${.CURDIR}/../../usr.bin/wall +MAN5= syslog.conf.5 +MAN8= syslogd.8 + +.include diff --git a/usr.sbin/syslogd/pathnames.h b/usr.sbin/syslogd/pathnames.h new file mode 100644 index 0000000..2dc61a8 --- /dev/null +++ b/usr.sbin/syslogd/pathnames.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)pathnames.h 8.1 (Berkeley) 6/6/93 + */ + +#include + +#define _PATH_KLOG "/dev/klog" +#define _PATH_LOGCONF "/etc/syslog.conf" +#define _PATH_LOGPID "/var/run/syslog.pid" diff --git a/usr.sbin/syslogd/syslog.conf.5 b/usr.sbin/syslogd/syslog.conf.5 new file mode 100644 index 0000000..39c0d5b --- /dev/null +++ b/usr.sbin/syslogd/syslog.conf.5 @@ -0,0 +1,251 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)syslog.conf.5 8.1 (Berkeley) 6/9/93 +.\" +.TH SYSLOG.CONF 5 "4 October 1997" GNO "File Formats" +.SH NAME +.BR syslog.conf \- +.BR syslogd (8) +configuration file +.SH DESCRIPTION +The +.BR syslog.conf +file is the configuration file for the +.BR syslogd (8) +program. +It consists of +blocks of lines separated by +.IR program +specifications, +with each line containing two fields: the +.IR selector +field which specifies the types of messages and priorities to which the +line applies, and an +.IR action +field which specifies the action to be taken if a message +.BR syslogd +receives matches the selection criteria. +The +.IR selector +field is separated from the +.IR action +field by one or more tab characters. +.LP +The +.IR Selectors +function +are encoded as a +.IR facility , +a period (``.''), and a +.IR level , +with no intervening white-space. +Both the +.IR facility +and the +.IR level +are case insensitive. +.LP +The +.IR facility +describes the part of the system generating the message, and is one of +the following keywords: auth, authpriv, cron, daemon, kern, lpr, mail, +mark, news, syslog, user, uucp and local0 through local7. +These keywords (with the exception of mark) correspond to the +similar +.B LOG_ +values specified to the +.BR openlog (3) +and +.BR syslog (3) +library routines. +.LP +The +.IR level +describes the severity of the message, and is a keyword from the +following ordered list (higher to lower): emerg, alert, crit, err, +warning, notice and debug. +These keywords correspond to the +similar +.B LOG_ +values specified to the +.BR syslog +library routine. +.LP +Each block of lines is separated from the previous block by a tag. The tag +is a line beginning with +.IR #!prog +or +.IR !prog +(the former is for compatibility with the previous syslogd, if one is sharing +syslog.conf files, for example) +and each block will be associated with calls to syslog from that specific +program. +.LP +See +.BR syslog (3) +for a further descriptions of both the +.IR facility +and +.IR level +keywords and their significance. It's preferred that selections be made on +.IR facility +rather than +.IR program , +since the latter can easily vary in a networked environment. In some cases, +though, an appropriate +.IR facility +simply doesn't exist (for example, +.IR ftpd +logs under LOG_DAEMON along with a myriad other programs). +.LP +If a received message matches the specified +.IR facility +and is of the specified +.IR level +.IR (or "a higher level) ," +and the first word in the message after the date matches the +.IR program , +the action specified in the +.IR action +field will be taken. +.LP +Multiple +.IR selectors +may be specified for a single +.IR action +by separating them with semicolon (``;'') characters. +It is important to note, however, that each +.IR selector +can modify the ones preceding it. +.LP +Multiple +.IR facilities +may be specified for a single +.IR level +by separating them with comma (``,'') characters. +.LP +An asterisk (``*'') can be used to specify all +.IR facilities +all +.IR levels +or all +.IR programs . +.LP +The special +.IR facility +``mark'' receives a message at priority ``info'' every 20 minutes +(see +.BR syslogd (8)). +This is not enabled by a +.IR facility +field containing an asterisk. +.LP +The special +.IR level +``none'' disables a particular +.IR facility . +.LP +The +.IR action +field of each line specifies the action to be taken when the +.IR selector +field selects a message. +There are four forms: +.RS +A pathname (beginning with a leading slash). +Selected messages are appended to the file. +.LP +A hostname (preceded by an at (``@'') sign). +Selected messages are forwarded to the +.BR syslogd +program on the named host. +.LP +A comma separated list of users. +Selected messages are written to those users +if they are logged in. +.LP +An asterisk. +Selected messages are written to all logged-in users. +.RE +.LP +Blank lines and lines whose first non-blank character is a hash (``#'') +character are ignored. +.SH EXAMPLES +.LP +A configuration file might appear as follows: +.nf +# Log all kernel messages, authentication messages of +# level notice or higher and anything of level err or +# higher to the console. +# Don't log private authentication messages! +*.err;kern.*;auth.notice;authpriv.none /dev/console + +# Log anything (except mail) of level info or higher. +# Don't log private authentication messages! +*.info;mail.none;authpriv.none /var/log/messages + +# The authpriv file has restricted access. +authpriv.* /var/log/secure + +# Log all the mail messages in one place. +mail.* /var/log/maillog + +# Everybody gets emergency messages, plus log them on another +# machine. +*.emerg * +*.emerg @arpa.berkeley.edu + +# Root and Eric get alert and higher messages. +*.alert root,eric + +# Save mail and news errors of level err and higher in a +# special file. +uucp,news.crit /var/log/spoolerr + +# Save ftpd transactions along with mail and news +!ftpd +*.* /var/log/spoolerr +.fi +.SH FILES +.RS +.IP \fB/etc/syslog.conf\fR +The +.BR syslogd (8) +configuration file. +.RE +.SH BUGS +The effects of multiple selectors are sometimes not intuitive. +For example ``mail.crit,*.err'' will select ``mail'' facility messages at +the level of ``err'' or higher, not at the level of ``crit'' or higher. +.SH SEE ALSO +.BR syslog (3), +.BR syslogd (8) diff --git a/usr.sbin/syslogd/syslogd.8 b/usr.sbin/syslogd/syslogd.8 new file mode 100644 index 0000000..44880d8 --- /dev/null +++ b/usr.sbin/syslogd/syslogd.8 @@ -0,0 +1,127 @@ +.\" Copyright (c) 1983, 1986, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)syslogd.8 8.1 (Berkeley) 6/6/93 +.\" +.TH SYSLOGD 8 "4 October 1997" GNO "System Administration" +.SH NAME +.BR syslogd +\- log systems messages +.SH SYNOPSIS +.BR syslogd +[ +.BR -f +.I config_file +] [ +.BR -m +.I mark_interval +] [ +.BR -p +.I log_socket +] +.SH DESCRIPTION +.BR Syslogd +reads and logs messages to the system console, log files, other +machines and/or users as specified by its configuration file. +The options are as follows: +.RS +.IP \fB-f\fR +Specify the pathname of an alternate configuration file; +the default is +.BR /etc/syslog.conf . +.IP \fB-m\fR +Select the number of minutes between ``mark'' messages; +the default is 20 minutes. +.IP \fB-p\fR +Specify the pathname of an alternate log socket; +the default is +.BR /dev/log . +.RE +.LP +.BR Syslogd +reads its configuration file when it starts up and whenever it +receives a hangup signal. +For information on the format of the configuration file, +see +.BR syslog.conf (5). +.LP +.BR Syslogd +reads messages from the +.B UNIX +domain socket +.BR /dev/log , +from an Internet domain socket specified in +.BR /etc/services , +and from the special device +.BR /dev/klog +(to read kernel messages). +.LP +.BR Syslogd +creates the file +.BR /var/run/syslog.pid , +and stores its process +id there. +This can be used to kill or reconfigure +.BR syslogd . +.LP +The message sent to +.BR syslogd +should consist of a single line. +The message can contain a priority code, which should be a preceding +decimal number in angle braces, for example, +.BR 5 . +This priority code should map into the priorities defined in the +include file +.BR . +.SH FILES +.RS +.IP \fB/etc/syslog.conf\fR +The configuration file. +.IP \fB/var/run/syslog.pid\fR +The process id of current +.BR syslogd . +.IP \fB/dev/log\fR +Name of the +.B UNIX +domain datagram log socket. +.IP \fB/dev/klog\fR +The kernel log device. +.RE +.SH SEE ALSO +.BR logger (1), +.BR syslog (3), +.BR services (5), +.BR syslog.conf (5) +.SH HISTORY +The +.BR syslogd +command appeared in +BSD 4.3. diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c new file mode 100644 index 0000000..5cae35c --- /dev/null +++ b/usr.sbin/syslogd/syslogd.c @@ -0,0 +1,1194 @@ +/* + * Copyright (c) 1983, 1988, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1983, 1988, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; +#endif /* not lint */ + +/* + * syslogd -- log system messages + * + * This program implements a system log. It takes a series of lines. + * Each line may have a priority, signified as "" as + * the first characters of the line. If this is + * not present, a default priority is used. + * + * To kill syslogd, send a signal 15 (terminate). A signal 1 (hup) will + * cause it to reread its configuration file. + * + * Defined Constants: + * + * MAXLINE -- the maximimum line length that can be handled. + * DEFUPRI -- the default priority for user messages + * DEFSPRI -- the default priority for kernel messages + * + * Author: Eric Allman + * extensive changes by Ralph Campbell + * more extensive changes by Eric Allman (again) + * Extension to log by program name as well as facility and priority + * by Peter da Silva. + */ + +#define MAXLINE 1024 /* maximum line length */ +#define MAXSVLINE 120 /* maximum saved line length */ +#define DEFUPRI (LOG_USER|LOG_NOTICE) +#define DEFSPRI (LOG_KERN|LOG_CRIT) +#define TIMERINTVL 30 /* interval for checking flush, mark */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "pathnames.h" + +#define SYSLOG_NAMES +#include + +char *LogName = _PATH_LOG; +char *ConfFile = _PATH_LOGCONF; +char *PidFile = _PATH_LOGPID; +char ctty[] = _PATH_CONSOLE; + +#define FDMASK(fd) (1 << (fd)) + +#define dprintf if (Debug) printf + +#define MAXUNAMES 20 /* maximum number of user names */ + +/* + * Flags to logmsg(). + */ + +#define IGN_CONS 0x001 /* don't print on console */ +#define SYNC_FILE 0x002 /* do fsync on file after printing */ +#define ADDDATE 0x004 /* add a date to the message */ +#define MARK 0x008 /* this message is a mark */ + +/* + * This structure represents the files that will have log + * copies printed. + */ + +struct filed { + struct filed *f_next; /* next in linked list */ + short f_type; /* entry type, see below */ + short f_file; /* file descriptor */ + time_t f_time; /* time this was last written */ + u_char f_pmask[LOG_NFACILITIES+1]; /* priority mask */ + char *f_program; /* program this applies to */ + union { + char f_uname[MAXUNAMES][UT_NAMESIZE+1]; + struct { + char f_hname[MAXHOSTNAMELEN+1]; + struct sockaddr_in f_addr; + } f_forw; /* forwarding address */ + char f_fname[MAXPATHLEN]; + } f_un; + char f_prevline[MAXSVLINE]; /* last message logged */ + char f_lasttime[16]; /* time of last occurrence */ + char f_prevhost[MAXHOSTNAMELEN+1]; /* host from which recd. */ + int f_prevpri; /* pri of f_prevline */ + int f_prevlen; /* length of f_prevline */ + int f_prevcount; /* repetition cnt of prevline */ + int f_repeatcount; /* number of "repeated" msgs */ +}; + +/* + * Intervals at which we flush out "message repeated" messages, + * in seconds after previous message is logged. After each flush, + * we move to the next interval until we reach the largest. + */ +int repeatinterval[] = { 30, 120, 600 }; /* # of secs before flush */ +#define MAXREPEAT ((sizeof(repeatinterval) / sizeof(repeatinterval[0])) - 1) +#define REPEATTIME(f) ((f)->f_time + repeatinterval[(f)->f_repeatcount]) +#define BACKOFF(f) { if (++(f)->f_repeatcount > MAXREPEAT) \ + (f)->f_repeatcount = MAXREPEAT; \ + } + +/* values for f_type */ +#define F_UNUSED 0 /* unused entry */ +#define F_FILE 1 /* regular file */ +#define F_TTY 2 /* terminal */ +#define F_CONSOLE 3 /* console terminal */ +#define F_FORW 4 /* remote machine */ +#define F_USERS 5 /* list of users */ +#define F_WALL 6 /* everyone logged on */ + +char *TypeNames[7] = { + "UNUSED", "FILE", "TTY", "CONSOLE", + "FORW", "USERS", "WALL" +}; + +struct filed *Files; +struct filed consfile; + +int Debug; /* debug flag */ +char LocalHostName[MAXHOSTNAMELEN+1]; /* our hostname */ +char *LocalDomain; /* our local domain name */ +int InetInuse = 0; /* non-zero if INET sockets are being used */ +int finet; /* Internet datagram socket */ +int LogPort; /* port number for INET connections */ +int Initialized = 0; /* set when we have initialized ourselves */ +int MarkInterval = 20 * 60; /* interval between marks in seconds */ +int MarkSeq = 0; /* mark sequence number */ + +void cfline __P((char *, struct filed *, char *)); +char *cvthname __P((struct sockaddr_in *)); +int decode __P((const char *, CODE *)); +void die __P((int)); +void domark __P((int)); +void fprintlog __P((struct filed *, int, char *)); +void init __P((int)); +void logerror __P((char *)); +void logmsg __P((int, char *, char *, int)); +void printline __P((char *, char *)); +void printsys __P((char *)); +void reapchild __P((int)); +char *ttymsg __P((struct iovec *, int, char *, int)); +void usage __P((void)); +void wallmsg __P((struct filed *, struct iovec *)); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + int ch, funix, i, inetm, fklog, klogm, len; + struct sockaddr_un sunx, fromunix; + struct sockaddr_in sin, frominet; + FILE *fp; + char *p, line[MSG_BSIZE + 1]; + + while ((ch = getopt(argc, argv, "df:m:p:")) != EOF) + switch(ch) { + case 'd': /* debug */ + Debug++; + break; + case 'f': /* configuration file */ + ConfFile = optarg; + break; + case 'm': /* mark interval */ + MarkInterval = atoi(optarg) * 60; + break; + case 'p': /* path */ + LogName = optarg; + break; + case '?': + default: + usage(); + } + if ((argc -= optind) != 0) + usage(); + + if (!Debug) + (void)daemon(0, 0); + else + setlinebuf(stdout); + + consfile.f_type = F_CONSOLE; + (void)strcpy(consfile.f_un.f_fname, ctty); + (void)gethostname(LocalHostName, sizeof(LocalHostName)); + if ((p = strchr(LocalHostName, '.')) != NULL) { + *p++ = '\0'; + LocalDomain = p; + } else + LocalDomain = ""; + (void)signal(SIGTERM, die); + (void)signal(SIGINT, Debug ? die : SIG_IGN); + (void)signal(SIGQUIT, Debug ? die : SIG_IGN); + (void)signal(SIGCHLD, reapchild); + (void)signal(SIGALRM, domark); + (void)alarm(TIMERINTVL); + (void)unlink(LogName); + +#ifndef SUN_LEN +#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) +#endif + memset(&sunx, 0, sizeof(sunx)); + sunx.sun_family = AF_UNIX; + (void)strncpy(sunx.sun_path, LogName, sizeof(sunx.sun_path)); + funix = socket(AF_UNIX, SOCK_DGRAM, 0); + if (funix < 0 || + bind(funix, (struct sockaddr *)&sunx, SUN_LEN(&sunx)) < 0 || + chmod(LogName, 0666) < 0) { + (void) sprintf(line, "cannot create %s", LogName); + logerror(line); + dprintf("cannot create %s (%d)\n", LogName, errno); + die(0); + } + finet = socket(AF_INET, SOCK_DGRAM, 0); + inetm = 0; + if (finet >= 0) { + struct servent *sp; + + sp = getservbyname("syslog", "udp"); + if (sp == NULL) { + errno = 0; + logerror("syslog/udp: unknown service"); + die(0); + } + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_port = LogPort = sp->s_port; + if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { + logerror("bind"); + if (!Debug) + die(0); + } else { + inetm = FDMASK(finet); + InetInuse = 1; + } + } + if ((fklog = open(_PATH_KLOG, O_RDONLY, 0)) >= 0) + klogm = FDMASK(fklog); + else { + dprintf("can't open %s (%d)\n", _PATH_KLOG, errno); + klogm = 0; + } + + /* tuck my process id away */ + fp = fopen(PidFile, "w"); + if (fp != NULL) { + fprintf(fp, "%d\n", getpid()); + (void) fclose(fp); + } + + dprintf("off & running....\n"); + + init(0); + (void)signal(SIGHUP, init); + + for (;;) { + int nfds, readfds = FDMASK(funix) | inetm | klogm; + + dprintf("readfds = %#x\n", readfds); + nfds = select(20, (fd_set *)&readfds, (fd_set *)NULL, + (fd_set *)NULL, (struct timeval *)NULL); + if (nfds == 0) + continue; + if (nfds < 0) { + if (errno != EINTR) + logerror("select"); + continue; + } + dprintf("got a message (%d, %#x)\n", nfds, readfds); + if (readfds & klogm) { + i = read(fklog, line, sizeof(line) - 1); + if (i > 0) { + line[i] = '\0'; + printsys(line); + } else if (i < 0 && errno != EINTR) { + logerror("klog"); + fklog = -1; + klogm = 0; + } + } + if (readfds & FDMASK(funix)) { + len = sizeof(fromunix); + i = recvfrom(funix, line, MAXLINE, 0, + (struct sockaddr *)&fromunix, &len); + if (i > 0) { + line[i] = '\0'; + printline(LocalHostName, line); + } else if (i < 0 && errno != EINTR) + logerror("recvfrom unix"); + } + if (readfds & inetm) { + len = sizeof(frominet); + i = recvfrom(finet, line, MAXLINE, 0, + (struct sockaddr *)&frominet, &len); + if (i > 0) { + line[i] = '\0'; + printline(cvthname(&frominet), line); + } else if (i < 0 && errno != EINTR) + logerror("recvfrom inet"); + } + } +} + +void +usage() +{ + + (void)fprintf(stderr, + "usage: syslogd [-f conffile] [-m markinterval] [-p logpath]\n"); + exit(1); +} + +/* + * Take a raw input line, decode the message, and print the message + * on the appropriate log files. + */ +void +printline(hname, msg) + char *hname; + char *msg; +{ + int c, pri; + char *p, *q, line[MAXLINE + 1]; + + /* test for special codes */ + pri = DEFUPRI; + p = msg; + if (*p == '<') { + pri = 0; + while (isdigit(*++p)) + pri = 10 * pri + (*p - '0'); + if (*p == '>') + ++p; + } + if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) + pri = DEFUPRI; + + /* don't allow users to log kernel messages */ + if (LOG_FAC(pri) == LOG_KERN) + pri = LOG_MAKEPRI(LOG_USER, LOG_PRI(pri)); + + q = line; + + while ((c = *p++ & 0177) != '\0' && + q < &line[sizeof(line) - 1]) + if (iscntrl(c)) + if (c == '\n') + *q++ = ' '; + else if (c == '\t') + *q++ = '\t'; + else { + *q++ = '^'; + *q++ = c ^ 0100; + } + else + *q++ = c; + *q = '\0'; + + logmsg(pri, line, hname, 0); +} + +/* + * Take a raw input line from /dev/klog, split and format similar to syslog(). + */ +void +printsys(msg) + char *msg; +{ + int c, pri, flags; + char *lp, *p, *q, line[MAXLINE + 1]; + + (void)strcpy(line, getbootfile()); + (void)strcat(line, ": "); + lp = line + strlen(line); + for (p = msg; *p != '\0'; ) { + flags = SYNC_FILE | ADDDATE; /* fsync file after write */ + pri = DEFSPRI; + if (*p == '<') { + pri = 0; + while (isdigit(*++p)) + pri = 10 * pri + (*p - '0'); + if (*p == '>') + ++p; + } else { + /* kernel printf's come out on console */ + flags |= IGN_CONS; + } + if (pri &~ (LOG_FACMASK|LOG_PRIMASK)) + pri = DEFSPRI; + q = lp; + while (*p != '\0' && (c = *p++) != '\n' && + q < &line[MAXLINE]) + *q++ = c; + *q = '\0'; + logmsg(pri, line, LocalHostName, flags); + } +} + +time_t now; + +/* + * Log a message to the appropriate log files, users, etc. based on + * the priority. + */ +void +logmsg(pri, msg, from, flags) + int pri; + char *msg, *from; + int flags; +{ + struct filed *f; + int fac, msglen, omask, prilev; + char *timestamp; + char prog[NAME_MAX+1]; + int i; + + dprintf("logmsg: pri %o, flags %x, from %s, msg %s\n", + pri, flags, from, msg); + + omask = sigblock(sigmask(SIGHUP)|sigmask(SIGALRM)); + + /* + * Check to see if msg looks non-standard. + */ + msglen = strlen(msg); + if (msglen < 16 || msg[3] != ' ' || msg[6] != ' ' || + msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') + flags |= ADDDATE; + + (void)time(&now); + if (flags & ADDDATE) + timestamp = ctime(&now) + 4; + else { + timestamp = msg; + msg += 16; + msglen -= 16; + } + + /* skip leading blanks */ + while(isspace(*msg)) { + msg++; + msglen--; + } + + /* extract facility and priority level */ + if (flags & MARK) + fac = LOG_NFACILITIES; + else + fac = LOG_FAC(pri); + prilev = LOG_PRI(pri); + + /* extract program name */ + for(i = 0; i < NAME_MAX; i++) { + if(!isalnum(msg[i])) + break; + prog[i] = msg[i]; + } + prog[i] = 0; + + /* log the message to the particular outputs */ + if (!Initialized) { + f = &consfile; + f->f_file = open(ctty, O_WRONLY, 0); + + if (f->f_file >= 0) { + fprintlog(f, flags, msg); + (void)close(f->f_file); + } + (void)sigsetmask(omask); + return; + } + for (f = Files; f; f = f->f_next) { + /* skip messages that are incorrect priority */ + if (f->f_pmask[fac] < prilev || + f->f_pmask[fac] == INTERNAL_NOPRI) + continue; + /* skip messages with the incorrect program name */ + if(f->f_program) + if(strcmp(prog, f->f_program) != 0) + continue; + + if (f->f_type == F_CONSOLE && (flags & IGN_CONS)) + continue; + + /* don't output marks to recently written files */ + if ((flags & MARK) && (now - f->f_time) < MarkInterval / 2) + continue; + + /* + * suppress duplicate lines to this file + */ + if ((flags & MARK) == 0 && msglen == f->f_prevlen && + !strcmp(msg, f->f_prevline) && + !strcmp(from, f->f_prevhost)) { + (void)strncpy(f->f_lasttime, timestamp, 15); + f->f_prevcount++; + dprintf("msg repeated %d times, %ld sec of %d\n", + f->f_prevcount, now - f->f_time, + repeatinterval[f->f_repeatcount]); + /* + * If domark would have logged this by now, + * flush it now (so we don't hold isolated messages), + * but back off so we'll flush less often + * in the future. + */ + if (now > REPEATTIME(f)) { + fprintlog(f, flags, (char *)NULL); + BACKOFF(f); + } + } else { + /* new line, save it */ + if (f->f_prevcount) + fprintlog(f, 0, (char *)NULL); + f->f_repeatcount = 0; + (void)strncpy(f->f_lasttime, timestamp, 15); + (void)strncpy(f->f_prevhost, from, + sizeof(f->f_prevhost)); + if (msglen < MAXSVLINE) { + f->f_prevlen = msglen; + f->f_prevpri = pri; + (void)strcpy(f->f_prevline, msg); + fprintlog(f, flags, (char *)NULL); + } else { + f->f_prevline[0] = 0; + f->f_prevlen = 0; + fprintlog(f, flags, msg); + } + } + } + (void)sigsetmask(omask); +} + +void +fprintlog(f, flags, msg) + struct filed *f; + int flags; + char *msg; +{ + struct iovec iov[6]; + struct iovec *v; + int l; + char line[MAXLINE + 1], repbuf[80], greetings[200]; + + v = iov; + if (f->f_type == F_WALL) { + v->iov_base = greetings; + v->iov_len = sprintf(greetings, + "\r\n\7Message from syslogd@%s at %.24s ...\r\n", + f->f_prevhost, ctime(&now)); + v++; + v->iov_base = ""; + v->iov_len = 0; + v++; + } else { + v->iov_base = f->f_lasttime; + v->iov_len = 15; + v++; + v->iov_base = " "; + v->iov_len = 1; + v++; + } + v->iov_base = f->f_prevhost; + v->iov_len = strlen(v->iov_base); + v++; + v->iov_base = " "; + v->iov_len = 1; + v++; + + if (msg) { + v->iov_base = msg; + v->iov_len = strlen(msg); + } else if (f->f_prevcount > 1) { + v->iov_base = repbuf; + v->iov_len = sprintf(repbuf, "last message repeated %d times", + f->f_prevcount); + } else { + v->iov_base = f->f_prevline; + v->iov_len = f->f_prevlen; + } + v++; + + dprintf("Logging to %s", TypeNames[f->f_type]); + f->f_time = now; + + switch (f->f_type) { + case F_UNUSED: + dprintf("\n"); + break; + + case F_FORW: + dprintf(" %s\n", f->f_un.f_forw.f_hname); + l = sprintf(line, "<%d>%.15s %s", f->f_prevpri, + iov[0].iov_base, iov[4].iov_base); + if (l > MAXLINE) + l = MAXLINE; + if (sendto(finet, line, l, 0, + (struct sockaddr *)&f->f_un.f_forw.f_addr, + sizeof(f->f_un.f_forw.f_addr)) != l) { + int e = errno; + (void)close(f->f_file); + f->f_type = F_UNUSED; + errno = e; + logerror("sendto"); + } + break; + + case F_CONSOLE: + if (flags & IGN_CONS) { + dprintf(" (ignored)\n"); + break; + } + /* FALLTHROUGH */ + + case F_TTY: + case F_FILE: + dprintf(" %s\n", f->f_un.f_fname); + if (f->f_type != F_FILE) { + v->iov_base = "\r\n"; + v->iov_len = 2; + } else { + v->iov_base = "\n"; + v->iov_len = 1; + } + again: + if (writev(f->f_file, iov, 6) < 0) { + int e = errno; + (void)close(f->f_file); + /* + * Check for errors on TTY's due to loss of tty + */ + if ((e == EIO || e == EBADF) && f->f_type != F_FILE) { + f->f_file = open(f->f_un.f_fname, + O_WRONLY|O_APPEND, 0); + if (f->f_file < 0) { + f->f_type = F_UNUSED; + logerror(f->f_un.f_fname); + } else + goto again; + } else { + f->f_type = F_UNUSED; + errno = e; + logerror(f->f_un.f_fname); + } + } else if (flags & SYNC_FILE) + (void)fsync(f->f_file); + break; + + case F_USERS: + case F_WALL: + dprintf("\n"); + v->iov_base = "\r\n"; + v->iov_len = 2; + wallmsg(f, iov); + break; + } + f->f_prevcount = 0; +} + +/* + * WALLMSG -- Write a message to the world at large + * + * Write the specified message to either the entire + * world, or a list of approved users. + */ +void +wallmsg(f, iov) + struct filed *f; + struct iovec *iov; +{ + static int reenter; /* avoid calling ourselves */ + FILE *uf; + struct utmp ut; + int i; + char *p; + char line[sizeof(ut.ut_line) + 1]; + + if (reenter++) + return; + if ((uf = fopen(_PATH_UTMP, "r")) == NULL) { + logerror(_PATH_UTMP); + reenter = 0; + return; + } + /* NOSTRICT */ + while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) { + if (ut.ut_name[0] == '\0') + continue; + strncpy(line, ut.ut_line, sizeof(ut.ut_line)); + line[sizeof(ut.ut_line)] = '\0'; + if (f->f_type == F_WALL) { + if ((p = ttymsg(iov, 6, line, 60*5)) != NULL) { + errno = 0; /* already in msg */ + logerror(p); + } + continue; + } + /* should we send the message to this user? */ + for (i = 0; i < MAXUNAMES; i++) { + if (!f->f_un.f_uname[i][0]) + break; + if (!strncmp(f->f_un.f_uname[i], ut.ut_name, + UT_NAMESIZE)) { + if ((p = ttymsg(iov, 6, line, 60*5)) != NULL) { + errno = 0; /* already in msg */ + logerror(p); + } + break; + } + } + } + (void)fclose(uf); + reenter = 0; +} + +void +reapchild(signo) + int signo; +{ + union wait status; + + while (wait3((int *)&status, WNOHANG, (struct rusage *)NULL) > 0) + ; +} + +/* + * Return a printable representation of a host address. + */ +char * +cvthname(f) + struct sockaddr_in *f; +{ + struct hostent *hp; + char *p; + + dprintf("cvthname(%s)\n", inet_ntoa(f->sin_addr)); + + if (f->sin_family != AF_INET) { + dprintf("Malformed from address\n"); + return ("???"); + } + hp = gethostbyaddr((char *)&f->sin_addr, + sizeof(struct in_addr), f->sin_family); + if (hp == 0) { + dprintf("Host name for your address (%s) unknown\n", + inet_ntoa(f->sin_addr)); + return (inet_ntoa(f->sin_addr)); + } + if ((p = strchr(hp->h_name, '.')) && strcmp(p + 1, LocalDomain) == 0) + *p = '\0'; + return (hp->h_name); +} + +void +domark(signo) + int signo; +{ + struct filed *f; + + now = time((time_t *)NULL); + MarkSeq += TIMERINTVL; + if (MarkSeq >= MarkInterval) { + logmsg(LOG_INFO, "-- MARK --", LocalHostName, ADDDATE|MARK); + MarkSeq = 0; + } + + for (f = Files; f; f = f->f_next) { + if (f->f_prevcount && now >= REPEATTIME(f)) { + dprintf("flush %s: repeated %d times, %d sec.\n", + TypeNames[f->f_type], f->f_prevcount, + repeatinterval[f->f_repeatcount]); + fprintlog(f, 0, (char *)NULL); + BACKOFF(f); + } + } + (void)alarm(TIMERINTVL); +} + +/* + * Print syslogd errors some place. + */ +void +logerror(type) + char *type; +{ + char buf[100]; + + if (errno) + (void)snprintf(buf, + sizeof(buf), "syslogd: %s: %s", type, strerror(errno)); + else + (void)snprintf(buf, sizeof(buf), "syslogd: %s", type); + errno = 0; + dprintf("%s\n", buf); + logmsg(LOG_SYSLOG|LOG_ERR, buf, LocalHostName, ADDDATE); +} + +void +die(signo) + int signo; +{ + struct filed *f; + char buf[100]; + + for (f = Files; f != NULL; f = f->f_next) { + /* flush any pending output */ + if (f->f_prevcount) + fprintlog(f, 0, (char *)NULL); + } + if (signo) { + dprintf("syslogd: exiting on signal %d\n", signo); + (void)sprintf(buf, "exiting on signal %d", signo); + errno = 0; + logerror(buf); + } + (void)unlink(LogName); + exit(0); +} + +/* + * INIT -- Initialize syslogd from configuration table + */ +void +init(signo) + int signo; +{ + int i; + FILE *cf; + struct filed *f, *next, **nextp; + char *p; + char cline[LINE_MAX]; + char prog[NAME_MAX+1]; + + dprintf("init\n"); + + /* + * Close all open log files. + */ + Initialized = 0; + for (f = Files; f != NULL; f = next) { + /* flush any pending output */ + if (f->f_prevcount) + fprintlog(f, 0, (char *)NULL); + + switch (f->f_type) { + case F_FILE: + case F_TTY: + case F_CONSOLE: + case F_FORW: + (void)close(f->f_file); + break; + } + next = f->f_next; + if(f->f_program) free(f->f_program); + free((char *)f); + } + Files = NULL; + nextp = &Files; + + /* open the configuration file */ + if ((cf = fopen(ConfFile, "r")) == NULL) { + dprintf("cannot open %s\n", ConfFile); + *nextp = (struct filed *)calloc(1, sizeof(*f)); + cfline("*.ERR\t/dev/console", *nextp, "*"); + (*nextp)->f_next = (struct filed *)calloc(1, sizeof(*f)); + cfline("*.PANIC\t*", (*nextp)->f_next, "*"); + Initialized = 1; + return; + } + + /* + * Foreach line in the conf table, open that file. + */ + f = NULL; + strcpy(prog, "*"); + while (fgets(cline, sizeof(cline), cf) != NULL) { + /* + * check for end-of-section, comments, strip off trailing + * spaces and newline character. #!prog is treated specially: + * following lines apply only to that program. + */ + for (p = cline; isspace(*p); ++p) + continue; + if (*p == 0) + continue; + if(*p == '#') { + p++; + if(*p!='!') + continue; + } + if(*p=='!') { + p++; + while(isspace(*p)) p++; + if(!*p) { + strcpy(prog, "*"); + continue; + } + for(i = 0; i < NAME_MAX; i++) { + if(!isalnum(p[i])) + break; + prog[i] = p[i]; + } + prog[i] = 0; + continue; + } + for (p = strchr(cline, '\0'); isspace(*--p);) + continue; + *++p = '\0'; + f = (struct filed *)calloc(1, sizeof(*f)); + *nextp = f; + nextp = &f->f_next; + cfline(cline, f, prog); + } + + /* close the configuration file */ + (void)fclose(cf); + + Initialized = 1; + + if (Debug) { + for (f = Files; f; f = f->f_next) { + for (i = 0; i <= LOG_NFACILITIES; i++) + if (f->f_pmask[i] == INTERNAL_NOPRI) + printf("X "); + else + printf("%d ", f->f_pmask[i]); + printf("%s: ", TypeNames[f->f_type]); + switch (f->f_type) { + case F_FILE: + case F_TTY: + case F_CONSOLE: + printf("%s", f->f_un.f_fname); + break; + + case F_FORW: + printf("%s", f->f_un.f_forw.f_hname); + break; + + case F_USERS: + for (i = 0; i < MAXUNAMES && *f->f_un.f_uname[i]; i++) + printf("%s, ", f->f_un.f_uname[i]); + break; + } + if(f->f_program) { + printf(" (%s)", f->f_program); + } + printf("\n"); + } + } + + logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", LocalHostName, ADDDATE); + dprintf("syslogd: restarted\n"); +} + +/* + * Crack a configuration file line + */ +void +cfline(line, f, prog) + char *line; + struct filed *f; + char *prog; +{ + struct hostent *hp; + int i, pri; + char *bp, *p, *q; + char buf[MAXLINE], ebuf[100]; + + dprintf("cfline(\"%s\", f, \"%s\")\n", line, prog); + + errno = 0; /* keep strerror() stuff out of logerror messages */ + + /* clear out file entry */ + memset(f, 0, sizeof(*f)); + for (i = 0; i <= LOG_NFACILITIES; i++) + f->f_pmask[i] = INTERNAL_NOPRI; + + /* save program name if any */ + if(prog && *prog=='*') prog = NULL; + if(prog) { + f->f_program = calloc(1, strlen(prog)+1); + if(f->f_program) { + strcpy(f->f_program, prog); + } + } + + /* scan through the list of selectors */ + for (p = line; *p && *p != '\t';) { + + /* find the end of this facility name list */ + for (q = p; *q && *q != '\t' && *q++ != '.'; ) + continue; + + /* collect priority name */ + for (bp = buf; *q && !strchr("\t,;", *q); ) + *bp++ = *q++; + *bp = '\0'; + + /* skip cruft */ + while (strchr(", ;", *q)) + q++; + + /* decode priority name */ + if (*buf == '*') + pri = LOG_PRIMASK + 1; + else { + pri = decode(buf, prioritynames); + if (pri < 0) { + (void)sprintf(ebuf, + "unknown priority name \"%s\"", buf); + logerror(ebuf); + return; + } + } + + /* scan facilities */ + while (*p && !strchr("\t.;", *p)) { + for (bp = buf; *p && !strchr("\t,;.", *p); ) + *bp++ = *p++; + *bp = '\0'; + if (*buf == '*') + for (i = 0; i < LOG_NFACILITIES; i++) + f->f_pmask[i] = pri; + else { + i = decode(buf, facilitynames); + if (i < 0) { + (void)sprintf(ebuf, + "unknown facility name \"%s\"", + buf); + logerror(ebuf); + return; + } + f->f_pmask[i >> 3] = pri; + } + while (*p == ',' || *p == ' ') + p++; + } + + p = q; + } + + /* skip to action part */ + while (*p == '\t') + p++; + + switch (*p) + { + case '@': + if (!InetInuse) + break; + (void)strcpy(f->f_un.f_forw.f_hname, ++p); + hp = gethostbyname(p); + if (hp == NULL) { + extern int h_errno; + + logerror(hstrerror(h_errno)); + break; + } + memset(&f->f_un.f_forw.f_addr, 0, + sizeof(f->f_un.f_forw.f_addr)); + f->f_un.f_forw.f_addr.sin_family = AF_INET; + f->f_un.f_forw.f_addr.sin_port = LogPort; + memmove(&f->f_un.f_forw.f_addr.sin_addr, hp->h_addr, hp->h_length); + f->f_type = F_FORW; + break; + + case '/': + (void)strcpy(f->f_un.f_fname, p); + if ((f->f_file = open(p, O_WRONLY|O_APPEND, 0)) < 0) { + f->f_file = F_UNUSED; + logerror(p); + break; + } + if (isatty(f->f_file)) + f->f_type = F_TTY; + else + f->f_type = F_FILE; + if (strcmp(p, ctty) == 0) + f->f_type = F_CONSOLE; + break; + + case '*': + f->f_type = F_WALL; + break; + + default: + for (i = 0; i < MAXUNAMES && *p; i++) { + for (q = p; *q && *q != ','; ) + q++; + (void)strncpy(f->f_un.f_uname[i], p, UT_NAMESIZE); + if ((q - p) > UT_NAMESIZE) + f->f_un.f_uname[i][UT_NAMESIZE] = '\0'; + else + f->f_un.f_uname[i][q - p] = '\0'; + while (*q == ',' || *q == ' ') + q++; + p = q; + } + f->f_type = F_USERS; + break; + } +} + + +/* + * Decode a symbolic name to a numeric value + */ +int +decode(name, codetab) + const char *name; + CODE *codetab; +{ + CODE *c; + char *p, buf[40]; + + if (isdigit(*name)) + return (atoi(name)); + + for (p = buf; *name && p < &buf[sizeof(buf) - 1]; p++, name++) { + if (isupper(*name)) + *p = tolower(*name); + else + *p = *name; + } + *p = '\0'; + for (c = codetab; c->c_name; c++) + if (!strcmp(buf, c->c_name)) + return (c->c_val); + + return (-1); +}