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