Skip to content

Commit e655abc

Browse files
committed
int/linux: add/use Dup3, Open, Openat
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent c690b66 commit e655abc

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-22
lines changed

internal/linux/linux.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import (
66
"golang.org/x/sys/unix"
77
)
88

9+
// Dup3 wraps [unix.Dup3].
10+
func Dup3(oldfd, newfd, flags int) error {
11+
err := retryOnEINTR(func() error {
12+
return unix.Dup3(oldfd, newfd, flags)
13+
})
14+
return os.NewSyscallError("dup3", err)
15+
}
16+
917
// Exec wraps [unix.Exec].
1018
func Exec(cmd string, args []string, env []string) error {
1119
err := retryOnEINTR(func() error {
@@ -23,6 +31,28 @@ func Getwd() (wd string, err error) {
2331
return wd, os.NewSyscallError("getwd", err)
2432
}
2533

34+
// Open wraps [unix.Open].
35+
func Open(path string, mode int, perm uint32) (fd int, err error) {
36+
fd, err = retryOnEINTR2(func() (int, error) {
37+
return unix.Open(path, mode, perm)
38+
})
39+
if err != nil {
40+
return -1, &os.PathError{Op: "open", Path: path, Err: err}
41+
}
42+
return fd, nil
43+
}
44+
45+
// Openat wraps [unix.Openat].
46+
func Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) {
47+
fd, err = retryOnEINTR2(func() (int, error) {
48+
return unix.Openat(dirfd, path, mode, perm)
49+
})
50+
if err != nil {
51+
return -1, &os.PathError{Op: "openat", Path: path, Err: err}
52+
}
53+
return fd, nil
54+
}
55+
2656
// Sendmsg wraps [unix.Sendmsg].
2757
func Sendmsg(fd int, p, oob []byte, to unix.Sockaddr, flags int) error {
2858
err := retryOnEINTR(func() error {

libcontainer/console_linux.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package libcontainer
33
import (
44
"os"
55

6+
"github.com/opencontainers/runc/internal/linux"
67
"golang.org/x/sys/unix"
78
)
89

@@ -26,16 +27,12 @@ func mountConsole(slavePath string) error {
2627
// dupStdio opens the slavePath for the console and dups the fds to the current
2728
// processes stdio, fd 0,1,2.
2829
func dupStdio(slavePath string) error {
29-
fd, err := unix.Open(slavePath, unix.O_RDWR, 0)
30+
fd, err := linux.Open(slavePath, unix.O_RDWR, 0)
3031
if err != nil {
31-
return &os.PathError{
32-
Op: "open",
33-
Path: slavePath,
34-
Err: err,
35-
}
32+
return err
3633
}
3734
for _, i := range []int{0, 1, 2} {
38-
if err := unix.Dup3(fd, i, 0); err != nil {
35+
if err := linux.Dup3(fd, i, 0); err != nil {
3936
return err
4037
}
4138
}

libcontainer/rootfs_linux.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/opencontainers/cgroups"
2525
devices "github.com/opencontainers/cgroups/devices/config"
2626
"github.com/opencontainers/cgroups/fs2"
27+
"github.com/opencontainers/runc/internal/linux"
2728
"github.com/opencontainers/runc/libcontainer/configs"
2829
"github.com/opencontainers/runc/libcontainer/utils"
2930
)
@@ -883,12 +884,8 @@ func reOpenDevNull() error {
883884
}
884885
if stat.Rdev == devNullStat.Rdev {
885886
// Close and re-open the fd.
886-
if err := unix.Dup3(int(file.Fd()), fd, 0); err != nil {
887-
return &os.PathError{
888-
Op: "dup3",
889-
Path: "fd " + strconv.Itoa(int(file.Fd())),
890-
Err: err,
891-
}
887+
if err := linux.Dup3(int(file.Fd()), fd, 0); err != nil {
888+
return err
892889
}
893890
}
894891
}
@@ -1063,15 +1060,15 @@ func pivotRoot(rootfs string) error {
10631060
// with pivot_root this allows us to pivot without creating directories in
10641061
// the rootfs. Shout-outs to the LXC developers for giving us this idea.
10651062

1066-
oldroot, err := unix.Open("/", unix.O_DIRECTORY|unix.O_RDONLY, 0)
1063+
oldroot, err := linux.Open("/", unix.O_DIRECTORY|unix.O_RDONLY, 0)
10671064
if err != nil {
1068-
return &os.PathError{Op: "open", Path: "/", Err: err}
1065+
return err
10691066
}
10701067
defer unix.Close(oldroot)
10711068

1072-
newroot, err := unix.Open(rootfs, unix.O_DIRECTORY|unix.O_RDONLY, 0)
1069+
newroot, err := linux.Open(rootfs, unix.O_DIRECTORY|unix.O_RDONLY, 0)
10731070
if err != nil {
1074-
return &os.PathError{Op: "open", Path: rootfs, Err: err}
1071+
return err
10751072
}
10761073
defer unix.Close(newroot)
10771074

libcontainer/standard_init_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ func (l *linuxStandardInit) Init() error {
262262
// user process. We open it through /proc/self/fd/$fd, because the fd that
263263
// was given to us was an O_PATH fd to the fifo itself. Linux allows us to
264264
// re-open an O_PATH fd through /proc.
265-
fd, err := unix.Open(fifoPath, unix.O_WRONLY|unix.O_CLOEXEC, 0)
265+
fd, err := linux.Open(fifoPath, unix.O_WRONLY|unix.O_CLOEXEC, 0)
266266
if err != nil {
267-
return &os.PathError{Op: "open exec fifo", Path: fifoPath, Err: err}
267+
return err
268268
}
269269
if _, err := unix.Write(fd, []byte("0")); err != nil {
270270
return &os.PathError{Op: "write exec fifo", Path: fifoPath, Err: err}

libcontainer/utils/utils_unix.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
_ "unsafe" // for go:linkname
1515

1616
securejoin "github.com/cyphar/filepath-securejoin"
17+
"github.com/opencontainers/runc/internal/linux"
1718
"github.com/sirupsen/logrus"
1819
"golang.org/x/sys/unix"
1920
)
@@ -358,9 +359,9 @@ func Openat(dir *os.File, path string, flags int, mode uint32) (*os.File, error)
358359
}
359360
flags |= unix.O_CLOEXEC
360361

361-
fd, err := unix.Openat(dirFd, path, flags, mode)
362+
fd, err := linux.Openat(dirFd, path, flags, mode)
362363
if err != nil {
363-
return nil, &os.PathError{Op: "openat", Path: path, Err: err}
364+
return nil, err
364365
}
365366
return os.NewFile(uintptr(fd), dir.Name()+"/"+path), nil
366367
}

tests/cmd/seccompagent/seccompagent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
securejoin "github.com/cyphar/filepath-securejoin"
17+
"github.com/opencontainers/runc/internal/linux"
1718
"github.com/opencontainers/runtime-spec/specs-go"
1819
libseccomp "github.com/seccomp/libseccomp-golang"
1920
"github.com/sirupsen/logrus"
@@ -124,7 +125,7 @@ func handleNewMessage(sockfd int) (uintptr, string, error) {
124125
func readArgString(pid uint32, offset int64) (string, error) {
125126
buffer := make([]byte, 4096) // PATH_MAX
126127

127-
memfd, err := unix.Open(fmt.Sprintf("/proc/%d/mem", pid), unix.O_RDONLY, 0o777)
128+
memfd, err := linux.Open(fmt.Sprintf("/proc/%d/mem", pid), unix.O_RDONLY, 0o777)
128129
if err != nil {
129130
return "", err
130131
}

0 commit comments

Comments
 (0)