blob: 22c30357b267325add97dd24946f899b6ca3b174 [file] [log] [blame]
Eric Andersenb183dfa2001-03-19 19:24:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenb183dfa2001-03-19 19:24:06 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenb183dfa2001-03-19 19:24:06 +00008 */
Denis Vlasenko1b97efd2007-08-17 19:18:06 +00009#include "libbb.h"
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +020010#if ENABLE_FEATURE_SYSLOG
11# include <syslog.h>
12#endif
Eric Andersenb183dfa2001-03-19 19:24:06 +000013
Denys Vlasenkofe336832014-05-02 07:18:55 +020014#if ENABLE_FEATURE_SYSLOG
Denys Vlasenko3dfe0ae2014-04-30 14:48:28 +020015smallint syslog_level = LOG_ERR;
Denys Vlasenkofe336832014-05-02 07:18:55 +020016#endif
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000017smallint logmode = LOGMODE_STDIO;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000018const char *msg_eol = "\n";
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000019
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000021{
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020022 char *msg, *msg1;
Denys Vlasenko46b49462016-04-03 16:55:03 +020023 char stack_msg[80];
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000024 int applet_len, strerr_len, msgeol_len, used;
25
26 if (!logmode)
27 return;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000028
Denis Vlasenko4ef7d3a2007-05-29 21:41:28 +000029 if (!s) /* nomsg[_and_die] uses NULL fmt */
30 s = ""; /* some libc don't like printf(NULL) */
31
Denys Vlasenko46b49462016-04-03 16:55:03 +020032 applet_len = strlen(applet_name) + 2; /* "applet: " */
33 strerr_len = strerr ? strlen(strerr) : 0;
34 msgeol_len = strlen(msg_eol);
35
36 /* This costs ~90 bytes of code, but avoids costly
37 * malloc()[in vasprintf]+realloc()+memmove()+free() in 99% of cases.
38 * ~40% speedup.
39 */
40 if ((int)sizeof(stack_msg) - applet_len > 0) {
41 va_list p2;
42
43 /* It is not portable to use va_list twice, need to va_copy it */
44 va_copy(p2, p);
45 used = vsnprintf(stack_msg + applet_len, (int)sizeof(stack_msg) - applet_len, s, p2);
46 va_end(p2);
47 msg = stack_msg;
48 used += applet_len;
49 if (used < (int)sizeof(stack_msg) - 3 - msgeol_len - strerr_len)
50 goto add_pfx_and_sfx;
51 }
52
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000053 used = vasprintf(&msg, s, p);
54 if (used < 0)
55 return;
56
57 /* This is ugly and costs +60 bytes compared to multiple
58 * fprintf's, but is guaranteed to do a single write.
59 * This is needed for e.g. httpd logging, when multiple
60 * children can produce log messages simultaneously. */
61
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020062 /* can't use xrealloc: it calls error_msg on failure,
63 * that may result in a recursion */
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000064 /* +3 is for ": " before strerr and for terminating NUL */
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020065 msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
66 if (!msg1) {
67 msg[used++] = '\n'; /* overwrites NUL */
68 applet_len = 0;
69 } else {
70 msg = msg1;
71 /* TODO: maybe use writev instead of memmoving? Need full_writev? */
72 memmove(msg + applet_len, msg, used);
73 used += applet_len;
Denys Vlasenko46b49462016-04-03 16:55:03 +020074 add_pfx_and_sfx:
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020075 strcpy(msg, applet_name);
76 msg[applet_len - 2] = ':';
77 msg[applet_len - 1] = ' ';
78 if (strerr) {
79 if (s[0]) { /* not perror_nomsg? */
80 msg[used++] = ':';
81 msg[used++] = ' ';
82 }
83 strcpy(&msg[used], strerr);
84 used += strerr_len;
Denis Vlasenko94e33652007-12-22 15:44:23 +000085 }
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020086 strcpy(&msg[used], msg_eol);
87 used += msgeol_len;
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000088 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000089
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000090 if (logmode & LOGMODE_STDIO) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +010091 fflush_all();
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020092 full_write(STDERR_FILENO, msg, used);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000093 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +020094#if ENABLE_FEATURE_SYSLOG
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000095 if (logmode & LOGMODE_SYSLOG) {
Denys Vlasenko3dfe0ae2014-04-30 14:48:28 +020096 syslog(syslog_level, "%s", msg + applet_len);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000097 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +020098#endif
Denys Vlasenko46b49462016-04-03 16:55:03 +020099 if (msg != stack_msg)
100 free(msg);
Eric Andersenb183dfa2001-03-19 19:24:06 +0000101}
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000102
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000103#ifdef VERSION_WITH_WRITEV
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000104/* Code size is approximately the same, but currently it's the only user
105 * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000106void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000107{
108 int strerr_len, msgeol_len;
109 struct iovec iov[3];
110
111#define used (iov[2].iov_len)
112#define msgv (iov[2].iov_base)
113#define msgc ((char*)(iov[2].iov_base))
114#define msgptr (&(iov[2].iov_base))
115
116 if (!logmode)
117 return;
118
119 if (!s) /* nomsg[_and_die] uses NULL fmt */
120 s = ""; /* some libc don't like printf(NULL) */
121
122 /* Prevent "derefing type-punned ptr will break aliasing rules" */
123 used = vasprintf((char**)(void*)msgptr, s, p);
124 if (used < 0)
125 return;
126
127 /* This is ugly and costs +60 bytes compared to multiple
128 * fprintf's, but is guaranteed to do a single write.
129 * This is needed for e.g. httpd logging, when multiple
130 * children can produce log messages simultaneously. */
131
132 strerr_len = strerr ? strlen(strerr) : 0;
133 msgeol_len = strlen(msg_eol);
134 /* +3 is for ": " before strerr and for terminating NUL */
135 msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
136 if (strerr) {
137 msgc[used++] = ':';
138 msgc[used++] = ' ';
139 strcpy(msgc + used, strerr);
140 used += strerr_len;
141 }
142 strcpy(msgc + used, msg_eol);
143 used += msgeol_len;
144
145 if (logmode & LOGMODE_STDIO) {
146 iov[0].iov_base = (char*)applet_name;
147 iov[0].iov_len = strlen(applet_name);
148 iov[1].iov_base = (char*)": ";
149 iov[1].iov_len = 2;
150 /*iov[2].iov_base = msgc;*/
151 /*iov[2].iov_len = used;*/
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100152 fflush_all();
153 writev(STDERR_FILENO, iov, 3);
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000154 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +0200155# if ENABLE_FEATURE_SYSLOG
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000156 if (logmode & LOGMODE_SYSLOG) {
157 syslog(LOG_ERR, "%s", msgc);
158 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +0200159# endif
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000160 free(msgc);
161}
162#endif
Denys Vlasenko7e1bb4b2010-07-04 17:16:44 +0200163
164
165void FAST_FUNC bb_error_msg_and_die(const char *s, ...)
166{
167 va_list p;
168
169 va_start(p, s);
170 bb_verror_msg(s, p, NULL);
171 va_end(p);
172 xfunc_die();
173}
174
175void FAST_FUNC bb_error_msg(const char *s, ...)
176{
177 va_list p;
178
179 va_start(p, s);
180 bb_verror_msg(s, p, NULL);
181 va_end(p);
182}