-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsysfs.c
196 lines (177 loc) · 4.25 KB
/
sysfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Access helpers for sysfs.
* Copyright (c) Mark Lord 2008
*
* You may use/distribute this freely, under the terms of either
* (your choice) the GNU General Public License version 2,
* or a BSD style license.
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <linux/types.h>
#include "hdparm.h"
static char *path_append (char *path, const char *new)
{
char *pathtail = path + strlen(path);
*pathtail = '/';
strcpy(pathtail+1, new);
return pathtail;
}
static int sysfs_write_attr (char *path, const char *attr, const char *fmt, void *val, int verbose)
{
FILE *fp;
int count = -1, err = 0;
char *pathtail = path_append(path, attr);
fp = fopen(path, "w");
if (!fp) {
err = errno;
} else if (fmt[0] != '%') {
err = EINVAL;
} else {
switch (fmt[1]) {
case 's':
count = fprintf(fp, fmt, val);
break;
case 'd':
case 'u':
count = fprintf(fp, fmt, *(unsigned int *)val);
break;
case 'l':
if (fmt[2] == 'l')
count = fprintf(fp, fmt, *(unsigned long long *)val);
else
count = fprintf(fp, fmt, *(unsigned long *)val);
break;
default:
errno = EINVAL;
}
if (count < 0)
err = errno;
fclose(fp);
}
if (err && verbose) perror(path);
*pathtail = '\0';
return err;
}
static int sysfs_read_attr (char *path, const char *attr, const char *fmt, void *val1, void *val2, int verbose)
{
FILE *fp;
int count, err = 0;
char *pathtail = path_append(path, attr);
fp = fopen(path, "r");
if (!fp) {
err = errno;
} else {
count = fscanf(fp, fmt, val1, val2);
if (count != (val2 ? 2 : 1))
err = (count == EOF) ? errno : EINVAL;
fclose(fp);
}
if (err && verbose) perror(path);
*pathtail = '\0';
return err;
}
static int sysfs_find_dev2 (char *path, dev_t dev, int recurse, int verbose)
{
DIR *dp;
struct dirent *entry;
char *pathtail;
if (!(dp = opendir(path))) {
int err = errno;
if (verbose) perror(path);
return err;
}
pathtail = path + strlen(path);
while ((entry = readdir(dp)) != NULL) {
if ((entry->d_type == DT_DIR || entry->d_type == DT_LNK) && entry->d_name[0] != '.') {
unsigned int maj, min;
sprintf(pathtail, "/%s", entry->d_name);
if (sysfs_read_attr(path, "/dev", "%u:%u", &maj, &min, verbose))
min = ~minor(dev);
else if (maj != (unsigned)major(dev))
continue;
if (min == (unsigned)minor(dev)
|| (recurse && sysfs_find_dev2(path, dev, recurse - 1, verbose) == 0)) {
closedir(dp);
return 0;
}
}
}
closedir(dp);
*pathtail = '\0';
if (verbose)
fprintf(stderr, "%u,%u: device not found in /sys\n", major(dev), minor(dev));
return ENOENT;
}
static int sysfs_find_dev (dev_t dev, char *path, int verbose)
{
int err, recurse = 1;
strcpy(path, "/sys/block");
err = sysfs_find_dev2(path, dev, recurse, 0);
if (err && verbose)
fprintf(stderr, "%s(%u:%u): %s\n", __func__,
major(dev), minor(dev), strerror(err));
return err;
}
static int get_dev_from_fd (int fd, dev_t *dev, int verbose)
{
struct stat st;
if (0 != fstat(fd, &st)) {
int err = errno;
if (verbose) perror(" fstat() failed");
return err;
}
if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
*dev = st.st_rdev;
else
*dev = st.st_dev;
return 0;
}
static int sysfs_find_fd (int fd, char **path_p, int verbose)
{
static int have_prev = 0;
static dev_t prev;
static char path[PATH_MAX];
dev_t dev;
int err;
memset(&dev, 0, sizeof(dev));
err = get_dev_from_fd(fd, &dev, verbose);
if (!err) {
if (have_prev && 0 == memcmp(&dev, &prev, sizeof(dev))) {
/*re-use previous path, since dev was unchanged from before */
} else {
prev = dev;
have_prev = 1;
err = sysfs_find_dev(dev, path, verbose);
}
}
if (err)
have_prev = 0;
else
*path_p = path;
return err;
}
int sysfs_get_attr (int fd, const char *attr, const char *fmt, void *val1, void *val2, int verbose)
{
char *path;
int err;
err = sysfs_find_fd(fd, &path, verbose);
if (!err)
err = sysfs_read_attr(path, attr, fmt, val1, val2, verbose);
return err;
}
int sysfs_set_attr (int fd, const char *attr, const char *fmt, void *val_p, int verbose)
{
char *path;
int err;
err = sysfs_find_fd(fd, &path, verbose);
if (!err)
err = sysfs_write_attr(path, attr, fmt, val_p, verbose);
return err;
}