From 62b5da6f9975dc17657a31c65fa7f7e8032acf9e Mon Sep 17 00:00:00 2001 From: mpl Date: Tue, 13 Nov 2012 23:31:07 +0100 Subject: [PATCH] find the path to the executable of the running instance to restart camli properly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds the functionality to find the path to the currently running executable, so that camlistore can be restarted without à priori knowledge of cwd. It should work for linux, netBSD and freeBSD, and it might work for openBSD. The naive (os.Args[0]) approach is still used with darwin. Reference: https://codereview.appspot.com/6736069/ Change-Id: Icc9fc2b4e1b3542cfbe6c457c1fefbe762eff2f1 --- pkg/osutil/restart_freebsd.go | 54 +++++++++++++++++++++++++++++++++++ pkg/osutil/restart_unix.go | 29 ++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 pkg/osutil/restart_freebsd.go diff --git a/pkg/osutil/restart_freebsd.go b/pkg/osutil/restart_freebsd.go new file mode 100644 index 000000000..a5202e6d9 --- /dev/null +++ b/pkg/osutil/restart_freebsd.go @@ -0,0 +1,54 @@ +// +build freebsd + +/* +Copyright 2012 The Camlistore Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package osutil + +import ( + "errors" + "os" + "runtime" + "syscall" + "unsafe" +) + +func init() { + osSelfPath = selfPathFreeBSD +} + +func selfPathFreeBSD() (string, error) { + mib := [4]int32{1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1} + + n := uintptr(0) + // get length + _, _, err := syscall.Syscall6(syscall.SYS__SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, 0, uintptr(unsafe.Pointer(&n)), 0, 0) + if err != 0 { + return "", err + } + if n == 0 { // shouldn't happen + return "", nil + } + buf := make([]byte, n) + _, _, err = syscall.Syscall6(syscall.SYS__SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 4, uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&n)), 0, 0) + if err != 0 { + return "", err + } + if n == 0 { // shouldn't happen + return "", nil + } + return string(buf[:n-1]), nil +} diff --git a/pkg/osutil/restart_unix.go b/pkg/osutil/restart_unix.go index 7c1eb64b1..a3a57940a 100644 --- a/pkg/osutil/restart_unix.go +++ b/pkg/osutil/restart_unix.go @@ -19,13 +19,40 @@ limitations under the License. package osutil import ( + "errors" "os" + "runtime" "syscall" ) +// if non-nil, osSelfPath is used from selfPath. +var osSelfPath func() (string, error) + +func selfPath() (string, error) { + if f := osSelfPath; f != nil { + return f() + } + switch runtime.GOOS { + case "linux": + return "/proc/self/exe", nil + case "netbsd": + return "/proc/curproc/exe", nil + case "openbsd": + return "/proc/curproc/file", nil + case "darwin": + // TODO(mpl): shall we do the whole dance for darwin, which requires modifying the runtime as well, if I understood minux's work correctly? + return os.Args[0], nil + } + return "", errors.New("No restart because selfPath() not implemented for " + runtime.GOOS) +} + // restartProcess returns an error if things couldn't be // restarted. On success, this function never returns // because the process becomes the new process. func RestartProcess() error { - return syscall.Exec(os.Args[0], os.Args, os.Environ()) + path, err := selfPath() + if err != nil { + return err + } + return syscall.Exec(path, os.Args, os.Environ()) }