Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Apr 2002 00:31:34 -0600
From:      Chad David <davidc@acns.ab.ca>
To:        current@freebsd.org
Subject:   savecore
Message-ID:  <20020419003134.A54078@colnta.acns.ab.ca>

next in thread | raw e-mail | index | archive | help

--OXfL5xGRrasGEqWY
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Any comments / objections to these patches to savecore and friends?

After I get more than two or three md5 named files in var/crash I
start to go cross eyed.

-- 
Chad David        davidc@acns.ab.ca
www.FreeBSD.org   davidc@freebsd.org
ACNS Inc.         Calgary, Alberta Canada
Fourthly, The constant breeders, beside the gain of eight shillings
sterling per annum by the sale of their children, will be rid of the
charge of maintaining them after the first year. - Johnathan Swift

--OXfL5xGRrasGEqWY
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="savecore.c.diff"

Index: Makefile
===================================================================
RCS file: /mnt1/ncvs/src/sbin/savecore/Makefile,v
retrieving revision 1.9
diff -u -d -r1.9 Makefile
--- Makefile	1 Apr 2002 08:27:19 -0000	1.9
+++ Makefile	19 Apr 2002 06:21:17 -0000
@@ -1,7 +1,5 @@
 # $FreeBSD: src/sbin/savecore/Makefile,v 1.9 2002/04/01 08:27:19 phk Exp $
 PROG=	savecore
 WARNS=	4
-NOMAN=	sorry, not yet.
-LDADD=	-lmd
 
 .include <bsd.prog.mk>
Index: savecore.8
===================================================================
RCS file: /mnt1/ncvs/src/sbin/savecore/savecore.8,v
retrieving revision 1.15
diff -u -d -r1.15 savecore.8
--- savecore.8	28 Oct 2001 16:41:56 -0000	1.15
+++ savecore.8	19 Apr 2002 06:20:04 -0000
@@ -42,81 +42,70 @@
 .Nm
 .Fl c
 .Nm
-.Op Fl fkvz
-.Op Fl N Ar system
-.Ar directory
+.Op Fl kvf
+.Op directory Op device ...
 .Sh DESCRIPTION
-.Nm Savecore
-copies the currently running kernel and its associated core dump into
-.Fa directory ,
-and enters a reboot message and information about the core dump into
-the system log.
+The
+.Nm savecore
+command saves system dumps from any and all filesystems that are of type
+swap and of type dump to the directory specified on the command line.
+If any optional devices are specified
+.Nm savecore
+will attempt to read dumps from them instead.
 .Pp
-The options are as follows:
+The
+.Nm savecore
+command accepts the following options:
 .Bl -tag -width indent
 .It Fl c
-Clear the dump, so that future invocations of
-.Nm
+Clear the dump's header so that future invocations of
+.Nm savecore
 will ignore it.
+When this option is specified
+.Nm savecore
+does not attempt to save the dump, and all other options are ignored.
 .It Fl f
-Force a dump to be taken even if the dump doesn't appear correct or there
-is insufficient disk space.
+Force a dump to be taken even if it has already been taken.
 .It Fl k
-Do not clear the dump after saving it.
-.It Fl N
-Use
-.Ar system
-as the kernel instead of the running kernel (as determined from
-.Xr getbootfile 3 ) .
+Do not clear the dump header after saving it.
 .It Fl v
-Print out some additional debugging information.
-.It Fl z
-Compress the core dump and kernel (see
-.Xr gzip 1 ) .
+Print additional debugging information, including the details of the dump
+header to stdout.
 .El
 .Pp
-.Nm Savecore
-checks the core dump in various ways to make sure that it is current and
-that it corresponds to the currently running system.
-If it passes these checks, it saves the core image in
+The
+.Nm savecore
+command attempts to verify that a core image is valid by verifying it's
+header (magic number and version etc.).
+If the header passes these checks
+.Nm savecore
+saves the core image in
 .Ar directory Ns Pa /vmcore.#
-and the system in
-.Ar directory Ns Pa /kernel.#
+and the header information in
+.Ar directory Ns Pa /info.# .
 The ``#'' is the number from the first line of the file
 .Ar directory Ns Pa /bounds ,
