]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/sysfs.c
md5: declare byteReverse as static
[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[NAME_MAX + 6 + 1];
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[NAME_MAX + 10 + 1];
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 subsystems
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 (optionally) 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 regular 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 && !sysfs_get_devname(&cxt, diskname, len))
789 goto err;
790 if (diskdevno)
791 *diskdevno = dev;
792
793 } else {
794 /*
795 * partitioned device
796 * - readlink /sys/dev/block/8:1 = ../../block/sda/sda1
797 * - dirname ../../block/sda/sda1 = ../../block/sda
798 * - basename ../../block/sda = sda
799 */
800 char linkpath[PATH_MAX];
801 char *name;
802 ssize_t linklen;
803
804 linklen = sysfs_readlink(&cxt, NULL, linkpath, sizeof(linkpath) - 1);
805 if (linklen < 0)
806 goto err;
807 linkpath[linklen] = '\0';
808
809 stripoff_last_component(linkpath); /* dirname */
810 name = stripoff_last_component(linkpath); /* basename */
811 if (!name)
812 goto err;
813
814 sysfs_devname_sys_to_dev(name);
815 if (diskname && len) {
816 strncpy(diskname, name, len);
817 diskname[len - 1] = '\0';
818 }
819
820 if (diskdevno) {
821 *diskdevno = sysfs_devname_to_devno(name, NULL);
822 if (!*diskdevno)
823 goto err;
824 }
825 }
826
827 done:
828 sysfs_deinit(&cxt);
829 return 0;
830 err:
831 sysfs_deinit(&cxt);
832 return -1;
833 }
834
835 /*
836 * Returns 1 if the device is private LVM device. The @uuid (if not NULL)
837 * returns DM device UUID, use free() to deallocate.
838 */
839 int sysfs_devno_is_lvm_private(dev_t devno, char **uuid)
840 {
841 struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
842 char *id = NULL;
843 int rc = 0;
844
845 if (sysfs_init(&cxt, devno, NULL) != 0)
846 return 0;
847
848 id = sysfs_strdup(&cxt, "dm/uuid");
849
850 /* Private LVM devices use "LVM-<uuid>-<name>" uuid format (important
851 * is the "LVM" prefix and "-<name>" postfix).
852 */
853 if (id && strncmp(id, "LVM-", 4) == 0) {
854 char *p = strrchr(id + 4, '-');
855
856 if (p && *(p + 1))
857 rc = 1;
858 }
859
860 sysfs_deinit(&cxt);
861 if (uuid)
862 *uuid = id;
863 else
864 free(id);
865 return rc;
866 }
867
868 /*
869 * Return 0 or 1, or < 0 in case of error
870 */
871 int sysfs_devno_is_wholedisk(dev_t devno)
872 {
873 dev_t disk;
874
875 if (sysfs_devno_to_wholedisk(devno, NULL, 0, &disk) != 0)
876 return -1;
877
878 return devno == disk;
879 }
880
881
882 int sysfs_scsi_get_hctl(struct sysfs_cxt *cxt, int *h, int *c, int *t, int *l)
883 {
884 char buf[PATH_MAX], *hctl;
885 ssize_t len;
886
887 if (!cxt || cxt->hctl_error)
888 return -EINVAL;
889 if (cxt->has_hctl)
890 goto done;
891
892 cxt->hctl_error = 1;
893 len = sysfs_readlink(cxt, "device", buf, sizeof(buf) - 1);
894 if (len < 0)
895 return len;
896
897 buf[len] = '\0';
898 hctl = strrchr(buf, '/');
899 if (!hctl)
900 return -1;
901 hctl++;
902
903 if (sscanf(hctl, "%u:%u:%u:%u", &cxt->scsi_host, &cxt->scsi_channel,
904 &cxt->scsi_target, &cxt->scsi_lun) != 4)
905 return -1;
906
907 cxt->has_hctl = 1;
908 done:
909 if (h)
910 *h = cxt->scsi_host;
911 if (c)
912 *c = cxt->scsi_channel;
913 if (t)
914 *t = cxt->scsi_target;
915 if (l)
916 *l = cxt->scsi_lun;
917
918 cxt->hctl_error = 0;
919 return 0;
920 }
921
922
923 static char *sysfs_scsi_host_attribute_path(struct sysfs_cxt *cxt,
924 const char *type, char *buf, size_t bufsz, const char *attr)
925 {
926 int len;
927 int host;
928
929 if (sysfs_scsi_get_hctl(cxt, &host, NULL, NULL, NULL))
930 return NULL;
931
932 if (attr)
933 len = snprintf(buf, bufsz, _PATH_SYS_CLASS "/%s_host/host%d/%s",
934 type, host, attr);
935 else
936 len = snprintf(buf, bufsz, _PATH_SYS_CLASS "/%s_host/host%d",
937 type, host);
938
939 return (len < 0 || (size_t) len >= bufsz) ? NULL : buf;
940 }
941
942 char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt,
943 const char *type, const char *attr)
944 {
945 char buf[1024];
946 int rc;
947 FILE *f;
948
949 if (!attr || !type ||
950 !sysfs_scsi_host_attribute_path(cxt, type, buf, sizeof(buf), attr))
951 return NULL;
952
953 if (!(f = fopen(buf, "r" UL_CLOEXECSTR)))
954 return NULL;
955
956 rc = fscanf(f, "%1023[^\n]", buf);
957 fclose(f);
958
959 return rc == 1 ? strdup(buf) : NULL;
960 }
961
962 int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type)
963 {
964 char buf[PATH_MAX];
965 struct stat st;
966
967 if (!type || !sysfs_scsi_host_attribute_path(cxt, type,
968 buf, sizeof(buf), NULL))
969 return 0;
970
971 return stat(buf, &st) == 0 && S_ISDIR(st.st_mode);
972 }
973
974 static char *sysfs_scsi_attribute_path(struct sysfs_cxt *cxt,
975 char *buf, size_t bufsz, const char *attr)
976 {
977 int len, h, c, t, l;
978
979 if (sysfs_scsi_get_hctl(cxt, &h, &c, &t, &l) != 0)
980 return NULL;
981
982 if (attr)
983 len = snprintf(buf, bufsz, _PATH_SYS_SCSI "/devices/%d:%d:%d:%d/%s",
984 h,c,t,l, attr);
985 else
986 len = snprintf(buf, bufsz, _PATH_SYS_SCSI "/devices/%d:%d:%d:%d",
987 h,c,t,l);
988 return (len < 0 || (size_t) len >= bufsz) ? NULL : buf;
989 }
990
991 int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr)
992 {
993 char path[PATH_MAX];
994 struct stat st;
995
996 if (!sysfs_scsi_attribute_path(cxt, path, sizeof(path), attr))
997 return 0;
998
999 return stat(path, &st) == 0;
1000 }
1001
1002 int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern)
1003 {
1004 char path[PATH_MAX], linkc[PATH_MAX];
1005 struct stat st;
1006 ssize_t len;
1007
1008 if (!sysfs_scsi_attribute_path(cxt, path, sizeof(path), NULL))
1009 return 0;
1010
1011 if (stat(path, &st) != 0)
1012 return 0;
1013
1014 len = readlink(path, linkc, sizeof(linkc) - 1);
1015 if (len < 0)
1016 return 0;
1017
1018 linkc[len] = '\0';
1019 return strstr(linkc, pattern) != NULL;
1020 }
1021
1022 #ifdef TEST_PROGRAM_SYSFS
1023 #include <errno.h>
1024 #include <err.h>
1025 #include <stdlib.h>
1026
1027 int main(int argc, char *argv[])
1028 {
1029 struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
1030 char *devname;
1031 dev_t devno, disk_devno;
1032 char path[PATH_MAX], *sub, *chain;
1033 char diskname[32];
1034 int i, is_part;
1035 uint64_t u64;
1036 ssize_t len;
1037
1038 if (argc != 2)
1039 errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]);
1040
1041 devname = argv[1];
1042 devno = sysfs_devname_to_devno(devname, NULL);
1043
1044 if (!devno)
1045 err(EXIT_FAILURE, "failed to read devno");
1046
1047 if (sysfs_init(&cxt, devno, NULL))
1048 return EXIT_FAILURE;
1049
1050 printf("NAME: %s\n", devname);
1051 printf("DEVNAME: %s\n", sysfs_get_devname(&cxt, path, sizeof(path)));
1052 printf("DEVPATH: %s\n", sysfs_devno_to_devpath(devno, path, sizeof(path)));
1053 printf("DEVNO: %u (%d:%d)\n", (unsigned int) devno, major(devno), minor(devno));
1054 printf("DEVNO-PATH: %s\n", sysfs_devno_path(devno, path, sizeof(path)));
1055
1056 sysfs_devno_to_wholedisk(devno, diskname, sizeof(diskname), &disk_devno);
1057 printf("WHOLEDISK-DEVNO: %u (%d:%d)\n", (unsigned int) disk_devno, major(disk_devno), minor(disk_devno));
1058 printf("WHOLEDISK-DEVNAME: %s\n", diskname);
1059
1060 is_part = sysfs_devno_has_attribute(devno, "partition");
1061 printf("PARTITION: %s\n", is_part ? "YES" : "NOT");
1062
1063 printf("HOTPLUG: %s\n", sysfs_is_hotpluggable(&cxt) ? "yes" : "no");
1064 printf("SLAVES: %d\n", sysfs_count_dirents(&cxt, "slaves"));
1065
1066 len = sysfs_readlink(&cxt, NULL, path, sizeof(path) - 1);
1067 if (len > 0) {
1068 path[len] = '\0';
1069 printf("DEVNOLINK: %s\n", path);
1070 }
1071
1072 if (!is_part) {
1073 printf("First 5 partitions:\n");
1074 for (i = 1; i <= 5; i++) {
1075 dev_t dev = sysfs_partno_to_devno(&cxt, i);
1076 if (dev)
1077 printf("\t#%d %d:%d\n", i, major(dev), minor(dev));
1078 }
1079 }
1080
1081 if (sysfs_read_u64(&cxt, "size", &u64))
1082 printf("read SIZE failed\n");
1083 else
1084 printf("SIZE: %jd\n", u64);
1085
1086 if (sysfs_read_int(&cxt, "queue/hw_sector_size", &i))
1087 printf("read SECTOR failed\n");
1088 else
1089 printf("SECTOR: %d\n", i);
1090
1091
1092 chain = sysfs_get_devchain(&cxt, path, sizeof(path));
1093 printf("SUBSUSTEMS:\n");
1094
1095 while (chain && sysfs_next_subsystem(&cxt, chain, &sub) == 0) {
1096 printf("\t%s\n", sub);
1097 free(sub);
1098 }
1099
1100
1101 sysfs_deinit(&cxt);
1102 return EXIT_SUCCESS;
1103 }
1104 #endif /* TEST_PROGRAM_SYSFS */