386BSD 0.0 development
authorWilliam F. Jolitz <wjolitz@soda.berkeley.edu>
Wed, 17 Apr 1991 00:34:40 +0000 (16:34 -0800)
committerWilliam F. Jolitz <wjolitz@soda.berkeley.edu>
Wed, 17 Apr 1991 00:34:40 +0000 (16:34 -0800)
Work on file usr/src/sbin/savecore/savecore.8
Work on file usr/src/sbin/savecore/savecore.c
Work on file usr/src/sbin/shutdown/pathnames.h
Work on file usr/src/sbin/shutdown/shutdown.8
Work on file usr/src/sbin/shutdown/shutdown.c
Work on file usr/src/sbin/slattach/slattach.c
Work on file usr/src/sbin/slattach/slattach.8

Co-Authored-By: Lynne Greer Jolitz <ljolitz@cardio.ucsf.edu>
Synthesized-from: 386BSD-0.0/src

usr/src/sbin/savecore/savecore.8 [new file with mode: 0644]
usr/src/sbin/savecore/savecore.c [new file with mode: 0644]
usr/src/sbin/shutdown/pathnames.h [new file with mode: 0644]
usr/src/sbin/shutdown/shutdown.8 [new file with mode: 0644]
usr/src/sbin/shutdown/shutdown.c [new file with mode: 0644]
usr/src/sbin/slattach/slattach.8 [new file with mode: 0644]
usr/src/sbin/slattach/slattach.c [new file with mode: 0644]

