-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interfaces-plugin: add read sys, proc
- Loading branch information
andrej
committed
Oct 13, 2022
1 parent
a8c2dc6
commit 9081501
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "plugin/common.h" | ||
#include "srpc/common.h" | ||
|
||
#include <errno.h> | ||
|
||
int read_from_proc_file(const char *dir_path, char *interface, const char *fn, int *val) | ||
{ | ||
int error = 0; | ||
char tmp_buffer[PATH_MAX]; | ||
FILE *fptr = NULL; | ||
char tmp_val[2] = {0}; | ||
|
||
error = snprintf(tmp_buffer, sizeof(tmp_buffer), "%s/%s/%s", dir_path, interface, fn); | ||
if (error < 0) { | ||
// snprintf error | ||
SRPLG_LOG_ERR(PLUGIN_NAME, "snprintf failed"); | ||
goto out; | ||
} | ||
|
||
// snprintf returns return the number of bytes that are written | ||
// reset error to 0 | ||
error = 0; | ||
|
||
fptr = fopen((const char *) tmp_buffer, "r"); | ||
|
||
if (fptr != NULL) { | ||
fgets(tmp_val, sizeof(tmp_val), fptr); | ||
|
||
*val = atoi(tmp_val); | ||
|
||
fclose(fptr); | ||
} else { | ||
SRPLG_LOG_ERR(PLUGIN_NAME, "failed to open %s: %s", tmp_buffer, strerror(errno)); | ||
error = -1; | ||
goto out; | ||
} | ||
|
||
out: | ||
return error; | ||
} | ||
|
||
int read_from_sys_file(const char* dir_path, char* interface, int* val) | ||
{ | ||
int error = 0; | ||
char tmp_buffer[PATH_MAX]; | ||
FILE* fptr = NULL; | ||
char tmp_val[4] = { 0 }; | ||
|
||
error = snprintf(tmp_buffer, sizeof(tmp_buffer), "%s/%s/type", dir_path, interface); | ||
if (error < 0) { | ||
// snprintf error | ||
SRPLG_LOG_ERR(PLUGIN_NAME, "%s: snprintf failed", __func__); | ||
goto out; | ||
} | ||
|
||
/* snprintf returns return the number of bytes that are written - reset error to 0 */ | ||
error = 0; | ||
|
||
fptr = fopen((const char*)tmp_buffer, "r"); | ||
|
||
if (fptr != NULL) { | ||
fgets(tmp_val, sizeof(tmp_val), fptr); | ||
|
||
*val = atoi(tmp_val); | ||
|
||
fclose(fptr); | ||
} else { | ||
SRPLG_LOG_ERR(PLUGIN_NAME, "%s: failed to open %s: %s", __func__, tmp_buffer, strerror(errno)); | ||
error = -1; | ||
goto out; | ||
} | ||
|
||
out: | ||
return error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef READ_H | ||
#define READ_H | ||
|
||
int read_from_proc_file(const char *dir_path, char *interface, const char *fn, int *val); | ||
|
||
int read_from_sys_file(const char* dir_path, char* interface, int* val); | ||
|
||
#endif /* READ_H */ |