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