浏览代码

unix: add Exec call

The syscall execve has no wrapper in this library, which it seems like
it should - Ian seemed to concur in CL 72550.

Add a docstring and an example, because I always get confused about
how to invoke the syscall.

Change-Id: I6100bbbf4ace9e3e341bf186a04cc03301da9aea
Reviewed-on: https://go-review.googlesource.com/101282
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Kevin Burke 7 年之前
父节点
当前提交
0deb464c5a
共有 2 个文件被更改,包括 25 次插入0 次删除
  1. 16 0
      unix/example_test.go
  2. 9 0
      unix/syscall_unix.go

+ 16 - 0
unix/example_test.go

@@ -0,0 +1,16 @@
+// Copyright 2018 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.
+
+package unix
+
+import (
+	"log"
+	"os"
+	"syscall"
+)
+
+func ExampleExec() {
+	err := syscall.Exec("/bin/ls", []string{"ls", "-al"}, os.Environ())
+	log.Fatal(err)
+}

+ 9 - 0
unix/syscall_unix.go

@@ -305,3 +305,12 @@ func SetNonblock(fd int, nonblocking bool) (err error) {
 	_, err = fcntl(fd, F_SETFL, flag)
 	return err
 }
+
+// Exec calls execve(2), which replaces the calling executable in the process
+// tree. argv0 should be the full path to an executable ("/bin/ls") and the
+// executable name should also be the first argument in argv (["ls", "-l"]).
+// envv are the environment variables that should be passed to the new
+// process (["USER=go", "PWD=/tmp"]).
+func Exec(argv0 string, argv []string, envv []string) error {
+	return syscall.Exec(argv0, argv, envv)
+}