Excluce some known dir from old scan.
[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                 if (dir_level == 0 &&
60                     (!strcmp(entry->d_name, "shm") ||
61                      !strcmp(entry->d_name, "fd") ||
62                      !strcmp(entry->d_name, "char") ||
63                      !strcmp(entry->d_name, "pts")))
64                         continue;
65
66                 strncpy(ptr, entry->d_name, space);
67                 if (stat(path, &st) < 0)
68                         continue;
69
70                 if (S_ISDIR(st.st_mode)) {
71                         result = __lookup_dev(path, dev, dir_level + 1, max_level);
72                         if (result)
73                                 break;
74                 } else if (S_ISBLK(st.st_mode)) {
75                         /* workaround: ignore dm-X devices, these are internal kernel names */
76                         if (dir_level == 0 && dm_is_dm_kernel_name(entry->d_name))
77                                 continue;
78                         if (st.st_rdev == dev) {
79                                 result = strdup(path);
80                                 break;
81                         }
82                 }
83         }
84
85         closedir(dir);
86         return result;
87 }
88
89 /*
90  * Non-udev systemd need to scan for device here.
91  */
92 static char *lookup_dev_old(int major, int minor)
93 {
94         dev_t dev;
95         char *result = NULL, buf[PATH_MAX + 1];
96
97         dev = makedev(major, minor);
98         strncpy(buf, "/dev", PATH_MAX);
99         buf[PATH_MAX] = '\0';
100
101         /* First try low level device */
102         if ((result = __lookup_dev(buf, dev, 0, 0)))
103                 return result;
104
105         /* If it is dm, try DM dir  */
106         if (dm_is_dm_device(major, minor)) {
107                 strncpy(buf, dm_get_dir(), PATH_MAX);
108                 if ((result = __lookup_dev(buf, dev, 0, 0)))
109                         return result;
110         }
111
112         strncpy(buf, "/dev", PATH_MAX);
113         return  __lookup_dev(buf, dev, 0, 4);
114 }
115
116 /*
117  * Returns string pointing to device in /dev according to "major:minor" dev_id
118  */
119 char *crypt_lookup_dev(const char *dev_id)
120 {
121         int major, minor;
122         char link[PATH_MAX], path[PATH_MAX], *devname, *devpath = NULL;
123         struct stat st;
124         ssize_t len;
125
126         if (sscanf(dev_id, "%d:%d", &major, &minor) != 2)
127                 return NULL;
128
129         if (snprintf(path, sizeof(path), "/sys/dev/block/%s", dev_id) < 0)
130                 return NULL;
131
132         len = readlink(path, link, sizeof(link));
133         if (len < 0) {
134                 /* Without /sys use old scan */
135                 if (stat("/sys/dev/block", &st) < 0)
136                         return lookup_dev_old(major, minor);
137                 return NULL;
138         }
139
140         link[len] = '\0';
141         devname = strrchr(link, '/');
142         if (!devname)
143                 return NULL;
144         devname++;
145
146         if (dm_is_dm_kernel_name(devname))
147                 devpath = dm_device_path(major, minor);
148         else if (snprintf(path, sizeof(path), "/dev/%s", devname) > 0)
149                 devpath = strdup(path);
150
151         /*
152          * Check that path is correct.
153          */
154         if (devpath && ((stat(devpath, &st) < 0) ||
155             !S_ISBLK(st.st_mode) ||
156             (st.st_rdev != makedev(major, minor)))) {
157                 free(devpath);
158                 /* Should never happen unless user mangles with dev nodes. */
159                 return lookup_dev_old(major, minor);
160         }
161
162         return devpath;
163 }