245
if(unlikely(ps->capacity->fd == -1)) {
246
error("Cannot open file '%s'", ps->capacity->filename);
247
power_supply_free(ps);
248
+ ps = NULL;
249
}
250
}
251
251
- ssize_t r = read(ps->capacity->fd, buffer, 30);
252
- if(unlikely(r < 1)) {
253
- error("Cannot read file '%s'", ps->capacity->filename);
254
- power_supply_free(ps);
255
- }
256
- else {
257
- buffer[r] = '\0';
258
- ps->capacity->value = str2ull(buffer);
259
- }
252
+ if (ps)
253
+ {
254
+ ssize_t r = read(ps->capacity->fd, buffer, 30);
255
+ if(unlikely(r < 1)) {
256
+ error("Cannot read file '%s'", ps->capacity->filename);
257
+ power_supply_free(ps);
258
+ ps = NULL;
259
+ }
260
+ else {
261
+ buffer[r] = '\0';
262
+ ps->capacity->value = str2ull(buffer);
263
261
- if(unlikely(!keep_fds_open)) {
262
- close(ps->capacity->fd);
263
- ps->capacity->fd = -1;
264
- }
265
- else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) {
266
- error("Cannot seek in file '%s'", ps->capacity->filename);
267
- close(ps->capacity->fd);
268
- ps->capacity->fd = -1;
264
+ if(unlikely(!keep_fds_open)) {
265
+ close(ps->capacity->fd);
266
+ ps->capacity->fd = -1;
267
+ }
268
+ else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) {
269
+ error("Cannot seek in file '%s'", ps->capacity->filename);
270
+ close(ps->capacity->fd);
271
+ ps->capacity->fd = -1;
272
+ }
273
+ }
274
}
275
}
276
277
// read property files
278
int read_error = 0;
279
struct ps_property *pr;
275
- for(pr = ps->property_root; pr && !read_error; pr = pr->next) {
276
- struct ps_property_dim *pd;
277
- for(pd = pr->property_dim_root; pd; pd = pd->next) {
278
- if(likely(!pd->always_zero)) {
279
- char buffer[30 + 1];
280
-
281
- if(unlikely(pd->fd == -1)) {
282
- pd->fd = open(pd->filename, O_RDONLY, 0666);
280
+ if (ps)
281
+ {
282
+ for(pr = ps->property_root; pr && !read_error; pr = pr->next) {
283
+ struct ps_property_dim *pd;
284
+ for(pd = pr->property_dim_root; pd; pd = pd->next) {
285
+ if(likely(!pd->always_zero)) {
286
+ char buffer[30 + 1];
287
+
288
if(unlikely(pd->fd == -1)) {
284
- error("Cannot open file '%s'", pd->filename);
289
+ pd->fd = open(pd->filename, O_RDONLY, 0666);
290
+ if(unlikely(pd->fd == -1)) {
291
+ error("Cannot open file '%s'", pd->filename);
292
+ read_error = 1;
293
+ power_supply_free(ps);
294
+ break;
295
+ }
296
+ }
297
+
298
+ ssize_t r = read(pd->fd, buffer, 30);
299
+ if(unlikely(r < 1)) {
300
+ error("Cannot read file '%s'", pd->filename);
301
read_error = 1;
302
power_supply_free(ps);
303
break;
304
}
289
- }
290
-
291
- ssize_t r = read(pd->fd, buffer, 30);
292
- if(unlikely(r < 1)) {
293
- error("Cannot read file '%s'", pd->filename);
294
- read_error = 1;
295
- power_supply_free(ps);
296
- break;
297
- }
298
- buffer[r] = '\0';
299
- pd->value = str2ull(buffer);
305
+ buffer[r] = '\0';
306
+ pd->value = str2ull(buffer);
307
301
- if(unlikely(!keep_fds_open)) {
302
- close(pd->fd);
303
- pd->fd = -1;
304
- }
305
- else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) {
306
- error("Cannot seek in file '%s'", pd->filename);
307
- close(pd->fd);
308
- pd->fd = -1;
308
+ if(unlikely(!keep_fds_open)) {
309
+ close(pd->fd);
310
+ pd->fd = -1;
311
+ }
312
+ else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) {
313
+ error("Cannot seek in file '%s'", pd->filename);
314
+ close(pd->fd);
315
+ pd->fd = -1;
316
+ }
317
}
318
}
319
}