1 /* $OpenBSD: readlabel.c,v 1.7 2002/02/21 16:22:23 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1996, Jason Downs. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <stdio.h> 30 #include <err.h> 31 #include <errno.h> 32 #include <fcntl.h> 33 #include <paths.h> 34 #include <string.h> 35 #include <unistd.h> 36 #include <sys/dkio.h> 37 #define DKTYPENAMES 38 #include <sys/disklabel.h> 39 #include <sys/ioctl.h> 40 #include <sys/param.h> 41 #include <sys/stat.h> 42 43 #include "util.h" 44 45 /* 46 * Try to get a disklabel for the specified device, and return mount_xxx 47 * style filesystem type name for the specified partition. 48 */ 49 50 char *readlabelfs(device, verbose) 51 char *device; 52 int verbose; 53 { 54 char rpath[MAXPATHLEN]; 55 char part, *type; 56 struct stat sbuf; 57 struct disklabel dk; 58 int fd; 59 60 /* Assuming device is of the form /dev/??p, build a raw partition. */ 61 if (stat(device, &sbuf) < 0) { 62 if (verbose) 63 warn("%s", device); 64 return(NULL); 65 } 66 switch(sbuf.st_mode & S_IFMT) { 67 case S_IFCHR: 68 /* Ok... already a raw device. Hmm. */ 69 strncpy(rpath, device, sizeof(rpath)); 70 rpath[sizeof(rpath) - 1] = '\0'; 71 72 /* Change partition name. */ 73 part = rpath[strlen(rpath) - 1]; 74 rpath[strlen(rpath) - 1] = 'a' + getrawpartition(); 75 break; 76 case S_IFBLK: 77 if (strlen(device) > sizeof(_PATH_DEV) - 1) { 78 snprintf(rpath, sizeof(rpath), "%sr%s", _PATH_DEV, 79 &device[sizeof(_PATH_DEV) - 1]); 80 81 /* Change partition name. */ 82 part = rpath[strlen(rpath) - 1]; 83 rpath[strlen(rpath) - 1] = 'a' + getrawpartition(); 84 break; 85 } 86 default: 87 if (verbose) 88 warnx("%s: not a device node", device); 89 return(NULL); 90 } 91 92 /* If rpath doesn't exist, change that partition back. */ 93 fd = open(rpath, O_RDONLY); 94 if (fd < 0) { 95 if (errno == ENOENT) { 96 rpath[strlen(rpath) - 1] = part; 97 98 fd = open(rpath, O_RDONLY); 99 if (fd < 0) { 100 if (verbose) 101 warn("%s", rpath); 102 return(NULL); 103 } 104 } else { 105 if (verbose) 106 warn("%s", rpath); 107 return(NULL); 108 } 109 } 110 if (ioctl(fd, DIOCGDINFO, &dk) < 0) { 111 if (verbose) 112 warn("%s: couldn't read disklabel", rpath); 113 close(fd); 114 return(NULL); 115 } 116 close(fd); 117 118 if (dk.d_partitions[part - 'a'].p_fstype > FSMAXTYPES) { 119 if (verbose) 120 warnx("%s: bad filesystem type in label", rpath); 121 return(NULL); 122 } 123 124 type = fstypesnames[dk.d_partitions[part - 'a'].p_fstype]; 125 return((type[0] == '\0') ? NULL : type); 126 } 127