-and it is incremented and stored back into the file each time
-.Nm
+and is incremented and stored back into the file each time
+.Nm savecore
 successfully runs.
 .Pp
-.Nm Savecore
-also checks the available disk space before attempting to make the copies.
-If there is insufficient disk space in the filesystem containing
-.Ar directory ,
-or if the file
-.Ar directory Ns Pa /minfree
-exists and the number of free kilobytes (for non-superusers) in the
-filesystem after the copies were made would be less than the number
-in the first line of this file, the copies are not attempted.
-.Pp
 If
-.Nm
-successfully copies the kernel and the core dump, the core dump is cleared
-so that future invocations of
-.Nm
+.Nm savecore
+successfully saves the core dump, and the
+.Fl k
+option is not specific, the dump's header is cleared so that future
+invocations of
+.Nm savecore
 will ignore it.
 .Pp
-.Nm Savecore
-is meant to be called near the end of the initialization file
+The
+.Nm savecore
+command is meant to be called from the initialization file
 .Pa /etc/rc
 (see
 .Xr rc 8 ) .
-.Sh BUGS
-The minfree code does not consider the effect of compression.
 .Sh SEE ALSO
-.Xr gzip 1 ,
-.Xr getbootfile 3 ,
 .Xr dumpon 8 ,
-.Xr syslogd 8
 .Sh HISTORY
 The
 .Nm
Index: savecore.c
===================================================================
RCS file: /mnt1/ncvs/src/sbin/savecore/savecore.c,v
retrieving revision 1.55
diff -u -d -r1.55 savecore.c
--- savecore.c	13 Apr 2002 08:20:15 -0000	1.55
+++ savecore.c	19 Apr 2002 06:14:01 -0000
@@ -43,7 +43,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <fstab.h>
-#include <md5.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -55,7 +54,7 @@
 
 static void
 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
-    const char *md5)
+    int bounds)
 {
 	uint64_t dumplen;
 	time_t t;
@@ -73,25 +72,68 @@
 	fprintf(f, "  Hostname: %s\n", h->hostname);
 	fprintf(f, "  Versionstring: %s", h->versionstring);
 	fprintf(f, "  Panicstring: %s\n", h->panicstring);
-	fprintf(f, "  MD5: %s\n", md5);
+	fprintf(f, "  Bounds: %d\n", bounds);
 }
 
+static int
+getbounds(void) {
+	FILE *fp;
+	char buf[6];
+	int ret;
+
+	ret = 0;
+
+	if ((fp = fopen("bounds", "r")) == NULL) {
+		warnx("unable to open bounds file, using 0");
+		goto new;
+	}
+
+	if (fgets(buf, sizeof buf, fp) == NULL) {
+		warnx("unable to read from bounds, using 0");
+		goto new;
+	}
+
+	errno = 0;
+	ret = (int)strtol(buf, NULL, 10);
+	if (ret == 0)
+		if (errno == EINVAL || errno == ERANGE)
+			warnx("invalid value found in bounds, using 0");
+
+new:
+	if (fp)
+		fclose(fp);
+
+	if ((fp = fopen("bounds", "w")) == NULL) {
+		warnx("unable to write to bounds file");
+		goto done;
+	}
+
+	if (verbose)
+		printf("bounds number: %d\n", ret);
+
+	fprintf(fp, "%d\n", (ret + 1));
+	fclose(fp);
+
+done:
+	return (ret);
+}
 
 static void
 DoFile(const char *device)
 {
 	struct kerneldumpheader kdhf, kdhl;
-	char buf[BUFSIZ];
+	char buf[BUFSIZ * 64];
 	struct stat sb;
-	off_t mediasize, dumpsize, firsthd, lasthd;
-	char *md5;
+	off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
 	FILE *info;
 	int fd, fdcore, fdinfo, error, wl;
+	int bounds;
 	u_int sectorsize;
 
 	if (verbose)
 		printf("Checking for kernel dump on device %s\n", device);
 
+	dmpcnt = 0;
 	mediasize = 0;
 	fd = open(device, O_RDWR);
 	if (fd < 0) {
@@ -123,7 +165,20 @@
 		if (verbose)
 			warnx("Magic mismatch on last dump header on %s",
 			    device);
-		goto closefd;
+		if (force) {
+			if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
+				    sizeof kdhl.magic) == 0) {
+				if (verbose)
+					warnx("Forcing magic on %s", device);
+				memcpy(kdhl.magic, KERNELDUMPMAGIC,
+				    sizeof kdhl.magic);
+			} else {
+				warnx("Unable to force dump");
+				goto closefd;
+			}
+		} else {
+			goto closefd;
+		}
 	}
 	if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
 		warnx("Unknown version (%d) in last dump header on %s",
@@ -152,8 +207,10 @@
 		warn("First and last dump headers disagree on %s", device);
 		goto closefd;
 	}
