chiark / gitweb /
40082abf03461a00580783c0055f7acf0f73df92
[elogind.git] / udev_db.c
1 /*
2  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33 #include "udev.h"
34
35
36 static size_t devpath_to_db_path(const char *devpath, char *filename, size_t len)
37 {
38         size_t start;
39
40         /* add location of db files */
41         strlcpy(filename, udev_root, len);
42         start = strlcat(filename, "/"DB_DIR"/", len);
43         strlcat(filename, devpath, len);
44         return path_encode(&filename[start], len - start);
45 }
46
47 /* reverse mapping from the device file name to the devpath */
48 static int name_index(const char *devpath, const char *name, int add)
49 {
50         char device[PATH_SIZE];
51         char filename[PATH_SIZE * 2];
52         size_t start;
53         int fd;
54
55         /* directory with device name */
56         strlcpy(filename, udev_root, sizeof(filename));
57         start = strlcat(filename, "/"DB_NAME_INDEX_DIR"/", sizeof(filename));
58         strlcat(filename, name, sizeof(filename));
59         path_encode(&filename[start], sizeof(filename) - start);
60         /* entry with the devpath */
61         strlcpy(device, devpath, sizeof(device));
62         path_encode(device, sizeof(device));
63         strlcat(filename, "/", sizeof(filename));
64         strlcat(filename, device, sizeof(filename));
65
66         if (add) {
67                 info("creating index: '%s'", filename);
68                 create_path(filename);
69                 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
70                 if (fd > 0)
71                         close(fd);
72         } else {
73                 info("removing index: '%s'", filename);
74                 unlink(filename);
75                 delete_path(filename);
76         }
77         return 0;
78 }
79
80 int udev_db_add_device(struct udevice *udev)
81 {
82         char filename[PATH_SIZE];
83
84         if (udev->test_run)
85                 return 0;
86
87         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
88         create_path(filename);
89         unlink(filename);
90
91         /*
92          * don't waste tmpfs memory pages, if we don't have any data to store
93          * create fake db-file; store the node-name in a symlink target
94          */
95         if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
96             !udev->partitions && !udev->ignore_remove) {
97                 dbg("nothing interesting to store, create symlink");
98                 if (symlink(udev->name, filename) != 0) {
99                         err("unable to create db link '%s': %s", filename, strerror(errno));
100                         return -1;
101                 }
102         } else {
103                 FILE *f;
104                 struct name_entry *name_loop;
105
106                 f = fopen(filename, "w");
107                 if (f == NULL) {
108                         err("unable to create db file '%s': %s", filename, strerror(errno));
109                         return -1;
110                 }
111                 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
112
113                 fprintf(f, "N:%s\n", udev->name);
114                 list_for_each_entry(name_loop, &udev->symlink_list, node) {
115                         fprintf(f, "S:%s\n", name_loop->name);
116                         /* add symlink-name to index */
117                         name_index(udev->dev->devpath, name_loop->name, 1);
118                 }
119                 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
120                 if (udev->link_priority)
121                         fprintf(f, "L:%u\n", udev->link_priority);
122                 if (udev->partitions)
123                         fprintf(f, "A:%u\n", udev->partitions);
124                 if (udev->ignore_remove)
125                         fprintf(f, "R:%u\n", udev->ignore_remove);
126                 list_for_each_entry(name_loop, &udev->env_list, node)
127                         fprintf(f, "E:%s\n", name_loop->name);
128                 fclose(f);
129         }
130
131         /* add name to index */
132         name_index(udev->dev->devpath, udev->name, 1);
133
134         return 0;
135 }
136
137 int udev_db_get_device(struct udevice *udev, const char *devpath)
138 {
139         struct stat stats;
140         char filename[PATH_SIZE];
141         char line[PATH_SIZE];
142         unsigned int maj, min;
143         char *bufline;
144         char *buf;
145         size_t bufsize;
146         size_t cur;
147         size_t count;
148
149         strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
150         devpath_to_db_path(devpath, filename, sizeof(filename));
151
152         if (lstat(filename, &stats) != 0) {
153                 info("no db file to read %s: %s", filename, strerror(errno));
154                 return -1;
155         }
156         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
157                 char target[NAME_SIZE];
158                 int target_len;
159
160                 info("found a symlink as db file");
161                 target_len = readlink(filename, target, sizeof(target));
162                 if (target_len > 0)
163                         target[target_len] = '\0';
164                 else {
165                         info("error reading db link %s: %s", filename, strerror(errno));
166                         return -1;
167                 }
168                 dbg("db link points to '%s'", target);
169                 strlcpy(udev->name, target, sizeof(udev->name));
170                 return 0;
171         }
172
173         if (file_map(filename, &buf, &bufsize) != 0) {
174                 info("error reading db file %s: %s", filename, strerror(errno));
175                 return -1;
176         }
177
178         cur = 0;
179         while (cur < bufsize) {
180                 count = buf_get_line(buf, bufsize, cur);
181                 bufline = &buf[cur];
182                 cur += count+1;
183
184                 switch(bufline[0]) {
185                 case 'N':
186                         if (count > sizeof(udev->name))
187                                 count = sizeof(udev->name);
188                         memcpy(udev->name, &bufline[2], count-2);
189                         udev->name[count-2] = '\0';
190                         break;
191                 case 'M':
192                         if (count > sizeof(line))
193                                 count = sizeof(line);
194                         memcpy(line, &bufline[2], count-2);
195                         line[count-2] = '\0';
196                         sscanf(line, "%u:%u", &maj, &min);
197                         udev->devt = makedev(maj, min);
198                         break;
199                 case 'S':
200                         if (count > sizeof(line))
201                                 count =  sizeof(line);
202                         memcpy(line, &bufline[2], count-2);
203                         line[count-2] = '\0';
204                         name_list_add(&udev->symlink_list, line, 0);
205                         break;
206                 case 'L':
207                         if (count > sizeof(line))
208                                 count =  sizeof(line);
209                         memcpy(line, &bufline[2], count-2);
210                         line[count-2] = '\0';
211                         udev->link_priority = atoi(line);
212                         break;
213                 case 'A':
214                         if (count > sizeof(line))
215                                 count =  sizeof(line);
216                         memcpy(line, &bufline[2], count-2);
217                         line[count-2] = '\0';
218                         udev->partitions = atoi(line);
219                         break;
220                 case 'R':
221                         if (count > sizeof(line))
222                                 count =  sizeof(line);
223                         memcpy(line, &bufline[2], count-2);
224                         line[count-2] = '\0';
225                         udev->ignore_remove = atoi(line);
226                         break;
227                 case 'E':
228                         if (count > sizeof(line))
229                                 count =  sizeof(line);
230                         memcpy(line, &bufline[2], count-2);
231                         line[count-2] = '\0';
232                         name_list_add(&udev->env_list, line, 0);
233                         break;
234                 }
235         }
236         file_unmap(buf, bufsize);
237
238         if (udev->name[0] == '\0')
239                 return -1;
240
241         return 0;
242 }
243
244 int udev_db_delete_device(struct udevice *udev)
245 {
246         char filename[PATH_SIZE];
247         struct name_entry *name_loop;
248
249         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
250         unlink(filename);
251
252         name_index(udev->dev->devpath, udev->name, 0);
253         list_for_each_entry(name_loop, &udev->symlink_list, node)
254                 name_index(udev->dev->devpath, name_loop->name, 0);
255
256         return 0;
257 }
258
259 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
260 {
261         char dirname[PATH_MAX];
262         size_t start;
263         DIR *dir;
264         int found = 0;
265
266         strlcpy(dirname, udev_root, sizeof(dirname));
267         start = strlcat(dirname, "/"DB_NAME_INDEX_DIR"/", sizeof(dirname));
268         strlcat(dirname, name, sizeof(dirname));
269         path_encode(&dirname[start], sizeof(dirname) - start);
270
271         dir = opendir(dirname);
272         if (dir == NULL) {
273                 info("no index directory '%s': %s", dirname, strerror(errno));
274                 return -1;
275         }
276
277         info("found index directory '%s'", dirname);
278         while (!found) {
279                 struct dirent *ent;
280                 char device[PATH_SIZE];
281                 struct udevice *udev;
282
283                 ent = readdir(dir);
284                 if (ent == NULL || ent->d_name[0] == '\0')
285                         break;
286                 if (ent->d_name[0] == '.')
287                         continue;
288
289                 strlcpy(device, ent->d_name, sizeof(device));
290                 path_decode(device);
291                 udev = udev_device_init();
292                 if (udev == NULL)
293                         break;
294                 if (udev_db_get_device(udev, device) == 0) {
295                         char filename[PATH_SIZE];
296                         struct stat statbuf;
297
298                         info("found db entry '%s'", device);
299                         strlcpy(filename, udev_root, sizeof(filename));
300                         strlcat(filename, "/", sizeof(filename));
301                         strlcat(filename, name, sizeof(filename));
302                         /* make sure device entry matches dev_t */
303                         if (stat(filename, &statbuf) == 0) {
304                                 if (statbuf.st_rdev == udev->devt) {
305                                         info("node '%s' matches dev_t", udev->name);
306                                         strlcpy(devpath, device, len);
307                                         found = 1;
308                                 }
309                         }
310                 }
311                 udev_device_cleanup(udev);
312         }
313
314         closedir(dir);
315         if (found)
316                 return 0;
317         else
318                 return -1;
319 }
320
321 int udev_db_get_all_entries(struct list_head *name_list)
322 {
323         char dbpath[PATH_MAX];
324         DIR *dir;
325
326         strlcpy(dbpath, udev_root, sizeof(dbpath));
327         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
328         dir = opendir(dbpath);
329         if (dir == NULL) {
330                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
331                 return -1;
332         }
333
334         while (1) {
335                 struct dirent *ent;
336                 char device[PATH_SIZE];
337
338                 ent = readdir(dir);
339                 if (ent == NULL || ent->d_name[0] == '\0')
340                         break;
341                 if (ent->d_name[0] == '.')
342                         continue;
343
344                 strlcpy(device, ent->d_name, sizeof(device));
345                 path_decode(device);
346                 name_list_add(name_list, device, 1);
347                 dbg("added '%s'", device);
348         }
349
350         closedir(dir);
351         return 0;
352 }