chiark / gitweb /
udevd: use dev_t or netif ifindex as database key
[elogind.git] / libudev / libudev-device-private.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stddef.h>
16 #include <stdbool.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <sys/stat.h>
21
22 #include "libudev.h"
23 #include "libudev-private.h"
24
25 static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
26 {
27         const char *id;
28         struct udev *udev = udev_device_get_udev(dev);
29         char filename[UTIL_PATH_SIZE];
30
31         id = udev_device_get_id_filename(dev);
32         if (id == NULL)
33                 return;
34         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/", tag, "/", id, NULL);
35
36         if (add) {
37                 util_create_path(udev, filename);
38                 symlink(udev_device_get_devpath(dev), filename);
39         } else {
40                 unlink(filename);
41         }
42 }
43
44 int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
45 {
46         struct udev_list_entry *list_entry;
47         bool found;
48
49         if (add && dev_old != NULL) {
50                 /* delete possible left-over tags */
51                 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
52                         const char *tag_old = udev_list_entry_get_name(list_entry);
53                         struct udev_list_entry *list_entry_current;
54
55                         found = false;
56                         udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
57                                 const char *tag = udev_list_entry_get_name(list_entry_current);
58
59                                 if (strcmp(tag, tag_old) == 0) {
60                                         found = true;
61                                         break;
62                                 }
63                         }
64                         if (!found)
65                                 udev_device_tag(dev_old, tag_old, false);
66                 }
67         }
68
69         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
70                 udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
71
72         return 0;
73 }
74
75 int udev_device_update_db(struct udev_device *udev_device)
76 {
77         const char *id;
78         struct udev *udev = udev_device_get_udev(udev_device);
79         char filename[UTIL_PATH_SIZE];
80         char filename_tmp[UTIL_PATH_SIZE];
81         FILE *f;
82         char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
83         size_t devlen = strlen(udev_get_dev_path(udev))+1;
84         char *s;
85         size_t l;
86         struct udev_list_entry *list_entry;
87         int ret;
88
89         id = udev_device_get_id_filename(udev_device);
90         if (id == NULL)
91                 return -1;
92         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
93         util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
94
95         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
96                 if (udev_list_entry_get_flags(list_entry))
97                         goto file;
98         if (udev_device_get_tags_list_entry(udev_device) != NULL)
99                 goto file;
100         if (udev_device_get_devlink_priority(udev_device) != 0)
101                 goto file;
102         if (udev_device_get_event_timeout(udev_device) >= 0)
103                 goto file;
104         if (udev_device_get_watch_handle(udev_device) >= 0)
105                 goto file;
106         if (udev_device_get_devnode(udev_device) == NULL)
107                 goto out;
108
109         /*
110          * if we have only the node and symlinks to store, try not to waste
111          * tmpfs memory -- store values, if they fit, in a symlink target
112          */
113         s = target;
114         l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
115         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
116                 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
117                 if (l == 0) {
118                         info(udev, "size of links too large, create file\n");
119                         goto file;
120                 }
121         }
122         udev_selinux_setfscreatecon(udev, filename_tmp, S_IFLNK);
123         util_create_path(udev, filename_tmp);
124         ret = symlink(target, filename_tmp);
125         udev_selinux_resetfscreatecon(udev);
126         if (ret != 0)
127                 goto file;
128         ret = rename(filename_tmp, filename);
129         if (ret != 0)
130                 goto file;
131         info(udev, "created db link (%s)\n", target);
132         goto out;
133 file:
134         util_create_path(udev, filename_tmp);
135         f = fopen(filename_tmp, "w");
136         if (f == NULL) {
137                 err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
138                 return -1;
139         }
140
141         if (udev_device_get_devnode(udev_device) != NULL) {
142                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
143                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
144                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
145         }
146         if (udev_device_get_devlink_priority(udev_device) != 0)
147                 fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device));
148         if (udev_device_get_event_timeout(udev_device) >= 0)
149                 fprintf(f, "T:%i\n", udev_device_get_event_timeout(udev_device));
150         if (udev_device_get_watch_handle(udev_device) >= 0)
151                 fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device));
152         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
153                 if (!udev_list_entry_get_flags(list_entry))
154                         continue;
155                 fprintf(f, "E:%s=%s\n",
156                         udev_list_entry_get_name(list_entry),
157                         udev_list_entry_get_value(list_entry));
158         }
159         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
160                 fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
161         fclose(f);
162         rename(filename_tmp, filename);
163         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
164 out:
165         return 0;
166 }
167
168 int udev_device_delete_db(struct udev_device *udev_device)
169 {
170         const char *id;
171         struct udev *udev = udev_device_get_udev(udev_device);
172         char filename[UTIL_PATH_SIZE];
173
174         id = udev_device_get_id_filename(udev_device);
175         if (id == NULL)
176                 return -1;
177         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
178         unlink(filename);
179         return 0;
180 }