Skip to content

Commit

Permalink
Export fopenat() to Compat
Browse files Browse the repository at this point in the history
  • Loading branch information
cgzones committed Aug 29, 2023
1 parent 3408256 commit 16a6ec5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
15 changes: 15 additions & 0 deletions Compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,18 @@ ssize_t Compat_readlink(openat_arg_t dirfd,

return readlink(linkPath, buf, bufsize);
}


FILE* Compat_fopenat(openat_arg_t openatArg, const char* pathname, const char* mode) {
assert(String_eq(mode, "r")); /* only currently supported mode */

int fd = Compat_openat(openatArg, pathname, O_RDONLY);
if (fd < 0)
return NULL;

FILE* stream = fdopen(fd, mode);
if (!stream)
close(fd);

return stream;
}
5 changes: 5 additions & 0 deletions Compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ in the source distribution for its full text.
#include <assert.h> // IWYU pragma: keep
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h> // IWYU pragma: keep

Expand Down Expand Up @@ -62,6 +63,10 @@ ssize_t Compat_readlink(openat_arg_t dirfd,
char* buf,
size_t bufsize);

FILE* Compat_fopenat(openat_arg_t openatArg,
const char* pathname,
const char* mode);

/*
* static_assert() hack for pre-C11
* TODO: drop after moving to -std=c11 or newer
Expand Down
30 changes: 8 additions & 22 deletions linux/LinuxProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,6 @@ in the source distribution for its full text.
#define PF_KTHREAD 0x00200000
#endif

static FILE* fopenat(openat_arg_t openatArg, const char* pathname, const char* mode) {
assert(String_eq(mode, "r")); /* only currently supported mode */

int fd = Compat_openat(openatArg, pathname, O_RDONLY);
if (fd < 0)
return NULL;

FILE* stream = fdopen(fd, mode);
if (!stream)
close(fd);

return stream;
}

static inline uint64_t fast_strtoull_dec(char** str, int maxlen) {
register uint64_t result = 0;

Expand Down Expand Up @@ -387,7 +373,7 @@ static bool LinuxProcessList_readStatusFile(Process* process, openat_arg_t procF
lp->vxid = 0;
#endif

FILE* statusfile = fopenat(procFd, "status", "r");
FILE* statusfile = Compat_fopenat(procFd, "status", "r");
if (!statusfile)
return false;

Expand Down Expand Up @@ -572,7 +558,7 @@ static void LinuxProcessList_readMaps(LinuxProcess* process, openat_arg_t procFd

proc->usesDeletedLib = false;

FILE* mapsfile = fopenat(procFd, "maps", "r");
FILE* mapsfile = Compat_fopenat(procFd, "maps", "r");
if (!mapsfile)
return;

Expand Down Expand Up @@ -677,7 +663,7 @@ static void LinuxProcessList_readMaps(LinuxProcess* process, openat_arg_t procFd
}

static bool LinuxProcessList_readStatmFile(LinuxProcess* process, openat_arg_t procFd, const LinuxMachine* host) {
FILE* statmfile = fopenat(procFd, "statm", "r");
FILE* statmfile = Compat_fopenat(procFd, "statm", "r");
if (!statmfile)
return false;

Expand All @@ -704,7 +690,7 @@ static bool LinuxProcessList_readStatmFile(LinuxProcess* process, openat_arg_t p
static bool LinuxProcessList_readSmapsFile(LinuxProcess* process, openat_arg_t procFd, bool haveSmapsRollup) {
//http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719
//kernel will return data in chunks of size PAGE_SIZE or less.
FILE* f = fopenat(procFd, haveSmapsRollup ? "smaps_rollup" : "smaps", "r");
FILE* f = Compat_fopenat(procFd, haveSmapsRollup ? "smaps_rollup" : "smaps", "r");
if (!f)
return false;

Expand Down Expand Up @@ -747,7 +733,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t
return;
}

FILE* file = fopenat(procFd, "status", "r");
FILE* file = Compat_fopenat(procFd, "status", "r");
if (!file) {
free(process->ctid);
process->ctid = NULL;
Expand Down Expand Up @@ -830,7 +816,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t
#endif

static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t procFd) {
FILE* file = fopenat(procFd, "cgroup", "r");
FILE* file = Compat_fopenat(procFd, "cgroup", "r");
if (!file) {
if (process->cgroup) {
free(process->cgroup);
Expand Down Expand Up @@ -902,7 +888,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t
}

static void LinuxProcessList_readOomData(LinuxProcess* process, openat_arg_t procFd) {
FILE* file = fopenat(procFd, "oom_score", "r");
FILE* file = Compat_fopenat(procFd, "oom_score", "r");
if (!file)
return;

Expand Down Expand Up @@ -935,7 +921,7 @@ static void LinuxProcessList_readAutogroup(LinuxProcess* process, openat_arg_t p
}

static void LinuxProcessList_readSecattrData(LinuxProcess* process, openat_arg_t procFd) {
FILE* file = fopenat(procFd, "attr/current", "r");
FILE* file = Compat_fopenat(procFd, "attr/current", "r");
if (!file) {
free(process->secattr);
process->secattr = NULL;
Expand Down

0 comments on commit 16a6ec5

Please sign in to comment.