This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plink.c
356 lines (303 loc) · 6.44 KB
/
plink.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 2005-2011 Junjiro R. Okajima
*
* This program, aufs is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _FILE_OFFSET_BITS 64 /* ftw.h */
#define _XOPEN_SOURCE 500 /* ftw.h */
#define _GNU_SOURCE /* ftw.h */
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <ftw.h>
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/aufs_type.h>
#include "au_util.h"
/* todo: try argz? */
static struct name_array {
char *o;
int bytes;
char *cur;
int nname;
} na;
static struct ino_array {
char *o;
int bytes;
union {
char *p;
ino_t *cur;
};
int nino;
} ia;
static int na_append(char *plink_dir, char *name)
{
int l, sz;
char *p;
const int cur = na.cur - na.o;
l = strlen(plink_dir) + strlen(name) + 2;
sz = na.bytes + l;
p = realloc(na.o, sz);
if (!p)
AuFin("realloc");
na.o = p;
na.bytes = sz;
na.cur = p + cur;
na.cur += sprintf(na.cur, "%s/%s", plink_dir, name) + 1;
na.nname++;
return 0;
}
static int ia_append(ino_t ino)
{
int sz;
char *p;
const int cur = ia.p - ia.o;
sz = na.bytes + sizeof(ino_t);
p = realloc(ia.o, sz);
if (!p)
AuFin("realloc");
ia.o = p;
ia.bytes = sz;
ia.p = p + cur;
*ia.cur++ = ino;
ia.nino++;
return 0;
}
static int build_array(char *plink_dir)
{
int err;
DIR *dp;
struct dirent *de;
char *p;
ino_t ino;
err = access(plink_dir, F_OK);
if (err)
return 0;
err = 0;
dp = opendir(plink_dir);
if (!dp)
AuFin("%s", plink_dir);
while ((de = readdir(dp))) {
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
#if 0
if (de->d_type == DT_DIR) {
errno = EISDIR;
AuFin(de->d_name);
}
#endif
err = na_append(plink_dir, de->d_name);
if (err)
break;
p = strchr(de->d_name, '.');
if (!p) {
errno = EINVAL;
AuFin("internal error, %s", de->d_name);
}
*p = 0;
errno = 0;
ino = strtoull(de->d_name, NULL, 0);
if (ino == /*ULLONG_MAX*/-1 && errno == ERANGE)
AuFin("internal error, %s", de->d_name);
err = ia_append(ino);
if (err)
break;
}
closedir(dp);
return err;
}
static int ia_test(ino_t ino)
{
int i;
ino_t *p;
/* todo: hash table */
ia.p = ia.o;
p = ia.cur;
for (i = 0; i < ia.nino; i++)
if (*p++ == ino)
return 1;
return 0;
}
/* ---------------------------------------------------------------------- */
static int ftw_list(const char *fname, const struct stat *st, int flags,
struct FTW *ftw)
{
if (!strcmp(fname + ftw->base, AUFS_WH_PLINKDIR))
return FTW_SKIP_SUBTREE;
if (flags == FTW_D || flags == FTW_DNR)
return FTW_CONTINUE;
if (ia_test(st->st_ino))
puts(fname);
return FTW_CONTINUE;
}
static int ftw_cpup(const char *fname, const struct stat *st, int flags,
struct FTW *ftw)
{
int err;
if (!strcmp(fname + ftw->base, AUFS_WH_PLINKDIR))
return FTW_SKIP_SUBTREE;
if (flags == FTW_D || flags == FTW_DNR)
return FTW_CONTINUE;
/*
* do nothing but update something harmless in order to make it copyup
*/
if (ia_test(st->st_ino)) {
Dpri("%s\n", fname);
if (!S_ISLNK(st->st_mode))
err = chown(fname, -1, -1);
else
err = lchown(fname, -1, -1);
if (err)
AuFin("%s", fname);
}
return FTW_CONTINUE;
}
/* ---------------------------------------------------------------------- */
static DIR *dp;
void au_plink_maint(char *path)
{
int err;
if (path) {
if (dp) {
errno = EINVAL;
AuFin("dp is not NULL");
}
dp = opendir(path);
if (!dp)
AuFin("%s", path);
err = ioctl(dirfd(dp), AUFS_CTL_PLINK_MAINT);
#ifndef DEBUG
if (err)
AuFin("AUFS_CTL_PLINK_MAINT");
#endif
} else {
err = closedir(dp);
if (err)
AuFin("closedir");
dp = NULL;
}
}
void au_clean_plink(void)
{
int err;
err = ioctl(dirfd(dp), AUFS_CTL_PLINK_CLEAN);
#ifndef DEBUG
if (err)
AuFin("AUFS_CTL_PLINK_CLEAN");
#endif
}
static int do_plink(char *cwd, int cmd, int nbr, char *br[])
{
int err, i, l;
struct rlimit rlim;
__nftw_func_t func;
char *p;
err = 0;
switch (cmd) {
case AuPlink_FLUSH:
/*FALLTHROUGH*/
case AuPlink_CPUP:
func = ftw_cpup;
break;
case AuPlink_LIST:
func = ftw_list;
break;
default:
errno = EINVAL;
AuFin(NULL);
func = NULL; /* never reach here */
}
for (i = 0; i < nbr; i++) {
//puts(br[i]);
p = strchr(br[i], '=');
if (strcmp(p + 1, AUFS_BRPERM_RW)
&& strcmp(p + 1, AUFS_BRPERM_RWNLWH))
continue;
*p = 0;
l = strlen(br[i]);
p = malloc(l + sizeof(AUFS_WH_PLINKDIR) + 2);
if (!p)
AuFin("malloc");
sprintf(p, "%s/%s", br[i], AUFS_WH_PLINKDIR);
//puts(p);
err = build_array(p);
if (err)
AuFin("build_array");
free(p);
}
if (!ia.nino)
goto out;
if (cmd == AuPlink_LIST) {
ia.p = ia.o;
for (i = 0; i < ia.nino; i++)
printf("%llu ", (unsigned long long)*ia.cur++);
putchar('\n');
}
err = getrlimit(RLIMIT_NOFILE, &rlim);
if (err)
AuFin("getrlimit");
nftw(cwd, func, rlim.rlim_cur - 10,
FTW_PHYS | FTW_MOUNT | FTW_ACTIONRETVAL);
/* ignore */
if (cmd == AuPlink_FLUSH) {
au_clean_plink();
na.cur = na.o;
for (i = 0; i < na.nname; i++) {
Dpri("%s\n", na.cur);
err = unlink(na.cur);
if (err)
AuFin("%s", na.cur);
na.cur += strlen(na.cur) + 1;
}
}
out:
free(ia.o);
free(na.o);
return err;
}
int au_plink(char cwd[], int cmd, int begin_maint, int end_maint)
{
int err, nbr;
struct mntent ent;
char **br;
if (begin_maint)
au_plink_maint(cwd);
err = au_proc_getmntent(cwd, &ent);
if (err)
AuFin("no such mount point");
if (hasmntopt(&ent, "noplink"))
goto out; /* success */
#ifdef DEBUG
//char a[] = "a,b,br:/tmp/br0=rw:/br1=ro";
char a[] = "a,b,si=1,c";
ent.mnt_opts = a;
#endif
err = au_br(&br, &nbr, &ent);
//printf("nbr %d\n", nbr);
if (err)
AuFin(NULL);
err = do_plink(cwd, cmd, nbr, br);
if (err)
AuFin(NULL);
out:
if (end_maint)
au_plink_maint(NULL);
return err;
}