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