]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/sysfs.c
9e973a4f76ee252d12def747f5b93bfe4974d042
[thirdparty/util-linux.git] / lib / sysfs.c
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7 #include <ctype.h>
8 #include <libgen.h>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include "c.h"
14 #include "pathnames.h"
15 #include "sysfs.h"
16 #include "fileutils.h"
17 #include "all-io.h"
18
19 char *sysfs_devno_attribute_path(dev_t devno, char *buf,
20 size_t bufsiz, const char *attr)
21 {
22 int len;
23
24 if (attr)
25 len = snprintf(buf, bufsiz, _PATH_SYS_DEVBLOCK "/%d:%d/%s",
26 major(devno), minor(devno), attr);
27 else
28 len = snprintf(buf, bufsiz, _PATH_SYS_DEVBLOCK "/%d:%d",
29 major(devno), minor(devno));
30
31 return (len < 0 || (size_t) len >= bufsiz) ? NULL : buf;
32 }
33
34 int sysfs_devno_has_attribute(dev_t devno, const char *attr)
35 {
36 char path[PATH_MAX];
37 struct stat info;
38
39 if (!sysfs_devno_attribute_path(devno, path, sizeof(path), attr))
40 return 0;
41 if (stat(path, &info) == 0)
42 return 1;
43 return 0;
44 }
45
46 char *sysfs_devno_path(dev_t devno, char *buf, size_t bufsiz)
47 {
48 return sysfs_devno_attribute_path(devno, buf, bufsiz, NULL);
49 }
50
51 dev_t sysfs_devname_to_devno(const char *name, const char *parent)
52 {
53 char buf[PATH_MAX], *path = NULL;
54 dev_t dev = 0;
55
56 if (strncmp("/dev/", name, 5) == 0) {
57 /*
58 * Read from /dev
59 */
60 struct stat st;
61
62 if (stat(name, &st) == 0)
63 dev = st.st_rdev;
64 else
65 name += 5; /* unaccesible, or not node in /dev */
66 }
67
68 if (!dev && parent && strncmp("dm-", name, 3)) {
69 /*
70 * Create path to /sys/block/<parent>/<name>/dev
71 */
72 char *_name = strdup(name), *_parent = strdup(parent);
73 int len;
74
75 if (!_name || !_parent) {
76 free(_name);
77 free(_parent);
78 return 0;
79 }
80 sysfs_devname_dev_to_sys(_name);
81 sysfs_devname_dev_to_sys(_parent);
82
83 len = snprintf(buf, sizeof(buf),
84 _PATH_SYS_BLOCK "/%s/%s/dev", _parent, _name);
85 free(_name);
86 free(_parent);
87 if (len < 0 || (size_t) len >= sizeof(buf))
88 return 0;
89 path = buf;
90
91 } else if (!dev) {
92 /*
93 * Create path to /sys/block/<sysname>/dev
94 */
95 char *_name = strdup(name);
96 int len;
97
98 if (!_name)
99 return 0;
100
101 sysfs_devname_dev_to_sys(_name);
102 len = snprintf(buf, sizeof(buf),
103 _PATH_SYS_BLOCK "/%s/dev", _name);
104 free(_name);
105 if (len < 0 || (size_t) len >= sizeof(buf))
106 return 0;
107 path = buf;
108 }
109
110 if (path) {
111 /*
112 * read devno from sysfs
113 */
114 FILE *f;
115 int maj = 0, min = 0;
116
117 f = fopen(path, "r" UL_CLOEXECSTR);
118 if (!f)
119 return 0;
120
121 if (fscanf(f, "%d:%d", &maj, &min) == 2)
122 dev = makedev(maj, min);
123 fclose(f);
124 }
125 return dev;
126 }
127
128 /*
129 * Returns devname (e.g. "/dev/sda1") for the given devno.
130 *
131 * Please, use more robust blkid_devno_to_devname() in your applications.
132 */
133 char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz)
134 {
135 struct sysfs_cxt cxt;
136 char *name;
137 size_t sz;
138 struct stat st;
139
140 if (sysfs_init(&cxt, devno, NULL))
141 return NULL;
142
143 name = sysfs_get_devname(&cxt, buf, bufsiz);
144 sysfs_deinit(&cxt);
145
146 if (!name)
147 return NULL;
148
149 sz = strlen(name);
150
151 if (sz + sizeof("/dev/") > bufsiz)
152 return NULL;
153
154 /* create the final "/dev/<name>" string */
155 memmove(buf + 5, name, sz + 1);
156 memcpy(buf, "/dev/", 5);
157
158 if (!stat(buf, &st) && S_ISBLK(st.st_mode) && st.st_rdev == devno)
159 return buf;
160
161 return NULL;
162 }
163
164 int sysfs_init(struct sysfs_cxt *cxt, dev_t devno, struct sysfs_cxt *parent)
165 {
166 char path[PATH_MAX];
167 int fd, rc;
168
169 memset(cxt, 0, sizeof(*cxt));
170 cxt->dir_fd = -1;
171
172 if (!sysfs_devno_path(devno, path, sizeof(path)))
173 goto err;
174
175 fd = open(path, O_RDONLY|O_CLOEXEC);
176 if (fd < 0)
177 goto err;
178 cxt->dir_fd = fd;
179
180 cxt->dir_path = strdup(path);
181 if (!cxt->dir_path)
182 goto err;
183 cxt->devno = devno;
184 cxt->parent = parent;
185 return 0;
186 err:
187 rc = errno > 0 ? -errno : -1;
188 sysfs_deinit(cxt);
189 return rc;
190 }
191
192 void sysfs_deinit(struct sysfs_cxt *cxt)
193 {
194 if (!cxt)
195 return;
196
197 if (cxt->dir_fd >= 0)
198 close(cxt->dir_fd);
199 free(cxt->dir_path);
200
201 memset(cxt, 0, sizeof(*cxt));
202
203 cxt->dir_fd = -1;
204 }
205
206 int sysfs_stat(struct sysfs_cxt *cxt, const char *attr, struct stat *st)
207 {
208 int rc = fstatat(cxt->dir_fd, attr, st, 0);
209
210 if (rc != 0 && errno == ENOENT &&
211 strncmp(attr, "queue/", 6) == 0 && cxt->parent) {
212
213 /* Exception for "queue/<attr>". These attributes are available
214 * for parental devices only
215 */
216 return fstatat(cxt->parent->dir_fd, attr, st, 0);
217 }
218 return rc;
219 }
220
221 int sysfs_has_attribute(struct sysfs_cxt *cxt, const char *attr)
222 {
223 struct stat st;
224
225 return sysfs_stat(cxt, attr, &st) == 0;
226 }
227
228 static int sysfs_open(struct sysfs_cxt *cxt, const char *attr, int flags)
229 {
230 int fd = openat(cxt->dir_fd, attr, flags);
231
232 if (fd == -1 && errno == ENOENT &&
233 strncmp(attr, "queue/", 6) == 0 && cxt->parent) {
234
235 /* Exception for "queue/<attr>". These attributes are available
236 * for parental devices only
237 */
238 fd = openat(cxt->parent->dir_fd, attr, flags);
239 }
240 return fd;
241 }
242
243 ssize_t sysfs_readlink(struct sysfs_cxt *cxt, const char *attr,
244 char *buf, size_t bufsiz)
245 {
246 if (!cxt->dir_path)
247 return -1;
248
249 if (attr)
250 return readlinkat(cxt->dir_fd, attr, buf, bufsiz);
251
252 /* read /sys/dev/block/<maj:min> link */
253 return readlink(cxt->dir_path, buf, bufsiz);
254 }
255
256 DIR *sysfs_opendir(struct sysfs_cxt *cxt, const char *attr)
257 {
258 DIR *dir;
259 int fd = -1;
260
261 if (attr)
262 fd = sysfs_open(cxt, attr, O_RDONLY|O_CLOEXEC);
263
264 else if (cxt->dir_fd >= 0)
265 /* request to open root of device in sysfs (/sys/block/<dev>)
266 * -- we cannot use cxt->sysfs_fd directly, because closedir()
267 * will close this our persistent file descriptor.
268 */
269 fd = dup_fd_cloexec(cxt->dir_fd, STDERR_FILENO + 1);
270
271 if (fd < 0)
272 return NULL;
273
274 dir = fdopendir(fd);
275 if (!dir) {
276 close(fd);
277 return NULL;
278 }
279 if (!attr)
280 rewinddir(dir);
281 return dir;
282 }
283
284
285 static FILE *sysfs_fopen(struct sysfs_cxt *cxt, const char *attr)
286 {
287 int fd = sysfs_open(cxt, attr, O_RDONLY|O_CLOEXEC);
288
289 return fd < 0 ? NULL : fdopen(fd, "r" UL_CLOEXECSTR);
290 }
291
292
293 static struct dirent *xreaddir(DIR *dp)
294 {
295 struct dirent *d;
296
297 while ((d = readdir(dp))) {
298 if (!strcmp(d->d_name, ".") ||
299 !strcmp(d->d_name, ".."))
300 continue;
301
302 /* blacklist here? */
303 break;
304 }
305 return d;
306 }
307
308 int sysfs_is_partition_dirent(DIR *dir, struct dirent *d, const char *parent_name)
309 {
310 char path[256];
311
312 #ifdef _DIRENT_HAVE_D_TYPE
313 if (d->d_type != DT_DIR &&
314 d->d_type != DT_LNK &&
315 d->d_type != DT_UNKNOWN)
316 return 0;
317 #endif
318 if (parent_name) {
319 const char *p = parent_name;
320 size_t len;
321
322 /* /dev/sda --> "sda" */
323 if (*parent_name == '/') {
324 p = strrchr(parent_name, '/');
325 if (!p)
326 return 0;
327 p++;
328 }
329
330 len = strlen(p);
331 if (strlen(d->d_name) <= len)
332 return 0;
333
334 /* partitions subdir name is
335 * "<parent>[:digit:]" or "<parent>p[:digit:]"
336 */
337 return strncmp(p, d->d_name, len) == 0 &&
338 ((*(d->d_name + len) == 'p' && isdigit(*(d->d_name + len + 1)))
339 || isdigit(*(d->d_name + len)));
340 }
341
342 /* Cannot use /partition file, not supported on old sysfs */
343 snprintf(path, sizeof(path), "%s/start", d->d_name);
344
345 return faccessat(dirfd(dir), path, R_OK, 0) == 0;
346 }
347
348 /*
349 * Converts @partno (partition number) to devno of the partition.
350 * The @cxt handles wholedisk device.
351 *
352 * Note that this code does not expect any special format of the
353 * partitions devnames.
354 */
355 dev_t sysfs_partno_to_devno(struct sysfs_cxt *cxt, int partno)
356 {
357 DIR *dir;
358 struct dirent *d;
359 char path[256];
360 dev_t devno = 0;
361
362 dir = sysfs_opendir(cxt, NULL);
363 if (!dir)
364 return 0;
365
366 while ((d = xreaddir(dir))) {
367 int n, maj, min;
368
369 if (!sysfs_is_partition_dirent(dir, d, NULL))
370 continue;
371
372 snprintf(path, sizeof(path), "%s/partition", d->d_name);
373 if (sysfs_read_int(cxt, path, &n))
374 continue;
375
376 if (n == partno) {
377 snprintf(path, sizeof(path), "%s/dev", d->d_name);
378 if (sysfs_scanf(cxt, path, "%d:%d", &maj, &min) == 2)
379 devno = makedev(maj, min);
380 break;
381 }
382 }
383
384 closedir(dir);
385 return devno;
386 }
387
388
389 int sysfs_scanf(struct sysfs_cxt *cxt, const char *attr, const char *fmt, ...)
390 {
391 FILE *f = sysfs_fopen(cxt, attr);
392 va_list ap;
393 int rc;
394
395 if (!f)
396 return -EINVAL;
397 va_start(ap, fmt);
398 rc = vfscanf(f, fmt, ap);
399 va_end(ap);
400
401 fclose(f);
402 return rc;
403 }
404
405
406 int sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr, int64_t *res)
407 {
408 int64_t x = 0;
409
410 if (sysfs_scanf(cxt, attr, "%"SCNd64, &x) == 1) {
411 if (res)
412 *res = x;
413 return 0;
414 }
415 return -1;
416 }
417
418 int sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t *res)
419 {
420 uint64_t x = 0;
421
422 if (sysfs_scanf(cxt, attr, "%"SCNu64, &x) == 1) {
423 if (res)
424 *res = x;
425 return 0;
426 }
427 return -1;
428 }
429
430 int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr, int *res)
431 {
432 int x = 0;
433
434 if (sysfs_scanf(cxt, attr, "%d", &x) == 1) {
435 if (res)
436 *res = x;
437 return 0;
438 }
439 return -1;
440 }
441
442 int sysfs_write_string(struct sysfs_cxt *cxt, const char *attr, const char *str)
443 {
444 int fd = sysfs_open(cxt, attr, O_WRONLY|O_CLOEXEC);
445 int rc, errsv;
446
447 if (fd < 0)
448 return -errno;
449 rc = write_all(fd, str, strlen(str));
450
451 errsv = errno;
452 close(fd);
453 errno = errsv;
454 return rc;
455 }
456
457 int sysfs_write_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t num)
458 {
459 char buf[sizeof(stringify_value(ULLONG_MAX))];
460 int fd, rc = 0, len, errsv;
461
462 fd = sysfs_open(cxt, attr, O_WRONLY|O_CLOEXEC);
463 if (fd < 0)
464 return -errno;
465
466 len = snprintf(buf, sizeof(buf), "%" PRIu64, num);
467 if (len < 0 || (size_t) len >= sizeof(buf))
468 rc = len < 0 ? -errno : -E2BIG;
469 else
470 rc = write_all(fd, buf, len);
471
472 errsv = errno;
473 close(fd);
474 errno = errsv;
475 return rc;
476 }
477
478 char *sysfs_strdup(struct sysfs_cxt *cxt, const char *attr)
479 {
480 char buf[BUFSIZ];
481 return sysfs_scanf(cxt, attr, "%1023[^\n]", buf) == 1 ?
482 strdup(buf) : NULL;
483 }
484
485
486 int sysfs_count_dirents(struct sysfs_cxt *cxt, const char *attr)
487 {
488 DIR *dir;
489 int r = 0;
490
491 if (!(dir = sysfs_opendir(cxt, attr)))
492 return 0;
493
494 while (xreaddir(dir)) r++;
495
496 closedir(dir);
497 return r;
498 }
499
500 int sysfs_count_partitions(struct sysfs_cxt *cxt, const char *devname)
501 {
502 DIR *dir;
503 struct dirent *d;
504 int r = 0;
505
506 if (!(dir = sysfs_opendir(cxt, NULL)))
507 return 0;
508
509 while ((d = xreaddir(dir))) {
510 if (sysfs_is_partition_dirent(dir, d, devname))
511 r++;
512 }
513
514 closedir(dir);
515 return r;
516 }
517
518 /*
519 * Returns slave name if there is only one slave, otherwise returns NULL.
520 * The result should be deallocated by free().
521 */
522 char *sysfs_get_slave(struct sysfs_cxt *cxt)
523 {
524 DIR *dir;
525 struct dirent *d;
526 char *name = NULL;
527
528 if (!(dir = sysfs_opendir(cxt, "slaves")))
529 return NULL;
530
531 while ((d = xreaddir(dir))) {
532 if (name)
533 goto err; /* more slaves */
534
535 name = strdup(d->d_name);
536 }
537
538 closedir(dir);
539 return name;
540 err:
541 free(name);
542 closedir(dir);
543 return NULL;
544 }
545
546 char *sysfs_get_devname(struct sysfs_cxt *cxt, char *buf, size_t bufsiz)
547 {
548 char linkpath[PATH_MAX];
549 char *name;
550 ssize_t sz;
551
552 sz = sysfs_readlink(cxt, NULL, linkpath, sizeof(linkpath) - 1);
553 if (sz < 0)
554 return NULL;
555 linkpath[sz] = '\0';
556
557 name = strrchr(linkpath, '/');
558 if (!name)
559 return NULL;
560
561 name++;
562 sz = strlen(name);
563
564 if ((size_t) sz + 1 > bufsiz)
565 return NULL;
566
567 memcpy(buf, name, sz + 1);
568 sysfs_devname_sys_to_dev(buf);
569
570 return buf;
571 }
572
573 #define SUBSYSTEM_LINKNAME "/subsystem"
574
575 /*
576 * For example:
577 *
578 * chain: /sys/dev/block/../../devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/ \
579 * 1-1.2:1.0/host65/target65:0:0/65:0:0:0/block/sdb
580 *
581 * The function check if <chain>/subsystem symlink exists, if yes then returns
582 * basename of the readlink result, and remove the last subdirectory from the
583 * <chain> path.
584 */
585 static char *get_subsystem(char *chain, char *buf, size_t bufsz)
586 {
587 size_t len;
588 char *p;
589
590 if (!chain || !*chain)
591 return NULL;
592
593 len = strlen(chain);
594 if (len + sizeof(SUBSYSTEM_LINKNAME) > PATH_MAX)
595 return NULL;
596
597 do {
598 ssize_t sz;
599
600 /* append "/subsystem" to the path */
601 memcpy(chain + len, SUBSYSTEM_LINKNAME, sizeof(SUBSYSTEM_LINKNAME));
602
603 /* try if subsystem symlink exists */
604 sz = readlink(chain, buf, bufsz - 1);
605
606 /* remove last subsystem from chain */
607 chain[len] = '\0';
608 p = strrchr(chain, '/');
609 if (p) {
610 *p = '\0';
611 len = p - chain;
612 }
613
614 if (sz > 0) {
615 /* we found symlink to subsystem, return basename */
616 buf[sz] = '\0';
617 return basename(buf);
618 }
619
620 } while (p);
621
622 return NULL;
623 }
624
625 /*
626 * Returns complete path to the device, the patch contains all all sybsystems
627 * used for the device.
628 */
629 char *sysfs_get_devchain(struct sysfs_cxt *cxt, char *buf, size_t bufsz)
630 {
631 /* read /sys/dev/block/<maj>:<min> symlink */
632 ssize_t sz = sysfs_readlink(cxt, NULL, buf, bufsz);
633 if (sz <= 0 || sz + sizeof(_PATH_SYS_DEVBLOCK "/") > bufsz)
634 return NULL;
635
636 buf[sz++] = '\0';
637
638 /* create absolute patch from the link */
639 memmove(buf + sizeof(_PATH_SYS_DEVBLOCK "/") - 1, buf, sz);
640 memcpy(buf, _PATH_SYS_DEVBLOCK "/", sizeof(_PATH_SYS_DEVBLOCK "/") - 1);
641
642 return buf;
643 }
644
645 /*
646 * The @subsys returns the next subsystem in the chain. Function modifies
647 * @devchain string.
648 *
649 * Returns: 0 in success, <0 on error, 1 on end of chain
650 */
651 int sysfs_next_subsystem(struct sysfs_cxt *cxt __attribute__((unused)),
652 char *devchain, char **subsys)
653 {
654 char subbuf[PATH_MAX];
655 char *sub;
656
657 if (!subsys || !devchain)
658 return -EINVAL;
659
660 *subsys = NULL;
661
662 while ((sub = get_subsystem(devchain, subbuf, sizeof(subbuf)))) {
663 *subsys = strdup(sub);
664 if (!*subsys)
665 return -ENOMEM;
666 return 0;
667 }
668
669 return 1;
670 }
671
672
673 static int is_hotpluggable_subsystem(const char *name)
674 {
675 static const char * const hotplug_subsystems[] = {
676 "usb",
677 "ieee1394",
678 "pcmcia",
679 "mmc",
680 "ccw"
681 };
682 size_t i;
683
684 for (i = 0; i < ARRAY_SIZE(hotplug_subsystems); i++)
685 if (strcmp(name, hotplug_subsystems[i]) == 0)
686 return 1;
687
688 return 0;
689 }
690
691 int sysfs_is_hotpluggable(struct sysfs_cxt *cxt)
692 {
693 char buf[PATH_MAX], *chain, *sub;
694 int rc = 0;
695
696
697 /* check /sys/dev/block/<maj>:<min>/removable attribute */
698 if (sysfs_read_int(cxt, "removable", &rc) == 0 && rc == 1)
699 return 1;
700
701 chain = sysfs_get_devchain(cxt, buf, sizeof(buf));
702
703 while (chain && sysfs_next_subsystem(cxt, chain, &sub) == 0) {
704 rc = is_hotpluggable_subsystem(sub);
705 if (rc) {
706 free(sub);
707 break;
708 }
709 free(sub);
710 }
711
712 return rc;
713 }
714
715 static int get_dm_wholedisk(struct sysfs_cxt *cxt, char *diskname,
716 size_t len, dev_t *diskdevno)
717 {
718 int rc = 0;
719 char *name;
720
721 /* Note, sysfs_get_slave() returns the first slave only,
722 * if there is more slaves, then return NULL
723 */
724 name = sysfs_get_slave(cxt);
725 if (!name)
726 return -1;
727
728 if (diskname && len) {
729 strncpy(diskname, name, len);
730 diskname[len - 1] = '\0';
731 }
732
733 if (diskdevno) {
734 *diskdevno = sysfs_devname_to_devno(name, NULL);
735 if (!*diskdevno)
736 rc = -1;
737 }
738
739 free(name);
740 return rc;
741 }
742
743 /*
744 * Returns by @diskdevno whole disk device devno and (optionaly) by
745 * @diskname the whole disk device name.
746 */
747 int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
748 size_t len, dev_t *diskdevno)
749 {
750 struct sysfs_cxt cxt;
751 int is_part = 0;
752
753 if (!dev || sysfs_init(&cxt, dev, NULL) != 0)
754 return -1;
755
756 is_part = sysfs_has_attribute(&cxt, "partition");
757 if (!is_part) {
758 /*
759 * Extra case for partitions mapped by device-mapper.
760 *
761 * All regualar partitions (added by BLKPG ioctl or kernel PT
762 * parser) have the /sys/.../partition file. The partitions
763 * mapped by DM don't have such file, but they have "part"
764 * prefix in DM UUID.
765 */
766 char *uuid = sysfs_strdup(&cxt, "dm/uuid");
767 char *tmp = uuid;
768 char *prefix = uuid ? strsep(&tmp, "-") : NULL;
769
770 if (prefix && strncasecmp(prefix, "part", 4) == 0)
771 is_part = 1;
772 free(uuid);
773
774 if (is_part &&
775 get_dm_wholedisk(&cxt, diskname, len, diskdevno) == 0)
776 /*
777 * partitioned device, mapped by DM
778 */
779 goto done;
780
781 is_part = 0;
782 }
783
784 if (!is_part) {
785 /*
786 * unpartitioned device
787 */
788 if (diskname && len) {
789 if (!sysfs_get_devname(&cxt, diskname, len))
790 goto err;
791 }
792 if (diskdevno)
793 *diskdevno = dev;
794
795 } else {
796 /*
797 * partitioned device
798 * - readlink /sys/dev/block/8:1 = ../../block/sda/sda1
799 * - dirname ../../block/sda/sda1 = ../../block/sda
800 * - basename ../../block/sda = sda
801 */
802 char linkpath[PATH_MAX];
803 char *name;
804 ssize_t linklen;
805
806 linklen = sysfs_readlink(&cxt, NULL, linkpath, sizeof(linkpath) - 1);
807 if (linklen < 0)
808 goto err;
809 linkpath[linklen] = '\0';
810
811 stripoff_last_component(linkpath); /* dirname */
812 name = stripoff_last_component(linkpath); /* basename */
813 if (!name)
814 goto err;
815
816 sysfs_devname_sys_to_dev(name);
817 if (diskname && len) {
818 strncpy(diskname, name, len);
819 diskname[len - 1] = '\0';
820 }
821
822 if (diskdevno) {
823 *diskdevno = sysfs_devname_to_devno(name, NULL);
824 if (!*diskdevno)
825 goto err;
826 }
827 }
828
829 done:
830 sysfs_deinit(&cxt);
831 return 0;
832 err:
833 sysfs_deinit(&cxt);
834 return -1;
835 }
836
837 /*
838 * Returns 1 if the device is private LVM device.
839 */
840 int sysfs_devno_is_lvm_private(dev_t devno)
841 {
842 struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
843 char *uuid = NULL;
844 int rc = 0;
845
846 if (sysfs_init(&cxt, devno, NULL) != 0)
847 return 0;
848
849 uuid = sysfs_strdup(&cxt, "dm/uuid");
850
851 /* Private LVM devices use "LVM-<uuid>-<name>" uuid format (important
852 * is the "LVM" prefix and "-<name>" postfix).
853 */
854 if (uuid && strncmp(uuid, "LVM-", 4) == 0) {
855 char *p = strrchr(uuid + 4, '-');
856
857 if (p && *(p + 1))
858 rc = 1;
859 }
860
861 sysfs_deinit(&cxt);
862 free(uuid);
863 return rc;
864 }
865
866 /*
867 * Return 0 or 1, or < 0 in case of error
868 */
869 int sysfs_devno_is_wholedisk(dev_t devno)
870 {
871 dev_t disk;
872
873 if (sysfs_devno_to_wholedisk(devno, NULL, 0, &disk) != 0)
874 return -1;
875
876 return devno == disk;
877 }
878
879
880 int sysfs_scsi_get_hctl(struct sysfs_cxt *cxt, int *h, int *c, int *t, int *l)
881 {
882 char buf[PATH_MAX], *hctl;
883 ssize_t len;
884
885 if (!cxt)
886 return -EINVAL;
887 if (cxt->has_hctl)
888 goto done;
889
890 len = sysfs_readlink(cxt, "device", buf, sizeof(buf) - 1);
891 if (len < 0)
892 return len;
893
894 buf[len] = '\0';
895 hctl = strrchr(buf, '/');
896 if (!hctl)
897 return -1;
898 hctl++;
899
900 if (sscanf(hctl, "%u:%u:%u:%u", &cxt->scsi_host, &cxt->scsi_channel,
901 &cxt->scsi_target, &cxt->scsi_lun) != 4)
902 return -1;
903
904 cxt->has_hctl = 1;
905 done:
906 if (h)
907 *h = cxt->scsi_host;
908 if (c)
909 *c = cxt->scsi_channel;
910 if (t)
911 *t = cxt->scsi_target;
912 if (l)
913 *l = cxt->scsi_lun;
914 return 0;
915 }
916
917
918 static char *sysfs_scsi_host_attribute_path(struct sysfs_cxt *cxt,
919 const char *type, char *buf, size_t bufsz, const char *attr)
920 {
921 int len;
922 int host;
923
924 if (sysfs_scsi_get_hctl(cxt, &host, NULL, NULL, NULL))
925 return NULL;
926
927 if (attr)
928 len = snprintf(buf, bufsz, _PATH_SYS_CLASS "/%s_host/host%d/%s",
929 type, host, attr);
930 else
931 len = snprintf(buf, bufsz, _PATH_SYS_CLASS "/%s_host/host%d",
932 type, host);
933
934 return (len < 0 || (size_t) len >= bufsz) ? NULL : buf;
935 }
936
937 char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt,
938 const char *type, const char *attr)
939 {
940 char buf[1024];
941 int rc;
942 FILE *f;
943
944 if (!attr || !type ||
945 !sysfs_scsi_host_attribute_path(cxt, type, buf, sizeof(buf), attr))
946 return NULL;
947
948 if (!(f = fopen(buf, "r" UL_CLOEXECSTR)))
949 return NULL;
950
951 rc = fscanf(f, "%1023[^\n]", buf);
952 fclose(f);
953
954 return rc == 1 ? strdup(buf) : NULL;
955 }
956
957 int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type)
958 {
959 char buf[PATH_MAX];
960 struct stat st;
961
962 if (!type || !sysfs_scsi_host_attribute_path(cxt, type,
963 buf, sizeof(buf), NULL))
964 return 0;
965
966 return stat(buf, &st) == 0 && S_ISDIR(st.st_mode);
967 }
968
969 static char *sysfs_scsi_attribute_path(struct sysfs_cxt *cxt,
970 char *buf, size_t bufsz, const char *attr)
971 {
972 int len, h, c, t, l;
973
974 if (sysfs_scsi_get_hctl(cxt, &h, &c, &t, &l) != 0)
975 return NULL;
976
977 if (attr)
978 len = snprintf(buf, bufsz, _PATH_SYS_SCSI "/devices/%d:%d:%d:%d/%s",
979 h,c,t,l, attr);
980 else
981 len = snprintf(buf, bufsz, _PATH_SYS_SCSI "/devices/%d:%d:%d:%d",
982 h,c,t,l);
983 return (len < 0 || (size_t) len >= bufsz) ? NULL : buf;
984 }
985
986 int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr)
987 {
988 char path[PATH_MAX];
989 struct stat st;
990
991 if (!sysfs_scsi_attribute_path(cxt, path, sizeof(path), attr))
992 return 0;
993
994 return stat(path, &st) == 0;
995 }
996
997 int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern)
998 {
999 char path[PATH_MAX], linkc[PATH_MAX];
1000 struct stat st;
1001 ssize_t len;
1002
1003 if (!sysfs_scsi_attribute_path(cxt, path, sizeof(path), NULL))
1004 return 0;
1005
1006 if (stat(path, &st) != 0)
1007 return 0;
1008
1009 len = readlink(path, linkc, sizeof(linkc) - 1);
1010 if (len < 0)
1011 return 0;
1012
1013 linkc[len] = '\0';
1014 return strstr(linkc, pattern) != NULL;
1015 }
1016
1017 #ifdef TEST_PROGRAM_SYSFS
1018 #include <errno.h>
1019 #include <err.h>
1020 #include <stdlib.h>
1021
1022 int main(int argc, char *argv[])
1023 {
1024 struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
1025 char *devname;
1026 dev_t devno, disk_devno;
1027 char path[PATH_MAX], *sub, *chain;
1028 char diskname[32];
1029 int i, is_part;
1030 uint64_t u64;
1031 ssize_t len;
1032
1033 if (argc != 2)
1034 errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]);
1035
1036 devname = argv[1];
1037 devno = sysfs_devname_to_devno(devname, NULL);
1038
1039 if (!devno)
1040 err(EXIT_FAILURE, "failed to read devno");
1041
1042 if (sysfs_init(&cxt, devno, NULL))
1043 return EXIT_FAILURE;
1044
1045 printf("NAME: %s\n", devname);
1046 printf("DEVNAME: %s\n", sysfs_get_devname(&cxt, path, sizeof(path)));
1047 printf("DEVPATH: %s\n", sysfs_devno_to_devpath(devno, path, sizeof(path)));
1048 printf("DEVNO: %u (%d:%d)\n", (unsigned int) devno, major(devno), minor(devno));
1049 printf("DEVNO-PATH: %s\n", sysfs_devno_path(devno, path, sizeof(path)));
1050
1051 sysfs_devno_to_wholedisk(devno, diskname, sizeof(diskname), &disk_devno);
1052 printf("WHOLEDISK-DEVNO: %u (%d:%d)\n", (unsigned int) disk_devno, major(disk_devno), minor(disk_devno));
1053 printf("WHOLEDISK-DEVNAME: %s\n", diskname);
1054
1055 is_part = sysfs_devno_has_attribute(devno, "partition");
1056 printf("PARTITION: %s\n", is_part ? "YES" : "NOT");
1057
1058 printf("HOTPLUG: %s\n", sysfs_is_hotpluggable(&cxt) ? "yes" : "no");
1059 printf("SLAVES: %d\n", sysfs_count_dirents(&cxt, "slaves"));
1060
1061 len = sysfs_readlink(&cxt, NULL, path, sizeof(path) - 1);
1062 if (len > 0) {
1063 path[len] = '\0';
1064 printf("DEVNOLINK: %s\n", path);
1065 }
1066
1067 if (!is_part) {
1068 printf("First 5 partitions:\n");
1069 for (i = 1; i <= 5; i++) {
1070 dev_t dev = sysfs_partno_to_devno(&cxt, i);
1071 if (dev)
1072 printf("\t#%d %d:%d\n", i, major(dev), minor(dev));
1073 }
1074 }
1075
1076 if (sysfs_read_u64(&cxt, "size", &u64))
1077 printf("read SIZE failed\n");
1078 else
1079 printf("SIZE: %jd\n", u64);
1080
1081 if (sysfs_read_int(&cxt, "queue/hw_sector_size", &i))
1082 printf("read SECTOR failed\n");
1083 else
1084 printf("SECTOR: %d\n", i);
1085
1086
1087 chain = sysfs_get_devchain(&cxt, path, sizeof(path));
1088 printf("SUBSUSTEMS:\n");
1089
1090 while (chain && sysfs_next_subsystem(&cxt, chain, &sub) == 0) {
1091 printf("\t%s\n", sub);
1092 free(sub);
1093 }
1094
1095
1096 sysfs_deinit(&cxt);
1097 return EXIT_SUCCESS;
1098 }
1099 #endif