cmd/sharefs: expand fuse_main
All checks were successful
Test / Create distribution (push) Successful in 42s
Test / Sandbox (push) Successful in 2m20s
Test / ShareFS (push) Successful in 3m22s
Test / Hpkg (push) Successful in 4m20s
Test / Sandbox (race detector) (push) Successful in 4m34s
Test / Hakurei (race detector) (push) Successful in 5m28s
Test / Hakurei (push) Successful in 2m41s
Test / Flake checks (push) Successful in 1m55s

This change should not change behaviour other than making output more consistent.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-12-27 02:25:18 +09:00
parent a27305cb4a
commit 6d3bd27220
4 changed files with 93 additions and 11 deletions

View File

@@ -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
}