]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/sysfs.h
Merge branch 'travis-checkusage' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / include / sysfs.h
CommitLineData
7fe16fda
KZ
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>
7fe16fda
KZ
14#include <fcntl.h>
15#include <string.h>
16#include <inttypes.h>
7fe16fda
KZ
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;
d0f7e5b4
KZ
24
25 unsigned int scsi_host,
26 scsi_channel,
27 scsi_target,
28 scsi_lun;
29
638402ed
KZ
30 unsigned int has_hctl : 1,
31 hctl_error : 1 ;
7fe16fda
KZ
32};
33
f5152f1a 34#define UL_SYSFSCXT_EMPTY { 0, -1, NULL, NULL, 0, 0, 0, 0, 0 }
7fe16fda
KZ
35
36extern char *sysfs_devno_attribute_path(dev_t devno, char *buf,
413993fc 37 size_t bufsiz, const char *attr);
7fe16fda 38extern int sysfs_devno_has_attribute(dev_t devno, const char *attr);
413993fc
KZ
39extern char *sysfs_devno_path(dev_t devno, char *buf, size_t bufsiz);
40extern char *sysfs_devno_to_devpath(dev_t devno, char *buf, size_t bufsiz);
7fe16fda
KZ
41extern dev_t sysfs_devname_to_devno(const char *name, const char *parent);
42
1aae31c0
KZ
43extern int sysfs_init(struct sysfs_cxt *cxt, dev_t devno, struct sysfs_cxt *parent)
44 __attribute__ ((warn_unused_result));
7fe16fda
KZ
45extern void sysfs_deinit(struct sysfs_cxt *cxt);
46
47extern DIR *sysfs_opendir(struct sysfs_cxt *cxt, const char *attr);
48
49extern int sysfs_stat(struct sysfs_cxt *cxt, const char *attr, struct stat *st);
413993fc
KZ
50extern ssize_t sysfs_readlink(struct sysfs_cxt *cxt, const char *attr,
51 char *buf, size_t bufsiz);
d8a84552 52extern int sysfs_has_attribute(struct sysfs_cxt *cxt, const char *attr);
7fe16fda
KZ
53
54extern int sysfs_scanf(struct sysfs_cxt *cxt, const char *attr,
55 const char *fmt, ...)
56 __attribute__ ((format (scanf, 3, 4)));
90e9fcda
KZ
57
58extern int sysfs_read_s64(struct sysfs_cxt *cxt, const char *attr, int64_t *res);
59extern int sysfs_read_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t *res);
60extern int sysfs_read_int(struct sysfs_cxt *cxt, const char *attr, int *res);
61
6c987ca9
KZ
62extern int sysfs_write_string(struct sysfs_cxt *cxt, const char *attr, const char *str);
63extern int sysfs_write_u64(struct sysfs_cxt *cxt, const char *attr, uint64_t num);
64
413993fc
KZ
65extern char *sysfs_get_devname(struct sysfs_cxt *cxt, char *buf, size_t bufsiz);
66
7fe16fda
KZ
67extern char *sysfs_strdup(struct sysfs_cxt *cxt, const char *attr);
68
69extern int sysfs_count_dirents(struct sysfs_cxt *cxt, const char *attr);
70extern int sysfs_count_partitions(struct sysfs_cxt *cxt, const char *devname);
778ad369 71extern dev_t sysfs_partno_to_devno(struct sysfs_cxt *cxt, int partno);
413993fc 72extern char *sysfs_get_slave(struct sysfs_cxt *cxt);
7fe16fda 73
e017ef8b
KZ
74extern char *sysfs_get_devchain(struct sysfs_cxt *cxt, char *buf, size_t bufsz);
75extern int sysfs_next_subsystem(struct sysfs_cxt *cxt, char *devchain, char **subsys);
76extern int sysfs_is_hotpluggable(struct sysfs_cxt *cxt);
77
7fe16fda
KZ
78extern int sysfs_is_partition_dirent(DIR *dir, struct dirent *d,
79 const char *parent_name);
80
3b66f48e
ML
81extern int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
82 size_t len, dev_t *diskdevno);
d0f7e5b4 83
39866431 84extern int sysfs_devno_is_lvm_private(dev_t devno);
b55e5886 85extern int sysfs_devno_is_wholedisk(dev_t devno);
39866431 86
d0f7e5b4
KZ
87extern int sysfs_scsi_get_hctl(struct sysfs_cxt *cxt, int *h,
88 int *c, int *t, int *l);
23a11c74
KZ
89extern char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt,
90 const char *type, const char *attr);
91extern int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type);
a5dc4d2d
KZ
92extern int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr);
93extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern);
d0f7e5b4 94
d0dc6c17 95/**
6c62abc4 96 * sysfs_devname_sys_to_dev:
d0dc6c17
SB
97 * @name: devname to be converted in place
98 *
99 * Linux kernel linux/drivers/base/core.c: device_get_devnode()
100 * defines a replacement of '!' in the /sys device name by '/' in the
9e930041 101 * /dev device name. This helper replaces all occurrences of '!' in
759b120d 102 * @name by '/' to convert from /sys to /dev.
d0dc6c17 103 */
6c62abc4 104static inline void sysfs_devname_sys_to_dev(char *name)
d0dc6c17
SB
105{
106 char *c;
107
108 if (name)
109 while ((c = strchr(name, '!')))
110 c[0] = '/';
111}
112
759b120d 113/**
6c62abc4 114 * sysfs_devname_dev_to_sys:
759b120d
SB
115 * @name: devname to be converted in place
116 *
6c62abc4 117 * See sysfs_devname_sys_to_dev().
759b120d 118 */
6c62abc4 119static inline void sysfs_devname_dev_to_sys(char *name)
759b120d
SB
120{
121 char *c;
122
123 if (name)
124 while ((c = strchr(name, '/')))
125 c[0] = '!';
126}
127
7fe16fda 128#endif /* UTIL_LINUX_SYSFS_H */