Create recyclix-flow.svg
[hband-ld-preload-libs.git] / src / ignore_read_lock.c
blob7126519e4d2d4614563ef42e2f9fce27211c4956
1 /*
2 * gcc -ldl -D _GNU_SOURCE -shared -fPIC -o ignore_read_lock.so ignore_read_lock.c
4 */
6 #include <dlfcn.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <err.h>
13 #include <limits.h>
14 #include <fcntl.h>
15 #include <stdarg.h>
17 #define SUCCESS 0
18 #define MIN(a,b) (((a)<(b))?(a):(b))
21 int fcntl(int fd, int cmd, ...)
23 va_list vargs;
24 va_start(vargs, cmd);
25 void* arg = va_arg(vargs, void*);
26 void* arg2 = va_arg(vargs, void*);
27 va_end(vargs);
28 struct flock *lk = arg;
30 int (*orig_fcntl)(int, int, ...) = dlsym(RTLD_NEXT, __func__);
31 int result = orig_fcntl(fd, cmd, arg, arg2);
33 //warnx("cmd = %d l_type = %d result = %d errno = %d", cmd, lk->l_type, result, errno);
35 if(cmd == F_SETLK64 && lk->l_type == F_RDLCK && result != SUCCESS && errno == EAGAIN)
37 char linkpath[PATH_MAX];
38 char path[PATH_MAX];
39 sprintf(linkpath, "/proc/self/fd/%d", fd);
40 ssize_t sz = readlink(linkpath, path, PATH_MAX);
41 path[MIN(sz, PATH_MAX-1)] = '\0';
42 warnx("%s: failed to acquire read lock, passing by", path);
43 result = SUCCESS;
45 return result;