Fix some complier warnings.
[platform/upstream/cryptsetup.git] / lib / utils_devpath.c
1 /*
2  * devname - search for device name
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include "utils_dm.h"
32
33 char *crypt_lookup_dev(const char *dev_id);
34 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size);
35 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
36
37 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
38 {
39         struct dirent *entry;
40         struct stat st;
41         char *ptr;
42         char *result = NULL;
43         DIR *dir;
44         int space;
45
46         /* Ignore strange nested directories */
47         if (dir_level > max_level)
48                 return NULL;
49
50         path[PATH_MAX - 1] = '\0';
51         ptr = path + strlen(path);
52         *ptr++ = '/';
53         *ptr = '\0';
54         space = PATH_MAX - (ptr - path);
55
56         dir = opendir(path);
57         if (!dir)
58                 return NULL;
59
60         while((entry = readdir(dir))) {
61                 if (entry->d_name[0] == '.' ||
62                     !strncmp(entry->d_name, "..", 2))
63                         continue;
64
65                 if (dir_level == 0 &&
66                     (!strcmp(entry->d_name, "shm") ||
67                      !strcmp(entry->d_name, "fd") ||
68                      !strcmp(entry->d_name, "char") ||
69                      !strcmp(entry->d_name, "pts")))
70                         continue;
71
72                 strncpy(ptr, entry->d_name, space);
73                 if (stat(path, &st) < 0)
74                         continue;
75
76                 if (S_ISDIR(st.st_mode)) {
77                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
78                         if (result)
79                                 break;
80                 } else if (S_ISBLK(st.st_mode)) {
81                         /* workaround: ignore dm-X devices, these are internal kernel names */
82                         if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
83                                 continue;
84                         if (st.st_rdev == dev) {
85                                 result = strdup(path);
86                                 break;
87                         }
88                 }
89         }
90
91         closedir(dir);
92         return result;
93 }
94
95 /*
96  * Non-udev systemd need to scan for device here.
97  */
98 static char *lookup_dev_old(int major, int minor)
99 {
100         dev_t dev;
101         char *result = NULL, buf[PATH_MAX + 1];
102
103         dev = makedev(major, minor);
104         strncpy(buf, "/dev", PATH_MAX);
105         buf[PATH_MAX] = '\0';
106
107         /* First try low level device */
108         if ((result = __lookup_dev(buf, dev, 0, 0)))
109                 return result;
110
111         /* If it is dm, try DM dir  */
112         if (dm_is_dm_device(major, minor)) {
113                 strncpy(buf, dm_get_dir(), PATH_MAX);
114                 if ((result = __lookup_dev(buf, dev, 0, 0)))
115                         return result;
116         }
117
118         strncpy(buf, "/dev", PATH_MAX);
119         return  __lookup_dev(buf, dev, 0, 4);
120 }
121
122 /*
123  * Returns string pointing to device in /dev according to "major:minor" dev_id
124  */
125 char *crypt_lookup_dev(const char *dev_id)
126 {
127         int major, minor;
128         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
129         struct stat st;
130         ssize_t len;
131
132         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
133                 return NULL;
134
135         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
136                 return NULL;
137
138         len = readlink(path, link, sizeof(link));
139         if (len < 0) {
140                 /* Without /sys use old scan */
141                 if (stat("/sys/dev/block", &st) < 0)
142                         return lookup_dev_old(major, minor);
143                 return NULL;
144         }
145
146         link[len] = '\0';
147         devname = strrchr(link, '/');
148         if (!devname)
149                 return NULL;
150         devname++;
151
152         if (dm_is_dm_kernel_name(devname))
153                 devpath = dm_device_path("/dev/mapper/", major, minor);
154         else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
155                 devpath = strdup(path);
156
157         /*
158          * Check that path is correct.
159          */
160         if (devpath && ((stat(devpath, &st) < 0) ||
161             !S_ISBLK(st.st_mode) ||
162             (st.st_rdev != makedev(major, minor)))) {
163                 free(devpath);
164                 /* Should never happen unless user mangles with dev nodes. */
165                 return lookup_dev_old(major, minor);
166         }
167
168         return devpath;
169 }
170
171 static int crypt_sysfs_get_major_minor(const char *kname, int *major, int *minor)
172 {
173         char path[PATH_MAX], tmp[64] = {0};
174         int fd, r = 0;
175
176         if (snprintf(path, sizeof(path), "/sys/block/%s/dev", kname) < 0)
177                 return 0;
178
179         if ((fd = open(path, O_RDONLY)) < 0)
180                 return 0;
181         r = read(fd, tmp, sizeof(tmp));
182         close(fd);
183
184         if (r <= 0)
185                 return 0;
186
187         tmp[63] = '\0';
188         if (sscanf(tmp, "%d:%d", major, minor) != 2)
189                 return 0;
190
191         return 1;
192 }
193
194 static int crypt_sysfs_get_holders_dir(const char *device, char *path, int size)
195 {
196         struct stat st;
197
198         if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
199                 return 0;
200
201         if (snprintf(path, size, "/sys/dev/block/%d:%d/holders",
202                      major(st.st_rdev), minor(st.st_rdev)) < 0)
203                 return 0;
204
205         return 1;
206 }
207
208 int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size)
209 {
210         DIR *dir;
211         struct dirent *d;
212         char path[PATH_MAX], *dmname;
213         int major, minor, r = 0;
214
215         if (!crypt_sysfs_get_holders_dir(device, path, sizeof(path)))
216                 return -EINVAL;
217
218         if (!(dir = opendir(path)))
219                 return -EINVAL;
220
221         while (!r && (d = readdir(dir))) {
222                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
223                         continue;
224
225                 if (!dm_is_dm_kernel_name(d->d_name)) {
226                         r = -EBUSY;
227                         break;
228                 }
229
230                 if (!crypt_sysfs_get_major_minor(d->d_name, &major, &minor)) {
231                         r = -EINVAL;
232                         break;
233                 }
234
235                 if (!(dmname = dm_device_path(NULL, major, minor))) {
236                         r = -EINVAL;
237                         break;
238                 }
239                 r = dm_check_segment(dmname, offset, size);
240                 free(dmname);
241         }
242         closedir(dir);
243
244         return r;
245 }
246
247 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
248 {
249         char path[PATH_MAX], tmp[64] = {0};
250         int fd, r;
251
252         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
253                      major, minor) < 0)
254                 return 0;
255
256         if ((fd = open(path, O_RDONLY)) < 0)
257                 return 0;
258         r = read(fd, tmp, sizeof(tmp));
259         close(fd);
260
261         if (r <= 0)
262                 return 0;
263
264         if (sscanf(tmp, "%d", rotational) != 1)
265                 return 0;
266
267         return 1;
268 }