forked from Smattr/execfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileops.c
452 lines (400 loc) · 13.7 KB
/
fileops.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* Implementations of all the FUSE operations for this file system. */
/* Use newer version of FUSE API. */
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include "entry.h"
#include "fileops.h"
#include "globals.h"
#include "log.h"
/* All the functions in this file are invoked as FUSE callbacks, which results
* in assertion failures being invisible to the user. To provide meaningful
* assertion functionality we provide our own assert() that prints failures to
* the log.
*/
#undef assert
#ifdef NDEBUG
#define assert(expr) ((void)0)
#else
#define assert(expr) do { \
if (!(expr)) { \
LOG("%s:%d: %s: Assertion `%s' failed", __FILE__, __LINE__, __func__, #expr); \
return -1; \
} \
} while(0)
#endif
#define BIT(n) (1UL << (n))
#define R BIT(2)
#define W BIT(1)
#define X BIT(0)
#define RIGHTS_MASK 0x3
/* Shell to use when opening a file read/write. */
#define SHELL "/bin/sh"
/* Whether this path is the root of the mount point. */
static int is_root(const char *path) {
return !strcmp("/", path);
}
/* Note: doing a linear search on the entries array is not an efficient way of
* implementing a file system that will be under heavy load, but we assume that
* there will be few entries in the file system and these will not be accessed
* frequently.
*/
static entry_t *find_entry(const char *path) {
if (path[0] != '/') {
/* We were passed a path outside this mount point (?) */
return NULL;
}
int i;
for (i = 0; i < entries_sz; ++i) {
if (!strcmp(path + 1, entries[i]->path)) {
return entries[i];
}
}
return NULL;
}
/* Determine the permissions of a given file in the context of the user
* currently operating on it.
*/
static unsigned int access_rights(entry_t *entry) {
struct fuse_context *context = fuse_get_context();
unsigned int rights;
if (context->uid == uid) {
rights = (entry->u_r ? R : 0)
| (entry->u_w ? W : 0)
| (entry->u_x ? X : 0);
} else if (context->gid == gid) {
rights = (entry->g_r ? R : 0)
| (entry->g_w ? W : 0)
| (entry->g_x ? X : 0);
} else {
rights = (entry->o_r ? R : 0)
| (entry->o_w ? W : 0)
| (entry->o_x ? X : 0);
}
return rights;
}
/* Called when the file system is unmounted. */
static void exec_destroy(void *private_data) {
LOG("destroy called (unmounting file system)");
log_close();
}
static int exec_flush(const char *path, struct fuse_file_info *fi) {
LOG("flush called on %s with handle %llu", path, fi->fh);
if (is_root(path)) {
return 0;
}
entry_t *e = find_entry(path);
if (e == NULL) {
return -ENOENT;
}
/* We don't need to flush at all because reading/writing is not done via
* streams.
*/
return 0;
}
static int exec_fsync(const char *path, int datasync, struct fuse_file_info *fi) {
LOG("fsync called on %s (%s)", path, datasync ? "datasync" : "metadata only");
if (is_root(path)) {
return 0;
}
entry_t *e = find_entry(path);
if (e == NULL) {
return -ENOENT;
}
/* Like flush, no need to do anything for fsync because we are doing
* unbuffered I/O.
*/
return 0;
}
/* Start of "interesting" code. The guts of the implementation are below in
* exec_getattr(), exec_open(), exec_read() and exec_write().
*/
static int exec_getattr(const char *path, struct stat *stbuf) {
LOG("getattr called on %s", path);
assert(stbuf != NULL);
/* stbuf->st_dev is ignored. */
/* stbuf->st_ino is ignored. */
/* Mark every entry as owned by the mounter. */
stbuf->st_uid = uid;
stbuf->st_gid = gid;
/* stbuf-st_rdev is irrelevant. */
/* stbuf->st_blksize is ignored. */
/* stbuf->st_blocks is ignored. */
/* The current time is as good as any considering any process reading this
* file may encounter different data to last time.
*/
stbuf->st_atime = stbuf->st_mtime = stbuf->st_ctime = time(NULL);
if (is_root(path)) {
stbuf->st_mode = S_IFDIR|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
stbuf->st_size = 0; /* FIXME: This should be set more appropriately. */
stbuf->st_nlink = 1;
} else {
entry_t *e = find_entry(path);
if (e == NULL) {
return -ENOENT;
}
/* It would be nice to mark entries as FIFOs (S_IFIFO), but
* irritatingly the kernel doesn't call FUSE handlers for FIFOs so we
* never get read/write calls.
*/
stbuf->st_mode = S_IFREG
| (e->u_r ? S_IRUSR : 0)
| (e->u_w ? S_IWUSR : 0)
| (e->u_x ? S_IXUSR : 0)
| (e->g_r ? S_IRGRP : 0)
| (e->g_w ? S_IWGRP : 0)
| (e->g_x ? S_IXGRP : 0)
| (e->o_r ? S_IROTH : 0)
| (e->o_w ? S_IWOTH : 0)
| (e->o_x ? S_IXOTH : 0);
stbuf->st_size = size;
stbuf->st_nlink = 1;
}
return 0;
}
/* Basically popen(path, "rw"), but popen doesn't let you do this. */
static int popen_rw(const char *path, uint64_t *handle) {
/* What we're going to do is create two pipes that we'll use as the read
* and write file descriptors. Stdout and stdin, repsectively, in the
* opened process need to connect to these pipes.
*/
int input[2], output[2];
if (pipe(input) != 0 || pipe(output) != 0) {
return -1;
}
/* Flush standard streams to avoid aberrations after forking. This
* shouldn't really be required as we aren't using any of these anyway.
*/
fflush(stdout); fflush(stderr); fflush(stdin);
pid_t pid = fork();
if (pid == -1) {
LOG("Failed to fork");
close(input[0]);
close(input[1]);
close(output[0]);
close(output[1]);
return -1;
} else if (pid == 0) {
/* We are the child. */
/* Close the ends of the pipe we don't need. */
close(input[1]); close(output[0]);
/* Overwrite our stdin and stdout such that they connect to the pipes.
*/
if (dup2(input[0], STDIN_FILENO) < 0 || dup2(output[1], STDOUT_FILENO) < 0) {
LOG("Failed to overwrite stdin/stdout after forking");
exit(1);
}
/* Overwrite our image with the command to execute. Note that exec will
* only return if it fails.
*/
(void)execl(SHELL, "sh", "-c", path, NULL);
LOG("Exec failed");
exit(1);
} else {
/* We are the parent. */
LOG("Forked off child %d to run %s", pid, path);
/* Close the ends of the pipe we don't need. */
close(input[0]); close(output[1]);
/* Pack the file descriptors we do need into the handle. */
assert(handle != NULL);
*handle = (((uint64_t)input[1]) << 32) | (uint64_t)output[0];
return 0;
}
assert(!"Unreachable");
}
static int exec_open(const char *path, struct fuse_file_info *fi) {
assert(fi != NULL);
LOG("open called on %s with flags %d", path, fi->flags);
entry_t *e = find_entry(path);
if (e == NULL) {
return -ENOENT;
}
unsigned int entry_rights = access_rights(e);
unsigned int rights = fi->flags & RIGHTS_MASK;
if (((rights == O_RDONLY || rights == O_RDWR) && !(entry_rights & R)) ||
((rights == O_WRONLY || rights == O_RDWR) && !(entry_rights & W))) {
return -EACCES;
}
LOG("Opening %s (%s) for %s", path, e->command,
rights == O_RDONLY ? "read" :
rights == O_WRONLY ? "write" : "read/write");
/* We're about to pack two file descriptors (ints) into a uint64_t, so let's
* check (at compile time) that they'll actually fit.
*/
typedef char _two_ints_fit_in_a_uint64_t
[sizeof(uint64_t) >= 2 * sizeof(int) ? 1 : -1];
/* Open the pipe and put the file descriptor in the low 32 bits of fi->fh
* if it's open for reading and the high 32 bits if it's open for writing.
* The reason for this is because we'll need two separate file descriptors
* to do a read/write pipe, in which case we can pack them both into
* fi->fh. Kind of handy that FUSE gives us 64 bits for a handle.
*/
FILE *f;
if (rights == O_RDONLY) {
f = popen(e->command, "r");
if (f == NULL) {
LOG("Failed to popen %s for reading", e->command);
return -EBADF;
}
fi->fh = (uint64_t)fileno(f); /* Low 32 bits. */
} else if (rights == O_WRONLY) {
f = popen(e->command, "w");
if (f == NULL) {
LOG("Failed to popen %s for writing", e->command);
return -EBADF;
}
fi->fh = ((uint64_t)fileno(f)) << 32; /* High 32 bits */
} else {
/* Opening a file for read/write is a bit more complicated because
* popen doesn't let us do this directly.
*/
if (popen_rw(e->command, &(fi->fh)) != 0) {
LOG("Failed to open %s for read/write", e->command);
return -EBADF;
}
}
LOG("Handle %llu returned from popen", fi->fh);
return 0;
}
static int exec_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
assert(fi != NULL);
LOG("read of %d bytes from %s with handle %llu", size, path, fi->fh);
/* 0 is actually a valid file descriptor, but we know it's stdin so there's
* no way it can be the one we're about to read from.
*/
assert(fi->fh != 0);
assert(size <= SSIZE_MAX); /* read() is undefined when passed >SSIZE_MAX */
ssize_t sz = read((int)(fi->fh & ((1ULL << 32) - 1)), buf, size);
if (sz == -1) {
LOG("read from %s failed with error %d", path, errno);
} else {
LOG("read from %s returned %d bytes", path, sz);
}
return sz;
}
static int exec_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
LOG("readdir called on %s", path);
if (!is_root(path)) {
/* Don't support subdirectories. */
return -EBADF;
}
int i;
for (i = offset; i < entries_sz; ++i) {
if (filler(buf, entries[i]->path, NULL, i + 1) != 0) {
return 0;
}
}
return 0;
}
static int exec_release(const char *path, struct fuse_file_info *fi) {
assert(fi != NULL);
LOG("Releasing %s with handle %llu", path, fi->fh);
assert(is_root(path) || find_entry(path) != NULL);
assert(fi->fh != 0);
int readfd = (int)(fi->fh & ((1ULL << 32) - 1));
int writefd = (int)(fi->fh >> 32);
if (readfd != 0) { /* File was opened for reading. */
(void)close(readfd);
}
if (writefd != 0) { /* File was opened for writing. */
(void)close(writefd);
}
return 0;
}
static int exec_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
assert(fi != NULL);
LOG("write of %d bytes to %s with handle %llu", size, path, fi->fh);
assert(fi->fh != 0);
assert(size <= SSIZE_MAX); /* write() is undefined when passed >SSIZE_MAX */
ssize_t sz = write((int)(fi->fh >> 32), buf, size);
if (sz == -1) {
LOG("write to %s failed with error %d", path, errno);
} else {
LOG("write to %s of %d bytes", path, sz);
}
return sz;
}
/* Stub out all the irrelevant functions. */
#define FAIL_STUB(func, args...) \
static int exec_ ## func(const char *path , ## args) { \
LOG("Fail stubbed function %s called on %s", __func__, path); \
return -EACCES; \
}
#define NOP_STUB(func, args...) \
static int exec_ ## func(const char *path , ## args) { \
LOG("No-op stubbed function %s called on %s", __func__, path); \
if (!is_root(path) && find_entry(path) == NULL) { \
return -ENOENT; \
} \
return 0; \
}
FAIL_STUB(bmap, size_t blocksize, uint64_t *idx);
FAIL_STUB(chmod, mode_t mode); /* Edit the config file to change permissions. */
FAIL_STUB(chown, uid_t uid, gid_t gid);
NOP_STUB(fsyncdir, int datasync, struct fuse_file_info *fi);
FAIL_STUB(link, const char *target);
FAIL_STUB(mkdir, mode_t mode); /* Subdirectories not supported. */
FAIL_STUB(mknod, mode_t mode, dev_t dev);
FAIL_STUB(readlink, char *buf, size_t size); /* Symlinks not supported. */
NOP_STUB(releasedir, struct fuse_file_info *fi);
FAIL_STUB(removexattr, const char *name);
FAIL_STUB(rename, const char *new_name);
FAIL_STUB(rmdir);
FAIL_STUB(setxattr, const char *name, const char *value, size_t size, int flags);
FAIL_STUB(symlink, const char *target);
NOP_STUB(truncate, off_t size);
FAIL_STUB(unlink); /* Edit the config file to remove entries. */
NOP_STUB(utime, struct utimbuf *buf);
NOP_STUB(utimens, const struct timespec tv[2]);
#undef FAIL_STUB
#undef NOP_STUB
#define OP(func) .func = &exec_ ## func
struct fuse_operations ops = {
.flag_nullpath_ok = 0, /* Don't accept NULL paths. */
// TODO access
OP(bmap),
OP(chmod),
OP(chown),
/* No need to implement create as open gets be called instead. */
OP(destroy),
// TODO fgetattr
OP(flush),
/* No need to implement ftruncate as truncate gets called instead. */
OP(fsync),
OP(fsyncdir),
OP(getattr),
// TODO getxattr
// TODO init
// TODO ioctl
OP(link),
// TODO listxattr
/* No need to implement lock. Let the kernel handle flocking. */
OP(mkdir),
OP(mknod),
OP(open),
// TODO opendir
// TODO poll
OP(read),
OP(readdir),
OP(readlink),
OP(release),
OP(releasedir),
OP(removexattr),
OP(rename),
OP(rmdir),
OP(setxattr),
// TODO statfs
OP(symlink),
OP(truncate),
OP(unlink),
OP(utime),
OP(utimens),
OP(write),
};
#undef OP