[lxc-devel] [lxd/master] exec: replace os.FindProcess() with syscall.Wait4()

brauner on Github lxc-bot at linuxcontainers.org
Wed Mar 8 00:55:56 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170308/8a80fcc1/attachment.bin>
-------------- next part --------------
From 880c5f2b70a9852abfe78812b0691557bbd58982 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Wed, 8 Mar 2017 01:51:46 +0100
Subject: [PATCH] exec: replace os.FindProcess() with syscall.Wait4()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 lxd/container_exec.go | 27 ++++++++-------------------
 lxd/main_forkexec.go  | 27 ++++++++-------------------
 2 files changed, 16 insertions(+), 38 deletions(-)

diff --git a/lxd/container_exec.go b/lxd/container_exec.go
index d4da9aa..ca3ba7f 100644
--- a/lxd/container_exec.go
+++ b/lxd/container_exec.go
@@ -301,30 +301,19 @@ func (s *execWs) Do(op *operation) error {
 		attachedChildIsBorn <- attachedPid
 	}
 
-	proc, err := os.FindProcess(pid)
-	if err != nil {
+	var ws syscall.WaitStatus
+	wpid, err := syscall.Wait4(pid, &ws, 0, nil)
+	if err != nil || wpid != pid {
 		return finisher(-1, fmt.Errorf("Failed finding process: %q", err))
 	}
 
-	procState, err := proc.Wait()
-	if err != nil {
-		return finisher(-1, fmt.Errorf("Failed waiting on process %d: %q", pid, err))
-	}
-
-	if procState.Success() {
-		return finisher(0, nil)
+	if ws.Exited() {
+		return finisher(ws.ExitStatus(), nil)
 	}
 
-	status, ok := procState.Sys().(syscall.WaitStatus)
-	if ok {
-		if status.Exited() {
-			return finisher(status.ExitStatus(), nil)
-		}
-
-		if status.Signaled() {
-			// 128 + n == Fatal error signal "n"
-			return finisher(128+int(status.Signal()), nil)
-		}
+	if ws.Signaled() {
+		// 128 + n == Fatal error signal "n"
+		return finisher(128+int(ws.Signal()), nil)
 	}
 
 	return finisher(-1, nil)
diff --git a/lxd/main_forkexec.go b/lxd/main_forkexec.go
index 917219f..bdbdcac 100644
--- a/lxd/main_forkexec.go
+++ b/lxd/main_forkexec.go
@@ -103,30 +103,19 @@ func cmdForkExec(args []string) (int, error) {
 		return -1, fmt.Errorf("Failed sending PID of executing command: %q", err)
 	}
 
-	proc, err := os.FindProcess(status)
-	if err != nil {
+	var ws syscall.WaitStatus
+	wpid, err := syscall.Wait4(status, &ws, 0, nil)
+	if err != nil || wpid != status {
 		return -1, fmt.Errorf("Failed finding process: %q", err)
 	}
 
-	procState, err := proc.Wait()
-	if err != nil {
-		return -1, fmt.Errorf("Failed waiting on process %d: %q", status, err)
-	}
-
-	if procState.Success() {
-		return 0, nil
+	if ws.Exited() {
+		return ws.ExitStatus(), nil
 	}
 
-	exCode, ok := procState.Sys().(syscall.WaitStatus)
-	if ok {
-		if exCode.Signaled() {
-			// 128 + n == Fatal error signal "n"
-			return 128 + int(exCode.Signal()), nil
-		}
-
-		if exCode.Exited() {
-			return exCode.ExitStatus(), nil
-		}
+	if ws.Signaled() {
+		// 128 + n == Fatal error signal "n"
+		return 128 + int(ws.Signal()), nil
 	}
 
 	return -1, fmt.Errorf("Command failed")


More information about the lxc-devel mailing list