]>
Commit | Line | Data |
---|---|---|
88a6477e KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2008-2012 Kay Sievers <kay@vrfy.org> | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
8dfc8dbe | 19 | |
8e41d35d DS |
20 | #include <stdlib.h> |
21 | #include <stdio.h> | |
8ea84a8a | 22 | #include <string.h> |
c81b35c0 | 23 | #include <stddef.h> |
28460195 | 24 | #include <stdbool.h> |
845d4751 | 25 | #include <unistd.h> |
8e41d35d | 26 | #include <fcntl.h> |
34bb5d05 | 27 | #include <sys/stat.h> |
8e41d35d | 28 | |
44b49d37 KS |
29 | #include "libudev.h" |
30 | #include "libudev-private.h" | |
2b41e68a | 31 | |
c1dbe11d | 32 | static void udev_device_tag(struct udev_device *dev, const char *tag, bool add) |
28460195 | 33 | { |
912541b0 | 34 | const char *id; |
912541b0 KS |
35 | char filename[UTIL_PATH_SIZE]; |
36 | ||
37 | id = udev_device_get_id_filename(dev); | |
38 | if (id == NULL) | |
39 | return; | |
d5a89d7d | 40 | strscpyl(filename, sizeof(filename), "/run/udev/tags/", tag, "/", id, NULL); |
912541b0 KS |
41 | |
42 | if (add) { | |
43 | int fd; | |
44 | ||
3cbd5f6b | 45 | mkdir_parents(filename, 0755); |
912541b0 KS |
46 | fd = open(filename, O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444); |
47 | if (fd >= 0) | |
48 | close(fd); | |
49 | } else { | |
50 | unlink(filename); | |
51 | } | |
c1dbe11d | 52 | } |
28460195 | 53 | |
c1dbe11d KS |
54 | int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add) |
55 | { | |
912541b0 KS |
56 | struct udev_list_entry *list_entry; |
57 | bool found; | |
58 | ||
59 | if (add && dev_old != NULL) { | |
60 | /* delete possible left-over tags */ | |
61 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) { | |
62 | const char *tag_old = udev_list_entry_get_name(list_entry); | |
63 | struct udev_list_entry *list_entry_current; | |
64 | ||
65 | found = false; | |
66 | udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) { | |
67 | const char *tag = udev_list_entry_get_name(list_entry_current); | |
68 | ||
33502ffe | 69 | if (streq(tag, tag_old)) { |
912541b0 KS |
70 | found = true; |
71 | break; | |
72 | } | |
73 | } | |
74 | if (!found) | |
75 | udev_device_tag(dev_old, tag_old, false); | |
76 | } | |
77 | } | |
78 | ||
79 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev)) | |
80 | udev_device_tag(dev, udev_list_entry_get_name(list_entry), add); | |
81 | ||
82 | return 0; | |
28460195 KS |
83 | } |
84 | ||
24d10766 KS |
85 | static bool device_has_info(struct udev_device *udev_device) |
86 | { | |
912541b0 KS |
87 | struct udev_list_entry *list_entry; |
88 | ||
89 | if (udev_device_get_devlinks_list_entry(udev_device) != NULL) | |
90 | return true; | |
91 | if (udev_device_get_devlink_priority(udev_device) != 0) | |
92 | return true; | |
93 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) | |
94 | if (udev_list_entry_get_num(list_entry)) | |
95 | return true; | |
96 | if (udev_device_get_tags_list_entry(udev_device) != NULL) | |
97 | return true; | |
98 | if (udev_device_get_watch_handle(udev_device) >= 0) | |
99 | return true; | |
100 | return false; | |
24d10766 KS |
101 | } |
102 | ||
aa8734ff | 103 | int udev_device_update_db(struct udev_device *udev_device) |
ca999860 | 104 | { |
912541b0 KS |
105 | bool has_info; |
106 | const char *id; | |
912541b0 KS |
107 | char filename[UTIL_PATH_SIZE]; |
108 | char filename_tmp[UTIL_PATH_SIZE]; | |
109 | FILE *f; | |
029f4f21 | 110 | int r; |
912541b0 KS |
111 | |
112 | id = udev_device_get_id_filename(udev_device); | |
113 | if (id == NULL) | |
114 | return -1; | |
115 | ||
116 | has_info = device_has_info(udev_device); | |
d5a89d7d | 117 | strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL); |
912541b0 KS |
118 | |
119 | /* do not store anything for otherwise empty devices */ | |
120 | if (!has_info && | |
121 | major(udev_device_get_devnum(udev_device)) == 0 && | |
122 | udev_device_get_ifindex(udev_device) == 0) { | |
123 | unlink(filename); | |
124 | return 0; | |
125 | } | |
126 | ||
127 | /* write a database file */ | |
d5a89d7d | 128 | strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL); |
3cbd5f6b | 129 | mkdir_parents(filename_tmp, 0755); |
912541b0 KS |
130 | f = fopen(filename_tmp, "we"); |
131 | if (f == NULL) { | |
56f64d95 | 132 | log_debug_errno(errno, "unable to create temporary db file '%s': %m", filename_tmp); |
a974cacd | 133 | return -errno; |
912541b0 KS |
134 | } |
135 | ||
136 | /* | |
137 | * set 'sticky' bit to indicate that we should not clean the | |
138 | * database when we transition from initramfs to the real root | |
139 | */ | |
140 | if (udev_device_get_db_persist(udev_device)) | |
141 | fchmod(fileno(f), 01644); | |
142 | ||
143 | if (has_info) { | |
144 | struct udev_list_entry *list_entry; | |
145 | ||
146 | if (major(udev_device_get_devnum(udev_device)) > 0) { | |
912541b0 | 147 | udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) |
4cb72937 | 148 | fprintf(f, "S:%s\n", udev_list_entry_get_name(list_entry) + strlen("/dev/")); |
912541b0 KS |
149 | if (udev_device_get_devlink_priority(udev_device) != 0) |
150 | fprintf(f, "L:%i\n", udev_device_get_devlink_priority(udev_device)); | |
151 | if (udev_device_get_watch_handle(udev_device) >= 0) | |
152 | fprintf(f, "W:%i\n", udev_device_get_watch_handle(udev_device)); | |
153 | } | |
154 | ||
155 | if (udev_device_get_usec_initialized(udev_device) > 0) | |
de0671ee | 156 | fprintf(f, "I:"USEC_FMT"\n", udev_device_get_usec_initialized(udev_device)); |
912541b0 KS |
157 | |
158 | udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) { | |
159 | if (!udev_list_entry_get_num(list_entry)) | |
160 | continue; | |
161 | fprintf(f, "E:%s=%s\n", | |
162 | udev_list_entry_get_name(list_entry), | |
163 | udev_list_entry_get_value(list_entry)); | |
164 | } | |
165 | ||
166 | udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) | |
167 | fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry)); | |
168 | } | |
169 | ||
170 | fclose(f); | |
029f4f21 VP |
171 | r = rename(filename_tmp, filename); |
172 | if (r < 0) | |
173 | return -1; | |
ff49bc32 | 174 | log_debug("created %s file '%s' for '%s'", has_info ? "db" : "empty", |
912541b0 KS |
175 | filename, udev_device_get_devpath(udev_device)); |
176 | return 0; | |
ca999860 GKH |
177 | } |
178 | ||
aa8734ff | 179 | int udev_device_delete_db(struct udev_device *udev_device) |
8e41d35d | 180 | { |
912541b0 | 181 | const char *id; |
912541b0 KS |
182 | char filename[UTIL_PATH_SIZE]; |
183 | ||
184 | id = udev_device_get_id_filename(udev_device); | |
185 | if (id == NULL) | |
186 | return -1; | |
d5a89d7d | 187 | strscpyl(filename, sizeof(filename), "/run/udev/data/", id, NULL); |
912541b0 KS |
188 | unlink(filename); |
189 | return 0; | |
8e41d35d | 190 | } |