busybox/libbb/verror_msg.c
James Byrne 253c4e787a Optionally re-introduce bb_info_msg()
Between Busybox 1.24.2 and 1.25.0 the bb_info_msg() function was
eliminated and calls to it changed to be bb_error_msg(). The downside of
this is that daemons now log all messages to syslog at the LOG_ERR level
which makes it hard to filter errors from informational messages.

This change optionally re-introduces bb_info_msg(), controlled by a new
option FEATURE_SYSLOG_INFO, restores all the calls to bb_info_msg() that
were removed (only in applets that set logmode to LOGMODE_SYSLOG or
LOGMODE_BOTH), and also changes informational messages in ifplugd and
ntpd.

The code size change of this is as follows (using 'defconfig' on x86_64
with gcc 7.3.0-27ubuntu1~18.04)

function                                             old     new   delta
bb_info_msg                                            -     182    +182
bb_vinfo_msg                                           -      27     +27
static.log7                                          194     198      +4
log8                                                 190     191      +1
log5                                                 190     191      +1
crondlog                                              45       -     -45
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 3/0 up/down: 215/-45)           Total: 170 bytes

If you don't care about everything being logged at LOG_ERR level
then when FEATURE_SYSLOG_INFO is disabled Busybox actually gets smaller:

function                                             old     new   delta
static.log7                                          194     200      +6
log8                                                 190     193      +3
log5                                                 190     193      +3
syslog_level                                           1       -      -1
bb_verror_msg                                        583     581      -2
crondlog                                              45       -     -45
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 3/1 up/down: 12/-48)            Total: -36 bytes

Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-04-30 10:51:27 +02:00

201 lines
4.8 KiB
C

/* vi: set sw=4 ts=4: */
/*
* Utility routines.
*
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#include "libbb.h"
#if ENABLE_FEATURE_SYSLOG
# include <syslog.h>
#endif
#if ENABLE_FEATURE_SYSLOG
static smallint syslog_level = LOG_ERR;
#endif
smallint logmode = LOGMODE_STDIO;
const char *msg_eol = "\n";
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
{
char *msg, *msg1;
char stack_msg[80];
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) */
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;
}
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. */
/* can't use xrealloc: it calls error_msg on failure,
* that may result in a recursion */
/* +3 is for ": " before strerr and for terminating NUL */
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;
add_pfx_and_sfx:
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;
}
strcpy(&msg[used], msg_eol);
used += msgeol_len;
}
if (logmode & LOGMODE_STDIO) {
fflush_all();
full_write(STDERR_FILENO, msg, used);
}
#if ENABLE_FEATURE_SYSLOG
if (logmode & LOGMODE_SYSLOG) {
syslog(syslog_level, "%s", msg + applet_len);
}
#endif
if (msg != stack_msg)
free(msg);
}
#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. */
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
{
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;
if (!s) /* nomsg[_and_die] uses NULL fmt */
s = ""; /* some libc don't like printf(NULL) */
/* 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;
if (logmode & LOGMODE_STDIO) {
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;*/
fflush_all();
writev(STDERR_FILENO, iov, 3);
}
# if ENABLE_FEATURE_SYSLOG
if (logmode & LOGMODE_SYSLOG) {
syslog(syslog_level, "%s", msgc);
}
# endif
free(msgc);
}
#endif
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);
}
#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);
}
#endif