Handle even non-standard and non-udev /dev in devpath.
[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 <sys/stat.h>
28 #include <sys/types.h>
29 #include "utils_dm.h"
30
31 static char *__lookup_dev(char *path, dev_t dev, int dir_level, const int max_level)
32 {
33         struct dirent *entry;
34         struct stat st;
35         char *ptr;
36         char *result = NULL;
37         DIR *dir;
38         int space;
39
40         /* Ignore strange nested directories */
41         if (dir_level > max_level)
42                 return NULL;
43
44         path[PATH_MAX - 1] = '\0';
45         ptr = path + strlen(path);
46         *ptr++ = '/';
47         *ptr = '\0';
48         space = PATH_MAX - (ptr - path);
49
50         dir = opendir(path);
51         if (!dir)
52                 return NULL;
53
54         while((entry = readdir(dir))) {
55                 if (entry->d_name[0] == '.' ||
56                     !strncmp(entry->d_name, "..", 2))
57                         continue;
58
59                 strncpy(ptr, entry->d_name, space);
60                 if (stat(path, &st) < 0)
61                         continue;
62
63                 if (S_ISDIR(st.st_mode)) {
64                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
65                         if (result)
66                                 break;
67                 } else if (S_ISBLK(st.st_mode)) {
68                         /* workaround: ignore dm-X devices, these are internal kernel names */
69                         if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
70                                 continue;
71                         if (st.st_rdev == dev) {
72                                 result = strdup(path);
73                                 break;
74                         }
75                 }
76         }
77
78         closedir(dir);
79         return result;
80 }
81
82 /*
83  * Non-udev systemd need to scan for device here.
84  */
85 static char *lookup_dev_old(int major, int minor)
86 {
87         dev_t dev;
88         char *result = NULL, buf[PATH_MAX + 1];
89
90         dev = makedev(major, minor);
91         strncpy(buf, "/dev", PATH_MAX);
92         buf[PATH_MAX] = '\0';
93
94         /* First try low level device */
95         if ((result = __lookup_dev(buf, dev, 0, 0)))
96                 return result;
97
98         /* If it is dm, try DM dir  */
99         if (dm_is_dm_device(major, minor)) {
100                 strncpy(buf, dm_get_dir(), PATH_MAX);
101                 if ((result = __lookup_dev(buf, dev, 0, 0)))
102                         return result;
103         }
104
105         strncpy(buf, "/dev", PATH_MAX);
106         return  __lookup_dev(buf, dev, 0, 4);
107 }
108
109 /*
110  * Returns string pointing to device in /dev according to "major:minor" dev_id
111  */
112 char *crypt_lookup_dev(const char *dev_id)
113 {
114         int major, minor;
115         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
116         struct stat st;
117         ssize_t len;
118
119         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
120                 return NULL;
121
122         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
123                 return NULL;
124
125         len = readlink(path, link, sizeof(link));
126         if (len < 0) {
127                 /* Without /sys use old scan */
128                 if (stat("/sys/dev/block", &st) < 0)
129                         return lookup_dev_old(major, minor);
130                 return NULL;
131         }
132
133         link[len] = '\0';
134         devname = strrchr(link, '/');
135         if (!devname)
136                 return NULL;
137         devname++;
138
139         if (dm_is_dm_kernel_name(devname))
140                 devpath = dm_device_path(major, minor);
141         else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
142                 devpath = strdup(path);
143
144         /*
145          * Check that path is correct.
146          */
147         if (devpath && ((stat(devpath, &st) < 0) ||
148             !S_ISBLK(st.st_mode) ||
149             (st.st_rdev != makedev(major, minor)))) {
150                 free(devpath);
151                 /* Should never happen unless user mangles with dev nodes. */
152                 return lookup_dev_old(major, minor);
153         }
154
155         return devpath;
156 }