1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
12 static void kill_helper(void)
14 if (G.helper_pid > 0) {
15 kill(G.helper_pid, SIGTERM);
20 // generic signal handler
21 static void signal_handler(int signo)
24 if (SIGALRM == signo) {
26 bb_error_msg_and_die("timed out");
29 // SIGCHLD. reap zombies
30 if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) {
32 bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(err));
36 bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err));
42 void FAST_FUNC launch_helper(const char **argv)
44 // setup vanilla unidirectional pipes interchange
51 // NB: handler must be installed before vfork
57 G.helper_pid = xvfork();
59 i = (!G.helper_pid) * 2; // for parent:0, for child:2
60 close(pipes[i + 1]); // 1 or 3 - closing one write end
61 close(pipes[2 - i]); // 2 or 0 - closing one read end
62 xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end
63 xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - other write end
66 // child: try to execute connection helper
67 // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
68 BB_EXECVP_or_die((char**)argv);
72 // check whether child is alive
73 //redundant:signal_handler(SIGCHLD);
74 // child seems OK -> parent goes on
78 char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
85 msg = xasprintf(fmt, param);
87 bb_error_msg("send:'%s'", msg);
88 printf("%s\r\n", msg);
94 // NB: parse_url can modify url[] (despite const), but only if '@' is there
96 static char* FAST_FUNC parse_url(char *url, char **user, char **pass)
98 // parse [user[:pass]@]host
100 char *s = strchr(url, '@');
101 *user = *pass = NULL;
106 s = strchr(*user, ':');
116 void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
119 SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
120 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
123 char src[SRC_BUF_SIZE];
126 char dst_buf[DST_BUF_SIZE + 1];
129 fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
132 // though we do not call uuencode(NULL, NULL) explicitly
133 // still we do not want to break things suddenly
141 size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
142 if ((ssize_t)size < 0)
143 bb_perror_msg_and_die(bb_msg_read_error);
146 if (len > SRC_BUF_SIZE)
151 // encode the buffer we just read in
152 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
159 fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
161 if (fname && NOT_LONE_DASH(fname))
167 * get username and password from a file descriptor
169 void FAST_FUNC get_cred_or_die(int fd)
172 G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
173 G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
175 G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
176 G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
178 if (!G.user || !*G.user || !G.pass)
179 bb_error_msg_and_die("no username or password");