chiark / gitweb /
libudev: GPL -> LGPL
[elogind.git] / udev / lib / libudev-device-db-write.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 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 <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <sys/stat.h>
20
21 #include "udev.h"
22
23 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
24 {
25         size_t start;
26
27         /* translate to location of db file */
28         util_strlcpy(filename, udev_get_dev_path(udev), len);
29         start = util_strlcat(filename, "/.udev/db/", len);
30         util_strlcat(filename, devpath, len);
31         return util_path_encode(&filename[start], len - start);
32 }
33
34 int udev_device_update_db(struct udev_device *udev_device)
35 {
36         struct udev *udev = udev_device_get_udev(udev_device);
37         char filename[UTIL_PATH_SIZE];
38         FILE *f;
39         char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
40         size_t devlen = strlen(udev_get_dev_path(udev))+1;
41         struct udev_list_entry *list_entry;
42         int ret;
43
44         devpath_to_db_path(udev,
45                            udev_device_get_devpath(udev_device),
46                            filename, sizeof(filename));
47         util_create_path(udev, filename);
48         unlink(filename);
49
50         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
51                 if (udev_list_entry_get_flag(list_entry))
52                         goto file;
53         if (udev_device_get_num_fake_partitions(udev_device) != 0)
54                 goto file;
55         if (udev_device_get_ignore_remove(udev_device))
56                 goto file;
57         if (udev_device_get_devlink_priority(udev_device) != 0)
58                 goto file;
59         if (udev_device_get_event_timeout(udev_device) >= 0)
60                 goto file;
61         if (udev_device_get_watch_handle(udev_device) >= 0)
62                 goto file;
63         if (udev_device_get_devnode(udev_device) == NULL)
64                 goto out;
65
66         /*
67          * if we have only the node and symlinks to store, try not to waste
68          * tmpfs memory -- store values, if they fit, in a symlink target
69          */
70         util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target));
71         udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
72                 size_t len;
73
74                 util_strlcat(target, " ", sizeof(target));
75                 len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target));
76                 if (len >= sizeof(target)) {
77                         info(udev, "size of links too large, create file\n");
78                         goto file;
79                 }
80         }
81         info(udev, "create db link (%s)\n", target);
82         udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
83         ret = symlink(target, filename);
84         udev_selinux_resetfscreatecon(udev);
85         if (ret == 0)
86                 goto out;
87 file:
88         f = fopen(filename, "w");
89         if (f == NULL) {
90                 err(udev, "unable to create db file '%s': %m\n", filename);
91                 return -1;
92                 }
93         info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
94
95         if (udev_device_get_devnode(udev_device) != NULL) {
96                 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
97                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
98                         fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
99         }
100         if (udev_device_get_devlink_priority(udev_device) != 0)
101                 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
102         if (udev_device_get_event_timeout(udev_device) >= 0)
103                 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
104         if (udev_device_get_num_fake_partitions(udev_device) != 0)
105                 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
106         if (udev_device_get_ignore_remove(udev_device))
107                 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
108         if (udev_device_get_watch_handle(udev_device) >= 0)
109                 fprintf(f, "W:%u\n", udev_device_get_watch_handle(udev_device));
110         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
111                 if (!udev_list_entry_get_flag(list_entry))
112                         continue;
113                 fprintf(f, "E:%s=%s\n",
114                         udev_list_entry_get_name(list_entry),
115                         udev_list_entry_get_value(list_entry));
116         }
117         fclose(f);
118 out:
119         return 0;
120 }
121
122 int udev_device_delete_db(struct udev_device *udev_device)
123 {
124         char filename[UTIL_PATH_SIZE];
125
126         devpath_to_db_path(udev_device_get_udev(udev_device),
127                            udev_device_get_devpath(udev_device),
128                            filename, sizeof(filename));
129         unlink(filename);
130         return 0;
131 }
132
133 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
134 {
135         char filename_old[UTIL_PATH_SIZE];
136         char filename[UTIL_PATH_SIZE];
137
138         devpath_to_db_path(udev_device_get_udev(udev_device),
139                            devpath_old,
140                            filename_old, sizeof(filename_old));
141         devpath_to_db_path(udev_device_get_udev(udev_device),
142                            udev_device_get_devpath(udev_device),
143                            filename, sizeof(filename));
144         return rename(filename_old, filename);
145 }