Fix some x86_64 warnings in the kernel (and remove nowerror).
[dragonfly.git] / sbin / savecore / savecore.c
blob9ddd627042c1e157238766c5c0f2718d42a92385
1 /*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
35 * Copyright (c) 1986, 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
67 #include <sys/cdefs.h>
69 #include <sys/param.h>
70 #include <sys/kerneldump.h>
71 #include <sys/diskslice.h>
72 #include <sys/ioctl.h>
73 #include <sys/param.h>
74 #include <sys/mount.h>
75 #include <sys/stat.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <fstab.h>
79 #include <paths.h>
80 #include <stdarg.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <syslog.h>
85 #include <time.h>
86 #include <unistd.h>
88 /* The size of the buffer used for I/O. */
89 #define BUFFERSIZE (1024*1024)
91 #define STATUS_BAD 0
92 #define STATUS_GOOD 1
93 #define STATUS_UNKNOWN 2
95 static int checkfor, compress, clear, force, keep, verbose; /* flags */
96 static int nfound, nsaved, nerr; /* statistics */
98 extern FILE *zopen(const char *, const char *);
100 static void
101 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
102 int bounds, const int status)
104 uint64_t dumplen;
105 time_t t;
106 const char *stat_str;
108 fprintf(f, "Dump header from device %s\n", device);
109 fprintf(f, " Architecture: %s\n", h->architecture);
110 fprintf(f, " Architecture Version: %u\n",
111 dtoh32(h->architectureversion));
112 dumplen = dtoh64(h->dumplength);
113 fprintf(f, " Dump Length: %lldB (%lld MB)\n", (long long)dumplen,
114 (long long)(dumplen >> 20));
115 fprintf(f, " Blocksize: %d\n", dtoh32(h->blocksize));
116 t = dtoh64(h->dumptime);
117 fprintf(f, " Dumptime: %s", ctime(&t));
118 fprintf(f, " Hostname: %s\n", h->hostname);
119 fprintf(f, " Magic: %s\n", h->magic);
120 fprintf(f, " Version String: %s", h->versionstring);
121 fprintf(f, " Panic String: %s\n", h->panicstring);
122 fprintf(f, " Dump Parity: %u\n", h->parity);
123 fprintf(f, " Bounds: %d\n", bounds);
125 switch(status) {
126 case STATUS_BAD:
127 stat_str = "bad";
128 break;
129 case STATUS_GOOD:
130 stat_str = "good";
131 break;
132 default:
133 stat_str = "unknown";
135 fprintf(f, " Dump Status: %s\n", stat_str);
136 fflush(f);
139 static int
140 getbounds(void) {
141 FILE *fp;
142 char buf[6];
143 int ret;
145 ret = 0;
147 if ((fp = fopen("bounds", "r")) == NULL) {
148 if (verbose)
149 printf("unable to open bounds file, using 0\n");
150 return (ret);
153 if (fgets(buf, sizeof buf, fp) == NULL) {
154 syslog(LOG_WARNING, "unable to read from bounds, using 0");
155 fclose(fp);
156 return (ret);
159 errno = 0;
160 ret = (int)strtol(buf, NULL, 10);
161 if (ret == 0 && (errno == EINVAL || errno == ERANGE))
162 syslog(LOG_WARNING, "invalid value found in bounds, using 0");
163 return (ret);
166 static void
167 writebounds(int bounds) {
168 FILE *fp;
170 if ((fp = fopen("bounds", "w")) == NULL) {
171 syslog(LOG_WARNING, "unable to write to bounds file: %m");
172 return;
175 if (verbose)
176 printf("bounds number: %d\n", bounds);
178 fprintf(fp, "%d\n", bounds);
179 fclose(fp);
183 * Check that sufficient space is available on the disk that holds the
184 * save directory.
186 static int
187 check_space(const char *savedir, off_t dumpsize)
189 FILE *fp;
190 off_t minfree, spacefree, totfree, needed;
191 struct statfs fsbuf;
192 char buf[100], path[MAXPATHLEN];
194 if (statfs(savedir, &fsbuf) < 0) {
195 syslog(LOG_ERR, "%s: %m", savedir);
196 exit(1);
198 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
199 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
201 (void)snprintf(path, sizeof(path), "%s/minfree", savedir);
202 if ((fp = fopen(path, "r")) == NULL)
203 minfree = 0;
204 else {
205 if (fgets(buf, sizeof(buf), fp) == NULL)
206 minfree = 0;
207 else
208 minfree = atoi(buf);
209 (void)fclose(fp);
212 needed = dumpsize / 1024 + 2; /* 2 for info file */
213 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
214 syslog(LOG_WARNING,
215 "no dump, not enough free space on device (%lld available, need %lld)",
216 (long long)(minfree > 0 ? spacefree : totfree),
217 (long long)needed);
218 return (0);
220 if (spacefree - needed < 0)
221 syslog(LOG_WARNING,
222 "dump performed, but free space threshold crossed");
223 return (1);
226 #define BLOCKSIZE (1<<12)
227 #define BLOCKMASK (~(BLOCKSIZE-1))
229 static void
230 DoFile(const char *savedir, const char *device)
232 static char *buf = NULL;
233 struct partinfo dpart;
234 struct kerneldumpheader kdhf, kdhl;
235 off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
236 FILE *info, *fp, *fpkern;
237 mode_t oumask;
238 int fd, fdinfo, fdkernin, error, wl;
239 int nr, nw, hs, he = 0;
240 int bounds, status;
241 u_int sectorsize;
243 bounds = getbounds();
244 dmpcnt = 0;
245 mediasize = 0;
246 status = STATUS_UNKNOWN;
248 if (buf == NULL) {
249 buf = malloc(BUFFERSIZE);
250 if (buf == NULL) {
251 syslog(LOG_ERR, "%m");
252 return;
256 if (verbose)
257 printf("checking for kernel dump on device %s\n", device);
259 fd = open(device, O_RDWR);
260 if (fd < 0) {
261 syslog(LOG_ERR, "%s: %m", device);
262 return;
265 bzero(&dpart, sizeof(dpart));
266 error = ioctl(fd, DIOCGPART, &dpart);
267 if (error) {
268 syslog(LOG_ERR,
269 "couldn't find media and/or sector size of %s: %m", device);
270 goto closefd;
272 mediasize = dpart.media_size;
273 sectorsize = dpart.media_blksize;
275 if (verbose) {
276 printf("mediasize = %lld\n", (long long)mediasize);
277 printf("sectorsize = %u\n", sectorsize);
280 lasthd = mediasize - sectorsize;
281 lseek(fd, lasthd, SEEK_SET);
282 error = read(fd, &kdhl, sizeof kdhl);
283 if (error != sizeof kdhl) {
284 syslog(LOG_ERR,
285 "error reading last dump header at offset %lld in %s: %m",
286 (long long)lasthd, device);
287 goto closefd;
289 if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic)) {
290 if (verbose)
291 printf("magic mismatch on last dump header on %s\n",
292 device);
294 status = STATUS_BAD;
295 if (force == 0)
296 goto closefd;
298 if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
299 sizeof kdhl.magic) == 0) {
300 if (verbose)
301 printf("forcing magic on %s\n", device);
302 memcpy(kdhl.magic, KERNELDUMPMAGIC,
303 sizeof kdhl.magic);
304 } else {
305 syslog(LOG_ERR, "unable to force dump - bad magic");
306 goto closefd;
309 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
310 syslog(LOG_ERR,
311 "unknown version (%d) in last dump header on %s",
312 dtoh32(kdhl.version), device);
314 status = STATUS_BAD;
315 if (force == 0)
316 goto closefd;
319 nfound++;
320 if (clear)
321 goto nuke;
323 if (kerneldump_parity(&kdhl)) {
324 syslog(LOG_ERR,
325 "parity error on last dump header on %s", device);
326 nerr++;
327 status = STATUS_BAD;
328 if (force == 0)
329 goto closefd;
331 dumpsize = dtoh64(kdhl.dumplength);
332 firsthd = lasthd - dumpsize - sizeof kdhf;
333 lseek(fd, firsthd, SEEK_SET);
334 error = read(fd, &kdhf, sizeof kdhf);
335 if (error != sizeof kdhf) {
336 syslog(LOG_ERR,
337 "error reading first dump header at offset %lld in %s: %m",
338 (long long)firsthd, device);
339 nerr++;
340 goto closefd;
343 if (verbose >= 2) {
344 printf("First dump headers:\n");
345 printheader(stdout, &kdhf, device, bounds, -1);
347 printf("\nLast dump headers:\n");
348 printheader(stdout, &kdhl, device, bounds, -1);
349 printf("\n");
352 if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
353 syslog(LOG_ERR,
354 "first and last dump headers disagree on %s", device);
355 nerr++;
356 status = STATUS_BAD;
357 if (force == 0)
358 goto closefd;
359 } else {
360 status = STATUS_GOOD;
363 if (checkfor) {
364 printf("A dump exists on %s\n", device);
365 close(fd);
366 exit(0);
369 if (kdhl.panicstring[0])
370 syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
371 else
372 syslog(LOG_ALERT, "reboot");
374 if (verbose)
375 printf("Checking for available free space\n");
376 if (!check_space(savedir, dumpsize)) {
377 nerr++;
378 goto closefd;
381 writebounds(bounds + 1);
384 * Write kernel file.
386 fdkernin = open(getbootfile(), O_RDONLY, 0);
387 if (fdkernin < 0) {
388 syslog(LOG_ERR, "%s: %m", getbootfile());
391 if (compress) {
392 sprintf(buf, "kern.%d.gz", bounds);
393 fpkern = zopen(buf, "w");
394 } else {
395 sprintf(buf, "kern.%d", bounds);
396 fpkern = fopen(buf, "w");
398 if (fpkern == NULL) {
399 syslog(LOG_ERR, "%s: %m", buf);
400 close(fdkernin);
403 syslog(LOG_NOTICE, "writing %skernel to %s",
404 compress ? "compressed " : "", buf);
406 while ((nr = read(fdkernin, buf, sizeof(buf))) > 0) {
407 nw = fwrite(buf, 1, nr, fpkern);
408 if (nw != nr) {
409 syslog(LOG_ERR, "kern.%d: %m", bounds);
410 syslog(LOG_WARNING,
411 "WARNING: kernel may be incomplete");
412 exit(1);
415 if (nr < 0) {
416 syslog(LOG_ERR, "%s: %m", getbootfile());
417 syslog(LOG_WARNING,
418 "WARNING: kernel may be incomplete");
419 exit(1);
421 fclose(fpkern);
422 close(fdkernin);
425 sprintf(buf, "info.%d", bounds);
428 * Create or overwrite any existing dump header files.
430 fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
431 if (fdinfo < 0) {
432 syslog(LOG_ERR, "%s: %m", buf);
433 nerr++;
434 goto closefd;
436 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
437 if (compress) {
438 sprintf(buf, "vmcore.%d.gz", bounds);
439 fp = zopen(buf, "w");
440 } else {
441 sprintf(buf, "vmcore.%d", bounds);
442 fp = fopen(buf, "w");
444 if (fp == NULL) {
445 syslog(LOG_ERR, "%s: %m", buf);
446 close(fdinfo);
447 nerr++;
448 goto closefd;
450 (void)umask(oumask);
452 info = fdopen(fdinfo, "w");
454 if (info == NULL) {
455 syslog(LOG_ERR, "fdopen failed: %m");
456 nerr++;
457 goto closefd;
460 if (verbose)
461 printheader(stdout, &kdhl, device, bounds, status);
463 printheader(info, &kdhl, device, bounds, status);
464 fclose(info);
466 syslog(LOG_NOTICE, "writing %score to %s",
467 compress ? "compressed " : "", buf);
469 while (dumpsize > 0) {
470 wl = BUFFERSIZE;
471 if (wl > dumpsize)
472 wl = dumpsize;
473 nr = read(fd, buf, wl);
474 if (nr != wl) {
475 if (nr == 0)
476 syslog(LOG_WARNING,
477 "WARNING: EOF on dump device");
478 else
479 syslog(LOG_ERR, "read error on %s: %m", device);
480 nerr++;
481 goto closeall;
483 if (compress) {
484 nw = fwrite(buf, 1, wl, fp);
485 } else {
486 for (nw = 0; nw < nr; nw = he) {
487 /* find a contiguous block of zeroes */
488 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
489 for (he = hs; he < nr && buf[he] == 0;
490 ++he)
491 /* nothing */ ;
492 /* is the hole long enough to matter? */
493 if (he >= hs + BLOCKSIZE)
494 break;
497 /* back down to a block boundary */
498 he &= BLOCKMASK;
501 * 1) Don't go beyond the end of the buffer.
502 * 2) If the end of the buffer is less than
503 * BLOCKSIZE bytes away, we're at the end
504 * of the file, so just grab what's left.
506 if (hs + BLOCKSIZE > nr)
507 hs = he = nr;
510 * At this point, we have a partial ordering:
511 * nw <= hs <= he <= nr
512 * If hs > nw, buf[nw..hs] contains non-zero data.
513 * If he > hs, buf[hs..he] is all zeroes.
515 if (hs > nw)
516 if (fwrite(buf + nw, hs - nw, 1, fp)
517 != 1)
518 break;
519 if (he > hs)
520 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
521 break;
524 if (nw != wl) {
525 syslog(LOG_ERR,
526 "write error on vmcore.%d file: %m", bounds);
527 syslog(LOG_WARNING,
528 "WARNING: vmcore may be incomplete");
529 nerr++;
530 goto closeall;
532 if (verbose) {
533 dmpcnt += wl;
534 printf("%llu\r", (unsigned long long)dmpcnt);
535 fflush(stdout);
537 dumpsize -= wl;
539 if (verbose)
540 printf("\n");
542 if (fclose(fp) < 0) {
543 syslog(LOG_ERR, "error on vmcore.%d: %m", bounds);
544 nerr++;
545 goto closeall;
547 nsaved++;
549 if (verbose)
550 printf("dump saved\n");
552 nuke:
553 if (clear || !keep) {
554 if (verbose)
555 printf("clearing dump header\n");
556 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
557 lseek(fd, lasthd, SEEK_SET);
558 error = write(fd, &kdhl, sizeof kdhl);
559 if (error != sizeof kdhl)
560 syslog(LOG_ERR,
561 "error while clearing the dump header: %m");
563 close(fd);
564 return;
566 closeall:
567 fclose(fp);
569 closefd:
570 close(fd);
573 static void
574 usage(void)
576 fprintf(stderr, "%s\n%s\n%s\n",
577 "usage: savecore -c",
578 " savecore -C [-v] [directory device]",
579 " savecore [-fkvz] [directory [device ...]]");
580 exit (1);
584 main(int argc, char **argv)
586 const char *savedir = ".";
587 struct fstab *fsp;
588 int i, ch, error;
590 checkfor = compress = clear = force = keep = verbose = 0;
591 nfound = nsaved = nerr = 0;
593 openlog("savecore", LOG_PERROR, LOG_DAEMON);
595 while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
596 switch(ch) {
597 case 'C':
598 checkfor = 1;
599 break;
600 case 'c':
601 clear = 1;
602 break;
603 case 'k':
604 keep = 1;
605 break;
606 case 'v':
607 verbose++;
608 break;
609 case 'f':
610 force = 1;
611 break;
612 case 'z':
613 compress = 1;
614 break;
615 case '?':
616 default:
617 usage();
619 if (checkfor && (clear || force || keep))
620 usage();
621 argc -= optind;
622 argv += optind;
623 if (argc >= 1) {
624 error = chdir(argv[0]);
625 if (error) {
626 syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
627 exit(1);
629 savedir = argv[0];
630 argc--;
631 argv++;
633 if (argc == 0) {
634 for (;;) {
635 fsp = getfsent();
636 if (fsp == NULL)
637 break;
638 if (strcmp(fsp->fs_vfstype, "swap") &&
639 strcmp(fsp->fs_vfstype, "dump"))
640 continue;
641 DoFile(savedir, fsp->fs_spec);
643 } else {
644 for (i = 0; i < argc; i++)
645 DoFile(savedir, argv[i]);
648 /* Emit minimal output. */
649 if (nfound == 0) {
650 if (checkfor) {
651 printf("No dump exists\n");
652 exit(1);
654 syslog(LOG_WARNING, "no dumps found");
656 else if (nsaved == 0) {
657 if (nerr != 0)
658 syslog(LOG_WARNING, "unsaved dumps found but not saved");
659 else
660 syslog(LOG_WARNING, "no unsaved dumps found");
663 return (0);