xref: /freebsd/sbin/savecore/savecore.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 char 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 void	 save_core __P((void));
138 void	 usage __P((void));
139 void	 Write __P((int, void *, int));
140 
141 int
142 main(argc, argv)
143 	int argc;
144 	char *argv[];
145 {
146 	int ch;
147 
148 	openlog("savecore", LOG_PERROR, LOG_DAEMON);
149 
150 	while ((ch = getopt(argc, argv, "cdfN:vz")) != -1)
151 		switch(ch) {
152 		case 'c':
153 			clear = 1;
154 			break;
155 		case 'd':		/* Not documented. */
156 		case 'v':
157 			verbose = 1;
158 			break;
159 		case 'f':
160 			force = 1;
161 			break;
162 		case 'N':
163 			kernel = optarg;
164 			break;
165 		case 'z':
166 			compress = 1;
167 			break;
168 		case '?':
169 		default:
170 			usage();
171 		}
172 	argc -= optind;
173 	argv += optind;
174 
175 	if (!clear) {
176 		if (argc != 1 && argc != 2)
177 			usage();
178 		dirname = argv[0];
179 	}
180 	if (argc == 2)
181 		kernel = argv[1];
182 
183 	(void)time(&now);
184 	kmem_setup();
185 
186 	if (clear) {
187 		clear_dump();
188 		exit(0);
189 	}
190 
191 	if (!dump_exists() && !force)
192 		exit(1);
193 
194 	check_kmem();
195 
196 	if (panicstr)
197 		syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
198 	else
199 		syslog(LOG_ALERT, "reboot");
200 
201 	get_dumpsize();
202 
203 	if ((!get_crashtime() || !check_space()) && !force)
204 		exit(1);
205 
206 	save_core();
207 
208 	clear_dump();
209 	exit(0);
210 }
211 
212 void
213 kmem_setup()
214 {
215 	FILE *fp;
216 	int kmem, i;
217 	const char *dump_sys;
218 	int mib[2];
219 	size_t len;
220 
221 	/*
222 	 * Some names we need for the currently running system, others for
223 	 * the system that was running when the dump was made.  The values
224 	 * obtained from the current system are used to look for things in
225 	 * /dev/kmem that cannot be found in the dump_sys namelist, but are
226 	 * presumed to be the same (since the disk partitions are probably
227 	 * the same!)
228 	 */
229 	if ((nlist(getbootfile(), current_nl)) == -1)
230 		syslog(LOG_ERR, "%s: nlist: %s", getbootfile(),
231 		       strerror(errno));
232 	for (i = 0; cursyms[i] != -1; i++)
233 		if (current_nl[cursyms[i]].n_value == 0) {
234 			syslog(LOG_ERR, "%s: %s not in namelist",
235 			    getbootfile(), current_nl[cursyms[i]].n_name);
236 			exit(1);
237 		}
238 
239 	dump_sys = kernel ? kernel : getbootfile();
240 	if ((nlist(dump_sys, dump_nl)) == -1)
241 		syslog(LOG_ERR, "%s: nlist: %s", dump_sys, strerror(errno));
242 	for (i = 0; dumpsyms[i] != -1; i++)
243 		if (dump_nl[dumpsyms[i]].n_value == 0) {
244 			syslog(LOG_ERR, "%s: %s not in namelist",
245 			    dump_sys, dump_nl[dumpsyms[i]].n_name);
246 			exit(1);
247 		}
248 
249 	mib[0] = CTL_KERN;
250 	mib[1] = KERN_DUMPDEV;
251 	len = sizeof dumpdev;
252 	if (sysctl(mib, 2, &dumpdev, &len, NULL, 0) == -1) {
253 		syslog(LOG_ERR, "sysctl: kern.dumpdev: %m");
254 		exit(1);
255 	}
256 	if (dumpdev == NODEV) {
257 		syslog(LOG_WARNING, "no core dump (no dumpdev)");
258 		exit(1);
259 	}
260 
261 	kmem = Open(_PATH_KMEM, O_RDONLY);
262 	Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET);
263 	(void)Read(kmem, &dumplo, sizeof(dumplo));
264 	if (verbose)
265 		(void)printf("dumplo = %ld (%ld * %d)\n",
266 		    dumplo, dumplo/DEV_BSIZE, DEV_BSIZE);
267 	Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET);
268 	(void)Read(kmem, &dumpmag, sizeof(dumpmag));
269 	dumplo *= DEV_BSIZE;
270 	ddname = find_dev(dumpdev);
271 	dumpfd = Open(ddname, O_RDWR);
272 	fp = fdopen(kmem, "r");
273 	if (fp == NULL) {
274 		syslog(LOG_ERR, "%s: fdopen: %m", _PATH_KMEM);
275 		exit(1);
276 	}
277 	if (kernel)
278 		return;
279 	(void)fseek(fp, (off_t)current_nl[X_VERSION].n_value, L_SET);
280 	(void)fgets(vers, sizeof(vers), fp);
281 
282 	/* Don't fclose(fp), we use dumpfd later. */
283 }
284 
285 void
286 check_kmem()
287 {
288 	char core_vers[1024], *p;
289 
290 	DumpRead(dumpfd, core_vers, sizeof(core_vers),
291 	    (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET);
292 	core_vers[sizeof(core_vers) - 1] = '\0';
293 	p = strchr(core_vers, '\n');
294 	if (p)
295 		p[1] = '\0';
296 	if (strcmp(vers, core_vers) && kernel == 0)
297 		syslog(LOG_WARNING,
298 		    "warning: %s version mismatch:\n\t\"%s\"\nand\t\"%s\"\n",
299 		    getbootfile(), vers, core_vers);
300 	DumpRead(dumpfd, &panicstr, sizeof(panicstr),
301 	    (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
302 	if (panicstr) {
303 		DumpRead(dumpfd, panic_mesg, sizeof(panic_mesg),
304 		    (off_t)(dumplo + ok(panicstr)), L_SET);
305 	}
306 }
307 
308 void
309 clear_dump()
310 {
311 	long newdumplo;
312 
313 	newdumplo = 0;
314 	DumpWrite(dumpfd, &newdumplo, sizeof(newdumplo),
315 	    (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
316 	close(dumpfd);
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 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 	/* Seek to the start of the core. */
383 	Lseek(dumpfd, (off_t)dumplo, L_SET);
384 
385 	/* Copy the core file. */
386 	syslog(LOG_NOTICE, "writing %score to %s",
387 	    compress ? "compressed " : "", path);
388 	for (; dumpsize > 0; dumpsize -= nr) {
389 		(void)printf("%6dK\r", dumpsize / 1024);
390 		(void)fflush(stdout);
391 		nr = read(dumpfd, buf, MIN(dumpsize, sizeof(buf)));
392 		if (nr <= 0) {
393 			if (nr == 0)
394 				syslog(LOG_WARNING,
395 				    "WARNING: EOF on dump device");
396 			else
397 				syslog(LOG_ERR, "%s: %m", ddname);
398 			goto err2;
399 		}
400 		if (compress)
401 			nw = fwrite(buf, 1, nr, fp);
402 		else
403 			nw = write(ofd, buf, nr);
404 		if (nw != nr) {
405 			syslog(LOG_ERR, "%s: %s",
406 			    path, strerror(nw == 0 ? EIO : errno));
407 err2:			syslog(LOG_WARNING,
408 			    "WARNING: vmcore may be incomplete");
409 			(void)printf("\n");
410 			exit(1);
411 		}
412 	}
413 
414 	if (compress)
415 		(void)fclose(fp);
416 	else
417 		(void)close(ofd);
418 
419 	/* Copy the kernel. */
420 	ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
421 	(void)snprintf(path, sizeof(path), "%s/kernel.%d%s",
422 	    dirname, bounds, compress ? ".Z" : "");
423 	if (compress) {
424 		if ((fp = zopen(path, "w", 0)) == NULL) {
425 			syslog(LOG_ERR, "%s: %s", path, strerror(errno));
426 			exit(1);
427 		}
428 	} else
429 		ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
430 	syslog(LOG_NOTICE, "writing %skernel to %s",
431 	    compress ? "compressed " : "", path);
432 	while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
433 		if (compress)
434 			nw = fwrite(buf, 1, nr, fp);
435 		else
436 			nw = write(ofd, buf, nr);
437 		if (nw != nr) {
438 			syslog(LOG_ERR, "%s: %s",
439 			    path, strerror(nw == 0 ? EIO : errno));
440 			syslog(LOG_WARNING,
441 			    "WARNING: kernel may be incomplete");
442 			exit(1);
443 		}
444 	}
445 	if (nr < 0) {
446 		syslog(LOG_ERR, "%s: %s",
447 		    kernel ? kernel : getbootfile(), strerror(errno));
448 		syslog(LOG_WARNING,
449 		    "WARNING: kernel may be incomplete");
450 		exit(1);
451 	}
452 	if (compress)
453 		(void)fclose(fp);
454 	else
455 		(void)close(ofd);
456 	close(ifd);
457 }
458 
459 char *
460 find_dev(dev)
461 	register dev_t dev;
462 {
463 	register DIR *dfd;
464 	struct dirent *dir;
465 	struct stat sb;
466 	char *dp, devname[MAXPATHLEN + 1];
467 
468 	if ((dfd = opendir(_PATH_DEV)) == NULL) {
469 		syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno));
470 		exit(1);
471 	}
472 	(void)strcpy(devname, _PATH_DEV);
473 	while ((dir = readdir(dfd))) {
474 		(void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
475 		if (lstat(devname, &sb)) {
476 			syslog(LOG_ERR, "%s: %s", devname, strerror(errno));
477 			continue;
478 		}
479 		if ((sb.st_mode & S_IFMT) != S_IFCHR &&
480 		    (sb.st_mode & S_IFMT) != S_IFBLK)
481 			continue;
482 		if (dev == sb.st_rdev) {
483 			closedir(dfd);
484 			if ((dp = strdup(devname)) == NULL) {
485 				syslog(LOG_ERR, "%s", strerror(errno));
486 				exit(1);
487 			}
488 			return (dp);
489 		}
490 	}
491 	closedir(dfd);
492 	syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
493 	exit(1);
494 }
495 
496 int
497 get_crashtime()
498 {
499 	time_t dumptime;			/* Time the dump was taken. */
500 
501 	DumpRead(dumpfd, &dumptime, sizeof(dumptime),
502 	    (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
503 	if (dumptime == 0) {
504 		if (verbose)
505 			syslog(LOG_ERR, "dump time is zero");
506 		return (0);
507 	}
508 	(void)printf("savecore: system went down at %s", ctime(&dumptime));
509 #define	LEEWAY	(7 * 86400)
510 	if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
511 		(void)printf("dump time is unreasonable\n");
512 		return (0);
513 	}
514 	return (1);
515 }
516 
517 void
518 get_dumpsize()
519 {
520 	/* Read the dump size. */
521 	DumpRead(dumpfd, &dumpsize, sizeof(dumpsize),
522 	    (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
523 	dumpsize *= getpagesize();
524 }
525 
526 int
527 check_space()
528 {
529 	register FILE *fp;
530 	const char *tkernel;
531 	off_t minfree, spacefree, totfree, kernelsize, needed;
532 	struct stat st;
533 	struct statfs fsbuf;
534 	char buf[100], path[MAXPATHLEN];
535 
536 	tkernel = kernel ? kernel : getbootfile();
537 	if (stat(tkernel, &st) < 0) {
538 		syslog(LOG_ERR, "%s: %m", tkernel);
539 		exit(1);
540 	}
541 	kernelsize = st.st_blocks * S_BLKSIZE;
542 
543 	if (statfs(dirname, &fsbuf) < 0) {
544 		syslog(LOG_ERR, "%s: %m", dirname);
545 		exit(1);
546 	}
547  	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
548 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
549 
550 	(void)snprintf(path, sizeof(path), "%s/minfree", dirname);
551 	if ((fp = fopen(path, "r")) == NULL)
552 		minfree = 0;
553 	else {
554 		if (fgets(buf, sizeof(buf), fp) == NULL)
555 			minfree = 0;
556 		else
557 			minfree = atoi(buf);
558 		(void)fclose(fp);
559 	}
560 
561 	needed = (dumpsize + kernelsize) / 1024;
562  	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
563 		syslog(LOG_WARNING,
564 	"no dump, not enough free space on device (%lld available, need %lld)",
565 		    (long long)(minfree > 0 ? spacefree : totfree),
566 		    (long long)needed);
567 		return (0);
568 	}
569 	if (spacefree - needed < 0)
570 		syslog(LOG_WARNING,
571 		    "dump performed, but free space threshold crossed");
572 	return (1);
573 }
574 
575 int
576 Open(name, rw)
577 	const char *name;
578 	int rw;
579 {
580 	int fd;
581 
582 	if ((fd = open(name, rw, 0)) < 0) {
583 		syslog(LOG_ERR, "%s: %m", name);
584 		exit(1);
585 	}
586 	return (fd);
587 }
588 
589 int
590 Read(fd, bp, size)
591 	int fd, size;
592 	void *bp;
593 {
594 	int nr;
595 
596 	nr = read(fd, bp, size);
597 	if (nr != size) {
598 		syslog(LOG_ERR, "read: %m");
599 		exit(1);
600 	}
601 	return (nr);
602 }
603 
604 void
605 Lseek(fd, off, flag)
606 	int fd, flag;
607 	off_t off;
608 {
609 	off_t ret;
610 
611 	ret = lseek(fd, off, flag);
612 	if (ret == -1) {
613 		syslog(LOG_ERR, "lseek: %m");
614 		exit(1);
615 	}
616 }
617 
618 /*
619  * DumpWrite and DumpRead block io requests to the * dump device.
620  */
621 #define DUMPBUFSIZE	8192
622 void
623 DumpWrite(fd, bp, size, off, flag)
624 	int fd, size, flag;
625 	void *bp;
626 	off_t off;
627 {
628 	unsigned char buf[DUMPBUFSIZE], *p, *q;
629 	off_t pos;
630 	int i, j;
631 
632 	if (flag != L_SET) {
633 		syslog(LOG_ERR, "lseek: not LSET");
634 		exit(2);
635 	}
636 	q = bp;
637 	while (size) {
638 		pos = off & ~(DUMPBUFSIZE - 1);
639 		Lseek(fd, pos, flag);
640 		(void)Read(fd, buf, sizeof(buf));
641 		j = off & (DUMPBUFSIZE - 1);
642 		p = buf + j;
643 		i = size;
644 		if (i > DUMPBUFSIZE - j)
645 			i = DUMPBUFSIZE - j;
646 		memcpy(p, q, i);
647 		Lseek(fd, pos, flag);
648 		(void)Write(fd, buf, sizeof(buf));
649 		size -= i;
650 		q += i;
651 		off += i;
652 	}
653 }
654 
655 void
656 DumpRead(fd, bp, size, off, flag)
657 	int fd, size, flag;
658 	void *bp;
659 	off_t off;
660 {
661 	unsigned char buf[DUMPBUFSIZE], *p, *q;
662 	off_t pos;
663 	int i, j;
664 
665 	if (flag != L_SET) {
666 		syslog(LOG_ERR, "lseek: not LSET");
667 		exit(2);
668 	}
669 	q = bp;
670 	while (size) {
671 		pos = off & ~(DUMPBUFSIZE - 1);
672 		Lseek(fd, pos, flag);
673 		(void)Read(fd, buf, sizeof(buf));
674 		j = off & (DUMPBUFSIZE - 1);
675 		p = buf + j;
676 		i = size;
677 		if (i > DUMPBUFSIZE - j)
678 			i = DUMPBUFSIZE - j;
679 		memcpy(q, p, i);
680 		size -= i;
681 		q += i;
682 		off += i;
683 	}
684 }
685 
686 int
687 Create(file, mode)
688 	char *file;
689 	int mode;
690 {
691 	register int fd;
692 
693 	fd = creat(file, mode);
694 	if (fd < 0) {
695 		syslog(LOG_ERR, "%s: %m", file);
696 		exit(1);
697 	}
698 	return (fd);
699 }
700 
701 void
702 Write(fd, bp, size)
703 	int fd, size;
704 	void *bp;
705 {
706 	int n;
707 
708 	if ((n = write(fd, bp, size)) < size) {
709 		syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO));
710 		exit(1);
711 	}
712 }
713 
714 void
715 usage()
716 {
717 	(void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory");
718 	exit(1);
719 }
720