chiark / gitweb /
replace strerror() usage with threadsafe "%m" format string
[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
131         if (udevice->test_run)
132                 return 0;
133
134         devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
135         create_path(udevice->udev, filename);
136         unlink(filename);
137
138         /*
139          * don't waste tmpfs memory pages, if we don't have any data to store
140          * create fake db-file; store the node-name in a symlink target
141          */
142         if (list_empty(&udevice->symlink_list) && list_empty(&udevice->env_list) &&
143             !udevice->partitions && !udevice->ignore_remove) {
144                 int ret;
145                 dbg(udevice->udev, "nothing interesting to store, create symlink\n");
146                 udev_selinux_setfscreatecon(udevice->udev, filename, S_IFLNK);
147                 ret = symlink(udevice->name, filename);
148                 udev_selinux_resetfscreatecon(udevice->udev);
149                 if (ret != 0) {
150                         err(udevice->udev, "unable to create db link '%s': %m\n", filename);
151                         return -1;
152                 }
153         } else {
154                 FILE *f;
155                 struct name_entry *name_loop;
156
157                 f = fopen(filename, "w");
158                 if (f == NULL) {
159                         err(udevice->udev, "unable to create db file '%s': %m\n", filename);
160                         return -1;
161                 }
162                 dbg(udevice->udev, "storing data for device '%s' in '%s'\n", udevice->dev->devpath, filename);
163
164                 fprintf(f, "N:%s\n", udevice->name);
165                 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
166                         fprintf(f, "S:%s\n", name_loop->name);
167                         /* add symlink-name to index */
168                         name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1);
169                 }
170                 fprintf(f, "M:%u:%u\n", major(udevice->devt), minor(udevice->devt));
171                 if (udevice->link_priority != 0)
172                         fprintf(f, "L:%u\n", udevice->link_priority);
173                 if (udevice->event_timeout >= 0)
174                         fprintf(f, "T:%u\n", udevice->event_timeout);
175                 if (udevice->partitions != 0)
176                         fprintf(f, "A:%u\n", udevice->partitions);
177                 if (udevice->ignore_remove)
178                         fprintf(f, "R:%u\n", udevice->ignore_remove);
179                 list_for_each_entry(name_loop, &udevice->env_list, node)
180                         fprintf(f, "E:%s\n", name_loop->name);
181                 fclose(f);
182         }
183
184         /* add name to index */
185         name_index(udevice->udev, udevice->dev->devpath, udevice->name, 1);
186
187         return 0;
188 }
189
190 int udev_db_get_device(struct udevice *udevice, const char *devpath)
191 {
192         struct stat stats;
193         char filename[UTIL_PATH_SIZE];
194         char line[UTIL_PATH_SIZE];
195         unsigned int maj, min;
196         char *bufline;
197         char *buf;
198         size_t bufsize;
199         size_t cur;
200         size_t count;
201
202         sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
203         devpath_to_db_path(udevice->udev, devpath, filename, sizeof(filename));
204
205         if (lstat(filename, &stats) != 0) {
206                 info(udevice->udev, "no db file to read %s: %m\n", filename);
207                 return -1;
208         }
209         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
210                 char target[UTIL_NAME_SIZE];
211                 int target_len;
212
213                 info(udevice->udev, "found a symlink as db file\n");
214                 target_len = readlink(filename, target, sizeof(target));
215                 if (target_len > 0)
216                         target[target_len] = '\0';
217                 else {
218                         info(udevice->udev, "error reading db link %s: %m\n", filename);
219                         return -1;
220                 }
221                 dbg(udevice->udev, "db link points to '%s'\n", target);
222                 util_strlcpy(udevice->name, target, sizeof(udevice->name));
223                 return 0;
224         }
225
226         if (file_map(filename, &buf, &bufsize) != 0) {
227                 info(udevice->udev, "error reading db file %s: %m\n", filename);
228                 return -1;
229         }
230
231         cur = 0;
232         while (cur < bufsize) {
233                 count = buf_get_line(buf, bufsize, cur);
234                 bufline = &buf[cur];
235                 cur += count+1;
236
237                 if (count > sizeof(line))
238                         count = sizeof(line);
239                 memcpy(line, &bufline[2], count-2);
240                 line[count-2] = '\0';
241
242                 switch(bufline[0]) {
243                 case 'N':
244                         util_strlcpy(udevice->name, line, sizeof(udevice->name));
245                         break;
246                 case 'M':
247                         sscanf(line, "%u:%u", &maj, &min);
248                         udevice->devt = makedev(maj, min);
249                         break;
250                 case 'S':
251                         name_list_add(udevice->udev, &udevice->symlink_list, line, 0);
252                         break;
253                 case 'L':
254                         udevice->link_priority = atoi(line);
255                         break;
256                 case 'T':
257                         udevice->event_timeout = atoi(line);
258                         break;
259                 case 'A':
260                         udevice->partitions = atoi(line);
261                         break;
262                 case 'R':
263                         udevice->ignore_remove = atoi(line);
264                         break;
265                 case 'E':
266                         name_list_add(udevice->udev, &udevice->env_list, line, 0);
267                         break;
268                 }
269         }
270         file_unmap(buf, bufsize);
271
272         if (udevice->name[0] == '\0')
273                 return -1;
274
275         return 0;
276 }
277
278 int udev_db_delete_device(struct udevice *udevice)
279 {
280         char filename[UTIL_PATH_SIZE];
281         struct name_entry *name_loop;
282
283         if (udevice->test_run)
284                 return 0;
285
286         devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
287         unlink(filename);
288
289         name_index(udevice->udev, udevice->dev->devpath, udevice->name, 0);
290         list_for_each_entry(name_loop, &udevice->symlink_list, node)
291                 name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 0);
292
293         return 0;
294 }