From a23ea646f3fb7b200758cc44e2b9ea15a57ba0bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 10 Nov 2016 08:52:01 -0800 Subject: [PATCH] vendor: update go-ps to not require cgo on darwin --- .../fsouza/go-dockerclient/.gitignore | 2 + .../fsouza/go-dockerclient/.travis.yml | 27 ++++ vendor/github.com/mitchellh/go-ps/README.md | 4 +- .../mitchellh/go-ps/process_darwin.go | 116 ++++++++++++++---- .../mitchellh/go-ps/process_darwin.h | 66 ---------- vendor/vendor.json | 7 ++ 6 files changed, 128 insertions(+), 94 deletions(-) create mode 100644 vendor/github.com/fsouza/go-dockerclient/.gitignore create mode 100644 vendor/github.com/fsouza/go-dockerclient/.travis.yml delete mode 100644 vendor/github.com/mitchellh/go-ps/process_darwin.h diff --git a/vendor/github.com/fsouza/go-dockerclient/.gitignore b/vendor/github.com/fsouza/go-dockerclient/.gitignore new file mode 100644 index 000000000..5f6b48eae --- /dev/null +++ b/vendor/github.com/fsouza/go-dockerclient/.gitignore @@ -0,0 +1,2 @@ +# temporary symlink for testing +testing/data/symlink diff --git a/vendor/github.com/fsouza/go-dockerclient/.travis.yml b/vendor/github.com/fsouza/go-dockerclient/.travis.yml new file mode 100644 index 000000000..68b137ad2 --- /dev/null +++ b/vendor/github.com/fsouza/go-dockerclient/.travis.yml @@ -0,0 +1,27 @@ +language: go +sudo: required +go: + - 1.4.2 + - 1.5.3 + - 1.6 + - tip +os: + - linux + - osx +env: + - GOARCH=amd64 DOCKER_VERSION=1.8.3 + - GOARCH=386 DOCKER_VERSION=1.8.3 + - GOARCH=amd64 DOCKER_VERSION=1.9.1 + - GOARCH=386 DOCKER_VERSION=1.9.1 + - GOARCH=amd64 DOCKER_VERSION=1.10.3 + - GOARCH=386 DOCKER_VERSION=1.10.3 +install: + - travis_retry travis-scripts/install.bash +script: + - travis-scripts/run-tests.bash +services: + - docker +matrix: + fast_finish: true + allow_failures: + - go: tip diff --git a/vendor/github.com/mitchellh/go-ps/README.md b/vendor/github.com/mitchellh/go-ps/README.md index 11cea98f0..881ad32a2 100644 --- a/vendor/github.com/mitchellh/go-ps/README.md +++ b/vendor/github.com/mitchellh/go-ps/README.md @@ -10,8 +10,7 @@ DLL methods for Windows, cgo for Darwin, etc. How it works: - * **Darwin** uses the `sysctl` syscall to retrieve the process table, via - cgo. + * **Darwin** uses the `sysctl` syscall to retrieve the process table. * **Unix** uses the procfs at `/proc` to inspect the process tree. * **Windows** uses the Windows API, and methods such as `CreateToolhelp32Snapshot` to get a point-in-time snapshot of @@ -33,4 +32,3 @@ implemented for this library that would be nice: * FreeBSD support * Plan9 support - * Eliminate the need for cgo with Darwin diff --git a/vendor/github.com/mitchellh/go-ps/process_darwin.go b/vendor/github.com/mitchellh/go-ps/process_darwin.go index 2b633ba5e..5ee87fb68 100644 --- a/vendor/github.com/mitchellh/go-ps/process_darwin.go +++ b/vendor/github.com/mitchellh/go-ps/process_darwin.go @@ -2,18 +2,13 @@ package ps -// #include "process_darwin.h" -import "C" - import ( - "sync" + "bytes" + "encoding/binary" + "syscall" + "unsafe" ) -// This lock is what verifies that C calling back into Go is only -// modifying data once at a time. -var darwinLock sync.Mutex -var darwinProcs []Process - type DarwinProcess struct { pid int ppid int @@ -32,17 +27,6 @@ func (p *DarwinProcess) Executable() string { return p.binary } -//export go_darwin_append_proc -func go_darwin_append_proc(pid C.pid_t, ppid C.pid_t, comm *C.char) { - proc := &DarwinProcess{ - pid: int(pid), - ppid: int(ppid), - binary: C.GoString(comm), - } - - darwinProcs = append(darwinProcs, proc) -} - func findProcess(pid int) (Process, error) { ps, err := processes() if err != nil { @@ -59,14 +43,96 @@ func findProcess(pid int) (Process, error) { } func processes() ([]Process, error) { - darwinLock.Lock() - defer darwinLock.Unlock() - darwinProcs = make([]Process, 0, 50) - - _, err := C.darwinProcesses() + buf, err := darwinSyscall() if err != nil { return nil, err } + procs := make([]*kinfoProc, 0, 50) + k := 0 + for i := _KINFO_STRUCT_SIZE; i < buf.Len(); i += _KINFO_STRUCT_SIZE { + proc := &kinfoProc{} + err = binary.Read(bytes.NewBuffer(buf.Bytes()[k:i]), binary.LittleEndian, proc) + if err != nil { + return nil, err + } + + k = i + procs = append(procs, proc) + } + + darwinProcs := make([]Process, len(procs)) + for i, p := range procs { + darwinProcs[i] = &DarwinProcess{ + pid: int(p.Pid), + ppid: int(p.PPid), + binary: darwinCstring(p.Comm), + } + } + return darwinProcs, nil } + +func darwinCstring(s [16]byte) string { + i := 0 + for _, b := range s { + if b != 0 { + i++ + } else { + break + } + } + + return string(s[:i]) +} + +func darwinSyscall() (*bytes.Buffer, error) { + mib := [4]int32{_CTRL_KERN, _KERN_PROC, _KERN_PROC_ALL, 0} + size := uintptr(0) + + _, _, errno := syscall.Syscall6( + syscall.SYS___SYSCTL, + uintptr(unsafe.Pointer(&mib[0])), + 4, + 0, + uintptr(unsafe.Pointer(&size)), + 0, + 0) + + if errno != 0 { + return nil, errno + } + + bs := make([]byte, size) + _, _, errno = syscall.Syscall6( + syscall.SYS___SYSCTL, + uintptr(unsafe.Pointer(&mib[0])), + 4, + uintptr(unsafe.Pointer(&bs[0])), + uintptr(unsafe.Pointer(&size)), + 0, + 0) + + if errno != 0 { + return nil, errno + } + + return bytes.NewBuffer(bs[0:size]), nil +} + +const ( + _CTRL_KERN = 1 + _KERN_PROC = 14 + _KERN_PROC_ALL = 0 + _KINFO_STRUCT_SIZE = 648 +) + +type kinfoProc struct { + _ [40]byte + Pid int32 + _ [199]byte + Comm [16]byte + _ [301]byte + PPid int32 + _ [84]byte +} diff --git a/vendor/github.com/mitchellh/go-ps/process_darwin.h b/vendor/github.com/mitchellh/go-ps/process_darwin.h deleted file mode 100644 index c7742ef6f..000000000 --- a/vendor/github.com/mitchellh/go-ps/process_darwin.h +++ /dev/null @@ -1,66 +0,0 @@ -// +build darwin - -#ifndef _GO_PROCESSDARWIN_H_INCLUDED -#define _GO_PROCESSDARWIN_H_INCLUDED - -#include -#include -#include - -// This is declared in process_darwin.go -extern void go_darwin_append_proc(pid_t, pid_t, char *); - -// Loads the process table and calls the exported Go function to insert -// the data back into the Go space. -// -// This function is implemented in C because while it would technically -// be possible to do this all in Go, I didn't want to go spelunking through -// header files to get all the structures properly. It is much easier to just -// call it in C and be done with it. -static inline int darwinProcesses() { - int err = 0; - int i = 0; - static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; - size_t length = 0; - struct kinfo_proc *result = NULL; - size_t resultCount = 0; - - // Get the length first - err = sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, - NULL, &length, NULL, 0); - if (err != 0) { - goto ERREXIT; - } - - // Allocate the appropriate sized buffer to read the process list - result = malloc(length); - - // Call sysctl again with our buffer to fill it with the process list - err = sysctl((int*)name, (sizeof(name) / sizeof(*name)) - 1, - result, &length, - NULL, 0); - if (err != 0) { - goto ERREXIT; - } - - resultCount = length / sizeof(struct kinfo_proc); - for (i = 0; i < resultCount; i++) { - struct kinfo_proc *single = &result[i]; - go_darwin_append_proc( - single->kp_proc.p_pid, - single->kp_eproc.e_ppid, - single->kp_proc.p_comm); - } - -ERREXIT: - if (result != NULL) { - free(result); - } - - if (err != 0) { - return errno; - } - return 0; -} - -#endif diff --git a/vendor/vendor.json b/vendor/vendor.json index 32a5cb8e3..3d4d0f28a 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -1878,6 +1878,13 @@ "path": "github.com/mitchellh/go-linereader", "revision": "07bab5fdd9580500aea6ada0e09df4aa28e68abd" }, + { + "checksumSHA1": "DVXnx4zyb0wkeIdIRjwjvR7Dslo=", + "origin": "github.com/hashicorp/terraform/vendor/github.com/mitchellh/go-ps", + "path": "github.com/mitchellh/go-ps", + "revision": "e2d21980687ce16e58469d98dcee92d27fbbd7fb", + "revisionTime": "2016-08-22T16:54:47Z" + }, { "path": "github.com/mitchellh/hashstructure", "revision": "6b17d669fac5e2f71c16658d781ec3fdd3802b69"