Skip to content

Instantly share code, notes, and snippets.

@vi
Created July 22, 2015 22:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vi/5451a2875d6e22fe3d46 to your computer and use it in GitHub Desktop.
Inhibit seek failures to force programs to continue when writing to pipes/sockets, LD_PRELOAD-based
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dlfcn.h>
#include <errno.h>
static int (*orig_llseek)(
unsigned int fd, unsigned long offset_high,
unsigned long offset_low, loff_t *result,
unsigned int whence) = NULL;
static int (*orig_fseek)(
FILE *stream, long offset, int whence
) = NULL;
int _llseek(unsigned int fd, unsigned long offset_high,
unsigned long offset_low, loff_t *result,
unsigned int whence) {
if(!orig_llseek) {
orig_llseek = dlsym(RTLD_NEXT, "_llseek");
}
int ret = (*orig_llseek)(fd, offset_high, offset_low, result, whence);
if (ret) {
fprintf(stderr, "Inhibited seek of %d to %llx\n", fd,
(unsigned long long)offset_high * 0x100000000LL + offset_low);
}
return 0;
}
int fseek(FILE *stream, long offset, int whence) {
if(!orig_fseek) {
orig_fseek = dlsym(RTLD_NEXT, "fseek");
}
int ret = (*orig_fseek)(stream, offset, whence);
if (ret) {
fprintf(stderr, "Inhibited fseek of %p to %lx (%d)\n", stream, offset, whence);
}
errno=0;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment