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