From 4731e263af6ebe32f2f777388193e0998e9b8f62 Mon Sep 17 00:00:00 2001 From: Bill Fenner Date: Sun, 13 Oct 1996 18:12:20 +0000 Subject: [PATCH] Make the savecore command work like the man page says: - make minfree work by getting the dump size before checking to see if the dump will fit on the filesystem - also fail to dump if no minfree is specified but there are not enough free blocks. Fix a typo in the man page. Fixes PR bin/1322 Submitted by: "Philippe C." --- sbin/savecore/savecore.8 | 4 ++-- sbin/savecore/savecore.c | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sbin/savecore/savecore.8 b/sbin/savecore/savecore.8 index 47e0a340b81a..5a613c9f7e0e 100644 --- a/sbin/savecore/savecore.8 +++ b/sbin/savecore/savecore.8 @@ -30,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" From: @(#)savecore.8 8.1 (Berkeley) 6/5/93 -.\" $Id$ +.\" $Id: savecore.8,v 1.3 1994/09/24 00:08:21 wollman Exp $ .\" .Dd September 23, 1994 .Dt SAVECORE 8 @@ -65,7 +65,7 @@ is insufficient disk space. Use .Ar system as the kernel instead of the running kernel (as determined from -.Xr getbootfile 3 ) +.Xr getbootfile 3 ). .It Fl v Prints out some additional debugging information. .It Fl z diff --git a/sbin/savecore/savecore.c b/sbin/savecore/savecore.c index 2fc2c6089ef2..b86b05ba7c7f 100644 --- a/sbin/savecore/savecore.c +++ b/sbin/savecore/savecore.c @@ -119,6 +119,7 @@ int Create __P((char *, int)); int dump_exists __P((void)); char *find_dev __P((dev_t, int)); int get_crashtime __P((void)); +int get_dumpsize __P((void)); void kmem_setup __P((void)); void log __P((int, char *, ...)); void Lseek __P((int, off_t, int)); @@ -189,6 +190,8 @@ main(argc, argv) else syslog(LOG_ALERT, "reboot"); + get_dumpsize(); + if ((!get_crashtime() || !check_space()) && !force) exit(1); @@ -370,15 +373,10 @@ err1: syslog(LOG_WARNING, "%s: %s", path, strerror(errno)); ifd = dumpfd; } - /* Read the dump size. */ - Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET); - (void)Read(dumpfd, &dumpsize, sizeof(dumpsize)); - /* Seek to the start of the core. */ Lseek(ifd, (off_t)dumplo, L_SET); /* Copy the core file. */ - dumpsize *= getpagesize(); syslog(LOG_NOTICE, "writing %score to %s", compress ? "compressed " : "", path); for (; dumpsize > 0; dumpsize -= nr) { @@ -528,12 +526,23 @@ get_crashtime() return (1); } +int +get_dumpsize() +{ + /* Read the dump size. */ + Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET); + (void)Read(dumpfd, &dumpsize, sizeof(dumpsize)); + dumpsize *= getpagesize(); + + return(1); +} + int check_space() { register FILE *fp; const char *tkernel; - off_t minfree, spacefree, kernelsize, needed; + off_t minfree, spacefree, totfree, kernelsize, needed; struct stat st; struct statfs fsbuf; char buf[100], path[MAXPATHLEN]; @@ -544,11 +553,13 @@ check_space() exit(1); } kernelsize = st.st_blocks * S_BLKSIZE; + if (statfs(dirname, &fsbuf) < 0) { syslog(LOG_ERR, "%s: %m", dirname); exit(1); } spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024; + totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024; (void)snprintf(path, sizeof(path), "%s/minfree", dirname); if ((fp = fopen(path, "r")) == NULL) @@ -562,12 +573,12 @@ check_space() } needed = (dumpsize + kernelsize) / 1024; - if (minfree > 0 && spacefree - needed < minfree) { + if (((minfree > 0) ? spacefree : totfree) - needed < minfree) { syslog(LOG_WARNING, "no dump, not enough free space on device"); return (0); } - if (spacefree - needed < minfree) + if (spacefree - needed < 0) syslog(LOG_WARNING, "dump performed, but free space threshold crossed"); return (1);