dumpon.c revision 136110
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 * 4. Neither the name of the University nor the names of its contributors 148478Swollman * may be used to endorse or promote products derived from this software 158478Swollman * without specific prior written permission. 168478Swollman * 178478Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 188478Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198478Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208478Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 218478Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228478Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238478Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248478Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258478Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268478Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278478Swollman * SUCH DAMAGE. 288478Swollman */ 298478Swollman 30114589Sobrien#if 0 318478Swollman#ifndef lint 3236999Scharnierstatic const char copyright[] = 338478Swollman"@(#) Copyright (c) 1980, 1993\n\ 348478Swollman The Regents of the University of California. All rights reserved.\n"; 358478Swollman#endif /* not lint */ 368478Swollman 378478Swollman#ifndef lint 3836999Scharnierstatic char sccsid[] = "From: @(#)swapon.c 8.1 (Berkeley) 6/5/93"; 39114589Sobrien#endif /* not lint */ 4036999Scharnier#endif 41114589Sobrien#include <sys/cdefs.h> 42114589Sobrien__FBSDID("$FreeBSD: head/sbin/dumpon/dumpon.c 136110 2004-10-04 06:54:19Z des $"); 438478Swollman 44136104Sdes#include <sys/param.h> 45136104Sdes#include <sys/disk.h> 46136104Sdes#include <sys/sysctl.h> 47136104Sdes 4836999Scharnier#include <err.h> 49136104Sdes#include <fcntl.h> 50136104Sdes#include <paths.h> 51136104Sdes#include <stdint.h> 528478Swollman#include <stdio.h> 5378732Sdd#include <stdlib.h> 5496381Salfred#include <string.h> 55136104Sdes#include <sysexits.h> 568478Swollman#include <unistd.h> 578478Swollman 58136104Sdesstatic int verbose; 598478Swollman 60136104Sdesstatic void 61136104Sdesusage(void) 62136104Sdes{ 63136104Sdes fprintf(stderr, "%s\n%s\n", 64136104Sdes "usage: dumpon [-v] special_file", 65136104Sdes " dumpon [-v] off"); 66136104Sdes exit(EX_USAGE); 67136104Sdes} 68136104Sdes 69136104Sdesstatic void 70136104Sdescheck_size(int fd, const char *fn) 71136104Sdes{ 72136104Sdes int name[] = { CTL_HW, HW_PHYSMEM }; 73136104Sdes size_t namelen = sizeof name / sizeof *name; 74136104Sdes unsigned long physmem; 75136104Sdes size_t len = sizeof physmem; 76136104Sdes off_t mediasize; 77136104Sdes 78136104Sdes if (sysctl(name, namelen, &physmem, &len, NULL, 0) != 0) 79136104Sdes err(EX_OSERR, "can't get memory size"); 80136104Sdes if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0) 81136104Sdes err(EX_OSERR, "%s: can't get size", fn); 82136110Sdes if ((uintmax_t)mediasize < (uintmax_t)physmem) { 83136104Sdes if (verbose) 84136104Sdes printf("%s is smaller than physical memory\n", fn); 85136104Sdes exit(EX_IOERR); 86136104Sdes } 87136104Sdes} 88136104Sdes 898478Swollmanint 9092542Simpmain(int argc, char *argv[]) 918478Swollman{ 92136104Sdes int ch; 9393491Sphk int i, fd; 9493491Sphk u_int u; 958478Swollman 9624359Simp while ((ch = getopt(argc, argv, "v")) != -1) 978478Swollman switch((char)ch) { 988478Swollman case 'v': 998478Swollman verbose = 1; 1008478Swollman break; 1018478Swollman default: 1028478Swollman usage(); 1038478Swollman } 104136104Sdes 105136104Sdes argc -= optind; 1068478Swollman argv += optind; 1078478Swollman 108136104Sdes if (argc != 1) 1098478Swollman usage(); 1108478Swollman 111136104Sdes if (strcmp(argv[0], "off") != 0) { 11293491Sphk fd = open(argv[0], O_RDONLY); 11393491Sphk if (fd < 0) 1148478Swollman err(EX_OSFILE, "%s", argv[0]); 115136104Sdes check_size(fd, argv[0]); 11693491Sphk u = 0; 11794272Sphk i = ioctl(fd, DIOCSKERNELDUMP, &u); 11893491Sphk u = 1; 11994272Sphk i = ioctl(fd, DIOCSKERNELDUMP, &u); 12093491Sphk if (i == 0 && verbose) 12193491Sphk printf("kernel dumps on %s\n", argv[0]); 1228478Swollman } else { 12393491Sphk fd = open(_PATH_DEVNULL, O_RDONLY); 12493491Sphk if (fd < 0) 12593491Sphk err(EX_OSFILE, "%s", _PATH_DEVNULL); 12693491Sphk u = 0; 12794272Sphk i = ioctl(fd, DIOCSKERNELDUMP, &u); 12893491Sphk if (i == 0 && verbose) 12993491Sphk printf("kernel dumps disabled\n"); 1308478Swollman } 13193491Sphk if (i < 0) 13294272Sphk err(EX_OSERR, "ioctl(DIOCSKERNELDUMP)"); 1338478Swollman 13493491Sphk exit (0); 1358478Swollman} 136