Compare commits
2 Commits
ec5f7bb75a
...
51e4774bb2
Author | SHA1 | Date | |
---|---|---|---|
51e4774bb2 | |||
8da229bf39 |
64
main.go
Normal file
64
main.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package Podman
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run(args ...string) error {
|
||||||
|
podman := exec.Command("/user/podman", args...)
|
||||||
|
podman.Env = append(os.Environ(), "TMPDIR=/tmp")
|
||||||
|
podman.Stdin = os.Stdin
|
||||||
|
podman.Stdout = os.Stdout
|
||||||
|
podman.Stderr = os.Stderr
|
||||||
|
|
||||||
|
exit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
podman.Process.Signal(<-exit)
|
||||||
|
}()
|
||||||
|
if err := podman.Run(); err != nil {
|
||||||
|
return fmt.Errorf("%v: %v", podman.Args, err)
|
||||||
|
}
|
||||||
|
exit <- os.Interrupt
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var numericRe = regexp.MustCompile(`^[0-9]+$`)
|
||||||
|
|
||||||
|
func Signal(name string, sig os.Signal) error {
|
||||||
|
fis, err := ioutil.ReadDir("/proc")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, fi := range fis {
|
||||||
|
if !fi.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !numericRe.MatchString(fi.Name()) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join("/proc", fi.Name(), "cmdline"))
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue // process vanished
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(b), name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pid, _ := strconv.Atoi(fi.Name()) // already verified to be numeric
|
||||||
|
p, _ := os.FindProcess(pid)
|
||||||
|
return p.Signal(sig)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Process not found: %s", name)
|
||||||
|
}
|
Reference in New Issue
Block a user