dumpon.c revision 78732
18478Swollman/*
28478Swollman * Copyright (c) 1980, 1993
38478Swollman *	The Regents of the University of California.  All rights reserved.
48478Swollman *
58478Swollman * Redistribution and use in source and binary forms, with or without
68478Swollman * modification, are permitted provided that the following conditions
78478Swollman * are met:
88478Swollman * 1. Redistributions of source code must retain the above copyright
98478Swollman *    notice, this list of conditions and the following disclaimer.
108478Swollman * 2. Redistributions in binary form must reproduce the above copyright
118478Swollman *    notice, this list of conditions and the following disclaimer in the
128478Swollman *    documentation and/or other materials provided with the distribution.
138478Swollman * 3. All advertising materials mentioning features or use of this software
148478Swollman *    must display the following acknowledgement:
158478Swollman *	This product includes software developed by the University of
168478Swollman *	California, Berkeley and its contributors.
178478Swollman * 4. Neither the name of the University nor the names of its contributors
188478Swollman *    may be used to endorse or promote products derived from this software
198478Swollman *    without specific prior written permission.
208478Swollman *
218478Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228478Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238478Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248478Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258478Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268478Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278478Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288478Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298478Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308478Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318478Swollman * SUCH DAMAGE.
328478Swollman */
338478Swollman
348478Swollman#ifndef lint
3536999Scharnierstatic const char copyright[] =
368478Swollman"@(#) Copyright (c) 1980, 1993\n\
378478Swollman	The Regents of the University of California.  All rights reserved.\n";
388478Swollman#endif /* not lint */
398478Swollman
408478Swollman#ifndef lint
4136999Scharnier#if 0
4236999Scharnierstatic char sccsid[] = "From: @(#)swapon.c	8.1 (Berkeley) 6/5/93";
4336999Scharnier#endif
448478Swollmanstatic const char rcsid[] =
4550476Speter  "$FreeBSD: head/sbin/dumpon/dumpon.c 78732 2001-06-24 23:04:23Z dd $";
468478Swollman#endif /* not lint */
478478Swollman
4836999Scharnier#include <err.h>
498478Swollman#include <stdio.h>
5078732Sdd#include <stdlib.h>
5178732Sdd#include <string.h>
528478Swollman#include <unistd.h>
538478Swollman#include <sys/param.h>
548478Swollman#include <sys/sysctl.h>
558478Swollman#include <sys/stat.h>
568478Swollman#include <sysexits.h>
578478Swollman
588478Swollmanvoid	usage __P((void)) __dead2;
598478Swollman
608478Swollmanint
618478Swollmanmain(int argc, char **argv)
628478Swollman{
638478Swollman	int ch, verbose, rv;
648478Swollman	struct stat stab;
658478Swollman	int mib[2];
668478Swollman
678478Swollman	verbose = rv = 0;
6824359Simp	while ((ch = getopt(argc, argv, "v")) != -1)
698478Swollman		switch((char)ch) {
708478Swollman		case 'v':
718478Swollman			verbose = 1;
728478Swollman			break;
738478Swollman		case '?':
748478Swollman		default:
758478Swollman			usage();
768478Swollman		}
778478Swollman	argv += optind;
788478Swollman
798478Swollman	if (!argv[0] || argv[1])
808478Swollman		usage();
818478Swollman
828478Swollman	if (strcmp(argv[0], "off")) {
838478Swollman		rv = stat(argv[0], &stab);
848478Swollman		if (rv) {
858478Swollman			err(EX_OSFILE, "%s", argv[0]);
868478Swollman		}
878478Swollman
8855415Sphk		if (!S_ISCHR(stab.st_mode)) {
8955415Sphk			errx(EX_USAGE,
9055415Sphk			     "%s: must specify a character disk device",
918478Swollman			     argv[0]);
928478Swollman		}
938478Swollman	} else {
948478Swollman		stab.st_rdev = NODEV;
958478Swollman	}
968478Swollman
978478Swollman	mib[0] = CTL_KERN;
988478Swollman	mib[1] = KERN_DUMPDEV;
998478Swollman
1008871Srgrimes	rv = sysctl(mib, 2, (void *)0, (size_t *)0, &stab.st_rdev,
1018478Swollman		    sizeof stab.st_rdev);
1028478Swollman	if (rv) {
1038478Swollman		err(EX_OSERR, "sysctl: kern.dumpdev");
1048478Swollman	}
1058478Swollman
1068478Swollman	if (verbose) {
1078478Swollman		if (stab.st_rdev == NODEV) {
10836999Scharnier			printf("dumpon: crash dumps disabled\n");
1098478Swollman		} else {
11036999Scharnier			printf("dumpon: crash dumps to %s (%lu, %lu)\n",
11136999Scharnier			       argv[0],
1128478Swollman			       (unsigned long)major(stab.st_rdev),
1138478Swollman			       (unsigned long)minor(stab.st_rdev));
1148478Swollman		}
1158478Swollman	}
1168478Swollman
1178478Swollman	return 0;
1188478Swollman}
1198478Swollman
1208478Swollmanvoid
1218478Swollmanusage()
1228478Swollman{
1238478Swollman	fprintf(stderr,
12436999Scharnier		"usage: dumpon [-v] special_file\n"
12536999Scharnier		"       dumpon [-v] off\n");
1268478Swollman	exit(EX_USAGE);
1278478Swollman}
128