blob: 1de624aadcb14d697ee38951cc56ef19ae59615a [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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
19struct 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
35extern char *sysfs_devno_attribute_path(dev_t devno, char *buf,
36 size_t bufsiz, const char *attr);
37extern int sysfs_devno_has_attribute(dev_t devno, const char *attr);
38extern char *sysfs_devno_path(dev_t devno, char *buf, size_t bufsiz);
39extern char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz);
40extern dev_t sysfs_devname_to_devno(const char *name, const char *parent);
41
42extern int sysfs_init(struct sysfs_cxt *cxt, dev_t devno, struct sysfs_cxt *parent)
43 __attribute__ ((warn_unused_result));
44extern void sysfs_deinit(struct sysfs_cxt *cxt);
45
46extern DIR *sysfs_opendir(struct sysfs_cxt *cxt, const char *attr);
47
48extern int sysfs_stat(struct sysfs_cxt *cxt, const char *attr, struct stat *st);
49extern ssize_t sysfs_readlink(struct sysfs_cxt *cxt, const char *attr,
50 char *buf, size_t bufsiz);
51extern int sysfs_has_attribute(struct sysfs_cxt *cxt, const char *attr);
52
53extern int sysfs_scanf(struct sysfs_cxt *cxt, const char *attr,
54 const char *fmt, ...)
55 __attribute__ ((format (scanf, 3, 4)));
56
57extern int sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr, int64_t *res);
58extern int sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t *res);
59extern int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr, int *res);
60
bigbiff7b4c7a62015-01-01 19:44:14 -050061extern int sysfs_write_string(struct sysfs_cxt *cxt, const char *attr, const char *str);
62extern int sysfs_write_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t num);
63
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050064extern char *sysfs_get_devname(struct sysfs_cxt *cxt, char *buf, size_t bufsiz);
65
66extern char *sysfs_strdup(struct sysfs_cxt *cxt, const char *attr);
67
68extern int sysfs_count_dirents(struct sysfs_cxt *cxt, const char *attr);
69extern int sysfs_count_partitions(struct sysfs_cxt *cxt, const char *devname);
70extern dev_t sysfs_partno_to_devno(struct sysfs_cxt *cxt, int partno);
71extern char *sysfs_get_slave(struct sysfs_cxt *cxt);
72
bigbiff7b4c7a62015-01-01 19:44:14 -050073extern char *sysfs_get_devchain(struct sysfs_cxt *cxt, char *buf, size_t bufsz);
74extern int sysfs_next_subsystem(struct sysfs_cxt *cxt, char *devchain, char **subsys);
75extern int sysfs_is_hotpluggable(struct sysfs_cxt *cxt);
76
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050077extern int sysfs_is_partition_dirent(DIR *dir, struct dirent *d,
78 const char *parent_name);
79
80extern int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
81 size_t len, dev_t *diskdevno);
82
bigbiff7b4c7a62015-01-01 19:44:14 -050083extern int sysfs_devno_is_lvm_private(dev_t devno);
84extern int sysfs_devno_is_wholedisk(dev_t devno);
85
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050086extern int sysfs_scsi_get_hctl(struct sysfs_cxt *cxt, int *h,
87 int *c, int *t, int *l);
88extern char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt,
89 const char *type, const char *attr);
90extern int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type);
91extern int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr);
92extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern);
93
94#endif /* UTIL_LINUX_SYSFS_H */