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