xref: /openbsd-src/lib/libutil/readlabel.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: readlabel.c,v 1.12 2014/06/30 00:25:37 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/param.h>
29 #include <sys/disk.h>
30 #include <sys/dkio.h>
31 #define DKTYPENAMES
32 #include <sys/disklabel.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <stdio.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <paths.h>
40 #include <string.h>
41 #include <unistd.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 char *
50 readlabelfs(char *device, int verbose)
51 {
52 	char rpath[MAXPATHLEN];
53 	struct dk_diskmap dm;
54 	struct disklabel dk;
55 	char part, *type;
56 	struct stat sbuf;
57 	int fd = -1;
58 
59 	/* Perform disk mapping if device is given as a DUID. */
60 	if (isduid(device, 0)) {
61 		if ((fd = open("/dev/diskmap", O_RDONLY)) != -1) {
62 			bzero(&dm, sizeof(struct dk_diskmap));
63 			strlcpy(rpath, device, sizeof(rpath));
64 			part = rpath[strlen(rpath) - 1];
65 			dm.device = rpath;
66 			dm.fd = fd;
67 			dm.flags = DM_OPENPART;
68 			if (ioctl(fd, DIOCMAP, &dm) == -1)
69 				close(fd);
70 			else
71 				goto disklabel;
72 		}
73 	}
74 
75 	/* Assuming device is of the form /dev/??p, build a raw partition. */
76 	if (stat(device, &sbuf) < 0) {
77 		if (verbose)
78 			warn("%s", device);
79 		return (NULL);
80 	}
81 	switch (sbuf.st_mode & S_IFMT) {
82 	case S_IFCHR:
83 		/* Ok... already a raw device.  Hmm. */
84 		strlcpy(rpath, device, sizeof(rpath));
85 
86 		/* Change partition name. */
87 		part = rpath[strlen(rpath) - 1];
88 		rpath[strlen(rpath) - 1] = 'a' + getrawpartition();
89 		break;
90 	case S_IFBLK:
91 		if (strlen(device) > sizeof(_PATH_DEV) - 1) {
92 			snprintf(rpath, sizeof(rpath), "%sr%s", _PATH_DEV,
93 			    &device[sizeof(_PATH_DEV) - 1]);
94 			/* Change partition name. */
95 			part = rpath[strlen(rpath) - 1];
96 			rpath[strlen(rpath) - 1] = 'a' + getrawpartition();
97 			break;
98 		}
99 		/* FALLTHROUGH */
100 	default:
101 		if (verbose)
102 			warnx("%s: not a device node", device);
103 		return (NULL);
104 	}
105 
106 	/* If rpath doesn't exist, change that partition back. */
107 	fd = open(rpath, O_RDONLY);
108 	if (fd < 0) {
109 		if (errno == ENOENT) {
110 			rpath[strlen(rpath) - 1] = part;
111 
112 			fd = open(rpath, O_RDONLY);
113 			if (fd < 0) {
114 				if (verbose)
115 					warn("%s", rpath);
116 				return (NULL);
117 			}
118 		} else {
119 			if (verbose)
120 				warn("%s", rpath);
121 			return (NULL);
122 		}
123 	}
124 
125 disklabel:
126 
127 	if (ioctl(fd, DIOCGDINFO, &dk) < 0) {
128 		if (verbose)
129 			warn("%s: couldn't read disklabel", rpath);
130 		close(fd);
131 		return (NULL);
132 	}
133 	close(fd);
134 
135 	if (dk.d_partitions[part - 'a'].p_fstype >= FSMAXTYPES) {
136 		if (verbose)
137 			warnx("%s: bad filesystem type in label", rpath);
138 		return (NULL);
139 	}
140 
141 	type = fstypesnames[dk.d_partitions[part - 'a'].p_fstype];
142 	return ((type[0] == '\0') ? NULL : type);
143 }
144