minimal process wrapper
This commit is contained in:
commit
f9e967b7d6
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
parent
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2018 Abiola Ibrahim, 2022 xenofem
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
13
README.md
Normal file
13
README.md
Normal file
|
@ -0,0 +1,13 @@
|
|||
# parent
|
||||
|
||||
A minimal process wrapper that just passes signals to its child. This
|
||||
is a more pared-down version of https://github.com/abiosoft/parent ,
|
||||
with no interpretation of arguments, just passing everything to the
|
||||
child exactly as it's received; it does also attempt to mimic its
|
||||
child's exit status if possible.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
parent <command> [<args>...]
|
||||
```
|
55
main.go
Normal file
55
main.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) <= 1 {
|
||||
exitErr("Usage: parent <command> [<args>...]")
|
||||
}
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c)
|
||||
|
||||
cmd := exec.Command(os.Args[1], os.Args[2:]...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Env = os.Environ()
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
exitErr(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
for sig := range c {
|
||||
cmd.Process.Signal(sig)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
var e *exec.ExitError
|
||||
if errors.As(err, &e) {
|
||||
if e.ProcessState.Exited() {
|
||||
os.Exit(e.ProcessState.ExitCode())
|
||||
} else {
|
||||
status, ok := e.ProcessState.Sys().(syscall.WaitStatus)
|
||||
if ok && status.Signaled() {
|
||||
os.Exit(128 + int(status.Signal()))
|
||||
}
|
||||
}
|
||||
}
|
||||
exitErr(err)
|
||||
}
|
||||
}
|
||||
|
||||
func exitErr(errs ...interface{}) {
|
||||
fmt.Fprintln(os.Stderr, errs...)
|
||||
os.Exit(1)
|
||||
}
|
Loading…
Reference in a new issue