]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/sysfs.c
2 * No copyright is claimed. This code is in the public domain; do with
5 * Written by Karel Zak <kzak@redhat.com>
11 #include "pathnames.h"
13 #include "fileutils.h"
15 char *sysfs_devno_attribute_path(dev_t devno
, char *buf
,
16 size_t bufsiz
, const char *attr
)
21 len
= snprintf(buf
, bufsiz
, _PATH_SYS_DEVBLOCK
"/%d:%d/%s",
22 major(devno
), minor(devno
), attr
);
24 len
= snprintf(buf
, bufsiz
, _PATH_SYS_DEVBLOCK
"/%d:%d",
25 major(devno
), minor(devno
));
27 return (len
< 0 || (size_t) len
+ 1 > bufsiz
) ? NULL
: buf
;
30 int sysfs_devno_has_attribute(dev_t devno
, const char *attr
)
35 if (!sysfs_devno_attribute_path(devno
, path
, sizeof(path
), attr
))
37 if (stat(path
, &info
) == 0)
42 char *sysfs_devno_path(dev_t devno
, char *buf
, size_t bufsiz
)
44 return sysfs_devno_attribute_path(devno
, buf
, bufsiz
, NULL
);
47 dev_t
sysfs_devname_to_devno(const char *name
, const char *parent
)
49 char buf
[PATH_MAX
], *path
= NULL
;
52 if (strncmp("/dev/", name
, 5) == 0) {
58 if (stat(name
, &st
) == 0)
61 name
+= 5; /* unaccesible, or not node in /dev */
64 if (!dev
&& parent
&& strncmp("dm-", name
, 3)) {
66 * Create path to /sys/block/<parent>/<name>/dev
68 int len
= snprintf(buf
, sizeof(buf
),
69 _PATH_SYS_BLOCK
"/%s/%s/dev", parent
, name
);
70 if (len
< 0 || (size_t) len
+ 1 > sizeof(buf
))
76 * Create path to /sys/block/<name>/dev
78 int len
= snprintf(buf
, sizeof(buf
),
79 _PATH_SYS_BLOCK
"/%s/dev", name
);
80 if (len
< 0 || (size_t) len
+ 1 > sizeof(buf
))
87 * read devno from sysfs
92 f
= fopen(path
, "r" UL_CLOEXECSTR
);
96 if (fscanf(f
, "%d:%d", &maj
, &min
) == 2)
97 dev
= makedev(maj
, min
);
104 * Returns devname (e.g. "/dev/sda1") for the given devno.
106 * Note that the @buf has to be large enough to store /sys/dev/block/<maj:min>
109 * Please, use more robust blkid_devno_to_devname() in your applications.
111 char *sysfs_devno_to_devpath(dev_t devno
, char *buf
, size_t bufsiz
)
113 struct sysfs_cxt cxt
;
118 if (sysfs_init(&cxt
, devno
, NULL
))
121 name
= sysfs_get_devname(&cxt
, buf
, bufsiz
);
129 if (sz
+ sizeof("/dev/") > bufsiz
)
132 /* create the final "/dev/<name>" string */
133 memmove(buf
+ 5, name
, sz
+ 1);
134 memcpy(buf
, "/dev/", 5);
136 if (!stat(buf
, &st
) && S_ISBLK(st
.st_mode
) && st
.st_rdev
== devno
)
142 int sysfs_init(struct sysfs_cxt
*cxt
, dev_t devno
, struct sysfs_cxt
*parent
)
147 memset(cxt
, 0, sizeof(*cxt
));
150 if (!sysfs_devno_path(devno
, path
, sizeof(path
)))
153 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
158 cxt
->dir_path
= strdup(path
);
162 cxt
->parent
= parent
;
165 rc
= errno
> 0 ? -errno
: -1;
170 void sysfs_deinit(struct sysfs_cxt
*cxt
)
175 if (cxt
->dir_fd
>= 0)
179 memset(cxt
, 0, sizeof(*cxt
));
184 int sysfs_stat(struct sysfs_cxt
*cxt
, const char *attr
, struct stat
*st
)
186 int rc
= fstat_at(cxt
->dir_fd
, cxt
->dir_path
, attr
, st
, 0);
188 if (rc
!= 0 && errno
== ENOENT
&&
189 strncmp(attr
, "queue/", 6) == 0 && cxt
->parent
) {
191 /* Exception for "queue/<attr>". These attributes are available
192 * for parental devices only
194 return fstat_at(cxt
->parent
->dir_fd
,
195 cxt
->parent
->dir_path
, attr
, st
, 0);
200 int sysfs_has_attribute(struct sysfs_cxt
*cxt
, const char *attr
)
204 return sysfs_stat(cxt
, attr
, &st
) == 0;
207 static int sysfs_open(struct sysfs_cxt
*cxt
, const char *attr
)
209 int fd
= open_at(cxt
->dir_fd
, cxt
->dir_path
, attr
, O_RDONLY
|O_CLOEXEC
);
211 if (fd
== -1 && errno
== ENOENT
&&
212 strncmp(attr
, "queue/", 6) == 0 && cxt
->parent
) {
214 /* Exception for "queue/<attr>". These attributes are available
215 * for parental devices only
217 fd
= open_at(cxt
->parent
->dir_fd
, cxt
->dir_path
, attr
,
223 ssize_t
sysfs_readlink(struct sysfs_cxt
*cxt
, const char *attr
,
224 char *buf
, size_t bufsiz
)
230 return readlink_at(cxt
->dir_fd
, cxt
->dir_path
, attr
, buf
, bufsiz
);
232 /* read /sys/dev/block/<maj:min> link */
233 return readlink(cxt
->dir_path
, buf
, bufsiz
);
236 DIR *sysfs_opendir(struct sysfs_cxt
*cxt
, const char *attr
)
242 fd
= sysfs_open(cxt
, attr
);
244 else if (cxt
->dir_fd
>= 0)
245 /* request to open root of device in sysfs (/sys/block/<dev>)
246 * -- we cannot use cxt->sysfs_fd directly, because closedir()
247 * will close this our persistent file descriptor.
249 fd
= dup(cxt
->dir_fd
);
265 static FILE *sysfs_fopen(struct sysfs_cxt
*cxt
, const char *attr
)
267 int fd
= sysfs_open(cxt
, attr
);
269 return fd
< 0 ? NULL
: fdopen(fd
, "r" UL_CLOEXECSTR
);
273 static struct dirent
*xreaddir(DIR *dp
)
277 while ((d
= readdir(dp
))) {
278 if (!strcmp(d
->d_name
, ".") ||
279 !strcmp(d
->d_name
, ".."))
282 /* blacklist here? */
288 int sysfs_is_partition_dirent(DIR *dir
, struct dirent
*d
, const char *parent_name
)
292 #ifdef _DIRENT_HAVE_D_TYPE
293 if (d
->d_type
!= DT_DIR
&&
294 d
->d_type
!= DT_LNK
&&
295 d
->d_type
!= DT_UNKNOWN
)
299 const char *p
= parent_name
;
302 /* /dev/sda --> "sda" */
303 if (*parent_name
== '/') {
304 p
= strrchr(parent_name
, '/');
311 if (strlen(d
->d_name
) <= len
)
314 /* partitions subdir name is
315 * "<parent>[:digit:]" or "<parent>p[:digit:]"
317 return strncmp(p
, d
->d_name
, len
) == 0 &&
318 ((*(d
->d_name
+ len
) == 'p' && isdigit(*(d
->d_name
+ len
+ 1)))
319 || isdigit(*(d
->d_name
+ len
)));
322 /* Cannot use /partition file, not supported on old sysfs */
323 snprintf(path
, sizeof(path
), "%s/start", d
->d_name
);
325 return faccessat(dirfd(dir
), path
, R_OK
, 0) == 0;
329 * Converts @partno (partition number) to devno of the partition.
330 * The @cxt handles wholedisk device.
332 * Note that this code does not expect any special format of the
333 * partitions devnames.
335 dev_t
sysfs_partno_to_devno(struct sysfs_cxt
*cxt
, int partno
)
342 dir
= sysfs_opendir(cxt
, NULL
);
346 while ((d
= xreaddir(dir
))) {
349 if (!sysfs_is_partition_dirent(dir
, d
, NULL
))
352 snprintf(path
, sizeof(path
), "%s/partition", d
->d_name
);
353 if (sysfs_read_int(cxt
, path
, &n
))
357 snprintf(path
, sizeof(path
), "%s/dev", d
->d_name
);
358 if (sysfs_scanf(cxt
, path
, "%d:%d", &maj
, &min
) == 2)
359 devno
= makedev(maj
, min
);
369 int sysfs_scanf(struct sysfs_cxt
*cxt
, const char *attr
, const char *fmt
, ...)
371 FILE *f
= sysfs_fopen(cxt
, attr
);
378 rc
= vfscanf(f
, fmt
, ap
);
386 int sysfs_read_s64(struct sysfs_cxt
*cxt
, const char *attr
, int64_t *res
)
390 if (sysfs_scanf(cxt
, attr
, "%"SCNd64
, &x
) == 1) {
398 int sysfs_read_u64(struct sysfs_cxt
*cxt
, const char *attr
, uint64_t *res
)
402 if (sysfs_scanf(cxt
, attr
, "%"SCNu64
, &x
) == 1) {
410 int sysfs_read_int(struct sysfs_cxt
*cxt
, const char *attr
, int *res
)
414 if (sysfs_scanf(cxt
, attr
, "%d", &x
) == 1) {
422 char *sysfs_strdup(struct sysfs_cxt
*cxt
, const char *attr
)
425 return sysfs_scanf(cxt
, attr
, "%1023[^\n]", buf
) == 1 ?
429 int sysfs_count_dirents(struct sysfs_cxt
*cxt
, const char *attr
)
434 if (!(dir
= sysfs_opendir(cxt
, attr
)))
437 while (xreaddir(dir
)) r
++;
443 int sysfs_count_partitions(struct sysfs_cxt
*cxt
, const char *devname
)
449 if (!(dir
= sysfs_opendir(cxt
, NULL
)))
452 while ((d
= xreaddir(dir
))) {
453 if (sysfs_is_partition_dirent(dir
, d
, devname
))
462 * Returns slave name if there is only one slave, otherwise returns NULL.
463 * The result should be deallocated by free().
465 char *sysfs_get_slave(struct sysfs_cxt
*cxt
)
471 if (!(dir
= sysfs_opendir(cxt
, "slaves")))
474 while ((d
= xreaddir(dir
))) {
476 goto err
; /* more slaves */
478 name
= strdup(d
->d_name
);
490 * Note that the @buf has to be large enough to store /sys/dev/block/<maj:min>
493 char *sysfs_get_devname(struct sysfs_cxt
*cxt
, char *buf
, size_t bufsiz
)
498 sz
= sysfs_readlink(cxt
, NULL
, buf
, bufsiz
- 1);
503 name
= strrchr(buf
, '/');
510 memmove(buf
, name
, sz
+ 1);
514 static int get_dm_wholedisk(struct sysfs_cxt
*cxt
, char *diskname
,
515 size_t len
, dev_t
*diskdevno
)
520 /* Note, sysfs_get_slave() returns the first slave only,
521 * if there is more slaves, then return NULL
523 name
= sysfs_get_slave(cxt
);
527 if (diskname
&& len
) {
528 strncpy(diskname
, name
, len
);
529 diskname
[len
- 1] = '\0';
533 *diskdevno
= sysfs_devname_to_devno(name
, NULL
);
542 int sysfs_devno_to_wholedisk(dev_t dev
, char *diskname
,
543 size_t len
, dev_t
*diskdevno
)
545 struct sysfs_cxt cxt
;
548 if (!dev
|| sysfs_init(&cxt
, dev
, NULL
) != 0)
551 is_part
= sysfs_has_attribute(&cxt
, "partition");
554 * Extra case for partitions mapped by device-mapper.
556 * All regualar partitions (added by BLKPG ioctl or kernel PT
557 * parser) have the /sys/.../partition file. The partitions
558 * mapped by DM don't have such file, but they have "part"
561 char *uuid
= sysfs_strdup(&cxt
, "dm/uuid");
563 char *prefix
= uuid
? strsep(&tmp
, "-") : NULL
;
565 if (prefix
&& strncasecmp(prefix
, "part", 4) == 0)
570 get_dm_wholedisk(&cxt
, diskname
, len
, diskdevno
) == 0)
572 * partitioned device, mapped by DM
581 * unpartitioned device
583 if (diskname
&& len
) {
584 if (!sysfs_get_devname(&cxt
, diskname
, len
))
593 * - readlink /sys/dev/block/8:1 = ../../block/sda/sda1
594 * - dirname ../../block/sda/sda1 = ../../block/sda
595 * - basename ../../block/sda = sda
597 char linkpath
[PATH_MAX
];
601 linklen
= sysfs_readlink(&cxt
, NULL
,
602 linkpath
, sizeof(linkpath
) - 1);
605 linkpath
[linklen
] = '\0';
607 stripoff_last_component(linkpath
); /* dirname */
608 name
= stripoff_last_component(linkpath
); /* basename */
612 if (diskname
&& len
) {
613 strncpy(diskname
, name
, len
);
614 diskname
[len
- 1] = '\0';
618 *diskdevno
= sysfs_devname_to_devno(name
, NULL
);
633 int sysfs_scsi_get_hctl(struct sysfs_cxt
*cxt
, int *h
, int *c
, int *t
, int *l
)
635 char buf
[PATH_MAX
], *hctl
;
643 len
= sysfs_readlink(cxt
, "device", buf
, sizeof(buf
) - 1);
648 hctl
= strrchr(buf
, '/');
653 if (sscanf(hctl
, "%u:%u:%u:%u", &cxt
->scsi_host
, &cxt
->scsi_channel
,
654 &cxt
->scsi_target
, &cxt
->scsi_lun
) != 4)
662 *c
= cxt
->scsi_channel
;
664 *t
= cxt
->scsi_target
;
671 static char *sysfs_scsi_host_attribute_path(struct sysfs_cxt
*cxt
,
672 const char *type
, char *buf
, size_t bufsz
, const char *attr
)
677 if (sysfs_scsi_get_hctl(cxt
, &host
, NULL
, NULL
, NULL
))
681 len
= snprintf(buf
, bufsz
, _PATH_SYS_CLASS
"/%s_host/host%d/%s",
684 len
= snprintf(buf
, bufsz
, _PATH_SYS_CLASS
"/%s_host/host%d",
687 return (len
< 0 || (size_t) len
+ 1 > bufsz
) ? NULL
: buf
;
690 char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt
*cxt
,
691 const char *type
, const char *attr
)
697 if (!attr
|| !type
||
698 !sysfs_scsi_host_attribute_path(cxt
, type
, buf
, sizeof(buf
), attr
))
701 if (!(f
= fopen(buf
, "r" UL_CLOEXECSTR
)))
704 rc
= fscanf(f
, "%1023[^\n]", buf
);
707 return rc
== 1 ? strdup(buf
) : NULL
;
710 int sysfs_scsi_host_is(struct sysfs_cxt
*cxt
, const char *type
)
715 if (!type
|| !sysfs_scsi_host_attribute_path(cxt
, type
,
716 buf
, sizeof(buf
), NULL
))
719 return stat(buf
, &st
) == 0 && S_ISDIR(st
.st_mode
);
722 static char *sysfs_scsi_attribute_path(struct sysfs_cxt
*cxt
,
723 char *buf
, size_t bufsz
, const char *attr
)
727 if (sysfs_scsi_get_hctl(cxt
, &h
, &c
, &t
, &l
) != 0)
731 len
= snprintf(buf
, bufsz
, _PATH_SYS_SCSI
"/devices/%d:%d:%d:%d/%s",
734 len
= snprintf(buf
, bufsz
, _PATH_SYS_SCSI
"/devices/%d:%d:%d:%d",
736 return (len
< 0 || (size_t) len
+ 1 > bufsz
) ? NULL
: buf
;
739 int sysfs_scsi_has_attribute(struct sysfs_cxt
*cxt
, const char *attr
)
744 if (!sysfs_scsi_attribute_path(cxt
, path
, sizeof(path
), attr
))
747 return stat(path
, &st
) == 0;
750 int sysfs_scsi_path_contains(struct sysfs_cxt
*cxt
, const char *pattern
)
752 char path
[PATH_MAX
], linkc
[PATH_MAX
];
756 if (!sysfs_scsi_attribute_path(cxt
, path
, sizeof(path
), NULL
))
759 if (stat(path
, &st
) != 0)
762 len
= readlink(path
, linkc
, sizeof(linkc
) - 1);
767 return strstr(linkc
, pattern
) != NULL
;
770 #ifdef TEST_PROGRAM_SYSFS
775 int main(int argc
, char *argv
[])
777 struct sysfs_cxt cxt
= UL_SYSFSCXT_EMPTY
;
786 errx(EXIT_FAILURE
, "usage: %s <devname>", argv
[0]);
789 devno
= sysfs_devname_to_devno(devname
, NULL
);
792 err(EXIT_FAILURE
, "failed to read devno");
794 is_part
= sysfs_devno_has_attribute(devno
, "partition");
796 printf("NAME: %s\n", devname
);
797 printf("DEVNO: %u (%d:%d)\n", (unsigned int) devno
, major(devno
), minor(devno
));
798 printf("DEVNOPATH: %s\n", sysfs_devno_path(devno
, path
, sizeof(path
)));
799 printf("DEVPATH: %s\n", sysfs_devno_to_devpath(devno
, path
, sizeof(path
)));
800 printf("PARTITION: %s\n", is_part
? "YES" : "NOT");
802 if (sysfs_init(&cxt
, devno
, NULL
))
805 len
= sysfs_readlink(&cxt
, NULL
, path
, sizeof(path
) - 1);
808 printf("DEVNOLINK: %s\n", path
);
812 printf("First 5 partitions:\n");
813 for (i
= 1; i
<= 5; i
++) {
814 dev_t dev
= sysfs_partno_to_devno(&cxt
, i
);
816 printf("\t#%d %d:%d\n", i
, major(dev
), minor(dev
));
820 printf("SLAVES: %d\n", sysfs_count_dirents(&cxt
, "slaves"));
822 if (sysfs_read_u64(&cxt
, "size", &u64
))
823 printf("read SIZE failed\n");
825 printf("SIZE: %jd\n", u64
);
827 if (sysfs_read_int(&cxt
, "queue/hw_sector_size", &i
))
828 printf("read SECTOR failed\n");
830 printf("SECTOR: %d\n", i
);
832 printf("DEVNAME: %s\n", sysfs_get_devname(&cxt
, path
, sizeof(path
)));