Browse code

move pkg/system: process to a separate package

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/10/15 22:10:25
Showing 10 changed files
... ...
@@ -15,8 +15,8 @@ import (
15 15
 	"github.com/docker/docker/errdefs"
16 16
 	"github.com/docker/docker/libnetwork"
17 17
 	"github.com/docker/docker/pkg/idtools"
18
+	"github.com/docker/docker/pkg/process"
18 19
 	"github.com/docker/docker/pkg/stringid"
19
-	"github.com/docker/docker/pkg/system"
20 20
 	"github.com/docker/docker/runconfig"
21 21
 	"github.com/moby/sys/mount"
22 22
 	"github.com/opencontainers/selinux/go-selinux/label"
... ...
@@ -352,9 +352,9 @@ func killProcessDirectly(container *container.Container) error {
352 352
 	}
353 353
 
354 354
 	// In case there were some exceptions(e.g., state of zombie and D)
355
-	if system.IsProcessAlive(pid) {
355
+	if process.Alive(pid) {
356 356
 		// Since we can not kill a zombie pid, add zombie check here
357
-		isZombie, err := system.IsProcessZombie(pid)
357
+		isZombie, err := process.Zombie(pid)
358 358
 		if err != nil {
359 359
 			logrus.WithError(err).WithField("container", container.ID).Warn("Container state is invalid")
360 360
 			return err
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/containerd/containerd"
15 15
 	"github.com/containerd/containerd/services/server/config"
16 16
 	"github.com/containerd/containerd/sys"
17
+	"github.com/docker/docker/pkg/process"
17 18
 	"github.com/docker/docker/pkg/system"
18 19
 	"github.com/pelletier/go-toml"
19 20
 	"github.com/pkg/errors"
... ...
@@ -145,7 +146,7 @@ func (r *remote) getContainerdPid() (int, error) {
145 145
 		if err != nil {
146 146
 			return -1, err
147 147
 		}
148
-		if system.IsProcessAlive(int(pid)) {
148
+		if process.Alive(int(pid)) {
149 149
 			return int(pid), nil
150 150
 		}
151 151
 	}
... ...
@@ -238,7 +239,7 @@ func (r *remote) startContainerd() error {
238 238
 
239 239
 	err = os.WriteFile(r.pidFile, []byte(strconv.Itoa(r.daemonPid)), 0660)
240 240
 	if err != nil {
241
-		system.KillProcess(r.daemonPid)
241
+		process.Kill(r.daemonPid)
242 242
 		return errors.Wrap(err, "libcontainerd: failed to save daemon pid to disk")
243 243
 	}
244 244
 
... ...
@@ -357,7 +358,7 @@ func (r *remote) monitorDaemon(ctx context.Context) {
357 357
 			r.logger.WithError(err).WithField("binary", binaryName).Debug("daemon is not responding")
358 358
 
359 359
 			transientFailureCount++
360
-			if transientFailureCount < maxConnectionRetryCount || system.IsProcessAlive(r.daemonPid) {
360
+			if transientFailureCount < maxConnectionRetryCount || process.Alive(r.daemonPid) {
361 361
 				delay = time.Duration(transientFailureCount) * 200 * time.Millisecond
362 362
 				continue
363 363
 			}
... ...
@@ -365,7 +366,7 @@ func (r *remote) monitorDaemon(ctx context.Context) {
365 365
 			client = nil
366 366
 		}
367 367
 
368
-		if system.IsProcessAlive(r.daemonPid) {
368
+		if process.Alive(r.daemonPid) {
369 369
 			r.logger.WithField("pid", r.daemonPid).Info("killing and restarting containerd")
370 370
 			r.killDaemon()
371 371
 		}
... ...
@@ -7,7 +7,7 @@ import (
7 7
 	"time"
8 8
 
9 9
 	"github.com/containerd/containerd/defaults"
10
-	"github.com/docker/docker/pkg/system"
10
+	"github.com/docker/docker/pkg/process"
11 11
 )
12 12
 
13 13
 const (
... ...
@@ -35,13 +35,13 @@ func (r *remote) stopDaemon() {
35 35
 	syscall.Kill(r.daemonPid, syscall.SIGTERM)
36 36
 	// Wait up to 15secs for it to stop
37 37
 	for i := time.Duration(0); i < shutdownTimeout; i += time.Second {
38
-		if !system.IsProcessAlive(r.daemonPid) {
38
+		if !process.Alive(r.daemonPid) {
39 39
 			break
40 40
 		}
41 41
 		time.Sleep(time.Second)
42 42
 	}
43 43
 
44
-	if system.IsProcessAlive(r.daemonPid) {
44
+	if process.Alive(r.daemonPid) {
45 45
 		r.logger.WithField("pid", r.daemonPid).Warn("daemon didn't stop within 15 secs, killing it")
46 46
 		syscall.Kill(r.daemonPid, syscall.SIGKILL)
47 47
 	}
... ...
@@ -51,7 +51,7 @@ func (r *remote) killDaemon() {
51 51
 	// Try to get a stack trace
52 52
 	syscall.Kill(r.daemonPid, syscall.SIGUSR1)
53 53
 	<-time.After(100 * time.Millisecond)
54
-	system.KillProcess(r.daemonPid)
54
+	process.Kill(r.daemonPid)
55 55
 }
56 56
 
57 57
 func (r *remote) platformCleanup() {
... ...
@@ -3,7 +3,7 @@ package supervisor // import "github.com/docker/docker/libcontainerd/supervisor"
3 3
 import (
4 4
 	"os"
5 5
 
6
-	"github.com/docker/docker/pkg/system"
6
+	"github.com/docker/docker/pkg/process"
7 7
 )
8 8
 
9 9
 const (
... ...
@@ -40,7 +40,7 @@ func (r *remote) stopDaemon() {
40 40
 }
41 41
 
42 42
 func (r *remote) killDaemon() {
43
-	system.KillProcess(r.daemonPid)
43
+	process.Kill(r.daemonPid)
44 44
 }
45 45
 
46 46
 func (r *remote) platformCleanup() {
47 47
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+// Package process provides a set of basic functions to manage individual
1
+// processes.
2
+package process
0 3
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+//go:build linux || freebsd || darwin
1
+// +build linux freebsd darwin
2
+
3
+package process
4
+
5
+import (
6
+	"bytes"
7
+	"fmt"
8
+	"os"
9
+
10
+	"golang.org/x/sys/unix"
11
+)
12
+
13
+// Alive returns true if process with a given pid is running.
14
+func Alive(pid int) bool {
15
+	err := unix.Kill(pid, 0)
16
+	if err == nil || err == unix.EPERM {
17
+		return true
18
+	}
19
+
20
+	return false
21
+}
22
+
23
+// Kill force-stops a process.
24
+func Kill(pid int) error {
25
+	err := unix.Kill(pid, unix.SIGKILL)
26
+	if err != nil && err != unix.ESRCH {
27
+		return err
28
+	}
29
+	return nil
30
+}
31
+
32
+// Zombie return true if process has a state with "Z"
33
+// http://man7.org/linux/man-pages/man5/proc.5.html
34
+func Zombie(pid int) (bool, error) {
35
+	data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
36
+	if err != nil {
37
+		if os.IsNotExist(err) {
38
+			return false, nil
39
+		}
40
+		return false, err
41
+	}
42
+	if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
43
+		return true, nil
44
+	}
45
+	return false, nil
46
+}
0 47
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+package process
1
+
2
+import "os"
3
+
4
+// Alive returns true if process with a given pid is running.
5
+func Alive(pid int) bool {
6
+	_, err := os.FindProcess(pid)
7
+
8
+	return err == nil
9
+}
10
+
11
+// Kill force-stops a process.
12
+func Kill(pid int) error {
13
+	p, err := os.FindProcess(pid)
14
+	if err == nil {
15
+		err = p.Kill()
16
+		if err != nil && err != os.ErrProcessDone {
17
+			return err
18
+		}
19
+	}
20
+	return nil
21
+}
22
+
23
+// Zombie is not supported on Windows.
24
+//
25
+// TODO(thaJeztah): remove once we remove the stubs from pkg/system.
26
+func Zombie(_ int) (bool, error) {
27
+	return false, nil
28
+}
0 29
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+//go:build linux || freebsd || darwin || windows
1
+// +build linux freebsd darwin windows
2
+
3
+package system
4
+
5
+import "github.com/docker/docker/pkg/process"
6
+
7
+var (
8
+	// IsProcessAlive returns true if process with a given pid is running.
9
+	//
10
+	// Deprecated: use [process.Alive].
11
+	IsProcessAlive = process.Alive
12
+
13
+	// IsProcessZombie return true if process has a state with "Z"
14
+	//
15
+	// Deprecated: use [process.Zombie].
16
+	//
17
+	// TODO(thaJeztah): remove the Windows implementation in process once we remove this stub.
18
+	IsProcessZombie = process.Zombie
19
+)
20
+
21
+// KillProcess force-stops a process.
22
+//
23
+// Deprecated: use [process.Kill].
24
+func KillProcess(pid int) {
25
+	_ = process.Kill(pid)
26
+}
0 27
deleted file mode 100644
... ...
@@ -1,43 +0,0 @@
1
-//go:build linux || freebsd || darwin
2
-// +build linux freebsd darwin
3
-
4
-package system // import "github.com/docker/docker/pkg/system"
5
-
6
-import (
7
-	"bytes"
8
-	"fmt"
9
-	"os"
10
-
11
-	"golang.org/x/sys/unix"
12
-)
13
-
14
-// IsProcessAlive returns true if process with a given pid is running.
15
-func IsProcessAlive(pid int) bool {
16
-	err := unix.Kill(pid, 0)
17
-	if err == nil || err == unix.EPERM {
18
-		return true
19
-	}
20
-
21
-	return false
22
-}
23
-
24
-// KillProcess force-stops a process.
25
-func KillProcess(pid int) {
26
-	unix.Kill(pid, unix.SIGKILL)
27
-}
28
-
29
-// IsProcessZombie return true if process has a state with "Z"
30
-// http://man7.org/linux/man-pages/man5/proc.5.html
31
-func IsProcessZombie(pid int) (bool, error) {
32
-	data, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
33
-	if err != nil {
34
-		if os.IsNotExist(err) {
35
-			return false, nil
36
-		}
37
-		return false, err
38
-	}
39
-	if cols := bytes.SplitN(data, []byte(" "), 4); len(cols) >= 3 && string(cols[2]) == "Z" {
40
-		return true, nil
41
-	}
42
-	return false, nil
43
-}
44 1
deleted file mode 100644
... ...
@@ -1,18 +0,0 @@
1
-package system // import "github.com/docker/docker/pkg/system"
2
-
3
-import "os"
4
-
5
-// IsProcessAlive returns true if process with a given pid is running.
6
-func IsProcessAlive(pid int) bool {
7
-	_, err := os.FindProcess(pid)
8
-
9
-	return err == nil
10
-}
11
-
12
-// KillProcess force-stops a process.
13
-func KillProcess(pid int) {
14
-	p, err := os.FindProcess(pid)
15
-	if err == nil {
16
-		_ = p.Kill()
17
-	}
18
-}