X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=mailutils%2Fmail.c;h=8e52a3efc319ae428cd282662ba76468d57dde1d;hb=14e289b3246b4266499ffc5747dcc5c98bdaf5b9;hp=f3098489705bc348047a5c0e2fa664ae4d04ef16;hpb=82b142996625d6bf20ee667ce602496cb270fccc;p=oweals%2Fbusybox.git diff --git a/mailutils/mail.c b/mailutils/mail.c index f30984897..8e52a3efc 100644 --- a/mailutils/mail.c +++ b/mailutils/mail.c @@ -4,16 +4,17 @@ * * Copyright (C) 2008 by Vladimir Dronnikov * - * Licensed under GPLv2, see file LICENSE in this tarball for details. + * Licensed under GPLv2, see file LICENSE in this source tree. */ #include "libbb.h" #include "mail.h" static void kill_helper(void) { - // TODO!!!: is there more elegant way to terminate child on program failure? - if (G.helper_pid > 0) + if (G.helper_pid > 0) { kill(G.helper_pid, SIGTERM); + G.helper_pid = 0; + } } // generic signal handler @@ -26,44 +27,50 @@ static void signal_handler(int signo) } // SIGCHLD. reap zombies - if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) + if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) { + if (WIFSIGNALED(err)) + bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(err)); if (WIFEXITED(err)) { G.helper_pid = 0; if (WEXITSTATUS(err)) - bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err)); + bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err)); } + } #undef err } void FAST_FUNC launch_helper(const char **argv) { // setup vanilla unidirectional pipes interchange - int idx; + int i; int pipes[4]; xpipe(pipes); - xpipe(pipes+2); - G.helper_pid = vfork(); - if (G.helper_pid < 0) - bb_perror_msg_and_die("vfork"); - idx = (!G.helper_pid) * 2; - xdup2(pipes[idx], STDIN_FILENO); - xdup2(pipes[3-idx], STDOUT_FILENO); - if (ENABLE_FEATURE_CLEAN_UP) - for (int i = 4; --i >= 0; ) - if (pipes[i] > STDOUT_FILENO) - close(pipes[i]); - if (!G.helper_pid) { - // child: try to execute connection helper - BB_EXECVP(*argv, (char **)argv); - _exit(127); - } - // parent: check whether child is alive + xpipe(pipes + 2); + + // NB: handler must be installed before vfork bb_signals(0 + (1 << SIGCHLD) + (1 << SIGALRM) , signal_handler); - signal_handler(SIGCHLD); + + G.helper_pid = xvfork(); + + i = (!G.helper_pid) * 2; // for parent:0, for child:2 + close(pipes[i + 1]); // 1 or 3 - closing one write end + close(pipes[2 - i]); // 2 or 0 - closing one read end + xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end + xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - other write end + + if (!G.helper_pid) { + // child: try to execute connection helper + // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec + BB_EXECVP_or_die((char**)argv); + } + + // parent + // check whether child is alive + //redundant:signal_handler(SIGCHLD); // child seems OK -> parent goes on atexit(kill_helper); } @@ -77,7 +84,7 @@ const FAST_FUNC char *command(const char *fmt, const char *param) msg = xasprintf(fmt, param); printf("%s\r\n", msg); } - fflush(stdout); + fflush_all(); return msg; } @@ -226,17 +233,13 @@ void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream) */ void FAST_FUNC get_cred_or_die(int fd) { - // either from TTY if (isatty(fd)) { - G.user = xstrdup(bb_ask_stdin("User: ")); - G.pass = xstrdup(bb_ask_stdin("Password: ")); - // or from STDIN + G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: ")); + G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: ")); } else { - FILE *fp = fdopen(fd, "r"); - G.user = xmalloc_fgetline(fp); - G.pass = xmalloc_fgetline(fp); - fclose(fp); + G.user = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL); + G.pass = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL); } - if (!G.user || !*G.user || !G.pass || !*G.pass) + if (!G.user || !*G.user || !G.pass) bb_error_msg_and_die("no username or password"); }