2008-11-07 05:12:42 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* helper routines
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-11-07 05:12:42 +05:30
|
|
|
*/
|
2021-01-04 05:48:44 +05:30
|
|
|
#if defined(__linux__)
|
|
|
|
# include <sys/prctl.h>
|
|
|
|
# define PRCTL
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
# include <sys/procctl.h>
|
|
|
|
# define PROCCTL
|
|
|
|
#endif
|
2008-11-07 05:12:42 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include "mail.h"
|
|
|
|
|
2020-12-17 17:21:58 +05:30
|
|
|
// common signal handler
|
2008-11-07 05:12:42 +05:30
|
|
|
static void signal_handler(int signo)
|
|
|
|
{
|
|
|
|
if (SIGALRM == signo) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("timed out");
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
|
2020-12-17 17:21:58 +05:30
|
|
|
// SIGCHLD. reap the zombie if we expect one
|
|
|
|
if (G.helper_pid == 0)
|
|
|
|
return;
|
|
|
|
#define status signo
|
|
|
|
if (safe_waitpid(G.helper_pid, &status, WNOHANG) > 0) {
|
|
|
|
G.helper_pid = 0;
|
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(status));
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
|
|
|
|
bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(status));
|
2009-03-04 23:26:00 +05:30
|
|
|
}
|
2020-12-17 17:21:58 +05:30
|
|
|
#undef status
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC launch_helper(const char **argv)
|
|
|
|
{
|
2020-12-17 17:21:58 +05:30
|
|
|
pid_t pid;
|
|
|
|
struct fd_pair child_out;
|
|
|
|
struct fd_pair child_in;
|
2008-11-07 05:12:42 +05:30
|
|
|
|
2020-12-17 17:21:58 +05:30
|
|
|
xpiped_pair(child_out);
|
|
|
|
xpiped_pair(child_in);
|
2009-03-04 23:26:00 +05:30
|
|
|
|
2009-03-10 21:31:57 +05:30
|
|
|
// NB: handler must be installed before vfork
|
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGCHLD)
|
|
|
|
+ (1 << SIGALRM)
|
|
|
|
, signal_handler);
|
|
|
|
|
2020-12-17 17:25:22 +05:30
|
|
|
fflush_all();
|
|
|
|
pid = xvfork();
|
2020-12-17 17:21:58 +05:30
|
|
|
if (pid == 0) {
|
2017-08-16 21:15:32 +05:30
|
|
|
// child
|
2020-12-17 17:21:58 +05:30
|
|
|
close(child_in.wr);
|
|
|
|
close(child_out.rd);
|
|
|
|
xmove_fd(child_in.rd, STDIN_FILENO);
|
|
|
|
xmove_fd(child_out.wr, STDOUT_FILENO);
|
2017-08-16 21:15:32 +05:30
|
|
|
// if parent dies, get SIGTERM
|
2021-01-04 05:48:44 +05:30
|
|
|
#if defined(PRCTL)
|
2017-08-16 21:15:32 +05:30
|
|
|
prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
|
2021-01-04 05:48:44 +05:30
|
|
|
#elif defined(PROCCTL)
|
|
|
|
{
|
|
|
|
int signum = SIGTERM;
|
|
|
|
procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum);
|
|
|
|
}
|
|
|
|
#endif
|
2017-08-16 21:15:32 +05:30
|
|
|
// try to execute connection helper
|
2009-03-10 21:31:57 +05:30
|
|
|
// NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
|
2010-07-04 04:27:03 +05:30
|
|
|
BB_EXECVP_or_die((char**)argv);
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
2020-12-17 17:25:22 +05:30
|
|
|
G.helper_pid = pid;
|
2020-12-17 17:21:58 +05:30
|
|
|
close(child_out.wr);
|
|
|
|
close(child_in.rd);
|
|
|
|
xmove_fd(child_out.rd, STDIN_FILENO);
|
|
|
|
xmove_fd(child_in.wr, STDOUT_FILENO);
|
2009-03-04 23:26:00 +05:30
|
|
|
|
2017-08-16 21:15:32 +05:30
|
|
|
// parent goes on
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:25 +05:30
|
|
|
void FAST_FUNC send_r_n(const char *s)
|
|
|
|
{
|
|
|
|
if (G.verbose)
|
|
|
|
bb_error_msg("send:'%s'", s);
|
|
|
|
printf("%s\r\n", s);
|
|
|
|
}
|
|
|
|
|
2010-12-20 09:42:39 +05:30
|
|
|
char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
|
2008-11-07 05:12:42 +05:30
|
|
|
{
|
2010-12-20 09:42:39 +05:30
|
|
|
char *msg;
|
2020-12-17 16:54:50 +05:30
|
|
|
if (G.timeout)
|
|
|
|
alarm(G.timeout);
|
2010-12-20 09:42:39 +05:30
|
|
|
msg = (char*)fmt;
|
|
|
|
if (fmt) {
|
2008-11-07 05:12:42 +05:30
|
|
|
msg = xasprintf(fmt, param);
|
2020-12-17 17:04:25 +05:30
|
|
|
send_r_n(msg);
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
2008-11-07 05:12:42 +05:30
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: parse_url can modify url[] (despite const), but only if '@' is there
|
|
|
|
/*
|
2010-12-20 09:42:39 +05:30
|
|
|
static char* FAST_FUNC parse_url(char *url, char **user, char **pass)
|
2008-11-07 05:12:42 +05:30
|
|
|
{
|
|
|
|
// parse [user[:pass]@]host
|
|
|
|
// return host
|
|
|
|
char *s = strchr(url, '@');
|
|
|
|
*user = *pass = NULL;
|
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*user = url;
|
|
|
|
url = s;
|
|
|
|
s = strchr(*user, ':');
|
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*pass = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-07-31 21:00:08 +05:30
|
|
|
static void encode_n_base64(const char *fname, const char *text, size_t len)
|
2008-11-07 05:12:42 +05:30
|
|
|
{
|
|
|
|
enum {
|
2011-11-19 02:55:35 +05:30
|
|
|
SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */
|
2008-11-07 05:12:42 +05:30
|
|
|
DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
|
|
|
|
};
|
|
|
|
#define src_buf text
|
2010-09-16 21:40:04 +05:30
|
|
|
char src[SRC_BUF_SIZE];
|
2008-11-07 05:12:42 +05:30
|
|
|
FILE *fp = fp;
|
|
|
|
char dst_buf[DST_BUF_SIZE + 1];
|
|
|
|
|
|
|
|
if (fname) {
|
2018-07-31 21:00:08 +05:30
|
|
|
fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : stdin;
|
2010-09-16 21:40:04 +05:30
|
|
|
src_buf = src;
|
2018-07-31 21:00:08 +05:30
|
|
|
}
|
2008-11-07 05:12:42 +05:30
|
|
|
|
|
|
|
while (1) {
|
|
|
|
size_t size;
|
|
|
|
if (fname) {
|
|
|
|
size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
|
|
|
|
if ((ssize_t)size < 0)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg_and_die(bb_msg_read_error);
|
2008-11-07 05:12:42 +05:30
|
|
|
} else {
|
|
|
|
size = len;
|
|
|
|
if (len > SRC_BUF_SIZE)
|
|
|
|
size = SRC_BUF_SIZE;
|
|
|
|
}
|
|
|
|
if (!size)
|
|
|
|
break;
|
|
|
|
// encode the buffer we just read in
|
|
|
|
bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
|
|
|
|
if (fname) {
|
2018-07-31 21:00:08 +05:30
|
|
|
puts("");
|
2008-11-07 05:12:42 +05:30
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2018-07-31 21:00:08 +05:30
|
|
|
void FAST_FUNC printstr_base64(const char *text)
|
|
|
|
{
|
|
|
|
encode_n_base64(NULL, text, strlen(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC printbuf_base64(const char *text, unsigned len)
|
|
|
|
{
|
|
|
|
encode_n_base64(NULL, text, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC printfile_base64(const char *fname)
|
|
|
|
{
|
|
|
|
encode_n_base64(fname, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2008-11-07 05:12:42 +05:30
|
|
|
/*
|
|
|
|
* get username and password from a file descriptor
|
|
|
|
*/
|
|
|
|
void FAST_FUNC get_cred_or_die(int fd)
|
|
|
|
{
|
|
|
|
if (isatty(fd)) {
|
2018-04-07 19:20:30 +05:30
|
|
|
G.user = bb_ask_noecho(fd, /* timeout: */ 0, "User: ");
|
|
|
|
G.pass = bb_ask_noecho(fd, /* timeout: */ 0, "Password: ");
|
2008-11-07 05:12:42 +05:30
|
|
|
} else {
|
2011-05-09 00:51:10 +05:30
|
|
|
G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
|
|
|
|
G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|
2009-03-04 23:26:00 +05:30
|
|
|
if (!G.user || !*G.user || !G.pass)
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("no username or password");
|
2008-11-07 05:12:42 +05:30
|
|
|
}
|