1 /* $OpenBSD: readlabel.c,v 1.2 1996/12/04 21:25:33 downsj 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 <util.h> 36 #include <sys/dkio.h> 37 #define DKTYPENAMES 38 #include <sys/disklabel.h> 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 42 /* 43 * Try to get a disklabel for the specified device, and return mount_xxx 44 * style filesystem type name for the specified partition. 45 */ 46 47 char *readlabelfs(device) 48 char *device; 49 { 50 char rpath[MAXPATHLEN]; 51 char part, *type; 52 struct stat sbuf; 53 struct disklabel dk; 54 int fd; 55 56 /* Assuming device is of the form /dev/??p, build a raw partition. */ 57 if (stat(device, &sbuf) < 0) { 58 warn("%s", device); 59 return(NULL); 60 } 61 switch(sbuf.st_mode & S_IFMT) { 62 case S_IFCHR: 63 /* Ok... already a raw device. Hmm. */ 64 strncpy(rpath, device, sizeof(rpath)); 65 rpath[sizeof(rpath) - 1] = '\0'; 66 67 /* Change partition name. */ 68 part = rpath[strlen(rpath) - 1]; 69 rpath[strlen(rpath) - 1] = 'a' + getrawpartition(); 70 break; 71 case S_IFBLK: 72 if (strlen(device) > strlen(_PATH_DEV)) { 73 snprintf(rpath, sizeof(rpath), "%sr%s", _PATH_DEV, 74 &device[strlen(_PATH_DEV)]); 75 76 /* Change partition name. */ 77 part = rpath[strlen(rpath) - 1]; 78 rpath[strlen(rpath) - 1] = 'a' + getrawpartition(); 79 break; 80 } 81 default: 82 warnx("%s: not a device node", device); 83 return(NULL); 84 } 85 86 /* If rpath doesn't exist, change that partition back. */ 87 fd = open(rpath, O_RDONLY); 88 if (fd < 0) { 89 if (errno == ENOENT) { 90 rpath[strlen(rpath) - 1] = part; 91 92 fd = open(rpath, O_RDONLY); 93 if (fd < 0) { 94 warn("%s", rpath); 95 return(NULL); 96 } 97 } else { 98 warn("%s", rpath); 99 return(NULL); 100 } 101 } 102 if (ioctl(fd, DIOCGDINFO, &dk) < 0) { 103 warn("%s: couldn't read disklabel", rpath); 104 close(fd); 105 return(NULL); 106 } 107 close(fd); 108 109 if (dk.d_partitions[part - 'a'].p_fstype > FSMAXTYPES) { 110 warnx("%s: bad filesystem type in label", rpath); 111 return(NULL); 112 } 113 114 type = fstypesnames[dk.d_partitions[part - 'a'].p_fstype]; 115 return((type[0] == '\0') ? NULL : type); 116 } 117