flushpl

LD_PRELOAD hook to force fflush
git clone https://git.sinitax.com/sinitax/flushpl
Log | Files | Refs | LICENSE | sfeed.txt

commit 2ee44627eff9037e50c4c508a1fcc83322850345
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 19 Sep 2022 18:04:49 +0200

Working preload

Diffstat:
A.gitignore | 2++
AMakefile | 5+++++
Aflush.c | 23+++++++++++++++++++++++
Atest.c | 7+++++++
4 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +flush.so +test diff --git a/Makefile b/Makefile @@ -0,0 +1,5 @@ +CFLAGS = -nostartfiles -fPIC -shared --std=gnu99 +LDLIBS = -ldl + +flush.so: flush.c + $(CC) -o $@ $< $(CFLAGS) $(LDLIBS) diff --git a/flush.c b/flush.c @@ -0,0 +1,23 @@ +#include <dlfcn.h> + +#include <stdlib.h> +#include <stdio.h> + +size_t +fwrite(const void *src, size_t size, size_t count, FILE *file) +{ + static size_t (*func)(const void *, size_t, size_t, FILE *) = NULL; + static void *libc = NULL; + size_t ret; + + if (!libc) libc = dlopen("libc.so.6", RTLD_GLOBAL | RTLD_LAZY); + if (!func) func = dlsym(libc, "fwrite"); + + if (!libc || !func) exit(1); + + ret = (*func)(src, size, count, file); + fflush(file); + + return ret; +} + diff --git a/test.c b/test.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int +main() +{ + fwrite("Hello\n", 6, 1, stdout); +}