CVS commit: src/sbin/savecore
Module Name:src Committed By: maxv Date: Fri Apr 3 19:09:43 UTC 2020 Modified Files: src/sbin/savecore: savecore.c Log Message: Avoid overflows when reading strings. To generate a diff of this commit: cvs rdiff -u -r1.89 -r1.90 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.89 src/sbin/savecore/savecore.c:1.90 --- src/sbin/savecore/savecore.c:1.89 Wed Nov 6 07:29:08 2019 +++ src/sbin/savecore/savecore.c Fri Apr 3 19:09:43 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.89 2019/11/06 07:29:08 mrg Exp $ */ +/* $NetBSD: savecore.c,v 1.90 2020/04/03 19:09:43 maxv Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1986, 19 #if 0 static char sccsid[] = "@(#)savecore.c 8.5 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: savecore.c,v 1.89 2019/11/06 07:29:08 mrg Exp $"); +__RCSID("$NetBSD: savecore.c,v 1.90 2020/04/03 19:09:43 maxv Exp $"); #endif #endif /* not lint */ @@ -275,6 +275,20 @@ main(int argc, char *argv[]) } static void +read_string(kvm_t *kd, u_long kva, char *buf, size_t size) +{ + size_t i; + + for (i = 0; i < size - 1; i++) { + (void)kvm_read(kd, kva + i, buf + i, 1); + if (buf[i] == '\0') + return; + } + + buf[size - 1] = '\0'; +} + +static void kmem_setup(int verbose) { long l_dumplo; @@ -331,9 +345,8 @@ kmem_setup(int verbose) (long long)dumplo, (long)(dumplo / DEV_BSIZE), (long)DEV_BSIZE); KREAD_LOGWARN(kd_kern, current_nl[X_DUMPMAG].n_value, dumpmag, exit(1)); - (void)kvm_read(kd_kern, current_nl[X_VERSION].n_value, vers, + read_string(kd_kern, current_nl[X_VERSION].n_value, vers, sizeof(vers)); - vers[sizeof(vers) - 1] = '\0'; if (current_nl[X_DUMPCDEV].n_value != 0) { KREAD_LOGWARN(kd_kern, current_nl[X_DUMPCDEV].n_value, dumpcdev, @@ -395,9 +408,8 @@ check_kmem(void) long panicloc, panicstart, panicend; char core_vers[1024]; - (void)kvm_read(kd_dump, dump_nl[X_VERSION].n_value, core_vers, + read_string(kd_dump, dump_nl[X_VERSION].n_value, core_vers, sizeof(core_vers)); - core_vers[sizeof(core_vers) - 1] = '\0'; if (strcmp(vers, core_vers) != 0) syslog(LOG_WARNING,
CVS commit: src/sbin/savecore
Module Name:src Committed By: mrg Date: Thu Dec 27 21:25:46 UTC 2018 Modified Files: src/sbin/savecore: savecore.c Log Message: avoid infinite loop in kmem_check(). fixes bug introduced in previous. To generate a diff of this commit: cvs rdiff -u -r1.87 -r1.88 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.87 src/sbin/savecore/savecore.c:1.88 --- src/sbin/savecore/savecore.c:1.87 Tue Nov 6 04:07:22 2018 +++ src/sbin/savecore/savecore.c Thu Dec 27 21:25:46 2018 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.87 2018/11/06 04:07:22 mrg Exp $ */ +/* $NetBSD: savecore.c,v 1.88 2018/12/27 21:25:46 mrg Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1986, 19 #if 0 static char sccsid[] = "@(#)savecore.c 8.5 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: savecore.c,v 1.87 2018/11/06 04:07:22 mrg Exp $"); +__RCSID("$NetBSD: savecore.c,v 1.88 2018/12/27 21:25:46 mrg Exp $"); #endif #endif /* not lint */ @@ -446,7 +446,7 @@ check_kmem(void) } nomsguf: KREAD_LOGWARN(kd_dump, dump_nl[X_PANICSTR].n_value, panicstr, - goto nomsguf); + return); if (panicstr) { cp = panic_mesg; panicloc = panicstr;
CVS commit: src/sbin/savecore
Module Name:src Committed By: mrg Date: Tue Nov 6 04:07:22 UTC 2018 Modified Files: src/sbin/savecore: savecore.c Log Message: put a bunch of the kvm_read + warn on failure code into a macro that describes more about what failed. now errors tell you which actual variable was being requested instead of simply saying "not yours". tested on amd64 as working. written for arm64 testing. To generate a diff of this commit: cvs rdiff -u -r1.86 -r1.87 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.86 src/sbin/savecore/savecore.c:1.87 --- src/sbin/savecore/savecore.c:1.86 Mon May 13 18:44:11 2013 +++ src/sbin/savecore/savecore.c Tue Nov 6 04:07:22 2018 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.86 2013/05/13 18:44:11 christos Exp $ */ +/* $NetBSD: savecore.c,v 1.87 2018/11/06 04:07:22 mrg Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1986, 19 #if 0 static char sccsid[] = "@(#)savecore.c 8.5 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: savecore.c,v 1.86 2013/05/13 18:44:11 christos Exp $"); +__RCSID("$NetBSD: savecore.c,v 1.87 2018/11/06 04:07:22 mrg Exp $"); #endif #endif /* not lint */ @@ -73,8 +73,20 @@ __RCSID("$NetBSD: savecore.c,v 1.86 2013 extern FILE *zopen(const char *fname, const char *mode); +/* + * Note that KREAD_LOGWARN takes a variable name, not pointer to it, unlike + * KREAD() itself. + */ #define KREAD(kd, addr, p)\ (kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p))) +#define KREAD_LOGWARN(kd, addr, p, err) \ +do { \ + if (KREAD(kd, addr, &(p)) != 0) {\ + syslog(LOG_WARNING, "%s:%d: kvm_read " #p ": %s", \ + __func__, __LINE__, kvm_geterr(kd)); \ + err; \ + }\ +} while (0) static struct nlist current_nl[] = { /* Namelist for currently running system. */ #define X_DUMPDEV 0 @@ -259,6 +271,7 @@ main(int argc, char *argv[]) static void kmem_setup(int verbose) { + long l_dumplo; kvm_t *kd_kern; char errbuf[_POSIX2_LINE_MAX]; int i, hdrsz; @@ -295,47 +308,30 @@ kmem_setup(int verbose) } } - if (KREAD(kd_kern, current_nl[X_DUMPDEV].n_value, ) != 0) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_kern)); - exit(1); - } + KREAD_LOGWARN(kd_kern, current_nl[X_DUMPDEV].n_value, dumpdev, exit(1)); if (dumpdev == NODEV) { syslog(LOG_WARNING, "no core dump (no dumpdev)"); exit(1); } - { - long l_dumplo; - - if (KREAD(kd_kern, current_nl[X_DUMPLO].n_value, _dumplo) != 0) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_kern)); - exit(1); - } - if (l_dumplo == -1) { + KREAD_LOGWARN(kd_kern, current_nl[X_DUMPLO].n_value, l_dumplo, exit(1)); + if (l_dumplo == -1) { syslog(LOG_WARNING, "no core dump (invalid dumplo)"); exit(1); - } - dumplo = DEV_BSIZE * (off_t) l_dumplo; } + dumplo = DEV_BSIZE * (off_t) l_dumplo; if (verbose) (void)printf("dumplo = %lld (%ld * %ld)\n", (long long)dumplo, (long)(dumplo / DEV_BSIZE), (long)DEV_BSIZE); - if (KREAD(kd_kern, current_nl[X_DUMPMAG].n_value, ) != 0) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_kern)); - exit(1); - } + KREAD_LOGWARN(kd_kern, current_nl[X_DUMPMAG].n_value, dumpmag, exit(1)); (void)kvm_read(kd_kern, current_nl[X_VERSION].n_value, vers, sizeof(vers)); vers[sizeof(vers) - 1] = '\0'; if (current_nl[X_DUMPCDEV].n_value != 0) { - if (KREAD(kd_kern, current_nl[X_DUMPCDEV].n_value, - ) != 0) { - syslog(LOG_WARNING, "kvm_read: %s", - kvm_geterr(kd_kern)); - exit(1); - } + KREAD_LOGWARN(kd_kern, current_nl[X_DUMPCDEV].n_value, dumpcdev, + exit(1)); ddname = find_dev(dumpcdev, S_IFCHR); } else ddname = find_dev(dumpdev, S_IFBLK); @@ -403,27 +399,20 @@ check_kmem(void) kvm_getkernelname(kd_dump), vers, core_vers); panicstart = panicend = 0; - if (KREAD(kd_dump, dump_nl[X_PANICSTART].n_value, ) != 0) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_dump)); - goto nomsguf; - } - if (KREAD(kd_dump, dump_nl[X_PANICEND].n_value, ) != 0) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_dump)); - goto nomsguf; - } + KREAD_LOGWARN(kd_dump, dump_nl[X_PANICSTART].n_value, panicstart, + goto nomsguf); + KREAD_LOGWARN(kd_dump, dump_nl[X_PANICEND].n_value, panicend, + goto nomsguf); + if (panicstart != 0 && panicend != 0) { - if (KREAD(kd_dump, dump_nl[X_MSGBUF].n_value, )) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_dump)); - goto nomsguf; - } - if (kvm_read(kd_dump, (long)bufp, , - offsetof(struct kern_msgbuf, msg_bufc)) != - offsetof(struct kern_msgbuf, msg_bufc)) { - syslog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kd_dump)); - goto nomsguf; - } + KREAD_LOGWARN(kd_dump, dump_nl[X_MSGBUF].n_value, bufp, + goto nomsguf); +
CVS commit: src/sbin/savecore
Module Name:src Committed By: kamil Date: Tue Jan 10 04:42:20 UTC 2017 Modified Files: src/sbin/savecore: zopen.c Log Message: Adapt funopen(3) call after switch to new zlib(3) In the prototype of the gzclose() function of changed from: typedef struct gzFile_s *gzFile;/* semi-opaque gzip file descriptor */ to: typedef voidp gzFile; This caused type mismatch when calling funopen(3) as: 'int (*)(void *)' != 'int (*)(struct gzFile_s *)' Cast gzclose to (int (*)(void *)) when used in funopen(3). To generate a diff of this commit: cvs rdiff -u -r1.3 -r1.4 src/sbin/savecore/zopen.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/zopen.c diff -u src/sbin/savecore/zopen.c:1.3 src/sbin/savecore/zopen.c:1.4 --- src/sbin/savecore/zopen.c:1.3 Tue Dec 25 09:24:45 2012 +++ src/sbin/savecore/zopen.c Tue Jan 10 04:42:20 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: zopen.c,v 1.3 2012/12/25 09:24:45 mbalmer Exp $ */ +/* $NetBSD: zopen.c,v 1.4 2017/01/10 04:42:20 kamil Exp $ */ /* * Public domain stdio wrapper for libz, written by Johan Danielsson. @@ -6,7 +6,7 @@ #include #ifndef lint -__RCSID("$NetBSD: zopen.c,v 1.3 2012/12/25 09:24:45 mbalmer Exp $"); +__RCSID("$NetBSD: zopen.c,v 1.4 2017/01/10 04:42:20 kamil Exp $"); #endif #include @@ -35,7 +35,7 @@ zopen(const char *fname, const char *mod return NULL; if(*mode == 'r') - return funopen(gz, xgzread, NULL, NULL, gzclose); + return funopen(gz, xgzread, NULL, NULL, (int (*)(void *))gzclose); else - return funopen(gz, NULL, xgzwrite, NULL, gzclose); + return funopen(gz, NULL, xgzwrite, NULL, (int (*)(void *))gzclose); }
CVS commit: src/sbin/savecore
Module Name:src Committed By: christos Date: Mon May 13 18:44:11 UTC 2013 Modified Files: src/sbin/savecore: savecore.c Log Message: - avoid using globals where it is simple. - simplify: only use stdio for output. To generate a diff of this commit: cvs rdiff -u -r1.85 -r1.86 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.85 src/sbin/savecore/savecore.c:1.86 --- src/sbin/savecore/savecore.c:1.85 Sat Apr 7 12:44:10 2012 +++ src/sbin/savecore/savecore.c Mon May 13 14:44:11 2013 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.85 2012/04/07 16:44:10 christos Exp $ */ +/* $NetBSD: savecore.c,v 1.86 2013/05/13 18:44:11 christos Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT(@(#) Copyright (c) 1986, 19 #if 0 static char sccsid[] = @(#)savecore.c 8.5 (Berkeley) 4/28/95; #else -__RCSID($NetBSD: savecore.c,v 1.85 2012/04/07 16:44:10 christos Exp $); +__RCSID($NetBSD: savecore.c,v 1.86 2013/05/13 18:44:11 christos Exp $); #endif #endif /* not lint */ @@ -154,30 +154,32 @@ static long panicstr; static char vers[1024]; static char gzmode[3]; -static int clear, compress, force, verbose; /* flags */ - static void check_kmem(void); static int check_space(void); static void clear_dump(void); static int Create(char *, int); -static int dump_exists(void); +static int dump_exists(int); static char *find_dev(dev_t, mode_t); static int get_crashtime(void); -static void kmem_setup(void); +static void kmem_setup(int); static void Lseek(int, off_t, int); static int Open(const char *, int rw); -static void save_core(void); +static void save_core(int); __dead static void usage(const char *fmt, ...) __printflike(1, 2); int main(int argc, char *argv[]) { - int ch, level, testonly; + int ch, level, testonly, compress, force, clear, verbose; char *ep; kernel = NULL; level = 1; /* default to fastest gzip compression */ + force = 0; + clear = 0; testonly = 0; + verbose = 0; + compress = 0; gzmode[0] = 'w'; openlog(savecore, LOG_PERROR, LOG_DAEMON); @@ -224,14 +226,14 @@ main(int argc, char *argv[]) gzmode[1] = level + '0'; (void)time(now); - kmem_setup(); + kmem_setup(verbose); if (clear !testonly) { clear_dump(); exit(0); } - if (!dump_exists() !force) + if (!dump_exists(verbose) !force) exit(1); if (testonly) @@ -248,14 +250,14 @@ main(int argc, char *argv[]) if ((!get_crashtime() || !check_space()) !force) exit(1); - save_core(); + save_core(compress); clear_dump(); exit(0); } static void -kmem_setup(void) +kmem_setup(int verbose) { kvm_t *kd_kern; char errbuf[_POSIX2_LINE_MAX]; @@ -473,7 +475,7 @@ nomsguf: } static int -dump_exists(void) +dump_exists(int verbose) { u_int32_t newdumpmag; @@ -516,16 +518,13 @@ clear_dump(void) static char buf[1024 * 1024]; static void -save_kernel(int ofd, FILE *fp, char *path) +save_kernel(FILE *fp, char *path) { int nw, nr, ifd; ifd = Open(kernel, O_RDONLY); while ((nr = read(ifd, buf, sizeof(buf))) 0) { - if (compress) - nw = fwrite(buf, 1, nr, fp); - else - nw = write(ofd, buf, nr); + nw = fwrite(buf, 1, nr, fp); if (nw != nr) { syslog(LOG_ERR, %s: %s, path, strerror(nw == 0 ? EIO : errno)); @@ -553,7 +552,7 @@ ksymsget(u_long addr, void *ptr, size_t } static int -save_ksyms(int ofd, FILE *fp, char *path) +save_ksyms(FILE *fp, char *path) { struct ksyms_hdr khdr; int nw, symsz, strsz; @@ -579,10 +578,7 @@ save_ksyms(int ofd, FILE *fp, char *path khdr.kh_shdr[STRTAB].sh_size = strsz; /* Write out the ELF headers. */ - if (compress) - nw = fwrite(khdr, 1, sizeof(khdr), fp); - else - nw = write(ofd, khdr, sizeof(khdr)); + nw = fwrite(khdr, 1, sizeof(khdr), fp); if (nw != sizeof(khdr)) { syslog(LOG_ERR, %s: %s, path, strerror(nw == 0 ? EIO : errno)); @@ -605,10 +601,7 @@ save_ksyms(int ofd, FILE *fp, char *path free(p); return 1; } - if (compress) - nw = fwrite(p, 1, st.sd_symsize, fp); - else - nw = write(ofd, p, st.sd_symsize); + nw = fwrite(p, 1, st.sd_symsize, fp); free(p); if (nw != st.sd_symsize) { syslog(LOG_ERR, %s: %s, @@ -633,10 +626,7 @@ save_ksyms(int ofd, FILE *fp, char *path free(p); return 1; } - if (compress) - nw = fwrite(p, 1, st.sd_strsize, fp); - else - nw = write(ofd, p, st.sd_strsize); + nw = fwrite(p, 1, st.sd_strsize, fp); free(p); if (nw != st.sd_strsize) { syslog(LOG_ERR, %s: %s, @@ -651,7 +641,7 @@ save_ksyms(int ofd, FILE *fp, char *path } static void -save_core(void) +save_core(int compress) { FILE *fp; int bounds, ifd, nr, nw, ofd, tryksyms; @@ -768,25 +758,25 @@ err2: syslog(LOG_WARNING, syslog(LOG_ERR, %s: %m, path); exit(1); } - } else + } else { ofd = Create(path, S_IRUSR |
CVS commit: src/sbin/savecore
Module Name:src Committed By: mbalmer Date: Tue Dec 25 09:24:45 UTC 2012 Modified Files: src/sbin/savecore: zopen.c Log Message: Consistent use of return. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/sbin/savecore/zopen.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/zopen.c diff -u src/sbin/savecore/zopen.c:1.2 src/sbin/savecore/zopen.c:1.3 --- src/sbin/savecore/zopen.c:1.2 Mon Jun 27 01:00:06 2005 +++ src/sbin/savecore/zopen.c Tue Dec 25 09:24:45 2012 @@ -1,4 +1,4 @@ -/* $NetBSD: zopen.c,v 1.2 2005/06/27 01:00:06 christos Exp $ */ +/* $NetBSD: zopen.c,v 1.3 2012/12/25 09:24:45 mbalmer Exp $ */ /* * Public domain stdio wrapper for libz, written by Johan Danielsson. @@ -6,7 +6,7 @@ #include sys/cdefs.h #ifndef lint -__RCSID($NetBSD: zopen.c,v 1.2 2005/06/27 01:00:06 christos Exp $); +__RCSID($NetBSD: zopen.c,v 1.3 2012/12/25 09:24:45 mbalmer Exp $); #endif #include stdio.h @@ -35,7 +35,7 @@ zopen(const char *fname, const char *mod return NULL; if(*mode == 'r') - return (funopen(gz, xgzread, NULL, NULL, gzclose)); + return funopen(gz, xgzread, NULL, NULL, gzclose); else - return (funopen(gz, NULL, xgzwrite, NULL, gzclose)); + return funopen(gz, NULL, xgzwrite, NULL, gzclose); }
CVS commit: src/sbin/savecore
Module Name:src Committed By: wiz Date: Tue Sep 13 08:54:11 UTC 2011 Modified Files: src/sbin/savecore: savecore.8 Log Message: Fix typo. To generate a diff of this commit: cvs rdiff -u -r1.35 -r1.36 src/sbin/savecore/savecore.8 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.8 diff -u src/sbin/savecore/savecore.8:1.35 src/sbin/savecore/savecore.8:1.36 --- src/sbin/savecore/savecore.8:1.35 Mon Sep 12 21:22:27 2011 +++ src/sbin/savecore/savecore.8 Tue Sep 13 08:54:11 2011 @@ -1,4 +1,4 @@ -.\ $NetBSD: savecore.8,v 1.35 2011/09/12 21:22:27 christos Exp $ +.\ $NetBSD: savecore.8,v 1.36 2011/09/13 08:54:11 wiz Exp $ .\ .\ Copyright (c) 1980, 1991, 1993 .\ The Regents of the University of California. All rights reserved. @@ -66,7 +66,7 @@ .Fa directory , and enters a reboot message and information about the core dump into the system log. -If a directory is not specified, than +If a directory is not specified, then .Pa /var/crash is used. .Pp
CVS commit: src/sbin/savecore
Module Name:src Committed By: christos Date: Tue Sep 13 19:55:28 UTC 2011 Modified Files: src/sbin/savecore: savecore.c Log Message: print the missing flag if missing argument... To generate a diff of this commit: cvs rdiff -u -r1.83 -r1.84 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.83 src/sbin/savecore/savecore.c:1.84 --- src/sbin/savecore/savecore.c:1.83 Mon Sep 12 17:22:07 2011 +++ src/sbin/savecore/savecore.c Tue Sep 13 15:55:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.83 2011/09/12 21:22:07 christos Exp $ */ +/* $NetBSD: savecore.c,v 1.84 2011/09/13 19:55:28 christos Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ #if 0 static char sccsid[] = @(#)savecore.c 8.5 (Berkeley) 4/28/95; #else -__RCSID($NetBSD: savecore.c,v 1.83 2011/09/12 21:22:07 christos Exp $); +__RCSID($NetBSD: savecore.c,v 1.84 2011/09/13 19:55:28 christos Exp $); #endif #endif /* not lint */ @@ -210,6 +210,7 @@ usage(Invalid compression `%s', optarg); break; case '?': + usage(Missing argument for flag `%c', optopt); default: usage(Unknown flag `%c', ch); }
CVS commit: src/sbin/savecore
Module Name:src Committed By: christos Date: Mon Sep 12 21:22:08 UTC 2011 Modified Files: src/sbin/savecore: savecore.c Log Message: - always print corrupted dump messages instead of silently failing - don't require /var/crash to be specified, default to it - determine and print the kernel name - print all messages to syslog To generate a diff of this commit: cvs rdiff -u -r1.82 -r1.83 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.82 src/sbin/savecore/savecore.c:1.83 --- src/sbin/savecore/savecore.c:1.82 Mon Aug 29 10:30:38 2011 +++ src/sbin/savecore/savecore.c Mon Sep 12 17:22:07 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.82 2011/08/29 14:30:38 joerg Exp $ */ +/* $NetBSD: savecore.c,v 1.83 2011/09/12 21:22:07 christos Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ #if 0 static char sccsid[] = @(#)savecore.c 8.5 (Berkeley) 4/28/95; #else -__RCSID($NetBSD: savecore.c,v 1.82 2011/08/29 14:30:38 joerg Exp $); +__RCSID($NetBSD: savecore.c,v 1.83 2011/09/12 21:22:07 christos Exp $); #endif #endif /* not lint */ @@ -68,6 +68,7 @@ #include unistd.h #include util.h #include limits.h +#include stdarg.h #include kvm.h extern FILE *zopen(const char *fname, const char *mode); @@ -141,7 +142,7 @@ static off_t dumpbytes; /* in bytes */ static const char *kernel; /* name of used kernel */ -static char *dirname; /* directory to save dumps in */ +static const char *dirname; /* directory to save dumps in */ static char *ddname; /* name of dump device */ static dev_t dumpdev; /* dump device */ static dev_t dumpcdev = NODEV; /* dump device (char equivalent) */ @@ -167,7 +168,7 @@ static int Open(const char *, int rw); static char *rawname(char *s); static void save_core(void); -__dead static void usage(void); +__dead static void usage(const char *fmt, ...) __printflike(1, 2); int main(int argc, char *argv[]) @@ -175,7 +176,6 @@ int ch, level, testonly; char *ep; - dirname = NULL; kernel = NULL; level = 1; /* default to fastest gzip compression */ testonly = 0; @@ -206,25 +206,22 @@ break; case 'Z': level = (int)strtol(optarg, ep, 10); - if (level 0 || level 9) { -(void)syslog(LOG_ERR, invalid compression %s, -optarg); -usage(); - } + if (level 0 || level 9) +usage(Invalid compression `%s', optarg); break; case '?': default: - usage(); + usage(Unknown flag `%c', ch); } argc -= optind; argv += optind; - if (argc != ((clear || testonly) ? 0 : 1)) - usage(); + if (argc != 0) + dirname = argv[0]; + else + dirname = /var/crash; gzmode[1] = level + '0'; - if (!clear) - dirname = argv[0]; (void)time(now); kmem_setup(); @@ -297,8 +294,7 @@ } if (KREAD(kd_kern, current_nl[X_DUMPDEV].n_value, dumpdev) != 0) { - if (verbose) - syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_kern)); + syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_kern)); exit(1); } if (dumpdev == NODEV) { @@ -309,8 +305,7 @@ long l_dumplo; if (KREAD(kd_kern, current_nl[X_DUMPLO].n_value, l_dumplo) != 0) { - if (verbose) - syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_kern)); + syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_kern)); exit(1); } if (l_dumplo == -1) { @@ -324,8 +319,7 @@ (void)printf(dumplo = %lld (%ld * %ld)\n, (long long)dumplo, (long)(dumplo / DEV_BSIZE), (long)DEV_BSIZE); if (KREAD(kd_kern, current_nl[X_DUMPMAG].n_value, dumpmag) != 0) { - if (verbose) - syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_kern)); + syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_kern)); exit(1); } @@ -336,9 +330,8 @@ if (current_nl[X_DUMPCDEV].n_value != 0) { if (KREAD(kd_kern, current_nl[X_DUMPCDEV].n_value, dumpcdev) != 0) { - if (verbose) -syslog(LOG_WARNING, kvm_read: %s, - kvm_geterr(kd_kern)); + syslog(LOG_WARNING, kvm_read: %s, + kvm_geterr(kd_kern)); exit(1); } ddname = find_dev(dumpcdev, S_IFCHR); @@ -405,47 +398,40 @@ if (strcmp(vers, core_vers) != 0) syslog(LOG_WARNING, warning: %s version mismatch:\n\t%s\nand\t%s\n, - kernel, vers, core_vers); + kvm_getkernelname(kd_dump), vers, core_vers); panicstart = panicend = 0; if (KREAD(kd_dump, dump_nl[X_PANICSTART].n_value, panicstart) != 0) { - if (verbose) - syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_dump)); + syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_dump)); goto nomsguf; } if (KREAD(kd_dump, dump_nl[X_PANICEND].n_value, panicend) != 0) { - if (verbose) - syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_dump)); + syslog(LOG_WARNING, kvm_read: %s, kvm_geterr(kd_dump)); goto nomsguf; } if (panicstart != 0 panicend != 0) { if (KREAD(kd_dump,
CVS commit: src/sbin/savecore
Module Name:src Committed By: christos Date: Mon Sep 12 21:22:27 UTC 2011 Modified Files: src/sbin/savecore: savecore.8 Log Message: document that dumpdir is now optional To generate a diff of this commit: cvs rdiff -u -r1.34 -r1.35 src/sbin/savecore/savecore.8 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.8 diff -u src/sbin/savecore/savecore.8:1.34 src/sbin/savecore/savecore.8:1.35 --- src/sbin/savecore/savecore.8:1.34 Sat Mar 7 17:08:08 2009 +++ src/sbin/savecore/savecore.8 Mon Sep 12 17:22:27 2011 @@ -1,4 +1,4 @@ -.\ $NetBSD: savecore.8,v 1.34 2009/03/07 22:08:08 ad Exp $ +.\ $NetBSD: savecore.8,v 1.35 2011/09/12 21:22:27 christos Exp $ .\ .\ Copyright (c) 1980, 1991, 1993 .\ The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\ .\ @(#)savecore.8 8.1 (Berkeley) 6/5/93 .\ -.Dd March 3, 2009 +.Dd September 13, 2011 .Dt SAVECORE 8 .Os .Sh NAME @@ -40,7 +40,7 @@ .Op Fl fvz .Op Fl N Ar system .Op Fl Z Ar level -.Ar directory +.Op Ar directory .Nm .Fl c .Op Fl v @@ -66,6 +66,9 @@ .Fa directory , and enters a reboot message and information about the core dump into the system log. +If a directory is not specified, than +.Pa /var/crash +is used. .Pp The kernel and core file can then be analyzed using various tools, including
CVS commit: src/sbin/savecore
Module Name:src Committed By: joerg Date: Mon Aug 29 14:30:38 UTC 2011 Modified Files: src/sbin/savecore: savecore.c Log Message: static + __dead To generate a diff of this commit: cvs rdiff -u -r1.81 -r1.82 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.81 src/sbin/savecore/savecore.c:1.82 --- src/sbin/savecore/savecore.c:1.81 Tue Aug 18 04:02:39 2009 +++ src/sbin/savecore/savecore.c Mon Aug 29 14:30:38 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.81 2009/08/18 04:02:39 dogcow Exp $ */ +/* $NetBSD: savecore.c,v 1.82 2011/08/29 14:30:38 joerg Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ #if 0 static char sccsid[] = @(#)savecore.c 8.5 (Berkeley) 4/28/95; #else -__RCSID($NetBSD: savecore.c,v 1.81 2009/08/18 04:02:39 dogcow Exp $); +__RCSID($NetBSD: savecore.c,v 1.82 2011/08/29 14:30:38 joerg Exp $); #endif #endif /* not lint */ @@ -75,7 +75,7 @@ #define KREAD(kd, addr, p)\ (kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p))) -struct nlist current_nl[] = { /* Namelist for currently running system. */ +static struct nlist current_nl[] = { /* Namelist for currently running system. */ #define X_DUMPDEV 0 { .n_name = _dumpdev }, #define X_DUMPLO 1 @@ -110,11 +110,11 @@ { .n_name = _ksyms_symtabs }, { .n_name = NULL }, }; -int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, X_DUMPCDEV, -1 }; -int dumpsyms[] = { X_TIME_SECOND, X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, +static int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, X_DUMPCDEV, -1 }; +static int dumpsyms[] = { X_TIME_SECOND, X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, X_SYMSZ, X_STRSZ, X_KHDR, X_SYMTABS, -1 }; -struct nlist dump_nl[] = { /* Name list for dumped system. */ +static struct nlist dump_nl[] = { /* Name list for dumped system. */ { .n_name = _dumpdev }, /* Entries MUST be the same as */ { .n_name = _dumplo }, /* those in current_nl[]. */ { .n_name = _time_second }, @@ -135,41 +135,39 @@ }; /* Types match kernel declarations. */ -off_t dumplo;/* where dump starts on dumpdev */ -u_int32_t dumpmag; /* magic number in dump */ -int dumpsize; /* amount of memory dumped */ -off_t dumpbytes; /* in bytes */ - -const char *kernel; /* name of used kernel */ -char *dirname; /* directory to save dumps in */ -char *ddname; /* name of dump device */ -dev_t dumpdev; /* dump device */ -dev_t dumpcdev = NODEV; /* dump device (char equivalent) */ -int dumpfd;/* read/write descriptor on dev */ -kvm_t *kd_dump; /* kvm descriptor on dev */ -time_t now;/* current date */ -char panic_mesg[1024]; -long panicstr; -char vers[1024]; -char gzmode[3]; +static off_t dumplo;/* where dump starts on dumpdev */ +static u_int32_t dumpmag; /* magic number in dump */ +static int dumpsize; /* amount of memory dumped */ +static off_t dumpbytes; /* in bytes */ + +static const char *kernel; /* name of used kernel */ +static char *dirname; /* directory to save dumps in */ +static char *ddname; /* name of dump device */ +static dev_t dumpdev; /* dump device */ +static dev_t dumpcdev = NODEV; /* dump device (char equivalent) */ +static int dumpfd;/* read/write descriptor on dev */ +static kvm_t *kd_dump; /* kvm descriptor on dev */ +static time_t now;/* current date */ +static char panic_mesg[1024]; +static long panicstr; +static char vers[1024]; +static char gzmode[3]; static int clear, compress, force, verbose; /* flags */ -void check_kmem(void); -int check_space(void); -void clear_dump(void); -int Create(char *, int); -int dump_exists(void); -char *find_dev(dev_t, mode_t); -int get_crashtime(void); -void kmem_setup(void); -void Lseek(int, off_t, int); -int main(int, char *[]); -int Open(const char *, int rw); -char *rawname(char *s); -void save_core(void); -void usage(void); -void Write(int, void *, int); +static void check_kmem(void); +static int check_space(void); +static void clear_dump(void); +static int Create(char *, int); +static int dump_exists(void); +static char *find_dev(dev_t, mode_t); +static int get_crashtime(void); +static void kmem_setup(void); +static void Lseek(int, off_t, int); +static int Open(const char *, int rw); +static char *rawname(char *s); +static void save_core(void); +__dead static void usage(void); int main(int argc, char *argv[]) @@ -259,7 +257,7 @@ exit(0); } -void +static void kmem_setup(void) { kvm_t *kd_kern; @@ -392,7 +390,7 @@ kvm_close(kd_kern); } -void +static void check_kmem(void) { char *cp, *bufdata; @@ -490,7 +488,7 @@ } } -int +static int dump_exists(void) { u_int32_t newdumpmag; @@ -525,7 +523,7 @@ return (1); } -void +static void clear_dump(void) { if (kvm_dump_inval(kd_dump) == -1) @@ -534,7 +532,7 @@ } -char
CVS commit: src/sbin/savecore
Module Name:src Committed By: dogcow Date: Tue Aug 18 04:02:40 UTC 2009 Modified Files: src/sbin/savecore: savecore.c Log Message: Instead of exiting with an obscure error message if -N /kernelname isn't specified, blithely assume the kernel will consume around 20 megs. To generate a diff of this commit: cvs rdiff -u -r1.80 -r1.81 src/sbin/savecore/savecore.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sbin/savecore/savecore.c diff -u src/sbin/savecore/savecore.c:1.80 src/sbin/savecore/savecore.c:1.81 --- src/sbin/savecore/savecore.c:1.80 Mon Apr 6 12:32:30 2009 +++ src/sbin/savecore/savecore.c Tue Aug 18 04:02:39 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: savecore.c,v 1.80 2009/04/06 12:32:30 lukem Exp $ */ +/* $NetBSD: savecore.c,v 1.81 2009/08/18 04:02:39 dogcow Exp $ */ /*- * Copyright (c) 1986, 1992, 1993 @@ -39,7 +39,7 @@ #if 0 static char sccsid[] = @(#)savecore.c 8.5 (Berkeley) 4/28/95; #else -__RCSID($NetBSD: savecore.c,v 1.80 2009/04/06 12:32:30 lukem Exp $); +__RCSID($NetBSD: savecore.c,v 1.81 2009/08/18 04:02:39 dogcow Exp $); #endif #endif /* not lint */ @@ -911,11 +911,9 @@ struct statvfs fsbuf; char mbuf[100], path[MAXPATHLEN]; - if (stat(kernel, st) 0) { - syslog(LOG_ERR, %s: %m, kernel); - exit(1); - } - kernelsize = st.st_blocks * S_BLKSIZE; + /* XXX assume a reasonable default, unless we find a kernel. */ + kernelsize = 20 * 1024 * 1024; + if (!stat(kernel, st)) kernelsize = st.st_blocks * S_BLKSIZE; if (statvfs(dirname, fsbuf) 0) { syslog(LOG_ERR, %s: %m, dirname); exit(1);