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