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