xref: /dragonfly/sbin/savecore/savecore.c (revision 1de703da)
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  * @(#) Copyright (c) 1986, 1992, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)savecore.c	8.3 (Berkeley) 1/2/94
35  * $FreeBSD: src/sbin/savecore/savecore.c,v 1.28.2.13 2002/04/07 21:17:50 asmodai Exp $
36  * $DragonFly: src/sbin/savecore/savecore.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/mount.h>
42 #include <sys/syslog.h>
43 #include <sys/sysctl.h>
44 
45 #include <vm/vm.h>
46 #include <vm/vm_param.h>
47 #include <vm/pmap.h>
48 
49 #include <dirent.h>
50 #include <fcntl.h>
51 #include <nlist.h>
52 #include <paths.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 extern FILE *zopen(const char *fname, const char *mode);
59 
60 #ifdef __alpha__
61 #define ok(number) ALPHA_K0SEG_TO_PHYS(number)
62 #endif
63 
64 #ifdef __i386__
65 #define ok(number) ((number) - kernbase)
66 #endif
67 
68 struct nlist current_nl[] = {	/* Namelist for currently running system. */
69 #define X_DUMPLO	0
70 	{ "_dumplo" },
71 #define X_TIME		1
72 	{ "_time_second" },
73 #define	X_DUMPSIZE	2
74 	{ "_dumpsize" },
75 #define X_VERSION	3
76 	{ "_version" },
77 #define X_PANICSTR	4
78 	{ "_panicstr" },
79 #define	X_DUMPMAG	5
80 	{ "_dumpmag" },
81 #define	X_KERNBASE	6
82 	{ "_kernbase" },
83 	{ "" },
84 };
85 int cursyms[] = { X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
86 int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
87 
88 struct nlist dump_nl[] = {	/* Name list for dumped system. */
89 	{ "_dumplo" },		/* Entries MUST be the same as */
90 	{ "_time_second" },	/*	those in current_nl[].  */
91 	{ "_dumpsize" },
92 	{ "_version" },
93 	{ "_panicstr" },
94 	{ "_dumpmag" },
95 	{ "_kernbase" },
96 	{ "" },
97 };
98 
99 /* Types match kernel declarations. */
100 u_long	dumpmag;			/* magic number in dump */
101 
102 /* Based on kernel variables, but with more convenient types. */
103 off_t	dumplo;				/* where dump starts on dumpdev */
104 off_t	dumpsize;			/* amount of memory dumped */
105 
106 char	*kernel;			/* user-specified kernel */
107 char	*savedir;			/* directory to save dumps in */
108 char	ddname[MAXPATHLEN];		/* name of dump device */
109 dev_t	dumpdev;			/* dump device */
110 int	dumpfd;				/* read/write descriptor on char dev */
111 time_t	now;				/* current date */
112 char	panic_mesg[1024];		/* panic message */
113 int	panicstr;		        /* flag: dump was caused by panic */
114 char	vers[1024];			/* version of kernel that crashed */
115 
116 #ifdef __i386__
117 u_long	kernbase;			/* offset of kvm to core file */
118 #endif
119 
120 int	clear, compress, force, verbose;	/* flags */
121 int	keep;			/* keep dump on device */
122 
123 void	 check_kmem __P((void));
124 int	 check_space __P((void));
125 void	 clear_dump __P((void));
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 void     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 *, ...)) __printf0like(2, 3);
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, "cdfkN: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 'k':
163 			keep = 1;
164 			break;
165 		case 'N':
166 			kernel = optarg;
167 			break;
168 		case 'z':
169 			compress = 1;
170 			break;
171 		case '?':
172 		default:
173 			usage();
174 		}
175 	argc -= optind;
176 	argv += optind;
177 
178 	if (!clear) {
179 		if (argc != 1 && argc != 2)
180 			usage();
181 		savedir = argv[0];
182 	}
183 	if (argc == 2)
184 		kernel = argv[1];
185 
186 	(void)time(&now);
187 	kmem_setup();
188 
189 	if (clear) {
190 		clear_dump();
191 		exit(0);
192 	}
193 
194 	if (!dump_exists() && !force)
195 		exit(1);
196 
197 	check_kmem();
198 
199 	if (panicstr)
200 		syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
201 	else
202 		syslog(LOG_ALERT, "reboot");
203 
204 	get_dumpsize();
205 
206 	if ((!get_crashtime() || !check_space()) && !force)
207 		exit(1);
208 
209 	save_core();
210 
211 	if (!keep)
212 		clear_dump();
213 
214 	exit(0);
215 }
216 
217 void
218 kmem_setup()
219 {
220 	int kmem, i;
221 	const char *dump_sys;
222 	size_t len;
223 	long kdumplo;		/* block number where dump starts on dumpdev */
224 	char *p;
225 
226 	/*
227 	 * Some names we need for the currently running system, others for
228 	 * the system that was running when the dump was made.  The values
229 	 * obtained from the current system are used to look for things in
230 	 * /dev/kmem that cannot be found in the dump_sys namelist, but are
231 	 * presumed to be the same (since the disk partitions are probably
232 	 * the same!)
233 	 */
234 	if ((nlist(getbootfile(), current_nl)) == -1)
235 		syslog(LOG_ERR, "%s: nlist: %m", getbootfile());
236 	for (i = 0; cursyms[i] != -1; i++)
237 		if (current_nl[cursyms[i]].n_value == 0) {
238 			syslog(LOG_ERR, "%s: %s not in namelist",
239 			    getbootfile(), current_nl[cursyms[i]].n_name);
240 			exit(1);
241 		}
242 
243 	dump_sys = kernel ? kernel : getbootfile();
244 	if ((nlist(dump_sys, dump_nl)) == -1)
245 		syslog(LOG_ERR, "%s: nlist: %m", dump_sys);
246 	for (i = 0; dumpsyms[i] != -1; i++)
247 		if (dump_nl[dumpsyms[i]].n_value == 0) {
248 			syslog(LOG_ERR, "%s: %s not in namelist",
249 			    dump_sys, dump_nl[dumpsyms[i]].n_name);
250 			exit(1);
251 		}
252 
253 #ifdef __i386__
254 	if (dump_nl[X_KERNBASE].n_value != 0)
255 		kernbase = dump_nl[X_KERNBASE].n_value;
256 	else
257 		kernbase = KERNBASE;
258 #endif
259 
260 	len = sizeof dumpdev;
261 	if (sysctlbyname("kern.dumpdev", &dumpdev, &len, NULL, 0) == -1) {
262 		syslog(LOG_ERR, "sysctl: kern.dumpdev: %m");
263 		exit(1);
264 	}
265 	if (dumpdev == NODEV) {
266 		syslog(LOG_WARNING, "no core dump (no dumpdev)");
267 		exit(1);
268 	}
269 
270 	kmem = Open(_PATH_KMEM, O_RDONLY);
271 	Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET);
272 	(void)Read(kmem, &kdumplo, sizeof(kdumplo));
273 	dumplo = (off_t)kdumplo * DEV_BSIZE;
274 	if (verbose)
275 		(void)printf("dumplo = %lld (%ld * %d)\n",
276 		    (long long)dumplo, kdumplo, DEV_BSIZE);
277 	Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET);
278 	(void)Read(kmem, &dumpmag, sizeof(dumpmag));
279 	find_dev(dumpdev);
280 	dumpfd = Open(ddname, O_RDWR);
281 	if (kernel)
282 		return;
283 
284 	lseek(kmem, (off_t)current_nl[X_VERSION].n_value, SEEK_SET);
285 	Read(kmem, vers, sizeof(vers));
286 	vers[sizeof(vers) - 1] = '\0';
287 	p = strchr(vers, '\n');
288 	if (p)
289 		p[1] = '\0';
290 
291 	/* Don't fclose(fp), we use kmem later. */
292 }
293 
294 void
295 check_kmem()
296 {
297 	char core_vers[1024], *p;
298 
299 	DumpRead(dumpfd, core_vers, sizeof(core_vers),
300 	    (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET);
301 	core_vers[sizeof(core_vers) - 1] = '\0';
302 	p = strchr(core_vers, '\n');
303 	if (p)
304 		p[1] = '\0';
305 	if (strcmp(vers, core_vers) && kernel == 0)
306 		syslog(LOG_WARNING,
307 		    "warning: %s version mismatch:\n\t\"%s\"\nand\t\"%s\"\n",
308 		    getbootfile(), vers, core_vers);
309 	DumpRead(dumpfd, &panicstr, sizeof(panicstr),
310 	    (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
311 	if (panicstr) {
312 		DumpRead(dumpfd, panic_mesg, sizeof(panic_mesg),
313 		    (off_t)(dumplo + ok(panicstr)), L_SET);
314 	}
315 }
316 
317 /*
318  * Clear the magic number in the dump header.
319  */
320 void
321 clear_dump()
322 {
323 	u_long newdumpmag;
324 
325 	newdumpmag = 0;
326 	DumpWrite(dumpfd, &newdumpmag, sizeof(newdumpmag),
327 	    (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
328 	close(dumpfd);
329 }
330 
331 /*
332  * Check if a dump exists by looking for a magic number in the dump
333  * header.
334  */
335 int
336 dump_exists()
337 {
338 	u_long newdumpmag;
339 
340 	DumpRead(dumpfd, &newdumpmag, sizeof(newdumpmag),
341 	    (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
342 	if (newdumpmag != dumpmag) {
343 		if (verbose)
344 			syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
345 			    newdumpmag, dumpmag);
346 		syslog(LOG_WARNING, "no core dump");
347 		return (0);
348 	}
349 	return (1);
350 }
351 
352 char buf[1024 * 1024];
353 #define BLOCKSIZE (1<<12)
354 #define BLOCKMASK (~(BLOCKSIZE-1))
355 
356 /*
357  * Save the core dump.
358  */
359 void
360 save_core()
361 {
362 	register FILE *fp;
363 	register int bounds, ifd, nr, nw;
364 	int hs, he;		/* start and end of hole */
365 	char path[MAXPATHLEN];
366 	mode_t oumask;
367 
368 	/*
369 	 * Get the current number and update the bounds file.  Do the update
370 	 * now, because may fail later and don't want to overwrite anything.
371 	 */
372 	(void)snprintf(path, sizeof(path), "%s/bounds", savedir);
373 	if ((fp = fopen(path, "r")) == NULL)
374 		goto err1;
375 	if (fgets(buf, sizeof(buf), fp) == NULL) {
376 		if (ferror(fp))
377 err1:			syslog(LOG_WARNING, "%s: %m", path);
378 		bounds = 0;
379 	} else
380 		bounds = atoi(buf);
381 	if (fp != NULL)
382 		(void)fclose(fp);
383 	if ((fp = fopen(path, "w")) == NULL)
384 		syslog(LOG_ERR, "%s: %m", path);
385 	else {
386 		(void)fprintf(fp, "%d\n", bounds + 1);
387 		(void)fclose(fp);
388 	}
389 
390 	/* Create the core file. */
391 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
392 	(void)snprintf(path, sizeof(path), "%s/vmcore.%d%s",
393 	    savedir, bounds, compress ? ".gz" : "");
394 	if (compress)
395 		fp = zopen(path, "w");
396 	else
397 		fp = fopen(path, "w");
398 	if (fp == NULL) {
399 		syslog(LOG_ERR, "%s: %m", path);
400 		exit(1);
401 	}
402 	(void)umask(oumask);
403 
404 	/* Seek to the start of the core. */
405 	Lseek(dumpfd, (off_t)dumplo, L_SET);
406 
407 	/* Copy the core file. */
408 	syslog(LOG_NOTICE, "writing %score to %s",
409 	    compress ? "compressed " : "", path);
410 	for (; dumpsize > 0; dumpsize -= nr) {
411 		(void)printf("%6dK\r", dumpsize / 1024);
412 		(void)fflush(stdout);
413 		nr = read(dumpfd, buf, MIN(dumpsize, sizeof(buf)));
414 		if (nr <= 0) {
415 			if (nr == 0)
416 				syslog(LOG_WARNING,
417 				    "WARNING: EOF on dump device");
418 			else
419 				syslog(LOG_ERR, "%s: %m", ddname);
420 			goto err2;
421 		}
422 		for (nw = 0; nw < nr; nw = he) {
423 			/* find a contiguous block of zeroes */
424 			for (hs = nw; hs < nr; hs += BLOCKSIZE) {
425 				for (he = hs; he < nr && buf[he] == 0; ++he)
426 					/* nothing */ ;
427 
428 				/* is the hole long enough to matter? */
429 				if (he >= hs + BLOCKSIZE)
430 					break;
431 			}
432 
433 			/* back down to a block boundary */
434 			he &= BLOCKMASK;
435 
436 			/*
437 			 * 1) Don't go beyond the end of the buffer.
438 			 * 2) If the end of the buffer is less than
439 			 *    BLOCKSIZE bytes away, we're at the end
440 			 *    of the file, so just grab what's left.
441 			 */
442 			if (hs + BLOCKSIZE > nr)
443 				hs = he = nr;
444 
445 			/*
446 			 * At this point, we have a partial ordering:
447 			 *     nw <= hs <= he <= nr
448 			 * If hs > nw, buf[nw..hs] contains non-zero data.
449 			 * If he > hs, buf[hs..he] is all zeroes.
450 			 */
451 			if (hs > nw)
452 				if (fwrite(buf + nw, hs - nw, 1, fp) != 1)
453 					break;
454 			if (he > hs)
455 				if (fseeko(fp, he - hs, SEEK_CUR) == -1)
456 					break;
457 		}
458 		if (nw != nr) {
459 			syslog(LOG_ERR, "%s: %m", path);
460 err2:			syslog(LOG_WARNING,
461 			    "WARNING: vmcore may be incomplete");
462 			(void)printf("\n");
463 			exit(1);
464 		}
465 	}
466 
467 	(void)fclose(fp);
468 
469 	/* Copy the kernel. */
470 	ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY);
471 	(void)snprintf(path, sizeof(path), "%s/kernel.%d%s",
472 	    savedir, bounds, compress ? ".gz" : "");
473 	if (compress)
474 		fp = zopen(path, "w");
475 	else
476 		fp = fopen(path, "w");
477 	if (fp == NULL) {
478 		syslog(LOG_ERR, "%s: %m", path);
479 		exit(1);
480 	}
481 	syslog(LOG_NOTICE, "writing %skernel to %s",
482 	    compress ? "compressed " : "", path);
483 	while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
484 		nw = fwrite(buf, 1, nr, fp);
485 		if (nw != nr) {
486 			syslog(LOG_ERR, "%s: %m", path);
487 			syslog(LOG_WARNING,
488 			    "WARNING: kernel may be incomplete");
489 			exit(1);
490 		}
491 	}
492 	if (nr < 0) {
493 		syslog(LOG_ERR, "%s: %m", kernel ? kernel : getbootfile());
494 		syslog(LOG_WARNING,
495 		    "WARNING: kernel may be incomplete");
496 		exit(1);
497 	}
498 	(void)fclose(fp);
499 	close(ifd);
500 }
501 
502 /*
503  * Verify that the specified device node exists and matches the
504  * specified device.
505  */
506 int
507 verify_dev(name, dev)
508 	char *name;
509 	register dev_t dev;
510 {
511 	struct stat sb;
512 
513 	if (lstat(name, &sb) == -1)
514 		return (-1);
515 	if (!S_ISCHR(sb.st_mode) || sb.st_rdev != dev)
516 		return (-1);
517 	return (0);
518 }
519 
520 /*
521  * Find the dump device.
522  *
523  *  1) try devname(3); see if it returns something sensible
524  *  2) scan /dev for the desired node
525  *  3) as a last resort, try to create the node we need
526  */
527 void
528 find_dev(dev)
529 	register dev_t dev;
530 {
531 	struct dirent *ent;
532 	char *dn, *dnp;
533 	DIR *d;
534 
535 	strcpy(ddname, _PATH_DEV);
536 	dnp = ddname + sizeof _PATH_DEV - 1;
537 	if ((dn = devname(dev, S_IFCHR)) != NULL) {
538 		strcpy(dnp, dn);
539 		if (verify_dev(ddname, dev) == 0)
540 			return;
541 	}
542 	if ((d = opendir(_PATH_DEV)) != NULL) {
543 		while ((ent = readdir(d))) {
544 			strcpy(dnp, ent->d_name);
545 			if (verify_dev(ddname, dev) == 0) {
546 				closedir(d);
547 				return;
548 			}
549 		}
550 		closedir(d);
551 	}
552 	strcpy(dnp, "dump");
553 	if (mknod(ddname, S_IFCHR|S_IRUSR|S_IWUSR, dev) == 0)
554 		return;
555 	syslog(LOG_ERR, "can't find device %d/%#x", major(dev), minor(dev));
556 	exit(1);
557 }
558 
559 /*
560  * Extract the date and time of the crash from the dump header, and
561  * make sure it looks sane (within one week of current date and time).
562  */
563 int
564 get_crashtime()
565 {
566 	time_t dumptime;			/* Time the dump was taken. */
567 
568 	DumpRead(dumpfd, &dumptime, sizeof(dumptime),
569 	    (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
570 	if (dumptime == 0) {
571 		if (verbose)
572 			syslog(LOG_ERR, "dump time is zero");
573 		return (0);
574 	}
575 	(void)printf("savecore: system went down at %s", ctime(&dumptime));
576 #define	LEEWAY	(7 * 86400)
577 	if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
578 		(void)printf("dump time is unreasonable\n");
579 		return (0);
580 	}
581 	return (1);
582 }
583 
584 /*
585  * Extract the size of the dump from the dump header.
586  */
587 void
588 get_dumpsize()
589 {
590 	int kdumpsize;	/* Number of pages in dump. */
591 
592 	/* Read the dump size. */
593 	DumpRead(dumpfd, &kdumpsize, sizeof(kdumpsize),
594 	    (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
595 	dumpsize = (off_t)kdumpsize * getpagesize();
596 }
597 
598 /*
599  * Check that sufficient space is available on the disk that holds the
600  * save directory.
601  */
602 int
603 check_space()
604 {
605 	register FILE *fp;
606 	const char *tkernel;
607 	off_t minfree, spacefree, totfree, kernelsize, needed;
608 	struct stat st;
609 	struct statfs fsbuf;
610 	char buf[100], path[MAXPATHLEN];
611 
612 	tkernel = kernel ? kernel : getbootfile();
613 	if (stat(tkernel, &st) < 0) {
614 		syslog(LOG_ERR, "%s: %m", tkernel);
615 		exit(1);
616 	}
617 	kernelsize = st.st_blocks * S_BLKSIZE;
618 
619 	if (statfs(savedir, &fsbuf) < 0) {
620 		syslog(LOG_ERR, "%s: %m", savedir);
621 		exit(1);
622 	}
623  	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
624 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
625 
626 	(void)snprintf(path, sizeof(path), "%s/minfree", savedir);
627 	if ((fp = fopen(path, "r")) == NULL)
628 		minfree = 0;
629 	else {
630 		if (fgets(buf, sizeof(buf), fp) == NULL)
631 			minfree = 0;
632 		else
633 			minfree = atoi(buf);
634 		(void)fclose(fp);
635 	}
636 
637 	needed = (dumpsize + kernelsize) / 1024;
638  	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
639 		syslog(LOG_WARNING,
640 	"no dump, not enough free space on device (%lld available, need %lld)",
641 		    (long long)(minfree > 0 ? spacefree : totfree),
642 		    (long long)needed);
643 		return (0);
644 	}
645 	if (spacefree - needed < 0)
646 		syslog(LOG_WARNING,
647 		    "dump performed, but free space threshold crossed");
648 	return (1);
649 }
650 
651 int
652 Open(name, rw)
653 	const char *name;
654 	int rw;
655 {
656 	int fd;
657 
658 	if ((fd = open(name, rw, 0)) < 0) {
659 		syslog(LOG_ERR, "%s: %m", name);
660 		exit(1);
661 	}
662 	return (fd);
663 }
664 
665 int
666 Read(fd, bp, size)
667 	int fd, size;
668 	void *bp;
669 {
670 	int nr;
671 
672 	nr = read(fd, bp, size);
673 	if (nr != size) {
674 		syslog(LOG_ERR, "read: %m");
675 		exit(1);
676 	}
677 	return (nr);
678 }
679 
680 void
681 Lseek(fd, off, flag)
682 	int fd, flag;
683 	off_t off;
684 {
685 	off_t ret;
686 
687 	ret = lseek(fd, off, flag);
688 	if (ret == -1) {
689 		syslog(LOG_ERR, "lseek: %m");
690 		exit(1);
691 	}
692 }
693 
694 /*
695  * DumpWrite and DumpRead block io requests to the * dump device.
696  */
697 #define DUMPBUFSIZE	8192
698 void
699 DumpWrite(fd, bp, size, off, flag)
700 	int fd, size, flag;
701 	void *bp;
702 	off_t off;
703 {
704 	unsigned char buf[DUMPBUFSIZE], *p, *q;
705 	off_t pos;
706 	int i, j;
707 
708 	if (flag != L_SET) {
709 		syslog(LOG_ERR, "lseek: not LSET");
710 		exit(2);
711 	}
712 	q = bp;
713 	while (size) {
714 		pos = off & ~(DUMPBUFSIZE - 1);
715 		Lseek(fd, pos, flag);
716 		(void)Read(fd, buf, sizeof(buf));
717 		j = off & (DUMPBUFSIZE - 1);
718 		p = buf + j;
719 		i = size;
720 		if (i > DUMPBUFSIZE - j)
721 			i = DUMPBUFSIZE - j;
722 		memcpy(p, q, i);
723 		Lseek(fd, pos, flag);
724 		(void)Write(fd, buf, sizeof(buf));
725 		size -= i;
726 		q += i;
727 		off += i;
728 	}
729 }
730 
731 void
732 DumpRead(fd, bp, size, off, flag)
733 	int fd, size, flag;
734 	void *bp;
735 	off_t off;
736 {
737 	unsigned char buf[DUMPBUFSIZE], *p, *q;
738 	off_t pos;
739 	int i, j;
740 
741 	if (flag != L_SET) {
742 		syslog(LOG_ERR, "lseek: not LSET");
743 		exit(2);
744 	}
745 	q = bp;
746 	while (size) {
747 		pos = off & ~(DUMPBUFSIZE - 1);
748 		Lseek(fd, pos, flag);
749 		(void)Read(fd, buf, sizeof(buf));
750 		j = off & (DUMPBUFSIZE - 1);
751 		p = buf + j;
752 		i = size;
753 		if (i > DUMPBUFSIZE - j)
754 			i = DUMPBUFSIZE - j;
755 		memcpy(q, p, i);
756 		size -= i;
757 		q += i;
758 		off += i;
759 	}
760 }
761 
762 void
763 Write(fd, bp, size)
764 	int fd, size;
765 	void *bp;
766 {
767 	int n;
768 
769 	if ((n = write(fd, bp, size)) < size) {
770 		syslog(LOG_ERR, "write: %m");
771 		exit(1);
772 	}
773 }
774 
775 void
776 usage()
777 {
778 	(void)syslog(LOG_ERR, "usage: savecore [-cfkvz] [-N system] directory");
779 	exit(1);
780 }
781