summaryrefslogtreecommitdiffstats
path: root/sbin/savecore
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2000-09-28 20:09:36 +0000
committerdes <des@FreeBSD.org>2000-09-28 20:09:36 +0000
commit9cd7f92a8872f85ae87cd8fbe02d0f0010351f18 (patch)
tree5c48d7eaaf0f38176bb17f92d40fb4f8b1b1b70e /sbin/savecore
parentf01a7783fc8f1e1fb67915f4718a5d88ab283b89 (diff)
downloadFreeBSD-src-9cd7f92a8872f85ae87cd8fbe02d0f0010351f18.zip
FreeBSD-src-9cd7f92a8872f85ae87cd8fbe02d0f0010351f18.tar.gz
Remove superfluous code:
1) use devname() instead of searching /dev for the dump device 2) use fopen() instead of open() so we don't need to differentiate between compressing and not compressing when writing the core file or the kernel (zopen() returns a FILE *, so we just use fwrite() in both cases) There should be no functional changes.
Diffstat (limited to 'sbin/savecore')
-rw-r--r--sbin/savecore/savecore.c107
1 files changed, 29 insertions, 78 deletions
diff --git a/sbin/savecore/savecore.c b/sbin/savecore/savecore.c
index 53f84d6..d5e0a31 100644
--- a/sbin/savecore/savecore.c
+++ b/sbin/savecore/savecore.c
@@ -55,7 +55,6 @@ static const char rcsid[] =
#include <vm/vm_param.h>
#include <vm/pmap.h>
-#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <nlist.h>
@@ -122,7 +121,6 @@ int clear, compress, force, verbose; /* flags */
void check_kmem __P((void));
int check_space __P((void));
void clear_dump __P((void));
-int Create __P((char *, int));
void DumpRead __P((int fd, void *bp, int size, off_t off, int flag));
void DumpWrite __P((int fd, void *bp, int size, off_t off, int flag));
int dump_exists __P((void));
@@ -340,7 +338,7 @@ void
save_core()
{
register FILE *fp;
- register int bounds, ifd, nr, nw, ofd;
+ register int bounds, ifd, nr, nw;
char path[MAXPATHLEN];
mode_t oumask;
@@ -370,14 +368,14 @@ err1: syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
(void)snprintf(path, sizeof(path), "%s/vmcore.%d%s",
savedir, bounds, compress ? ".Z" : "");
- if (compress) {
- if ((fp = zopen(path, "w", 0)) == NULL) {
- syslog(LOG_ERR, "%s: %s", path, strerror(errno));
- exit(1);
- }
- ofd = -1; /* Not actually used. */
- } else
- ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if (compress)
+ fp = zopen(path, "w", 0);
+ else
+ fp = fopen(path, "w");
+ if (fp == NULL) {
+ syslog(LOG_ERR, "%s: %s", path, strerror(errno));
+ exit(1);
+ }
(void)umask(oumask);
/* Seek to the start of the core. */
@@ -398,10 +396,7 @@ err1: syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
syslog(LOG_ERR, "%s: %m", ddname);
goto err2;
}
- 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));
@@ -412,29 +407,24 @@ err2: syslog(LOG_WARNING,
}
}
- if (compress)
- (void)fclose(fp);
- else
- (void)close(ofd);
+ (void)fclose(fp);
/* Copy the kernel. */
ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
(void)snprintf(path, sizeof(path), "%s/kernel.%d%s",
savedir, bounds, compress ? ".Z" : "");
- if (compress) {
- if ((fp = zopen(path, "w", 0)) == NULL) {
- syslog(LOG_ERR, "%s: %s", path, strerror(errno));
- exit(1);
- }
- } else
- ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ if (compress)
+ fp = zopen(path, "w", 0);
+ else
+ fp = fopen(path, "w");
+ if (fp == NULL) {
+ syslog(LOG_ERR, "%s: %s", path, strerror(errno));
+ exit(1);
+ }
syslog(LOG_NOTICE, "writing %skernel to %s",
compress ? "compressed " : "", path);
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));
@@ -450,10 +440,7 @@ err2: syslog(LOG_WARNING,
"WARNING: kernel may be incomplete");
exit(1);
}
- if (compress)
- (void)fclose(fp);
- else
- (void)close(ofd);
+ (void)fclose(fp);
close(ifd);
}
@@ -461,36 +448,15 @@ char *
find_dev(dev)
register dev_t dev;
{
- register DIR *dfd;
- struct dirent *dir;
- struct stat sb;
- char *dp, devname[MAXPATHLEN + 1];
-
- if ((dfd = opendir(_PATH_DEV)) == NULL) {
- syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno));
- exit(1);
- }
- (void)strcpy(devname, _PATH_DEV);
- while ((dir = readdir(dfd))) {
- (void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
- if (lstat(devname, &sb)) {
- syslog(LOG_ERR, "%s: %s", devname, strerror(errno));
- continue;
- }
- if ((sb.st_mode & S_IFMT) != S_IFCHR &&
- (sb.st_mode & S_IFMT) != S_IFBLK)
- continue;
- if (dev == sb.st_rdev) {
- closedir(dfd);
- if ((dp = strdup(devname)) == NULL) {
- syslog(LOG_ERR, "%s", strerror(errno));
- exit(1);
- }
- return (dp);
- }
+ char *dn;
+
+ if ((dn = devname(dev, S_IFCHR)) != NULL) {
+ if (asprintf(&dn, "/dev/%s", dn) != -1)
+ return dn;
+ syslog(LOG_ERR, "insufficient memory");
+ } else {
+ syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
}
- closedir(dfd);
- syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
exit(1);
}
@@ -684,21 +650,6 @@ DumpRead(fd, bp, size, off, flag)
}
}
-int
-Create(file, mode)
- char *file;
- int mode;
-{
- register int fd;
-
- fd = creat(file, mode);
- if (fd < 0) {
- syslog(LOG_ERR, "%s: %m", file);
- exit(1);
- }
- return (fd);
-}
-
void
Write(fd, bp, size)
int fd, size;
OpenPOWER on IntegriCloud