diff --git a/usr/src/sbin/savecore/savecore.8 b/usr/src/sbin/savecore/savecore.8
new file mode 100644 (file)
index 0000000..9bd3705
--- /dev/null
@@ -0,0 +1,109 @@
+.\" Copyright (c) 1980, 1991 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.
+.\"
+.\"     @(#)savecore.8 6.6 (Berkeley) 3/16/91
+.\"
+.Dd March 16, 1991
+.Dt SAVECORE 8
+.Os BSD 4
+.Sh NAME
+.Nm savecore
+.Nd "save a core dump of the operating system"
+.Sh SYNOPSIS
+.Nm savecore
+.Ar dirname
+.Op Ar system
+.Sh DESCRIPTION
+.Nm Savecore
+is meant to be called near the end of the initialization file
+.Pa /etc/rc
+(see
+.Xr rc 8 ) .
+Its function
+is to save the core dump of the system (assuming one was made) and to
+write a reboot message in the shutdown log.
+.Pp
+Operands supported by
+.Nm savecore :
+.Bl -tag -width Ar
+.It Ar dirname
+The directory name where the core dump will be written.
+.It Ar system
+If the core dump was from a system other than
+.Pa /vmunix ,
+the name
+of that system must be supplied as
+.Ar system .
+.El
+.Pp
+.Nm Savecore
+checks the core dump to be certain it corresponds with the
+current running unix.  If the core dump is current, it saves the core
+image in
+.Ar dirname Ns Pa /vmcore.n
+and the namelist
+in
+.Ar dirname Ns Pa /vmunix.n
+Sequence numbers are incremented and appended to the end of the pathnames
+each time
+.Nm savecore
+is run in the directory.
+.Pp
+Before savecore writes out a core image, it reads a number from the file
+.Ar dirname Ns Pa /minfree .
+If the number of free kilobytes on the filesystem which contains
+.Ar dirname
+is less than the number obtained from the minfree file,
+the core dump is not saved.
+If the minfree file does not exist, savecore always writes out the core
+file (assuming that a core dump was taken).
+.Pp
+.Nm Savecore
+also logs a reboot message using facility
+.Dv LOG_AUTH
+(see
+.Xr syslog 3 )
+If the system crashed as a result of a panic,
+.Nm savecore
+logs the panic string too.
+.Sh FILES
+.Bl -tag -width /vmunixxx -compact
+.It Pa /vmunix
+current
+.Tn UNIX
+.El
+.Sh BUGS
+Can be fooled into thinking a core dump is the wrong size.
+.Sh HISTORY
+The
+.Nm
+command appeared in
+.Bx 4.1 .
diff --git a/usr/src/sbin/savecore/savecore.c b/usr/src/sbin/savecore/savecore.c
new file mode 100644 (file)
index 0000000..56a5792
--- /dev/null
@@ -0,0 +1,591 @@
+/*
+ * Copyright (c) 1980, 1986, 1989 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
+char copyright[] =
+"@(#) Copyright (c) 1980, 1986, 1989 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)savecore.c 5.26 (Berkeley) 4/8/91";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/file.h>
+#include <sys/syslog.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <nlist.h>
+#include <paths.h>
+
+#define        DAY     (60L*60L*24L)
+#define        LEEWAY  (3*DAY)
+
+#define eq(a,b) (!strcmp(a,b))
+#ifdef vax
+#define ok(number) ((number)&0x7fffffff)
+#else
+#ifdef tahoe
+#define ok(number) ((number)&~0xc0000000)
+#else
+#ifdef i386
+#define ok(number) ((number)&~0xfe000000)
+#else
+#define ok(number) (number)
+#endif
+#endif
+#endif
+
+struct nlist current_nl[] = {  /* namelist for currently running system */
+#define X_DUMPDEV      0
+       { "_dumpdev" },
+#define X_DUMPLO       1
+       { "_dumplo" },
+#define X_TIME         2
+       { "_time" },
+#define        X_DUMPSIZE      3
+       { "_dumpsize" },
+#define X_VERSION      4
+       { "_version" },
+#define X_PANICSTR     5
+       { "_panicstr" },
+#define        X_DUMPMAG       6
+       { "_dumpmag" },
+       { "" },
+};
+
+struct nlist dump_nl[] = {     /* name list for dumped system */
+       { "_dumpdev" },         /* entries MUST be the same as */
+       { "_dumplo" },          /*      those in current_nl[]  */
+       { "_time" },
+       { "_dumpsize" },
+       { "_version" },
+       { "_panicstr" },
+       { "_dumpmag" },
+       { "" },
+};
+
+char   *system;
+char   *dirname;                       /* directory to save dumps in */
+char   *ddname;                        /* name of dump device */
+int    dumpfd;                         /* read/write descriptor on block dev */
+char   *find_dev();
+dev_t  dumpdev;                        /* dump device */
+time_t dumptime;                       /* time the dump was taken */
+int    dumplo;                         /* where dump starts on dumpdev */
+int    dumpsize;                       /* amount of memory dumped */
+int    dumpmag;                        /* magic number in dump */
+time_t now;                            /* current date */
+char   *path();
+char   *malloc();
+char   *ctime();
+char   vers[80];
+char   core_vers[80];
+char   panic_mesg[80];
+int    panicstr;
+off_t  lseek();
+off_t  Lseek();
+int    verbose;
+int    force;
+int    clear;
+extern int errno;
+
+main(argc, argv)
+       char **argv;
+       int argc;
+{
+       extern char *optarg;
+       extern int optind;
+       int ch;
+       char *cp;
+
+       while ((ch = getopt(argc, argv, "cdfv")) != EOF)
+               switch(ch) {
+               case 'c':
+                       clear = 1;
+                       break;
+               case 'd':               /* not documented */
+               case 'v':
+                       verbose = 1;
+                       break;
+               case 'f':
+                       force = 1;
+                       break;
+               case '?':
+               default:
+                       usage();
+               }
+       argc -= optind;
+       argv += optind;
+
+       /* This is wrong, but I want "savecore -c" to work. */
+       if (!clear) {
+               if (argc != 1 && argc != 2)
+                       usage();
+               dirname = argv[0];
+       }
+       if (argc == 2)
+               system = argv[1];
+
+       openlog("savecore", LOG_ODELAY, LOG_AUTH);
+
+       read_kmem();
+       if (!dump_exists()) {
+               (void)fprintf(stderr, "savecore: no core dump\n");
+               if (!force)
+                       exit(0);
+       }
+       if (clear) {
+               clear_dump();
+               exit(0);
+       }
+       (void) time(&now);
+       check_kmem();
+       if (panicstr)
+               log(LOG_CRIT, "reboot after panic: %s\n", panic_mesg);
+       else
+               syslog(LOG_CRIT, "reboot\n");
+
+       if (access(dirname, W_OK) < 0) {
+               Perror(LOG_ERR, "%s: %m\n", dirname);
+               exit(1);
+       }
+       if ((!get_crashtime() || !check_space()) && !force)
+               exit(1);
+       save_core();
+       clear_dump();
+       exit(0);
+}
+
+dump_exists()
+{
+       int word;
+
+       Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
+       Read(dumpfd, (char *)&word, sizeof (word));
+       if (verbose && word != dumpmag)
+               printf("magic number mismatch: %x != %x\n", word, dumpmag);
+       return (word == dumpmag);
+}
+
+clear_dump()
+{
+       int zero = 0;
+
+       Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
+       Write(dumpfd, (char *)&zero, sizeof (zero));
+}
+
+char *
+find_dev(dev, type)
+       register dev_t dev;
+       register int type;
+{
+       register DIR *dfd = opendir(_PATH_DEV);
+       struct dirent *dir;
+       struct stat statb;
+       static char devname[MAXPATHLEN + 1];
+       char *dp;
+
+       strcpy(devname, _PATH_DEV);
+       while ((dir = readdir(dfd))) {
+               strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
+               if (stat(devname, &statb)) {
+                       perror(devname);
+                       continue;
+               }
+               if ((statb.st_mode&S_IFMT) != type)
+                       continue;
+               if (dev == statb.st_rdev) {
+                       closedir(dfd);
+                       dp = malloc(strlen(devname)+1);
+                       strcpy(dp, devname);
+                       return (dp);
+               }
+       }
+       closedir(dfd);
+       log(LOG_ERR, "Can't find device %d/%d\n", major(dev), minor(dev));
+       exit(1);
+       /*NOTREACHED*/
+}
+
+char *
+rawname(s)
+       char *s;
+{
+       static char name[MAXPATHLEN];
+       char *sl, *rindex();
+
+       if ((sl = rindex(s, '/')) == NULL || sl[1] == '0') {
+               log(LOG_ERR, "can't make raw dump device name from %s?\n", s);
+               return (s);
+       }
+       sprintf(name, "%.*s/r%s", sl - s, s, sl + 1);
+       return (name);
+}
+
+int    cursyms[] =
+    { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
+int    dumpsyms[] =
+    { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
+read_kmem()
+{
+       register char *cp;
+       FILE *fp;
+       char *dump_sys;
+       int kmem, i;
+       
+       dump_sys = system ? system : _PATH_UNIX;
+       nlist(_PATH_UNIX, current_nl);
+       nlist(dump_sys, dump_nl);
+       /*
+        * Some names we need for the currently running system,
+        * others for the system that was running when the dump was made.
+        * The values obtained from the current system are used
+        * to look for things in /dev/kmem that cannot be found
+        * in the dump_sys namelist, but are presumed to be the same
+        * (since the disk partitions are probably the same!)
+        */
+       for (i = 0; cursyms[i] != -1; i++)
+               if (current_nl[cursyms[i]].n_value == 0) {
+                       log(LOG_ERR, "%s: %s not in namelist\n", _PATH_UNIX,
+                           current_nl[cursyms[i]].n_name);
+                       exit(1);
+               }
+       for (i = 0; dumpsyms[i] != -1; i++)
+               if (dump_nl[dumpsyms[i]].n_value == 0) {
+                       log(LOG_ERR, "%s: %s not in namelist\n", dump_sys,
+                           dump_nl[dumpsyms[i]].n_name);
+                       exit(1);
+               }
+       kmem = Open(_PATH_KMEM, O_RDONLY);
+       Lseek(kmem, (long)current_nl[X_DUMPDEV].n_value, L_SET);
+       Read(kmem, (char *)&dumpdev, sizeof (dumpdev));
+       Lseek(kmem, (long)current_nl[X_DUMPLO].n_value, L_SET);
+       Read(kmem, (char *)&dumplo, sizeof (dumplo));
+       if (verbose)
+               printf("dumplo = %d (%d * %d)\n", dumplo, dumplo/DEV_BSIZE,
+                   DEV_BSIZE);
+       Lseek(kmem, (long)current_nl[X_DUMPMAG].n_value, L_SET);
+       Read(kmem, (char *)&dumpmag, sizeof (dumpmag));
+       dumplo *= DEV_BSIZE;
+       ddname = find_dev(dumpdev, S_IFBLK);
+       dumpfd = Open(ddname, O_RDWR);
+       fp = fdopen(kmem, "r");
+       if (fp == NULL) {
+               log(LOG_ERR, "Couldn't fdopen kmem\n");
+               exit(1);
+       }
+       if (system)
+               return;
+       fseek(fp, (long)current_nl[X_VERSION].n_value, L_SET);
+       fgets(vers, sizeof (vers), fp);
+       fclose(fp);
+}
+
+check_kmem()
+{
+       FILE *fp;
+       register char *cp;
+
+       fp = fdopen(dumpfd, "r");
+       if (fp == NULL) {
+               log(LOG_ERR, "Can't fdopen dumpfd\n");
+               exit(1);
+       }
+       fseek(fp, (off_t)(dumplo+ok(dump_nl[X_VERSION].n_value)), L_SET);
+       fgets(core_vers, sizeof (core_vers), fp);
+       if (!eq(vers, core_vers) && system == 0) {
+               log(LOG_WARNING, "Warning: %s version mismatch:\n", _PATH_UNIX);
+               log(LOG_WARNING, "\t%s\n", vers);
+               log(LOG_WARNING, "and\t%s\n", core_vers);
+       }
+       fseek(fp, (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
+       fread((char *)&panicstr, sizeof (panicstr), 1, fp);
+       if (panicstr) {
+               fseek(fp, dumplo + ok(panicstr), L_SET);
+               cp = panic_mesg;
+               do
+                       *cp = getc(fp);
+               while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]);
+       }
+       /* don't fclose(fp); we want the file descriptor */
+}
+
+get_crashtime()
+{
+       time_t clobber = (time_t)0;
+
+       Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
+       Read(dumpfd, (char *)&dumptime, sizeof dumptime);
+       if (dumptime == 0) {
+               if (verbose)
+                       printf("Dump time is zero.\n");
+               return (0);
+       }
+       printf("System went down at %s", ctime(&dumptime));
+       if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
+               printf("dump time is unreasonable\n");
+               return (0);
+       }
+       return (1);
+}
+
+char *
+path(file)
+       char *file;
+{
+       register char *cp = malloc(strlen(file) + strlen(dirname) + 2);
+
+       (void) strcpy(cp, dirname);
+       (void) strcat(cp, "/");
+       (void) strcat(cp, file);
+       return (cp);
+}
+
+check_space()
+{
+       long minfree, spacefree;
+       struct statfs fsbuf;
+
+       if (statfs(dirname, &fsbuf) < 0) {
+               Perror(LOG_ERR, "%s: %m\n", dirname);
+               exit(1);
+       }
+       spacefree = fsbuf.f_bavail * fsbuf.f_fsize / 1024;
+       minfree = read_number("minfree");
+       if (minfree > 0 && spacefree - dumpsize < minfree) {
+               log(LOG_WARNING, "Dump omitted, not enough space on device\n");
+               return (0);
+       }
+       if (spacefree - dumpsize < minfree)
+               log(LOG_WARNING,
+                   "Dump performed, but free space threshold crossed\n");
+       return (1);
+}
+
+read_number(fn)
+       char *fn;
+{
+       char lin[80];
+       register FILE *fp;
+
+       fp = fopen(path(fn), "r");
+       if (fp == NULL)
+               return (0);
+       if (fgets(lin, 80, fp) == NULL) {
+               fclose(fp);
+               return (0);
+       }
+       fclose(fp);
+       return (atoi(lin));
+}
+
+#define        BUFSIZE         (256*1024)              /* 1/4 Mb */
+
+save_core()
+{
+       register int n;
+       register char *cp;
+       register int ifd, ofd, bounds;
+       int ret;
+       char *bfile;
+       register FILE *fp;
+
+       cp = malloc(BUFSIZE);
+       if (cp == 0) {
+               log(LOG_ERR, "savecore: Can't allocate i/o buffer.\n");
+               return;
+       }
+       bounds = read_number("bounds");
+       ifd = Open(system ? system : _PATH_UNIX, O_RDONLY);
+       (void)sprintf(cp, "vmunix.%d", bounds);
+       ofd = Create(path(cp), 0644);
+       while((n = Read(ifd, cp, BUFSIZE)) > 0)
+               Write(ofd, cp, n);
+       close(ifd);
+       close(ofd);
+       if ((ifd = open(rawname(ddname), O_RDONLY)) == -1) {
+               log(LOG_WARNING, "Can't open %s (%m); using block device",
+                       rawname(ddname));
+               ifd = dumpfd;
+       }
+       Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
+       Read(dumpfd, (char *)&dumpsize, sizeof (dumpsize));
+       (void)sprintf(cp, "vmcore.%d", bounds);
+       ofd = Create(path(cp), 0644);
+       Lseek(ifd, (off_t)dumplo, L_SET);
+       dumpsize *= NBPG;
+       log(LOG_NOTICE, "Saving %d bytes of image in vmcore.%d\n",
+           dumpsize, bounds);
+       while (dumpsize > 0) {
+               n = read(ifd, cp,
+                   dumpsize > BUFSIZE ? BUFSIZE : dumpsize);
+               if (n <= 0) {
+                       if (n == 0)
+                               log(LOG_WARNING,
+                                   "WARNING: EOF on dump device; %s\n",
+                                   "vmcore may be incomplete");
+                       else
+                               Perror(LOG_ERR, "read from dumpdev: %m",
+                                   "read");
+                       break;
+               }
+               if ((ret = write(ofd, cp, n)) < n) {
+                       if (ret < 0)
+                               Perror(LOG_ERR, "write: %m", "write");
+                       else
+                               log(LOG_ERR, "short write: wrote %d of %d\n",
+                                   ret, n);
+                       log(LOG_WARNING, "WARNING: vmcore may be incomplete\n");
+                       break;
+               }
+               dumpsize -= n;
+       }
+       close(ifd);
+       close(ofd);
+       bfile = path("bounds");
+       fp = fopen(bfile, "w");
+       if (fp) {
+               fprintf(fp, "%d\n", bounds+1);
+               fclose(fp);
+       } else
+               Perror(LOG_ERR, "Can't create bounds file %s: %m", bfile);
+       free(cp);
+}
+
+/*
+ * Versions of std routines that exit on error.
+ */
+Open(name, rw)
+       char *name;
+       int rw;
+{
+       int fd;
+
+       fd = open(name, rw);
+       if (fd < 0) {
+               Perror(LOG_ERR, "%s: %m", name);
+               exit(1);
+       }
+       return (fd);
+}
+
+Read(fd, buff, size)
+       int fd, size;
+       char *buff;
+{
+       int ret;
+
+       ret = read(fd, buff, size);
+       if (ret < 0) {
+               Perror(LOG_ERR, "read: %m", "read");
+               exit(1);
+       }
+       return (ret);
+}
+
+off_t
+Lseek(fd, off, flag)
+       int fd, flag;
+       long off;
+{
+       long ret;
+
+       ret = lseek(fd, off, flag);
+       if (ret == -1) {
+               Perror(LOG_ERR, "lseek: %m", "lseek");
+               exit(1);
+       }
+       return (ret);
+}
+
+Create(file, mode)
+       char *file;
+       int mode;
+{
+       register int fd;
+
+       fd = creat(file, mode);
+       if (fd < 0) {
+               Perror(LOG_ERR, "%s: %m", file);
+               exit(1);
+       }
+       return (fd);
+}
+
+Write(fd, buf, size)
+       int fd, size;
+       char *buf;
+{
+       int n;
+
+       if ((n = write(fd, buf, size)) < size) {
+               if (n < 0)
+                       Perror(LOG_ERR, "write: %m", "write");
+               else
+                       log(LOG_ERR, "short write: wrote %d of %d\n", n, size);
+               exit(1);
+       }
+}
+
+/* VARARGS2 */
+log(level, msg, a1, a2)
+       int level;
+       char *msg;
+{
+
+       fprintf(stderr, msg, a1, a2);
+       syslog(level, msg, a1, a2);
+}
+
+Perror(level, msg, s)
+       int level;
+       char *msg, *s;
+{
+       int oerrno = errno;
+       
+       perror(s);
+       errno = oerrno;
+       syslog(level, msg, s);
+}
+
+usage()
+{
+       (void)fprintf(stderr, "usage: savecore [-cfv] dirname [system]\n");
+       exit(1);
+}
diff --git a/usr/src/sbin/shutdown/pathnames.h b/usr/src/sbin/shutdown/pathnames.h
new file mode 100644 (file)
index 0000000..00d1443
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 1989 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 5.5 (Berkeley) 6/1/90
+ */
+
+#include <paths.h>
+
+#define        _PATH_FASTBOOT  "/fastboot"
+#define        _PATH_HALT      "/sbin/halt"
+#define        _PATH_REBOOT    "/sbin/reboot"
+#define        _PATH_WALL      "/usr/bin/wall"
diff --git a/usr/src/sbin/shutdown/shutdown.8 b/usr/src/sbin/shutdown/shutdown.8
new file mode 100644 (file)
index 0000000..3da1050
--- /dev/null
@@ -0,0 +1,162 @@
+.\" Copyright (c) 1988, 1991 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 6.8 (Berkeley) 3/16/91
+.\"
+.Dd March 16, 1991
+.Dt SHUTDOWN 8
+.Os BSD 4
+.Sh NAME
+.Nm shutdown
+.Nd "close down the system at a given time"
+.Sh SYNOPSIS
+.Nm shutdown
+.Op Fl 
+.Op Fl fhkrn
+.Ar time
+.Op Ar warning-message ...
+.Sh DESCRIPTION
+.Nm 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.
+.Pp
+Available friendlinesses:
+.Bl -tag -width time
+.It Fl f
+.Nm Shutdown
+arranges, in the manner of
+.Xr fastboot 8 ,
+for the file systems
+.Em not to be
+checked on reboot.
+.It Fl h
+The system is halted at the specified
+.Ar time
+when
+.Nm shutdown
+execs
+.Xr halt 8 .
+.It Fl k
+Kick every body off.
+The
+.Fl k
+option
+does not actually halt the system, but leaves the
+system multi-user with logins disabled (for all but super-user).
+.It Fl n
+Prevent the normal
+.Xr sync 2
+before stopping.
+.It Fl r 
+.Nm Shutdown
+execs
+.Xr reboot 8
+at the specified
+.Ar time .
+.It Ar time
+.Ar Time
+is the time at which
+.Nm shutdown
+will bring the system down and
+may be the word
+.Ar now
+(indicating an immediate shutdown) or
+specify a future time in one of two formats:
+.Ar +number ,
+or
+.Ar yymmddhhmm ,
+where the year, month, and day may be defaulted
+to the current system values.  The first form brings the system down in
+.Ar number
+minutes and the second at the absolute time specified.
+.It Ar warning-message
+Any other arguments comprise the warning message that is broadcast
+to users currently logged into the system.
+.It Fl
+If
+.Ql Fl
+is supplied as an option, the warning message is read from the standard
+input.
+.El
+.Pp
+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
+.Pa /etc/nologin
+and copying the
+warning message there.  If this file exists when a user attempts to
+log in,
+.Xr login 1
+prints its contents and exits.  The file is
+removed just before
+.Nm shutdown
+exits.
+.Pp
+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
+.Xr 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
+.Pa /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
+.Bl -tag -width /etc/nologin -compact
+.It Pa /etc/nologin
+tells login not to let anyone log in
+.It Pa /fastboot
+tells
+.Xr rc 8
+not to run fsck when rebooting
+.El
+.Sh SEE ALSO
+.Xr login 1 ,
+.Xr wall 1 ,
+.Xr fastboot 8 ,
+.Xr halt 8 ,
+.Xr 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
+.Nm
+command appeared in
+.Bx 4.0 .
diff --git a/usr/src/sbin/shutdown/shutdown.c b/usr/src/sbin/shutdown/shutdown.c
new file mode 100644 (file)
index 0000000..97dce56
--- /dev/null
@@ -0,0 +1,469 @@
+/*
+ * Copyright (c) 1988, 1990 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
+char copyright[] =
+"@(#) Copyright (c) 1988 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)shutdown.c 5.16 (Berkeley) 2/3/91";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/file.h>
+#include <sys/resource.h>
+#include <sys/syslog.h>
+#include <sys/signal.h>
+#include <setjmp.h>
+#include <tzfile.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <ctype.h>
+#include "pathnames.h"
+
+#ifdef DEBUG
+#undef _PATH_NOLOGIN
+#define        _PATH_NOLOGIN   "./nologin"
+#undef _PATH_FASTBOOT
+#define        _PATH_FASTBOOT  "./fastboot"
+#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,
+}, *tp = tlist;
+#undef H
+#undef M
+#undef S
+
+static time_t offset, shuttime;
+static int dofast, dohalt, doreboot, killflg, mbuflen;
+static char *nosync, *whom, mbuf[BUFSIZ];
+
+main(argc, argv)
+       int argc;
+       char **argv;
+{
+       extern int optind;
+       register char *p, *endp;
+       int arglen, ch, len, readstdin;
+       struct passwd *pw;
+       char *strcat(), *getlogin();
+       uid_t geteuid();
+
+#ifndef DEBUG
+       if (geteuid()) {
+               (void)fprintf(stderr, "shutdown: NOT super-user\n");
+               exit(1);
+       }
+#endif
+       nosync = NULL;
+       readstdin = 0;
+       while ((ch = getopt(argc, argv, "-fhknr")) != EOF)
+               switch (ch) {
+               case '-':
+                       readstdin = 1;
+                       break;
+               case 'f':
+                       dofast = 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 (dofast && 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*/
+}
+
+loop()
+{
+       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)
+                               warn();
+                       (void)sleep(sltime);
+               }
+       }
+       for (;; ++tp) {
+               warn();
+               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;
+
+warn()
+{
+       static int first;
+       static char hostname[MAXHOSTNAMELEN + 1];
+       char wcmd[MAXPATHLEN + 4];
+       FILE *pf;
+       char *ctime();
+       void timeout();
+
+       if (!first++)
+               (void)gethostname(hostname, sizeof(hostname));
+
+       /* undoc -n option to wall suppresses normal wall banner */
+       (void)sprintf(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",
+           tp->timeleft ? "": "FINAL ", whom, hostname);
+
+       if (tp->timeleft > 10*60)
+               (void)fprintf(pf, "System going down at %5.5s\n\n",
+                   ctime(&shuttime) + 11);
+       else if (tp->timeleft > 59)
+               (void)fprintf(pf, "System going down in %d minute%s\n\n",
+                   tp->timeleft / 60, (tp->timeleft > 60) ? "s" : "");
+       else if (tp->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()
+{
+       longjmp(alarmbuf, 1);
+}
+
+die_you_gravy_sucking_pig_dog()
+{
+       void finish();
+
+       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");
+               finish();
+       }
+       if (dofast)
+               doitfast();
+#ifdef DEBUG
+       if (doreboot)
+               (void)printf("reboot");
+       else if (dohalt)
+               (void)printf("halt");
+       if (nosync)
+               (void)printf(" no sync");
+       if (dofast)
+               (void)printf(" no fsck");
+       (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();
+}
+
+#define        ATOI2(p)        (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
+
+getoffset(timearg)
+       register char *timearg;
+{
+       register struct tm *lt;
+       register char *p;
+       time_t now, time();
+
+       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        FSMSG   "fastboot file for fsck\n"
+doitfast()
+{
+       int fastfd;
+
+       if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
+           0664)) >= 0) {
+               (void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
+               (void)close(fastfd);
+       }
+}
+
+#define        NOMSG   "\n\nNO LOGINS: System going down at "
+nolog()
+{
+       int logfd;
+       char *ct, *ctime();
+       void finish();
+
+       (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()
+{
+       (void)unlink(_PATH_NOLOGIN);
+       exit(0);
+}
+
+badtime()
+{
+       (void)fprintf(stderr, "shutdown: bad time format.\n");
+       exit(1);
+}
+
+usage()
+{
+       fprintf(stderr, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");
+       exit(1);
+}
diff --git a/usr/src/sbin/slattach/slattach.8 b/usr/src/sbin/slattach/slattach.8
new file mode 100644 (file)
index 0000000..16c688a
--- /dev/null
@@ -0,0 +1,90 @@
+.\" Copyright (c) 1986, 1991 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.
+.\"
+.\"     @(#)slattach.8 6.4 (Berkeley) 3/16/91
+.\"
+.Dd March 16, 1991
+.Dt SLATTACH 8
+.Os BSD 4.3
+.Sh NAME
+.Nm slattach
+.Nd attach serial lines as network interfaces
+.Sh SYOPNSIS
+.Nm Slattach
+.Ar ttyname Op Ar baudrate
+.Sh DESCRIPTION
+.Nm Slattach
+is used to assign a tty line to a network interface,
+and to define the network source and destination addresses.
+The following operands are supported by
+.Nm slattach :
+.Bl -tag -width Ar
+.It Ar ttyname
+Specifies the name of the tty device.
+.Ar Ttyname
+should be a string of the form
+.Ql ttyXX ,
+or
+.Ql /dev/ttyXX .
+.It Ar baudrate
+Specifies the speed of the connection. If not specified, the
+default of 9600 is used.
+.El
+.Pp
+Only the super-user may attach a network interface.
+.Pp
+To detach the interface, use
+.Dq Li ifconfig interface-name down
+after killing off the
+.Nm slattach
+process.
+.Ar Interface-name
+is the name that is shown by
+.Xr netstat 1
+.Sh EXAMPLES
+.Bd -literal -offset indent -compact
+slattach ttyh8
+slattach /dev/tty01 4800
+.Ed
+.Sh DIAGNOSTICS
+Messages indicating the specified interface does not exit, the
+requested address is unknown, the user is not privileged and
+tried to alter an interface's configuration.
+.Sh SEE ALSO
+.Xr netstat 1 ,
+.Xr netintro 4 ,
+.Xr ifconfig 8 ,
+.Xr rc 8
+.Sh HISTORY
+The
+.Nm
+command appeared in
+.Bx 4.3 .
diff --git a/usr/src/sbin/slattach/slattach.c b/usr/src/sbin/slattach/slattach.c
new file mode 100644 (file)
index 0000000..cd34ccb
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Rick Adams.
+ *
+ * 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
+char copyright[] =
+"@(#) Copyright (c) 1988 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)slattach.c 4.6 (Berkeley) 6/1/90";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sgtty.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <paths.h>
+
+#define DEFAULT_BAUD   9600
+int    slipdisc = SLIPDISC;
+
+char   devname[32];
+char   hostname[MAXHOSTNAMELEN];
+
+main(argc, argv)
+       int argc;
+       char *argv[];
+{
+       register int fd;
+       register char *dev = argv[1];
+       struct sgttyb sgtty;
+       int     speed;
+
+       if (argc < 2 || argc > 3) {
+               fprintf(stderr, "usage: %s ttyname [baudrate]\n", argv[0]);
+               exit(1);
+       }
+       speed = argc == 3 ? findspeed(atoi(argv[2])) : findspeed(DEFAULT_BAUD);
+       if (speed == 0) {
+               fprintf(stderr, "unknown speed %s", argv[2]);
+               exit(1);
+       }
+       if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
+               (void)sprintf(devname, "%s/%s", _PATH_DEV, dev);
+               dev = devname;
+       }
+       if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) {
+               perror(dev);
+               exit(1);
+       }
+       sgtty.sg_flags = RAW | ANYP;
+       sgtty.sg_ispeed = sgtty.sg_ospeed = speed;
+       if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
+               perror("ioctl(TIOCSETP)");
+               exit(1);
+       }
+       if (ioctl(fd, TIOCSETD, &slipdisc) < 0) {
+               perror("ioctl(TIOCSETD)");
+               exit(1);
+       }
+
+       if (fork() > 0)
+               exit(0);
+       for (;;)
+               sigpause(0L);
+}
+
+struct sg_spds {
+       int sp_val, sp_name;
+}       spds[] = {
+#ifdef B50
+       { 50, B50 },
+#endif
+#ifdef B75
+       { 75, B75 },
+#endif
+#ifdef B110
+       { 110, B110 },
+#endif
+#ifdef B150
+       { 150, B150 },
+#endif
+#ifdef B200
+       { 200, B200 },
+#endif
+#ifdef B300
+       { 300, B300 },
+#endif
+#ifdef B600
+       { 600, B600 },
+#endif
+#ifdef B1200
+       { 1200, B1200 },
+#endif
+#ifdef B1800
+       { 1800, B1800 },
+#endif
+#ifdef B2000
+       { 2000, B2000 },
+#endif
+#ifdef B2400
+       { 2400, B2400 },
+#endif
+#ifdef B3600
+       { 3600, B3600 },
+#endif
+#ifdef B4800
+       { 4800, B4800 },
+#endif
+#ifdef B7200
+       { 7200, B7200 },
+#endif
+#ifdef B9600
+       { 9600, B9600 },
+#endif
+#ifdef EXTA
+       { 19200, EXTA },
+#endif
+#ifdef EXTB
+       { 38400, EXTB },
+#endif
+       { 0, 0 }
+};
+
+findspeed(speed)
+       register int speed;
+{
+       register struct sg_spds *sp;
+
+       sp = spds;
+       while (sp->sp_val && sp->sp_val != speed)
+               sp++;
+       return (sp->sp_name);
+}