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