From 30316e3fa9afd65cbdc629c56740f79b30459827 Mon Sep 17 00:00:00 2001 From: Eugene Yakubovich Date: Mon, 15 Jun 2015 12:31:07 -0700 Subject: [PATCH] dhcp: make netns path absolute prior to RPC The dhcp daemon may be running with a different cwd and so the netns paths need to be absolute. This patch also refactors the code to pull out the common, RPC parts, out into a separate function. --- plugins/ipam/dhcp/main.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/plugins/ipam/dhcp/main.go b/plugins/ipam/dhcp/main.go index 2bc66257..ca802719 100644 --- a/plugins/ipam/dhcp/main.go +++ b/plugins/ipam/dhcp/main.go @@ -18,6 +18,7 @@ import ( "fmt" "net/rpc" "os" + "path/filepath" "github.com/appc/cni/pkg/plugin" "github.com/appc/cni/pkg/skel" @@ -34,30 +35,38 @@ func main() { } func cmdAdd(args *skel.CmdArgs) error { - client, err := rpc.DialHTTP("unix", socketPath) - if err != nil { - return fmt.Errorf("error dialing DHCP daemon: %v", err) + result := plugin.Result{} + if err := rpcCall("DHCP.Allocate", args, &result); err != nil { + return err } - - result := &plugin.Result{} - err = client.Call("DHCP.Allocate", args, result) - if err != nil { - return fmt.Errorf("error calling DHCP.Add: %v", err) - } - return result.Print() } func cmdDel(args *skel.CmdArgs) error { + result := struct{}{} + if err := rpcCall("DHCP.Release", args, &result); err != nil { + return fmt.Errorf("error dialing DHCP daemon: %v", err) + } + return nil +} + +func rpcCall(method string, args *skel.CmdArgs, result interface{}) error { client, err := rpc.DialHTTP("unix", socketPath) if err != nil { return fmt.Errorf("error dialing DHCP daemon: %v", err) } - dummy := struct{}{} - err = client.Call("DHCP.Release", args, &dummy) + // The daemon may be running under a different working dir + // so make sure the netns path is absolute. + netns, err := filepath.Abs(args.Netns) if err != nil { - return fmt.Errorf("error calling DHCP.Del: %v", err) + return fmt.Errorf("failed to make %q an absolute path: %v", args.Netns, err) + } + args.Netns = netns + + err = client.Call(method, args, result) + if err != nil { + return fmt.Errorf("error calling %v: %v", method, err) } return nil