proxy: fix up the vsock interface

- don't try to create a `FileConn` because the Go library sees through
  the scam and rejects it
- explicitly keep a reference to the `ctl` file just in case the GC
  decides its dead and should be closed.

Signed-off-by: David Scott <dave.scott@docker.com>
This commit is contained in:
David Scott
2016-04-15 11:03:47 +01:00
parent a321da38e5
commit c24687e2e1
3 changed files with 43 additions and 18 deletions

View File

@@ -8,53 +8,57 @@ import (
"os"
"libproxy"
"strings"
"vsock"
)
func main() {
host, port, container := parseHostContainerAddrs()
err := exposePort(host, port)
ctl, err := exposePort(host, port)
if err != nil {
sendError(err)
}
p, err := libproxy.NewProxy(host, container)
p, err := libproxy.NewProxy(&vsock.VsockAddr{Port: uint(port)}, container)
if err != nil {
sendError(err)
}
go handleStopSignals(p)
sendOK()
p.Run()
ctl.Close() // ensure ctl remains alive and un-GCed until here
os.Exit(0)
}
func exposePort(host net.Addr, port int) error {
func exposePort(host net.Addr, port int) (*os.File, error) {
name := host.String()
log.Printf("exposePort %s\n", name)
err := os.Mkdir("/port/"+name, 0)
if err != nil {
log.Printf("Failed to mkdir /port/%s: %#v\n", name, err)
return err
return nil, err
}
ctl, err := os.OpenFile("/port/"+name+"/ctl", os.O_RDWR, 0)
if err != nil {
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
return err
return nil, err
}
_, err = ctl.WriteString(fmt.Sprintf("%s:%08x", name, port))
if err != nil {
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
return err
return nil, err
}
_, err = ctl.Seek(0, 0)
if err != nil {
log.Printf("Failed to seek on /port/%s/ctl: %#v\n", name, err)
return err
return nil, err
}
results := make([]byte, 100)
count, err := ctl.Read(results)
if err != nil {
log.Printf("Failed to read from /port/%s/ctl: %#v\n", name, err)
return err
return nil, err
}
// We deliberately keep the control file open since 9P clunk
// will trigger a shutdown on the host side.
@@ -63,8 +67,8 @@ func exposePort(host net.Addr, port int) error {
if strings.HasPrefix(response, "ERROR ") {
os.Remove("/port/" + name + "/ctl")
response = strings.Trim(response[6:], " \t\r\n")
return errors.New(response)
return nil, errors.New(response)
}
return nil
// Hold on to a reference to prevent premature GC and close
return ctl, nil
}