From f9e967b7d66916ec076ff4c5d48d1450e311cbfa Mon Sep 17 00:00:00 2001 From: xenofem Date: Sat, 27 Aug 2022 15:06:26 -0400 Subject: [PATCH] minimal process wrapper --- .gitignore | 1 + LICENSE | 21 +++++++++++++++++++++ README.md | 13 +++++++++++++ go.mod | 3 +++ main.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25dd2a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +parent \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..603bddf --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0e2de5b --- /dev/null +++ b/README.md @@ -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 [...] +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b9841de --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.xeno.science/xenofem/parent + +go 1.18 diff --git a/main.go b/main.go new file mode 100644 index 0000000..c025fea --- /dev/null +++ b/main.go @@ -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 [...]") + } + + 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) +}