aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-10-30 00:46:23 +0900
committerOphestra <cat@gensokyo.uk>2025-10-30 01:20:51 +0900
commiteeb9f98e5bd9d4fac6706edb87d06bf757f0fb79 (patch)
tree5417d1c2e18d87be6d5d6a90db7d6f8a4546ba5d
parent3f9f331501acc3dc367276501822ef1a260dfd24 (diff)
internal/outcome/shim: move signal constants
The magic numbers hurt readability. Signed-off-by: Ophestra <cat@gensokyo.uk>
-rw-r--r--internal/outcome/shim-signal.c19
-rw-r--r--internal/outcome/shim-signal.h8
-rw-r--r--internal/outcome/shim.go19
-rw-r--r--internal/outcome/shim_test.go4
4 files changed, 33 insertions, 17 deletions
diff --git a/internal/outcome/shim-signal.c b/internal/outcome/shim-signal.c
index fc005347..7e562068 100644
--- a/internal/outcome/shim-signal.c
+++ b/internal/outcome/shim-signal.c
@@ -8,35 +8,32 @@
static pid_t hakurei_shim_param_ppid = -1;
static int hakurei_shim_fd = -1;
-static ssize_t hakurei_shim_write(const void *buf, size_t count) {
+/* see shim.go for handling of the message */
+static inline ssize_t hakurei_shim_write(hakurei_shim_msg msg) {
int savedErrno = errno;
- ssize_t ret = write(hakurei_shim_fd, buf, count);
+ unsigned char buf = (unsigned char)msg;
+ ssize_t ret = write(hakurei_shim_fd, &buf, 1);
if (ret == -1 && errno != EAGAIN)
exit(EXIT_FAILURE);
errno = savedErrno;
return ret;
}
-/* see shim_linux.go for handling of the value */
static void hakurei_shim_sigaction(int sig, siginfo_t *si, void *ucontext) {
if (sig != SIGCONT || si == NULL) {
- /* unreachable */
- hakurei_shim_write("\2", 1);
+ hakurei_shim_write(HAKUREI_SHIM_INVALID);
return;
}
if (si->si_pid == hakurei_shim_param_ppid) {
- /* monitor requests shim exit */
- hakurei_shim_write("\0", 1);
+ hakurei_shim_write(HAKUREI_SHIM_EXIT_REQUESTED);
return;
}
- /* unexpected si_pid */
- hakurei_shim_write("\3", 1);
+ hakurei_shim_write(HAKUREI_SHIM_BAD_PID);
if (getppid() != hakurei_shim_param_ppid)
- /* shim orphaned before monitor delivers a signal */
- hakurei_shim_write("\1", 1);
+ hakurei_shim_write(HAKUREI_SHIM_ORPHAN);
}
void hakurei_shim_setup_cont_signal(pid_t ppid, int fd) {
diff --git a/internal/outcome/shim-signal.h b/internal/outcome/shim-signal.h
index bdeae3b0..b0229a91 100644
--- a/internal/outcome/shim-signal.h
+++ b/internal/outcome/shim-signal.h
@@ -1,3 +1,11 @@
#include <signal.h>
+/* see shim.go for documentation */
+typedef enum {
+ HAKUREI_SHIM_EXIT_REQUESTED,
+ HAKUREI_SHIM_ORPHAN,
+ HAKUREI_SHIM_INVALID,
+ HAKUREI_SHIM_BAD_PID,
+} hakurei_shim_msg;
+
void hakurei_shim_setup_cont_signal(pid_t ppid, int fd);
diff --git a/internal/outcome/shim.go b/internal/outcome/shim.go
index a16bdc70..acc962c1 100644
--- a/internal/outcome/shim.go
+++ b/internal/outcome/shim.go
@@ -22,6 +22,17 @@ import (
//#include "shim-signal.h"
import "C"
+const (
+ /* hakurei requests shim exit */
+ shimMsgExitRequested = C.HAKUREI_SHIM_EXIT_REQUESTED
+ /* shim orphaned before hakurei delivers a signal */
+ shimMsgOrphaned = C.HAKUREI_SHIM_ORPHAN
+ /* unreachable */
+ shimMsgInvalid = C.HAKUREI_SHIM_INVALID
+ /* unexpected si_pid */
+ shimMsgBadPID = C.HAKUREI_SHIM_BAD_PID
+)
+
// setupContSignal sets up the SIGCONT signal handler for the cross-uid shim exit hack.
// The signal handler is implemented in C, signals can be processed by reading from the returned reader.
// The returned function must be called after all signal processing concludes.
@@ -161,7 +172,7 @@ func shimEntrypoint(k syscallDispatcher) {
}
switch buf[0] {
- case 0: // got SIGCONT from monitor: shim exit requested
+ case shimMsgExitRequested: // got SIGCONT from hakurei: shim exit requested
if fp := cancelContainer.Load(); stateParams.params.ForwardCancel && fp != nil && *fp != nil {
(*fp)()
// shim now bound by ShimWaitDelay, implemented below
@@ -171,13 +182,13 @@ func shimEntrypoint(k syscallDispatcher) {
// setup has not completed, terminate immediately
k.exit(hst.ExitRequest)
- case 1: // got SIGCONT via pdeath_signal: monitor died before delivering signal
+ case shimMsgOrphaned: // got SIGCONT after orphaned: hakurei died before delivering signal
k.exit(hst.ExitOrphan)
- case 2: // unreachable
+ case shimMsgInvalid: // unreachable
msg.Verbose("sa_sigaction got invalid siginfo")
- case 3: // got SIGCONT from unexpected process: hopefully the terminal driver
+ case shimMsgBadPID: // got SIGCONT from unexpected process: hopefully the terminal driver
msg.Verbose("got SIGCONT from unexpected process")
default: // unreachable
diff --git a/internal/outcome/shim_test.go b/internal/outcome/shim_test.go
index d493f709..046c22b6 100644
--- a/internal/outcome/shim_test.go
+++ b/internal/outcome/shim_test.go
@@ -148,9 +148,9 @@ func TestShimEntrypoint(t *testing.T) {
// deferred
call("wKeepAlive", stub.ExpectArgs{}, nil, nil),
}, Tracks: []stub.Expect{{Calls: []stub.Call{
- call("rcRead", stub.ExpectArgs{}, []byte{2}, nil),
+ call("rcRead", stub.ExpectArgs{}, []byte{shimMsgInvalid}, nil),
call("verbose", stub.ExpectArgs{[]any{"sa_sigaction got invalid siginfo"}}, nil, nil),
- call("rcRead", stub.ExpectArgs{}, []byte{3}, nil),
+ call("rcRead", stub.ExpectArgs{}, []byte{shimMsgBadPID}, nil),
call("verbose", stub.ExpectArgs{[]any{"got SIGCONT from unexpected process"}}, nil, nil),
call("rcRead", stub.ExpectArgs{}, nil, nil), // stub terminates this goroutine
}}}}, nil},