14 #define INIT(x) real_ ## x = dlsym(RTLD_NEXT, #x); \
16 fprintf(stderr, "Would the real " #x " please stand up? %s\n", dlerror()); \
20 DIR *opendir(const char *name)
22 int fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE);
28 DIR *__opendir(const char *name)
33 static int (*real_glob)(const char *pattern, int flags,
34 int (*errfunc) (const char *epath, int eerrno),
37 int glob(const char *pattern, int flags,
38 int (*errfunc) (const char *epath, int eerrno),
41 if (!(flags & GLOB_ALTDIRFUNC)) {
42 pglob->gl_closedir = closedir;
43 pglob->gl_readdir = readdir;
44 pglob->gl_opendir = opendir;
45 pglob->gl_lstat = lstat;
46 pglob->gl_stat = stat;
47 flags |= GLOB_ALTDIRFUNC;
52 return real_glob(pattern, flags, errfunc, pglob);
55 #define PWD_LOCKFILE "/etc/.pwd.lock"
57 static int lock_fd = -1;
59 /* FIXME: Ignores multi-thread issues.
60 * Doesn't wait for the file to become lockable
64 struct flock fl = { 0 };
66 /* This process already holds the lock */
70 lock_fd = open(PWD_LOCKFILE, O_WRONLY|O_CREAT, 0600);
74 if (fcntl(lock_fd, F_SETFD, fcntl(lock_fd, F_GETFD, 0) | FD_CLOEXEC) == -1) {
80 fl.l_whence = SEEK_SET;
81 return fcntl(lock_fd, F_SETLKW, &fl);
91 result = close(lock_fd);
96 static (*real_open)(const char *name, int flags, ...);
97 int open(const char *name, int flags, ...)
100 if (flags & O_CREAT) {
103 mode = va_arg(va, mode_t);
109 return real_open(name, flags, mode);
112 static FILE *(*real_fopen)(const char *name, const char *flags);
113 FILE *fopen(const char *name, const char *flags)
115 char *str, *ptr = strchr(flags, 'e');
119 ptr = (str + (ptr - flags));
120 strcpy(ptr, ptr + 1);
127 ret = real_fopen(name, str);
133 static void _init() __attribute__((constructor));