=/^G show total bytes now; document may not always work
[unix-history] / usr / src / sbin / savecore / savecore.c
index 4e1d79c..71292a3 100644 (file)
@@ -1,18 +1,29 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1980, 1986 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+"@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)savecore.c 5.7 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)savecore.c 5.14 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * savecore
 
 /*
  * savecore
@@ -25,7 +36,7 @@ static char sccsid[] = "@(#)savecore.c        5.7 (Berkeley) %G%";
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/file.h>
-#include <syslog.h>
+#include <sys/syslog.h>
 
 #define        DAY     (60L*60L*24L)
 #define        LEEWAY  (3*DAY)
 
 #define        DAY     (60L*60L*24L)
 #define        LEEWAY  (3*DAY)
@@ -34,8 +45,12 @@ static char sccsid[] = "@(#)savecore.c       5.7 (Berkeley) %G%";
 #ifdef vax
 #define ok(number) ((number)&0x7fffffff)
 #else
 #ifdef vax
 #define ok(number) ((number)&0x7fffffff)
 #else
+#ifdef tahoe
+#define ok(number) ((number)&~0xc0000000)
+#else
 #define ok(number) (number)
 #endif
 #define ok(number) (number)
 #endif
+#endif
 
 struct nlist current_nl[] = {  /* namelist for currently running system */
 #define X_DUMPDEV      0
 
 struct nlist current_nl[] = {  /* namelist for currently running system */
 #define X_DUMPDEV      0
@@ -86,6 +101,9 @@ int  panicstr;
 off_t  lseek();
 off_t  Lseek();
 int    Verbose;
 off_t  lseek();
 off_t  Lseek();
 int    Verbose;
+int    force;
+int    clear;
+extern int errno;
 
 main(argc, argv)
        char **argv;
 
 main(argc, argv)
        char **argv;
@@ -97,14 +115,22 @@ main(argc, argv)
        while (argc > 0 && argv[0][0] == '-') {
                for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
 
        while (argc > 0 && argv[0][0] == '-') {
                for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
 
+               case 'f':
+                       force++;
+                       break;
+
                case 'v':
                        Verbose++;
                        break;
 
                case 'v':
                        Verbose++;
                        break;
 
+               case 'c':
+                       clear++;
+                       break;
+
                default:
                usage:
                        fprintf(stderr,
                default:
                usage:
                        fprintf(stderr,
-                           "usage: savecore [-v] dirname [ system ]\n");
+                           "usage: savecore [-f] [-v] dirname [ system ]\n");
                        exit(1);
                }
                argc--, argv++;
                        exit(1);
                }
                argc--, argv++;
@@ -116,7 +142,7 @@ main(argc, argv)
                system = argv[1];
        openlog("savecore", LOG_ODELAY, LOG_AUTH);
        if (access(dirname, W_OK) < 0) {
                system = argv[1];
        openlog("savecore", LOG_ODELAY, LOG_AUTH);
        if (access(dirname, W_OK) < 0) {
-               syslog(LOG_ERR, "%s: %m", dirname);
+               Perror(LOG_ERR, "%s: %m", dirname);
                exit(1);
        }
        read_kmem();
                exit(1);
        }
        read_kmem();
@@ -170,7 +196,7 @@ find_dev(dev, type)
                        return (dp);
                }
        }
                        return (dp);
                }
        }
-       syslog(LOG_ERR, "Can't find device %d/%d\n", major(dev), minor(dev));
+       log(LOG_ERR, "Can't find device %d/%d\n", major(dev), minor(dev));
        exit(1);
        /*NOTREACHED*/
 }
        exit(1);
        /*NOTREACHED*/
 }
@@ -199,13 +225,13 @@ read_kmem()
         */
        for (i = 0; cursyms[i] != -1; i++)
                if (current_nl[cursyms[i]].n_value == 0) {
         */
        for (i = 0; cursyms[i] != -1; i++)
                if (current_nl[cursyms[i]].n_value == 0) {
-                       syslog(LOG_ERR, "/vmunix: %s not in namelist",
+                       log(LOG_ERR, "/vmunix: %s not in namelist\n",
                            current_nl[cursyms[i]].n_name);
                        exit(1);
                }
        for (i = 0; dumpsyms[i] != -1; i++)
                if (dump_nl[dumpsyms[i]].n_value == 0) {
                            current_nl[cursyms[i]].n_name);
                        exit(1);
                }
        for (i = 0; dumpsyms[i] != -1; i++)
                if (dump_nl[dumpsyms[i]].n_value == 0) {
-                       syslog(LOG_ERR, "%s: %s not in namelist", dump_sys,
+                       log(LOG_ERR, "%s: %s not in namelist\n", dump_sys,
                            dump_nl[dumpsyms[i]].n_name);
                        exit(1);
                }
                            dump_nl[dumpsyms[i]].n_name);
                        exit(1);
                }
