]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
57fa1d09 TG |
2 | |
3 | #include <ctype.h> | |
57fa1d09 | 4 | #include <net/if.h> |
07630cea | 5 | #include <sys/types.h> |
57fa1d09 TG |
6 | |
7 | #include "sd-device.h" | |
8 | ||
b5efdb8a | 9 | #include "alloc-util.h" |
57fa1d09 TG |
10 | #include "device-internal.h" |
11 | #include "device-private.h" | |
07630cea | 12 | #include "device-util.h" |
3ffd4af2 | 13 | #include "fd-util.h" |
07630cea | 14 | #include "fileio.h" |
f4f15635 | 15 | #include "fs-util.h" |
07630cea LP |
16 | #include "hashmap.h" |
17 | #include "macro.h" | |
18 | #include "mkdir.h" | |
d8b4d14d | 19 | #include "nulstr-util.h" |
6bedfcbb | 20 | #include "parse-util.h" |
07630cea | 21 | #include "path-util.h" |
07630cea | 22 | #include "set.h" |
8b43440b | 23 | #include "string-table.h" |
07630cea LP |
24 | #include "string-util.h" |
25 | #include "strv.h" | |
26 | #include "strxcpyx.h" | |
e4de7287 | 27 | #include "tmpfile-util.h" |
ee104e11 | 28 | #include "user-util.h" |
57fa1d09 TG |
29 | |
30 | int device_add_property(sd_device *device, const char *key, const char *value) { | |
31 | int r; | |
32 | ||
33 | assert(device); | |
34 | assert(key); | |
35 | ||
36 | r = device_add_property_aux(device, key, value, false); | |
37 | if (r < 0) | |
38 | return r; | |
39 | ||
40 | if (key[0] != '.') { | |
41 | r = device_add_property_aux(device, key, value, true); | |
42 | if (r < 0) | |
43 | return r; | |
44 | } | |
45 | ||
46 | return 0; | |
47 | } | |
48 | ||
57fa1d09 TG |
49 | void device_set_devlink_priority(sd_device *device, int priority) { |
50 | assert(device); | |
51 | ||
52 | device->devlink_priority = priority; | |
53 | } | |
54 | ||
55 | void device_set_is_initialized(sd_device *device) { | |
56 | assert(device); | |
57 | ||
58 | device->is_initialized = true; | |
59 | } | |
60 | ||
61 | int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) { | |
dc5042c0 | 62 | usec_t when; |
57fa1d09 TG |
63 | |
64 | assert(device); | |
65 | ||
66 | if (device_old && device_old->usec_initialized > 0) | |
dc5042c0 | 67 | when = device_old->usec_initialized; |
57fa1d09 | 68 | else |
dc5042c0 | 69 | when = now(CLOCK_MONOTONIC); |
57fa1d09 | 70 | |
dc5042c0 | 71 | return device_set_usec_initialized(device, when); |
57fa1d09 TG |
72 | } |
73 | ||
57fa1d09 TG |
74 | uint64_t device_get_properties_generation(sd_device *device) { |
75 | assert(device); | |
76 | ||
77 | return device->properties_generation; | |
78 | } | |
79 | ||
80 | uint64_t device_get_tags_generation(sd_device *device) { | |
81 | assert(device); | |
82 | ||
83 | return device->tags_generation; | |
84 | } | |
85 | ||
86 | uint64_t device_get_devlinks_generation(sd_device *device) { | |
87 | assert(device); | |
88 | ||
89 | return device->devlinks_generation; | |
90 | } | |
91 | ||
92 | int device_get_devnode_mode(sd_device *device, mode_t *mode) { | |
93 | int r; | |
94 | ||
95 | assert(device); | |
57fa1d09 TG |
96 | |
97 | r = device_read_db(device); | |
98 | if (r < 0) | |
99 | return r; | |
100 | ||
dcfbde3a YW |
101 | if (device->devmode == (mode_t) -1) |
102 | return -ENOENT; | |
103 | ||
78ffb476 YW |
104 | if (mode) |
105 | *mode = device->devmode; | |
57fa1d09 TG |
106 | |
107 | return 0; | |
108 | } | |
109 | ||
110 | int device_get_devnode_uid(sd_device *device, uid_t *uid) { | |
111 | int r; | |
112 | ||
113 | assert(device); | |
57fa1d09 TG |
114 | |
115 | r = device_read_db(device); | |
116 | if (r < 0) | |
117 | return r; | |
118 | ||
dcfbde3a YW |
119 | if (device->devuid == (uid_t) -1) |
120 | return -ENOENT; | |
121 | ||
78ffb476 YW |
122 | if (uid) |
123 | *uid = device->devuid; | |
57fa1d09 TG |
124 | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static int device_set_devuid(sd_device *device, const char *uid) { | |
129 | unsigned u; | |
130 | int r; | |
131 | ||
132 | assert(device); | |
133 | assert(uid); | |
134 | ||
135 | r = safe_atou(uid, &u); | |
136 | if (r < 0) | |
137 | return r; | |
138 | ||
139 | r = device_add_property_internal(device, "DEVUID", uid); | |
140 | if (r < 0) | |
141 | return r; | |
142 | ||
143 | device->devuid = u; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | int device_get_devnode_gid(sd_device *device, gid_t *gid) { | |
149 | int r; | |
150 | ||
151 | assert(device); | |
57fa1d09 TG |
152 | |
153 | r = device_read_db(device); | |
154 | if (r < 0) | |
155 | return r; | |
156 | ||
dcfbde3a YW |
157 | if (device->devgid == (gid_t) -1) |
158 | return -ENOENT; | |
159 | ||
78ffb476 YW |
160 | if (gid) |
161 | *gid = device->devgid; | |
57fa1d09 TG |
162 | |
163 | return 0; | |
164 | } | |
165 | ||
166 | static int device_set_devgid(sd_device *device, const char *gid) { | |
167 | unsigned g; | |
168 | int r; | |
169 | ||
170 | assert(device); | |
171 | assert(gid); | |
172 | ||
173 | r = safe_atou(gid, &g); | |
174 | if (r < 0) | |
175 | return r; | |
176 | ||
177 | r = device_add_property_internal(device, "DEVGID", gid); | |
178 | if (r < 0) | |
179 | return r; | |
180 | ||
181 | device->devgid = g; | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
5ebd3fc3 YW |
186 | int device_get_action(sd_device *device, DeviceAction *action) { |
187 | assert(device); | |
188 | ||
189 | if (device->action < 0) | |
190 | return -ENOENT; | |
191 | ||
192 | if (action) | |
193 | *action = device->action; | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | static int device_set_action(sd_device *device, const char *action) { | |
199 | DeviceAction a; | |
200 | int r; | |
201 | ||
202 | assert(device); | |
203 | assert(action); | |
204 | ||
205 | a = device_action_from_string(action); | |
206 | if (a < 0) | |
207 | return -EINVAL; | |
208 | ||
209 | r = device_add_property_internal(device, "ACTION", action); | |
210 | if (r < 0) | |
211 | return r; | |
212 | ||
213 | device->action = a; | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | int device_get_seqnum(sd_device *device, uint64_t *seqnum) { | |
219 | assert(device); | |
220 | ||
221 | if (device->seqnum == 0) | |
222 | return -ENOENT; | |
223 | ||
224 | if (seqnum) | |
225 | *seqnum = device->seqnum; | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | static int device_set_seqnum(sd_device *device, const char *str) { | |
231 | uint64_t seqnum; | |
232 | int r; | |
233 | ||
234 | assert(device); | |
235 | assert(str); | |
236 | ||
237 | r = safe_atou64(str, &seqnum); | |
238 | if (r < 0) | |
239 | return r; | |
240 | if (seqnum == 0) | |
241 | return -EINVAL; | |
242 | ||
243 | r = device_add_property_internal(device, "SEQNUM", str); | |
244 | if (r < 0) | |
245 | return r; | |
246 | ||
247 | device->seqnum = seqnum; | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
401cb614 | 252 | static int device_amend(sd_device *device, const char *key, const char *value) { |
57fa1d09 TG |
253 | int r; |
254 | ||
255 | assert(device); | |
256 | assert(key); | |
257 | assert(value); | |
258 | ||
259 | if (streq(key, "DEVPATH")) { | |
260 | char *path; | |
261 | ||
262 | path = strjoina("/sys", value); | |
263 | ||
264 | /* the caller must verify or trust this data (e.g., if it comes from the kernel) */ | |
265 | r = device_set_syspath(device, path, false); | |
266 | if (r < 0) | |
c7d54dae | 267 | return log_device_debug_errno(device, r, "sd-device: Failed to set syspath to '%s': %m", path); |
57fa1d09 TG |
268 | } else if (streq(key, "SUBSYSTEM")) { |
269 | r = device_set_subsystem(device, value); | |
270 | if (r < 0) | |
c7d54dae | 271 | return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem to '%s': %m", value); |
57fa1d09 TG |
272 | } else if (streq(key, "DEVTYPE")) { |
273 | r = device_set_devtype(device, value); | |
274 | if (r < 0) | |
c7d54dae | 275 | return log_device_debug_errno(device, r, "sd-device: Failed to set devtype to '%s': %m", value); |
57fa1d09 TG |
276 | } else if (streq(key, "DEVNAME")) { |
277 | r = device_set_devname(device, value); | |
278 | if (r < 0) | |
c7d54dae | 279 | return log_device_debug_errno(device, r, "sd-device: Failed to set devname to '%s': %m", value); |
57fa1d09 | 280 | } else if (streq(key, "USEC_INITIALIZED")) { |
dc5042c0 ZJS |
281 | usec_t t; |
282 | ||
283 | r = safe_atou64(value, &t); | |
284 | if (r < 0) | |
285 | return log_device_debug_errno(device, r, "sd-device: Failed to parse timestamp '%s': %m", value); | |
286 | ||
287 | r = device_set_usec_initialized(device, t); | |
57fa1d09 | 288 | if (r < 0) |
c7d54dae | 289 | return log_device_debug_errno(device, r, "sd-device: Failed to set usec-initialized to '%s': %m", value); |
57fa1d09 TG |
290 | } else if (streq(key, "DRIVER")) { |
291 | r = device_set_driver(device, value); | |
292 | if (r < 0) | |
c7d54dae | 293 | return log_device_debug_errno(device, r, "sd-device: Failed to set driver to '%s': %m", value); |
57fa1d09 TG |
294 | } else if (streq(key, "IFINDEX")) { |
295 | r = device_set_ifindex(device, value); | |
296 | if (r < 0) | |
c7d54dae | 297 | return log_device_debug_errno(device, r, "sd-device: Failed to set ifindex to '%s': %m", value); |
57fa1d09 TG |
298 | } else if (streq(key, "DEVMODE")) { |
299 | r = device_set_devmode(device, value); | |
300 | if (r < 0) | |
c7d54dae | 301 | return log_device_debug_errno(device, r, "sd-device: Failed to set devmode to '%s': %m", value); |
57fa1d09 TG |
302 | } else if (streq(key, "DEVUID")) { |
303 | r = device_set_devuid(device, value); | |
304 | if (r < 0) | |
c7d54dae | 305 | return log_device_debug_errno(device, r, "sd-device: Failed to set devuid to '%s': %m", value); |
57fa1d09 TG |
306 | } else if (streq(key, "DEVGID")) { |
307 | r = device_set_devgid(device, value); | |
308 | if (r < 0) | |
c7d54dae | 309 | return log_device_debug_errno(device, r, "sd-device: Failed to set devgid to '%s': %m", value); |
5ebd3fc3 YW |
310 | } else if (streq(key, "ACTION")) { |
311 | r = device_set_action(device, value); | |
312 | if (r < 0) | |
313 | return log_device_debug_errno(device, r, "sd-device: Failed to set action to '%s': %m", value); | |
314 | } else if (streq(key, "SEQNUM")) { | |
315 | r = device_set_seqnum(device, value); | |
316 | if (r < 0) | |
317 | return log_device_debug_errno(device, r, "sd-device: Failed to set SEQNUM to '%s': %m", value); | |
57fa1d09 | 318 | } else if (streq(key, "DEVLINKS")) { |
87a4d416 ZJS |
319 | for (const char *p = value;;) { |
320 | _cleanup_free_ char *word = NULL; | |
57fa1d09 | 321 | |
87a4d416 ZJS |
322 | r = extract_first_word(&p, &word, NULL, 0); |
323 | if (r < 0) | |
324 | return r; | |
325 | if (r == 0) | |
326 | break; | |
57fa1d09 | 327 | |
87a4d416 | 328 | r = device_add_devlink(device, word); |
57fa1d09 | 329 | if (r < 0) |
87a4d416 | 330 | return log_device_debug_errno(device, r, "sd-device: Failed to add devlink '%s': %m", word); |
57fa1d09 | 331 | } |
e77b146f | 332 | } else if (STR_IN_SET(key, "TAGS", "CURRENT_TAGS")) { |
87a4d416 ZJS |
333 | for (const char *p = value;;) { |
334 | _cleanup_free_ char *word = NULL; | |
57fa1d09 | 335 | |
87a4d416 ZJS |
336 | r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
337 | if (r < 0) | |
338 | return r; | |
339 | if (r == 0) | |
340 | break; | |
57fa1d09 | 341 | |
87a4d416 | 342 | r = device_add_tag(device, word, streq(key, "CURRENT_TAGS")); |
57fa1d09 | 343 | if (r < 0) |
87a4d416 | 344 | return log_device_debug_errno(device, r, "sd-device: Failed to add tag '%s': %m", word); |
57fa1d09 TG |
345 | } |
346 | } else { | |
347 | r = device_add_property_internal(device, key, value); | |
348 | if (r < 0) | |
c7d54dae | 349 | return log_device_debug_errno(device, r, "sd-device: Failed to add property '%s=%s': %m", key, value); |
57fa1d09 TG |
350 | } |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
5ebd3fc3 | 355 | static int device_append(sd_device *device, char *key, const char **_major, const char **_minor) { |
57fa1d09 TG |
356 | const char *major = NULL, *minor = NULL; |
357 | char *value; | |
358 | int r; | |
359 | ||
360 | assert(device); | |
361 | assert(key); | |
362 | assert(_major); | |
363 | assert(_minor); | |
57fa1d09 TG |
364 | |
365 | value = strchr(key, '='); | |
9e791238 ZJS |
366 | if (!value) |
367 | return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), | |
368 | "sd-device: Not a key-value pair: '%s'", key); | |
57fa1d09 TG |
369 | |
370 | *value = '\0'; | |
371 | ||
372 | value++; | |
373 | ||
374 | if (streq(key, "MAJOR")) | |
375 | major = value; | |
376 | else if (streq(key, "MINOR")) | |
377 | minor = value; | |
378 | else { | |
401cb614 | 379 | r = device_amend(device, key, value); |
57fa1d09 TG |
380 | if (r < 0) |
381 | return r; | |
382 | } | |
383 | ||
384 | if (major != 0) | |
385 | *_major = major; | |
386 | ||
387 | if (minor != 0) | |
388 | *_minor = minor; | |
389 | ||
57fa1d09 TG |
390 | return 0; |
391 | } | |
392 | ||
393 | void device_seal(sd_device *device) { | |
394 | assert(device); | |
395 | ||
396 | device->sealed = true; | |
397 | } | |
398 | ||
5ebd3fc3 | 399 | static int device_verify(sd_device *device) { |
57fa1d09 TG |
400 | assert(device); |
401 | ||
9e791238 ZJS |
402 | if (!device->devpath || !device->subsystem || device->action < 0 || device->seqnum == 0) |
403 | return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), | |
404 | "sd-device: Device created from strv or nulstr lacks devpath, subsystem, action or seqnum."); | |
57fa1d09 TG |
405 | |
406 | device->sealed = true; | |
407 | ||
408 | return 0; | |
409 | } | |
410 | ||
411 | int device_new_from_strv(sd_device **ret, char **strv) { | |
4afd3348 | 412 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
57fa1d09 TG |
413 | char **key; |
414 | const char *major = NULL, *minor = NULL; | |
57fa1d09 TG |
415 | int r; |
416 | ||
417 | assert(ret); | |
418 | assert(strv); | |
419 | ||
420 | r = device_new_aux(&device); | |
421 | if (r < 0) | |
422 | return r; | |
423 | ||
424 | STRV_FOREACH(key, strv) { | |
5ebd3fc3 | 425 | r = device_append(device, *key, &major, &minor); |
57fa1d09 TG |
426 | if (r < 0) |
427 | return r; | |
428 | } | |
429 | ||
430 | if (major) { | |
431 | r = device_set_devnum(device, major, minor); | |
432 | if (r < 0) | |
c7d54dae | 433 | return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor); |
57fa1d09 TG |
434 | } |
435 | ||
5ebd3fc3 | 436 | r = device_verify(device); |
57fa1d09 TG |
437 | if (r < 0) |
438 | return r; | |
439 | ||
1cc6c93a | 440 | *ret = TAKE_PTR(device); |
57fa1d09 TG |
441 | |
442 | return 0; | |
443 | } | |
444 | ||
445 | int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) { | |
4afd3348 | 446 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
57fa1d09 | 447 | const char *major = NULL, *minor = NULL; |
57fa1d09 TG |
448 | unsigned i = 0; |
449 | int r; | |
450 | ||
451 | assert(ret); | |
452 | assert(nulstr); | |
453 | assert(len); | |
454 | ||
455 | r = device_new_aux(&device); | |
456 | if (r < 0) | |
457 | return r; | |
458 | ||
459 | while (i < len) { | |
460 | char *key; | |
461 | const char *end; | |
462 | ||
463 | key = (char*)&nulstr[i]; | |
464 | end = memchr(key, '\0', len - i); | |
9e791238 ZJS |
465 | if (!end) |
466 | return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), | |
467 | "sd-device: Failed to parse nulstr"); | |
468 | ||
57fa1d09 TG |
469 | i += end - key + 1; |
470 | ||
5ebd3fc3 | 471 | r = device_append(device, key, &major, &minor); |
57fa1d09 TG |
472 | if (r < 0) |
473 | return r; | |
474 | } | |
475 | ||
476 | if (major) { | |
477 | r = device_set_devnum(device, major, minor); | |
478 | if (r < 0) | |
c7d54dae | 479 | return log_device_debug_errno(device, r, "sd-device: Failed to set devnum %s:%s: %m", major, minor); |
57fa1d09 TG |
480 | } |
481 | ||
5ebd3fc3 | 482 | r = device_verify(device); |
57fa1d09 TG |
483 | if (r < 0) |
484 | return r; | |
485 | ||
1cc6c93a | 486 | *ret = TAKE_PTR(device); |
57fa1d09 TG |
487 | |
488 | return 0; | |
489 | } | |
490 | ||
491 | static int device_update_properties_bufs(sd_device *device) { | |
492 | const char *val, *prop; | |
ccc1002a TG |
493 | _cleanup_free_ char **buf_strv = NULL; |
494 | _cleanup_free_ uint8_t *buf_nulstr = NULL; | |
d854ba50 | 495 | size_t allocated_nulstr = 0; |
ccc1002a | 496 | size_t nulstr_len = 0, num = 0, i = 0; |
57fa1d09 TG |
497 | |
498 | assert(device); | |
499 | ||
aa20f49a TG |
500 | if (!device->properties_buf_outdated) |
501 | return 0; | |
502 | ||
57fa1d09 TG |
503 | FOREACH_DEVICE_PROPERTY(device, prop, val) { |
504 | size_t len = 0; | |
505 | ||
506 | len = strlen(prop) + 1 + strlen(val); | |
507 | ||
508 | buf_nulstr = GREEDY_REALLOC0(buf_nulstr, allocated_nulstr, nulstr_len + len + 2); | |
509 | if (!buf_nulstr) | |
510 | return -ENOMEM; | |
511 | ||
57fa1d09 TG |
512 | strscpyl((char *)buf_nulstr + nulstr_len, len + 1, prop, "=", val, NULL); |
513 | nulstr_len += len + 1; | |
d854ba50 | 514 | ++num; |
57fa1d09 TG |
515 | } |
516 | ||
ccc1002a TG |
517 | /* build buf_strv from buf_nulstr */ |
518 | buf_strv = new0(char *, num + 1); | |
519 | if (!buf_strv) | |
520 | return -ENOMEM; | |
d854ba50 | 521 | |
d854ba50 | 522 | NULSTR_FOREACH(val, (char*) buf_nulstr) { |
ccc1002a | 523 | buf_strv[i] = (char *) val; |
d854ba50 MP |
524 | assert(i < num); |
525 | i++; | |
526 | } | |
57fa1d09 | 527 | |
f9ecfd3b | 528 | free_and_replace(device->properties_nulstr, buf_nulstr); |
ccc1002a | 529 | device->properties_nulstr_len = nulstr_len; |
f9ecfd3b | 530 | free_and_replace(device->properties_strv, buf_strv); |
ccc1002a | 531 | |
57fa1d09 TG |
532 | device->properties_buf_outdated = false; |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
537 | int device_get_properties_nulstr(sd_device *device, const uint8_t **nulstr, size_t *len) { | |
538 | int r; | |
539 | ||
540 | assert(device); | |
541 | assert(nulstr); | |
542 | assert(len); | |
543 | ||
aa20f49a TG |
544 | r = device_update_properties_bufs(device); |
545 | if (r < 0) | |
546 | return r; | |
57fa1d09 TG |
547 | |
548 | *nulstr = device->properties_nulstr; | |
549 | *len = device->properties_nulstr_len; | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | int device_get_properties_strv(sd_device *device, char ***strv) { | |
555 | int r; | |
556 | ||
557 | assert(device); | |
558 | assert(strv); | |
559 | ||
560 | r = device_update_properties_bufs(device); | |
561 | if (r < 0) | |
562 | return r; | |
563 | ||
564 | *strv = device->properties_strv; | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
569 | int device_get_devlink_priority(sd_device *device, int *priority) { | |
570 | int r; | |
571 | ||
572 | assert(device); | |
573 | assert(priority); | |
574 | ||
575 | r = device_read_db(device); | |
576 | if (r < 0) | |
577 | return r; | |
578 | ||
579 | *priority = device->devlink_priority; | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
584 | int device_get_watch_handle(sd_device *device, int *handle) { | |
585 | int r; | |
586 | ||
587 | assert(device); | |
57fa1d09 TG |
588 | |
589 | r = device_read_db(device); | |
590 | if (r < 0) | |
591 | return r; | |
592 | ||
dcfbde3a YW |
593 | if (device->watch_handle < 0) |
594 | return -ENOENT; | |
595 | ||
78ffb476 YW |
596 | if (handle) |
597 | *handle = device->watch_handle; | |
57fa1d09 TG |
598 | |
599 | return 0; | |
600 | } | |
601 | ||
602 | void device_set_watch_handle(sd_device *device, int handle) { | |
603 | assert(device); | |
604 | ||
605 | device->watch_handle = handle; | |
606 | } | |
607 | ||
608 | int device_rename(sd_device *device, const char *name) { | |
609 | _cleanup_free_ char *dirname = NULL; | |
270384b2 | 610 | const char *new_syspath, *interface; |
57fa1d09 TG |
611 | int r; |
612 | ||
613 | assert(device); | |
614 | assert(name); | |
615 | ||
616 | dirname = dirname_malloc(device->syspath); | |
617 | if (!dirname) | |
618 | return -ENOMEM; | |
619 | ||
270384b2 | 620 | new_syspath = prefix_roota(dirname, name); |
57fa1d09 TG |
621 | |
622 | /* the user must trust that the new name is correct */ | |
623 | r = device_set_syspath(device, new_syspath, false); | |
624 | if (r < 0) | |
625 | return r; | |
626 | ||
627 | r = sd_device_get_property_value(device, "INTERFACE", &interface); | |
628 | if (r >= 0) { | |
f7b1c8d1 EV |
629 | /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */ |
630 | r = device_add_property_internal(device, "INTERFACE_OLD", interface); | |
57fa1d09 TG |
631 | if (r < 0) |
632 | return r; | |
633 | ||
f7b1c8d1 | 634 | r = device_add_property_internal(device, "INTERFACE", name); |
57fa1d09 TG |
635 | if (r < 0) |
636 | return r; | |
637 | } else if (r != -ENOENT) | |
638 | return r; | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
643 | int device_shallow_clone(sd_device *old_device, sd_device **new_device) { | |
4afd3348 | 644 | _cleanup_(sd_device_unrefp) sd_device *ret = NULL; |
57fa1d09 TG |
645 | int r; |
646 | ||
647 | assert(old_device); | |
648 | assert(new_device); | |
649 | ||
650 | r = device_new_aux(&ret); | |
651 | if (r < 0) | |
652 | return r; | |
653 | ||
654 | r = device_set_syspath(ret, old_device->syspath, false); | |
655 | if (r < 0) | |
656 | return r; | |
657 | ||
658 | r = device_set_subsystem(ret, old_device->subsystem); | |
659 | if (r < 0) | |
660 | return r; | |
661 | ||
662 | ret->devnum = old_device->devnum; | |
663 | ||
1cc6c93a | 664 | *new_device = TAKE_PTR(ret); |
57fa1d09 TG |
665 | |
666 | return 0; | |
667 | } | |
668 | ||
669 | int device_clone_with_db(sd_device *old_device, sd_device **new_device) { | |
4afd3348 | 670 | _cleanup_(sd_device_unrefp) sd_device *ret = NULL; |
57fa1d09 TG |
671 | int r; |
672 | ||
673 | assert(old_device); | |
674 | assert(new_device); | |
675 | ||
676 | r = device_shallow_clone(old_device, &ret); | |
677 | if (r < 0) | |
678 | return r; | |
679 | ||
680 | r = device_read_db(ret); | |
681 | if (r < 0) | |
682 | return r; | |
683 | ||
684 | ret->sealed = true; | |
685 | ||
1cc6c93a | 686 | *new_device = TAKE_PTR(ret); |
57fa1d09 TG |
687 | |
688 | return 0; | |
689 | } | |
690 | ||
691 | int device_new_from_synthetic_event(sd_device **new_device, const char *syspath, const char *action) { | |
4afd3348 | 692 | _cleanup_(sd_device_unrefp) sd_device *ret = NULL; |
57fa1d09 TG |
693 | int r; |
694 | ||
695 | assert(new_device); | |
696 | assert(syspath); | |
697 | assert(action); | |
698 | ||
699 | r = sd_device_new_from_syspath(&ret, syspath); | |
700 | if (r < 0) | |
701 | return r; | |
702 | ||
703 | r = device_read_uevent_file(ret); | |
704 | if (r < 0) | |
705 | return r; | |
706 | ||
5ebd3fc3 | 707 | r = device_set_action(ret, action); |
57fa1d09 TG |
708 | if (r < 0) |
709 | return r; | |
710 | ||
1cc6c93a | 711 | *new_device = TAKE_PTR(ret); |
57fa1d09 TG |
712 | |
713 | return 0; | |
714 | } | |
715 | ||
ad5944d7 YW |
716 | int device_new_from_stat_rdev(sd_device **ret, const struct stat *st) { |
717 | char type; | |
718 | ||
719 | assert(ret); | |
720 | assert(st); | |
721 | ||
722 | if (S_ISBLK(st->st_mode)) | |
723 | type = 'b'; | |
724 | else if (S_ISCHR(st->st_mode)) | |
725 | type = 'c'; | |
726 | else | |
727 | return -ENOTTY; | |
728 | ||
729 | return sd_device_new_from_devnum(ret, type, st->st_rdev); | |
730 | } | |
731 | ||
57fa1d09 TG |
732 | int device_copy_properties(sd_device *device_dst, sd_device *device_src) { |
733 | const char *property, *value; | |
a3ce8136 | 734 | Iterator i; |
57fa1d09 TG |
735 | int r; |
736 | ||
737 | assert(device_dst); | |
738 | assert(device_src); | |
739 | ||
a3ce8136 YW |
740 | r = device_properties_prepare(device_src); |
741 | if (r < 0) | |
742 | return r; | |
743 | ||
5ce41697 | 744 | ORDERED_HASHMAP_FOREACH_KEY(value, property, device_src->properties_db, i) { |
a3ce8136 YW |
745 | r = device_add_property_aux(device_dst, property, value, true); |
746 | if (r < 0) | |
747 | return r; | |
748 | } | |
749 | ||
5ce41697 | 750 | ORDERED_HASHMAP_FOREACH_KEY(value, property, device_src->properties, i) { |
a3ce8136 | 751 | r = device_add_property_aux(device_dst, property, value, false); |
57fa1d09 TG |
752 | if (r < 0) |
753 | return r; | |
754 | } | |
755 | ||
756 | return 0; | |
757 | } | |
758 | ||
759 | void device_cleanup_tags(sd_device *device) { | |
760 | assert(device); | |
761 | ||
e77b146f LP |
762 | device->all_tags = set_free_free(device->all_tags); |
763 | device->current_tags = set_free_free(device->current_tags); | |
57fa1d09 | 764 | device->property_tags_outdated = true; |
313cefa1 | 765 | device->tags_generation++; |
57fa1d09 TG |
766 | } |
767 | ||
768 | void device_cleanup_devlinks(sd_device *device) { | |
769 | assert(device); | |
770 | ||
771 | set_free_free(device->devlinks); | |
772 | device->devlinks = NULL; | |
773 | device->property_devlinks_outdated = true; | |
313cefa1 | 774 | device->devlinks_generation++; |
57fa1d09 TG |
775 | } |
776 | ||
777 | void device_remove_tag(sd_device *device, const char *tag) { | |
778 | assert(device); | |
779 | assert(tag); | |
780 | ||
e77b146f | 781 | free(set_remove(device->current_tags, tag)); |
57fa1d09 | 782 | device->property_tags_outdated = true; |
313cefa1 | 783 | device->tags_generation++; |
57fa1d09 TG |
784 | } |
785 | ||
786 | static int device_tag(sd_device *device, const char *tag, bool add) { | |
787 | const char *id; | |
788 | char *path; | |
789 | int r; | |
790 | ||
791 | assert(device); | |
792 | assert(tag); | |
793 | ||
794 | r = device_get_id_filename(device, &id); | |
795 | if (r < 0) | |
796 | return r; | |
797 | ||
798 | path = strjoina("/run/udev/tags/", tag, "/", id); | |
799 | ||
800 | if (add) { | |
801 | r = touch_file(path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0444); | |
802 | if (r < 0) | |
803 | return r; | |
804 | } else { | |
805 | r = unlink(path); | |
806 | if (r < 0 && errno != ENOENT) | |
807 | return -errno; | |
808 | } | |
809 | ||
810 | return 0; | |
811 | } | |
812 | ||
813 | int device_tag_index(sd_device *device, sd_device *device_old, bool add) { | |
814 | const char *tag; | |
815 | int r = 0, k; | |
816 | ||
817 | if (add && device_old) { | |
818 | /* delete possible left-over tags */ | |
819 | FOREACH_DEVICE_TAG(device_old, tag) { | |
820 | if (!sd_device_has_tag(device, tag)) { | |
821 | k = device_tag(device_old, tag, false); | |
822 | if (r >= 0 && k < 0) | |
823 | r = k; | |
824 | } | |
825 | } | |
826 | } | |
827 | ||
828 | FOREACH_DEVICE_TAG(device, tag) { | |
829 | k = device_tag(device, tag, add); | |
830 | if (r >= 0 && k < 0) | |
831 | r = k; | |
832 | } | |
833 | ||
834 | return r; | |
835 | } | |
836 | ||
837 | static bool device_has_info(sd_device *device) { | |
838 | assert(device); | |
839 | ||
840 | if (!set_isempty(device->devlinks)) | |
841 | return true; | |
842 | ||
843 | if (device->devlink_priority != 0) | |
844 | return true; | |
845 | ||
846 | if (!ordered_hashmap_isempty(device->properties_db)) | |
847 | return true; | |
848 | ||
e77b146f LP |
849 | if (!set_isempty(device->all_tags)) |
850 | return true; | |
851 | ||
852 | if (!set_isempty(device->current_tags)) | |
57fa1d09 TG |
853 | return true; |
854 | ||
855 | if (device->watch_handle >= 0) | |
856 | return true; | |
857 | ||
858 | return false; | |
859 | } | |
860 | ||
861 | void device_set_db_persist(sd_device *device) { | |
862 | assert(device); | |
863 | ||
864 | device->db_persist = true; | |
865 | } | |
866 | ||
867 | int device_update_db(sd_device *device) { | |
868 | const char *id; | |
869 | char *path; | |
870 | _cleanup_fclose_ FILE *f = NULL; | |
871 | _cleanup_free_ char *path_tmp = NULL; | |
872 | bool has_info; | |
873 | int r; | |
874 | ||
875 | assert(device); | |
876 | ||
877 | has_info = device_has_info(device); | |
878 | ||
879 | r = device_get_id_filename(device, &id); | |
880 | if (r < 0) | |
881 | return r; | |
882 | ||
883 | path = strjoina("/run/udev/data/", id); | |
884 | ||
885 | /* do not store anything for otherwise empty devices */ | |
886 | if (!has_info && major(device->devnum) == 0 && device->ifindex == 0) { | |
887 | r = unlink(path); | |
888 | if (r < 0 && errno != ENOENT) | |
889 | return -errno; | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | /* write a database file */ | |
895 | r = mkdir_parents(path, 0755); | |
896 | if (r < 0) | |
897 | return r; | |
898 | ||
899 | r = fopen_temporary(path, &f, &path_tmp); | |
900 | if (r < 0) | |
901 | return r; | |
902 | ||
903 | /* | |
904 | * set 'sticky' bit to indicate that we should not clean the | |
905 | * database when we transition from initramfs to the real root | |
906 | */ | |
907 | if (device->db_persist) { | |
908 | r = fchmod(fileno(f), 01644); | |
909 | if (r < 0) { | |
910 | r = -errno; | |
911 | goto fail; | |
912 | } | |
913 | } else { | |
914 | r = fchmod(fileno(f), 0644); | |
915 | if (r < 0) { | |
916 | r = -errno; | |
917 | goto fail; | |
918 | } | |
919 | } | |
920 | ||
921 | if (has_info) { | |
922 | const char *property, *value, *tag; | |
923 | Iterator i; | |
924 | ||
925 | if (major(device->devnum) > 0) { | |
926 | const char *devlink; | |
927 | ||
928 | FOREACH_DEVICE_DEVLINK(device, devlink) | |
fbd0b64f | 929 | fprintf(f, "S:%s\n", devlink + STRLEN("/dev/")); |
57fa1d09 TG |
930 | |
931 | if (device->devlink_priority != 0) | |
932 | fprintf(f, "L:%i\n", device->devlink_priority); | |
933 | ||
934 | if (device->watch_handle >= 0) | |
935 | fprintf(f, "W:%i\n", device->watch_handle); | |
936 | } | |
937 | ||
938 | if (device->usec_initialized > 0) | |
939 | fprintf(f, "I:"USEC_FMT"\n", device->usec_initialized); | |
940 | ||
941 | ORDERED_HASHMAP_FOREACH_KEY(value, property, device->properties_db, i) | |
942 | fprintf(f, "E:%s=%s\n", property, value); | |
943 | ||
944 | FOREACH_DEVICE_TAG(device, tag) | |
e77b146f LP |
945 | fprintf(f, "G:%s\n", tag); /* Any tag */ |
946 | ||
947 | SET_FOREACH(tag, device->current_tags, i) | |
948 | fprintf(f, "Q:%s\n", tag); /* Current tag */ | |
57fa1d09 TG |
949 | } |
950 | ||
951 | r = fflush_and_check(f); | |
952 | if (r < 0) | |
953 | goto fail; | |
954 | ||
955 | r = rename(path_tmp, path); | |
956 | if (r < 0) { | |
957 | r = -errno; | |
958 | goto fail; | |
959 | } | |
960 | ||
c7d54dae YW |
961 | log_device_debug(device, "sd-device: Created %s file '%s' for '%s'", has_info ? "db" : "empty", |
962 | path, device->devpath); | |
57fa1d09 TG |
963 | |
964 | return 0; | |
965 | ||
966 | fail: | |
dacd6cee LP |
967 | (void) unlink(path); |
968 | (void) unlink(path_tmp); | |
57fa1d09 | 969 | |
c7d54dae | 970 | return log_device_debug_errno(device, r, "sd-device: Failed to create %s file '%s' for '%s'", has_info ? "db" : "empty", path, device->devpath); |
57fa1d09 TG |
971 | } |
972 | ||
973 | int device_delete_db(sd_device *device) { | |
974 | const char *id; | |
975 | char *path; | |
976 | int r; | |
977 | ||
978 | assert(device); | |
979 | ||
980 | r = device_get_id_filename(device, &id); | |
981 | if (r < 0) | |
982 | return r; | |
983 | ||
984 | path = strjoina("/run/udev/data/", id); | |
985 | ||
986 | r = unlink(path); | |
987 | if (r < 0 && errno != ENOENT) | |
988 | return -errno; | |
989 | ||
990 | return 0; | |
991 | } | |
5ebd3fc3 YW |
992 | |
993 | static const char* const device_action_table[_DEVICE_ACTION_MAX] = { | |
994 | [DEVICE_ACTION_ADD] = "add", | |
995 | [DEVICE_ACTION_REMOVE] = "remove", | |
996 | [DEVICE_ACTION_CHANGE] = "change", | |
997 | [DEVICE_ACTION_MOVE] = "move", | |
998 | [DEVICE_ACTION_ONLINE] = "online", | |
999 | [DEVICE_ACTION_OFFLINE] = "offline", | |
1000 | [DEVICE_ACTION_BIND] = "bind", | |
1001 | [DEVICE_ACTION_UNBIND] = "unbind", | |
1002 | }; | |
1003 | ||
1004 | DEFINE_STRING_TABLE_LOOKUP(device_action, DeviceAction); | |
9e0196b1 YW |
1005 | |
1006 | void dump_device_action_table(void) { | |
1007 | DUMP_STRING_TABLE(device_action, DeviceAction, _DEVICE_ACTION_MAX); | |
1008 | } |