forked from g7/mce-plugin-libhybris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sysfs-val.c
374 lines (311 loc) · 9.17 KB
/
sysfs-val.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/** @file sysfs-val.c
*
* mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
* <p>
* Copyright (C) 2017 Jolla Ltd.
* <p>
* @author Simo Piiroinen <[email protected]>
*
* mce-plugin-libhybris is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* mce-plugin-libhybris is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mce-plugin-libhybris; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sysfs-val.h"
#include "plugin-logging.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
/* ========================================================================= *
* TYPES
* ========================================================================= */
struct sysfsval_t
{
char *sv_path;
int sv_file;
int sv_curr;
};
/* ========================================================================= *
* PROTOS
* ========================================================================= */
static void sysfsval_ctor (sysfsval_t *self);
static void sysfsval_dtor (sysfsval_t *self);
sysfsval_t *sysfsval_create (void);
void sysfsval_delete (sysfsval_t *self);
bool sysfsval_open_rw (sysfsval_t *self, const char *path);
bool sysfsval_open_ro (sysfsval_t *self, const char *path);
static bool sysfsval_open_ex (sysfsval_t *self, const char *path, mode_t mode);
void sysfsval_close (sysfsval_t *self);
const char *sysfsval_path (const sysfsval_t *self);
int sysfsval_get (const sysfsval_t *self);
bool sysfsval_set (sysfsval_t *self, int value);
void sysfsval_invalidate(sysfsval_t *self);
bool sysfsval_refresh (sysfsval_t *self);
/* ========================================================================= *
* CODE
* ========================================================================= */
/** Initialize sysfsval_t object to a sane state
*
* @param self sysfsval_t object pointer
*/
static void
sysfsval_ctor(sysfsval_t *self)
{
self->sv_path = 0;
self->sv_file = -1;
self->sv_curr = -1;
}
/** Release all dynamically allocated resources used by sysfsval_t object
*
* @param self sysfsval_t object pointer
*/
static void
sysfsval_dtor(sysfsval_t *self)
{
sysfsval_close(self);
}
/** Allocate and initialize an sysfsval_t object
*
* @return sysfsval_t object pointer
*/
sysfsval_t *
sysfsval_create(void)
{
sysfsval_t *self = calloc(1, sizeof *self);
sysfsval_ctor(self);
return self;
}
/** De-initialize and release sysfsval_t object
*
* @param self sysfsval_t object pointer, or NULL
*/
void
sysfsval_delete(sysfsval_t *self)
{
if( self ) {
sysfsval_dtor(self);
free(self);
}
}
/** Assign path to sysfsval_t object and open the file in read+write mode
*
* @param self sysfsval_t object pointer
*
* @return true if file was opened succesfully, false otherwise
*/
bool
sysfsval_open_rw(sysfsval_t *self, const char *path)
{
return sysfsval_open_ex(self, path, O_RDWR);
}
/** Assign path to sysfsval_t object and open the file in read-only mode
*
* @param self sysfsval_t object pointer
*
* @return true if file was opened succesfully, false otherwise
*/
bool
sysfsval_open_ro(sysfsval_t *self, const char *path)
{
return sysfsval_open_ex(self, path, O_RDONLY);
}
static bool
sysfsval_open_ex(sysfsval_t *self, const char *path, mode_t mode)
{
bool ack = false;
sysfsval_close(self);
if( !path )
goto EXIT;
if( (self->sv_path = strdup(path)) == 0 )
goto EXIT;
if( (self->sv_file = open(path, mode)) == -1 ) {
if( errno == ENOENT )
mce_log(LOG_DEBUG, "%s: open: %m", path);
else
mce_log(LOG_ERR, "%s: open: %m", path);
goto EXIT;
}
mce_log(LOG_DEBUG, "%s: opened", sysfsval_path(self));
/* Note: Current value is not fetched by default */
ack = true;
EXIT:
if( !ack )
sysfsval_close(self);
return ack;
}
/** Close file associated with sysfsval_t object and forget file path
*
* @param self sysfsval_t object pointer
*/
void
sysfsval_close(sysfsval_t *self)
{
if( self->sv_file != -1 ) {
mce_log(LOG_DEBUG, "%s: closed", sysfsval_path(self));
close(self->sv_file), self->sv_file = -1;
}
free(self->sv_path), self->sv_path = 0;
}
/** Get file path associated with sysfsval_t object
*
* Note: This function is meant to be used only for diagnostic
* logging and it can be assumed to always return a valid c string.
*
* @param self sysfsval_t object pointer
*
* @return file path, or "unset"
*/
const char *
sysfsval_path(const sysfsval_t *self)
{
return self->sv_path ?: "unset";
}
/** Get integer value associated with sysfsval_t object
*
* @param self sysfsval_t object pointer
*
* @return cached content of sysfs file as integer, or -1
*/
int
sysfsval_get(const sysfsval_t *self)
{
return self->sv_curr;
}
/** Update sysfs content associated with sysfsval_t object
*
* @param self sysfsval_t object pointer
* @param value number to write to sysfs file
*
* @return false if updating sysfs content failed, true otherwise
*/
bool
sysfsval_set(sysfsval_t *self, int value)
{
bool ack = true;
int prev = self->sv_curr;
self->sv_curr = value;
if( prev == self->sv_curr )
goto EXIT;
/* If file is closed: assume it was optional and do not
* spam journal with transitions related to it */
if( self->sv_file == -1 )
goto EXIT;
mce_log(LOG_DEBUG, "%s: write: %d -> %d", sysfsval_path(self),
prev, self->sv_curr);
char data[256];
int todo = snprintf(data, sizeof data, "%d", value);
int done = write(self->sv_file, data, todo);
if( done == todo )
goto EXIT;
ack = false;
if( done == -1 )
mce_log(LOG_ERR, "%s: write: %m", sysfsval_path(self));
else
mce_log(LOG_ERR, "%s: write: partial", sysfsval_path(self));
EXIT:
return ack;
}
/** Update cached value associated with sysfsval_t object
*
* Meant to be used in cases where it is known that writing to
* one sysfs file changes also the value of some other sysfs
* control file and we want to be able to avoid unnecessary
* write when the next sysfsval_set() call is made.
*
* @param self sysfsval_t object pointer
* @param value number to write to sysfs file
*/
void
sysfsval_assume(sysfsval_t *self, int value)
{
int prev = self->sv_curr;
self->sv_curr = value;
if( prev == self->sv_curr )
goto EXIT;
/* If file is closed: assume it was optional and do not
* spam journal with transitions related to it */
if( self->sv_file == -1 )
goto EXIT;
mce_log(LOG_DEBUG, "%s: assume: %d -> %d", sysfsval_path(self),
prev, self->sv_curr);
EXIT:
return;
}
/** Invalidate cached value associated with sysfsval_t object
*
* Meant to be used in cases where it is known that the
* sysfs file needs to be written the next time sysfsval_set()
* gets called.
*
* @param self sysfsval_t object pointer
* @param value number to write to sysfs file
*/
void
sysfsval_invalidate(sysfsval_t *self)
{
int prev = self->sv_curr;
self->sv_curr = -1;
if( prev == self->sv_curr )
goto EXIT;
/* If file is closed: assume it was optional and do not
* spam journal with transitions related to it */
if( self->sv_file == -1 )
goto EXIT;
mce_log(LOG_DEBUG, "%s: invalidated", sysfsval_path(self));
EXIT:
return;
}
/** Read value from sysfs file associated with sysfsval_t object
*
* Meant to be used for obtainining initial value / in cases
* it is known that the cached value is most likely not reflecting
* the actual data available via sysfs.
*
* @param self sysfsval_t object pointer
*
* @return true if reading succeeded, false otherwise
*/
bool
sysfsval_refresh(sysfsval_t *self)
{
bool ack = false;
int value = -1;
char data[256];
if( self->sv_file == -1 )
goto EXIT;
if( lseek(self->sv_file, 0, SEEK_SET) == -1 ) {
mce_log(LOG_ERR, "%s: seek: %m", sysfsval_path(self));
goto EXIT;
}
int done = read(self->sv_file, data, sizeof data - 1);
if( done == -1 ) {
mce_log(LOG_ERR, "%s: read: %m", sysfsval_path(self));
goto EXIT;
}
if( done == 0 ) {
mce_log(LOG_ERR, "%s: read: EOF", sysfsval_path(self));
goto EXIT;
}
data[done] = 0;
value = strtol(data, 0, 0);
mce_log(LOG_DEBUG, "%s: read: %d -> %d", sysfsval_path(self),
self->sv_curr, value);
self->sv_curr = value;
ack = true;
EXIT:
if( !ack )
sysfsval_invalidate(self);
return ack;
}