tools/xenstore: get_handle: Allocate struct before opening fd
authorIan Jackson <ian.jackson@eu.citrix.com>
Fri, 15 Sep 2017 12:44:50 +0000 (13:44 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Wed, 11 Oct 2017 11:51:22 +0000 (12:51 +0100)
Now we can also abolish the temporary local variable "fd" and simply
use h->fd.

This ordering is necessary to be able to call
xentoolcore__register_active_handle sensibly.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
tools/xenstore/xs.c

index 65cba86f93d84c91425a46766df20f333b925cd6..7f85bb24d8493b3dcfce9fe98e5f4032736b958b 100644 (file)
@@ -223,27 +223,26 @@ static struct xs_handle *get_handle(const char *connect_to)
 {
        struct stat buf;
        struct xs_handle *h = NULL;
-       int fd = -1, saved_errno;
+       int saved_errno;
+
+       h = malloc(sizeof(*h));
+       if (h == NULL)
+               goto err;
+
+       memset(h, 0, sizeof(*h));
+       h->fd = -1;
 
        if (stat(connect_to, &buf) != 0)
                goto err;
 
        if (S_ISSOCK(buf.st_mode))
-               fd = get_socket(connect_to);
+               h->fd = get_socket(connect_to);
        else
-               fd = get_dev(connect_to);
+               h->fd = get_dev(connect_to);
 
-       if (fd == -1)
+       if (h->fd == -1)
                goto err;
 
-       h = malloc(sizeof(*h));
-       if (h == NULL)
-               goto err;
-
-       memset(h, 0, sizeof(*h));
-
-       h->fd = fd;
-
        INIT_LIST_HEAD(&h->reply_list);
        INIT_LIST_HEAD(&h->watch_list);
 
@@ -267,7 +266,10 @@ static struct xs_handle *get_handle(const char *connect_to)
 err:
        saved_errno = errno;
 
-       if (fd >= 0) close(fd);
+       if (h) {
+               if (h->fd >= 0)
+                       close(h->fd);
+       }
        free(h);
 
        errno = saved_errno;