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