chiark / gitweb /
write_cd_rules: identity-based persistence
[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 int devpath_to_db_path(const char *devpath, char *filename, size_t len)
37 {
38         size_t start, end, i;
39
40         /* add location of db files */
41         strlcpy(filename, udev_root, len);
42         start = strlcat(filename, "/"DB_DIR, len);
43         end = strlcat(filename, devpath, len);
44         if (end > len)
45                 end = len;
46
47         /* replace '/' to transform path into a filename */
48         for (i = start+1; i < end; i++)
49                 if (filename[i] == '/')
50                         filename[i] = PATH_TO_NAME_CHAR;
51
52         return 0;
53 }
54
55 static int db_file_to_devpath(const char *filename, char *devpath, size_t len)
56 {
57         size_t end, i;
58
59         strlcpy(devpath, "/", len);
60         end = strlcat(devpath, filename, len);
61
62         /* replace PATH_TO_NAME_CHAR to transform name into devpath */
63         for (i = 1; i < end; i++)
64                 if (devpath[i] == PATH_TO_NAME_CHAR)
65                         devpath[i] = '/';
66
67         return 0;
68 }
69
70 int udev_db_add_device(struct udevice *udev)
71 {
72         char filename[PATH_SIZE];
73
74         if (udev->test_run)
75                 return 0;
76
77         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
78         create_path(filename);
79
80         /*
81          * create only a symlink with the name as the target
82          * if we don't have any interesting data to remember
83          */
84         if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
85             !udev->partitions && !udev->ignore_remove) {
86                 dbg("nothing interesting to store, create symlink");
87                 unlink(filename);
88                 if (symlink(udev->name, filename) != 0) {
89                         err("unable to create db link '%s': %s", filename, strerror(errno));
90                         return -1;
91                 }
92         } else {
93                 struct name_entry *name_loop;
94                 FILE *f;
95
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->dev->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         return 0;
116 }
117
118 int udev_db_get_device(struct udevice *udev, const char *devpath)
119 {
120         struct stat stats;
121         char filename[PATH_SIZE];
122         char line[PATH_SIZE];
123         unsigned int maj, min;
124         char *bufline;
125         char *buf;
126         size_t bufsize;
127         size_t cur;
128         size_t count;
129
130         strlcpy(udev->dev->devpath, devpath, sizeof(udev->dev->devpath));
131         devpath_to_db_path(devpath, filename, sizeof(filename));
132
133         if (lstat(filename, &stats) != 0) {
134                 info("no db file to read %s: %s", filename, strerror(errno));
135                 return -1;
136         }
137         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
138                 char target[NAME_SIZE];
139                 int target_len;
140
141                 info("found a symlink as db file");
142                 target_len = readlink(filename, target, sizeof(target));
143                 if (target_len > 0)
144                         target[target_len] = '\0';
145                 else {
146                         info("error reading db link %s: %s", filename, strerror(errno));
147                         return -1;
148                 }
149                 dbg("db link points to '%s'", target);
150                 strlcpy(udev->name, target, sizeof(udev->name));
151                 return 0;
152         }
153
154         if (file_map(filename, &buf, &bufsize) != 0) {
155                 info("error reading db file %s: %s", filename, strerror(errno));
156                 return -1;
157         }
158
159         cur = 0;
160         while (cur < bufsize) {
161                 count = buf_get_line(buf, bufsize, cur);
162                 bufline = &buf[cur];
163                 cur += count+1;
164
165                 switch(bufline[0]) {
166                 case 'N':
167                         if (count > sizeof(udev->name))
168                                 count = sizeof(udev->name);
169                         memcpy(udev->name, &bufline[2], count-2);
170                         udev->name[count-2] = '\0';
171                         break;
172                 case 'M':
173                         if (count > sizeof(line))
174                                 count = sizeof(line);
175                         memcpy(line, &bufline[2], count-2);
176                         line[count-2] = '\0';
177                         sscanf(line, "%u:%u", &maj, &min);
178                         udev->devt = makedev(maj, min);
179                         break;
180                 case 'S':
181                         if (count > sizeof(line))
182                                 count =  sizeof(line);
183                         memcpy(line, &bufline[2], count-2);
184                         line[count-2] = '\0';
185                         name_list_add(&udev->symlink_list, line, 0);
186                         break;
187                 case 'A':
188                         if (count > sizeof(line))
189                                 count =  sizeof(line);
190                         memcpy(line, &bufline[2], count-2);
191                         line[count-2] = '\0';
192                         udev->partitions = atoi(line);
193                         break;
194                 case 'R':
195                         if (count > sizeof(line))
196                                 count =  sizeof(line);
197                         memcpy(line, &bufline[2], count-2);
198                         line[count-2] = '\0';
199                         udev->ignore_remove = atoi(line);
200                         break;
201                 case 'E':
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->env_list, line, 0);
207                         break;
208                 }
209         }
210         file_unmap(buf, bufsize);
211
212         if (udev->name[0] == '\0')
213                 return -1;
214
215         return 0;
216 }
217
218 int udev_db_delete_device(struct udevice *udev)
219 {
220         char filename[PATH_SIZE];
221
222         devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
223         unlink(filename);
224
225         return 0;
226 }
227
228 int udev_db_lookup_name(const char *name, char *devpath, size_t len)
229 {
230         char dbpath[PATH_MAX];
231         DIR *dir;
232         int found = 0;
233
234         strlcpy(dbpath, udev_root, sizeof(dbpath));
235         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
236         dir = opendir(dbpath);
237         if (dir == NULL) {
238                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
239                 return -1;
240         }
241
242         while (!found) {
243                 struct dirent *ent;
244                 char filename[PATH_SIZE];
245                 char nodename[PATH_SIZE];
246                 struct stat stats;
247                 char *bufline;
248                 char *buf;
249                 size_t bufsize;
250                 size_t cur;
251                 size_t count;
252
253                 ent = readdir(dir);
254                 if (ent == NULL || ent->d_name[0] == '\0')
255                         break;
256                 if (ent->d_name[0] == '.')
257                         continue;
258
259                 snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
260                 filename[sizeof(filename)-1] = '\0';
261                 dbg("looking at '%s'", filename);
262
263                 if (lstat(filename, &stats) != 0) {
264                         info("unable to read %s: %s", filename, strerror(errno));
265                         continue;
266                 }
267                 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
268                         char target[NAME_SIZE];
269                         int target_len;
270
271                         info("found a symlink as db file");
272                         target_len = readlink(filename, target, sizeof(target));
273                         if (target_len > 0)
274                                 target[target_len] = '\0';
275                         else {
276                                 info("error reading db link %s: %s", filename, strerror(errno));
277                                 return -1;
278                         }
279                         dbg("db link points to '%s'", target);
280                         if (strcmp(name, target) == 0) {
281                                 db_file_to_devpath(ent->d_name, devpath, len);
282                                 found =1;
283                         }
284                         continue;
285                 }
286
287                 if (file_map(filename, &buf, &bufsize) != 0) {
288                         info("unable to read db file '%s': %s", filename, strerror(errno));
289                         continue;
290                 }
291
292                 cur = 0;
293                 while (cur < bufsize && !found) {
294                         count = buf_get_line(buf, bufsize, cur);
295                         bufline = &buf[cur];
296                         cur += count+1;
297
298                         switch(bufline[0]) {
299                         case 'N':
300                         case 'S':
301                                 if (count > sizeof(nodename))
302                                         count = sizeof(nodename);
303                                 memcpy(nodename, &bufline[2], count-2);
304                                 nodename[count-2] = '\0';
305                                 dbg("compare '%s' '%s'", nodename, name);
306                                 if (strcmp(nodename, name) == 0) {
307                                         db_file_to_devpath(ent->d_name, devpath, len);
308                                         found = 1;
309                                 }
310                                 break;
311                         default:
312                                 continue;
313                         }
314                 }
315                 file_unmap(buf, bufsize);
316         }
317
318         closedir(dir);
319         if (found)
320                 return 0;
321         else
322                 return -1;
323 }
324
325 int udev_db_get_all_entries(struct list_head *name_list)
326 {
327         char dbpath[PATH_MAX];
328         DIR *dir;
329
330         strlcpy(dbpath, udev_root, sizeof(dbpath));
331         strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
332         dir = opendir(dbpath);
333         if (dir == NULL) {
334                 info("no udev_db available '%s': %s", dbpath, strerror(errno));
335                 return -1;
336         }
337
338         while (1) {
339                 struct dirent *ent;
340                 char filename[PATH_SIZE] = "/";
341                 size_t end, i;
342
343                 ent = readdir(dir);
344                 if (ent == NULL || ent->d_name[0] == '\0')
345                         break;
346                 if (ent->d_name[0] == '.')
347                         continue;
348
349                 end = strlcat(filename, ent->d_name, sizeof(filename));
350                 for (i = 1; i < end; i++)
351                         if (filename[i] == PATH_TO_NAME_CHAR)
352                                 filename[i] = '/';
353                 name_list_add(name_list, filename, 1);
354                 dbg("added '%s'", filename);
355         }
356
357         closedir(dir);
358         return 0;
359 }