summaryrefslogtreecommitdiffstats
path: root/linux/Platform.c
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2020-11-25 12:42:36 +0100
committerBenBE <BenBE@geshi.org>2020-12-02 20:39:36 +0100
commitf7a89529330044f4e2a38e85a88ec90f839ae64e (patch)
tree23b187477ea294d9a0f7d0557c3a51acdd0cbdd0 /linux/Platform.c
parent1d8192c10b4e149c7f9126f00534f6a8488d10c4 (diff)
Add xReadfile wrapper for reading small to medium size files
Inspired by proposed Linux syscall Avoid file descriptor leaks like 4af8c63f
Diffstat (limited to 'linux/Platform.c')
-rw-r--r--linux/Platform.c59
1 files changed, 12 insertions, 47 deletions
diff --git a/linux/Platform.c b/linux/Platform.c
index ff4fcf69..1462f82e 100644
--- a/linux/Platform.c
+++ b/linux/Platform.c
@@ -717,28 +717,6 @@ static void Platform_Battery_getProcData(double* percent, ACPresence* isOnAC) {
// READ FROM /sys
// ----------------------------------------
-static inline ssize_t xread(int fd, void* buf, size_t count) {
- // Read some bytes. Retry on EINTR and when we don't get as many bytes as we requested.
- size_t alreadyRead = 0;
- for (;;) {
- ssize_t res = read(fd, buf, count);
- if (res == -1) {
- if (errno == EINTR)
- continue;
- return -1;
- }
-
- if (res > 0) {
- buf = ((char*)buf) + res;
- count -= res;
- alreadyRead += res;
- }
-
- if (count == 0 || res == 0)
- return alreadyRead;
- }
-}
-
static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
*percent = NAN;
@@ -760,31 +738,22 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
char filePath[256];
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/type", entryName);
- int fd1 = open(filePath, O_RDONLY);
- if (fd1 == -1)
- continue;
char type[8];
- ssize_t typelen = xread(fd1, type, 7);
- close(fd1);
- if (typelen < 1)
+ ssize_t r = xReadfile(filePath, type, sizeof(type));
+ if (r < 3)
continue;
if (type[0] == 'B' && type[1] == 'a' && type[2] == 't') {
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
- int fd2 = open(filePath, O_RDONLY);
- if (fd2 == -1) {
- closedir(dir);
- return;
- }
+
char buffer[1024];
- ssize_t buflen = xread(fd2, buffer, 1023);
- close(fd2);
- if (buflen < 1) {
+ r = xReadfile(filePath, buffer, sizeof(buffer));
+ if (r < 0) {
closedir(dir);
return;
}
- buffer[buflen] = '\0';
+
char* buf = buffer;
char* line = NULL;
bool full = false;
@@ -836,19 +805,15 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
continue;
xSnprintf(filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
- int fd3 = open(filePath, O_RDONLY);
- if (fd3 == -1) {
+
+ char buffer[2];
+
+ r = xReadfile(filePath, buffer, sizeof(buffer));
+ if (r < 1) {
closedir(dir);
return;
}
- char buffer[2] = "";
- for (;;) {
- ssize_t res = read(fd3, buffer, 1);
- if (res == -1 && errno == EINTR)
- continue;
- break;
- }
- close(fd3);
+
if (buffer[0] == '0')
*isOnAC = AC_ABSENT;
else if (buffer[0] == '1')

© 2014-2024 Faster IT GmbH | imprint | privacy policy