Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions XUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ in the source distribution for its full text.
#error "Must have #include \"config.h\" line at the top of the file that includes these XUtils helper functions"
#endif

#include <dirent.h>
#include <stdbool.h>
#include <stddef.h> // IWYU pragma: keep
#include <stdio.h>
Expand Down Expand Up @@ -152,4 +153,20 @@ unsigned int countTrailingZeros(unsigned int x);
/* IEC unit prefixes */
static const char unitPrefixes[] = { 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q' };

static inline bool skipEndOfLine(FILE* fp) {
char buffer[1024];
while (fgets(buffer, sizeof(buffer), fp)) {
if (strchr(buffer, '\n')) {
return true;
}
}
return false;
}

static inline int xDirfd(DIR* dirp) {
int r = dirfd(dirp);
assert(r >= 0);
return r;
}

#endif
14 changes: 8 additions & 6 deletions linux/LinuxMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void LinuxMachine_updateCPUcount(LinuxMachine* this) {
continue;

#ifdef HAVE_OPENAT
int cpuDirFd = openat(dirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW);
int cpuDirFd = openat(xDirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW);
Comment thread
BenBE marked this conversation as resolved.
if (cpuDirFd < 0)
continue;
#else
Expand Down Expand Up @@ -497,11 +497,13 @@ static void LinuxMachine_scanCPUTime(LinuxMachine* this) {

this->period = (double)this->cpuData[0].totalPeriod / super->activeCPUs;

char buffer[PROC_LINE_LENGTH + 1];
while (fgets(buffer, sizeof(buffer), file)) {
if (String_startsWith(buffer, "procs_running")) {
this->runningTasks = strtoul(buffer + strlen("procs_running"), NULL, 10);
break;
if (!ferror(file) && !feof(file)) {
char buffer[PROC_LINE_LENGTH + 1];
while (fgets(buffer, sizeof(buffer), file)) {
Comment thread
BenBE marked this conversation as resolved.
if (String_startsWith(buffer, "procs_running")) {
this->runningTasks = (unsigned int) strtoul(buffer + strlen("procs_running"), NULL, 10);
break;
}
}
}

Expand Down
16 changes: 6 additions & 10 deletions linux/LinuxProcessTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,11 @@ static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t
while (fgets(buffer, sizeof(buffer), fp)) {
if (!strchr(buffer, '\n')) {
// Partial line, skip to end of this line
while (fgets(buffer, sizeof(buffer), fp)) {
if (strchr(buffer, '\n')) {
break;
}
if (!skipEndOfLine(fp)) {
fclose(fp);
Comment thread
BenBE marked this conversation as resolved.
return false;
}
continue;

}

if (String_startsWith(buffer, "Pss:")) {
Expand Down Expand Up @@ -840,12 +839,9 @@ static void LinuxProcessTable_readOpenVZData(LinuxProcess* process, openat_arg_t
while (fgets(linebuf, sizeof(linebuf), file) != NULL) {
if (strchr(linebuf, '\n') == NULL) {
// Partial line, skip to end of this line
while (fgets(linebuf, sizeof(linebuf), file) != NULL) {
if (strchr(linebuf, '\n') != NULL) {
break;
}
if (!skipEndOfLine(file)) {
break;
}
continue;
}

char* name_value_sep = strchr(linebuf, ':');
Expand Down
6 changes: 3 additions & 3 deletions linux/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ char* Platform_getProcessEnv(pid_t pid) {
size += bytes;
capacity += 4096;
env = xRealloc(env, capacity);
} while ((bytes = fread(env + size, 1, capacity - size, fp)) > 0);
} while (!ferror(fp) && !feof(fp) && (bytes = fread(env + size, 1, capacity - size, fp)) > 0);

fclose(fp);

Expand Down Expand Up @@ -580,7 +580,7 @@ void Platform_getPressureStall(const char* file, bool some, double* ten, double*
return;
}
int total = fscanf(fp, "some avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred);
if (!some) {
if (total != EOF && !some) {
total = fscanf(fp, "full avg10=%32lf avg60=%32lf avg300=%32lf total=%*f ", ten, sixty, threehundred);
}
(void) total;
Expand Down Expand Up @@ -785,7 +785,7 @@ static void Platform_Battery_getSysData(double* percent, ACPresence* isOnAC) {
const char* entryName = dirEntry->d_name;

#ifdef HAVE_OPENAT
int entryFd = openat(dirfd(dir), entryName, O_DIRECTORY | O_PATH);
int entryFd = openat(xDirfd(dir), entryName, O_DIRECTORY | O_PATH);
if (entryFd < 0)
continue;
#else
Expand Down