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
33 * @(#) Copyright (c) 1986, 1992, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)savecore.c 8.3 (Berkeley) 1/2/94
35 * $FreeBSD: src/sbin/savecore/savecore.c,v 1.28.2.13 2002/04/07 21:17:50 asmodai Exp $
36 * $DragonFly: src/sbin/savecore/savecore.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
39 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/syslog.h>
43 #include <sys/sysctl.h>
46 #include <vm/vm_param.h>
58 extern FILE *zopen(const char *fname, const char *mode);
61 #define ok(number) ALPHA_K0SEG_TO_PHYS(number)
65 #define ok(number) ((number) - kernbase)
68 struct nlist current_nl[] = { /* Namelist for currently running system. */
85 int cursyms[] = { X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
86 int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
88 struct nlist dump_nl[] = { /* Name list for dumped system. */
89 { "_dumplo" }, /* Entries MUST be the same as */
90 { "_time_second" }, /* those in current_nl[]. */
99 /* Types match kernel declarations. */
100 u_long dumpmag; /* magic number in dump */
102 /* Based on kernel variables, but with more convenient types. */
103 off_t dumplo; /* where dump starts on dumpdev */
104 off_t dumpsize; /* amount of memory dumped */
106 char *kernel; /* user-specified kernel */
107 char *savedir; /* directory to save dumps in */
108 char ddname[MAXPATHLEN]; /* name of dump device */
109 dev_t dumpdev; /* dump device */
110 int dumpfd; /* read/write descriptor on char dev */
111 time_t now; /* current date */
112 char panic_mesg[1024]; /* panic message */
113 int panicstr; /* flag: dump was caused by panic */
114 char vers[1024]; /* version of kernel that crashed */
117 u_long kernbase; /* offset of kvm to core file */
120 int clear, compress, force, verbose; /* flags */
121 int keep; /* keep dump on device */
123 void check_kmem __P((void));
124 int check_space __P((void));
125 void clear_dump __P((void));
126 void DumpRead __P((int fd, void *bp, int size, off_t off, int flag));
127 void DumpWrite __P((int fd, void *bp, int size, off_t off, int flag));
128 int dump_exists __P((void));
129 void find_dev __P((dev_t));
130 int get_crashtime __P((void));
131 void get_dumpsize __P((void));
132 void kmem_setup __P((void));
133 void log __P((int, char *, ...)) __printf0like(2, 3);
134 void Lseek __P((int, off_t, int));
135 int Open __P((const char *, int rw));
136 int Read __P((int, void *, int));
137 void save_core __P((void));
138 void usage __P((void));
139 void Write __P((int, void *, int));
148 openlog("savecore", LOG_PERROR, LOG_DAEMON);
150 while ((ch = getopt(argc, argv, "cdfkN:vz")) != -1)
155 case 'd': /* Not documented. */
179 if (argc != 1 && argc != 2)
194 if (!dump_exists() && !force)
200 syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
202 syslog(LOG_ALERT, "reboot");
206 if ((!get_crashtime() || !check_space()) && !force)
221 const char *dump_sys;
223 long kdumplo; /* block number where dump starts on dumpdev */
227 * Some names we need for the currently running system, others for
228 * the system that was running when the dump was made. The values
229 * obtained from the current system are used to look for things in
230 * /dev/kmem that cannot be found in the dump_sys namelist, but are
231 * presumed to be the same (since the disk partitions are probably
234 if ((nlist(getbootfile(), current_nl)) == -1)
235 syslog(LOG_ERR, "%s: nlist: %m", getbootfile());
236 for (i = 0; cursyms[i] != -1; i++)
237 if (current_nl[cursyms[i]].n_value == 0) {
238 syslog(LOG_ERR, "%s: %s not in namelist",
239 getbootfile(), current_nl[cursyms[i]].n_name);
243 dump_sys = kernel ? kernel : getbootfile();
244 if ((nlist(dump_sys, dump_nl)) == -1)
245 syslog(LOG_ERR, "%s: nlist: %m", dump_sys);
246 for (i = 0; dumpsyms[i] != -1; i++)
247 if (dump_nl[dumpsyms[i]].n_value == 0) {
248 syslog(LOG_ERR, "%s: %s not in namelist",
249 dump_sys, dump_nl[dumpsyms[i]].n_name);
254 if (dump_nl[X_KERNBASE].n_value != 0)
255 kernbase = dump_nl[X_KERNBASE].n_value;
260 len = sizeof dumpdev;
261 if (sysctlbyname("kern.dumpdev", &dumpdev, &len, NULL, 0) == -1) {
262 syslog(LOG_ERR, "sysctl: kern.dumpdev: %m");
265 if (dumpdev == NODEV) {
266 syslog(LOG_WARNING, "no core dump (no dumpdev)");
270 kmem = Open(_PATH_KMEM, O_RDONLY);
271 Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET);
272 (void)Read(kmem, &kdumplo, sizeof(kdumplo));
273 dumplo = (off_t)kdumplo * DEV_BSIZE;
275 (void)printf("dumplo = %lld (%ld * %d)\n",
276 (long long)dumplo, kdumplo, DEV_BSIZE);
277 Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET);
278 (void)Read(kmem, &dumpmag, sizeof(dumpmag));
280 dumpfd = Open(ddname, O_RDWR);
284 lseek(kmem, (off_t)current_nl[X_VERSION].n_value, SEEK_SET);
285 Read(kmem, vers, sizeof(vers));
286 vers[sizeof(vers) - 1] = '\0';
287 p = strchr(vers, '\n');
291 /* Don't fclose(fp), we use kmem later. */
297 char core_vers[1024], *p;
299 DumpRead(dumpfd, core_vers, sizeof(core_vers),
300 (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET);
301 core_vers[sizeof(core_vers) - 1] = '\0';
302 p = strchr(core_vers, '\n');
305 if (strcmp(vers, core_vers) && kernel == 0)
307 "warning: %s version mismatch:\n\t\"%s\"\nand\t\"%s\"\n",
308 getbootfile(), vers, core_vers);
309 DumpRead(dumpfd, &panicstr, sizeof(panicstr),
310 (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
312 DumpRead(dumpfd, panic_mesg, sizeof(panic_mesg),
313 (off_t)(dumplo + ok(panicstr)), L_SET);
318 * Clear the magic number in the dump header.
326 DumpWrite(dumpfd, &newdumpmag, sizeof(newdumpmag),
327 (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
332 * Check if a dump exists by looking for a magic number in the dump
340 DumpRead(dumpfd, &newdumpmag, sizeof(newdumpmag),
341 (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
342 if (newdumpmag != dumpmag) {
344 syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
345 newdumpmag, dumpmag);
346 syslog(LOG_WARNING, "no core dump");
352 char buf[1024 * 1024];
353 #define BLOCKSIZE (1<<12)
354 #define BLOCKMASK (~(BLOCKSIZE-1))
357 * Save the core dump.
363 register int bounds, ifd, nr, nw;
364 int hs, he; /* start and end of hole */
365 char path[MAXPATHLEN];
369 * Get the current number and update the bounds file. Do the update
370 * now, because may fail later and don't want to overwrite anything.
372 (void)snprintf(path, sizeof(path), "%s/bounds", savedir);
373 if ((fp = fopen(path, "r")) == NULL)
375 if (fgets(buf, sizeof(buf), fp) == NULL) {
377 err1: syslog(LOG_WARNING, "%s: %m", path);
383 if ((fp = fopen(path, "w")) == NULL)
384 syslog(LOG_ERR, "%s: %m", path);
386 (void)fprintf(fp, "%d\n", bounds + 1);
390 /* Create the core file. */
391 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
392 (void)snprintf(path, sizeof(path), "%s/vmcore.%d%s",
393 savedir, bounds, compress ? ".gz" : "");
395 fp = zopen(path, "w");
397 fp = fopen(path, "w");
399 syslog(LOG_ERR, "%s: %m", path);
404 /* Seek to the start of the core. */
405 Lseek(dumpfd, (off_t)dumplo, L_SET);
407 /* Copy the core file. */
408 syslog(LOG_NOTICE, "writing %score to %s",
409 compress ? "compressed " : "", path);
410 for (; dumpsize > 0; dumpsize -= nr) {
411 (void)printf("%6dK\r", dumpsize / 1024);
412 (void)fflush(stdout);
413 nr = read(dumpfd, buf, MIN(dumpsize, sizeof(buf)));
417 "WARNING: EOF on dump device");
419 syslog(LOG_ERR, "%s: %m", ddname);
422 for (nw = 0; nw < nr; nw = he) {
423 /* find a contiguous block of zeroes */
424 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
425 for (he = hs; he < nr && buf[he] == 0; ++he)
428 /* is the hole long enough to matter? */
429 if (he >= hs + BLOCKSIZE)
433 /* back down to a block boundary */
437 * 1) Don't go beyond the end of the buffer.
438 * 2) If the end of the buffer is less than
439 * BLOCKSIZE bytes away, we're at the end
440 * of the file, so just grab what's left.
442 if (hs + BLOCKSIZE > nr)
446 * At this point, we have a partial ordering:
447 * nw <= hs <= he <= nr
448 * If hs > nw, buf[nw..hs] contains non-zero data.
449 * If he > hs, buf[hs..he] is all zeroes.
452 if (fwrite(buf + nw, hs - nw, 1, fp) != 1)
455 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
459 syslog(LOG_ERR, "%s: %m", path);
460 err2: syslog(LOG_WARNING,
461 "WARNING: vmcore may be incomplete");
469 /* Copy the kernel. */
470 ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
471 (void)snprintf(path, sizeof(path), "%s/kernel.%d%s",
472 savedir, bounds, compress ? ".gz" : "");
474 fp = zopen(path, "w");
476 fp = fopen(path, "w");
478 syslog(LOG_ERR, "%s: %m", path);
481 syslog(LOG_NOTICE, "writing %skernel to %s",
482 compress ? "compressed " : "", path);
483 while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
484 nw = fwrite(buf, 1, nr, fp);
486 syslog(LOG_ERR, "%s: %m", path);
488 "WARNING: kernel may be incomplete");
493 syslog(LOG_ERR, "%s: %m", kernel ? kernel : getbootfile());
495 "WARNING: kernel may be incomplete");
503 * Verify that the specified device node exists and matches the
507 verify_dev(name, dev)
513 if (lstat(name, &sb) == -1)
515 if (!S_ISCHR(sb.st_mode) || sb.st_rdev != dev)
521 * Find the dump device.
523 * 1) try devname(3); see if it returns something sensible
524 * 2) scan /dev for the desired node
525 * 3) as a last resort, try to create the node we need
535 strcpy(ddname, _PATH_DEV);
536 dnp = ddname + sizeof _PATH_DEV - 1;
537 if ((dn = devname(dev, S_IFCHR)) != NULL) {
539 if (verify_dev(ddname, dev) == 0)
542 if ((d = opendir(_PATH_DEV)) != NULL) {
543 while ((ent = readdir(d))) {
544 strcpy(dnp, ent->d_name);
545 if (verify_dev(ddname, dev) == 0) {
553 if (mknod(ddname, S_IFCHR|S_IRUSR|S_IWUSR, dev) == 0)
555 syslog(LOG_ERR, "can't find device %d/%#x", major(dev), minor(dev));
560 * Extract the date and time of the crash from the dump header, and
561 * make sure it looks sane (within one week of current date and time).
566 time_t dumptime; /* Time the dump was taken. */
568 DumpRead(dumpfd, &dumptime, sizeof(dumptime),
569 (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
572 syslog(LOG_ERR, "dump time is zero");
575 (void)printf("savecore: system went down at %s", ctime(&dumptime));
576 #define LEEWAY (7 * 86400)
577 if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
578 (void)printf("dump time is unreasonable\n");
585 * Extract the size of the dump from the dump header.
590 int kdumpsize; /* Number of pages in dump. */
592 /* Read the dump size. */
593 DumpRead(dumpfd, &kdumpsize, sizeof(kdumpsize),
594 (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
595 dumpsize = (off_t)kdumpsize * getpagesize();
599 * Check that sufficient space is available on the disk that holds the
607 off_t minfree, spacefree, totfree, kernelsize, needed;
610 char buf[100], path[MAXPATHLEN];
612 tkernel = kernel ? kernel : getbootfile();
613 if (stat(tkernel, &st) < 0) {
614 syslog(LOG_ERR, "%s: %m", tkernel);
617 kernelsize = st.st_blocks * S_BLKSIZE;
619 if (statfs(savedir, &fsbuf) < 0) {
620 syslog(LOG_ERR, "%s: %m", savedir);
623 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
624 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
626 (void)snprintf(path, sizeof(path), "%s/minfree", savedir);
627 if ((fp = fopen(path, "r")) == NULL)
630 if (fgets(buf, sizeof(buf), fp) == NULL)
637 needed = (dumpsize + kernelsize) / 1024;
638 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
640 "no dump, not enough free space on device (%lld available, need %lld)",
641 (long long)(minfree > 0 ? spacefree : totfree),
645 if (spacefree - needed < 0)
647 "dump performed, but free space threshold crossed");
658 if ((fd = open(name, rw, 0)) < 0) {
659 syslog(LOG_ERR, "%s: %m", name);
672 nr = read(fd, bp, size);
674 syslog(LOG_ERR, "read: %m");
687 ret = lseek(fd, off, flag);
689 syslog(LOG_ERR, "lseek: %m");
695 * DumpWrite and DumpRead block io requests to the * dump device.
697 #define DUMPBUFSIZE 8192
699 DumpWrite(fd, bp, size, off, flag)
704 unsigned char buf[DUMPBUFSIZE], *p, *q;
709 syslog(LOG_ERR, "lseek: not LSET");
714 pos = off & ~(DUMPBUFSIZE - 1);
715 Lseek(fd, pos, flag);
716 (void)Read(fd, buf, sizeof(buf));
717 j = off & (DUMPBUFSIZE - 1);
720 if (i > DUMPBUFSIZE - j)
723 Lseek(fd, pos, flag);
724 (void)Write(fd, buf, sizeof(buf));
732 DumpRead(fd, bp, size, off, flag)
737 unsigned char buf[DUMPBUFSIZE], *p, *q;
742 syslog(LOG_ERR, "lseek: not LSET");
747 pos = off & ~(DUMPBUFSIZE - 1);
748 Lseek(fd, pos, flag);
749 (void)Read(fd, buf, sizeof(buf));
750 j = off & (DUMPBUFSIZE - 1);
753 if (i > DUMPBUFSIZE - j)
769 if ((n = write(fd, bp, size)) < size) {
770 syslog(LOG_ERR, "write: %m");
778 (void)syslog(LOG_ERR, "usage: savecore [-cfkvz] [-N system] directory");