1 /* 2 * Copyright (c) 1980, 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char copyright[] = 33 "@(#) Copyright (c) 1980, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "From: @(#)swapon.c 8.1 (Berkeley) 6/5/93"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/disk.h> 46 #include <sys/sysctl.h> 47 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <paths.h> 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <sysexits.h> 57 #include <unistd.h> 58 59 static int verbose; 60 61 static void 62 usage(void) 63 { 64 fprintf(stderr, "%s\n%s\n%s\n", 65 "usage: dumpon [-v] special_file", 66 " dumpon [-v] off", 67 " dumpon [-v] -l"); 68 exit(EX_USAGE); 69 } 70 71 static void 72 check_size(int fd, const char *fn) 73 { 74 int name[] = { CTL_HW, HW_PHYSMEM }; 75 size_t namelen = sizeof(name) / sizeof(*name); 76 unsigned long physmem; 77 size_t len; 78 off_t mediasize; 79 int minidump; 80 81 len = sizeof(minidump); 82 if (sysctlbyname("debug.minidump", &minidump, &len, NULL, 0) == 0 && 83 minidump == 1) 84 return; 85 len = sizeof(physmem); 86 if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0) 87 err(EX_OSERR, "can't get memory size"); 88 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0) 89 err(EX_OSERR, "%s: can't get size", fn); 90 if ((uintmax_t)mediasize < (uintmax_t)physmem) { 91 if (verbose) 92 printf("%s is smaller than physical memory\n", fn); 93 exit(EX_IOERR); 94 } 95 } 96 97 static void 98 listdumpdev(void) 99 { 100 char dumpdev[PATH_MAX]; 101 size_t len; 102 const char *sysctlname = "kern.shutdown.dumpdevname"; 103 104 len = sizeof(dumpdev); 105 if (sysctlbyname(sysctlname, &dumpdev, &len, NULL, 0) != 0) { 106 if (errno == ENOMEM) { 107 err(EX_OSERR, "Kernel returned too large of a buffer for '%s'\n", 108 sysctlname); 109 } else { 110 err(EX_OSERR, "Sysctl get '%s'\n", sysctlname); 111 } 112 } 113 if (verbose) { 114 printf("kernel dumps on "); 115 } 116 if (strlen(dumpdev) == 0) { 117 printf("%s\n", _PATH_DEVNULL); 118 } else { 119 printf("%s\n", dumpdev); 120 } 121 } 122 123 int 124 main(int argc, char *argv[]) 125 { 126 int ch; 127 int i, fd; 128 u_int u; 129 int do_listdumpdev = 0; 130 131 while ((ch = getopt(argc, argv, "lv")) != -1) 132 switch((char)ch) { 133 case 'l': 134 do_listdumpdev = 1; 135 break; 136 case 'v': 137 verbose = 1; 138 break; 139 default: 140 usage(); 141 } 142 143 argc -= optind; 144 argv += optind; 145 146 if (do_listdumpdev) { 147 listdumpdev(); 148 exit(EX_OK); 149 } 150 151 if (argc != 1) 152 usage(); 153 154 if (strcmp(argv[0], "off") != 0) { 155 fd = open(argv[0], O_RDONLY); 156 if (fd < 0) 157 err(EX_OSFILE, "%s", argv[0]); 158 check_size(fd, argv[0]); 159 u = 0; 160 i = ioctl(fd, DIOCSKERNELDUMP, &u); 161 u = 1; 162 i = ioctl(fd, DIOCSKERNELDUMP, &u); 163 if (i == 0 && verbose) 164 printf("kernel dumps on %s\n", argv[0]); 165 } else { 166 fd = open(_PATH_DEVNULL, O_RDONLY); 167 if (fd < 0) 168 err(EX_OSFILE, "%s", _PATH_DEVNULL); 169 u = 0; 170 i = ioctl(fd, DIOCSKERNELDUMP, &u); 171 if (i == 0 && verbose) 172 printf("kernel dumps disabled\n"); 173 } 174 if (i < 0) 175 err(EX_OSERR, "ioctl(DIOCSKERNELDUMP)"); 176 177 exit (0); 178 } 179