blob: 613432906b7845344ac9d0fa41a204169de9d788 [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000014smallint logmode = LOGMODE_STDIO;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000015const char *msg_eol = "\n";
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000016
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000017void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000018{
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020019 char *msg, *msg1;
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000020 int applet_len, strerr_len, msgeol_len, used;
21
22 if (!logmode)
23 return;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000024
Denis Vlasenko4ef7d3a2007-05-29 21:41:28 +000025 if (!s) /* nomsg[_and_die] uses NULL fmt */
26 s = ""; /* some libc don't like printf(NULL) */
27
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000028 used = vasprintf(&msg, s, p);
29 if (used < 0)
30 return;
31
32 /* This is ugly and costs +60 bytes compared to multiple
33 * fprintf's, but is guaranteed to do a single write.
34 * This is needed for e.g. httpd logging, when multiple
35 * children can produce log messages simultaneously. */
36
37 applet_len = strlen(applet_name) + 2; /* "applet: " */
38 strerr_len = strerr ? strlen(strerr) : 0;
39 msgeol_len = strlen(msg_eol);
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020040 /* can't use xrealloc: it calls error_msg on failure,
41 * that may result in a recursion */
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000042 /* +3 is for ": " before strerr and for terminating NUL */
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020043 msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
44 if (!msg1) {
45 msg[used++] = '\n'; /* overwrites NUL */
46 applet_len = 0;
47 } else {
48 msg = msg1;
49 /* TODO: maybe use writev instead of memmoving? Need full_writev? */
50 memmove(msg + applet_len, msg, used);
51 used += applet_len;
52 strcpy(msg, applet_name);
53 msg[applet_len - 2] = ':';
54 msg[applet_len - 1] = ' ';
55 if (strerr) {
56 if (s[0]) { /* not perror_nomsg? */
57 msg[used++] = ':';
58 msg[used++] = ' ';
59 }
60 strcpy(&msg[used], strerr);
61 used += strerr_len;
Denis Vlasenko94e33652007-12-22 15:44:23 +000062 }
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020063 strcpy(&msg[used], msg_eol);
64 used += msgeol_len;
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000065 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000066
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000067 if (logmode & LOGMODE_STDIO) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +010068 fflush_all();
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020069 full_write(STDERR_FILENO, msg, used);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000070 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +020071#if ENABLE_FEATURE_SYSLOG
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000072 if (logmode & LOGMODE_SYSLOG) {
73 syslog(LOG_ERR, "%s", msg + applet_len);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000074 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +020075#endif
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000076 free(msg);
Eric Andersenb183dfa2001-03-19 19:24:06 +000077}
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000078
79
80#ifdef VERSION_WITH_WRITEV
81
82/* Code size is approximately the same, but currently it's the only user
83 * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
84
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000085void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000086{
87 int strerr_len, msgeol_len;
88 struct iovec iov[3];
89
90#define used (iov[2].iov_len)
91#define msgv (iov[2].iov_base)
92#define msgc ((char*)(iov[2].iov_base))
93#define msgptr (&(iov[2].iov_base))
94
95 if (!logmode)
96 return;
97
98 if (!s) /* nomsg[_and_die] uses NULL fmt */
99 s = ""; /* some libc don't like printf(NULL) */
100
101 /* Prevent "derefing type-punned ptr will break aliasing rules" */
102 used = vasprintf((char**)(void*)msgptr, s, p);
103 if (used < 0)
104 return;
105
106 /* This is ugly and costs +60 bytes compared to multiple
107 * fprintf's, but is guaranteed to do a single write.
108 * This is needed for e.g. httpd logging, when multiple
109 * children can produce log messages simultaneously. */
110
111 strerr_len = strerr ? strlen(strerr) : 0;
112 msgeol_len = strlen(msg_eol);
113 /* +3 is for ": " before strerr and for terminating NUL */
114 msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
115 if (strerr) {
116 msgc[used++] = ':';
117 msgc[used++] = ' ';
118 strcpy(msgc + used, strerr);
119 used += strerr_len;
120 }
121 strcpy(msgc + used, msg_eol);
122 used += msgeol_len;
123
124 if (logmode & LOGMODE_STDIO) {
125 iov[0].iov_base = (char*)applet_name;
126 iov[0].iov_len = strlen(applet_name);
127 iov[1].iov_base = (char*)": ";
128 iov[1].iov_len = 2;
129 /*iov[2].iov_base = msgc;*/
130 /*iov[2].iov_len = used;*/
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100131 fflush_all();
132 writev(STDERR_FILENO, iov, 3);
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000133 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +0200134# if ENABLE_FEATURE_SYSLOG
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000135 if (logmode & LOGMODE_SYSLOG) {
136 syslog(LOG_ERR, "%s", msgc);
137 }
Nguyễn Thái Ngọc Duy125d0152010-04-09 14:34:28 +0200138# endif
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000139 free(msgc);
140}
141#endif