aboutsummaryrefslogtreecommitdiffhomepage
path: root/container
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-03-25 15:34:20 +0900
committerOphestra <cat@gensokyo.uk>2026-03-25 15:39:29 +0900
commit50403e9d60a69640107d20525379d4714da2f739 (patch)
treeeb9a72082f5aaf04c886db68b07ad8ccb9c88e72 /container
parentb98c5f2e219c142ab89c09445b6c014120f0760f (diff)
internal/netlink: wrap netpoll via context
This removes netpoll boilerplate for the most common use case. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'container')
-rw-r--r--container/dispatcher.go14
-rw-r--r--container/dispatcher_test.go3
-rw-r--r--container/init.go7
3 files changed, 17 insertions, 7 deletions
diff --git a/container/dispatcher.go b/container/dispatcher.go
index dce5f023..9f971fa5 100644
--- a/container/dispatcher.go
+++ b/container/dispatcher.go
@@ -1,6 +1,7 @@
package container
import (
+ "context"
"io"
"io/fs"
"net"
@@ -66,7 +67,7 @@ type syscallDispatcher interface {
// ensureFile provides ensureFile.
ensureFile(name string, perm, pperm os.FileMode) error
// mustLoopback provides mustLoopback.
- mustLoopback(msg message.Msg)
+ mustLoopback(ctx context.Context, msg message.Msg)
// seccompLoad provides [seccomp.Load].
seccompLoad(rules []std.NativeRule, flags seccomp.ExportFlag) error
@@ -170,7 +171,7 @@ func (k direct) mountTmpfs(fsname, target string, flags uintptr, size int, perm
func (direct) ensureFile(name string, perm, pperm os.FileMode) error {
return ensureFile(name, perm, pperm)
}
-func (direct) mustLoopback(msg message.Msg) {
+func (direct) mustLoopback(ctx context.Context, msg message.Msg) {
var lo int
if ifi, err := net.InterfaceByName("lo"); err != nil {
msg.GetLogger().Fatalln(err)
@@ -199,11 +200,14 @@ func (direct) mustLoopback(msg message.Msg) {
msg.GetLogger().Fatalf("RTNETLINK answers: %v", err)
default:
- msg.GetLogger().Fatalf("RTNETLINK answers with malformed message")
+ if err == context.DeadlineExceeded || err == context.Canceled {
+ msg.GetLogger().Fatalf("interrupted RTNETLINK operation")
+ }
+ msg.GetLogger().Fatal("RTNETLINK answers with malformed message")
}
}
- must(c.SendNewaddrLo(uint32(lo)))
- must(c.SendIfInfomsg(syscall.RTM_NEWLINK, 0, &syscall.IfInfomsg{
+ must(c.SendNewaddrLo(ctx, uint32(lo)))
+ must(c.SendIfInfomsg(ctx, syscall.RTM_NEWLINK, 0, &syscall.IfInfomsg{
Family: syscall.AF_UNSPEC,
Index: int32(lo),
Flags: syscall.IFF_UP,
diff --git a/container/dispatcher_test.go b/container/dispatcher_test.go
index ee2c3f19..b57c53b9 100644
--- a/container/dispatcher_test.go
+++ b/container/dispatcher_test.go
@@ -2,6 +2,7 @@ package container
import (
"bytes"
+ "context"
"fmt"
"io"
"io/fs"
@@ -468,7 +469,7 @@ func (k *kstub) ensureFile(name string, perm, pperm os.FileMode) error {
stub.CheckArg(k.Stub, "pperm", pperm, 2))
}
-func (*kstub) mustLoopback(message.Msg) { /* noop */ }
+func (*kstub) mustLoopback(context.Context, message.Msg) { /* noop */ }
func (k *kstub) seccompLoad(rules []std.NativeRule, flags seccomp.ExportFlag) error {
k.Helper()
diff --git a/container/init.go b/container/init.go
index 799bb29b..7a561733 100644
--- a/container/init.go
+++ b/container/init.go
@@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
+ "os/signal"
"path"
"slices"
"strconv"
@@ -175,7 +176,11 @@ func initEntrypoint(k syscallDispatcher, msg message.Msg) {
}
if !params.HostNet {
- k.mustLoopback(msg)
+ ctx, cancel := signal.NotifyContext(context.Background(), CancelSignal,
+ os.Interrupt, SIGTERM, SIGQUIT)
+ defer cancel() // for panics
+ k.mustLoopback(ctx, msg)
+ cancel()
}
// write uid/gid map here so parent does not need to set dumpable