diff options
author | Alessio Caiazza <acaiazza@gitlab.com> | 2019-05-21 15:49:34 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-05-21 15:49:34 +0300 |
commit | b0966f215c97946d372c0da3d8854b0d0813432a (patch) | |
tree | d1b263fd2fa81d726a4e19edde4546b8d937acb3 /cmd/gitaly-wrapper | |
parent | 80872b805c01310fb695dc19afb5b1b8e6d63e65 (diff) |
Restore FreeBSD compatibility
Diffstat (limited to 'cmd/gitaly-wrapper')
-rw-r--r-- | cmd/gitaly-wrapper/main.go | 19 | ||||
-rw-r--r-- | cmd/gitaly-wrapper/main_test.go | 64 | ||||
-rw-r--r-- | cmd/gitaly-wrapper/proc_path_darwin.go | 25 | ||||
-rw-r--r-- | cmd/gitaly-wrapper/proc_path_linux.go | 10 |
4 files changed, 17 insertions, 101 deletions
diff --git a/cmd/gitaly-wrapper/main.go b/cmd/gitaly-wrapper/main.go index 4a8ea6ae2..79f100f6d 100644 --- a/cmd/gitaly-wrapper/main.go +++ b/cmd/gitaly-wrapper/main.go @@ -13,6 +13,7 @@ import ( "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/internal/config" + "gitlab.com/gitlab-org/gitaly/internal/ps" ) const ( @@ -43,7 +44,7 @@ func main() { log.WithError(err).Fatal("find gitaly") } - if gitaly != nil { + if gitaly != nil && isGitaly(gitaly, gitalyBin) { log.Info("adopting a process") } else { log.Info("spawning a process") @@ -82,10 +83,6 @@ func findGitaly() (*os.Process, error) { } if isAlive(gitaly) { - if ok, err := isGitaly(pid); !ok { - return nil, err - } - return gitaly, nil } @@ -143,17 +140,17 @@ func isAlive(p *os.Process) bool { return p.Signal(syscall.Signal(0)) == nil } -func isGitaly(pid int) (bool, error) { - command, err := procPath(pid) +func isGitaly(p *os.Process, gitalyBin string) bool { + command, err := ps.Comm(p.Pid) if err != nil { - return false, err + return false } - if path.Base(command) == "gitaly" { - return true, nil + if path.Base(command) == path.Base(gitalyBin) { + return true } - return false, nil + return false } func pidFile() string { diff --git a/cmd/gitaly-wrapper/main_test.go b/cmd/gitaly-wrapper/main_test.go index cde68e840..f4d98a374 100644 --- a/cmd/gitaly-wrapper/main_test.go +++ b/cmd/gitaly-wrapper/main_test.go @@ -1,11 +1,9 @@ package main import ( - "io" "io/ioutil" "os" "os/exec" - "path" "strconv" "testing" @@ -33,60 +31,16 @@ func TestStolenPid(t *testing.T) { require.NoError(t, err) require.NoError(t, pidFile.Close()) - gitaly, err := findGitaly() + tail, err := findGitaly() require.NoError(t, err) - require.Nil(t, gitaly) -} - -func TestExistingGitaly(t *testing.T) { - defer func(oldValue string) { - os.Setenv(config.EnvPidFile, oldValue) - }(os.Getenv(config.EnvPidFile)) - - tmpDir, err := ioutil.TempDir("", "gitaly-pid") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) - - pidFile := path.Join(tmpDir, "gitaly.pid") - fakeGitaly := path.Join(tmpDir, "gitaly") - - require.NoError(t, buildFakeGitaly(t, fakeGitaly), "Can't build a fake gitaly binary") - - os.Setenv(config.EnvPidFile, pidFile) - - cmd := exec.Command(fakeGitaly, "-f") - require.NoError(t, cmd.Start()) - defer cmd.Process.Kill() - - require.NoError(t, ioutil.WriteFile(pidFile, []byte(strconv.Itoa(cmd.Process.Pid)), 0644)) - - gitaly, err := findGitaly() - require.NoError(t, err) - require.NotNil(t, gitaly) - require.Equal(t, cmd.Process.Pid, gitaly.Pid) - gitaly.Kill() -} - -func buildFakeGitaly(t *testing.T, path string) error { - tail := exec.Command("tail", "-f") - require.NoError(t, tail.Start()) - defer tail.Process.Kill() - - tailPath, err := procPath(tail.Process.Pid) - require.NoError(t, err) - tail.Process.Kill() - - src, err := os.Open(tailPath) - require.NoError(t, err) - defer src.Close() - - out, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0744) - require.NoError(t, err) - defer out.Close() + require.NotNil(t, tail) + require.Equal(t, cmd.Process.Pid, tail.Pid) - if _, err := io.Copy(out, src); err != nil { - return err - } + t.Run("stolen", func(t *testing.T) { + require.False(t, isGitaly(tail, "/path/to/gitaly")) + }) - return out.Sync() + t.Run("not stolen", func(t *testing.T) { + require.True(t, isGitaly(tail, "/path/to/tail")) + }) } diff --git a/cmd/gitaly-wrapper/proc_path_darwin.go b/cmd/gitaly-wrapper/proc_path_darwin.go deleted file mode 100644 index 4979cc203..000000000 --- a/cmd/gitaly-wrapper/proc_path_darwin.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -// #include <libproc.h> -// #include <stdlib.h> -import "C" - -import ( - "fmt" - "unsafe" -) - -func procPath(pid int) (string, error) { - // MacOS does not implement procfs, this simple function calls proc_pidpath from MacOS libproc - // https://opensource.apple.com/source/xnu/xnu-2422.1.72/libsyscall/wrappers/libproc/libproc.h.auto.html - // this is just for testing purpose as we do not support MacOS as a production environment - - buf := C.CString(string(make([]byte, C.PROC_PIDPATHINFO_MAXSIZE))) - defer C.free(unsafe.Pointer(buf)) - - if ret, err := C.proc_pidpath(C.int(pid), unsafe.Pointer(buf), C.PROC_PIDPATHINFO_MAXSIZE); ret <= 0 { - return "", fmt.Errorf("failed process path retrieval: %v", err) - } - - return C.GoString(buf), nil -} diff --git a/cmd/gitaly-wrapper/proc_path_linux.go b/cmd/gitaly-wrapper/proc_path_linux.go deleted file mode 100644 index d48828a93..000000000 --- a/cmd/gitaly-wrapper/proc_path_linux.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import ( - "os" - "strconv" -) - -func procPath(pid int) (path string, err error) { - return os.Readlink("/proc/" + strconv.Itoa(pid) + "/exe") -} |