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