mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-12-03 09:17:01 +00:00
Previously the proxy.go would directly call `os.Exit`. This patch causes control to return to `main` where we can tear down any port forward. Signed-off-by: David Scott <dave.scott@docker.com>
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"pkg/proxy"
|
|
)
|
|
|
|
// proxyForever signals the parent success/failure and runs the proxy forever
|
|
func proxyForever(p proxy.Proxy, err error) error {
|
|
f := os.NewFile(3, "signal-parent")
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(f, "1\n%s", err)
|
|
f.Close()
|
|
return err
|
|
}
|
|
go handleStopSignals(p)
|
|
fmt.Fprint(f, "0\n")
|
|
f.Close()
|
|
|
|
// Run will block until the proxy stops
|
|
p.Run()
|
|
return nil
|
|
}
|
|
|
|
// From docker/libnetwork/portmapper/proxy.go:
|
|
|
|
// parseHostContainerAddrs parses the flags passed on reexec to create the TCP or UDP
|
|
// net.Addrs to map the host and container ports
|
|
func parseHostContainerAddrs() (host net.Addr, container net.Addr) {
|
|
var (
|
|
proto = flag.String("proto", "tcp", "proxy protocol")
|
|
hostIP = flag.String("host-ip", "", "host ip")
|
|
hostPort = flag.Int("host-port", -1, "host port")
|
|
containerIP = flag.String("container-ip", "", "container ip")
|
|
containerPort = flag.Int("container-port", -1, "container port")
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
switch *proto {
|
|
case "tcp":
|
|
host = &net.TCPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
|
|
container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
|
|
case "udp":
|
|
host = &net.UDPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
|
|
container = &net.UDPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
|
|
default:
|
|
log.Fatalf("unsupported protocol %s", *proto)
|
|
}
|
|
|
|
return host, container
|
|
}
|
|
|
|
func handleStopSignals(p proxy.Proxy) {
|
|
s := make(chan os.Signal, 10)
|
|
signal.Notify(s, os.Interrupt, syscall.SIGTERM, syscall.SIGSTOP)
|
|
|
|
for range s {
|
|
p.Close()
|
|
}
|
|
}
|