123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #include <sys/prctl.h>
- #include "libbb.h"
- #include "mail.h"
- static void signal_handler(int signo)
- {
- #define err signo
- if (SIGALRM == signo) {
- bb_error_msg_and_die("timed out");
- }
-
- 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("helper exited (%u)", WEXITSTATUS(err));
- }
- }
- #undef err
- }
- void FAST_FUNC launch_helper(const char **argv)
- {
-
- int i;
- int pipes[4];
- xpipe(pipes);
- xpipe(pipes + 2);
-
- bb_signals(0
- + (1 << SIGCHLD)
- + (1 << SIGALRM)
- , signal_handler);
- G.helper_pid = xvfork();
- i = (!G.helper_pid) * 2;
- close(pipes[i + 1]);
- close(pipes[2 - i]);
- xmove_fd(pipes[i], STDIN_FILENO);
- xmove_fd(pipes[3 - i], STDOUT_FILENO);
-
-
-
- if (!G.helper_pid) {
-
-
- prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
-
-
- BB_EXECVP_or_die((char**)argv);
- }
-
- }
- char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
- {
- char *msg;
- if (timeout)
- alarm(timeout);
- msg = (char*)fmt;
- if (fmt) {
- msg = xasprintf(fmt, param);
- if (verbose)
- bb_error_msg("send:'%s'", msg);
- printf("%s\r\n", msg);
- }
- fflush_all();
- return msg;
- }
- void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
- {
- enum {
- SRC_BUF_SIZE = 57,
- DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
- };
- #define src_buf text
- char src[SRC_BUF_SIZE];
- FILE *fp = fp;
- ssize_t len = len;
- char dst_buf[DST_BUF_SIZE + 1];
- if (fname) {
- fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
- src_buf = src;
- } else if (text) {
-
-
- len = strlen(text);
- } else
- return;
- while (1) {
- size_t size;
- if (fname) {
- size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
- if ((ssize_t)size < 0)
- bb_perror_msg_and_die(bb_msg_read_error);
- } else {
- size = len;
- if (len > SRC_BUF_SIZE)
- size = SRC_BUF_SIZE;
- }
- if (!size)
- break;
-
- bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
- if (fname) {
- puts(eol);
- } else {
- src_buf += size;
- len -= size;
- }
- fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
- }
- if (fname && NOT_LONE_DASH(fname))
- fclose(fp);
- #undef src_buf
- }
- void FAST_FUNC get_cred_or_die(int fd)
- {
- if (isatty(fd)) {
- G.user = xstrdup(bb_ask(fd, 0, "User: "));
- G.pass = xstrdup(bb_ask(fd, 0, "Password: "));
- } else {
- G.user = xmalloc_reads(fd, NULL);
- G.pass = xmalloc_reads(fd, NULL);
- }
- if (!G.user || !*G.user || !G.pass)
- bb_error_msg_and_die("no username or password");
- }
|