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