1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2019 The FreeBSD Foundation
5 *
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 extern "C" {
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/wait.h>
37
38 #include <dirent.h>
39 #include <fcntl.h>
40 #include <grp.h>
41 #include <pwd.h>
42 #include <semaphore.h>
43 #include <unistd.h>
44 }
45
46 #include <gtest/gtest.h>
47
48 #include "mockfs.hh"
49 #include "utils.hh"
50
51 using namespace testing;
52
53 /*
54 * The default max_write is set to this formula in libfuse, though
55 * individual filesystems can lower it. The "- 4096" was added in
56 * commit 154ffe2, with the commit message "fix".
57 */
58 const uint32_t libfuse_max_write = 32 * getpagesize() + 0x1000 - 4096;
59
60 /* Check that fusefs(4) is accessible and the current user can mount(2) */
check_environment()61 void check_environment()
62 {
63 const char *devnode = "/dev/fuse";
64 const char *bsdextended_node = "security.mac.bsdextended.enabled";
65 int bsdextended_val = 0;
66 size_t bsdextended_size = sizeof(bsdextended_val);
67 int bsdextended_found;
68 const char *usermount_node = "vfs.usermount";
69 int usermount_val = 0;
70 size_t usermount_size = sizeof(usermount_val);
71 if (eaccess(devnode, R_OK | W_OK)) {
72 if (errno == ENOENT) {
73 GTEST_SKIP() << devnode << " does not exist";
74 } else if (errno == EACCES) {
75 GTEST_SKIP() << devnode <<
76 " is not accessible by the current user";
77 } else {
78 GTEST_SKIP() << strerror(errno);
79 }
80 }
81 // mac_bsdextended(4), when enabled, generates many more GETATTR
82 // operations. The fusefs tests' expectations don't account for those,
83 // and adding extra code to handle them obfuscates the real purpose of
84 // the tests. Better just to skip the fusefs tests if mac_bsdextended
85 // is enabled.
86 bsdextended_found = sysctlbyname(bsdextended_node, &bsdextended_val,
87 &bsdextended_size, NULL, 0);
88 if (bsdextended_found == 0 && bsdextended_val != 0)
89 GTEST_SKIP() <<
90 "The fusefs tests are incompatible with mac_bsdextended.";
91 ASSERT_EQ(sysctlbyname(usermount_node, &usermount_val, &usermount_size,
92 NULL, 0),
93 0);
94 if (geteuid() != 0 && !usermount_val)
95 GTEST_SKIP() << "current user is not allowed to mount";
96 }
97
cache_mode_to_s(enum cache_mode cm)98 const char *cache_mode_to_s(enum cache_mode cm) {
99 switch (cm) {
100 case Uncached:
101 return "Uncached";
102 case Writethrough:
103 return "Writethrough";
104 case Writeback:
105 return "Writeback";
106 case WritebackAsync:
107 return "WritebackAsync";
108 default:
109 return "Unknown";
110 }
111 }
112
is_unsafe_aio_enabled(void)113 bool is_unsafe_aio_enabled(void) {
114 const char *node = "vfs.aio.enable_unsafe";
115 int val = 0;
116 size_t size = sizeof(val);
117
118 if (sysctlbyname(node, &val, &size, NULL, 0)) {
119 perror("sysctlbyname");
120 return (false);
121 }
122 return (val != 0);
123 }
124
125 class FuseEnv: public Environment {
SetUp()126 virtual void SetUp() {
127 check_environment();
128 }
129 };
130
SetUp()131 void FuseTest::SetUp() {
132 const char *maxbcachebuf_node = "vfs.maxbcachebuf";
133 const char *maxphys_node = "kern.maxphys";
134 size_t size;
135
136 size = sizeof(m_maxbcachebuf);
137 ASSERT_EQ(0, sysctlbyname(maxbcachebuf_node, &m_maxbcachebuf, &size,
138 NULL, 0)) << strerror(errno);
139 size = sizeof(m_maxphys);
140 ASSERT_EQ(0, sysctlbyname(maxphys_node, &m_maxphys, &size, NULL, 0))
141 << strerror(errno);
142 /*
143 * Set the default max_write to a distinct value from MAXPHYS to catch
144 * bugs that confuse the two.
145 */
146 if (m_maxwrite == 0)
147 m_maxwrite = MIN(libfuse_max_write, (uint32_t)m_maxphys / 2);
148
149 try {
150 m_mock = new MockFS(m_maxread, m_maxreadahead, m_allow_other,
151 m_default_permissions, m_push_symlinks_in, m_ro,
152 m_pm, m_init_flags, m_kernel_minor_version,
153 m_maxwrite, m_async, m_noclusterr, m_time_gran,
154 m_nointr, m_noatime, m_fsname, m_subtype,
155 m_no_auto_init);
156 /*
157 * FUSE_ACCESS is called almost universally. Expecting it in
158 * each test case would be super-annoying. Instead, set a
159 * default expectation for FUSE_ACCESS and return ENOSYS.
160 *
161 * Individual test cases can override this expectation since
162 * googlemock evaluates expectations in LIFO order.
163 */
164 EXPECT_CALL(*m_mock, process(
165 ResultOf([=](auto in) {
166 return (in.header.opcode == FUSE_ACCESS);
167 }, Eq(true)),
168 _)
169 ).Times(AnyNumber())
170 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
171 /*
172 * FUSE_BMAP is called for most test cases that read data. Set
173 * a default expectation and return ENOSYS.
174 *
175 * Individual test cases can override this expectation since
176 * googlemock evaluates expectations in LIFO order.
177 */
178 EXPECT_CALL(*m_mock, process(
179 ResultOf([=](auto in) {
180 return (in.header.opcode == FUSE_BMAP);
181 }, Eq(true)),
182 _)
183 ).Times(AnyNumber())
184 .WillRepeatedly(Invoke(ReturnErrno(ENOSYS)));
185 } catch (std::system_error err) {
186 FAIL() << err.what();
187 }
188 }
189
190 void
expect_access(uint64_t ino,mode_t access_mode,int error)191 FuseTest::expect_access(uint64_t ino, mode_t access_mode, int error)
192 {
193 EXPECT_CALL(*m_mock, process(
194 ResultOf([=](auto in) {
195 return (in.header.opcode == FUSE_ACCESS &&
196 in.header.nodeid == ino &&
197 in.body.access.mask == access_mode);
198 }, Eq(true)),
199 _)
200 ).WillOnce(Invoke(ReturnErrno(error)));
201 }
202
203 void
expect_destroy(int error)204 FuseTest::expect_destroy(int error)
205 {
206 EXPECT_CALL(*m_mock, process(
207 ResultOf([=](auto in) {
208 return (in.header.opcode == FUSE_DESTROY);
209 }, Eq(true)),
210 _)
211 ).WillOnce(Invoke(ReturnImmediate([=](auto in, auto& out) {
212 m_mock->m_quit = true;
213 out.header.len = sizeof(out.header);
214 out.header.unique = in.header.unique;
215 out.header.error = -error;
216 })));
217 }
218
219 void
expect_fallocate(uint64_t ino,uint64_t offset,uint64_t length,uint32_t mode,int error,int times)220 FuseTest::expect_fallocate(uint64_t ino, uint64_t offset, uint64_t length,
221 uint32_t mode, int error, int times)
222 {
223 EXPECT_CALL(*m_mock, process(
224 ResultOf([=](auto in) {
225 return (in.header.opcode == FUSE_FALLOCATE &&
226 in.header.nodeid == ino &&
227 in.body.fallocate.offset == offset &&
228 in.body.fallocate.length == length &&
229 in.body.fallocate.mode == mode);
230 }, Eq(true)),
231 _)
232 ).Times(times)
233 .WillRepeatedly(Invoke(ReturnErrno(error)));
234 }
235
236 void
expect_flush(uint64_t ino,int times,ProcessMockerT r)237 FuseTest::expect_flush(uint64_t ino, int times, ProcessMockerT r)
238 {
239 EXPECT_CALL(*m_mock, process(
240 ResultOf([=](auto in) {
241 return (in.header.opcode == FUSE_FLUSH &&
242 in.header.nodeid == ino);
243 }, Eq(true)),
244 _)
245 ).Times(times)
246 .WillRepeatedly(Invoke(r));
247 }
248
249 void
expect_forget(uint64_t ino,uint64_t nlookup,sem_t * sem)250 FuseTest::expect_forget(uint64_t ino, uint64_t nlookup, sem_t *sem)
251 {
252 EXPECT_CALL(*m_mock, process(
253 ResultOf([=](auto in) {
254 return (in.header.opcode == FUSE_FORGET &&
255 in.header.nodeid == ino &&
256 in.body.forget.nlookup == nlookup);
257 }, Eq(true)),
258 _)
259 ).WillOnce(Invoke([=](auto in __unused, auto &out __unused) {
260 if (sem != NULL)
261 sem_post(sem);
262 /* FUSE_FORGET has no response! */
263 }));
264 }
265
expect_getattr(uint64_t ino,uint64_t size)266 void FuseTest::expect_getattr(uint64_t ino, uint64_t size)
267 {
268 EXPECT_CALL(*m_mock, process(
269 ResultOf([=](auto in) {
270 return (in.header.opcode == FUSE_GETATTR &&
271 in.header.nodeid == ino);
272 }, Eq(true)),
273 _)
274 ).WillOnce(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
275 SET_OUT_HEADER_LEN(out, attr);
276 out.body.attr.attr.ino = ino; // Must match nodeid
277 out.body.attr.attr.mode = S_IFREG | 0644;
278 out.body.attr.attr.size = size;
279 out.body.attr.attr_valid = UINT64_MAX;
280 })));
281 }
282
expect_getxattr(uint64_t ino,const char * attr,ProcessMockerT r)283 void FuseTest::expect_getxattr(uint64_t ino, const char *attr, ProcessMockerT r)
284 {
285 EXPECT_CALL(*m_mock, process(
286 ResultOf([=](auto in) {
287 const char *a = (const char*)in.body.bytes +
288 sizeof(fuse_getxattr_in);
289 return (in.header.opcode == FUSE_GETXATTR &&
290 in.header.nodeid == ino &&
291 0 == strcmp(attr, a));
292 }, Eq(true)),
293 _)
294 ).WillOnce(Invoke(r));
295 }
296
expect_lookup(const char * relpath,uint64_t ino,mode_t mode,uint64_t size,int times,uint64_t attr_valid,uid_t uid,gid_t gid)297 void FuseTest::expect_lookup(const char *relpath, uint64_t ino, mode_t mode,
298 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
299 {
300 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
301 .Times(times)
302 .WillRepeatedly(Invoke(
303 ReturnImmediate([=](auto in __unused, auto& out) {
304 SET_OUT_HEADER_LEN(out, entry);
305 out.body.entry.attr.mode = mode;
306 out.body.entry.nodeid = ino;
307 out.body.entry.attr.nlink = 1;
308 out.body.entry.attr_valid = attr_valid;
309 out.body.entry.attr.size = size;
310 out.body.entry.attr.uid = uid;
311 out.body.entry.attr.gid = gid;
312 })));
313 }
314
expect_lookup_7_8(const char * relpath,uint64_t ino,mode_t mode,uint64_t size,int times,uint64_t attr_valid,uid_t uid,gid_t gid)315 void FuseTest::expect_lookup_7_8(const char *relpath, uint64_t ino, mode_t mode,
316 uint64_t size, int times, uint64_t attr_valid, uid_t uid, gid_t gid)
317 {
318 EXPECT_LOOKUP(FUSE_ROOT_ID, relpath)
319 .Times(times)
320 .WillRepeatedly(Invoke(
321 ReturnImmediate([=](auto in __unused, auto& out) {
322 SET_OUT_HEADER_LEN(out, entry_7_8);
323 out.body.entry.attr.mode = mode;
324 out.body.entry.nodeid = ino;
325 out.body.entry.attr.nlink = 1;
326 out.body.entry.attr_valid = attr_valid;
327 out.body.entry.attr.size = size;
328 out.body.entry.attr.uid = uid;
329 out.body.entry.attr.gid = gid;
330 })));
331 }
332
expect_open(uint64_t ino,uint32_t flags,int times)333 void FuseTest::expect_open(uint64_t ino, uint32_t flags, int times)
334 {
335 EXPECT_CALL(*m_mock, process(
336 ResultOf([=](auto in) {
337 return (in.header.opcode == FUSE_OPEN &&
338 in.header.nodeid == ino);
339 }, Eq(true)),
340 _)
341 ).Times(times)
342 .WillRepeatedly(Invoke(
343 ReturnImmediate([=](auto in __unused, auto& out) {
344 out.header.len = sizeof(out.header);
345 SET_OUT_HEADER_LEN(out, open);
346 out.body.open.fh = FH;
347 out.body.open.open_flags = flags;
348 })));
349 }
350
expect_opendir(uint64_t ino)351 void FuseTest::expect_opendir(uint64_t ino)
352 {
353 /* opendir(3) calls fstatfs */
354 EXPECT_CALL(*m_mock, process(
355 ResultOf([](auto in) {
356 return (in.header.opcode == FUSE_STATFS);
357 }, Eq(true)),
358 _)
359 ).WillRepeatedly(Invoke(
360 ReturnImmediate([=](auto i __unused, auto& out) {
361 SET_OUT_HEADER_LEN(out, statfs);
362 })));
363
364 EXPECT_CALL(*m_mock, process(
365 ResultOf([=](auto in) {
366 return (in.header.opcode == FUSE_OPENDIR &&
367 in.header.nodeid == ino);
368 }, Eq(true)),
369 _)
370 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
371 out.header.len = sizeof(out.header);
372 SET_OUT_HEADER_LEN(out, open);
373 out.body.open.fh = FH;
374 })));
375 }
376
expect_read(uint64_t ino,uint64_t offset,uint64_t isize,uint64_t osize,const void * contents,int flags,uint64_t fh)377 void FuseTest::expect_read(uint64_t ino, uint64_t offset, uint64_t isize,
378 uint64_t osize, const void *contents, int flags, uint64_t fh)
379 {
380 EXPECT_CALL(*m_mock, process(
381 ResultOf([=](auto in) {
382 return (in.header.opcode == FUSE_READ &&
383 in.header.nodeid == ino &&
384 in.body.read.fh == fh &&
385 in.body.read.offset == offset &&
386 in.body.read.size == isize &&
387 (flags == -1 ?
388 (in.body.read.flags == O_RDONLY ||
389 in.body.read.flags == O_RDWR)
390 : in.body.read.flags == (uint32_t)flags));
391 }, Eq(true)),
392 _)
393 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
394 assert(osize <= sizeof(out.body.bytes));
395 out.header.len = sizeof(struct fuse_out_header) + osize;
396 memmove(out.body.bytes, contents, osize);
397 }))).RetiresOnSaturation();
398 }
399
expect_readdir(uint64_t ino,uint64_t off,std::vector<struct dirent> & ents)400 void FuseTest::expect_readdir(uint64_t ino, uint64_t off,
401 std::vector<struct dirent> &ents)
402 {
403 EXPECT_CALL(*m_mock, process(
404 ResultOf([=](auto in) {
405 return (in.header.opcode == FUSE_READDIR &&
406 in.header.nodeid == ino &&
407 in.body.readdir.fh == FH &&
408 in.body.readdir.offset == off);
409 }, Eq(true)),
410 _)
411 ).WillRepeatedly(Invoke(ReturnImmediate([=](auto in, auto& out) {
412 struct fuse_dirent *fde = (struct fuse_dirent*)&(out.body);
413 int i = 0;
414
415 out.header.error = 0;
416 out.header.len = 0;
417
418 for (const auto& it: ents) {
419 size_t entlen, entsize;
420
421 fde->ino = it.d_fileno;
422 fde->off = it.d_off;
423 fde->type = it.d_type;
424 fde->namelen = it.d_namlen;
425 strncpy(fde->name, it.d_name, it.d_namlen);
426 entlen = FUSE_NAME_OFFSET + fde->namelen;
427 entsize = FUSE_DIRENT_SIZE(fde);
428 /*
429 * The FUSE protocol does not require zeroing out the
430 * unused portion of the name. But it's a good
431 * practice to prevent information disclosure to the
432 * FUSE client, even though the client is usually the
433 * kernel
434 */
435 memset(fde->name + fde->namelen, 0, entsize - entlen);
436 if (out.header.len + entsize > in.body.read.size) {
437 printf("Overflow in readdir expectation: i=%d\n"
438 , i);
439 break;
440 }
441 out.header.len += entsize;
442 fde = (struct fuse_dirent*)
443 ((intmax_t*)fde + entsize / sizeof(intmax_t));
444 i++;
445 }
446 out.header.len += sizeof(out.header);
447 })));
448
449 }
expect_release(uint64_t ino,uint64_t fh)450 void FuseTest::expect_release(uint64_t ino, uint64_t fh)
451 {
452 EXPECT_CALL(*m_mock, process(
453 ResultOf([=](auto in) {
454 return (in.header.opcode == FUSE_RELEASE &&
455 in.header.nodeid == ino &&
456 in.body.release.fh == fh);
457 }, Eq(true)),
458 _)
459 ).WillOnce(Invoke(ReturnErrno(0)));
460 }
461
expect_releasedir(uint64_t ino,ProcessMockerT r)462 void FuseTest::expect_releasedir(uint64_t ino, ProcessMockerT r)
463 {
464 EXPECT_CALL(*m_mock, process(
465 ResultOf([=](auto in) {
466 return (in.header.opcode == FUSE_RELEASEDIR &&
467 in.header.nodeid == ino &&
468 in.body.release.fh == FH);
469 }, Eq(true)),
470 _)
471 ).WillOnce(Invoke(r));
472 }
473
expect_unlink(uint64_t parent,const char * path,int error)474 void FuseTest::expect_unlink(uint64_t parent, const char *path, int error)
475 {
476 EXPECT_CALL(*m_mock, process(
477 ResultOf([=](auto in) {
478 return (in.header.opcode == FUSE_UNLINK &&
479 0 == strcmp(path, in.body.unlink) &&
480 in.header.nodeid == parent);
481 }, Eq(true)),
482 _)
483 ).WillOnce(Invoke(ReturnErrno(error)));
484 }
485
expect_write(uint64_t ino,uint64_t offset,uint64_t isize,uint64_t osize,uint32_t flags_set,uint32_t flags_unset,const void * contents)486 void FuseTest::expect_write(uint64_t ino, uint64_t offset, uint64_t isize,
487 uint64_t osize, uint32_t flags_set, uint32_t flags_unset,
488 const void *contents)
489 {
490 EXPECT_CALL(*m_mock, process(
491 ResultOf([=](auto in) {
492 const char *buf = (const char*)in.body.bytes +
493 sizeof(struct fuse_write_in);
494 bool pid_ok;
495 uint32_t wf = in.body.write.write_flags;
496
497 assert(isize <= sizeof(in.body.bytes) -
498 sizeof(struct fuse_write_in));
499 if (wf & FUSE_WRITE_CACHE)
500 pid_ok = true;
501 else
502 pid_ok = (pid_t)in.header.pid == getpid();
503
504 return (in.header.opcode == FUSE_WRITE &&
505 in.header.nodeid == ino &&
506 in.body.write.fh == FH &&
507 in.body.write.offset == offset &&
508 in.body.write.size == isize &&
509 pid_ok &&
510 (wf & flags_set) == flags_set &&
511 (wf & flags_unset) == 0 &&
512 (in.body.write.flags == O_WRONLY ||
513 in.body.write.flags == O_RDWR) &&
514 0 == bcmp(buf, contents, isize));
515 }, Eq(true)),
516 _)
517 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
518 SET_OUT_HEADER_LEN(out, write);
519 out.body.write.size = osize;
520 })));
521 }
522
expect_write_7_8(uint64_t ino,uint64_t offset,uint64_t isize,uint64_t osize,const void * contents)523 void FuseTest::expect_write_7_8(uint64_t ino, uint64_t offset, uint64_t isize,
524 uint64_t osize, const void *contents)
525 {
526 EXPECT_CALL(*m_mock, process(
527 ResultOf([=](auto in) {
528 const char *buf = (const char*)in.body.bytes +
529 FUSE_COMPAT_WRITE_IN_SIZE;
530 bool pid_ok = (pid_t)in.header.pid == getpid();
531
532 assert(isize <= sizeof(in.body.bytes) -
533 FUSE_COMPAT_WRITE_IN_SIZE);
534 return (in.header.opcode == FUSE_WRITE &&
535 in.header.nodeid == ino &&
536 in.body.write.fh == FH &&
537 in.body.write.offset == offset &&
538 in.body.write.size == isize &&
539 pid_ok &&
540 0 == bcmp(buf, contents, isize));
541 }, Eq(true)),
542 _)
543 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
544 SET_OUT_HEADER_LEN(out, write);
545 out.body.write.size = osize;
546 })));
547 }
548
549 void
get_unprivileged_id(uid_t * uid,gid_t * gid)550 get_unprivileged_id(uid_t *uid, gid_t *gid)
551 {
552 struct passwd *pw;
553 struct group *gr;
554
555 /*
556 * First try "tests", Kyua's default unprivileged user. XXX after
557 * GoogleTest gains a proper Kyua wrapper, get this with the Kyua API
558 */
559 pw = getpwnam("tests");
560 if (pw == NULL) {
561 /* Fall back to "nobody" */
562 pw = getpwnam("nobody");
563 }
564 if (pw == NULL)
565 GTEST_SKIP() << "Test requires an unprivileged user";
566 /* Use group "nobody", which is Kyua's default unprivileged group */
567 gr = getgrnam("nobody");
568 if (gr == NULL)
569 GTEST_SKIP() << "Test requires an unprivileged group";
570 *uid = pw->pw_uid;
571 *gid = gr->gr_gid;
572 }
573
574 void
fork(bool drop_privs,int * child_status,std::function<void ()> parent_func,std::function<int ()> child_func)575 FuseTest::fork(bool drop_privs, int *child_status,
576 std::function<void()> parent_func,
577 std::function<int()> child_func)
578 {
579 sem_t *sem;
580 int mprot = PROT_READ | PROT_WRITE;
581 int mflags = MAP_ANON | MAP_SHARED;
582 pid_t child;
583 uid_t uid;
584 gid_t gid;
585
586 if (drop_privs) {
587 get_unprivileged_id(&uid, &gid);
588 if (IsSkipped())
589 return;
590 }
591
592 sem = (sem_t*)mmap(NULL, sizeof(*sem), mprot, mflags, -1, 0);
593 ASSERT_NE(MAP_FAILED, sem) << strerror(errno);
594 ASSERT_EQ(0, sem_init(sem, 1, 0)) << strerror(errno);
595
596 if ((child = ::fork()) == 0) {
597 /* In child */
598 int err = 0;
599
600 if (sem_wait(sem)) {
601 perror("sem_wait");
602 err = 1;
603 goto out;
604 }
605
606 if (drop_privs && 0 != setegid(gid)) {
607 perror("setegid");
608 err = 1;
609 goto out;
610 }
611 if (drop_privs && 0 != setreuid(-1, uid)) {
612 perror("setreuid");
613 err = 1;
614 goto out;
615 }
616 err = child_func();
617
618 out:
619 sem_destroy(sem);
620 _exit(err);
621 } else if (child > 0) {
622 /*
623 * In parent. Cleanup must happen here, because it's still
624 * privileged.
625 */
626 m_mock->m_child_pid = child;
627 ASSERT_NO_FATAL_FAILURE(parent_func());
628
629 /* Signal the child process to go */
630 ASSERT_EQ(0, sem_post(sem)) << strerror(errno);
631
632 ASSERT_LE(0, wait(child_status)) << strerror(errno);
633 } else {
634 FAIL() << strerror(errno);
635 }
636 munmap(sem, sizeof(*sem));
637 return;
638 }
639
640 void
reclaim_vnode(const char * path)641 FuseTest::reclaim_vnode(const char *path)
642 {
643 int err;
644
645 err = sysctlbyname(reclaim_mib, NULL, 0, path, strlen(path) + 1);
646 ASSERT_EQ(0, err) << strerror(errno);
647 }
648
usage(char * progname)649 static void usage(char* progname) {
650 fprintf(stderr, "Usage: %s [-v]\n\t-v increase verbosity\n", progname);
651 exit(2);
652 }
653
main(int argc,char ** argv)654 int main(int argc, char **argv) {
655 int ch;
656 FuseEnv *fuse_env = new FuseEnv;
657
658 InitGoogleTest(&argc, argv);
659 AddGlobalTestEnvironment(fuse_env);
660
661 while ((ch = getopt(argc, argv, "v")) != -1) {
662 switch (ch) {
663 case 'v':
664 verbosity++;
665 break;
666 default:
667 usage(argv[0]);
668 break;
669 }
670 }
671
672 return (RUN_ALL_TESTS());
673 }
674