xref: /freebsd/sbin/savecore/savecore.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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 	bounds = ifd = nr = nw = ofd = 0;
347 
348 	/*
349 	 * Get the current number and update the bounds file.  Do the update
350 	 * now, because may fail later and don't want to overwrite anything.
351 	 */
352 	(void)snprintf(path, sizeof(path), "%s/bounds", dirname);
353 	if ((fp = fopen(path, "r")) == NULL)
354 		goto err1;
355 	if (fgets(buf, sizeof(buf), fp) == NULL) {
356 		if (ferror(fp))
357 err1:			syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
358 		bounds = 0;
359 	} else
360 		bounds = atoi(buf);
361 	if (fp != NULL)
362 		(void)fclose(fp);
363 	if ((fp = fopen(path, "w")) == NULL)
364 		syslog(LOG_ERR, "%s: %m", path);
365 	else {
366 		(void)fprintf(fp, "%d\n", bounds + 1);
367 		(void)fclose(fp);
368 	}
369 
370 	/* Create the core file. */
371 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
372 	(void)snprintf(path, sizeof(path), "%s/vmcore.%d%s",
373 	    dirname, bounds, compress ? ".Z" : "");
374 	if (compress) {
375 		if ((fp = zopen(path, "w", 0)) == NULL) {
376 			syslog(LOG_ERR, "%s: %s", path, strerror(errno));
377 			exit(1);
378 		}
379 	} else
380 		ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
381 	(void)umask(oumask);
382 
383 	/* Open the raw device. */
384 	rawp = rawname(ddname);
385 	if ((ifd = open(rawp, O_RDONLY)) == -1) {
386 		syslog(LOG_WARNING, "%s: %m; using block device", rawp);
387 		ifd = dumpfd;
388 	}
389 
390 	/* Seek to the start of the core. */
391 	Lseek(ifd, (off_t)dumplo, L_SET);
392 
393 	/* Copy the core file. */
394 	syslog(LOG_NOTICE, "writing %score to %s",
395 	    compress ? "compressed " : "", path);
396 	for (; dumpsize > 0; dumpsize -= nr) {
397 		(void)printf("%6dK\r", dumpsize / 1024);
398 		(void)fflush(stdout);
399 		nr = read(ifd, buf, MIN(dumpsize, sizeof(buf)));
400 		if (nr <= 0) {
401 			if (nr == 0)
402 				syslog(LOG_WARNING,
403 				    "WARNING: EOF on dump device");
404 			else
405 				syslog(LOG_ERR, "%s: %m", rawp);
406 			goto err2;
407 		}
408 		if (compress)
409 			nw = fwrite(buf, 1, nr, fp);
410 		else
411 			nw = write(ofd, buf, nr);
412 		if (nw != nr) {
413 			syslog(LOG_ERR, "%s: %s",
414 			    path, strerror(nw == 0 ? EIO : errno));
415 err2:			syslog(LOG_WARNING,
416 			    "WARNING: vmcore may be incomplete");
417 			(void)printf("\n");
418 			exit(1);
419 		}
420 	}
421 	(void)close(ifd);
422 	if (compress)
423 		(void)fclose(fp);
424 	else
425 		(void)close(ofd);
426 
427 	/* Copy the kernel. */
428 	ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
429 	(void)snprintf(path, sizeof(path), "%s/kernel.%d%s",
430 	    dirname, bounds, compress ? ".Z" : "");
431 	if (compress) {
432 		if ((fp = zopen(path, "w", 0)) == NULL) {
433 			syslog(LOG_ERR, "%s: %s", path, strerror(errno));
434 			exit(1);
435 		}
436 	} else
437 		ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
438 	syslog(LOG_NOTICE, "writing %skernel to %s",
439 	    compress ? "compressed " : "", path);
440 	while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
441 		if (compress)
442 			nw = fwrite(buf, 1, nr, fp);
443 		else
444 			nw = write(ofd, buf, nr);
445 		if (nw != nr) {
446 			syslog(LOG_ERR, "%s: %s",
447 			    path, strerror(nw == 0 ? EIO : errno));
448 			syslog(LOG_WARNING,
449 			    "WARNING: kernel may be incomplete");
450 			exit(1);
451 		}
452 	}
453 	if (nr < 0) {
454 		syslog(LOG_ERR, "%s: %s",
455 		    kernel ? kernel : getbootfile(), strerror(errno));
456 		syslog(LOG_WARNING,
457 		    "WARNING: kernel may be incomplete");
458 		exit(1);
459 	}
460 	if (compress)
461 		(void)fclose(fp);
462 	else
463 		(void)close(ofd);
464 }
465 
466 char *
467 find_dev(dev)
468 	register dev_t dev;
469 {
470 	register DIR *dfd;
471 	struct dirent *dir;
472 	struct stat sb;
473 	char *dp, devname[MAXPATHLEN + 1];
474 
475 	if ((dfd = opendir(_PATH_DEV)) == NULL) {
476 		syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno));
477 		exit(1);
478 	}
479 	(void)strcpy(devname, _PATH_DEV);
480 	while ((dir = readdir(dfd))) {
481 		(void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
482 		if (lstat(devname, &sb)) {
483 			syslog(LOG_ERR, "%s: %s", devname, strerror(errno));
484 			continue;
485 		}
486 		if ((sb.st_mode & S_IFMT) != S_IFCHR &&
487 		    (sb.st_mode & S_IFMT) != S_IFBLK)
488 			continue;
489 		if (dev == sb.st_rdev) {
490 			closedir(dfd);
491 			if ((dp = strdup(devname)) == NULL) {
492 				syslog(LOG_ERR, "%s", strerror(errno));
493 				exit(1);
494 			}
495 			return (dp);
496 		}
497 	}
498 	closedir(dfd);
499 	syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
500 	exit(1);
501 }
502 
503 char *
504 rawname(s)
505 	char *s;
506 {
507 	char *sl, name[MAXPATHLEN];
508 
509 	if ((sl = rindex(s, '/')) == NULL || sl[1] == '0') {
510 		syslog(LOG_ERR,
511 		    "can't make raw dump device name from %s", s);
512 		return (s);
513 	}
514 	(void)snprintf(name, sizeof(name), "%.*s/r%s", (int)(sl - s), s, sl + 1);
515 	if ((sl = strdup(name)) == NULL) {
516 		syslog(LOG_ERR, "%s", strerror(errno));
517 		exit(1);
518 	}
519 	return (sl);
520 }
521 
522 int
523 get_crashtime()
524 {
525 	time_t dumptime;			/* Time the dump was taken. */
526 
527 	DumpRead(dumpfd, &dumptime, sizeof(dumptime),
528 	    (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
529 	if (dumptime == 0) {
530 		if (verbose)
531 			syslog(LOG_ERR, "dump time is zero");
532 		return (0);
533 	}
534 	(void)printf("savecore: system went down at %s", ctime(&dumptime));
535 #define	LEEWAY	(7 * 86400)
536 	if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
537 		(void)printf("dump time is unreasonable\n");
538 		return (0);
539 	}
540 	return (1);
541 }
542 
543 void
544 get_dumpsize()
545 {
546 	/* Read the dump size. */
547 	DumpRead(dumpfd, &dumpsize, sizeof(dumpsize),
548 	    (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
549 	dumpsize *= getpagesize();
550 }
551 
552 int
553 check_space()
554 {
555 	register FILE *fp;
556 	const char *tkernel;
557 	off_t minfree, spacefree, totfree, kernelsize, needed;
558 	struct stat st;
559 	struct statfs fsbuf;
560 	char buf[100], path[MAXPATHLEN];
561 
562 	tkernel = kernel ? kernel : getbootfile();
563 	if (stat(tkernel, &st) < 0) {
564 		syslog(LOG_ERR, "%s: %m", tkernel);
565 		exit(1);
566 	}
567 	kernelsize = st.st_blocks * S_BLKSIZE;
568 
569 	if (statfs(dirname, &fsbuf) < 0) {
570 		syslog(LOG_ERR, "%s: %m", dirname);
571 		exit(1);
572 	}
573  	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
574 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
575 
576 	(void)snprintf(path, sizeof(path), "%s/minfree", dirname);
577 	if ((fp = fopen(path, "r")) == NULL)
578 		minfree = 0;
579 	else {
580 		if (fgets(buf, sizeof(buf), fp) == NULL)
581 			minfree = 0;
582 		else
583 			minfree = atoi(buf);
584 		(void)fclose(fp);
585 	}
586 
587 	needed = (dumpsize + kernelsize) / 1024;
588  	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
589 		syslog(LOG_WARNING,
590 		    "no dump, not enough free space on device (%ld available, need %ld",
591 			(minfree > 0) ? spacefree : totfree, needed);
592 		return (0);
593 	}
594 	if (spacefree - needed < 0)
595 		syslog(LOG_WARNING,
596 		    "dump performed, but free space threshold crossed");
597 	return (1);
598 }
599 
600 int
601 Open(name, rw)
602 	const char *name;
603 	int rw;
604 {
605 	int fd;
606 
607 	if ((fd = open(name, rw, 0)) < 0) {
608 		syslog(LOG_ERR, "%s: %m", name);
609 		exit(1);
610 	}
611 	return (fd);
612 }
613 
614 int
615 Read(fd, bp, size)
616 	int fd, size;
617 	void *bp;
618 {
619 	int nr;
620 
621 	nr = read(fd, bp, size);
622 	if (nr != size) {
623 		syslog(LOG_ERR, "read: %m");
624 		exit(1);
625 	}
626 	return (nr);
627 }
628 
629 void
630 Lseek(fd, off, flag)
631 	int fd, flag;
632 	off_t off;
633 {
634 	off_t ret;
635 
636 	ret = lseek(fd, off, flag);
637 	if (ret == -1) {
638 		syslog(LOG_ERR, "lseek: %m");
639 		exit(1);
640 	}
641 }
642 
643 /*
644  * DumpWrite and DumpRead block io requests to the * dump device.
645  */
646 #define DUMPBUFSIZE	8192
647 void
648 DumpWrite(fd, bp, size, off, flag)
649 	int fd, size, flag;
650 	void *bp;
651 	off_t off;
652 {
653 	unsigned char buf[DUMPBUFSIZE], *p, *q;
654 	off_t pos;
655 	int i, j;
656 
657 	if (flag != L_SET) {
658 		syslog(LOG_ERR, "lseek: not LSET");
659 		exit(2);
660 	}
661 	q = bp;
662 	while (size) {
663 		pos = off & ~(DUMPBUFSIZE - 1);
664 		Lseek(fd, pos, flag);
665 		(void)Read(fd, buf, sizeof(buf));
666 		j = off & (DUMPBUFSIZE - 1);
667 		p = buf + j;
668 		i = size;
669 		if (i > DUMPBUFSIZE - j)
670 			i = DUMPBUFSIZE - j;
671 		memcpy(p, q, i);
672 		Lseek(fd, pos, flag);
673 		(void)Write(fd, buf, sizeof(buf));
674 		size -= i;
675 		q += i;
676 		off += i;
677 	}
678 }
679 
680 void
681 DumpRead(fd, bp, size, off, flag)
682 	int fd, size, flag;
683 	void *bp;
684 	off_t off;
685 {
686 	unsigned char buf[DUMPBUFSIZE], *p, *q;
687 	off_t pos;
688 	int i, j;
689 
690 	if (flag != L_SET) {
691 		syslog(LOG_ERR, "lseek: not LSET");
692 		exit(2);
693 	}
694 	q = bp;
695 	while (size) {
696 		pos = off & ~(DUMPBUFSIZE - 1);
697 		Lseek(fd, pos, flag);
698 		(void)Read(fd, buf, sizeof(buf));
699 		j = off & (DUMPBUFSIZE - 1);
700 		p = buf + j;
701 		i = size;
702 		if (i > DUMPBUFSIZE - j)
703 			i = DUMPBUFSIZE - j;
704 		memcpy(q, p, i);
705 		size -= i;
706 		q += i;
707 		off += i;
708 	}
709 }
710 
711 int
712 Create(file, mode)
713 	char *file;
714 	int mode;
715 {
716 	register int fd;
717 
718 	fd = creat(file, mode);
719 	if (fd < 0) {
720 		syslog(LOG_ERR, "%s: %m", file);
721 		exit(1);
722 	}
723 	return (fd);
724 }
725 
726 void
727 Write(fd, bp, size)
728 	int fd, size;
729 	void *bp;
730 {
731 	int n;
732 
733 	if ((n = write(fd, bp, size)) < size) {
734 		syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO));
735 		exit(1);
736 	}
737 }
738 
739 void
740 usage()
741 {
742 	(void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory");
743 	exit(1);
744 }
745