sendmail: document and fix usage of fd #4, fix check for helper failure.
authorDenis Vlasenko <vda.linux@googlemail.com>
Wed, 4 Mar 2009 17:56:00 +0000 (17:56 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Wed, 4 Mar 2009 17:56:00 +0000 (17:56 -0000)
function                                             old     new   delta
packed_usage                                       25663   25694     +31
signal_handler                                       191     215     +24
kill_helper                                           22      31      +9
launch_helper                                        194     184     -10
get_cred_or_die                                      142     129     -13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/2 up/down: 64/-23)             Total: 41 bytes

include/usage.h
mailutils/mail.c

index 3363e10..de6f037 100644 (file)
      "\n       -H 'prog args'  Run connection helper. E.g. openssl for encryption:" \
      "\n                       -H 'exec openssl s_client -quiet -tls1 -starttls smtp" \
      "\n                                       -connect smtp.gmail.com:25' <email.txt" \
+     "\n                                       [4<username_and_passwd.txt]" \
      "\n       -S server[:port] Server" \
        ) \
        USE_FEATURE_SENDMAIL_MAILXX( \
index f309848..71f46c8 100644 (file)
 
 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);
+       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]);
+
+       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
                BB_EXECVP(*argv, (char **)argv);
                _exit(127);
        }
-       // parent: check whether child is alive
+
+       // parent
        bb_signals(0
                + (1 << SIGCHLD)
                + (1 << SIGALRM)
                , signal_handler);
-       signal_handler(SIGCHLD);
+       // check whether child is alive
+       //redundant:signal_handler(SIGCHLD);
        // child seems OK -> parent goes on
        atexit(kill_helper);
 }
@@ -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");
 }