Skip to content

Commit 031cd72

Browse files
authored
Implement pdeathSignal handling in Runc (#431)
Add support for parent death signal (pdeathSignal) to ensure child processes receive a signal when the parent process dies. This addresses the FIXME comment in Runc.execute(). ## Changes - Add pdeathSignal field to exec_command_attrs C struct - Implement prctl(PR_SET_PDEATHSIG) in child process handler (Linux only) - Expose pdeathSignal through Command.Attrs Swift API - Wire up pdeathSignal in Runc.execute() to remove FIXME ## Implementation Details The implementation uses Linux-specific prctl() to set the parent death signal, ensuring proper cleanup when parent processes terminate. The feature is conditionally compiled for Linux only, maintaining compatibility with other platforms.
1 parent 4a8f945 commit 031cd72

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Sources/CShim/exec_command.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include <sys/types.h>
3333
#include <sys/wait.h>
3434
#include <unistd.h>
35+
#if defined(__linux__)
36+
#include <sys/prctl.h>
37+
#endif
3538

3639
#include "exec_command.h"
3740

@@ -98,6 +101,7 @@ void exec_command_attrs_init(struct exec_command_attrs *attrs) {
98101
attrs->mask = 0;
99102
attrs->uid = -1;
100103
attrs->gid = -1;
104+
attrs->pdeathSignal = 0;
101105
}
102106

103107
static void child_handler(const int sync_pipes[2], const char *executable,
@@ -253,6 +257,15 @@ static void child_handler(const int sync_pipes[2], const char *executable,
253257
}
254258
}
255259

260+
#if defined(__linux__)
261+
// Set parent death signal if specified
262+
if (attrs.pdeathSignal != 0) {
263+
if (prctl(PR_SET_PDEATHSIG, attrs.pdeathSignal) != 0) {
264+
goto fail;
265+
}
266+
}
267+
#endif
268+
256269
// close exec everything outside of our child's fd_table.
257270
if (cloexec_from(file_handle_count) != 0) {
258271
goto fail;

Sources/CShim/include/exec_command.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct exec_command_attrs {
3838
gid_t gid;
3939
/// signal mask for the child process
4040
int mask;
41+
/// parent death signal (Linux only, 0 to disable)
42+
int pdeathSignal;
4143
};
4244

4345
void exec_command_attrs_init(struct exec_command_attrs *attrs);

Sources/ContainerizationOS/Command.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public struct Command: Sendable {
6868
public var uid: UInt32?
6969
/// Set the process group ID.
7070
public var gid: UInt32?
71+
/// Signal to send when parent process dies (Linux only).
72+
public var pdeathSignal: Int32?
7173

7274
public init(
7375
setPGroup: Bool = false,
@@ -77,7 +79,8 @@ public struct Command: Sendable {
7779
setsid: Bool = false,
7880
setctty: Bool = false,
7981
uid: UInt32? = nil,
80-
gid: UInt32? = nil
82+
gid: UInt32? = nil,
83+
pdeathSignal: Int32? = nil
8184
) {
8285
self.setPGroup = setPGroup
8386
self.resetIDs = resetIDs
@@ -87,6 +90,7 @@ public struct Command: Sendable {
8790
self.setctty = setctty
8891
self.uid = uid
8992
self.gid = gid
93+
self.pdeathSignal = pdeathSignal
9094
}
9195
}
9296

@@ -207,6 +211,10 @@ extension Command {
207211
attrs.gid = gid
208212
}
209213

214+
if let pdeathSignal = self.attrs.pdeathSignal {
215+
attrs.pdeathSignal = pdeathSignal
216+
}
217+
210218
var pid: pid_t = 0
211219
var argv = ([executable] + arguments).map { strdup($0) } + [nil]
212220
defer {

vminitd/Sources/vminitd/Runc/Runc.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ extension Runc {
339339
cmd.stdout = stdout ?? outPipe.fileHandleForWriting
340340
cmd.stderr = stderr ?? outPipe.fileHandleForWriting
341341

342-
// FIXME: pdeathSignal handling if Command supported it.
342+
if let pdeathSignal = pdeathSignal {
343+
cmd.attrs.pdeathSignal = pdeathSignal
344+
}
343345

344346
if setpgid {
345347
cmd.attrs.setPGroup = true

0 commit comments

Comments
 (0)