@@ -220,7 +246,7 @@ read_kmem()
        ddname = find_dev(dumpdev, S_IFBLK);
        fp = fdopen(kmem, "r");
        if (fp == NULL) {
        ddname = find_dev(dumpdev, S_IFBLK);
        fp = fdopen(kmem, "r");
        if (fp == NULL) {
-               syslog(LOG_ERR, "Couldn't fdopen kmem");
+               log(LOG_ERR, "Couldn't fdopen kmem\n");
                exit(1);
        }
        if (system)
                exit(1);
        }
        if (system)
@@ -237,16 +263,17 @@ check_kmem()
 
        fp = fopen(ddname, "r");
        if (fp == NULL) {
 
        fp = fopen(ddname, "r");
        if (fp == NULL) {
-               syslog(LOG_ERR, "%s: %m", ddname);
+               Perror(LOG_ERR, "%s: %m", ddname);
                exit(1);
        }
        fseek(fp, (off_t)(dumplo+ok(dump_nl[X_VERSION].n_value)), L_SET);
        fgets(core_vers, sizeof (core_vers), fp);
        fclose(fp);
                exit(1);
        }
        fseek(fp, (off_t)(dumplo+ok(dump_nl[X_VERSION].n_value)), L_SET);
        fgets(core_vers, sizeof (core_vers), fp);
        fclose(fp);
