Add context to DM helpers.
[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  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /* These are DM helpers used only by this file */
34 int dm_is_dm_device(int major, int minor);
35 int dm_is_dm_kernel_name(const char *name);
36 char *dm_device_path(const char *prefix, int major, int minor);
37
38 char *crypt_lookup_dev(const char *dev_id);
39 int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
40
41 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
42 {
43         struct dirent *entry;
44         struct stat st;
45         char *ptr;
46         char *result = NULL;
47         DIR *dir;
48         int space;
49
50         /* Ignore strange nested directories */
51         if (dir_level > max_level)
52                 return NULL;
53
54         path[PATH_MAX - 1] = '\0';
55         ptr = path + strlen(path);
56         *ptr++ = '/';
57         *ptr = '\0';
58         space = PATH_MAX - (ptr - path);
59
60         dir = opendir(path);
61         if (!dir)
62                 return NULL;
63
64         while((entry = readdir(dir))) {
65                 if (entry->d_name[0] == '.' ||
66                     !strncmp(entry->d_name, "..", 2))
67                         continue;
68
69                 if (dir_level == 0 &&
70                     (!strcmp(entry->d_name, "shm") ||
71                      !strcmp(entry->d_name, "fd") ||
72                      !strcmp(entry->d_name, "char") ||
73                      !strcmp(entry->d_name, "pts")))
74                         continue;
75
76                 strncpy(ptr, entry->d_name, space);
77                 if (stat(path, &st) < 0)
78                         continue;
79
80                 if (S_ISDIR(st.st_mode)) {
81                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
82                         if (result)
83                                 break;
84                 } else if (S_ISBLK(st.st_mode)) {
85                         /* workaround: ignore dm-X devices, these are internal kernel names */
86                         if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
87                                 continue;
88                         if (st.st_rdev == dev) {
89                                 result = strdup(path);
90                                 break;
91                         }
92                 }
93         }
94
95         closedir(dir);
96         return result;
97 }
98
99 /*
100  * Non-udev systemd need to scan for device here.
101  */
102 static char *lookup_dev_old(int major, int minor)
103 {
104         dev_t dev;
105         char *result = NULL, buf[PATH_MAX + 1];
106
107         dev = makedev(major, minor);
108         strncpy(buf, "/dev", PATH_MAX);
109         buf[PATH_MAX] = '\0';
110
111         /* First try low level device */
112         if ((result = __lookup_dev(buf, dev, 0, 0)))
113                 return result;
114
115         /* If it is dm, try DM dir  */
116         if (dm_is_dm_device(major, minor)) {
117                 strncpy(buf, dm_get_dir(), PATH_MAX);
118                 if ((result = __lookup_dev(buf, dev, 0, 0)))
119                         return result;
120         }
121
122         strncpy(buf, "/dev", PATH_MAX);
123         return  __lookup_dev(buf, dev, 0, 4);
124 }
125
126 /*
127  * Returns string pointing to device in /dev according to "major:minor" dev_id
128  */
129 char *crypt_lookup_dev(const char *dev_id)
130 {
131         int major, minor;
132         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
133         struct stat st;
134         ssize_t len;
135
136         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
137                 return NULL;
138
139         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
140                 return NULL;
141
142         len = readlink(path, link, sizeof(link));
143         if (len < 0) {
144                 /* Without /sys use old scan */
145                 if (stat("/sys/dev/block", &st) < 0)
146                         return lookup_dev_old(major, minor);
147                 return NULL;
148         }
149
150         link[len] = '\0';
151         devname = strrchr(link, '/');
152         if (!devname)
153                 return NULL;
154         devname++;
155
156         if (dm_is_dm_kernel_name(devname))
157                 devpath = dm_device_path("/dev/mapper/", major, minor);
158         else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
159                 devpath = strdup(path);
160
161         /*
162          * Check that path is correct.
163          */
164         if (devpath && ((stat(devpath, &st) < 0) ||
165             !S_ISBLK(st.st_mode) ||
166             (st.st_rdev != makedev(major, minor)))) {
167                 free(devpath);
168                 /* Should never happen unless user mangles with dev nodes. */
169                 return lookup_dev_old(major, minor);
170         }
171
172         return devpath;
173 }
174
175 int crypt_sysfs_get_rotational(int major, int minor, int *rotational)
176 {
177         char path[PATH_MAX], tmp[64] = {0};
178         int fd, r;
179
180         if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/queue/rotational",
181                      major, minor) < 0)
182                 return 0;
183
184         if ((fd = open(path, O_RDONLY)) < 0)
185                 return 0;
186         r = read(fd, tmp, sizeof(tmp));
187         close(fd);
188
189         if (r <= 0)
190                 return 0;
191
192         if (sscanf(tmp, "%d", rotational) != 1)
193                 return 0;
194
195         return 1;
196 }