]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
main ipc_activity: use goto to simplify nesting
authorVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 24 Aug 2017 11:50:51 +0000 (13:50 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 24 Aug 2017 11:51:26 +0000 (13:51 +0200)
daemon/main.c

index ad665499097ce5ade28be97c8a3ea14c2f0ac1da..cdf622b20b33af13ebaa053235f6b8726176665c 100644 (file)
@@ -209,7 +209,7 @@ static bool ipc_readall(int fd, char *dst, size_t len)
        return true;
 }
 
-static void ipc_activity(uv_poll_thandle, int status, int events)
+static void ipc_activity(uv_poll_t *handle, int status, int events)
 {
        struct engine *engine = handle->data;
        if (status != 0) {
@@ -222,40 +222,41 @@ static void ipc_activity(uv_poll_t* handle, int status, int events)
        (void) uv_fileno((uv_handle_t *)(handle), &fd);
        /* Read expression from IPC pipe */
        uint32_t len = 0;
-       if (ipc_readall(fd, (char *)&len, sizeof(len))) {
-               auto_free char *rbuf = NULL;
-               if (len < UINT32_MAX) {
-                       rbuf = malloc(len + 1);
-               } else {
-                       errno = EINVAL;
-               }
-               if (!rbuf) {
-                       kr_log_error("[system] ipc: %s\n", strerror(errno));
-                       engine_stop(engine); /* Panic and stop this fork. */
-                       return;
-               }
-               if (ipc_readall(fd, rbuf, len)) {
-                       rbuf[len] = '\0';
-                       /* Run expression */
-                       const char *message = "";
-                       int ret = engine_ipc(engine, rbuf);
-                       if (ret > 0) {
-                               message = lua_tostring(engine->L, -1);
-                       }
-                       /* Send response back */
-                       len = strlen(message);
-                       if (write(fd, &len, sizeof(len)) != sizeof(len) ||
-                               write(fd, message, len) != len) {
-                               kr_log_error("[system] ipc: %s\n", strerror(errno));
-                       }
-                       /* Clear the Lua stack */
-                       lua_settop(engine->L, 0);
-               } else {
-                       kr_log_error("[system] ipc: %s\n", strerror(errno));
-               }
+       auto_free char *rbuf = NULL;
+       if (!ipc_readall(fd, (char *)&len, sizeof(len))) {
+               goto failure;
+       }
+       if (len < UINT32_MAX) {
+               rbuf = malloc(len + 1);
        } else {
+               errno = EINVAL;
+       }
+       if (!rbuf) {
+               kr_log_error("[system] ipc: %s\n", strerror(errno));
+               engine_stop(engine); /* Panic and stop this fork. */
+               return;
+       }
+       if (!ipc_readall(fd, rbuf, len)) {
+               goto failure;
+       }
+       rbuf[len] = '\0';
+       /* Run expression */
+       const char *message = "";
+       int ret = engine_ipc(engine, rbuf);
+       if (ret > 0) {
+               message = lua_tostring(engine->L, -1);
+       }
+       /* Send response back */
+       len = strlen(message);
+       if (write(fd, &len, sizeof(len)) != sizeof(len) ||
+               write(fd, message, len) != len) {
                kr_log_error("[system] ipc: %s\n", strerror(errno));
        }
+       /* Clear the Lua stack */
+       lua_settop(engine->L, 0);
+       return;
+failure:
+       kr_log_error("[system] ipc: %s\n", strerror(errno));
 }
 
 static bool ipc_watch(uv_loop_t *loop, struct engine *engine, int fd)