2001-03-20 00:54:06 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-20 00:54:06 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-20 00:54:06 +05:30
|
|
|
*/
|
2007-08-18 00:48:06 +05:30
|
|
|
#include "libbb.h"
|
2010-04-09 18:04:28 +05:30
|
|
|
#if ENABLE_FEATURE_SYSLOG
|
|
|
|
# include <syslog.h>
|
|
|
|
#endif
|
2001-03-20 00:54:06 +05:30
|
|
|
|
2014-05-02 10:48:55 +05:30
|
|
|
#if ENABLE_FEATURE_SYSLOG
|
2019-04-12 22:31:51 +05:30
|
|
|
static smallint syslog_level = LOG_ERR;
|
2014-05-02 10:48:55 +05:30
|
|
|
#endif
|
2007-04-07 06:14:31 +05:30
|
|
|
smallint logmode = LOGMODE_STDIO;
|
2006-09-07 21:50:03 +05:30
|
|
|
const char *msg_eol = "\n";
|
2006-09-07 00:06:50 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
2001-03-20 00:54:06 +05:30
|
|
|
{
|
2009-08-01 10:23:03 +05:30
|
|
|
char *msg, *msg1;
|
2016-04-03 20:25:03 +05:30
|
|
|
char stack_msg[80];
|
2007-08-18 00:48:06 +05:30
|
|
|
int applet_len, strerr_len, msgeol_len, used;
|
|
|
|
|
|
|
|
if (!logmode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!s) /* nomsg[_and_die] uses NULL fmt */
|
|
|
|
s = ""; /* some libc don't like printf(NULL) */
|
|
|
|
|
2016-04-03 20:25:03 +05:30
|
|
|
applet_len = strlen(applet_name) + 2; /* "applet: " */
|
|
|
|
strerr_len = strerr ? strlen(strerr) : 0;
|
|
|
|
msgeol_len = strlen(msg_eol);
|
|
|
|
|
|
|
|
/* This costs ~90 bytes of code, but avoids costly
|
|
|
|
* malloc()[in vasprintf]+realloc()+memmove()+free() in 99% of cases.
|
|
|
|
* ~40% speedup.
|
|
|
|
*/
|
|
|
|
if ((int)sizeof(stack_msg) - applet_len > 0) {
|
|
|
|
va_list p2;
|
|
|
|
|
|
|
|
/* It is not portable to use va_list twice, need to va_copy it */
|
|
|
|
va_copy(p2, p);
|
|
|
|
used = vsnprintf(stack_msg + applet_len, (int)sizeof(stack_msg) - applet_len, s, p2);
|
|
|
|
va_end(p2);
|
|
|
|
msg = stack_msg;
|
|
|
|
used += applet_len;
|
|
|
|
if (used < (int)sizeof(stack_msg) - 3 - msgeol_len - strerr_len)
|
|
|
|
goto add_pfx_and_sfx;
|
|
|
|
}
|
|
|
|
|
2007-08-18 00:48:06 +05:30
|
|
|
used = vasprintf(&msg, s, p);
|
|
|
|
if (used < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* This is ugly and costs +60 bytes compared to multiple
|
|
|
|
* fprintf's, but is guaranteed to do a single write.
|
|
|
|
* This is needed for e.g. httpd logging, when multiple
|
|
|
|
* children can produce log messages simultaneously. */
|
|
|
|
|
2009-08-01 10:23:03 +05:30
|
|
|
/* can't use xrealloc: it calls error_msg on failure,
|
|
|
|
* that may result in a recursion */
|
2007-08-18 00:48:06 +05:30
|
|
|
/* +3 is for ": " before strerr and for terminating NUL */
|
2009-08-01 10:23:03 +05:30
|
|
|
msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
|
|
|
|
if (!msg1) {
|
|
|
|
msg[used++] = '\n'; /* overwrites NUL */
|
|
|
|
applet_len = 0;
|
|
|
|
} else {
|
|
|
|
msg = msg1;
|
|
|
|
/* TODO: maybe use writev instead of memmoving? Need full_writev? */
|
|
|
|
memmove(msg + applet_len, msg, used);
|
|
|
|
used += applet_len;
|
2016-04-03 20:25:03 +05:30
|
|
|
add_pfx_and_sfx:
|
2009-08-01 10:23:03 +05:30
|
|
|
strcpy(msg, applet_name);
|
|
|
|
msg[applet_len - 2] = ':';
|
|
|
|
msg[applet_len - 1] = ' ';
|
|
|
|
if (strerr) {
|
|
|
|
if (s[0]) { /* not perror_nomsg? */
|
|
|
|
msg[used++] = ':';
|
|
|
|
msg[used++] = ' ';
|
|
|
|
}
|
|
|
|
strcpy(&msg[used], strerr);
|
|
|
|
used += strerr_len;
|
2007-12-22 21:14:23 +05:30
|
|
|
}
|
2009-08-01 10:23:03 +05:30
|
|
|
strcpy(&msg[used], msg_eol);
|
|
|
|
used += msgeol_len;
|
2007-08-18 00:48:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (logmode & LOGMODE_STDIO) {
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
2009-08-01 10:23:03 +05:30
|
|
|
full_write(STDERR_FILENO, msg, used);
|
2007-08-18 00:48:06 +05:30
|
|
|
}
|
2010-04-09 18:04:28 +05:30
|
|
|
#if ENABLE_FEATURE_SYSLOG
|
2007-08-18 00:48:06 +05:30
|
|
|
if (logmode & LOGMODE_SYSLOG) {
|
2014-04-30 18:18:28 +05:30
|
|
|
syslog(syslog_level, "%s", msg + applet_len);
|
2007-08-18 00:48:06 +05:30
|
|
|
}
|
2010-04-09 18:04:28 +05:30
|
|
|
#endif
|
2016-04-03 20:25:03 +05:30
|
|
|
if (msg != stack_msg)
|
|
|
|
free(msg);
|
2007-08-18 00:48:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef VERSION_WITH_WRITEV
|
|
|
|
/* Code size is approximately the same, but currently it's the only user
|
|
|
|
* of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
2007-08-18 00:48:06 +05:30
|
|
|
{
|
|
|
|
int strerr_len, msgeol_len;
|
|
|
|
struct iovec iov[3];
|
|
|
|
|
|
|
|
#define used (iov[2].iov_len)
|
|
|
|
#define msgv (iov[2].iov_base)
|
|
|
|
#define msgc ((char*)(iov[2].iov_base))
|
|
|
|
#define msgptr (&(iov[2].iov_base))
|
|
|
|
|
|
|
|
if (!logmode)
|
|
|
|
return;
|
2006-09-07 00:06:50 +05:30
|
|
|
|
2007-05-30 03:11:28 +05:30
|
|
|
if (!s) /* nomsg[_and_die] uses NULL fmt */
|
|
|
|
s = ""; /* some libc don't like printf(NULL) */
|
|
|
|
|
2007-08-18 00:48:06 +05:30
|
|
|
/* Prevent "derefing type-punned ptr will break aliasing rules" */
|
|
|
|
used = vasprintf((char**)(void*)msgptr, s, p);
|
|
|
|
if (used < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* This is ugly and costs +60 bytes compared to multiple
|
|
|
|
* fprintf's, but is guaranteed to do a single write.
|
|
|
|
* This is needed for e.g. httpd logging, when multiple
|
|
|
|
* children can produce log messages simultaneously. */
|
|
|
|
|
|
|
|
strerr_len = strerr ? strlen(strerr) : 0;
|
|
|
|
msgeol_len = strlen(msg_eol);
|
|
|
|
/* +3 is for ": " before strerr and for terminating NUL */
|
|
|
|
msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
|
|
|
|
if (strerr) {
|
|
|
|
msgc[used++] = ':';
|
|
|
|
msgc[used++] = ' ';
|
|
|
|
strcpy(msgc + used, strerr);
|
|
|
|
used += strerr_len;
|
|
|
|
}
|
|
|
|
strcpy(msgc + used, msg_eol);
|
|
|
|
used += msgeol_len;
|
|
|
|
|
2006-09-07 00:06:50 +05:30
|
|
|
if (logmode & LOGMODE_STDIO) {
|
2007-08-18 00:48:06 +05:30
|
|
|
iov[0].iov_base = (char*)applet_name;
|
|
|
|
iov[0].iov_len = strlen(applet_name);
|
|
|
|
iov[1].iov_base = (char*)": ";
|
|
|
|
iov[1].iov_len = 2;
|
|
|
|
/*iov[2].iov_base = msgc;*/
|
|
|
|
/*iov[2].iov_len = used;*/
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
|
|
|
writev(STDERR_FILENO, iov, 3);
|
2006-09-07 00:06:50 +05:30
|
|
|
}
|
2010-04-09 18:04:28 +05:30
|
|
|
# if ENABLE_FEATURE_SYSLOG
|
2007-08-18 00:48:06 +05:30
|
|
|
if (logmode & LOGMODE_SYSLOG) {
|
2019-04-12 22:31:51 +05:30
|
|
|
syslog(syslog_level, "%s", msgc);
|
2006-09-07 00:06:50 +05:30
|
|
|
}
|
2010-04-09 18:04:28 +05:30
|
|
|
# endif
|
2007-08-18 00:48:06 +05:30
|
|
|
free(msgc);
|
2001-03-20 00:54:06 +05:30
|
|
|
}
|
2007-08-18 00:48:06 +05:30
|
|
|
#endif
|
2010-07-04 20:46:44 +05:30
|
|
|
|
|
|
|
|
|
|
|
void FAST_FUNC bb_error_msg_and_die(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list p;
|
|
|
|
|
|
|
|
va_start(p, s);
|
|
|
|
bb_verror_msg(s, p, NULL);
|
|
|
|
va_end(p);
|
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC bb_error_msg(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list p;
|
|
|
|
|
|
|
|
va_start(p, s);
|
|
|
|
bb_verror_msg(s, p, NULL);
|
|
|
|
va_end(p);
|
|
|
|
}
|
2019-04-12 22:31:51 +05:30
|
|
|
|
|
|
|
#if ENABLE_FEATURE_SYSLOG_INFO
|
|
|
|
void FAST_FUNC bb_vinfo_msg(const char *s, va_list p)
|
|
|
|
{
|
|
|
|
syslog_level = LOG_INFO;
|
|
|
|
bb_verror_msg(s, p, NULL);
|
|
|
|
syslog_level = LOG_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC bb_info_msg(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list p;
|
|
|
|
|
|
|
|
va_start(p, s);
|
|
|
|
bb_vinfo_msg(s, p);
|
|
|
|
va_end(p);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
void FAST_FUNC bb_simple_info_msg(const char *s)
|
|
|
|
{
|
|
|
|
bb_info_msg("%s", s);
|
|
|
|
}
|
2019-04-12 22:31:51 +05:30
|
|
|
#endif
|
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
|
|
|
|
|
|
|
void FAST_FUNC bb_simple_error_msg(const char *s)
|
|
|
|
{
|
|
|
|
bb_error_msg("%s", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC bb_simple_error_msg_and_die(const char *s)
|
|
|
|
{
|
|
|
|
bb_error_msg_and_die("%s", s);
|
|
|
|
}
|