chiark / gitweb /
change parameter order of udev_db_search_name()
[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 #include "udev_db.h"
39
40 #define PATH_TO_NAME_CHAR               '@'
41
42 static int get_db_filename(const char *devpath, char *filename, size_t len)
43 {
44         size_t start, end, i;
45
46         start = strlcpy(filename, udev_db_path, 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 int udev_db_add_device(struct udevice *udev)
60 {
61         char filename[PATH_SIZE];
62         struct name_entry *name_loop;
63         FILE *f;
64
65         if (udev->test_run)
66                 return 0;
67
68         /* don't write anything if udev created only the node with the
69          * kernel name without any interesting data to remember
70          */
71         if (strcmp(udev->name, udev->kernel_name) == 0 &&
72             list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
73             !udev->partitions && !udev->ignore_remove) {
74                 dbg("nothing interesting to store in udevdb, skip");
75                 goto exit;
76         }
77
78         get_db_filename(udev->devpath, filename, sizeof(filename));
79         create_path(filename);
80         f = fopen(filename, "w");
81         if (f == NULL) {
82                 err("unable to create db file '%s'", filename);
83                 return -1;
84         }
85         dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
86
87         fprintf(f, "P:%s\n", udev->devpath);
88         fprintf(f, "N:%s\n", udev->name);
89         list_for_each_entry(name_loop, &udev->symlink_list, node)
90                 fprintf(f, "S:%s\n", name_loop->name);
91         fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
92         if (udev->partitions)
93                 fprintf(f, "A:%u\n", udev->partitions);
94         if (udev->ignore_remove)
95                 fprintf(f, "R:%u\n", udev->ignore_remove);
96         list_for_each_entry(name_loop, &udev->env_list, node)
97                 fprintf(f, "E:%s\n", name_loop->name);
98         fclose(f);
99
100 exit:
101         return 0;
102 }
103
104 static int parse_db_file(struct udevice *udev, const char *filename)
105 {
106         char line[PATH_SIZE];
107         unsigned int major, minor;
108         char *bufline;
109         char *buf;
110         size_t bufsize;
111         size_t cur;
112         size_t count;
113
114         if (file_map(filename, &buf, &bufsize) != 0) {
115                 dbg("no db file to read '%s'", filename);
116                 return -1;
117         }
118
119         cur = 0;
120         while (cur < bufsize) {
121                 count = buf_get_line(buf, bufsize, cur);
122                 bufline = &buf[cur];
123                 cur += count+1;
124
125                 switch(bufline[0]) {
126                 case 'P':
127                         if (count > sizeof(udev->devpath))
128                                 count = sizeof(udev->devpath);
129                         memcpy(udev->devpath, &bufline[2], count-2);
130                         udev->devpath[count-2] = '\0';
131                         break;
132                 case 'N':
133                         if (count > sizeof(udev->name))
134                                 count = sizeof(udev->name);
135                         memcpy(udev->name, &bufline[2], count-2);
136                         udev->name[count-2] = '\0';
137                         break;
138                 case 'M':
139                         if (count > sizeof(line))
140                                 count = sizeof(line);
141                         memcpy(line, &bufline[2], count-2);
142                         line[count-2] = '\0';
143                         sscanf(line, "%u:%u", &major, &minor);
144                         udev->devt = makedev(major, minor);
145                         break;
146                 case 'S':
147                         if (count > sizeof(line))
148                                 count =  sizeof(line);
149                         memcpy(line, &bufline[2], count-2);
150                         line[count-2] = '\0';
151                         name_list_add(&udev->symlink_list, line, 0);
152                         break;
153                 case 'A':
154                         if (count > sizeof(line))
155                                 count =  sizeof(line);
156                         memcpy(line, &bufline[2], count-2);
157                         line[count-2] = '\0';
158                         udev->partitions = atoi(line);
159                         break;
160                 case 'R':
161                         if (count > sizeof(line))
162                                 count =  sizeof(line);
163                         memcpy(line, &bufline[2], count-2);
164                         line[count-2] = '\0';
165                         udev->ignore_remove = atoi(line);
166                         break;
167                 case 'E':
168                         if (count > sizeof(line))
169                                 count =  sizeof(line);
170                         memcpy(line, &bufline[2], count-2);
171                         line[count-2] = '\0';
172                         name_list_add(&udev->env_list, line, 0);
173                         break;
174                 }
175         }
176         file_unmap(buf, bufsize);
177
178         if (udev->name[0] == '\0')
179                 return -1;
180
181         return 0;
182 }
183
184 int udev_db_delete_device(struct udevice *udev)
185 {
186         char filename[PATH_SIZE];
187
188         get_db_filename(udev->devpath, filename, sizeof(filename));
189         unlink(filename);
190
191         return 0;
192 }
193
194 int udev_db_get_device(struct udevice *udev, const char *devpath)
195 {
196         char filename[PATH_SIZE];
197
198         get_db_filename(devpath, filename, sizeof(filename));
199         if (parse_db_file(udev, filename) != 0)
200                 return -1;
201
202         return 0;
203 }
204
205 int udev_db_search_name(const char *name, char *devpath, size_t len)
206 {
207         DIR *dir;
208         char path[PATH_SIZE];
209         int found = 0;
210
211         dir = opendir(udev_db_path);
212         if (dir == NULL) {
213                 err("unable to open udev_db '%s'", udev_db_path);
214                 return -1;
215         }
216
217         while (!found) {
218                 struct dirent *ent;
219                 char filename[PATH_SIZE];
220                 char nodename[PATH_SIZE];
221                 char *bufline;
222                 char *buf;
223                 size_t bufsize;
224                 size_t cur;
225                 size_t count;
226
227                 ent = readdir(dir);
228                 if (ent == NULL || ent->d_name[0] == '\0')
229                         break;
230
231                 if (ent->d_name[0] == '.')
232                         continue;
233
234                 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
235                 filename[sizeof(filename)-1] = '\0';
236                 dbg("looking at '%s'", filename);
237
238                 if (file_map(filename, &buf, &bufsize) != 0) {
239                         err("unable to read db file '%s'", filename);
240                         continue;
241                 }
242
243                 cur = 0;
244                 while (cur < bufsize && !found) {
245                         count = buf_get_line(buf, bufsize, cur);
246                         bufline = &buf[cur];
247                         cur += count+1;
248
249                         switch(bufline[0]) {
250                         case 'P':
251                                 if (count > sizeof(path))
252                                         count = sizeof(path);
253                                 memcpy(path, &bufline[2], count-2);
254                                 path[count-2] = '\0';
255                                 break;
256                         case 'N':
257                         case 'S':
258                                 if (count > sizeof(nodename))
259                                         count = sizeof(nodename);
260                                 memcpy(nodename, &bufline[2], count-2);
261                                 nodename[count-2] = '\0';
262                                 dbg("compare '%s' '%s'", nodename, name);
263                                 if (strcmp(nodename, name) == 0) {
264                                         found = 1;
265                                         break;
266                                 }
267                                 break;
268                         default:
269                                 continue;
270                         }
271                 }
272                 file_unmap(buf, bufsize);
273         }
274
275         closedir(dir);
276         if (found) {
277                 strlcpy(devpath, path, len);
278                 return 0;
279         } else
280                 return -1;
281 }
282
283 int udev_db_get_all_entries(struct list_head *name_list)
284 {
285         DIR *dir;
286
287         dir = opendir(udev_db_path);
288         if (dir == NULL) {
289                 err("unable to open udev_db '%s'", udev_db_path);
290                 return -1;
291         }
292
293         while (1) {
294                 struct dirent *ent;
295                 char filename[PATH_SIZE] = "/";
296                 size_t end, i;
297
298                 ent = readdir(dir);
299                 if (ent == NULL || ent->d_name[0] == '\0')
300                         break;
301
302                 if (ent->d_name[0] == '.')
303                         continue;
304
305                 end = strlcat(filename, ent->d_name, sizeof(filename));
306                 for (i = 1; i < end; i++)
307                         if (filename[i] == PATH_TO_NAME_CHAR)
308                                 filename[i] = '/';
309                 name_list_add(name_list, filename, 1);
310                 dbg("added '%s'", filename);
311         }
312
313         closedir(dir);
314         return 0;
315 }