clang -cc1 -cc1 -triple x86_64-redhat-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name registry.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/__w/nvme-cli/nvme-cli/.build-ci -fcoverage-compilation-dir=/__w/nvme-cli/nvme-cli/.build-ci -resource-dir /usr/bin/../lib/clang/22 -include /__w/nvme-cli/nvme-cli/.build-ci/nvme-config.h -I libnvme/src/libnvme3.so.1.0.0.p -I libnvme/src -I ../libnvme/src -I ccan -I ../ccan -I . -I .. -D _FILE_OFFSET_BITS=64 -D _GNU_SOURCE -U NDEBUG -internal-isystem /usr/bin/../lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/16/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=gnu11 -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-opt-analyze-headers -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /__w/nvme-cli/nvme-cli/.build-ci/scan-results/2026-10-24-125112-639-1 -x c ../libnvme/src/nvme/registry.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include <dirent.h> |
| 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <limits.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <shared/compiler-attributes-util.h> |
| 22 | #include <shared/fs-util.h> |
| 23 | #include <shared/io-util.h> |
| 24 | |
| 25 | #include "cleanup.h" |
| 26 | #include "private.h" |
| 27 | #include "private-fabrics.h" |
| 28 | #include "registry.h" |
| 29 | |
| 30 | #define REGISTRY_DIR_DEFAULT RUNDIR "/nvme/registry" |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | static const char *registry_dir(struct libnvme_global_ctx *ctx) |
| 41 | { |
| 42 | static const char *cache; |
| 43 | static char buf[PATH_MAX]; |
| 44 | int n; |
| 45 | |
| 46 | if (cache) |
| 47 | return cache; |
| 48 | if (!ctx->test_base_dir) { |
| 49 | cache = REGISTRY_DIR_DEFAULT; |
| 50 | return cache; |
| 51 | } |
| 52 | |
| 53 | n = snprintf(buf, sizeof(buf), "%s/registry", ctx->test_base_dir); |
| 54 | cache = (n > 0 && (size_t)n < sizeof(buf)) ? buf : REGISTRY_DIR_DEFAULT; |
| 55 | return cache; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | static char *registry_path(struct libnvme_global_ctx *ctx, const char *device, |
| 64 | const char *attr) |
| 65 | { |
| 66 | char *path = NULL; |
| 67 | int n; |
| 68 | |
| 69 | if (attr) |
| 70 | n = asprintf(&path, "%s/%s/%s", registry_dir(ctx), |
| 71 | device, attr); |
| 72 | else |
| 73 | n = asprintf(&path, "%s/%s", registry_dir(ctx), device); |
| 74 | return n < 0 ? NULL : path; |
| 75 | } |
| 76 | |
| 77 | static bool valid_device(const char *device) |
| 78 | { |
| 79 | const char *p; |
| 80 | |
| 81 | if (!device || strncmp(device, "nvme", 4) != 0) |
| 82 | return false; |
| 83 | p = device + 4; |
| 84 | if (!*p) |
| 85 | return false; |
| 86 | for (; *p; p++) { |
| 87 | if (*p < '0' || *p > '9') |
| 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | static bool valid_attr(const char *attr) |
| 94 | { |
| 95 | const char *p; |
| 96 | |
| 97 | if (!attr || !*attr) |
| 98 | return false; |
| 99 | for (p = attr; *p; p++) { |
| 100 | if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || |
| 101 | (*p >= '0' && *p <= '9') || *p == '_' || *p == '-') |
| 102 | continue; |
| 103 | return false; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | static ssize_t read_full(int fd, char *buf, size_t count) |
| 113 | { |
| 114 | size_t off = 0; |
| 115 | |
| 116 | while (off < count) { |
| 117 | ssize_t r = read(fd, buf + off, count - off); |
| 118 | |
| 119 | if (r < 0) { |
| 120 | if (errno == EINTR) |
| 121 | continue; |
| 122 | return -errno; |
| 123 | } |
| 124 | if (r == 0) |
| 125 | break; |
| 126 | off += r; |
| 127 | } |
| 128 | return off; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | static int read_attr_fd(int fd, char **out) |
| 138 | { |
| 139 | struct stat st; |
| 140 | char *val; |
| 141 | ssize_t n; |
| 142 | |
| 143 | if (fstat(fd, &st) < 0) |
| 144 | return -errno; |
| 145 | if (st.st_size == 0) |
| 146 | return -ENOENT; |
| 147 | |
| 148 | val = malloc(st.st_size + 1); |
| 149 | if (!val) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | n = read_full(fd, val, st.st_size); |
| 153 | if (n < 0) { |
| 154 | free(val); |
| 155 | return n; |
| 156 | } |
| 157 | |
| 158 | while (n > 0 && (val[n - 1] == '\n' || val[n - 1] == '\r')) |
| 159 | n--; |
| 160 | val[n] = '\0'; |
| 161 | *out = val; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | static int write_attr_atomic(int dir_fd, const char *dir_path, |
| 182 | const char *attr, const char *value) |
| 183 | { |
| 184 | __cleanup_free char *tmp_path = NULL; |
| 185 | __cleanup_free char *final_path = NULL; |
| 186 | int fd, ret; |
| 187 | |
| 188 | if (!valid_attr(attr)) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | if (!value) { |
| 192 | if (unlinkat(dir_fd, attr, 0) < 0 && errno != ENOENT) |
| 193 | return -errno; |
| 194 | fsync(dir_fd); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | if (asprintf(&final_path, "%s/%s", dir_path, attr) < 0) |
| 199 | return -ENOMEM; |
| 200 | if (asprintf(&tmp_path, "%s/%s.tmp.XXXXXX", dir_path, attr) < 0) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | fd = shr_mkstemp(tmp_path); |
| 204 | if (fd < 0) |
| 205 | return fd; |
| 206 | |
| 207 | |
| 208 | if (fchmod(fd, 0644) < 0) { |
| 209 | ret = -errno; |
| 210 | goto err; |
| 211 | } |
| 212 | |
| 213 | ret = shr_write_all(fd, value, strlen(value)); |
| 214 | if (ret) |
| 215 | goto err; |
| 216 | ret = shr_write_all(fd, "\n", 1); |
| 217 | if (ret) |
| 218 | goto err; |
| 219 | close(fd); |
| 220 | |
| 221 | if (rename(tmp_path, final_path) < 0) { |
| 222 | ret = -errno; |
| 223 | unlink(tmp_path); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | fsync(dir_fd); |
| 229 | return 0; |
| 230 | |
| 231 | err: |
| 232 | close(fd); |
| 233 | unlink(tmp_path); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | static char *read_uevent_seqnum(void) |
| 247 | { |
| 248 | char buf[32]; |
| 249 | ssize_t n; |
| 250 | int fd; |
| 251 | |
| 252 | fd = open("/sys/kernel/uevent_seqnum", O_RDONLY | O_CLOEXEC); |
| 253 | if (fd < 0) |
| |
| 254 | return NULL; |
| 255 | n = read_full(fd, buf, sizeof(buf) - 1); |
| 256 | close(fd); |
| 257 | if (n <= 0) |
| |
| 258 | return NULL; |
| 259 | |
| 260 | |
| 261 | while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) |
| 25 | | Assuming the condition is false | |
|
| 26 | | Loop condition is false. Execution continues on line 263 | |
|
| 262 | n--; |
| 263 | if (n == 0) |
| |
| 264 | return NULL; |
| 265 | buf[n] = '\0'; |
| 266 | |
| 267 | return strdup(buf); |
| |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | static int delete_dir(const char *path) |
| 276 | { |
| 277 | struct dirent *de; |
| 278 | int dir_fd, ret = 0; |
| 279 | DIR *d; |
| 280 | |
| 281 | d = opendir(path); |
| 282 | if (!d) |
| 283 | return -errno; |
| 284 | |
| 285 | dir_fd = dirfd(d); |
| 286 | if (dir_fd < 0) { |
| 287 | ret = -errno; |
| 288 | closedir(d); |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | while ((de = readdir(d)) != NULL) { |
| 293 | if (de->d_name[0] == '.') |
| 294 | continue; |
| 295 | if (unlinkat(dir_fd, de->d_name, 0) < 0 && errno != ENOENT) |
| 296 | ret = -errno; |
| 297 | } |
| 298 | closedir(d); |
| 299 | |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | |
| 303 | if (rmdir(path) < 0 && errno != ENOENT) |
| 304 | return -errno; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | static int delete_dir_atomic(struct libnvme_global_ctx *ctx, const char *path) |
| 319 | { |
| 320 | __cleanup_free char *trash = NULL; |
| 321 | |
| 322 | if (asprintf(&trash, "%s/.trash.XXXXXX", registry_dir(ctx)) < 0) |
| 323 | return -ENOMEM; |
| 324 | if (!mkdtemp(trash)) |
| 325 | return -errno; |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | if (rename(path, trash) < 0) { |
| 334 | int ret = -errno; |
| 335 | |
| 336 | rmdir(trash); |
| 337 | return ret; |
| 338 | } |
| 339 | return delete_dir(trash); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | static int create_entry(struct libnvme_global_ctx *ctx, const char *device, |
| 356 | const char *attr, const char *value, bool replace) |
| 357 | { |
| 358 | __cleanup_free char *tmp_dir = NULL; |
| 359 | __cleanup_free char *final_path = NULL; |
| 360 | __cleanup_free char *seqnum = NULL; |
| 361 | int dir_fd, ret; |
| 362 | |
| 363 | final_path = registry_path(ctx, device, NULL); |
| 364 | if (!final_path) |
| 13 | | Assuming 'final_path' is non-null | |
|
| |
| 365 | return -ENOMEM; |
| 366 | |
| 367 | |
| 368 | ret = shr_mkdir_p(registry_dir(ctx), 0755); |
| 369 | if (ret) |
| |
| |
| 370 | return ret; |
| 371 | |
| 372 | if (asprintf(&tmp_dir, "%s/.%s.tmp.XXXXXX", |
| 17 | | Assuming the condition is false | |
|
| |
| 373 | registry_dir(ctx), device) < 0) |
| 374 | return -ENOMEM; |
| 375 | if (!mkdtemp(tmp_dir)) |
| |
| 376 | return -errno; |
| 377 | |
| 378 | |
| 379 | if (chmod(tmp_dir, 0755) < 0) { |
| |
| 380 | ret = -errno; |
| 381 | goto err; |
| 382 | } |
| 383 | |
| 384 | dir_fd = open(tmp_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 385 | if (dir_fd < 0) { |
| |
| 386 | ret = -errno; |
| 387 | goto err; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | seqnum = read_uevent_seqnum(); |
| 22 | | Calling 'read_uevent_seqnum' | |
|
| 29 | | Returned allocated memory | |
|
| 397 | if (seqnum) |
| 30 | | Assuming 'seqnum' is non-null | |
|
| |
| 398 | write_attr_atomic(dir_fd, tmp_dir, "seqnum", seqnum); |
| 399 | |
| 400 | ret = write_attr_atomic(dir_fd, tmp_dir, attr, value); |
| 32 | | Potential leak of memory pointed to by 'seqnum' |
|
| 401 | close(dir_fd); |
| 402 | if (ret) |
| 403 | goto err; |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | if (replace) |
| 410 | delete_dir_atomic(ctx, final_path); |
| 411 | if (rename(tmp_dir, final_path) < 0) { |
| 412 | ret = -errno; |
| 413 | if (ret == -ENOTEMPTY) |
| 414 | ret = -EEXIST; |
| 415 | goto err; |
| 416 | } |
| 417 | return 0; |
| 418 | |
| 419 | err: |
| 420 | delete_dir(tmp_dir); |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | int libnvmf_registry_create_instance(struct libnvme_global_ctx *ctx, |
| 432 | int instance, const char *owner) |
| 433 | { |
| 434 | char device[32]; |
| 435 | |
| 436 | snprintf(device, sizeof(device), "nvme%d", instance); |
| 437 | |
| 438 | return create_entry(ctx, device, "owner", owner, true); |
| 439 | } |
| 440 | |
| 441 | int libnvmf_registry_delete_instance(struct libnvme_global_ctx *ctx, |
| 442 | int instance) |
| 443 | { |
| 444 | char device[32]; |
| 445 | int ret; |
| 446 | |
| 447 | snprintf(device, sizeof(device), "nvme%d", instance); |
| 448 | ret = libnvmf_registry_delete(ctx, device); |
| 449 | return (ret == -ENOENT) ? 0 : ret; |
| 450 | } |
| 451 | |
| 452 | __shr_public int libnvmf_registry_retrieve(struct libnvme_global_ctx *ctx, |
| 453 | const char *device, |
| 454 | const char *attr, char **value) |
| 455 | { |
| 456 | __cleanup_free char *path = NULL; |
| 457 | int fd, ret; |
| 458 | |
| 459 | if (!ctx) |
| 460 | return -EINVAL; |
| 461 | if (!device || !attr || !value) |
| 462 | return -EINVAL; |
| 463 | if (!valid_device(device)) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | path = registry_path(ctx, device, attr); |
| 467 | if (!path) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | fd = open(path, O_RDONLY | O_CLOEXEC); |
| 471 | if (fd < 0) |
| 472 | return -errno; |
| 473 | |
| 474 | ret = read_attr_fd(fd, value); |
| 475 | close(fd); |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | __shr_public int libnvmf_registry_attr_equal(struct libnvme_global_ctx *ctx, |
| 480 | const char *device, |
| 481 | const char *attr, |
| 482 | const char *value) |
| 483 | { |
| 484 | char *stored = NULL; |
| 485 | int rc; |
| 486 | |
| 487 | if (!ctx) |
| 488 | return -EINVAL; |
| 489 | |
| 490 | rc = libnvmf_registry_retrieve(ctx, device, attr, &stored); |
| 491 | if (rc < 0 && rc != -ENOENT) |
| 492 | return rc; |
| 493 | if (!stored) |
| 494 | rc = value ? 1 : 0; |
| 495 | else |
| 496 | rc = (value && !strcmp(stored, value)) ? 0 : 1; |
| 497 | free(stored); |
| 498 | return rc; |
| 499 | } |
| 500 | |
| 501 | __shr_public int libnvmf_registry_update(struct libnvme_global_ctx *ctx, |
| 502 | const char *device, |
| 503 | const char *attr, const char *value) |
| 504 | { |
| 505 | __cleanup_free char *dir_path = NULL; |
| 506 | int dir_fd, ret; |
| 507 | |
| 508 | if (!ctx) |
| 1 | Assuming 'ctx' is non-null | |
|
| 509 | return -EINVAL; |
| 510 | if (!device || !valid_device(device)) |
| 2 | | Assuming 'device' is non-null | |
|
| 3 | | Assuming the condition is false | |
|
| |
| 511 | return -EINVAL; |
| 512 | if (!valid_attr(attr)) |
| |
| 513 | return -EINVAL; |
| 514 | |
| 515 | dir_path = registry_path(ctx, device, NULL); |
| 516 | if (!dir_path) |
| 6 | | Assuming 'dir_path' is non-null | |
|
| |
| 517 | return -ENOMEM; |
| 518 | |
| 519 | dir_fd = open(dir_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 520 | if (dir_fd < 0 && errno == ENOENT) { |
| 8 | | Assuming the condition is true | |
|
| |
| 521 | |
| 522 | if (!value) |
| 10 | | Assuming 'value' is non-null | |
|
| |
| 523 | return 0; |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | ret = create_entry(ctx, device, attr, value, false); |
| |
| 530 | if (ret != -EEXIST) |
| 531 | return ret; |
| 532 | |
| 533 | |
| 534 | dir_fd = open(dir_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 535 | } |
| 536 | if (dir_fd < 0) |
| 537 | return -errno; |
| 538 | |
| 539 | ret = write_attr_atomic(dir_fd, dir_path, attr, value); |
| 540 | close(dir_fd); |
| 541 | return ret; |
| 542 | } |
| 543 | |
| 544 | __shr_public int libnvmf_registry_delete(struct libnvme_global_ctx *ctx, |
| 545 | const char *device) |
| 546 | { |
| 547 | __cleanup_free char *path = NULL; |
| 548 | |
| 549 | if (!ctx) |
| 550 | return -EINVAL; |
| 551 | if (!device || !valid_device(device)) |
| 552 | return -EINVAL; |
| 553 | |
| 554 | path = registry_path(ctx, device, NULL); |
| 555 | if (!path) |
| 556 | return -ENOMEM; |
| 557 | |
| 558 | return delete_dir_atomic(ctx, path); |
| 559 | } |
| 560 | |
| 561 | __shr_public int libnvmf_registry_device_for_each( |
| 562 | struct libnvme_global_ctx *ctx, |
| 563 | int (*callback)(const char *device, void *user_data), |
| 564 | void *user_data) |
| 565 | { |
| 566 | char dev_path[NAME_MAX + 6]; |
| 567 | struct dirent *de; |
| 568 | int dir_fd; |
| 569 | DIR *d; |
| 570 | int ret = 0; |
| 571 | |
| 572 | if (!ctx) |
| 573 | return -EINVAL; |
| 574 | if (!callback) |
| 575 | return -EINVAL; |
| 576 | |
| 577 | d = opendir(registry_dir(ctx)); |
| 578 | if (!d) { |
| 579 | if (errno == ENOENT) |
| 580 | return 0; |
| 581 | return -errno; |
| 582 | } |
| 583 | |
| 584 | dir_fd = dirfd(d); |
| 585 | if (dir_fd < 0) { |
| 586 | ret = -errno; |
| 587 | closedir(d); |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | while ((de = readdir(d)) != NULL) { |
| 592 | if (de->d_name[0] == '.') |
| 593 | continue; |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | if (de->d_type != DT_DIR) { |
| 600 | struct stat st; |
| 601 | |
| 602 | if (de->d_type != DT_UNKNOWN) |
| 603 | continue; |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | if (fstatat(dir_fd, de->d_name, &st, 0) < 0 || |
| 610 | !S_ISDIR(st.st_mode)) |
| 611 | continue; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | snprintf(dev_path, sizeof(dev_path), "/dev/%s", de->d_name); |
| 616 | if (access(dev_path, F_OK) != 0) |
| 617 | continue; |
| 618 | |
| 619 | ret = callback(de->d_name, user_data); |
| 620 | if (ret) |
| 621 | break; |
| 622 | } |
| 623 | closedir(d); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | __shr_public int libnvmf_registry_attr_for_each( |
| 628 | struct libnvme_global_ctx *ctx, |
| 629 | const char *device, |
| 630 | void (*callback)(const char *attr, const char *value, |
| 631 | void *user_data), |
| 632 | void *user_data) |
| 633 | { |
| 634 | __cleanup_free char *dir_path = NULL; |
| 635 | struct dirent *de; |
| 636 | int dir_fd; |
| 637 | DIR *d; |
| 638 | |
| 639 | if (!ctx) |
| 640 | return -EINVAL; |
| 641 | if (!device || !callback) |
| 642 | return -EINVAL; |
| 643 | if (!valid_device(device)) |
| 644 | return -EINVAL; |
| 645 | |
| 646 | dir_path = registry_path(ctx, device, NULL); |
| 647 | if (!dir_path) |
| 648 | return -ENOMEM; |
| 649 | |
| 650 | d = opendir(dir_path); |
| 651 | if (!d) |
| 652 | return -errno; |
| 653 | |
| 654 | dir_fd = dirfd(d); |
| 655 | if (dir_fd < 0) { |
| 656 | int ret = -errno; |
| 657 | |
| 658 | closedir(d); |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | while ((de = readdir(d)) != NULL) { |
| 663 | __cleanup_free char *val = NULL; |
| 664 | int fd, rc; |
| 665 | |
| 666 | if (de->d_name[0] == '.') |
| 667 | continue; |
| 668 | |
| 669 | if (strstr(de->d_name, ".tmp.")) |
| 670 | continue; |
| 671 | |
| 672 | fd = openat(dir_fd, de->d_name, O_RDONLY | O_CLOEXEC); |
| 673 | if (fd < 0) |
| 674 | continue; |
| 675 | |
| 676 | rc = read_attr_fd(fd, &val); |
| 677 | close(fd); |
| 678 | if (rc == 0) |
| 679 | callback(de->d_name, val, user_data); |
| 680 | } |
| 681 | closedir(d); |
| 682 | return 0; |
| 683 | } |