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