chiark / gitweb /
consistent key naming to match only the event device or include all parent devices
[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 "udev.h"
34
35
36 static int devpath_to_db_path(const char *devpath, char *filename, size_t len)
37 {
38         size_t start, end, i;
39
40         /* add location of db files */
41         strlcpy(filename, udev_root, len);
42         start = strlcat(filename, "/"DB_DIR, len);
43         end = strlcat(filename, devpath, len);
44         if (end > len)
45                 end = len;
46
47         /* replace '/' to transform path into a filename */
48         for (i = start+1; i < end; i++)
49                 if (filename[i] == '/')
50                         filename[i] = PATH_TO_NAME_CHAR;
51
52         return 0;
53 }
54
55 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
56 {
57         size_t end, i;
58
59         strlcpy(devpath, "/", len);
60         end = strlcat(devpath, filename, len);
61
62         /* replace PATH_TO_NAME_CHAR to transform name into devpath */
63         for (i = 1; i < end; i++)
64                 if (devpath[i] == PATH_TO_NAME_CHAR)
65                         devpath[i] = '/';
66
67         return 0;
68 }
69
70 int udev_db_add_device(struct udevice *udev)
71 {
72         char filename[PATH_SIZE];
73
74         if (udev->test_run)
75                 return 0;
76
77         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
78         create_path(filename);
79
80         /*
81          * create only a symlink with the name as the target
82          * if we don't have any interesting data to remember
83          */
84         if (strcmp(udev->name, udev->dev->kernel) == 0 &&
85             list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
86             !udev->partitions && !udev->ignore_remove) {
87                 dbg("nothing interesting to store, create symlink");
88                 unlink(filename);
89                 if (symlink(udev->name, filename) != 0) {
90                         err("unable to create db link '%s': %s", filename, strerror(errno));
91                         return -1;
92                 }
93         } else {
94                 struct name_entry *name_loop;
95                 FILE *f;
96
97                 f = fopen(filename, "w");
98                 if (f == NULL) {
99                         err("unable to create db file '%s': %s", filename, strerror(errno));
100                         return -1;
101                 }
102                 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
103
104                 fprintf(f, "N:%s\n", udev->name);
105                 list_for_each_entry(name_loop, &udev->symlink_list, node)
106                         fprintf(f, "S:%s\n", name_loop->name);
107                 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
108                 if (udev->partitions)
109                         fprintf(f, "A:%u\n", udev->partitions);
110                 if (udev->ignore_remove)
111                         fprintf(f, "R:%u\n", udev->ignore_remove);
112                 list_for_each_entry(name_loop, &udev->env_list, node)
113                         fprintf(f, "E:%s\n", name_loop->name);
114                 fclose(f);
115         }
116         return 0;
117 }
118
119 int udev_db_get_device(struct udevice *udev, const char *devpath)
120 {
121         struct stat stats;
122         char filename[PATH_SIZE];
123         char line[PATH_SIZE];
124         unsigned int major, minor;
125         char *bufline;
126         char *buf;
127         size_t bufsize;
128         size_t cur;
129         size_t count;
130
131         strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
132         devpath_to_db_path(devpath, filename, sizeof(filename));
133
134         if (lstat(filename, &stats) != 0) {
135                 info("no db file to read %s: %s", filename, strerror(errno));
136                 return -1;
137         }
138         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
139                 char target[NAME_SIZE];
140                 int target_len;
141
142                 info("found a symlink as db file");
143                 target_len = readlink(filename, target, sizeof(target));
144                 if (target_len > 0)
145                         target[target_len] = '\0';
146                 else {
147                         info("error reading db link %s: %s", filename, strerror(errno));
148                         return -1;
149                 }
150                 dbg("db link points to '%s'", target);
151                 strlcpy(udev->name, target, sizeof(udev->name));
152                 return 0;
153         }
154
155         if (file_map(filename, &buf, &bufsize) != 0) {
156                 info("error reading db file %s: %s", filename, strerror(errno));
157                 return -1;
158         }
159
160         cur = 0;
161         while (cur < bufsize) {
162                 count = buf_get_line(buf, bufsize, cur);
163                 bufline = &buf[cur];
164                 cur += count+1;
165
166                 switch(bufline[0]) {
167                 case 'N':
168                         if (count > sizeof(udev->name))
169                                 count = sizeof(udev->name);
170                         memcpy(udev->name, &bufline[2], count-2);
171                         udev->name[count-2] = '\0';
172                         break;
173                 case 'M':
174                         if (count > sizeof(line))
175                                 count = sizeof(line);
176                         memcpy(line, &bufline[2], count-2);
177                         line[count-2] = '\0';
178                         sscanf(line, "%u:%u", &major, &minor);
179                         udev->devt = makedev(major, minor);
180                         break;
181                 case 'S':
182                         if (count > sizeof(line))
183                                 count =  sizeof(line);
184                         memcpy(line, &bufline[2], count-2);
185                         line[count-2] = '\0';
186                         name_list_add(&udev->symlink_list, line, 0);
187                         break;
188                 case 'A':
189                         if (count > sizeof(line))
190                                 count =  sizeof(line);
191                         memcpy(line, &bufline[2], count-2);
192                         line[count-2] = '\0';
193                         udev->partitions = atoi(line);
194                         break;
195                 case 'R':
196                         if (count > sizeof(line))
197                                 count =  sizeof(line);
198                         memcpy(line, &bufline[2], count-2);
199                         line[count-2] = '\0';
200                         udev->ignore_remove = atoi(line);
201                         break;
202                 case 'E':
203                         if (count > sizeof(line))
204                                 count =  sizeof(line);
205                         memcpy(line, &bufline[2], count-2);
206                         line[count-2] = '\0';
207                         name_list_add(&udev->env_list, line, 0);
208                         break;
209                 }
210         }
211         file_unmap(buf, bufsize);
212
213         if (udev->name[0] == '\0')
214                 return -1;
215
216         return 0;
217 }
218
219 int udev_db_delete_device(struct udevice *udev)
220 {
221         char filename[PATH_SIZE];
222
223         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
224         unlink(filename);
225
226         return 0;
227 }
228
229 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
230 {
231         char dbpath[PATH_MAX];
232         DIR *dir;
233         int found = 0;
234
235         strlcpy(dbpath, udev_root, sizeof(dbpath));
236         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
237         dir = opendir(dbpath);
238         if (dir == NULL) {
239                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
240                 return -1;
241         }
242
243         while (!found) {
244                 struct dirent *ent;
245                 char filename[PATH_SIZE];
246                 char nodename[PATH_SIZE];
247                 struct stat stats;
248                 char *bufline;
249                 char *buf;
250                 size_t bufsize;
251                 size_t cur;
252                 size_t count;
253
254                 ent = readdir(dir);
255                 if (ent == NULL || ent->d_name[0] == '\0')
256                         break;
257                 if (ent->d_name[0] == '.')
258                         continue;
259
260                 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
261                 filename[sizeof(filename)-1] = '\0';
262                 dbg("looking at '%s'", filename);
263
264                 if (lstat(filename, &stats) != 0) {
265                         info("unable to read %s: %s", filename, strerror(errno));
266                         continue;
267                 }
268                 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
269                         char target[NAME_SIZE];
270                         int target_len;
271
272                         info("found a symlink as db file");
273                         target_len = readlink(filename, target, sizeof(target));
274                         if (target_len > 0)
275                                 target[target_len] = '\0';
276                         else {
277                                 info("error reading db link %s: %s", filename, strerror(errno));
278                                 return -1;
279                         }
280                         dbg("db link points to '%s'", target);
281                         if (strcmp(name, target) == 0) {
282                                 db_file_to_devpath(ent->d_name, devpath, len);
283                                 found =1;
284                         }
285                         continue;
286                 }
287
288                 if (file_map(filename, &buf, &bufsize) != 0) {
289                         info("unable to read db file '%s': %s", filename, strerror(errno));
290                         continue;
291                 }
292
293                 cur = 0;
294                 while (cur < bufsize && !found) {
295                         count = buf_get_line(buf, bufsize, cur);
296                         bufline = &buf[cur];
297                         cur += count+1;
298
299                         switch(bufline[0]) {
300                         case 'N':
301                         case 'S':
302                                 if (count > sizeof(nodename))
303                                         count = sizeof(nodename);
304                                 memcpy(nodename, &bufline[2], count-2);
305                                 nodename[count-2] = '\0';
306                                 dbg("compare '%s' '%s'", nodename, name);
307                                 if (strcmp(nodename, name) == 0) {
308                                         db_file_to_devpath(ent->d_name, devpath, len);
309                                         found = 1;
310                                 }
311                                 break;
312                         default:
313                                 continue;
314                         }
315                 }
316                 file_unmap(buf, bufsize);
317         }
318
319         closedir(dir);
320         if (found)
321                 return 0;
322         else
323                 return -1;
324 }
325
326 int udev_db_get_all_entries(struct list_head *name_list)
327 {
328         char dbpath[PATH_MAX];
329         DIR *dir;
330
331         strlcpy(dbpath, udev_root, sizeof(dbpath));
332         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
333         dir = opendir(dbpath);
334         if (dir == NULL) {
335                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
336                 return -1;
337         }
338
339         while (1) {
340                 struct dirent *ent;
341                 char filename[PATH_SIZE] = "/";
342                 size_t end, i;
343
344                 ent = readdir(dir);
345                 if (ent == NULL || ent->d_name[0] == '\0')
346                         break;
347                 if (ent->d_name[0] == '.')
348                         continue;
349
350                 end = strlcat(filename, ent->d_name, sizeof(filename));
351                 for (i = 1; i < end; i++)
352                         if (filename[i] == PATH_TO_NAME_CHAR)
353                                 filename[i] = '/';
354                 name_list_add(name_list, filename, 1);
355                 dbg("added '%s'", filename);
356         }
357
358         closedir(dir);
359         return 0;
360 }