Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wifi: close Linux genetlink Conn on error #5

Merged
merged 1 commit into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func newClient() (*client, error) {
func initClient(c genl) (*client, error) {
family, err := c.GetFamily(nl80211.GenlName)
if err != nil {
// Ensure the genl socket is closed on error to avoid leaking file
// descriptors.
_ = c.Close()
return nil, err
}

Expand Down
26 changes: 24 additions & 2 deletions client_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ func TestLinux_clientStationInfoOK(t *testing.T) {
}
}

func TestLinux_initClientErrorCloseConn(t *testing.T) {
// Assume that nl80211 does not exist on this system.
// The genetlink Conn should be closed to avoid leaking file descriptors.
g := &testGENL{
err: os.ErrNotExist,
}

if _, err := initClient(g); err == nil {
t.Fatal("no error occurred, but expected one")
}

if want, got := true, g.closed; want != got {
t.Fatalf("unexpected genetlink conn closed:\n- want: %v\n- got: %v",
want, got)
}
}

type check struct {
Command uint8
Flags netlink.HeaderFlags
Expand Down Expand Up @@ -413,14 +430,19 @@ var _ genl = &testGENL{}
type testGENL struct {
family genetlink.Family
messages []genetlink.Message
closed bool
err error

precheck func(m genetlink.Message, family uint16, flags netlink.HeaderFlags)
}

func (g *testGENL) Close() error { return nil }
func (g *testGENL) Close() error {
g.closed = true
return nil
}

func (g *testGENL) GetFamily(name string) (genetlink.Family, error) {
return g.family, nil
return g.family, g.err
}

func (g *testGENL) Execute(m genetlink.Message, family uint16, flags netlink.HeaderFlags) ([]genetlink.Message, error) {
Expand Down