876b6caf6d76a86edd1ad62ca8a773963f00b3dd
[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-2012, Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2012, Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include "utils_dm.h"
33
34 /* These are DM helpers used only by this file */
35 int dm_is_dm_device(int major, int minor);
36 int dm_is_dm_kernel_name(const char *name);
37 char *dm_device_path(const char *prefix, int major, int minor);
38
39 char *crypt_lookup_dev(const char *dev_id);
40 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
41
42 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
43 {
44         struct dirent *entry;
45         struct stat st;
46         char *ptr;
47         char *result = NULL;
48         DIR *dir;
49         int space;
50
51         /* Ignore strange nested directories */
52         if (dir_level > max_level)
53                 return NULL;
54
55         path[PATH_MAX - 1] = '\0';
56         ptr = path + strlen(path);
57         *ptr++ = '/';
58         *ptr = '\0';
59         space = PATH_MAX - (ptr - path);
60
61         dir = opendir(path);
62         if (!dir)
63                 return NULL;
64
65         while((entry = readdir(dir))) {
66                 if (entry->d_name[0] == '.' ||
67                     !strncmp(entry->d_name, "..", 2))
68                         continue;
69
70                 if (dir_level == 0 &&
71                     (!strcmp(entry->d_name, "shm") ||
72                      !strcmp(entry->d_name, "fd") ||
73                      !strcmp(entry->d_name, "char") ||
74                      !strcmp(entry->d_name, "pts")))
75                         continue;
76
77                 strncpy(ptr, entry->d_name, space);
78                 if (stat(path, &st) < 0)
79                         continue;
80
81                 if (S_ISDIR(st.st_mode)) {
82                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
83                         if (result)
84                                 break;
85                 } else if (S_ISBLK(st.st_mode)) {
86                         /* workaround: ignore dm-X devices, these are internal kernel names */
87                         if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
88                                 continue;
89                         if (st.st_rdev == dev) {
90                                 result = strdup(path);
91                                 break;
92                         }
93                 }
94         }
95
96         closedir(dir);
97         return result;
98 }
99
100 /*
101  * Non-udev systemd need to scan for device here.
102  */
103 static char *lookup_dev_old(int major, int minor)
104 {
105         dev_t dev;
106         char *result = NULL, buf[PATH_MAX + 1];
107
108         dev = makedev(major, minor);
109         strncpy(buf, "/dev", PATH_MAX);
110         buf[PATH_MAX] = '\0';
111
112         /* First try low level device */
113         if ((result = __lookup_dev(buf, dev, 0, 0)))
114                 return result;
115
116         /* If it is dm, try DM dir  */
117         if (dm_is_dm_device(major, minor)) {
118                 strncpy(buf, dm_get_dir(), PATH_MAX);
119                 if ((result = __lookup_dev(buf, dev, 0, 0)))
120                         return result;
121         }
122
123         strncpy(buf, "/dev", PATH_MAX);
124         return  __lookup_dev(buf, dev, 0, 4);
125 }
126
127 /*
128  * Returns string pointing to device in /dev according to "major:minor" dev_id
129  */
130 char *crypt_lookup_dev(const char *dev_id)
131 {
132         int major, minor;
133         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
134         struct stat st;
135         ssize_t len;
136
137         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
138                 return NULL;
139
140         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
141                 return NULL;
142
143         len = readlink(path, link, sizeof(link) - 1);
144         if (len < 0) {
145                 /* Without /sys use old scan */
146                 if (stat("/sys/dev/block", &st) < 0)
147                         return lookup_dev_old(major, minor);
148                 return NULL;
149         }
150
151         link[len] = '\0';
152         devname = strrchr(link, '/');
153         if (!devname)
154                 return NULL;
155         devname++;
156
157         if (dm_is_dm_kernel_name(devname))
158                 devpath = dm_device_path("/dev/mapper/", major, minor);
159         else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
160                 devpath = strdup(path);
161
162         /*
163          * Check that path is correct.
164          */
165         if (devpath && ((stat(devpath, &st) < 0) ||
166             !S_ISBLK(st.st_mode) ||
167             (st.st_rdev != makedev(major, minor)))) {
168                 free(devpath);
169                 /* Should never happen unless user mangles with dev nodes. */
170                 return lookup_dev_old(major, minor);
171         }
172
173         return devpath;
174 }
175
176 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
177 {
178         char path[PATH_MAX], tmp[64] = {0};
179         int fd, r;
180
181         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
182                      major, minor) < 0)
183                 return 0;
184
185         if ((fd = open(path, O_RDONLY)) < 0)
186                 return 0;
187         r = read(fd, tmp, sizeof(tmp));
188         close(fd);
189
190         if (r <= 0)
191                 return 0;
192
193         if (sscanf(tmp, "%d", rotational) != 1)
194                 return 0;
195
196         return 1;
197 }