-	md5 = MD5Data((unsigned char *)&kdhl, sizeof kdhl, NULL);
-	sprintf(buf, "%s.info", md5);
+
+	bounds = getbounds();
+
+	sprintf(buf, "info.%d", bounds);
 
 	/*
 	 * See if the dump has been saved already. Don't save the dump
@@ -162,8 +219,8 @@
 	if (stat(buf, &sb) == 0) {
 		if (!force) {
 			if (verbose)
-				printf("Dump on device %s already saved\n",
-				    device);
+				printf("Dump %d on device %s already saved\n",
+				    bounds, device);
 			goto closefd;
 		}
 	} else if (errno != ENOENT) {
@@ -179,7 +236,7 @@
 		warn("%s", buf);
 		goto closefd;
 	}
-	sprintf(buf, "%s.core", md5);
+	sprintf(buf, "vmcore.%d", bounds);
 	fdcore = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
 	if (fdcore < 0) {
 		warn("%s", buf);
@@ -189,12 +246,13 @@
 	info = fdopen(fdinfo, "w");
 
 	if (verbose)
-		printheader(stdout, &kdhl, device, md5);
+		printheader(stdout, &kdhl, device, bounds);
 
 	printf("Saving dump to file %s\n", buf);
 	nsaved++;
 
-	printheader(info, &kdhl, device, md5);
+	printheader(info, &kdhl, device, bounds);
+	fclose(info);
 
 	while (dumpsize > 0) {
 		wl = sizeof(buf);
@@ -207,12 +265,16 @@
 		}
 		error = write(fdcore, buf, wl);
 		if (error != wl) {
-			warn("Write error on %s.core file", md5);
+			warn("Write error on vmcore.%d file", bounds);
 			goto closeall;
 		}
+		if (verbose) {
+			dmpcnt += wl;
+			printf("%llu\r", dmpcnt);
+			fflush(stdout);
+		}
 		dumpsize -= wl;
 	}
-	close(fdinfo);
 	close(fdcore);
 
 	if (verbose)
@@ -222,7 +284,7 @@
 	if (clear || !keep) {
 		if (verbose)
 			printf("Clearing dump header\n");
-		memset(&kdhl, 0, sizeof kdhl);
+		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
 		lseek(fd, lasthd, SEEK_SET);
 		error = write(fd, &kdhl, sizeof kdhl);
 		if (error != sizeof kdhl)

--OXfL5xGRrasGEqWY
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="kerneldump.h.diff"

Index: kerneldump.h
===================================================================
RCS file: /mnt1/ncvs/src/sys/sys/kerneldump.h,v
retrieving revision 1.3
diff -u -d -r1.3 kerneldump.h
--- kerneldump.h	3 Apr 2002 07:24:10 -0000	1.3
+++ kerneldump.h	17 Apr 2002 22:05:48 -0000
@@ -60,6 +60,7 @@
 struct kerneldumpheader {
 	char		magic[20];
 #define	KERNELDUMPMAGIC		"FreeBSD Kernel Dump"
+#define KERNELDUMPMAGIC_CLEARED "FreeBSD Cleard Dump"
 	char		architecture[12];
 	uint32_t	version;
 #define	KERNELDUMPVERSION	1

--OXfL5xGRrasGEqWY--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020419003134.A54078>