xref: /freebsd/sbin/savecore/savecore.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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, int));
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, S_IFBLK);
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 	register char *cp;
290 	char core_vers[1024], *p;
291 
292 	DumpRead(dumpfd, core_vers, sizeof(core_vers),
293 	    (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET);
294 	core_vers[sizeof(core_vers) - 1] = '\0';
295 	p = strchr(core_vers, '\n');
296 	if (p)
297 		p[1] = '\0';
298 	if (strcmp(vers, core_vers) && kernel == 0)
299 		syslog(LOG_WARNING,
300 		    "warning: %s version mismatch:\n\t\"%s\"\nand\t\"%s\"\n",
301 		    getbootfile(), vers, core_vers);
302 	DumpRead(dumpfd, &panicstr, sizeof(panicstr),
303 	    (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
304 	if (panicstr) {
305 		DumpRead(dumpfd, panic_mesg, sizeof(panic_mesg),
306 		    (off_t)(dumplo + ok(panicstr)), L_SET);
307 	}
308 }
309 
310 void
311 clear_dump()
312 {
313 	long newdumplo;
314 
315 	newdumplo = 0;
316 	DumpWrite(dumpfd, &newdumplo, sizeof(newdumplo),
317 	    (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
318 }
319 
320 int
321 dump_exists()
322 {
323 	int newdumpmag;
324 
325 	DumpRead(dumpfd, &newdumpmag, sizeof(newdumpmag),
326 	    (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
327 	if (newdumpmag != dumpmag) {
328 		if (verbose)
329 			syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
330 			    newdumpmag, dumpmag);
331 		syslog(LOG_WARNING, "no core dump");
332 		return (0);
333 	}
334 	return (1);
335 }
336 
337 char buf[1024 * 1024];
338 
339 void
340 save_core()
341 {
342 	register FILE *fp;
343 	register int bounds, ifd, nr, nw, ofd;
344 	char *rawp, path[MAXPATHLEN];
345 	mode_t oumask;
346 
347 	/*
348 	 * Get the current number and update the bounds file.  Do the update
349 	 * now, because may fail later and don't want to overwrite anything.
350 	 */
351 	(void)snprintf(path, sizeof(path), "%s/bounds", dirname);
352 	if ((fp = fopen(path, "r")) == NULL)
353 		goto err1;
354 	if (fgets(buf, sizeof(buf), fp) == NULL) {
355 		if (ferror(fp))
356 err1:			syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
357 		bounds = 0;
358 	} else
359 		bounds = atoi(buf);
360 	if (fp != NULL)
361 		(void)fclose(fp);
362 	if ((fp = fopen(path, "w")) == NULL)
363 		syslog(LOG_ERR, "%s: %m", path);
364 	else {
365 		(void)fprintf(fp, "%d\n", bounds + 1);
366 		(void)fclose(fp);
367 	}
368 
369 	/* Create the core file. */
370 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
371 	(void)snprintf(path, sizeof(path), "%s/vmcore.%d%s",
372 	    dirname, bounds, compress ? ".Z" : "");
373 	if (compress) {
374 		if ((fp = zopen(path, "w", 0)) == NULL) {
375 			syslog(LOG_ERR, "%s: %s", path, strerror(errno));
376 			exit(1);
377 		}
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, type)
467 	register dev_t dev;
468 	register int type;
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) != type)
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 	(void)snprintf(name, sizeof(name), "%.*s/r%s", 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");
590 		return (0);
591 	}
592 	if (spacefree - needed < 0)
593 		syslog(LOG_WARNING,
594 		    "dump performed, but free space threshold crossed");
595 	return (1);
596 }
597 
598 int
599 Open(name, rw)
600 	const char *name;
601 	int rw;
602 {
603 	int fd;
604 
605 	if ((fd = open(name, rw, 0)) < 0) {
606 		syslog(LOG_ERR, "%s: %m", name);
607 		exit(1);
608 	}
609 	return (fd);
610 }
611 
612 int
613 Read(fd, bp, size)
614 	int fd, size;
615 	void *bp;
616 {
617 	int nr;
618 
619 	nr = read(fd, bp, size);
620 	if (nr != size) {
621 		syslog(LOG_ERR, "read: %m");
622 		exit(1);
623 	}
624 	return (nr);
625 }
626 
627 void
628 Lseek(fd, off, flag)
629 	int fd, flag;
630 	off_t off;
631 {
632 	off_t ret;
633 
634 	ret = lseek(fd, off, flag);
635 	if (ret == -1) {
636 		syslog(LOG_ERR, "lseek: %m");
637 		exit(1);
638 	}
639 }
640 
641 /*
642  * DumpWrite and DumpRead block io requests to the * dump device.
643  */
644 #define DUMPBUFSIZE	8192
645 void
646 DumpWrite(fd, bp, size, off, flag)
647 	int fd, size, flag;
648 	void *bp;
649 	off_t off;
650 {
651 	unsigned char buf[DUMPBUFSIZE], *p, *q;
652 	off_t pos;
653 	int i, j;
654 
655 	if (flag != L_SET) {
656 		syslog(LOG_ERR, "lseek: not LSET");
657 		exit(2);
658 	}
659 	q = bp;
660 	while (size) {
661 		pos = off & ~(DUMPBUFSIZE - 1);
662 		Lseek(fd, pos, flag);
663 		(void)Read(fd, buf, sizeof(buf));
664 		j = off & (DUMPBUFSIZE - 1);
665 		p = buf + j;
666 		i = size;
667 		if (i > DUMPBUFSIZE - j)
668 			i = DUMPBUFSIZE - j;
669 		memcpy(p, q, i);
670 		Lseek(fd, pos, flag);
671 		(void)Write(fd, buf, sizeof(buf));
672 		size -= i;
673 		q += i;
674 		off += i;
675 	}
676 }
677 
678 void
679 DumpRead(fd, bp, size, off, flag)
680 	int fd, size, flag;
681 	void *bp;
682 	off_t off;
683 {
684 	unsigned char buf[DUMPBUFSIZE], *p, *q;
685 	off_t pos;
686 	int i, j;
687 
688 	if (flag != L_SET) {
689 		syslog(LOG_ERR, "lseek: not LSET");
690 		exit(2);
691 	}
692 	q = bp;
693 	while (size) {
694 		pos = off & ~(DUMPBUFSIZE - 1);
695 		Lseek(fd, pos, flag);
696 		(void)Read(fd, buf, sizeof(buf));
697 		j = off & (DUMPBUFSIZE - 1);
698 		p = buf + j;
699 		i = size;
700 		if (i > DUMPBUFSIZE - j)
701 			i = DUMPBUFSIZE - j;
702 		memcpy(q, p, i);
703 		size -= i;
704 		q += i;
705 		off += i;
706 	}
707 }
708 
709 int
710 Create(file, mode)
711 	char *file;
712 	int mode;
713 {
714 	register int fd;
715 
716 	fd = creat(file, mode);
717 	if (fd < 0) {
718 		syslog(LOG_ERR, "%s: %m", file);
719 		exit(1);
720 	}
721 	return (fd);
722 }
723 
724 void
725 Write(fd, bp, size)
726 	int fd, size;
727 	void *bp;
728 {
729 	int n;
730 
731 	if ((n = write(fd, bp, size)) < size) {
732 		syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO));
733 		exit(1);
734 	}
735 }
736 
737 void
738 usage()
739 {
740 	(void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory");
741 	exit(1);
742 }
743