mirror of https://github.com/perkeep/perkeep.git
Merge "find the path to the executable of the running instance to restart camli properly"
This commit is contained in:
commit
3fe746e3aa
|
@ -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
|
||||
}
|
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue