bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Karel Zak <kzak@redhat.com> |
| 3 | */ |
| 4 | #ifndef UTIL_LINUX_SYSFS_H |
| 5 | #define UTIL_LINUX_SYSFS_H |
| 6 | |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdint.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <string.h> |
| 16 | #include <inttypes.h> |
| 17 | #include <dirent.h> |
| 18 | |
| 19 | struct sysfs_cxt { |
| 20 | dev_t devno; |
| 21 | int dir_fd; /* /sys/block/<name> */ |
| 22 | char *dir_path; |
| 23 | struct sysfs_cxt *parent; |
| 24 | |
| 25 | unsigned int scsi_host, |
| 26 | scsi_channel, |
| 27 | scsi_target, |
| 28 | scsi_lun; |
| 29 | |
| 30 | unsigned int has_hctl : 1; |
| 31 | }; |
| 32 | |
| 33 | #define UL_SYSFSCXT_EMPTY { 0, -1, NULL, NULL, 0, 0, 0, 0, 0 } |
| 34 | |
| 35 | extern char *sysfs_devno_attribute_path(dev_t devno, char *buf, |
| 36 | size_t bufsiz, const char *attr); |
| 37 | extern int sysfs_devno_has_attribute(dev_t devno, const char *attr); |
| 38 | extern char *sysfs_devno_path(dev_t devno, char *buf, size_t bufsiz); |
| 39 | extern char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz); |
| 40 | extern dev_t sysfs_devname_to_devno(const char *name, const char *parent); |
| 41 | |
| 42 | extern int sysfs_init(struct sysfs_cxt *cxt, dev_t devno, struct sysfs_cxt *parent) |
| 43 | __attribute__ ((warn_unused_result)); |
| 44 | extern void sysfs_deinit(struct sysfs_cxt *cxt); |
| 45 | |
| 46 | extern DIR *sysfs_opendir(struct sysfs_cxt *cxt, const char *attr); |
| 47 | |
| 48 | extern int sysfs_stat(struct sysfs_cxt *cxt, const char *attr, struct stat *st); |
| 49 | extern ssize_t sysfs_readlink(struct sysfs_cxt *cxt, const char *attr, |
| 50 | char *buf, size_t bufsiz); |
| 51 | extern int sysfs_has_attribute(struct sysfs_cxt *cxt, const char *attr); |
| 52 | |
| 53 | extern int sysfs_scanf(struct sysfs_cxt *cxt, const char *attr, |
| 54 | const char *fmt, ...) |
| 55 | __attribute__ ((format (scanf, 3, 4))); |
| 56 | |
| 57 | extern int sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr, int64_t *res); |
| 58 | extern int sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t *res); |
| 59 | extern int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr, int *res); |
| 60 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 61 | extern int sysfs_write_string(struct sysfs_cxt *cxt, const char *attr, const char *str); |
| 62 | extern int sysfs_write_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t num); |
| 63 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 64 | extern char *sysfs_get_devname(struct sysfs_cxt *cxt, char *buf, size_t bufsiz); |
| 65 | |
| 66 | extern char *sysfs_strdup(struct sysfs_cxt *cxt, const char *attr); |
| 67 | |
| 68 | extern int sysfs_count_dirents(struct sysfs_cxt *cxt, const char *attr); |
| 69 | extern int sysfs_count_partitions(struct sysfs_cxt *cxt, const char *devname); |
| 70 | extern dev_t sysfs_partno_to_devno(struct sysfs_cxt *cxt, int partno); |
| 71 | extern char *sysfs_get_slave(struct sysfs_cxt *cxt); |
| 72 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 73 | extern char *sysfs_get_devchain(struct sysfs_cxt *cxt, char *buf, size_t bufsz); |
| 74 | extern int sysfs_next_subsystem(struct sysfs_cxt *cxt, char *devchain, char **subsys); |
| 75 | extern int sysfs_is_hotpluggable(struct sysfs_cxt *cxt); |
| 76 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 77 | extern int sysfs_is_partition_dirent(DIR *dir, struct dirent *d, |
| 78 | const char *parent_name); |
| 79 | |
| 80 | extern int sysfs_devno_to_wholedisk(dev_t dev, char *diskname, |
| 81 | size_t len, dev_t *diskdevno); |
| 82 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 83 | extern int sysfs_devno_is_lvm_private(dev_t devno); |
| 84 | extern int sysfs_devno_is_wholedisk(dev_t devno); |
| 85 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 86 | extern int sysfs_scsi_get_hctl(struct sysfs_cxt *cxt, int *h, |
| 87 | int *c, int *t, int *l); |
| 88 | extern char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt, |
| 89 | const char *type, const char *attr); |
| 90 | extern int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type); |
| 91 | extern int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr); |
| 92 | extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern); |
| 93 | |
| 94 | #endif /* UTIL_LINUX_SYSFS_H */ |