chiark / gitweb /
use udev_root instead of "/dev"in selinux matchpathcon_init_prefix()
[elogind.git] / udev_db.c
1 /*
2  * udev_db.c
3  *
4  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stddef.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <dirent.h>
32
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
35 #include "udev.h"
36 #include "udev_utils.h"
37 #include "logging.h"
38
39
40 static int devpath_to_db_path(const char *devpath, char *filename, size_t len)
41 {
42         size_t start, end, i;
43
44         /* add location of db files */
45         strlcpy(filename, udev_root, len);
46         start = strlcat(filename, "/"DB_DIR, len);
47         end = strlcat(filename, devpath, len);
48         if (end > len)
49                 end = len;
50
51         /* replace '/' to transform path into a filename */
52         for (i = start+1; i < end; i++)
53                 if (filename[i] == '/')
54                         filename[i] = PATH_TO_NAME_CHAR;
55
56         return 0;
57 }
58
59 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
60 {
61         size_t end, i;
62
63         strlcpy(devpath, "/", len);
64         end = strlcat(devpath, filename, len);
65
66         /* replace PATH_TO_NAME_CHAR to transform name into devpath */
67         for (i = 1; i < end; i++)
68                 if (devpath[i] == PATH_TO_NAME_CHAR)
69                         devpath[i] = '/';
70
71         return 0;
72 }
73
74 int udev_db_add_device(struct udevice *udev)
75 {
76         char filename[PATH_SIZE];
77         struct name_entry *name_loop;
78         FILE *f;
79
80         if (udev->test_run)
81                 return 0;
82
83         /* don't write anything if udev created only the node with the
84          * kernel name without any interesting data to remember
85          */
86         if (strcmp(udev->name, udev->kernel_name) == 0 &&
87             list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
88             !udev->partitions && !udev->ignore_remove) {
89                 dbg("nothing interesting to store in udevdb, skip");
90                 goto exit;
91         }
92
93         devpath_to_db_path(udev->devpath, filename, sizeof(filename));
94         create_path(filename);
95         f = fopen(filename, "w");
96         if (f == NULL) {
97                 err("unable to create db file '%s': %s", filename, strerror(errno));
98                 return -1;
99         }
100         dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
101
102         fprintf(f, "N:%s\n", udev->name);
103         list_for_each_entry(name_loop, &udev->symlink_list, node)
104                 fprintf(f, "S:%s\n", name_loop->name);
105         fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
106         if (udev->partitions)
107                 fprintf(f, "A:%u\n", udev->partitions);
108         if (udev->ignore_remove)
109                 fprintf(f, "R:%u\n", udev->ignore_remove);
110         list_for_each_entry(name_loop, &udev->env_list, node)
111                 fprintf(f, "E:%s\n", name_loop->name);
112         fclose(f);
113
114 exit:
115         return 0;
116 }
117
118 int udev_db_get_device(struct udevice *udev, const char *devpath)
119 {
120         char filename[PATH_SIZE];
121         char line[PATH_SIZE];
122         unsigned int major, minor;
123         char *bufline;
124         char *buf;
125         size_t bufsize;
126         size_t cur;
127         size_t count;
128
129         devpath_to_db_path(devpath, filename, sizeof(filename));
130         if (file_map(filename, &buf, &bufsize) != 0) {
131                 info("no db file to read %s: %s", filename, strerror(errno));
132                 return -1;
133         }
134
135         strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
136         cur = 0;
137         while (cur < bufsize) {
138                 count = buf_get_line(buf, bufsize, cur);
139                 bufline = &buf[cur];
140                 cur += count+1;
141
142                 switch(bufline[0]) {
143                 case 'N':
144                         if (count > sizeof(udev->name))
145                                 count = sizeof(udev->name);
146                         memcpy(udev->name, &bufline[2], count-2);
147                         udev->name[count-2] = '\0';
148                         break;
149                 case 'M':
150                         if (count > sizeof(line))
151                                 count = sizeof(line);
152                         memcpy(line, &bufline[2], count-2);
153                         line[count-2] = '\0';
154                         sscanf(line, "%u:%u", &major, &minor);
155                         udev->devt = makedev(major, minor);
156                         break;
157                 case 'S':
158                         if (count > sizeof(line))
159                                 count =  sizeof(line);
160                         memcpy(line, &bufline[2], count-2);
161                         line[count-2] = '\0';
162                         name_list_add(&udev->symlink_list, line, 0);
163                         break;
164                 case 'A':
165                         if (count > sizeof(line))
166                                 count =  sizeof(line);
167                         memcpy(line, &bufline[2], count-2);
168                         line[count-2] = '\0';
169                         udev->partitions = atoi(line);
170                         break;
171                 case 'R':
172                         if (count > sizeof(line))
173                                 count =  sizeof(line);
174                         memcpy(line, &bufline[2], count-2);
175                         line[count-2] = '\0';
176                         udev->ignore_remove = atoi(line);
177                         break;
178                 case 'E':
179                         if (count > sizeof(line))
180                                 count =  sizeof(line);
181                         memcpy(line, &bufline[2], count-2);
182                         line[count-2] = '\0';
183                         name_list_add(&udev->env_list, line, 0);
184                         break;
185                 }
186         }
187         file_unmap(buf, bufsize);
188
189         if (udev->name[0] == '\0')
190                 return -1;
191
192         return 0;
193 }
194
195 int udev_db_delete_device(struct udevice *udev)
196 {
197         char filename[PATH_SIZE];
198
199         devpath_to_db_path(udev->devpath, filename, sizeof(filename));
200         unlink(filename);
201
202         return 0;
203 }
204
205 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
206 {
207         char dbpath[PATH_MAX];
208         DIR *dir;
209         int found = 0;
210
211         strlcpy(dbpath, udev_root, sizeof(dbpath));
212         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
213         dir = opendir(dbpath);
214         if (dir == NULL) {
215                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
216                 return -1;
217         }
218
219         while (!found) {
220                 struct dirent *ent;
221                 char filename[PATH_SIZE];
222                 char nodename[PATH_SIZE];
223                 char *bufline;
224                 char *buf;
225                 size_t bufsize;
226                 size_t cur;
227                 size_t count;
228
229                 ent = readdir(dir);
230                 if (ent == NULL || ent->d_name[0] == '\0')
231                         break;
232                 if (ent->d_name[0] == '.')
233                         continue;
234
235                 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
236                 filename[sizeof(filename)-1] = '\0';
237                 dbg("looking at '%s'", filename);
238
239                 if (file_map(filename, &buf, &bufsize) != 0) {
240                         err("unable to read db file '%s': %s", filename, strerror(errno));
241                         continue;
242                 }
243
244                 cur = 0;
245                 while (cur < bufsize && !found) {
246                         count = buf_get_line(buf, bufsize, cur);
247                         bufline = &buf[cur];
248                         cur += count+1;
249
250                         switch(bufline[0]) {
251                         case 'N':
252                         case 'S':
253                                 if (count > sizeof(nodename))
254                                         count = sizeof(nodename);
255                                 memcpy(nodename, &bufline[2], count-2);
256                                 nodename[count-2] = '\0';
257                                 dbg("compare '%s' '%s'", nodename, name);
258                                 if (strcmp(nodename, name) == 0) {
259                                         db_file_to_devpath(ent->d_name, devpath, len);
260                                         found = 1;
261                                 }
262                                 break;
263                         default:
264                                 continue;
265                         }
266                 }
267                 file_unmap(buf, bufsize);
268         }
269
270         closedir(dir);
271         if (found)
272                 return 0;
273         else
274                 return -1;
275 }
276
277 int udev_db_get_all_entries(struct list_head *name_list)
278 {
279         char dbpath[PATH_MAX];
280         DIR *dir;
281
282         strlcpy(dbpath, udev_root, sizeof(dbpath));
283         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
284         dir = opendir(dbpath);
285         if (dir == NULL) {
286                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
287                 return -1;
288         }
289
290         while (1) {
291                 struct dirent *ent;
292                 char filename[PATH_SIZE] = "/";
293                 size_t end, i;
294
295                 ent = readdir(dir);
296                 if (ent == NULL || ent->d_name[0] == '\0')
297                         break;
298                 if (ent->d_name[0] == '.')
299                         continue;
300
301                 end = strlcat(filename, ent->d_name, sizeof(filename));
302                 for (i = 1; i < end; i++)
303                         if (filename[i] == PATH_TO_NAME_CHAR)
304                                 filename[i] = '/';
305                 name_list_add(name_list, filename, 1);
306                 dbg("added '%s'", filename);
307         }
308
309         closedir(dir);
310         return 0;
311 }