aboutsummaryrefslogtreecommitdiff
path: root/src/os/pidfd_linux.go
blob: 5a830dadb252e537c7dbfd08450d04b12a07ed7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Support for pidfd was added during the course of a few Linux releases:
//  v5.1: pidfd_send_signal syscall;
//  v5.2: CLONE_PIDFD flag for clone syscall;
//  v5.3: pidfd_open syscall, clone3 syscall;
//  v5.4: P_PIDFD idtype support for waitid syscall;
//  v5.6: pidfd_getfd syscall.

package os

import (
	"internal/syscall/unix"
	"sync"
	"syscall"
	"unsafe"
)

func ensurePidfd(sysAttr *syscall.SysProcAttr) *syscall.SysProcAttr {
	if !pidfdWorks() {
		return sysAttr
	}

	var pidfd int

	if sysAttr == nil {
		return &syscall.SysProcAttr{
			PidFD: &pidfd,
		}
	}
	if sysAttr.PidFD == nil {
		newSys := *sysAttr // copy
		newSys.PidFD = &pidfd
		return &newSys
	}

	return sysAttr
}

func getPidfd(sysAttr *syscall.SysProcAttr) uintptr {
	if !pidfdWorks() {
		return unsetHandle
	}

	return uintptr(*sysAttr.PidFD)
}

func pidfdFind(pid int) (uintptr, error) {
	if !pidfdWorks() {
		return unsetHandle, syscall.ENOSYS
	}

	h, err := unix.PidFDOpen(pid, 0)
	if err == nil {
		return h, nil
	}
	return unsetHandle, convertESRCH(err)
}

func (p *Process) pidfdRelease() {
	// Release pidfd unconditionally.
	handle := p.handle.Swap(unsetHandle)
	if handle != unsetHandle {
		syscall.Close(int(handle))
	}
}

// _P_PIDFD is used as idtype argument to waitid syscall.
const _P_PIDFD = 3

func (p *Process) pidfdWait() (*ProcessState, error) {
	handle := p.handle.Load()
	if handle == unsetHandle || !pidfdWorks() {
		return nil, syscall.ENOSYS
	}
	var (
		info   unix.SiginfoChild
		rusage syscall.Rusage
		e      syscall.Errno
	)
	for {
		_, _, e = syscall.Syscall6(syscall.SYS_WAITID, _P_PIDFD, handle, uintptr(unsafe.Pointer(&info)), syscall.WEXITED, uintptr(unsafe.Pointer(&rusage)), 0)
		if e != syscall.EINTR {
			break
		}
	}
	if e != 0 {
		if e == syscall.EINVAL {
			// This is either invalid option value (which should not happen
			// as we only use WEXITED), or missing P_PIDFD support (Linux
			// kernel < 5.4), meaning pidfd support is not implemented.
			e = syscall.ENOSYS
		}
		return nil, e
	}
	p.setDone()
	p.pidfdRelease()
	return &ProcessState{
		pid:    int(info.Pid),
		status: info.WaitStatus(),
		rusage: &rusage,
	}, nil
}

func (p *Process) pidfdSendSignal(s syscall.Signal) error {
	handle := p.handle.Load()
	if handle == unsetHandle || !pidfdWorks() {
		return syscall.ENOSYS
	}
	return convertESRCH(unix.PidFDSendSignal(handle, s))
}

func pidfdWorks() bool {
	return checkPidfdOnce() == nil
}

var checkPidfdOnce = sync.OnceValue(checkPidfd)

// checkPidfd checks whether all required pidfd-related syscalls work.
// This consists of pidfd_open and pidfd_send_signal syscalls, and waitid
// syscall with idtype of P_PIDFD.
//
// Reasons for non-working pidfd syscalls include an older kernel and an
// execution environment in which the above system calls are restricted by
// seccomp or a similar technology.
func checkPidfd() error {
	// Get a pidfd of the current process (opening of "/proc/self" won't
	// work for waitid).
	fd, err := unix.PidFDOpen(syscall.Getpid(), 0)
	if err != nil {
		return NewSyscallError("pidfd_open", err)
	}
	defer syscall.Close(int(fd))

	// Check waitid(P_PIDFD) works.
	for {
		_, _, err = syscall.Syscall6(syscall.SYS_WAITID, _P_PIDFD, fd, 0, syscall.WEXITED, 0, 0)
		if err != syscall.EINTR {
			break
		}
	}
	// Expect ECHILD from waitid since we're not our own parent.
	if err != syscall.ECHILD {
		return NewSyscallError("pidfd_wait", err)
	}

	// Check pidfd_send_signal works (should be able to send 0 to itself).
	if err := unix.PidFDSendSignal(fd, 0); err != nil {
		return NewSyscallError("pidfd_send_signal", err)
	}

	return nil
}