-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterceptrename.c
161 lines (137 loc) · 4.25 KB
/
interceptrename.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#define _XOPEN_SOURCE 500
#include <errno.h>
#include <glob.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <syscall.h>
#include <unistd.h>
#include <libsyscall_intercept_hook_point.h>
static int hook(long syscall_number,
long arg0, long arg1,
long arg2, long arg3,
long arg4, long arg5,
long *result)
{
int retry, start_errno;
if (syscall_number != SYS_rename)
return 1;
retry = true;
start_errno = errno;
*result = syscall_no_intercept(syscall_number, arg0, arg1);
while (retry)
{
retry = false;
if (*result == -EACCES)
{
char path[PATH_MAX];
size_t pathlen;
if (NULL == realpath((char*)arg0, path))
{
return 0; /* error canonicalizing -> real EACCESS */
}
pathlen = strlen(path);
{
struct stat statbuf;
if (stat((char*)arg0, &statbuf) != 0)
return 0; /* couldn't stat path -> real EACCES */
if (! S_ISDIR(statbuf.st_mode))
return 0; /* not a directory -> real EACCES */
}
/* search for open filehandles in running processes */
size_t procidx;
glob_t procs = { .gl_pathc = 0 };
/* enumerate processes */
glob("/proc/*", GLOB_NOSORT, NULL, &procs);
for (procidx = 0; !retry && procidx < procs.gl_pathc; ++ procidx)
{
size_t fdidx;
char * procpath = procs.gl_pathv[procidx];
/* extract pid from /proc dirname */
unsigned long pid = atol( procpath + sizeof("/proc") );
/* convert proc path to proc fd path */
size_t fdpathbuflen = strlen(procpath) + sizeof("/fd/*");
char fdpathbuf[fdpathbuflen];
glob_t fds = { .gl_pathc = 0, .gl_offs = 1 };
strncpy(fdpathbuf, procpath, fdpathbuflen);
strncat(fdpathbuf, "/fd/*", sizeof("/fd/*"));
/* enumerate open fds for proc */
glob(fdpathbuf, GLOB_NOSORT | GLOB_DOOFFS, NULL, &fds);
strncpy(fdpathbuf, procpath, fdpathbuflen);
strncat(fdpathbuf, "/cwd", sizeof("/cwd"));
fds.gl_pathv[0] = fdpathbuf; /* check cwd too */
for (fdidx = 0; fdidx < fds.gl_pathc + fds.gl_offs; ++ fdidx)
{
char cmppath[PATH_MAX];
if (NULL == realpath(fds.gl_pathv[fdidx], cmppath))
continue;
if (0 == strncmp(path, cmppath, pathlen) && '\0' != cmppath[pathlen])
{
/* open file is within dir */
FILE * tty = fopen("/dev/tty", "w");
fprintf(tty,
"\n"
"########################################################################\n"
"# WSL Directory pinned ( https://github.com/Microsoft/WSL/issues/1529 )\n"
"# Directory: %s\n"
"# Process holding: %ld\n"
"# File held: %s\n"
"# Waiting 15 seconds, then trying again ...\n"
"########################################################################\n",
path, pid, cmppath);
sleep(15);
fprintf(tty,
"########################################################################\n"
"# Retrying system call ...\n"
"########################################################################\n"
);
fclose(tty);
retry = true;
break;
}
}
globfree(&fds);
}
globfree(&procs);
/* ensure at least two tries, in case file was rapidly closed before processes were walked */
{
FILE * tty = fopen("/dev/tty", "w");
if (! retry)
{
fprintf(tty,
"\n"
"########################################################################\n"
"# System call gave -EACCES, but no files open within dir found in /proc.\n"
"# Directory: %s\n"
"# Retrying system call ...\n"
"########################################################################\n",
path
);
}
errno = start_errno;
*result = syscall_no_intercept(syscall_number, arg0, arg1);
if (*result != -EACCES)
{
fprintf(tty,
"\n"
"########################################################################\n"
"# https://github.com/Microsoft/WSL/issues/1529\n"
"# System call gave -EACCES on first try, but not on retry.\n"
"# Directory: %s\n"
"########################################################################\n",
path
);
}
fclose(tty);
}
}
}
return 0;
}
static __attribute__((constructor)) void init()
{
intercept_hook_point = hook;
}