BSD 4_3_Net_2 release
[unix-history] / usr / src / sbin / savecore / savecore.c
index a34f33c..56a5792 100644 (file)
@@ -2,7 +2,33 @@
  * Copyright (c) 1980, 1986, 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980, 1986, 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * 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
  */
 
 #ifndef lint
@@ -12,7 +38,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)savecore.c 5.26 (Berkeley) %G%";
+static char sccsid[] = "@(#)savecore.c 5.26 (Berkeley) 4/8/91";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -138,6 +164,31 @@ main(argc, argv)
        openlog("savecore", LOG_ODELAY, LOG_AUTH);
 
        read_kmem();
        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()
 }
 
 dump_exists()
@@ -164,10 +215,15 @@ find_dev(dev, type)
        register dev_t dev;
        register int type;
 {
        register dev_t dev;
        register int type;
 {
+       register DIR *dfd = opendir(_PATH_DEV);
+       struct dirent *dir;
        struct stat statb;
        struct stat statb;
+       static char devname[MAXPATHLEN + 1];
        char *dp;
 
        strcpy(devname, _PATH_DEV);
        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 (stat(devname, &statb)) {
                        perror(devname);
                        continue;
@@ -175,11 +231,13 @@ find_dev(dev, type)
                if ((statb.st_mode&S_IFMT) != type)
                        continue;
                if (dev == statb.st_rdev) {
                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);
                }
        }
                        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*/
        log(LOG_ERR, "Can't find device %d/%d\n", major(dev), minor(dev));
        exit(1);
        /*NOTREACHED*/
@@ -375,6 +433,8 @@ save_core()
        }
        bounds = read_number("bounds");
        ifd = Open(system ? system : _PATH_UNIX, O_RDONLY);
        }
        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);
        while((n = Read(ifd, cp, BUFSIZE)) > 0)
                Write(ofd, cp, n);
        close(ifd);