savecore.c revision 1.29 1 /* $NetBSD: savecore.c,v 1.29 1997/04/21 12:50:43 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1986, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1986, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)savecore.c 8.3 (Berkeley) 1/2/94";
45 #else
46 static char rcsid[] = "$NetBSD: savecore.c,v 1.29 1997/04/21 12:50:43 mrg Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/mount.h>
53 #include <sys/syslog.h>
54 #include <sys/time.h>
55
56 #include <dirent.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <nlist.h>
60 #include <paths.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <tzfile.h>
65 #include <unistd.h>
66 #include <limits.h>
67 #include <kvm.h>
68
69 extern FILE *zopen __P((const char *fname, const char *mode, int bits));
70
71 #define KREAD(kd, addr, p)\
72 (kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
73
74 struct nlist current_nl[] = { /* Namelist for currently running system. */
75 #define X_DUMPDEV 0
76 { "_dumpdev" },
77 #define X_DUMPLO 1
78 { "_dumplo" },
79 #define X_TIME 2
80 { "_time" },
81 #define X_DUMPSIZE 3
82 { "_dumpsize" },
83 #define X_VERSION 4
84 { "_version" },
85 #define X_PANICSTR 5
86 { "_panicstr" },
87 #define X_DUMPMAG 6
88 { "_dumpmag" },
89 { NULL },
90 };
91 int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
92 int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
93
94 struct nlist dump_nl[] = { /* Name list for dumped system. */
95 { "_dumpdev" }, /* Entries MUST be the same as */
96 { "_dumplo" }, /* those in current_nl[]. */
97 { "_time" },
98 { "_dumpsize" },
99 { "_version" },
100 { "_panicstr" },
101 { "_dumpmag" },
102 { NULL },
103 };
104
105 /* Types match kernel declarations. */
106 long dumplo; /* where dump starts on dumpdev */
107 int dumpmag; /* magic number in dump */
108 int dumpsize; /* amount of memory dumped */
109
110 char *kernel;
111 char *dirname; /* directory to save dumps in */
112 char *ddname; /* name of dump device */
113 dev_t dumpdev; /* dump device */
114 int dumpfd; /* read/write descriptor on block dev */
115 kvm_t *kd_dump; /* kvm descriptor on block dev */
116 time_t now; /* current date */
117 char panic_mesg[1024];
118 long panicstr;
119 char vers[1024];
120
121 int clear, compress, force, verbose; /* flags */
122
123 void check_kmem __P((void));
124 int check_space __P((void));
125 void clear_dump __P((void));
126 int Create __P((char *, int));
127 int dump_exists __P((void));
128 char *find_dev __P((dev_t, int));
129 int get_crashtime __P((void));
130 void kmem_setup __P((void));
131 void log __P((int, char *, ...));
132 void Lseek __P((int, off_t, int));
133 int Open __P((char *, int rw));
134 char *rawname __P((char *s));
135 void save_core __P((void));
136 void usage __P((void));
137 void Write __P((int, void *, int));
138
139 int
140 main(argc, argv)
141 int argc;
142 char *argv[];
143 {
144 int ch;
145
146 openlog("savecore", LOG_PERROR, LOG_DAEMON);
147
148 while ((ch = getopt(argc, argv, "cdfN:vz")) != -1)
149 switch(ch) {
150 case 'c':
151 clear = 1;
152 break;
153 case 'd': /* Not documented. */
154 case 'v':
155 verbose = 1;
156 break;
157 case 'f':
158 force = 1;
159 break;
160 case 'N':
161 kernel = optarg;
162 break;
163 case 'z':
164 compress = 1;
165 break;
166 case '?':
167 default:
168 usage();
169 }
170 argc -= optind;
171 argv += optind;
172
173 if (!clear) {
174 if (argc != 1 && argc != 2)
175 usage();
176 dirname = argv[0];
177 }
178 if (argc == 2)
179 kernel = argv[1];
180
181 (void)time(&now);
182 kmem_setup();
183
184 if (clear) {
185 clear_dump();
186 exit(0);
187 }
188
189 if (!dump_exists() && !force)
190 exit(1);
191
192 check_kmem();
193
194 if (panicstr)
195 syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
196 else
197 syslog(LOG_ALERT, "reboot");
198
199 if ((!get_crashtime() || !check_space()) && !force)
200 exit(1);
201
202 save_core();
203
204 clear_dump();
205 exit(0);
206 }
207
208 void
209 kmem_setup()
210 {
211 kvm_t *kd_kern;
212 char errbuf[_POSIX2_LINE_MAX];
213 int i, hdrsz;
214 char *dump_sys;
215
216 /*
217 * Some names we need for the currently running system, others for
218 * the system that was running when the dump was made. The values
219 * obtained from the current system are used to look for things in
220 * /dev/kmem that cannot be found in the dump_sys namelist, but are
221 * presumed to be the same (since the disk partitions are probably
222 * the same!)
223 */
224 kd_kern = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
225 if (kd_kern == NULL) {
226 syslog(LOG_ERR, "%s: kvm_openfiles: %s", _PATH_UNIX, errbuf);
227 exit(1);
228 }
229 if (kvm_nlist(kd_kern, current_nl) == -1)
230 syslog(LOG_ERR, "%s: kvm_nlist: %s", _PATH_UNIX,
231 kvm_geterr(kd_kern));
232
233 for (i = 0; cursyms[i] != -1; i++)
234 if (current_nl[cursyms[i]].n_value == 0) {
235 syslog(LOG_ERR, "%s: %s not in namelist",
236 _PATH_UNIX, current_nl[cursyms[i]].n_name);
237 exit(1);
238 }
239
240 KREAD(kd_kern, current_nl[X_DUMPDEV].n_value, &dumpdev);
241 if (dumpdev == NODEV) {
242 syslog(LOG_WARNING, "no core dump (no dumpdev)");
243 exit(1);
244 }
245 KREAD(kd_kern, current_nl[X_DUMPLO].n_value, &dumplo);
246 dumplo *= DEV_BSIZE;
247 if (verbose)
248 (void)printf("dumplo = %d (%d * %d)\n",
249 dumplo, dumplo / DEV_BSIZE, DEV_BSIZE);
250 KREAD(kd_kern, current_nl[X_DUMPMAG].n_value, &dumpmag);
251
252 if (kernel == NULL) {
253 (void)kvm_read(kd_kern, current_nl[X_VERSION].n_value,
254 vers, sizeof(vers));
255 vers[sizeof(vers) - 1] = '\0';
256 }
257
258 ddname = find_dev(dumpdev, S_IFBLK);
259 dumpfd = Open(ddname, O_RDWR);
260
261 dump_sys = kernel ? kernel : _PATH_UNIX;
262
263 kd_dump = kvm_openfiles(dump_sys, ddname, NULL, O_RDWR, errbuf);
264 if (kd_dump == NULL) {
265 syslog(LOG_ERR, "%s: kvm_openfiles: %s", dump_sys, errbuf);
266 exit(1);
267 }
268
269 if (kvm_nlist(kd_dump, dump_nl) == -1)
270 syslog(LOG_ERR, "%s: kvm_nlist: %s", dump_sys,
271 kvm_geterr(kd_dump));
272
273 for (i = 0; dumpsyms[i] != -1; i++)
274 if (dump_nl[dumpsyms[i]].n_value == 0) {
275 syslog(LOG_ERR, "%s: %s not in namelist",
276 dump_sys, dump_nl[dumpsyms[i]].n_name);
277 exit(1);
278 }
279 hdrsz = kvm_dump_mkheader(kd_dump, (off_t)dumplo);
280
281 /*
282 * If 'hdrsz' == 0, kvm_dump_mkheader() failed on the magic-number
283 * checks, ergo no dump is present...
284 */
285 if (hdrsz == 0) {
286 syslog(LOG_WARNING, "no core dump");
287 exit(1);
288 }
289 if (hdrsz == -1) {
290 syslog(LOG_ERR, "%s: kvm_dump_mkheader: %s", dump_sys,
291 kvm_geterr(kd_dump));
292 exit(1);
293 }
294 dumplo += hdrsz;
295 kvm_close(kd_kern);
296 }
297
298 void
299 check_kmem()
300 {
301 register char *cp;
302 register long panicloc;
303 char core_vers[1024];
304
305 (void)kvm_read(kd_dump, dump_nl[X_VERSION].n_value, core_vers,
306 sizeof(core_vers));
307 core_vers[sizeof(core_vers) - 1] = '\0';
308
309 if (strcmp(vers, core_vers) && kernel == 0)
310 syslog(LOG_WARNING,
311 "warning: %s version mismatch:\n\t%s\nand\t%s\n",
312 _PATH_UNIX, vers, core_vers);
313
314 KREAD(kd_dump, dump_nl[X_PANICSTR].n_value, &panicstr);
315 if (panicstr) {
316 cp = panic_mesg;
317 panicloc = panicstr;
318 do {
319 KREAD(kd_dump, panicloc, cp);
320 panicloc++;
321 } while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]);
322 }
323 }
324
325 int
326 dump_exists()
327 {
328 int newdumpmag;
329
330 KREAD(kd_dump, dump_nl[X_DUMPMAG].n_value, &newdumpmag);
331
332 /* Read the dump size. */
333 KREAD(kd_dump, dump_nl[X_DUMPSIZE].n_value, &dumpsize);
334 dumpsize *= getpagesize();
335
336 /*
337 * Return zero if core dump doesn't seem to be there, and note
338 * it for syslog. This check and return happens after the dump size
339 * is read, so dumpsize is whether or not the core is valid (for -f).
340 */
341 if (newdumpmag != dumpmag) {
342 if (verbose)
343 syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
344 newdumpmag, dumpmag);
345 syslog(LOG_WARNING, "no core dump");
346 return (0);
347 }
348 return (1);
349 }
350
351 void
352 clear_dump()
353 {
354 if (kvm_dump_inval(kd_dump) == -1)
355 syslog(LOG_ERR, "%s: kvm_clear_dump: %s", ddname,
356 kvm_geterr(kd_dump));
357
358 }
359
360 char buf[1024 * 1024];
361
362 void
363 save_core()
364 {
365 register FILE *fp;
366 register int bounds, ifd, nr, nw, ofd;
367 char *rawp, path[MAXPATHLEN];
368
369 /*
370 * Get the current number and update the bounds file. Do the update
371 * now, because may fail later and don't want to overwrite anything.
372 */
373 umask(002);
374 (void)snprintf(path, sizeof(path), "%s/bounds", dirname);
375 if ((fp = fopen(path, "r")) == NULL)
376 goto err1;
377 if (fgets(buf, sizeof(buf), fp) == NULL) {
378 if (ferror(fp))
379 err1: syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
380 bounds = 0;
381 } else
382 bounds = atoi(buf);
383 if (fp != NULL)
384 (void)fclose(fp);
385 if ((fp = fopen(path, "w")) == NULL)
386 syslog(LOG_ERR, "%s: %m", path);
387 else {
388 (void)fprintf(fp, "%d\n", bounds + 1);
389 (void)fclose(fp);
390 }
391 (void)fclose(fp);
392
393 /* Create the core file. */
394 (void)snprintf(path, sizeof(path), "%s/netbsd.%d.core%s",
395 dirname, bounds, compress ? ".Z" : "");
396 if (compress) {
397 if ((fp = zopen(path, "w", 0)) == NULL) {
398 syslog(LOG_ERR, "%s: %s", path, strerror(errno));
399 exit(1);
400 }
401 } else {
402 ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
403 fp = fdopen(ofd, "w");
404 if (fp == NULL) {
405 syslog(LOG_ERR, "%s: fdopen: %s", path);
406 exit(1);
407 }
408 }
409
410 /* Open the raw device. */
411 rawp = rawname(ddname);
412 if ((ifd = open(rawp, O_RDONLY)) == -1) {
413 syslog(LOG_WARNING, "%s: %m; using block device", rawp);
414 ifd = dumpfd;
415 }
416
417 /* Seek to the start of the core. */
418 Lseek(ifd, (off_t)dumplo, L_SET);
419
420 if (kvm_dump_wrtheader(kd_dump, fp, dumpsize) == -1) {
421 syslog(LOG_ERR, "kvm_dump_wrtheader: %s : %s", path,
422 kvm_geterr(kd_dump));
423 exit(1);
424 }
425
426 /* Copy the core file. */
427 syslog(LOG_NOTICE, "writing %score to %s",
428 compress ? "compressed " : "", path);
429 for (; dumpsize > 0; dumpsize -= nr) {
430 (void)printf("%6dK\r", dumpsize / 1024);
431 (void)fflush(stdout);
432 nr = read(ifd, buf, MIN(dumpsize, sizeof(buf)));
433 if (nr <= 0) {
434 if (nr == 0)
435 syslog(LOG_WARNING,
436 "WARNING: EOF on dump device");
437 else
438 syslog(LOG_ERR, "%s: %m", rawp);
439 goto err2;
440 }
441 nw = fwrite(buf, 1, nr, fp);
442 if (nw != nr) {
443 syslog(LOG_ERR, "%s: %s",
444 path, strerror(nw == 0 ? EIO : errno));
445 err2: syslog(LOG_WARNING,
446 "WARNING: core may be incomplete");
447 (void)printf("\n");
448 exit(1);
449 }
450 }
451 (void)close(ifd);
452 (void)fclose(fp);
453
454 /* Copy the kernel. */
455 ifd = Open(kernel ? kernel : _PATH_UNIX, O_RDONLY);
456 (void)snprintf(path, sizeof(path), "%s/netbsd.%d%s",
457 dirname, bounds, compress ? ".Z" : "");
458 if (compress) {
459 if ((fp = zopen(path, "w", 0)) == NULL) {
460 syslog(LOG_ERR, "%s: %s", path, strerror(errno));
461 exit(1);
462 }
463 } else
464 ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
465 syslog(LOG_NOTICE, "writing %skernel to %s",
466 compress ? "compressed " : "", path);
467 while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
468 if (compress)
469 nw = fwrite(buf, 1, nr, fp);
470 else
471 nw = write(ofd, buf, nr);
472 if (nw != nr) {
473 syslog(LOG_ERR, "%s: %s",
474 path, strerror(nw == 0 ? EIO : errno));
475 syslog(LOG_WARNING,
476 "WARNING: kernel may be incomplete");
477 exit(1);
478 }
479 }
480 if (nr < 0) {
481 syslog(LOG_ERR, "%s: %s",
482 kernel ? kernel : _PATH_UNIX, strerror(errno));
483 syslog(LOG_WARNING,
484 "WARNING: kernel may be incomplete");
485 exit(1);
486 }
487 if (compress)
488 (void)fclose(fp);
489 else
490 (void)close(ofd);
491 }
492
493 char *
494 find_dev(dev, type)
495 register dev_t dev;
496 register int type;
497 {
498 register DIR *dfd;
499 struct dirent *dir;
500 struct stat sb;
501 char *dp, devname[MAXPATHLEN + 1];
502
503 if ((dfd = opendir(_PATH_DEV)) == NULL) {
504 syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno));
505 exit(1);
506 }
507 (void)strcpy(devname, _PATH_DEV);
508 while ((dir = readdir(dfd))) {
509 (void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
510 if (lstat(devname, &sb)) {
511 syslog(LOG_ERR, "%s: %s", devname, strerror(errno));
512 continue;
513 }
514 if ((sb.st_mode & S_IFMT) != type)
515 continue;
516 if (dev == sb.st_rdev) {
517 closedir(dfd);
518 if ((dp = strdup(devname)) == NULL) {
519 syslog(LOG_ERR, "%s", strerror(errno));
520 exit(1);
521 }
522 return (dp);
523 }
524 }
525 closedir(dfd);
526 syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
527 exit(1);
528 }
529
530 char *
531 rawname(s)
532 char *s;
533 {
534 char *sl, name[MAXPATHLEN];
535
536 if ((sl = strrchr(s, '/')) == NULL || sl[1] == '0') {
537 syslog(LOG_ERR,
538 "can't make raw dump device name from %s", s);
539 return (s);
540 }
541 (void)snprintf(name, sizeof(name), "%.*s/r%s", sl - s, s, sl + 1);
542 if ((sl = strdup(name)) == NULL) {
543 syslog(LOG_ERR, "%s", strerror(errno));
544 exit(1);
545 }
546 return (sl);
547 }
548
549 int
550 get_crashtime()
551 {
552 time_t dumptime; /* Time the dump was taken. */
553
554 KREAD(kd_dump, dump_nl[X_TIME].n_value, &dumptime);
555 if (dumptime == 0) {
556 if (verbose)
557 syslog(LOG_ERR, "dump time is zero");
558 return (0);
559 }
560 (void)printf("savecore: system went down at %s", ctime(&dumptime));
561 #define LEEWAY (7 * SECSPERDAY)
562 if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
563 (void)printf("dump time is unreasonable\n");
564 return (0);
565 }
566 return (1);
567 }
568
569 int
570 check_space()
571 {
572 register FILE *fp;
573 char *tkernel;
574 off_t minfree, spacefree, kernelsize, needed;
575 struct stat st;
576 struct statfs fsbuf;
577 char buf[100], path[MAXPATHLEN];
578
579 tkernel = kernel ? kernel : _PATH_UNIX;
580 if (stat(tkernel, &st) < 0) {
581 syslog(LOG_ERR, "%s: %m", tkernel);
582 exit(1);
583 }
584 kernelsize = st.st_blocks * S_BLKSIZE;
585 if (statfs(dirname, &fsbuf) < 0) {
586 syslog(LOG_ERR, "%s: %m", dirname);
587 exit(1);
588 }
589 spacefree = (fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
590
591 (void)snprintf(path, sizeof(path), "%s/minfree", dirname);
592 if ((fp = fopen(path, "r")) == NULL)
593 minfree = 0;
594 else {
595 if (fgets(buf, sizeof(buf), fp) == NULL)
596 minfree = 0;
597 else
598 minfree = atoi(buf);
599 (void)fclose(fp);
600 }
601
602 needed = (dumpsize + kernelsize) / 1024;
603 if (minfree > 0 && spacefree - needed < minfree) {
604 syslog(LOG_WARNING,
605 "no dump, not enough free space on device");
606 return (0);
607 }
608 if (spacefree - needed < minfree)
609 syslog(LOG_WARNING,
610 "dump performed, but free space threshold crossed");
611 return (1);
612 }
613
614 int
615 Open(name, rw)
616 char *name;
617 int rw;
618 {
619 int fd;
620
621 if ((fd = open(name, rw, 0)) < 0) {
622 syslog(LOG_ERR, "%s: %m", name);
623 exit(1);
624 }
625 return (fd);
626 }
627
628 void
629 Lseek(fd, off, flag)
630 int fd, flag;
631 off_t off;
632 {
633 off_t ret;
634
635 ret = lseek(fd, off, flag);
636 if (ret == -1) {
637 syslog(LOG_ERR, "lseek: %m");
638 exit(1);
639 }
640 }
641
642 int
643 Create(file, mode)
644 char *file;
645 int mode;
646 {
647 register int fd;
648
649 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode);
650 if (fd < 0) {
651 syslog(LOG_ERR, "%s: %m", file);
652 exit(1);
653 }
654 return (fd);
655 }
656
657 void
658 Write(fd, bp, size)
659 int fd, size;
660 void *bp;
661 {
662 int n;
663
664 if ((n = write(fd, bp, size)) < size) {
665 syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO));
666 exit(1);
667 }
668 }
669
670 void
671 usage()
672 {
673 (void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory");
674 exit(1);
675 }
676