diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-12-27 02:25:18 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-12-27 02:30:28 +0900 |
| commit | 6d3bd27220c80eb58a11b75054fbbcf67e50cc28 (patch) | |
| tree | 68dd700e8988c9591cc8b703d0146292962ff07e /cmd/sharefs/fuse.go | |
| parent | a27305cb4aa4e382b9474175139ab72bff220fd6 (diff) | |
cmd/sharefs: expand fuse_main
This change should not change behaviour other than making output more consistent.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd/sharefs/fuse.go')
| -rw-r--r-- | cmd/sharefs/fuse.go | 82 |
1 files changed, 77 insertions, 5 deletions
diff --git a/cmd/sharefs/fuse.go b/cmd/sharefs/fuse.go index 4b9764d9..8b638b75 100644 --- a/cmd/sharefs/fuse.go +++ b/cmd/sharefs/fuse.go @@ -25,6 +25,8 @@ import ( "strconv" "syscall" "unsafe" + + "hakurei.app/internal/info" ) type ( @@ -207,6 +209,8 @@ func unsafeAddArgument(args *fuseArgs, arg string) { } func _main(argc int, argv **C.char) int { + runtime.LockOSThread() + // don't mask creation mode, kernel already did that syscall.Umask(0) @@ -222,6 +226,34 @@ func _main(argc int, argv **C.char) int { var priv C.struct_sharefs_private pinner.Pin(&priv) + var opts C.struct_fuse_cmdline_opts + if C.fuse_parse_cmdline(&args, &opts) != 0 { + return 1 + } + defer func() { + if opts.mountpoint != nil { + C.free(unsafe.Pointer(opts.mountpoint)) + } + C.fuse_opt_free_args(&args) + }() + + if opts.show_version != 0 { + fmt.Println("hakurei version", info.Version()) + fmt.Println("FUSE library version", C.GoString(C.fuse_pkgversion())) + C.fuse_lowlevel_version() + return 0 + } + + if opts.show_help != 0 { + showHelp(&args) + return 0 + } + + if opts.show_help == 0 && opts.mountpoint == nil { + log.Println("no mountpoint specified") + return 2 + } + { source, setuid, setgid, ret := parseOpts(&args) if ret != 0 { @@ -247,8 +279,7 @@ func _main(argc int, argv **C.char) int { priv.setuid, priv.setgid = C.uintptr_t(setuid), C.uintptr_t(setgid) } - // TODO(ophestra): spawn container here, set PR_SET_NO_NEW_PRIVS and enforce landlock - fuse_main_return := C._fuse_main(args.argc, args.argv, &C.struct_fuse_operations{ + op := C.struct_fuse_operations{ init: closure(C.sharefs_init), destroy: closure(C.sharefs_destroy), @@ -268,11 +299,52 @@ func _main(argc int, argv **C.char) int { statfs: closure(C.sharefs_statfs), release: closure(C.sharefs_release), fsync: closure(C.sharefs_fsync), - }, unsafe.Pointer(&priv)) + } + + fuse := C.fuse_new_fn(&args, &op, C.size_t(unsafe.Sizeof(op)), unsafe.Pointer(&priv)) + if fuse == nil { + return 3 + } + defer C.fuse_destroy(fuse) + + if C.fuse_mount(fuse, opts.mountpoint) != 0 { + return 4 + } + defer C.fuse_unmount(fuse) + + // TODO(ophestra): spawn container here, set PR_SET_NO_NEW_PRIVS and enforce landlock + if C.fuse_daemonize(opts.foreground) != 0 { + return 5 + } + + se := C.fuse_get_session(fuse) + if C.fuse_set_signal_handlers(se) != 0 { + return 6 + } + defer C.fuse_remove_signal_handlers(se) + + if opts.singlethread != 0 { + if C.fuse_loop(fuse) != 0 { + return 8 + } + } else { + loopConfig := C.fuse_loop_cfg_create() + if loopConfig == nil { + return 7 + } + defer C.fuse_loop_cfg_destroy(loopConfig) + + C.fuse_loop_cfg_set_clone_fd(loopConfig, C.uint(opts.clone_fd)) + + C.fuse_loop_cfg_set_idle_threads(loopConfig, opts.max_idle_threads) + C.fuse_loop_cfg_set_max_threads(loopConfig, opts.max_threads) + if C.fuse_loop_mt(fuse, loopConfig) != 0 { + return 8 + } + } if priv.init_failed { return 1 - } else { - return int(fuse_main_return) } + return 0 } |
