Skip to content

Commit

Permalink
child: createCmd: propogate systemd files to actual process within na…
Browse files Browse the repository at this point in the history
…mespace

The child will inheret the file descriptors from the parent, but it needs to
pass them along to its child, the actual target process within the environment.
  • Loading branch information
charliemirabile committed Mar 28, 2024
1 parent 41a98f2 commit a48228f
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ var propagationStates = map[string]uintptr{
"rslave": uintptr(unix.MS_REC | unix.MS_SLAVE),
}

func setupFiles(cmd *exec.Cmd) {
// 0 1 and 2 are used for stdin. stdout, and stderr
const firstExtraFD = 3
systemdActivationFDs := 0
// check for systemd socket activation sockets
if v := os.Getenv("LISTEN_FDS"); v != "" {
if num, err := strconv.Atoi(v); err == nil {
systemdActivationFDs = num
cmd.ExtraFiles = make([]*os.File, systemdActivationFDs)
}
}
for fd := 0; fd < systemdActivationFDs; fd++ {
cmd.ExtraFiles[fd] = os.NewFile(uintptr(firstExtraFD + fd), "")
}
}


func createCmd(targetCmd []string) (*exec.Cmd, error) {
var args []string
if len(targetCmd) > 1 {
Expand All @@ -47,6 +64,7 @@ func createCmd(targetCmd []string) (*exec.Cmd, error) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
setupFiles(cmd)
return cmd, nil
}

Expand Down

0 comments on commit a48228f

Please sign in to comment.