Skip to content

Commit 65859c2

Browse files
committed
contrib/fs-idmap: Move logic to a new function
We can't call log.Fatalf() and defer functions, as the former doesn't call any defers. Let's just move the code to a new function and call os.Exit() only in main, when all defer executed. Signed-off-by: Rodrigo Campos <[email protected]>
1 parent 569742b commit 65859c2

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

contrib/cmd/fs-idmap/fs-idmap.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ func main() {
1414
if len(os.Args) != 2 {
1515
log.Fatalf("usage: %s path_to_mount_set_attr", os.Args[0])
1616
}
17-
1817
src := os.Args[1]
18+
if err := supportsIDMap(src); err != nil {
19+
log.Fatalf("fatal error: %v", err)
20+
}
21+
}
22+
23+
func supportsIDMap(src string) error {
1924
treeFD, err := unix.OpenTree(unix.AT_FDCWD, src, uint(unix.OPEN_TREE_CLONE|unix.OPEN_TREE_CLOEXEC|unix.AT_EMPTY_PATH))
2025
if err != nil {
21-
log.Fatalf("error calling open_tree %q: %v", src, err)
26+
return fmt.Errorf("error calling open_tree %q: %w", src, err)
2227
}
2328
defer unix.Close(treeFD)
2429

@@ -29,7 +34,7 @@ func main() {
2934
GidMappings: []syscall.SysProcIDMap{{ContainerID: 0, HostID: 65536, Size: 65536}},
3035
}
3136
if err := cmd.Start(); err != nil {
32-
log.Fatalf("failed to run the helper binary: %v", err)
37+
return fmt.Errorf("failed to run the helper binary: %w", err)
3338
}
3439
defer func() {
3540
_ = cmd.Process.Kill()
@@ -39,7 +44,7 @@ func main() {
3944
path := fmt.Sprintf("/proc/%d/ns/user", cmd.Process.Pid)
4045
var userNsFile *os.File
4146
if userNsFile, err = os.Open(path); err != nil {
42-
log.Fatalf("unable to get user ns file descriptor: %v", err)
47+
return fmt.Errorf("unable to get user ns file descriptor: %w", err)
4348
}
4449
defer userNsFile.Close()
4550

@@ -48,6 +53,8 @@ func main() {
4853
Userns_fd: uint64(userNsFile.Fd()),
4954
}
5055
if err := unix.MountSetattr(treeFD, "", unix.AT_EMPTY_PATH, &attr); err != nil {
51-
log.Fatalf("error calling mount_setattr: %v", err)
56+
fmt.Errorf("error calling mount_setattr: %w", err)
5257
}
58+
59+
return nil
5360
}

0 commit comments

Comments
 (0)