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