2 * Copyright (c) 1986, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1986, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)savecore.c 8.3 (Berkeley) 1/2/94";
44 static const char rcsid[] =
45 "$FreeBSD: src/sbin/savecore/savecore.c,v 1.28.2.13 2002/04/07 21:17:50 asmodai Exp $";
48 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
55 #include <vm/vm_param.h>
67 extern FILE *zopen(const char *fname, const char *mode);
70 #define ok(number) ALPHA_K0SEG_TO_PHYS(number)
74 #define ok(number) ((number) - kernbase)
77 struct nlist current_nl[] = { /* Namelist for currently running system. */
94 int cursyms[] = { X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
95 int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
97 struct nlist dump_nl[] = { /* Name list for dumped system. */
98 { "_dumplo" }, /* Entries MUST be the same as */
99 { "_time_second" }, /* those in current_nl[]. */
108 /* Types match kernel declarations. */
109 u_long dumpmag; /* magic number in dump */
111 /* Based on kernel variables, but with more convenient types. */
112 off_t dumplo; /* where dump starts on dumpdev */
113 off_t dumpsize; /* amount of memory dumped */
115 char *kernel; /* user-specified kernel */
116 char *savedir; /* directory to save dumps in */
117 char ddname[MAXPATHLEN]; /* name of dump device */
118 dev_t dumpdev; /* dump device */
119 int dumpfd; /* read/write descriptor on char dev */
120 time_t now; /* current date */
121 char panic_mesg[1024]; /* panic message */
122 int panicstr; /* flag: dump was caused by panic */
123 char vers[1024]; /* version of kernel that crashed */
126 u_long kernbase; /* offset of kvm to core file */
129 int clear, compress, force, verbose; /* flags */
130 int keep; /* keep dump on device */
132 void check_kmem __P((void));
133 int check_space __P((void));
134 void clear_dump __P((void));
135 void DumpRead __P((int fd, void *bp, int size, off_t off, int flag));
136 void DumpWrite __P((int fd, void *bp, int size, off_t off, int flag));
137 int dump_exists __P((void));
138 void find_dev __P((dev_t));
139 int get_crashtime __P((void));
140 void get_dumpsize __P((void));
141 void kmem_setup __P((void));
142 void log __P((int, char *, ...)) __printf0like(2, 3);
143 void Lseek __P((int, off_t, int));
144 int Open __P((const char *, int rw));
145 int Read __P((int, void *, int));
146 void save_core __P((void));
147 void usage __P((void));
148 void Write __P((int, void *, int));
157 openlog("savecore", LOG_PERROR, LOG_DAEMON);
159 while ((ch = getopt(argc, argv, "cdfkN:vz")) != -1)
164 case 'd': /* Not documented. */
188 if (argc != 1 && argc != 2)
203 if (!dump_exists() && !force)
209 syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
211 syslog(LOG_ALERT, "reboot");
215 if ((!get_crashtime() || !check_space()) && !force)
230 const char *dump_sys;
232 long kdumplo; /* block number where dump starts on dumpdev */
236 * Some names we need for the currently running system, others for
237 * the system that was running when the dump was made. The values
238 * obtained from the current system are used to look for things in
239 * /dev/kmem that cannot be found in the dump_sys namelist, but are
240 * presumed to be the same (since the disk partitions are probably
243 if ((nlist(getbootfile(), current_nl)) == -1)
244 syslog(LOG_ERR, "%s: nlist: %m", getbootfile());
245 for (i = 0; cursyms[i] != -1; i++)
246 if (current_nl[cursyms[i]].n_value == 0) {
247 syslog(LOG_ERR, "%s: %s not in namelist",
248 getbootfile(), current_nl[cursyms[i]].n_name);
252 dump_sys = kernel ? kernel : getbootfile();
253 if ((nlist(dump_sys, dump_nl)) == -1)
254 syslog(LOG_ERR, "%s: nlist: %m", dump_sys);
255 for (i = 0; dumpsyms[i] != -1; i++)
256 if (dump_nl[dumpsyms[i]].n_value == 0) {
257 syslog(LOG_ERR, "%s: %s not in namelist",
258 dump_sys, dump_nl[dumpsyms[i]].n_name);
263 if (dump_nl[X_KERNBASE].n_value != 0)
264 kernbase = dump_nl[X_KERNBASE].n_value;
269 len = sizeof dumpdev;
270 if (sysctlbyname("kern.dumpdev", &dumpdev, &len, NULL, 0) == -1) {
271 syslog(LOG_ERR, "sysctl: kern.dumpdev: %m");
274 if (dumpdev == NODEV) {
275 syslog(LOG_WARNING, "no core dump (no dumpdev)");
279 kmem = Open(_PATH_KMEM, O_RDONLY);
280 Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET);
281 (void)Read(kmem, &kdumplo, sizeof(kdumplo));
282 dumplo = (off_t)kdumplo * DEV_BSIZE;
284 (void)printf("dumplo = %lld (%ld * %d)\n",
285 (long long)dumplo, kdumplo, DEV_BSIZE);
286 Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET);
287 (void)Read(kmem, &dumpmag, sizeof(dumpmag));
289 dumpfd = Open(ddname, O_RDWR);
293 lseek(kmem, (off_t)current_nl[X_VERSION].n_value, SEEK_SET);
294 Read(kmem, vers, sizeof(vers));
295 vers[sizeof(vers) - 1] = '\0';
296 p = strchr(vers, '\n');
300 /* Don't fclose(fp), we use kmem later. */
306 char core_vers[1024], *p;
308 DumpRead(dumpfd, core_vers, sizeof(core_vers),
309 (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET);
310 core_vers[sizeof(core_vers) - 1] = '\0';
311 p = strchr(core_vers, '\n');
314 if (strcmp(vers, core_vers) && kernel == 0)
316 "warning: %s version mismatch:\n\t\"%s\"\nand\t\"%s\"\n",
317 getbootfile(), vers, core_vers);
318 DumpRead(dumpfd, &panicstr, sizeof(panicstr),
319 (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
321 DumpRead(dumpfd, panic_mesg, sizeof(panic_mesg),
322 (off_t)(dumplo + ok(panicstr)), L_SET);
327 * Clear the magic number in the dump header.
335 DumpWrite(dumpfd, &newdumpmag, sizeof(newdumpmag),
336 (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
341 * Check if a dump exists by looking for a magic number in the dump
349 DumpRead(dumpfd, &newdumpmag, sizeof(newdumpmag),
350 (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
351 if (newdumpmag != dumpmag) {
353 syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
354 newdumpmag, dumpmag);
355 syslog(LOG_WARNING, "no core dump");
361 char buf[1024 * 1024];
362 #define BLOCKSIZE (1<<12)
363 #define BLOCKMASK (~(BLOCKSIZE-1))
366 * Save the core dump.
372 register int bounds, ifd, nr, nw;
373 int hs, he; /* start and end of hole */
374 char path[MAXPATHLEN];
378 * Get the current number and update the bounds file. Do the update
379 * now, because may fail later and don't want to overwrite anything.
381 (void)snprintf(path, sizeof(path), "%s/bounds", savedir);
382 if ((fp = fopen(path, "r")) == NULL)
384 if (fgets(buf, sizeof(buf), fp) == NULL) {
386 err1: syslog(LOG_WARNING, "%s: %m", path);
392 if ((fp = fopen(path, "w")) == NULL)
393 syslog(LOG_ERR, "%s: %m", path);
395 (void)fprintf(fp, "%d\n", bounds + 1);
399 /* Create the core file. */
400 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
401 (void)snprintf(path, sizeof(path), "%s/vmcore.%d%s",
402 savedir, bounds, compress ? ".gz" : "");
404 fp = zopen(path, "w");
406 fp = fopen(path, "w");
408 syslog(LOG_ERR, "%s: %m", path);
413 /* Seek to the start of the core. */
414 Lseek(dumpfd, (off_t)dumplo, L_SET);
416 /* Copy the core file. */
417 syslog(LOG_NOTICE, "writing %score to %s",
418 compress ? "compressed " : "", path);
419 for (; dumpsize > 0; dumpsize -= nr) {
420 (void)printf("%6dK\r", dumpsize / 1024);
421 (void)fflush(stdout);
422 nr = read(dumpfd, buf, MIN(dumpsize, sizeof(buf)));
426 "WARNING: EOF on dump device");
428 syslog(LOG_ERR, "%s: %m", ddname);
431 for (nw = 0; nw < nr; nw = he) {
432 /* find a contiguous block of zeroes */
433 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
434 for (he = hs; he < nr && buf[he] == 0; ++he)
437 /* is the hole long enough to matter? */
438 if (he >= hs + BLOCKSIZE)
442 /* back down to a block boundary */
446 * 1) Don't go beyond the end of the buffer.
447 * 2) If the end of the buffer is less than
448 * BLOCKSIZE bytes away, we're at the end
449 * of the file, so just grab what's left.
451 if (hs + BLOCKSIZE > nr)
455 * At this point, we have a partial ordering:
456 * nw <= hs <= he <= nr
457 * If hs > nw, buf[nw..hs] contains non-zero data.
458 * If he > hs, buf[hs..he] is all zeroes.
461 if (fwrite(buf + nw, hs - nw, 1, fp) != 1)
464 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
468 syslog(LOG_ERR, "%s: %m", path);
469 err2: syslog(LOG_WARNING,
470 "WARNING: vmcore may be incomplete");
478 /* Copy the kernel. */
479 ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
480 (void)snprintf(path, sizeof(path), "%s/kernel.%d%s",
481 savedir, bounds, compress ? ".gz" : "");
483 fp = zopen(path, "w");
485 fp = fopen(path, "w");
487 syslog(LOG_ERR, "%s: %m", path);
490 syslog(LOG_NOTICE, "writing %skernel to %s",
491 compress ? "compressed " : "", path);
492 while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
493 nw = fwrite(buf, 1, nr, fp);
495 syslog(LOG_ERR, "%s: %m", path);
497 "WARNING: kernel may be incomplete");
502 syslog(LOG_ERR, "%s: %m", kernel ? kernel : getbootfile());
504 "WARNING: kernel may be incomplete");
512 * Verify that the specified device node exists and matches the
516 verify_dev(name, dev)
522 if (lstat(name, &sb) == -1)
524 if (!S_ISCHR(sb.st_mode) || sb.st_rdev != dev)
530 * Find the dump device.
532 * 1) try devname(3); see if it returns something sensible
533 * 2) scan /dev for the desired node
534 * 3) as a last resort, try to create the node we need
544 strcpy(ddname, _PATH_DEV);
545 dnp = ddname + sizeof _PATH_DEV - 1;
546 if ((dn = devname(dev, S_IFCHR)) != NULL) {
548 if (verify_dev(ddname, dev) == 0)
551 if ((d = opendir(_PATH_DEV)) != NULL) {
552 while ((ent = readdir(d))) {
553 strcpy(dnp, ent->d_name);
554 if (verify_dev(ddname, dev) == 0) {
562 if (mknod(ddname, S_IFCHR|S_IRUSR|S_IWUSR, dev) == 0)
564 syslog(LOG_ERR, "can't find device %d/%#x", major(dev), minor(dev));
569 * Extract the date and time of the crash from the dump header, and
570 * make sure it looks sane (within one week of current date and time).
575 time_t dumptime; /* Time the dump was taken. */
577 DumpRead(dumpfd, &dumptime, sizeof(dumptime),
578 (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
581 syslog(LOG_ERR, "dump time is zero");
584 (void)printf("savecore: system went down at %s", ctime(&dumptime));
585 #define LEEWAY (7 * 86400)
586 if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
587 (void)printf("dump time is unreasonable\n");
594 * Extract the size of the dump from the dump header.
599 int kdumpsize; /* Number of pages in dump. */
601 /* Read the dump size. */
602 DumpRead(dumpfd, &kdumpsize, sizeof(kdumpsize),
603 (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
604 dumpsize = (off_t)kdumpsize * getpagesize();
608 * Check that sufficient space is available on the disk that holds the
616 off_t minfree, spacefree, totfree, kernelsize, needed;
619 char buf[100], path[MAXPATHLEN];
621 tkernel = kernel ? kernel : getbootfile();
622 if (stat(tkernel, &st) < 0) {
623 syslog(LOG_ERR, "%s: %m", tkernel);
626 kernelsize = st.st_blocks * S_BLKSIZE;
628 if (statfs(savedir, &fsbuf) < 0) {
629 syslog(LOG_ERR, "%s: %m", savedir);
632 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
633 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
635 (void)snprintf(path, sizeof(path), "%s/minfree", savedir);
636 if ((fp = fopen(path, "r")) == NULL)
639 if (fgets(buf, sizeof(buf), fp) == NULL)
646 needed = (dumpsize + kernelsize) / 1024;
647 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
649 "no dump, not enough free space on device (%lld available, need %lld)",
650 (long long)(minfree > 0 ? spacefree : totfree),
654 if (spacefree - needed < 0)
656 "dump performed, but free space threshold crossed");
667 if ((fd = open(name, rw, 0)) < 0) {
668 syslog(LOG_ERR, "%s: %m", name);
681 nr = read(fd, bp, size);
683 syslog(LOG_ERR, "read: %m");
696 ret = lseek(fd, off, flag);
698 syslog(LOG_ERR, "lseek: %m");
704 * DumpWrite and DumpRead block io requests to the * dump device.
706 #define DUMPBUFSIZE 8192
708 DumpWrite(fd, bp, size, off, flag)
713 unsigned char buf[DUMPBUFSIZE], *p, *q;
718 syslog(LOG_ERR, "lseek: not LSET");
723 pos = off & ~(DUMPBUFSIZE - 1);
724 Lseek(fd, pos, flag);
725 (void)Read(fd, buf, sizeof(buf));
726 j = off & (DUMPBUFSIZE - 1);
729 if (i > DUMPBUFSIZE - j)
732 Lseek(fd, pos, flag);
733 (void)Write(fd, buf, sizeof(buf));
741 DumpRead(fd, bp, size, off, flag)
746 unsigned char buf[DUMPBUFSIZE], *p, *q;
751 syslog(LOG_ERR, "lseek: not LSET");
756 pos = off & ~(DUMPBUFSIZE - 1);
757 Lseek(fd, pos, flag);
758 (void)Read(fd, buf, sizeof(buf));
759 j = off & (DUMPBUFSIZE - 1);
762 if (i > DUMPBUFSIZE - j)
778 if ((n = write(fd, bp, size)) < size) {
779 syslog(LOG_ERR, "write: %m");
787 (void)syslog(LOG_ERR, "usage: savecore [-cfkvz] [-N system] directory");