]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
worker interactive mode: check stdin type
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 10 Dec 2018 16:06:37 +0000 (17:06 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 10 Dec 2018 16:15:47 +0000 (17:15 +0100)
In particular, redirection from a file was leading to abort(),
so we provide an error message instead.

daemon/main.c

index 4e3e379cf2d3b830fffe07ab525916618e7004c7..5b212e78b8c32f0a56561f356066b2dd65644670 100644 (file)
@@ -414,8 +414,24 @@ static void help(int argc, char *argv[])
               " [rundir]             Path to the working directory (default: .)\n");
 }
 
+/** \return exit code for main()  */
 static int run_worker(uv_loop_t *loop, struct engine *engine, fd_array_t *ipc_set, bool leader, struct args *args)
 {
+       /* Only some kinds of stdin work with uv_pipe_t.
+        * Otherwise we would abort() from libuv e.g. with </dev/null */
+       if (args->interactive) switch (uv_guess_handle(0)) {
+       case UV_TTY:            /* standard terminal */
+       case UV_NAMED_PIPE:     /* echo 'quit()' | kresd ... */
+               break;
+       default:
+               kr_log_error(
+                       "[system] error: standard input is not a terminal or pipe; "
+                       "use '-f 1' if you want non-interactive mode.  "
+                       "Commands can be simply added to your configuration file or sent over the tty/$PID control socket.\n"
+                       );
+               return 1;
+       }
+
        /* Control sockets or TTY */
        auto_free char *sock_file = NULL;
        uv_pipe_t pipe;
@@ -462,7 +478,7 @@ static int run_worker(uv_loop_t *loop, struct engine *engine, fd_array_t *ipc_se
                unlink(sock_file);
        }
        uv_close((uv_handle_t *)&pipe, NULL); /* Seems OK even on the stopped loop. */
-       return kr_ok();
+       return 0;
 }
 
 #ifdef HAS_SYSTEMD
@@ -807,11 +823,6 @@ int main(int argc, char **argv)
 
        /* Run the event loop */
        ret = run_worker(loop, &engine, &ipc_set, fork_id == 0, &args);
-       if (ret != 0) {
-               perror("[system] worker failed");
-               ret = EXIT_FAILURE;
-               goto cleanup;
-       }
 
 cleanup:/* Cleanup. */
        engine_deinit(&engine);