Skip to content

Commit 9d5e754

Browse files
committed
move pkg/system: process to a separate package
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 0040fb9 commit 9d5e754

9 files changed

Lines changed: 85 additions & 39 deletions

File tree

daemon/container_operations_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"github.com/docker/docker/errdefs"
1616
"github.com/docker/docker/libnetwork"
1717
"github.com/docker/docker/pkg/idtools"
18+
"github.com/docker/docker/pkg/process"
1819
"github.com/docker/docker/pkg/stringid"
19-
"github.com/docker/docker/pkg/system"
2020
"github.com/docker/docker/runconfig"
2121
"github.com/moby/sys/mount"
2222
"github.com/opencontainers/selinux/go-selinux/label"
@@ -352,9 +352,9 @@ func killProcessDirectly(container *container.Container) error {
352352
}
353353

354354
// In case there were some exceptions(e.g., state of zombie and D)
355-
if system.IsProcessAlive(pid) {
355+
if process.Alive(pid) {
356356
// Since we can not kill a zombie pid, add zombie check here
357-
isZombie, err := system.IsProcessZombie(pid)
357+
isZombie, err := process.Zombie(pid)
358358
if err != nil {
359359
logrus.WithError(err).WithField("container", container.ID).Warn("Container state is invalid")
360360
return err

libcontainerd/supervisor/remote_daemon.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/containerd/containerd"
1515
"github.com/containerd/containerd/services/server/config"
1616
"github.com/containerd/containerd/sys"
17+
"github.com/docker/docker/pkg/process"
1718
"github.com/docker/docker/pkg/system"
1819
"github.com/pelletier/go-toml"
1920
"github.com/pkg/errors"
@@ -145,7 +146,7 @@ func (r *remote) getContainerdPid() (int, error) {
145146
if err != nil {
146147
return -1, err
147148
}
148-
if system.IsProcessAlive(int(pid)) {
149+
if process.Alive(int(pid)) {
149150
return int(pid), nil
150151
}
151152
}
@@ -238,7 +239,7 @@ func (r *remote) startContainerd() error {
238239

239240
err = os.WriteFile(r.pidFile, []byte(strconv.Itoa(r.daemonPid)), 0660)
240241
if err != nil {
241-
system.KillProcess(r.daemonPid)
242+
process.Kill(r.daemonPid)
242243
return errors.Wrap(err, "libcontainerd: failed to save daemon pid to disk")
243244
}
244245

@@ -357,15 +358,15 @@ func (r *remote) monitorDaemon(ctx context.Context) {
357358
r.logger.WithError(err).WithField("binary", binaryName).Debug("daemon is not responding")
358359

359360
transientFailureCount++
360-
if transientFailureCount < maxConnectionRetryCount || system.IsProcessAlive(r.daemonPid) {
361+
if transientFailureCount < maxConnectionRetryCount || process.Alive(r.daemonPid) {
361362
delay = time.Duration(transientFailureCount) * 200 * time.Millisecond
362363
continue
363364
}
364365
client.Close()
365366
client = nil
366367
}
367368

368-
if system.IsProcessAlive(r.daemonPid) {
369+
if process.Alive(r.daemonPid) {
369370
r.logger.WithField("pid", r.daemonPid).Info("killing and restarting containerd")
370371
r.killDaemon()
371372
}

libcontainerd/supervisor/remote_daemon_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/containerd/containerd/defaults"
10-
"github.com/docker/docker/pkg/system"
10+
"github.com/docker/docker/pkg/process"
1111
)
1212

1313
const (
@@ -35,13 +35,13 @@ func (r *remote) stopDaemon() {
3535
syscall.Kill(r.daemonPid, syscall.SIGTERM)
3636
// Wait up to 15secs for it to stop
3737
for i := time.Duration(0); i < shutdownTimeout; i += time.Second {
38-
if !system.IsProcessAlive(r.daemonPid) {
38+
if !process.Alive(r.daemonPid) {
3939
break
4040
}
4141
time.Sleep(time.Second)
4242
}
4343

44-
if system.IsProcessAlive(r.daemonPid) {
44+
if process.Alive(r.daemonPid) {
4545
r.logger.WithField("pid", r.daemonPid).Warn("daemon didn't stop within 15 secs, killing it")
4646
syscall.Kill(r.daemonPid, syscall.SIGKILL)
4747
}
@@ -51,7 +51,7 @@ func (r *remote) killDaemon() {
5151
// Try to get a stack trace
5252
syscall.Kill(r.daemonPid, syscall.SIGUSR1)
5353
<-time.After(100 * time.Millisecond)
54-
system.KillProcess(r.daemonPid)
54+
process.Kill(r.daemonPid)
5555
}
5656

5757
func (r *remote) platformCleanup() {

libcontainerd/supervisor/remote_daemon_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package supervisor // import "github.com/docker/docker/libcontainerd/supervisor"
33
import (
44
"os"
55

6-
"github.com/docker/docker/pkg/system"
6+
"github.com/docker/docker/pkg/process"
77
)
88

99
const (
@@ -40,7 +40,7 @@ func (r *remote) stopDaemon() {
4040
}
4141

4242
func (r *remote) killDaemon() {
43-
system.KillProcess(r.daemonPid)
43+
process.Kill(r.daemonPid)
4444
}
4545

4646
func (r *remote) platformCleanup() {

pkg/process/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package process provides a set of basic functions to manage individual
2+
// processes.
3+
package process
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build linux || freebsd || darwin
22
// +build linux freebsd darwin
33

4-
package system // import "github.com/docker/docker/pkg/system"
4+
package process
55

66
import (
77
"bytes"
@@ -11,8 +11,8 @@ import (
1111
"golang.org/x/sys/unix"
1212
)
1313

14-
// IsProcessAlive returns true if process with a given pid is running.
15-
func IsProcessAlive(pid int) bool {
14+
// Alive returns true if process with a given pid is running.
15+
func Alive(pid int) bool {
1616
err := unix.Kill(pid, 0)
1717
if err == nil || err == unix.EPERM {
1818
return true
@@ -21,14 +21,18 @@ func IsProcessAlive(pid int) bool {
2121
return false
2222
}
2323

24-
// KillProcess force-stops a process.
25-
func KillProcess(pid int) {
26-
unix.Kill(pid, unix.SIGKILL)
24+
// Kill force-stops a process.
25+
func Kill(pid int) error {
26+
err := unix.Kill(pid, unix.SIGKILL)
27+
if err != nil && err != unix.ESRCH {
28+
return err
29+
}
30+
return nil
2731
}
2832

29-
// IsProcessZombie return true if process has a state with "Z"
33+
// Zombie return true if process has a state with "Z"
3034
// http://man7.org/linux/man-pages/man5/proc.5.html
31-
func IsProcessZombie(pid int) (bool, error) {
35+
func Zombie(pid int) (bool, error) {
3236
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
3337
if err != nil {
3438
if os.IsNotExist(err) {

pkg/process/process_windows.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package process
2+
3+
import "os"
4+
5+
// Alive returns true if process with a given pid is running.
6+
func Alive(pid int) bool {
7+
_, err := os.FindProcess(pid)
8+
9+
return err == nil
10+
}
11+
12+
// Kill force-stops a process.
13+
func Kill(pid int) error {
14+
p, err := os.FindProcess(pid)
15+
if err == nil {
16+
err = p.Kill()
17+
if err != nil && err != os.ErrProcessDone {
18+
return err
19+
}
20+
}
21+
return nil
22+
}
23+
24+
// Zombie is not supported on Windows.
25+
//
26+
// TODO(thaJeztah): remove once we remove the stubs from pkg/system.
27+
func Zombie(_ int) (bool, error) {
28+
return false, nil
29+
}

pkg/system/process_deprecated.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build linux || freebsd || darwin || windows
2+
// +build linux freebsd darwin windows
3+
4+
package system
5+
6+
import "github.com/docker/docker/pkg/process"
7+
8+
var (
9+
// IsProcessAlive returns true if process with a given pid is running.
10+
//
11+
// Deprecated: use [process.Alive].
12+
IsProcessAlive = process.Alive
13+
14+
// IsProcessZombie return true if process has a state with "Z"
15+
//
16+
// Deprecated: use [process.Zombie].
17+
//
18+
// TODO(thaJeztah): remove the Windows implementation in process once we remove this stub.
19+
IsProcessZombie = process.Zombie
20+
)
21+
22+
// KillProcess force-stops a process.
23+
//
24+
// Deprecated: use [process.Kill].
25+
func KillProcess(pid int) {
26+
_ = process.Kill(pid)
27+
}

pkg/system/process_windows.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)