xref: /freebsd/sbin/dumpon/dumpon.c (revision b54a17cd2f06396001e486d55e165f42cc68d5d6)
183f9dfabSGarrett Wollman /*
283f9dfabSGarrett Wollman  * Copyright (c) 1980, 1993
383f9dfabSGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
483f9dfabSGarrett Wollman  *
583f9dfabSGarrett Wollman  * Redistribution and use in source and binary forms, with or without
683f9dfabSGarrett Wollman  * modification, are permitted provided that the following conditions
783f9dfabSGarrett Wollman  * are met:
883f9dfabSGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
983f9dfabSGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1083f9dfabSGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1183f9dfabSGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1283f9dfabSGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1383f9dfabSGarrett Wollman  * 4. Neither the name of the University nor the names of its contributors
1483f9dfabSGarrett Wollman  *    may be used to endorse or promote products derived from this software
1583f9dfabSGarrett Wollman  *    without specific prior written permission.
1683f9dfabSGarrett Wollman  *
1783f9dfabSGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1883f9dfabSGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1983f9dfabSGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2083f9dfabSGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2183f9dfabSGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2283f9dfabSGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2383f9dfabSGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2483f9dfabSGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2583f9dfabSGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2683f9dfabSGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2783f9dfabSGarrett Wollman  * SUCH DAMAGE.
2883f9dfabSGarrett Wollman  */
2983f9dfabSGarrett Wollman 
30c69284caSDavid E. O'Brien #if 0
3183f9dfabSGarrett Wollman #ifndef lint
328f034b11SPhilippe Charnier static const char copyright[] =
3383f9dfabSGarrett Wollman "@(#) Copyright (c) 1980, 1993\n\
3483f9dfabSGarrett Wollman 	The Regents of the University of California.  All rights reserved.\n";
3583f9dfabSGarrett Wollman #endif /* not lint */
3683f9dfabSGarrett Wollman 
3783f9dfabSGarrett Wollman #ifndef lint
388f034b11SPhilippe Charnier static char sccsid[] = "From: @(#)swapon.c	8.1 (Berkeley) 6/5/93";
3983f9dfabSGarrett Wollman #endif /* not lint */
40c69284caSDavid E. O'Brien #endif
41c69284caSDavid E. O'Brien #include <sys/cdefs.h>
42c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
4383f9dfabSGarrett Wollman 
4483f9dfabSGarrett Wollman #include <sys/param.h>
452dd527b3SPoul-Henning Kamp #include <sys/disk.h>
46c0046e26SDag-Erling Smørgrav #include <sys/sysctl.h>
4783f9dfabSGarrett Wollman 
48c0046e26SDag-Erling Smørgrav #include <err.h>
49c0046e26SDag-Erling Smørgrav #include <fcntl.h>
50c0046e26SDag-Erling Smørgrav #include <paths.h>
51c0046e26SDag-Erling Smørgrav #include <stdint.h>
52c0046e26SDag-Erling Smørgrav #include <stdio.h>
53c0046e26SDag-Erling Smørgrav #include <stdlib.h>
54c0046e26SDag-Erling Smørgrav #include <string.h>
55c0046e26SDag-Erling Smørgrav #include <sysexits.h>
56c0046e26SDag-Erling Smørgrav #include <unistd.h>
57c0046e26SDag-Erling Smørgrav 
58c0046e26SDag-Erling Smørgrav static int	verbose;
59c0046e26SDag-Erling Smørgrav 
60c0046e26SDag-Erling Smørgrav static void
61c0046e26SDag-Erling Smørgrav usage(void)
62c0046e26SDag-Erling Smørgrav {
63c0046e26SDag-Erling Smørgrav 	fprintf(stderr, "%s\n%s\n",
64c0046e26SDag-Erling Smørgrav 	    "usage: dumpon [-v] special_file",
65c0046e26SDag-Erling Smørgrav 	    "       dumpon [-v] off");
66c0046e26SDag-Erling Smørgrav 	exit(EX_USAGE);
67c0046e26SDag-Erling Smørgrav }
68c0046e26SDag-Erling Smørgrav 
69c0046e26SDag-Erling Smørgrav static void
70c0046e26SDag-Erling Smørgrav check_size(int fd, const char *fn)
71c0046e26SDag-Erling Smørgrav {
72c0046e26SDag-Erling Smørgrav 	int name[] = { CTL_HW, HW_PHYSMEM };
73b54a17cdSJohn Baldwin 	size_t namelen = sizeof(name) / sizeof(*name);
74c0046e26SDag-Erling Smørgrav 	unsigned long physmem;
75ce893772SPaul Saab 	size_t len;
76c0046e26SDag-Erling Smørgrav 	off_t mediasize;
77ce893772SPaul Saab 	int minidump;
78c0046e26SDag-Erling Smørgrav 
79b54a17cdSJohn Baldwin 	len = sizeof(minidump);
80ce893772SPaul Saab 	if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 &&
81ce893772SPaul Saab 	    minidump == 1)
82ce893772SPaul Saab 		return;
83b54a17cdSJohn Baldwin 	len = sizeof(physmem);
84c0046e26SDag-Erling Smørgrav 	if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0)
85c0046e26SDag-Erling Smørgrav 		err(EX_OSERR, "can't get memory size");
86c0046e26SDag-Erling Smørgrav 	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0)
87c0046e26SDag-Erling Smørgrav 		err(EX_OSERR, "%s: can't get size", fn);
889872f15dSDag-Erling Smørgrav 	if ((uintmax_t)mediasize < (uintmax_t)physmem) {
89c0046e26SDag-Erling Smørgrav 		if (verbose)
90c0046e26SDag-Erling Smørgrav 			printf("%s is smaller than physical memory\n", fn);
91c0046e26SDag-Erling Smørgrav 		exit(EX_IOERR);
92c0046e26SDag-Erling Smørgrav 	}
93c0046e26SDag-Erling Smørgrav }
9483f9dfabSGarrett Wollman 
9583f9dfabSGarrett Wollman int
96d1a939c1SWarner Losh main(int argc, char *argv[])
9783f9dfabSGarrett Wollman {
98c0046e26SDag-Erling Smørgrav 	int ch;
999e9c1cadSPoul-Henning Kamp 	int i, fd;
1009e9c1cadSPoul-Henning Kamp 	u_int u;
10183f9dfabSGarrett Wollman 
1028d64695cSWarner Losh 	while ((ch = getopt(argc, argv, "v")) != -1)
10383f9dfabSGarrett Wollman 		switch((char)ch) {
10483f9dfabSGarrett Wollman 		case 'v':
10583f9dfabSGarrett Wollman 			verbose = 1;
10683f9dfabSGarrett Wollman 			break;
10783f9dfabSGarrett Wollman 		default:
10883f9dfabSGarrett Wollman 			usage();
10983f9dfabSGarrett Wollman 		}
110c0046e26SDag-Erling Smørgrav 
111c0046e26SDag-Erling Smørgrav 	argc -= optind;
11283f9dfabSGarrett Wollman 	argv += optind;
11383f9dfabSGarrett Wollman 
114c0046e26SDag-Erling Smørgrav 	if (argc != 1)
11583f9dfabSGarrett Wollman 		usage();
11683f9dfabSGarrett Wollman 
117c0046e26SDag-Erling Smørgrav 	if (strcmp(argv[0], "off") != 0) {
1189e9c1cadSPoul-Henning Kamp 		fd = open(argv[0], O_RDONLY);
1199e9c1cadSPoul-Henning Kamp 		if (fd < 0)
12083f9dfabSGarrett Wollman 			err(EX_OSFILE, "%s", argv[0]);
121c0046e26SDag-Erling Smørgrav 		check_size(fd, argv[0]);
1229e9c1cadSPoul-Henning Kamp 		u = 0;
1237f086a08SPoul-Henning Kamp 		i = ioctl(fd, DIOCSKERNELDUMP, &u);
1249e9c1cadSPoul-Henning Kamp 		u = 1;
1257f086a08SPoul-Henning Kamp 		i = ioctl(fd, DIOCSKERNELDUMP, &u);
1269e9c1cadSPoul-Henning Kamp 		if (i == 0 && verbose)
1279e9c1cadSPoul-Henning Kamp 			printf("kernel dumps on %s\n", argv[0]);
12883f9dfabSGarrett Wollman 	} else {
1299e9c1cadSPoul-Henning Kamp 		fd = open(_PATH_DEVNULL, O_RDONLY);
1309e9c1cadSPoul-Henning Kamp 		if (fd < 0)
1319e9c1cadSPoul-Henning Kamp 			err(EX_OSFILE, "%s", _PATH_DEVNULL);
1329e9c1cadSPoul-Henning Kamp 		u = 0;
1337f086a08SPoul-Henning Kamp 		i = ioctl(fd, DIOCSKERNELDUMP, &u);
1349e9c1cadSPoul-Henning Kamp 		if (i == 0 && verbose)
1359e9c1cadSPoul-Henning Kamp 			printf("kernel dumps disabled\n");
13683f9dfabSGarrett Wollman 	}
1379e9c1cadSPoul-Henning Kamp 	if (i < 0)
1387f086a08SPoul-Henning Kamp 		err(EX_OSERR, "ioctl(DIOCSKERNELDUMP)");
13983f9dfabSGarrett Wollman 
1409e9c1cadSPoul-Henning Kamp 	exit (0);
14183f9dfabSGarrett Wollman }
142