-       if (!eq(vers, core_vers) && system == 0)
-               fprintf(stderr,
-                  "Warning: vmunix version mismatch:\n\t%sand\n\t%s",
-                  vers, core_vers);
+       if (!eq(vers, core_vers) && system == 0) {
+               log(LOG_WARNING, "Warning: vmunix version mismatch:\n");
+               log(LOG_WARNING, "\t%s\n", vers);
+               log(LOG_WARNING, "and\t%s\n", core_vers);
+       }
        fp = fopen(ddname, "r");
        fseek(fp, (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
        fread((char *)&panicstr, sizeof (panicstr), 1, fp);
        fp = fopen(ddname, "r");
        fseek(fp, (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
        fread((char *)&panicstr, sizeof (panicstr), 1, fp);
@@ -271,7 +298,7 @@ get_crashtime()
        close(dumpfd);
        if (dumptime == 0) {
                if (Verbose)
        close(dumpfd);
        if (dumptime == 0) {
                if (Verbose)
-                       printf("Dump time not found.\n");
+                       printf("Dump time is zero.\n");
                return (0);
        }
        printf("System went down at %s", ctime(&dumptime));
                return (0);
        }
        printf("System went down at %s", ctime(&dumptime));
@@ -302,22 +329,22 @@ check_space()
        struct fs fs;
 
        if (stat(dirname, &dsb) < 0) {
        struct fs fs;
 
        if (stat(dirname, &dsb) < 0) {
-               syslog(LOG_ERR, "%s: %m", dirname);
+               Perror(LOG_ERR, "%s: %m", dirname);
                exit(1);
        }
        ddev = find_dev(dsb.st_dev, S_IFBLK);
        dfd = Open(ddev, O_RDONLY);
                exit(1);
        }
        ddev = find_dev(dsb.st_dev, S_IFBLK);
        dfd = Open(ddev, O_RDONLY);
-       Lseek(dfd, (long)(SBLOCK * DEV_BSIZE), L_SET);
+       Lseek(dfd, SBOFF, L_SET);
        Read(dfd, (char *)&fs, sizeof (fs));
        close(dfd);
        spacefree = freespace(&fs, fs.fs_minfree) * fs.fs_fsize / 1024;
        if (spacefree < read_number("minfree")) {
        Read(dfd, (char *)&fs, sizeof (fs));
        close(dfd);
        spacefree = freespace(&fs, fs.fs_minfree) * fs.fs_fsize / 1024;
        if (spacefree < read_number("minfree")) {
-               syslog(LOG_WARNING, "Dump omitted, not enough space on device");
+               log(LOG_WARNING, "Dump omitted, not enough space on device\n");
                return (0);
        }
        if (freespace(&fs, fs.fs_minfree) < 0)
                return (0);
        }
        if (freespace(&fs, fs.fs_minfree) < 0)
-               syslog(LOG_WARNING,
-                   "Dump performed, but free space threshold crossed");
+               log(LOG_WARNING,
+                   "Dump performed, but free space threshold crossed\n");
        return (1);
 }
 
        return (1);
 }
 
@@ -361,16 +388,16 @@ save_core()
        ifd = Open(ddname, O_RDONLY);
        Lseek(ifd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
        Read(ifd, (char *)&dumpsize, sizeof (dumpsize));
        ifd = Open(ddname, O_RDONLY);
        Lseek(ifd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
        Read(ifd, (char *)&dumpsize, sizeof (dumpsize));
-       sprintf(cp, "vmcore.%d", bounds);
+       (void)sprintf(cp, "vmcore.%d", bounds);
        ofd = Create(path(cp), 0644);
        Lseek(ifd, (off_t)dumplo, L_SET);
        ofd = Create(path(cp), 0644);
        Lseek(ifd, (off_t)dumplo, L_SET);
-       printf("Saving %d bytes of image in vmcore.%d\n", NBPG*dumpsize,
-               bounds);
+       log(LOG_NOTICE, "Saving %d bytes of image in vmcore.%d\n",
+           NBPG*dumpsize, bounds);
        while (dumpsize > 0) {
                n = Read(ifd, cp,
                    (dumpsize > BUFPAGES ? BUFPAGES : dumpsize) * NBPG);
                if (n == 0) {
        while (dumpsize > 0) {
                n = Read(ifd, cp,
                    (dumpsize > BUFPAGES ? BUFPAGES : dumpsize) * NBPG);
                if (n == 0) {
-                       printf("WARNING: core may be incomplete\n");
+                       log(LOG_WARNING, "WARNING: vmcore may be incomplete\n");
                        break;
                }
                Write(ofd, cp, n);
                        break;
                }
                Write(ofd, cp, n);
@@ -395,7 +422,7 @@ Open(name, rw)
 
        fd = open(name, rw);
        if (fd < 0) {
 
        fd = open(name, rw);
        if (fd < 0) {
-               syslog(LOG_ERR, "%s: %m", name);
+               Perror(LOG_ERR, "%s: %m", name);
                exit(1);
        }
        return (fd);
                exit(1);
        }
        return (fd);
@@ -409,7 +436,7 @@ Read(fd, buff, size)
 
        ret = read(fd, buff, size);
        if (ret < 0) {
 
        ret = read(fd, buff, size);
        if (ret < 0) {
-               syslog(LOG_ERR, "read: %m");
+               Perror(LOG_ERR, "read: %m");
                exit(1);
        }
        return (ret);
                exit(1);
        }
        return (ret);
@@ -424,7 +451,7 @@ Lseek(fd, off, flag)
 
        ret = lseek(fd, off, flag);
        if (ret == -1) {
 
        ret = lseek(fd, off, flag);
        if (ret == -1) {
-               syslog(LOG_ERR, "lseek: %m");
+               Perror(LOG_ERR, "lseek: %m");
                exit(1);
        }
        return (ret);
                exit(1);
        }
        return (ret);
@@ -438,7 +465,7 @@ Create(file, mode)
 
        fd = creat(file, mode);
        if (fd < 0) {
 
        fd = creat(file, mode);
        if (fd < 0) {
-               syslog(LOG_ERR, "%s: %m", file);
+               Perror(LOG_ERR, "%s: %m", file);
                exit(1);
        }
        return (fd);
                exit(1);
        }
        return (fd);
@@ -450,7 +477,27 @@ Write(fd, buf, size)
 {
 
        if (write(fd, buf, size) < size) {
 {
 
        if (write(fd, buf, size) < size) {
-               syslog(LOG_ERR, "write: %m");
+               Perror(LOG_ERR, "write: %m");
                exit(1);
        }
 }
                exit(1);
        }
 }
+
+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;
+{
+       int oerrno = errno;
+       
+       perror(s);
+       errno = oerrno;
+       syslog(level, msg, s);
+}