2001-03-19 19:24:06 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-19 19:24:06 +00:00
|
|
|
*
|
2006-05-19 19:29:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-19 19:24:06 +00:00
|
|
|
*/
|
2007-08-17 19:18:06 +00:00
|
|
|
#include "libbb.h"
|
2010-04-09 12:34:28 +00:00
|
|
|
#if ENABLE_FEATURE_SYSLOG
|
|
|
|
# include <syslog.h>
|
|
|
|
#endif
|
2001-03-19 19:24:06 +00:00
|
|
|
|
2007-04-07 00:44:31 +00:00
|
|
|
smallint logmode = LOGMODE_STDIO;
|
2006-09-07 16:20:03 +00:00
|
|
|
const char *msg_eol = "\n";
|
2006-09-06 18:36:50 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
2001-03-19 19:24:06 +00:00
|
|
|
{
|
2009-08-01 04:53:03 +00:00
|
|
|
char *msg, *msg1;
|
2007-08-17 19:18:06 +00:00
|
|
|
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) */
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
applet_len = strlen(applet_name) + 2; /* "applet: " */
|
|
|
|
strerr_len = strerr ? strlen(strerr) : 0;
|
|
|
|
msgeol_len = strlen(msg_eol);
|
2009-08-01 04:53:03 +00:00
|
|
|
/* can't use xrealloc: it calls error_msg on failure,
|
|
|
|
* that may result in a recursion */
|
2007-08-17 19:18:06 +00:00
|
|
|
/* +3 is for ": " before strerr and for terminating NUL */
|
2009-08-01 04:53:03 +00:00
|
|
|
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;
|
|
|
|
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 15:44:23 +00:00
|
|
|
}
|
2009-08-01 04:53:03 +00:00
|
|
|
strcpy(&msg[used], msg_eol);
|
|
|
|
used += msgeol_len;
|
2007-08-17 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (logmode & LOGMODE_STDIO) {
|
2009-11-02 13:19:51 +00:00
|
|
|
fflush_all();
|
2009-08-01 04:53:03 +00:00
|
|
|
full_write(STDERR_FILENO, msg, used);
|
2007-08-17 19:18:06 +00:00
|
|
|
}
|
2010-04-09 12:34:28 +00:00
|
|
|
#if ENABLE_FEATURE_SYSLOG
|
2007-08-17 19:18:06 +00:00
|
|
|
if (logmode & LOGMODE_SYSLOG) {
|
|
|
|
syslog(LOG_ERR, "%s", msg + applet_len);
|
|
|
|
}
|
2010-04-09 12:34:28 +00:00
|
|
|
#endif
|
2007-08-17 19:18:06 +00:00
|
|
|
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. */
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
|
2007-08-17 19:18:06 +00:00
|
|
|
{
|
|
|
|
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-06 18:36:50 +00:00
|
|
|
|
2007-05-29 21:41:28 +00:00
|
|
|
if (!s) /* nomsg[_and_die] uses NULL fmt */
|
|
|
|
s = ""; /* some libc don't like printf(NULL) */
|
|
|
|
|
2007-08-17 19:18:06 +00:00
|
|
|
/* 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-06 18:36:50 +00:00
|
|
|
if (logmode & LOGMODE_STDIO) {
|
2007-08-17 19:18:06 +00:00
|
|
|
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 13:19:51 +00:00
|
|
|
fflush_all();
|
|
|
|
writev(STDERR_FILENO, iov, 3);
|
2006-09-06 18:36:50 +00:00
|
|
|
}
|
2010-04-09 12:34:28 +00:00
|
|
|
# if ENABLE_FEATURE_SYSLOG
|
2007-08-17 19:18:06 +00:00
|
|
|
if (logmode & LOGMODE_SYSLOG) {
|
|
|
|
syslog(LOG_ERR, "%s", msgc);
|
2006-09-06 18:36:50 +00:00
|
|
|
}
|
2010-04-09 12:34:28 +00:00
|
|
|
# endif
|
2007-08-17 19:18:06 +00:00
|
|
|
free(msgc);
|
2001-03-19 19:24:06 +00:00
|
|
|
}
|
2007-08-17 19:18:06 +00:00
|
|
|
#endif
|