Created
March 11, 2026 22:20
-
-
Save pnf/b74694da3cc29c0ab3bd47b32d61fd08 to your computer and use it in GitHub Desktop.
intercept open
This file contains hidden or 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
| #define _GNU_SOURCE | |
| #include <dlfcn.h> | |
| #include <fcntl.h> | |
| #include <stdarg.h> | |
| #include <string.h> | |
| // Target path of your FIFO | |
| static const char *FIFO_PATH = NULL; | |
| typedef int (*orig_openat_t)(int dirfd, const char *pathname, int flags, ...); | |
| int openat(int dirfd, const char *pathname, int flags, ...) { | |
| orig_openat_t orig = (orig_openat_t)dlsym(RTLD_NEXT, "openat"); | |
| // If O_EXCL is set and this is our FIFO path, strip O_EXCL | |
| if (!FIFO_PATH) FIFO_PATH = getenv("JMAP_FIFO_PATH"); | |
| if (FIFO_PATH && (flags & O_EXCL) && pathname && | |
| strcmp(pathname, FIFO_PATH) == 0) { | |
| flags &= ~O_EXCL; | |
| flags &= ~O_TRUNC; // don't truncate a FIFO either | |
| } | |
| if (flags & (O_CREAT | O_TMPFILE)) { | |
| va_list ap; | |
| va_start(ap, flags); | |
| mode_t mode = va_arg(ap, mode_t); | |
| va_end(ap); | |
| return orig(dirfd, pathname, flags, mode); | |
| } | |
| return orig(dirfd, pathname, flags); | |
| } | |
| // glibc's open() calls openat() internally on modern systems, | |
| // but intercept it too for safety | |
| int open(const char *pathname, int flags, ...) { | |
| if (flags & (O_CREAT | O_TMPFILE)) { | |
| va_list ap; | |
| va_start(ap, flags); | |
| mode_t mode = va_arg(ap, mode_t); | |
| va_end(ap); | |
| return openat(AT_FDCWD, pathname, flags, mode); | |
| } | |
| return openat(AT_FDCWD, pathname, flags); | |
| } | |
| int open64(const char *pathname, int flags, ...) { | |
| if (flags & (O_CREAT | O_TMPFILE)) { | |
| va_list ap; | |
| va_start(ap, flags); | |
| mode_t mode = va_arg(ap, mode_t); | |
| va_end(ap); | |
| return openat(AT_FDCWD, pathname, flags, mode); | |
| } | |
| return openat(AT_FDCWD, pathname, flags); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment