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