Docs · I/O adapters
Intercept STDIO streams.
The STDIO interceptor routes fopen, fread, fwrite, fseek and the other unformatted stream calls on clio:: paths to the CLIO filesystem module. Like the POSIX interceptor, it is preloaded into an unmodified program; unlike it, it covers only part of the stream API, so check the list below against what your program calls.
- Library
- libclio_cte_stdio.so
- Build
- CLIO_CTE_ENABLE_STDIO_ADAPTER=ON CLIO_CORE_ENABLE_ELF=ON
- Load
- LD_PRELOAD
- Platform
- Linux
- Install
- Source builds: Spack (+stdio) or CMake
How it works
When fopen is given a path with a clio:: component, the interceptor opens the file in the clio_cte_filesystem module and returns a FILE * that is really an opaque token for that descriptor. The program only ever hands the token back to the stream functions, which the interceptor also defines, so it never notices. Every other fopen, and every call on an ordinary stream such as stdout, goes to the real C library. Files land in the same clio-fs namespace as the POSIX interceptor and Context FS use, with the marker removed: clio::/demo/log.txt is /demo/log.txt.
1. Build it
$ cmake -S . -B build -DCLIO_CORE_ENABLE_ELF=ON -DCLIO_CTE_ENABLE_STDIO_ADAPTER=ON
$ cmake --build build -j"$(nproc)"
$ ls build/bin/libclio_cte_stdio.soThe option is off by default and needs ELF support (libelf). It is only available from a source build, with Spack (+stdio) or CMake; see Install.
2. Start a runtime
$ build/bin/clio-run start &As for the POSIX interceptor, the runtime's configuration must compose a clio_cte_filesystem pool; the default configuration does. CLIO_CFS_POOL selects a pool by another name or by id.
3. Run a program under the interceptor
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
const char *path = argc > 1 ? argv[1] : "clio::/demo/log.txt";
FILE *f = fopen(path, "w");
if (!f) { perror("fopen"); return 1; }
const char line[] = "step 1 done\n";
fwrite(line, 1, strlen(line), f);
fputs("step 2 done\n", f);
fclose(f);
char buf[64];
f = fopen(path, "r");
if (!f) { perror("fopen"); return 1; }
while (fgets(buf, sizeof(buf), f) != NULL) { /* test the return value, not feof() */
printf("read: %s", buf);
}
fclose(f);
return 0;
}$ cc -o hello_stdio hello_stdio.c
$ LD_PRELOAD=$PWD/build/bin/libclio_cte_stdio.so ./hello_stdio clio::/demo/log.txt
read: step 1 done
read: step 2 doneA program that mixes FILE * streams and raw descriptors on the same clio:: file needs both interceptors: LD_PRELOAD="$PWD/build/bin/libclio_cte_stdio.so $PWD/build/bin/libclio_cte_posix.so".
What is intercepted
- Open and close
fopen,fopen64,fdopen(on a descriptor the POSIX interceptor returned),freopen,freopen64,fclose. Modesr,w,aand their+forms map to the usualopenflags.- Write
fwrite,fputc,putc,putw,fputs.- Read
fread,fgetc,getc,getw,fgets.- Position
fseek,fseeko,fseeko64,ftell,rewind,fgetpos,fgetpos64,fsetpos,fsetpos64.- Other
fflush, which on an intercepted stream is a durability barrier likefsync(the file's bytes are put on a persistent device).filenoreturns the CLIO descriptor (8192 or above), which only the POSIX interceptor understands.
Limits
An intercepted FILE * is not a real C library stream. Functions the interceptor does not define receive the token and misbehave. That includes formatted I/O (fprintf, vfprintf, fscanf), feof, ferror, clearerr, ungetc and setvbuf. Use fputs/fwrite (format into a buffer with snprintf first) and check return values instead of feof.
- Fortified builds. The interceptor does not define the checked variants (
__fgets_chk,__fread_chk) that glibc substitutes forfgetsandfreadwhen a program is compiled with_FORTIFY_SOURCEand optimization, so those calls are not intercepted. Build the program without_FORTIFY_SOURCE(add-U_FORTIFY_SOURCEwhere the toolchain enables it by default). - No user-space buffering. Each call is a request to the filesystem module, so writing one character at a time with
fputccosts one request per character. Write whole buffers. - Writes are synchronous and report their own errors, as in the POSIX interceptor.
freopenonly applies to a stream that is already intercepted, and only onto anotherclio::path. Reopeningstdoutonto aclio::path goes to the realfreopen.- Linux only, dynamically linked programs only, and only
clio::paths are redirected.
Where to go next
- POSIX interceptor: descriptors,
statandfsync. - Configuration reference.