mirror of
https://github.com/containers/podman.git
synced 2025-12-09 23:27:09 +08:00
Merge pull request #447 from mheon/sig_proxy
Add signal proxying to podman run and attach
This commit is contained in:
@@ -16,6 +16,10 @@ var (
|
||||
Name: "no-stdin",
|
||||
Usage: "Do not attach STDIN. The default is false.",
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
Name: "sig-proxy",
|
||||
Usage: "proxy received signals to the process (default true)",
|
||||
},
|
||||
LatestFlag,
|
||||
}
|
||||
attachDescription = "The podman attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively."
|
||||
@@ -63,6 +67,10 @@ func attachCmd(c *cli.Context) error {
|
||||
return errors.Errorf("you can only attach to running containers")
|
||||
}
|
||||
|
||||
if c.BoolT("sig-proxy") {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
if err := ctr.Attach(c.Bool("no-stdin"), c.String("detach-keys")); err != nil {
|
||||
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
|
||||
}
|
||||
|
||||
@@ -334,10 +334,6 @@ var createFlags = []cli.Flag{
|
||||
Usage: "Size of `/dev/shm`. The format is `<number><unit>`.",
|
||||
Value: "65536k",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "sig-proxy",
|
||||
Usage: "Proxy received signals to the process (default true)",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "stop-signal",
|
||||
Usage: "Signal to stop a container. Default is SIGTERM",
|
||||
|
||||
@@ -117,7 +117,6 @@ type createConfig struct {
|
||||
Resources createResourceConfig
|
||||
Rm bool //rm
|
||||
ShmDir string
|
||||
SigProxy bool //sig-proxy
|
||||
StopSignal syscall.Signal // stop-signal
|
||||
StopTimeout uint // stop-timeout
|
||||
Sysctl map[string]string //sysctl
|
||||
@@ -715,7 +714,6 @@ func parseCreateOpts(c *cli.Context, runtime *libpod.Runtime, imageName string,
|
||||
},
|
||||
Rm: c.Bool("rm"),
|
||||
ShmDir: shmDir,
|
||||
SigProxy: c.Bool("sig-proxy"),
|
||||
StopSignal: stopSignal,
|
||||
StopTimeout: c.Uint("stop-timeout"),
|
||||
Sysctl: sysctl,
|
||||
|
||||
@@ -13,11 +13,16 @@ import (
|
||||
|
||||
var runDescription = "Runs a command in a new container from the given image"
|
||||
|
||||
var runFlags []cli.Flag = append(createFlags, cli.BoolTFlag{
|
||||
Name: "sig-proxy",
|
||||
Usage: "proxy received signals to the process (default true)",
|
||||
})
|
||||
|
||||
var runCommand = cli.Command{
|
||||
Name: "run",
|
||||
Usage: "run a command in a new container",
|
||||
Description: runDescription,
|
||||
Flags: createFlags,
|
||||
Flags: runFlags,
|
||||
Action: runCmd,
|
||||
ArgsUsage: "IMAGE [COMMAND [ARG...]]",
|
||||
SkipArgReorder: true,
|
||||
@@ -133,6 +138,10 @@ func runCmd(c *cli.Context) error {
|
||||
return errors.Wrapf(err, "unable to start container %q", ctr.ID())
|
||||
}
|
||||
|
||||
if c.BoolT("sig-proxy") {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
// Wait for attach to complete
|
||||
err = <-attachChan
|
||||
if err != nil {
|
||||
|
||||
33
cmd/podman/sigproxy.go
Normal file
33
cmd/podman/sigproxy.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"github.com/docker/docker/pkg/signal"
|
||||
"github.com/projectatomic/libpod/libpod"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ProxySignals(ctr *libpod.Container) {
|
||||
sigBuffer := make(chan os.Signal, 128)
|
||||
signal.CatchAll(sigBuffer)
|
||||
|
||||
logrus.Debugf("Enabling signal proxying")
|
||||
|
||||
go func() {
|
||||
for s := range sigBuffer {
|
||||
// Ignore SIGCHLD and SIGPIPE - these are mostly likely
|
||||
// intended for the podman command itself.
|
||||
if s == signal.SIGCHLD || s == signal.SIGPIPE {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := ctr.Kill(uint(s.(syscall.Signal))); err != nil {
|
||||
logrus.Errorf("Error forwarding signal %d to container %s: %v", s, ctr.ID(), err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return
|
||||
}
|
||||
@@ -25,6 +25,10 @@ var (
|
||||
Name: "interactive, i",
|
||||
Usage: "Keep STDIN open even if not attached",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "sig-proxy",
|
||||
Usage: "proxy received signals to the process",
|
||||
},
|
||||
LatestFlag,
|
||||
}
|
||||
startDescription = `
|
||||
@@ -60,6 +64,10 @@ func startCmd(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.Bool("sig-proxy") && !attach {
|
||||
return errors.Wrapf(libpod.ErrInvalidArg, "you cannot use sig-proxy without --attach")
|
||||
}
|
||||
|
||||
runtime, err := getRuntime(c)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error creating libpod runtime")
|
||||
@@ -106,6 +114,10 @@ func startCmd(c *cli.Context) error {
|
||||
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
|
||||
}
|
||||
|
||||
if c.Bool("sig-proxy") {
|
||||
ProxySignals(ctr)
|
||||
}
|
||||
|
||||
// Wait for attach to complete
|
||||
err = <-attachChan
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user