-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibc-opendir-hack.c
139 lines (120 loc) · 2.71 KB
/
libc-opendir-hack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <glob.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#define INIT(x) real_ ## x = dlsym(RTLD_NEXT, #x); \
if (!real_ ## x) { \
fprintf(stderr, "Would the real " #x " please stand up? %s\n", dlerror()); \
exit(1); \
}
DIR *opendir(const char *name)
{
int fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE);
if (fd == -1)
return NULL;
return fdopendir(fd);
}
DIR *__opendir(const char *name)
{
return opendir(name);
}
static int (*real_glob)(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob);
int glob(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob)
{
if (!(flags & GLOB_ALTDIRFUNC)) {
pglob->gl_closedir = closedir;
pglob->gl_readdir = readdir;
pglob->gl_opendir = opendir;
pglob->gl_lstat = lstat;
pglob->gl_stat = stat;
flags |= GLOB_ALTDIRFUNC;
}
if (!real_glob) {
INIT(glob)
}
return real_glob(pattern, flags, errfunc, pglob);
}
#define PWD_LOCKFILE "/etc/.pwd.lock"
static int lock_fd = -1;
/* FIXME: Ignores multi-thread issues.
* Doesn't wait for the file to become lockable
*/
int lckpwdf(void)
{
struct flock fl = { 0 };
/* This process already holds the lock */
if (lock_fd != -1)
return -1;
lock_fd = open(PWD_LOCKFILE, O_WRONLY|O_CREAT, 0600);
if (lock_fd == -1)
return -1;
if (fcntl(lock_fd, F_SETFD, fcntl(lock_fd, F_GETFD, 0) | FD_CLOEXEC) == -1) {
close(lock_fd);
return -1;
}
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
return fcntl(lock_fd, F_SETLKW, &fl);
}
int ulckpwdf(void)
{
int result;
if (lock_fd == -1)
return -1;
result = close(lock_fd);
lock_fd = -1;
return result;
}
static (*real_open)(const char *name, int flags, ...);
int open(const char *name, int flags, ...)
{
mode_t mode;
if (flags & O_CREAT) {
va_list va;
va_start(va, flags);
mode = va_arg(va, mode_t);
va_end(va);
}
if (!real_open) {
INIT(open)
}
return real_open(name, flags, mode);
}
static FILE *(*real_fopen)(const char *name, const char *flags);
FILE *fopen(const char *name, const char *flags)
{
char *str, *ptr = strchr(flags, 'e');
FILE *ret;
if (ptr) {
str = strdup(flags);
ptr = (str + (ptr - flags));
strcpy(ptr, ptr + 1);
}
else
str = flags;
if (!real_fopen) {
INIT(fopen)
}
ret = real_fopen(name, str);
if (ptr)
free(str);
return ret;
}
static void _init() __attribute__((constructor));
static void _init()
{
INIT(glob)
INIT(open)
INIT(fopen)
}