]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
commands: tell mainloop to reap client fd on error
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 25 Nov 2017 16:51:01 +0000 (17:51 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 17 Dec 2017 14:58:44 +0000 (15:58 +0100)
This is the proper way to handle errors.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/commands.c
src/lxc/commands_utils.c

index 5b82fadd2b70f94882c7357ba167a17285b72ae0..c3fad8ff718bfd21041f43a7d347f2868e0e7860 100644 (file)
@@ -858,8 +858,8 @@ int lxc_cmd_add_state_client(const char *name, const char *lxcpath,
 
        state = PTR_TO_INT(cmd.rsp.data);
        if (state < MAX_STATE) {
-               TRACE("Container is already in requested state %s",
-                     lxc_state2str(state));
+               TRACE("Container is already in requested state %s", lxc_state2str(state));
+               close(cmd.rsp.ret);
                return state;
        }
 
@@ -871,21 +871,35 @@ int lxc_cmd_add_state_client(const char *name, const char *lxcpath,
 static int lxc_cmd_add_state_client_callback(int fd, struct lxc_cmd_req *req,
                                             struct lxc_handler *handler)
 {
+       int ret;
        struct lxc_cmd_rsp rsp = {0};
 
        if (req->datalen < 0)
-               return -1;
+               goto reap_client_fd;
 
        if (req->datalen > (sizeof(lxc_state_t) * MAX_STATE))
-               return -1;
+               goto reap_client_fd;
 
        if (!req->data)
-               return -1;
+               goto reap_client_fd;
 
        rsp.ret = lxc_add_state_client(fd, handler, (lxc_state_t *)req->data);
+       if (rsp.ret < 0)
+               goto reap_client_fd;
+
        rsp.data = INT_TO_PTR(rsp.ret);
 
-       return lxc_cmd_rsp_send(fd, &rsp);
+       ret = lxc_cmd_rsp_send(fd, &rsp);
+       if (ret < 0)
+               goto reap_client_fd;
+
+       return 0;
+
+reap_client_fd:
+       /* Special indicator to lxc_cmd_handler() to close the fd and do related
+        * cleanup.
+        */
+       return 1;
 }
 
 static int lxc_cmd_process(int fd, struct lxc_cmd_req *req,
index 986d9904b35d024e8100431d0659f3115f45ef07..98b5262ebabf394db0c1e3efec0a803d5fdd2534 100644 (file)
@@ -219,6 +219,7 @@ int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler,
        } else {
                process_unlock();
                free(newclient);
+               free(tmplist);
                return state;
        }