xref: /freebsd/sbin/dumpon/dumpon.c (revision ce893772bdbdda7b9c0c479c6aee9c7fe2a70689)
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 };
73c0046e26SDag-Erling Smørgrav 	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 
79ce893772SPaul Saab 
80ce893772SPaul Saab 	len = sizeof minidump;
81ce893772SPaul Saab 	if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 &&
82ce893772SPaul Saab 	    minidump == 1)
83ce893772SPaul Saab 		return;
84ce893772SPaul Saab 	len = sizeof physmem;
85c0046e26SDag-Erling Smørgrav 	if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0)
86c0046e26SDag-Erling Smørgrav 		err(EX_OSERR, "can't get memory size");
87c0046e26SDag-Erling Smørgrav 	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0)
88c0046e26SDag-Erling Smørgrav 		err(EX_OSERR, "%s: can't get size", fn);
899872f15dSDag-Erling Smørgrav 	if ((uintmax_t)mediasize < (uintmax_t)physmem) {
90c0046e26SDag-Erling Smørgrav 		if (verbose)
91c0046e26SDag-Erling Smørgrav 			printf("%s is smaller than physical memory\n", fn);
92c0046e26SDag-Erling Smørgrav 		exit(EX_IOERR);
93c0046e26SDag-Erling Smørgrav 	}
94c0046e26SDag-Erling Smørgrav }
9583f9dfabSGarrett Wollman 
9683f9dfabSGarrett Wollman int
97d1a939c1SWarner Losh main(int argc, char *argv[])
9883f9dfabSGarrett Wollman {
99c0046e26SDag-Erling Smørgrav 	int ch;
1009e9c1cadSPoul-Henning Kamp 	int i, fd;
1019e9c1cadSPoul-Henning Kamp 	u_int u;
10283f9dfabSGarrett Wollman 
1038d64695cSWarner Losh 	while ((ch = getopt(argc, argv, "v")) != -1)
10483f9dfabSGarrett Wollman 		switch((char)ch) {
10583f9dfabSGarrett Wollman 		case 'v':
10683f9dfabSGarrett Wollman 			verbose = 1;
10783f9dfabSGarrett Wollman 			break;
10883f9dfabSGarrett Wollman 		default:
10983f9dfabSGarrett Wollman 			usage();
11083f9dfabSGarrett Wollman 		}
111c0046e26SDag-Erling Smørgrav 
112c0046e26SDag-Erling Smørgrav 	argc -= optind;
11383f9dfabSGarrett Wollman 	argv += optind;
11483f9dfabSGarrett Wollman 
115c0046e26SDag-Erling Smørgrav 	if (argc != 1)
11683f9dfabSGarrett Wollman 		usage();
11783f9dfabSGarrett Wollman 
118c0046e26SDag-Erling Smørgrav 	if (strcmp(argv[0], "off") != 0) {
1199e9c1cadSPoul-Henning Kamp 		fd = open(argv[0], O_RDONLY);
1209e9c1cadSPoul-Henning Kamp 		if (fd < 0)
12183f9dfabSGarrett Wollman 			err(EX_OSFILE, "%s", argv[0]);
122c0046e26SDag-Erling Smørgrav 		check_size(fd, argv[0]);
1239e9c1cadSPoul-Henning Kamp 		u = 0;
1247f086a08SPoul-Henning Kamp 		i = ioctl(fd, DIOCSKERNELDUMP, &u);
1259e9c1cadSPoul-Henning Kamp 		u = 1;
1267f086a08SPoul-Henning Kamp 		i = ioctl(fd, DIOCSKERNELDUMP, &u);
1279e9c1cadSPoul-Henning Kamp 		if (i == 0 && verbose)
1289e9c1cadSPoul-Henning Kamp 			printf("kernel dumps on %s\n", argv[0]);
12983f9dfabSGarrett Wollman 	} else {
1309e9c1cadSPoul-Henning Kamp 		fd = open(_PATH_DEVNULL, O_RDONLY);
1319e9c1cadSPoul-Henning Kamp 		if (fd < 0)
1329e9c1cadSPoul-Henning Kamp 			err(EX_OSFILE, "%s", _PATH_DEVNULL);
1339e9c1cadSPoul-Henning Kamp 		u = 0;
1347f086a08SPoul-Henning Kamp 		i = ioctl(fd, DIOCSKERNELDUMP, &u);
1359e9c1cadSPoul-Henning Kamp 		if (i == 0 && verbose)
1369e9c1cadSPoul-Henning Kamp 			printf("kernel dumps disabled\n");
13783f9dfabSGarrett Wollman 	}
1389e9c1cadSPoul-Henning Kamp 	if (i < 0)
1397f086a08SPoul-Henning Kamp 		err(EX_OSERR, "ioctl(DIOCSKERNELDUMP)");
14083f9dfabSGarrett Wollman 
1419e9c1cadSPoul-Henning Kamp 	exit (0);
14283f9dfabSGarrett Wollman }
143