| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "libudev.h" |
| 6 |
#include "sd-device.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "device-private.h" |
| 10 |
#include "device-util.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "libudev-device-internal.h" |
| 13 |
#include "libudev-list-internal.h" |
| 14 |
|
| 15 |
/** |
| 16 |
* SECTION:libudev-device |
| 17 |
* @short_description: kernel sys devices |
| 18 |
* |
| 19 |
* Representation of kernel sys devices. Devices are uniquely identified |
| 20 |
* by their syspath, every device has exactly one path in the kernel sys |
| 21 |
* filesystem. Devices usually belong to a kernel subsystem, and have |
| 22 |
* a unique name inside that subsystem. |
| 23 |
*/ |
| 24 |
|
| 25 |
/** |
| 26 |
* udev_device: |
| 27 |
* |
| 28 |
* Opaque object representing one kernel sys device. |
| 29 |
*/ |
| 30 |
struct udev_device { |
| 31 |
struct udev *udev; |
| 32 |
|
| 33 |
/* real device object */ |
| 34 |
sd_device *device; |
| 35 |
|
| 36 |
/* legacy */ |
| 37 |
unsigned n_ref; |
| 38 |
|
| 39 |
struct udev_device *parent; |
| 40 |
bool parent_set; |
| 41 |
|
| 42 |
struct udev_list *properties; |
| 43 |
uint64_t properties_generation; |
| 44 |
struct udev_list *all_tags, *current_tags; |
| 45 |
uint64_t all_tags_generation, current_tags_generation; |
| 46 |
struct udev_list *devlinks; |
| 47 |
uint64_t devlinks_generation; |
| 48 |
bool properties_read:1; |
| 49 |
bool all_tags_read:1; |
| 50 |
bool current_tags_read:1; |
| 51 |
bool devlinks_read:1; |
| 52 |
struct udev_list *sysattrs; |
| 53 |
bool sysattrs_read; |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
* udev_device_get_seqnum: |
| 58 |
* @udev_device: udev device |
| 59 |
* |
| 60 |
* This is only valid if the device was received through a monitor. Devices read from |
| 61 |
* sys do not have a sequence number. |
| 62 |
* |
| 63 |
* Returns: the kernel event sequence number, or 0 if there is no sequence number available. |
| 64 |
**/ |
| 65 |
_public_ unsigned long long udev_device_get_seqnum(struct udev_device *udev_device) { |
| 66 |
uint64_t seqnum; |
| 67 |
|
| 68 |
assert_return_errno(udev_device, 0, EINVAL); |
| 69 |
|
| 70 |
if (sd_device_get_seqnum(udev_device->device, &seqnum) < 0) |
| 71 |
return 0; |
| 72 |
|
| 73 |
return seqnum; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* udev_device_get_devnum: |
| 78 |
* @udev_device: udev device |
| 79 |
* |
| 80 |
* Get the device major/minor number. |
| 81 |
* |
| 82 |
* Returns: the dev_t number. |
| 83 |
**/ |
| 84 |
_public_ dev_t udev_device_get_devnum(struct udev_device *udev_device) { |
| 85 |
dev_t devnum; |
| 86 |
int r; |
| 87 |
|
| 88 |
assert_return_errno(udev_device, makedev(0, 0), EINVAL); |
| 89 |
|
| 90 |
r = sd_device_get_devnum(udev_device->device, &devnum); |
| 91 |
if (r == -ENOENT) |
| 92 |
return makedev(0, 0); |
| 93 |
if (r < 0) |
| 94 |
return_with_errno(makedev(0, 0), r); |
| 95 |
|
| 96 |
return devnum; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* udev_device_get_driver: |
| 101 |
* @udev_device: udev device |
| 102 |
* |
| 103 |
* Get the kernel driver name. |
| 104 |
* |
| 105 |
* Returns: the driver name string, or #NULL if there is no driver attached. |
| 106 |
**/ |
| 107 |
_public_ const char* udev_device_get_driver(struct udev_device *udev_device) { |
| 108 |
const char *driver; |
| 109 |
int r; |
| 110 |
|
| 111 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 112 |
|
| 113 |
r = sd_device_get_driver(udev_device->device, &driver); |
| 114 |
if (r < 0) |
| 115 |
return_with_errno(NULL, r); |
| 116 |
|
| 117 |
return driver; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* udev_device_get_devtype: |
| 122 |
* @udev_device: udev device |
| 123 |
* |
| 124 |
* Retrieve the devtype string of the udev device. |
| 125 |
* |
| 126 |
* Returns: the devtype name of the udev device, or #NULL if it cannot be determined |
| 127 |
**/ |
| 128 |
_public_ const char* udev_device_get_devtype(struct udev_device *udev_device) { |
| 129 |
const char *devtype; |
| 130 |
int r; |
| 131 |
|
| 132 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 133 |
|
| 134 |
r = sd_device_get_devtype(udev_device->device, &devtype); |
| 135 |
if (r == -ENOENT) |
| 136 |
return NULL; |
| 137 |
if (r < 0) |
| 138 |
return_with_errno(NULL, r); |
| 139 |
|
| 140 |
return devtype; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* udev_device_get_subsystem: |
| 145 |
* @udev_device: udev device |
| 146 |
* |
| 147 |
* Retrieve the subsystem string of the udev device. The string does not |
| 148 |
* contain any "/". |
| 149 |
* |
| 150 |
* Returns: the subsystem name of the udev device, or #NULL if it cannot be determined |
| 151 |
**/ |
| 152 |
_public_ const char* udev_device_get_subsystem(struct udev_device *udev_device) { |
| 153 |
const char *subsystem; |
| 154 |
int r; |
| 155 |
|
| 156 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 157 |
|
| 158 |
r = sd_device_get_subsystem(udev_device->device, &subsystem); |
| 159 |
if (r < 0) |
| 160 |
return_with_errno(NULL, r); |
| 161 |
|
| 162 |
return subsystem; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* udev_device_get_property_value: |
| 167 |
* @udev_device: udev device |
| 168 |
* @key: property name |
| 169 |
* |
| 170 |
* Get the value of a given property. |
| 171 |
* |
| 172 |
* Returns: the property string, or #NULL if there is no such property. |
| 173 |
**/ |
| 174 |
_public_ const char* udev_device_get_property_value(struct udev_device *udev_device, const char *key) { |
| 175 |
const char *value; |
| 176 |
int r; |
| 177 |
|
| 178 |
assert_return_errno(udev_device && key, NULL, EINVAL); |
| 179 |
|
| 180 |
r = sd_device_get_property_value(udev_device->device, key, &value); |
| 181 |
if (r < 0) |
| 182 |
return_with_errno(NULL, r); |
| 183 |
|
| 184 |
return value; |
| 185 |
} |
| 186 |
|
| 187 |
struct udev_device* udev_device_new(struct udev *udev, sd_device *device) { |
| 188 |
_cleanup_(udev_list_freep) struct udev_list *properties = NULL, *all_tags = NULL, *current_tags = NULL, *sysattrs = NULL, *devlinks = NULL; |
| 189 |
struct udev_device *udev_device; |
| 190 |
|
| 191 |
assert(device); |
| 192 |
|
| 193 |
properties = udev_list_new(true); |
| 194 |
if (!properties) |
| 195 |
return_with_errno(NULL, ENOMEM); |
| 196 |
all_tags = udev_list_new(true); |
| 197 |
if (!all_tags) |
| 198 |
return_with_errno(NULL, ENOMEM); |
| 199 |
current_tags = udev_list_new(true); |
| 200 |
if (!current_tags) |
| 201 |
return_with_errno(NULL, ENOMEM); |
| 202 |
sysattrs = udev_list_new(true); |
| 203 |
if (!sysattrs) |
| 204 |
return_with_errno(NULL, ENOMEM); |
| 205 |
devlinks = udev_list_new(true); |
| 206 |
if (!devlinks) |
| 207 |
return_with_errno(NULL, ENOMEM); |
| 208 |
|
| 209 |
udev_device = new(struct udev_device, 1); |
| 210 |
if (!udev_device) |
| 211 |
return_with_errno(NULL, ENOMEM); |
| 212 |
|
| 213 |
*udev_device = (struct udev_device) { |
| 214 |
.n_ref = 1, |
| 215 |
.udev = udev, |
| 216 |
.device = sd_device_ref(device), |
| 217 |
.properties = TAKE_PTR(properties), |
| 218 |
.all_tags = TAKE_PTR(all_tags), |
| 219 |
.current_tags = TAKE_PTR(current_tags), |
| 220 |
.sysattrs = TAKE_PTR(sysattrs), |
| 221 |
.devlinks = TAKE_PTR(devlinks), |
| 222 |
}; |
| 223 |
|
| 224 |
return udev_device; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* udev_device_new_from_syspath: |
| 229 |
* @udev: udev library context |
| 230 |
* @syspath: sys device path including sys directory |
| 231 |
* |
| 232 |
* Create new udev device, and fill in information from the sys |
| 233 |
* device and the udev database entry. The syspath is the absolute |
| 234 |
* path to the device, including the sys mount point. |
| 235 |
* |
| 236 |
* The initial refcount is 1, and needs to be decremented to |
| 237 |
* release the resources of the udev device. |
| 238 |
* |
| 239 |
* Returns: a new udev device, or #NULL, if it does not exist |
| 240 |
**/ |
| 241 |
_public_ struct udev_device* udev_device_new_from_syspath(struct udev *udev, const char *syspath) { |
| 242 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL; |
| 243 |
int r; |
| 244 |
|
| 245 |
r = sd_device_new_from_syspath(&device, syspath); |
| 246 |
if (r < 0) |
| 247 |
return_with_errno(NULL, r); |
| 248 |
|
| 249 |
return udev_device_new(udev, device); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* udev_device_new_from_devnum: |
| 254 |
* @udev: udev library context |
| 255 |
* @type: char or block device |
| 256 |
* @devnum: device major/minor number |
| 257 |
* |
| 258 |
* Create new udev device, and fill in information from the sys |
| 259 |
* device and the udev database entry. The device is looked-up |
| 260 |
* by its major/minor number and type. Character and block device |
| 261 |
* numbers are not unique across the two types. |
| 262 |
* |
| 263 |
* The initial refcount is 1, and needs to be decremented to |
| 264 |
* release the resources of the udev device. |
| 265 |
* |
| 266 |
* Returns: a new udev device, or #NULL, if it does not exist |
| 267 |
**/ |
| 268 |
_public_ struct udev_device* udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum) { |
| 269 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL; |
| 270 |
int r; |
| 271 |
|
| 272 |
r = sd_device_new_from_devnum(&device, type, devnum); |
| 273 |
if (r < 0) |
| 274 |
return_with_errno(NULL, r); |
| 275 |
|
| 276 |
return udev_device_new(udev, device); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* udev_device_new_from_device_id: |
| 281 |
* @udev: udev library context |
| 282 |
* @id: text string identifying a kernel device |
| 283 |
* |
| 284 |
* Create new udev device, and fill in information from the sys |
| 285 |
* device and the udev database entry. The device is looked-up |
| 286 |
* by a special string: |
| 287 |
* b8:2 - block device major:minor |
| 288 |
* c128:1 - char device major:minor |
| 289 |
* n3 - network device ifindex |
| 290 |
* +sound:card29 - kernel driver core subsystem:device name |
| 291 |
* |
| 292 |
* The initial refcount is 1, and needs to be decremented to |
| 293 |
* release the resources of the udev device. |
| 294 |
* |
| 295 |
* Returns: a new udev device, or #NULL, if it does not exist |
| 296 |
**/ |
| 297 |
_public_ struct udev_device* udev_device_new_from_device_id(struct udev *udev, const char *id) { |
| 298 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL; |
| 299 |
int r; |
| 300 |
|
| 301 |
r = sd_device_new_from_device_id(&device, id); |
| 302 |
if (r < 0) |
| 303 |
return_with_errno(NULL, r); |
| 304 |
|
| 305 |
return udev_device_new(udev, device); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* udev_device_new_from_subsystem_sysname: |
| 310 |
* @udev: udev library context |
| 311 |
* @subsystem: the subsystem of the device |
| 312 |
* @sysname: the name of the device |
| 313 |
* |
| 314 |
* Create new udev device, and fill in information from the sys device |
| 315 |
* and the udev database entry. The device is looked up by the subsystem |
| 316 |
* and name string of the device, like "mem" / "zero", or "block" / "sda". |
| 317 |
* |
| 318 |
* The initial refcount is 1, and needs to be decremented to |
| 319 |
* release the resources of the udev device. |
| 320 |
* |
| 321 |
* Returns: a new udev device, or #NULL, if it does not exist |
| 322 |
**/ |
| 323 |
_public_ struct udev_device* udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname) { |
| 324 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL; |
| 325 |
int r; |
| 326 |
|
| 327 |
r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname); |
| 328 |
if (r < 0) |
| 329 |
return_with_errno(NULL, r); |
| 330 |
|
| 331 |
return udev_device_new(udev, device); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* udev_device_new_from_environment |
| 336 |
* @udev: udev library context |
| 337 |
* |
| 338 |
* Create new udev device, and fill in information from the |
| 339 |
* current process environment. This only works reliably if |
| 340 |
* the process is called from a udev rule. It is usually used |
| 341 |
* for tools executed from IMPORT= rules. |
| 342 |
* |
| 343 |
* The initial refcount is 1, and needs to be decremented to |
| 344 |
* release the resources of the udev device. |
| 345 |
* |
| 346 |
* Returns: a new udev device, or #NULL, if it does not exist |
| 347 |
**/ |
| 348 |
_public_ struct udev_device* udev_device_new_from_environment(struct udev *udev) { |
| 349 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL; |
| 350 |
int r; |
| 351 |
|
| 352 |
r = device_new_from_strv(&device, environ); |
| 353 |
if (r < 0) |
| 354 |
return_with_errno(NULL, r); |
| 355 |
|
| 356 |
return udev_device_new(udev, device); |
| 357 |
} |
| 358 |
|
| 359 |
static struct udev_device* device_new_from_parent(struct udev_device *child) { |
| 360 |
sd_device *parent; |
| 361 |
int r; |
| 362 |
|
| 363 |
assert_return_errno(child, NULL, EINVAL); |
| 364 |
|
| 365 |
r = sd_device_get_parent(child->device, &parent); |
| 366 |
if (r < 0) |
| 367 |
return_with_errno(NULL, r); |
| 368 |
|
| 369 |
return udev_device_new(child->udev, parent); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* udev_device_get_parent: |
| 374 |
* @udev_device: the device to start searching from |
| 375 |
* |
| 376 |
* Find the next parent device, and fill in information from the sys |
| 377 |
* device and the udev database entry. |
| 378 |
* |
| 379 |
* Returned device is not referenced. It is attached to the child |
| 380 |
* device, and will be cleaned up when the child device is cleaned up. |
| 381 |
* |
| 382 |
* It is not necessarily just the upper level directory, empty or not |
| 383 |
* recognized sys directories are ignored. |
| 384 |
* |
| 385 |
* It can be called as many times as needed, without caring about |
| 386 |
* references. |
| 387 |
* |
| 388 |
* Returns: a new udev device, or #NULL, if it no parent exist. |
| 389 |
**/ |
| 390 |
_public_ struct udev_device* udev_device_get_parent(struct udev_device *udev_device) { |
| 391 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 392 |
|
| 393 |
if (!udev_device->parent_set) { |
| 394 |
udev_device->parent_set = true; |
| 395 |
udev_device->parent = device_new_from_parent(udev_device); |
| 396 |
} |
| 397 |
|
| 398 |
/* TODO: errno will differ here in case parent == NULL */ |
| 399 |
return udev_device->parent; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* udev_device_get_parent_with_subsystem_devtype: |
| 404 |
* @udev_device: udev device to start searching from |
| 405 |
* @subsystem: the subsystem of the device |
| 406 |
* @devtype: the type (DEVTYPE) of the device |
| 407 |
* |
| 408 |
* Find the next parent device, with a matching subsystem and devtype |
| 409 |
* value, and fill in information from the sys device and the udev |
| 410 |
* database entry. |
| 411 |
* |
| 412 |
* If devtype is #NULL, only subsystem is checked, and any devtype will |
| 413 |
* match. |
| 414 |
* |
| 415 |
* Returned device is not referenced. It is attached to the child |
| 416 |
* device, and will be cleaned up when the child device is cleaned up. |
| 417 |
* |
| 418 |
* It can be called as many times as needed, without caring about |
| 419 |
* references. |
| 420 |
* |
| 421 |
* Returns: a new udev device, or #NULL if no matching parent exists. |
| 422 |
**/ |
| 423 |
_public_ struct udev_device* udev_device_get_parent_with_subsystem_devtype( |
| 424 |
struct udev_device *udev_device, |
| 425 |
const char *subsystem, |
| 426 |
const char *devtype) { |
| 427 |
|
| 428 |
sd_device *parent; |
| 429 |
int r; |
| 430 |
|
| 431 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 432 |
|
| 433 |
/* this relies on the fact that finding the subdevice of a parent or the |
| 434 |
parent of a subdevice commute */ |
| 435 |
|
| 436 |
/* first find the correct sd_device */ |
| 437 |
r = sd_device_get_parent_with_subsystem_devtype(udev_device->device, subsystem, devtype, &parent); |
| 438 |
if (r < 0) |
| 439 |
return_with_errno(NULL, r); |
| 440 |
|
| 441 |
/* then walk the chain of udev_device parents until the corresponding |
| 442 |
one is found */ |
| 443 |
while ((udev_device = udev_device_get_parent(udev_device))) |
| 444 |
if (udev_device->device == parent) |
| 445 |
return udev_device; |
| 446 |
|
| 447 |
return_with_errno(NULL, ENOENT); |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* udev_device_get_udev: |
| 452 |
* @udev_device: udev device |
| 453 |
* |
| 454 |
* Retrieve the udev library context the device was created with. |
| 455 |
* |
| 456 |
* Returns: the udev library context |
| 457 |
**/ |
| 458 |
_public_ struct udev* udev_device_get_udev(struct udev_device *udev_device) { |
| 459 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 460 |
|
| 461 |
return udev_device->udev; |
| 462 |
} |
| 463 |
|
| 464 |
static struct udev_device* udev_device_free(struct udev_device *udev_device) { |
| 465 |
assert(udev_device); |
| 466 |
|
| 467 |
sd_device_unref(udev_device->device); |
| 468 |
udev_device_unref(udev_device->parent); |
| 469 |
|
| 470 |
udev_list_free(udev_device->properties); |
| 471 |
udev_list_free(udev_device->sysattrs); |
| 472 |
udev_list_free(udev_device->all_tags); |
| 473 |
udev_list_free(udev_device->current_tags); |
| 474 |
udev_list_free(udev_device->devlinks); |
| 475 |
|
| 476 |
return mfree(udev_device); |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* udev_device_ref: |
| 481 |
* @udev_device: udev device |
| 482 |
* |
| 483 |
* Take a reference of a udev device. |
| 484 |
* |
| 485 |
* Returns: the passed udev device |
| 486 |
**/ |
| 487 |
|
| 488 |
/** |
| 489 |
* udev_device_unref: |
| 490 |
* @udev_device: udev device |
| 491 |
* |
| 492 |
* Drop a reference of a udev device. If the refcount reaches zero, |
| 493 |
* the resources of the device will be released. |
| 494 |
* |
| 495 |
* Returns: #NULL |
| 496 |
**/ |
| 497 |
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_device, udev_device, udev_device_free); |
| 498 |
|
| 499 |
/** |
| 500 |
* udev_device_get_devpath: |
| 501 |
* @udev_device: udev device |
| 502 |
* |
| 503 |
* Retrieve the kernel devpath value of the udev device. The path |
| 504 |
* does not contain the sys mount point, and starts with a '/'. |
| 505 |
* |
| 506 |
* Returns: the devpath of the udev device |
| 507 |
**/ |
| 508 |
_public_ const char* udev_device_get_devpath(struct udev_device *udev_device) { |
| 509 |
const char *devpath; |
| 510 |
int r; |
| 511 |
|
| 512 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 513 |
|
| 514 |
r = sd_device_get_devpath(udev_device->device, &devpath); |
| 515 |
if (r < 0) |
| 516 |
return_with_errno(NULL, r); |
| 517 |
|
| 518 |
return devpath; |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* udev_device_get_syspath: |
| 523 |
* @udev_device: udev device |
| 524 |
* |
| 525 |
* Retrieve the sys path of the udev device. The path is an |
| 526 |
* absolute path and starts with the sys mount point. |
| 527 |
* |
| 528 |
* Returns: the sys path of the udev device |
| 529 |
**/ |
| 530 |
_public_ const char* udev_device_get_syspath(struct udev_device *udev_device) { |
| 531 |
const char *syspath; |
| 532 |
int r; |
| 533 |
|
| 534 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 535 |
|
| 536 |
r = sd_device_get_syspath(udev_device->device, &syspath); |
| 537 |
if (r < 0) |
| 538 |
return_with_errno(NULL, r); |
| 539 |
|
| 540 |
return syspath; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* udev_device_get_sysname: |
| 545 |
* @udev_device: udev device |
| 546 |
* |
| 547 |
* Get the kernel device name in /sys. |
| 548 |
* |
| 549 |
* Returns: the name string of the device |
| 550 |
**/ |
| 551 |
_public_ const char* udev_device_get_sysname(struct udev_device *udev_device) { |
| 552 |
const char *sysname; |
| 553 |
int r; |
| 554 |
|
| 555 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 556 |
|
| 557 |
r = sd_device_get_sysname(udev_device->device, &sysname); |
| 558 |
if (r < 0) |
| 559 |
return_with_errno(NULL, r); |
| 560 |
|
| 561 |
return sysname; |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* udev_device_get_sysnum: |
| 566 |
* @udev_device: udev device |
| 567 |
* |
| 568 |
* Get the instance number of the device. |
| 569 |
* |
| 570 |
* Returns: the trailing number string of the device name |
| 571 |
**/ |
| 572 |
_public_ const char* udev_device_get_sysnum(struct udev_device *udev_device) { |
| 573 |
const char *sysnum; |
| 574 |
int r; |
| 575 |
|
| 576 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 577 |
|
| 578 |
r = sd_device_get_sysnum(udev_device->device, &sysnum); |
| 579 |
if (r == -ENOENT) |
| 580 |
return NULL; |
| 581 |
if (r < 0) |
| 582 |
return_with_errno(NULL, r); |
| 583 |
|
| 584 |
return sysnum; |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* udev_device_get_devnode: |
| 589 |
* @udev_device: udev device |
| 590 |
* |
| 591 |
* Retrieve the device node file name belonging to the udev device. |
| 592 |
* The path is an absolute path, and starts with the device directory. |
| 593 |
* |
| 594 |
* Returns: the device node file name of the udev device, or #NULL if no device node exists |
| 595 |
**/ |
| 596 |
_public_ const char* udev_device_get_devnode(struct udev_device *udev_device) { |
| 597 |
const char *devnode; |
| 598 |
int r; |
| 599 |
|
| 600 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 601 |
|
| 602 |
r = sd_device_get_devname(udev_device->device, &devnode); |
| 603 |
if (r < 0) |
| 604 |
return_with_errno(NULL, r); |
| 605 |
|
| 606 |
return devnode; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* udev_device_get_devlinks_list_entry: |
| 611 |
* @udev_device: udev device |
| 612 |
* |
| 613 |
* Retrieve the list of device links pointing to the device file of |
| 614 |
* the udev device. The next list entry can be retrieved with |
| 615 |
* udev_list_entry_get_next(), which returns #NULL if no more entries exist. |
| 616 |
* The devlink path can be retrieved from the list entry by |
| 617 |
* udev_list_entry_get_name(). The path is an absolute path, and starts with |
| 618 |
* the device directory. |
| 619 |
* |
| 620 |
* Returns: the first entry of the device node link list |
| 621 |
**/ |
| 622 |
_public_ struct udev_list_entry* udev_device_get_devlinks_list_entry(struct udev_device *udev_device) { |
| 623 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 624 |
|
| 625 |
if (device_get_devlinks_generation(udev_device->device) != udev_device->devlinks_generation || |
| 626 |
!udev_device->devlinks_read) { |
| 627 |
udev_list_cleanup(udev_device->devlinks); |
| 628 |
|
| 629 |
FOREACH_DEVICE_DEVLINK(udev_device->device, devlink) |
| 630 |
if (!udev_list_entry_add(udev_device->devlinks, devlink, NULL)) |
| 631 |
return_with_errno(NULL, ENOMEM); |
| 632 |
|
| 633 |
udev_device->devlinks_read = true; |
| 634 |
udev_device->devlinks_generation = device_get_devlinks_generation(udev_device->device); |
| 635 |
} |
| 636 |
|
| 637 |
return udev_list_get_entry(udev_device->devlinks); |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* udev_device_get_event_properties_entry: |
| 642 |
* @udev_device: udev device |
| 643 |
* |
| 644 |
* Retrieve the list of key/value device properties of the udev |
| 645 |
* device. The next list entry can be retrieved with udev_list_entry_get_next(), |
| 646 |
* which returns #NULL if no more entries exist. The property name |
| 647 |
* can be retrieved from the list entry by udev_list_entry_get_name(), |
| 648 |
* the property value by udev_list_entry_get_value(). |
| 649 |
* |
| 650 |
* Returns: the first entry of the property list |
| 651 |
**/ |
| 652 |
_public_ struct udev_list_entry* udev_device_get_properties_list_entry(struct udev_device *udev_device) { |
| 653 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 654 |
|
| 655 |
if (device_get_properties_generation(udev_device->device) != udev_device->properties_generation || |
| 656 |
!udev_device->properties_read) { |
| 657 |
udev_list_cleanup(udev_device->properties); |
| 658 |
|
| 659 |
FOREACH_DEVICE_PROPERTY(udev_device->device, key, value) |
| 660 |
if (!udev_list_entry_add(udev_device->properties, key, value)) |
| 661 |
return_with_errno(NULL, ENOMEM); |
| 662 |
|
| 663 |
udev_device->properties_read = true; |
| 664 |
udev_device->properties_generation = device_get_properties_generation(udev_device->device); |
| 665 |
} |
| 666 |
|
| 667 |
return udev_list_get_entry(udev_device->properties); |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* udev_device_get_action: |
| 672 |
* @udev_device: udev device |
| 673 |
* |
| 674 |
* This is only valid if the device was received through a monitor. Devices read from |
| 675 |
* sys do not have an action string. Usual actions are: add, remove, change, move, |
| 676 |
* online, offline. |
| 677 |
* |
| 678 |
* Returns: the kernel action value, or #NULL if there is no action value available. |
| 679 |
**/ |
| 680 |
_public_ const char* udev_device_get_action(struct udev_device *udev_device) { |
| 681 |
sd_device_action_t action; |
| 682 |
|
| 683 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 684 |
|
| 685 |
if (sd_device_get_action(udev_device->device, &action) < 0) |
| 686 |
return NULL; |
| 687 |
|
| 688 |
return device_action_to_string(action); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* udev_device_get_usec_since_initialized: |
| 693 |
* @udev_device: udev device |
| 694 |
* |
| 695 |
* Return the number of microseconds passed since udev set up the |
| 696 |
* device for the first time. |
| 697 |
* |
| 698 |
* This is only implemented for devices with need to store properties |
| 699 |
* in the udev database. All other devices return 0 here. |
| 700 |
* |
| 701 |
* Returns: the number of microseconds since the device was first seen. |
| 702 |
**/ |
| 703 |
_public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device) { |
| 704 |
usec_t ts; |
| 705 |
int r; |
| 706 |
|
| 707 |
assert_return(udev_device, -EINVAL); |
| 708 |
|
| 709 |
r = sd_device_get_usec_since_initialized(udev_device->device, &ts); |
| 710 |
if (r < 0) |
| 711 |
return_with_errno(0, r); |
| 712 |
|
| 713 |
return ts; |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
* udev_device_get_sysattr_value: |
| 718 |
* @udev_device: udev device |
| 719 |
* @sysattr: attribute name |
| 720 |
* |
| 721 |
* The retrieved value is cached in the device. Repeated calls will return the same |
| 722 |
* value and not open the attribute again. |
| 723 |
* |
| 724 |
* Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value. |
| 725 |
**/ |
| 726 |
_public_ const char* udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr) { |
| 727 |
const char *value; |
| 728 |
int r; |
| 729 |
|
| 730 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 731 |
|
| 732 |
r = sd_device_get_sysattr_value(udev_device->device, sysattr, &value); |
| 733 |
if (r < 0) |
| 734 |
return_with_errno(NULL, r); |
| 735 |
|
| 736 |
return value; |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* udev_device_set_sysattr_value: |
| 741 |
* @udev_device: udev device |
| 742 |
* @sysattr: attribute name |
| 743 |
* @value: new value to be set |
| 744 |
* |
| 745 |
* Update the contents of the sys attribute and the cached value of the device. |
| 746 |
* |
| 747 |
* Returns: Negative error code on failure or 0 on success. |
| 748 |
**/ |
| 749 |
_public_ int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, const char *value) { |
| 750 |
int r; |
| 751 |
|
| 752 |
assert_return(udev_device, -EINVAL); |
| 753 |
|
| 754 |
r = sd_device_set_sysattr_value(udev_device->device, sysattr, value); |
| 755 |
if (r < 0) |
| 756 |
return r; |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* udev_device_get_sysattr_list_entry: |
| 763 |
* @udev_device: udev device |
| 764 |
* |
| 765 |
* Retrieve the list of available sysattrs, with value being empty; |
| 766 |
* This just return all available sysfs attributes for a particular |
| 767 |
* device without reading their values. |
| 768 |
* |
| 769 |
* Returns: the first entry of the property list |
| 770 |
**/ |
| 771 |
_public_ struct udev_list_entry* udev_device_get_sysattr_list_entry(struct udev_device *udev_device) { |
| 772 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 773 |
|
| 774 |
if (!udev_device->sysattrs_read) { |
| 775 |
udev_list_cleanup(udev_device->sysattrs); |
| 776 |
|
| 777 |
FOREACH_DEVICE_SYSATTR(udev_device->device, sysattr) |
| 778 |
if (!udev_list_entry_add(udev_device->sysattrs, sysattr, NULL)) |
| 779 |
return_with_errno(NULL, ENOMEM); |
| 780 |
|
| 781 |
udev_device->sysattrs_read = true; |
| 782 |
} |
| 783 |
|
| 784 |
return udev_list_get_entry(udev_device->sysattrs); |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
* udev_device_get_is_initialized: |
| 789 |
* @udev_device: udev device |
| 790 |
* |
| 791 |
* Check if udev has already handled the device and has set up |
| 792 |
* device node permissions and context, or has renamed a network |
| 793 |
* device. |
| 794 |
* |
| 795 |
* This is only implemented for devices with a device node |
| 796 |
* or network interfaces. All other devices return 1 here. |
| 797 |
* |
| 798 |
* Returns: 1 if the device is set up. 0 otherwise. |
| 799 |
**/ |
| 800 |
_public_ int udev_device_get_is_initialized(struct udev_device *udev_device) { |
| 801 |
int r; |
| 802 |
|
| 803 |
assert_return(udev_device, -EINVAL); |
| 804 |
|
| 805 |
r = sd_device_get_is_initialized(udev_device->device); |
| 806 |
if (r < 0) |
| 807 |
return_with_errno(0, r); |
| 808 |
|
| 809 |
return r; |
| 810 |
} |
| 811 |
|
| 812 |
/** |
| 813 |
* udev_device_get_tags_list_entry: |
| 814 |
* @udev_device: udev device |
| 815 |
* |
| 816 |
* Retrieve the list of tags attached to the udev device. The next |
| 817 |
* list entry can be retrieved with udev_list_entry_get_next(), |
| 818 |
* which returns #NULL if no more entries exist. The tag string |
| 819 |
* can be retrieved from the list entry by udev_list_entry_get_name(). |
| 820 |
* |
| 821 |
* Returns: the first entry of the tag list |
| 822 |
**/ |
| 823 |
_public_ struct udev_list_entry* udev_device_get_tags_list_entry(struct udev_device *udev_device) { |
| 824 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 825 |
|
| 826 |
if (device_get_tags_generation(udev_device->device) != udev_device->all_tags_generation || |
| 827 |
!udev_device->all_tags_read) { |
| 828 |
udev_list_cleanup(udev_device->all_tags); |
| 829 |
|
| 830 |
FOREACH_DEVICE_TAG(udev_device->device, tag) |
| 831 |
if (!udev_list_entry_add(udev_device->all_tags, tag, NULL)) |
| 832 |
return_with_errno(NULL, ENOMEM); |
| 833 |
|
| 834 |
udev_device->all_tags_read = true; |
| 835 |
udev_device->all_tags_generation = device_get_tags_generation(udev_device->device); |
| 836 |
} |
| 837 |
|
| 838 |
return udev_list_get_entry(udev_device->all_tags); |
| 839 |
} |
| 840 |
|
| 841 |
_public_ struct udev_list_entry* udev_device_get_current_tags_list_entry(struct udev_device *udev_device) { |
| 842 |
assert_return_errno(udev_device, NULL, EINVAL); |
| 843 |
|
| 844 |
if (device_get_tags_generation(udev_device->device) != udev_device->current_tags_generation || |
| 845 |
!udev_device->current_tags_read) { |
| 846 |
udev_list_cleanup(udev_device->current_tags); |
| 847 |
|
| 848 |
FOREACH_DEVICE_CURRENT_TAG(udev_device->device, tag) |
| 849 |
if (!udev_list_entry_add(udev_device->current_tags, tag, NULL)) |
| 850 |
return_with_errno(NULL, ENOMEM); |
| 851 |
|
| 852 |
udev_device->current_tags_read = true; |
| 853 |
udev_device->current_tags_generation = device_get_tags_generation(udev_device->device); |
| 854 |
} |
| 855 |
|
| 856 |
return udev_list_get_entry(udev_device->current_tags); |
| 857 |
} |
| 858 |
|
| 859 |
/** |
| 860 |
* udev_device_has_tag: |
| 861 |
* @udev_device: udev device |
| 862 |
* @tag: tag name |
| 863 |
* |
| 864 |
* Check if a given device has a certain tag associated. |
| 865 |
* |
| 866 |
* Returns: 1 if the tag is found. 0 otherwise. |
| 867 |
**/ |
| 868 |
_public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag) { |
| 869 |
assert_return(udev_device, 0); |
| 870 |
|
| 871 |
return sd_device_has_tag(udev_device->device, tag) > 0; |
| 872 |
} |
| 873 |
|
| 874 |
_public_ int udev_device_has_current_tag(struct udev_device *udev_device, const char *tag) { |
| 875 |
assert_return(udev_device, 0); |
| 876 |
|
| 877 |
return sd_device_has_current_tag(udev_device->device, tag) > 0; |
| 878 |
} |
| 879 |
|
| 880 |
sd_device* udev_device_get_sd_device(struct udev_device *udev_device) { |
| 881 |
assert(udev_device); |
| 882 |
|
| 883 |
return udev_device->device; |
| 884 |
} |