dumpon.c revision 21673
1207753Smm/*
2207753Smm * Copyright (c) 1980, 1993
3207753Smm *	The Regents of the University of California.  All rights reserved.
4207753Smm *
5207753Smm * Redistribution and use in source and binary forms, with or without
6207753Smm * modification, are permitted provided that the following conditions
7207753Smm * are met:
8207753Smm * 1. Redistributions of source code must retain the above copyright
9207753Smm *    notice, this list of conditions and the following disclaimer.
10207753Smm * 2. Redistributions in binary form must reproduce the above copyright
11207753Smm *    notice, this list of conditions and the following disclaimer in the
12207753Smm *    documentation and/or other materials provided with the distribution.
13207753Smm * 3. All advertising materials mentioning features or use of this software
14207753Smm *    must display the following acknowledgement:
15207753Smm *	This product includes software developed by the University of
16207753Smm *	California, Berkeley and its contributors.
17207753Smm * 4. Neither the name of the University nor the names of its contributors
18207753Smm *    may be used to endorse or promote products derived from this software
19207753Smm *    without specific prior written permission.
20207753Smm *
21207753Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22207753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23207753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24207753Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25207753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26213700Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27292588Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28292588Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29292588Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30207753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31207753Smm * SUCH DAMAGE.
32207753Smm */
33207753Smm
34207753Smm#ifndef lint
35207753Smmstatic char copyright[] =
36207753Smm"@(#) Copyright (c) 1980, 1993\n\
37207753Smm	The Regents of the University of California.  All rights reserved.\n";
38207753Smm#endif /* not lint */
39207753Smm
40207753Smm#ifndef lint
41207753Smm/*static char sccsid[] = "From: @(#)swapon.c	8.1 (Berkeley) 6/5/93";*/
42207753Smmstatic const char rcsid[] =
43263285Sdelphij	"$FreeBSD: head/sbin/dumpon/dumpon.c 21673 1997-01-14 07:20:47Z jkh $";
44207753Smm#endif /* not lint */
45207753Smm
46263285Sdelphij#include <errno.h>
47207753Smm#include <stdio.h>
48207753Smm#include <stdlib.h>
49207753Smm#include <unistd.h>
50207753Smm#include <sys/param.h>
51207753Smm#include <sys/types.h>
52207753Smm#include <sys/sysctl.h>
53207753Smm#include <sys/stat.h>
54312518Sdelphij#include <sysexits.h>
55292588Sdelphij#include <err.h>
56292588Sdelphij
57292588Sdelphijvoid	usage __P((void)) __dead2;
58292588Sdelphijstatic char *whoami;
59292588Sdelphij
60292588Sdelphijint
61207753Smmmain(int argc, char **argv)
62292588Sdelphij{
63207753Smm	extern char *optarg;
64207753Smm	extern int optind;
65207753Smm	int ch, verbose, rv;
66207753Smm	struct stat stab;
67207753Smm	int mib[2];
68207753Smm
69207753Smm	verbose = rv = 0;
70207753Smm	whoami = argv[0];
71207753Smm	while ((ch = getopt(argc, argv, "v")) != EOF)
72263285Sdelphij		switch((char)ch) {
73263285Sdelphij		case 'v':
74207753Smm			verbose = 1;
75213700Smm			break;
76213700Smm		case '?':
77213700Smm		default:
78213700Smm			usage();
79213700Smm		}
80213700Smm	argv += optind;
81213700Smm
82213700Smm	if (!argv[0] || argv[1])
83207753Smm		usage();
84207753Smm
85207753Smm	if (strcmp(argv[0], "off")) {
86207753Smm		rv = stat(argv[0], &stab);
87207753Smm		if (rv) {
88263285Sdelphij			err(EX_OSFILE, "%s", argv[0]);
89263285Sdelphij		}
90263285Sdelphij
91263285Sdelphij		if (!S_ISBLK(stab.st_mode)) {
92263285Sdelphij			errx(EX_USAGE, "%s: must specify a block device",
93263285Sdelphij			     argv[0]);
94263285Sdelphij		}
95263285Sdelphij	} else {
96263285Sdelphij		stab.st_rdev = NODEV;
97263285Sdelphij	}
98207753Smm
99207753Smm	mib[0] = CTL_KERN;
100263285Sdelphij	mib[1] = KERN_DUMPDEV;
101263285Sdelphij
102207753Smm	rv = sysctl(mib, 2, (void *)0, (size_t *)0, &stab.st_rdev,
103207753Smm		    sizeof stab.st_rdev);
104207753Smm	if (rv) {
105207753Smm		err(EX_OSERR, "sysctl: kern.dumpdev");
106207753Smm	}
107207753Smm
108207753Smm	if (verbose) {
109207753Smm		if (stab.st_rdev == NODEV) {
110207753Smm			printf("%s: crash dumps disabled\n", whoami);
111207753Smm		} else {
112207753Smm			printf("%s: crash dumps to %s (%lu, %lu)\n",
113207753Smm			       whoami, argv[0],
114207753Smm			       (unsigned long)major(stab.st_rdev),
115207753Smm			       (unsigned long)minor(stab.st_rdev));
116263285Sdelphij		}
117263285Sdelphij	}
118263285Sdelphij
119263285Sdelphij	return 0;
120263285Sdelphij}
121263285Sdelphij
122207753Smmvoid
123207753Smmusage()
124207753Smm{
125207753Smm	fprintf(stderr,
126223935Smm		"usage: %s [-v] special_file\n"
127207753Smm		"       %s [-v] off\n", whoami, whoami);
128207753Smm	exit(EX_USAGE);
129207753Smm}
130207753Smm