blob: 70f792f39413a91fc85fb1521f4e0c722f4fb0d8 [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 */
9
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000010#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000011#include <syslog.h>
Eric Andersenb183dfa2001-03-19 19:24:06 +000012
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000013smallint logmode = LOGMODE_STDIO;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000014const char *msg_eol = "\n";
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000015
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000016void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000017{
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020018 char *msg, *msg1;
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000019 int applet_len, strerr_len, msgeol_len, used;
20
21 if (!logmode)
22 return;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000023
Denis Vlasenko4ef7d3a2007-05-29 21:41:28 +000024 if (!s) /* nomsg[_and_die] uses NULL fmt */
25 s = ""; /* some libc don't like printf(NULL) */
26
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000027 used = vasprintf(&msg, s, p);
28 if (used < 0)
29 return;
30
31 /* This is ugly and costs +60 bytes compared to multiple
32 * fprintf's, but is guaranteed to do a single write.
33 * This is needed for e.g. httpd logging, when multiple
34 * children can produce log messages simultaneously. */
35
36 applet_len = strlen(applet_name) + 2; /* "applet: " */
37 strerr_len = strerr ? strlen(strerr) : 0;
38 msgeol_len = strlen(msg_eol);
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020039 /* can't use xrealloc: it calls error_msg on failure,
40 * that may result in a recursion */
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000041 /* +3 is for ": " before strerr and for terminating NUL */
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020042 msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
43 if (!msg1) {
44 msg[used++] = '\n'; /* overwrites NUL */
45 applet_len = 0;
46 } else {
47 msg = msg1;
48 /* TODO: maybe use writev instead of memmoving? Need full_writev? */
49 memmove(msg + applet_len, msg, used);
50 used += applet_len;
51 strcpy(msg, applet_name);
52 msg[applet_len - 2] = ':';
53 msg[applet_len - 1] = ' ';
54 if (strerr) {
55 if (s[0]) { /* not perror_nomsg? */
56 msg[used++] = ':';
57 msg[used++] = ' ';
58 }
59 strcpy(&msg[used], strerr);
60 used += strerr_len;
Denis Vlasenko94e33652007-12-22 15:44:23 +000061 }
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020062 strcpy(&msg[used], msg_eol);
63 used += msgeol_len;
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000064 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000065
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000066 if (logmode & LOGMODE_STDIO) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +010067 fflush_all();
Denys Vlasenkoe2944af2009-08-01 06:53:03 +020068 full_write(STDERR_FILENO, msg, used);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000069 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000070 if (logmode & LOGMODE_SYSLOG) {
71 syslog(LOG_ERR, "%s", msg + applet_len);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000072 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000073 free(msg);
Eric Andersenb183dfa2001-03-19 19:24:06 +000074}
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000075
76
77#ifdef VERSION_WITH_WRITEV
78
79/* Code size is approximately the same, but currently it's the only user
80 * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
81
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000082void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000083{
84 int strerr_len, msgeol_len;
85 struct iovec iov[3];
86
87#define used (iov[2].iov_len)
88#define msgv (iov[2].iov_base)
89#define msgc ((char*)(iov[2].iov_base))
90#define msgptr (&(iov[2].iov_base))
91
92 if (!logmode)
93 return;
94
95 if (!s) /* nomsg[_and_die] uses NULL fmt */
96 s = ""; /* some libc don't like printf(NULL) */
97
98 /* Prevent "derefing type-punned ptr will break aliasing rules" */
99 used = vasprintf((char**)(void*)msgptr, s, p);
100 if (used < 0)
101 return;
102
103 /* This is ugly and costs +60 bytes compared to multiple
104 * fprintf's, but is guaranteed to do a single write.
105 * This is needed for e.g. httpd logging, when multiple
106 * children can produce log messages simultaneously. */
107
108 strerr_len = strerr ? strlen(strerr) : 0;
109 msgeol_len = strlen(msg_eol);
110 /* +3 is for ": " before strerr and for terminating NUL */
111 msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
112 if (strerr) {
113 msgc[used++] = ':';
114 msgc[used++] = ' ';
115 strcpy(msgc + used, strerr);
116 used += strerr_len;
117 }
118 strcpy(msgc + used, msg_eol);
119 used += msgeol_len;
120
121 if (logmode & LOGMODE_STDIO) {
122 iov[0].iov_base = (char*)applet_name;
123 iov[0].iov_len = strlen(applet_name);
124 iov[1].iov_base = (char*)": ";
125 iov[1].iov_len = 2;
126 /*iov[2].iov_base = msgc;*/
127 /*iov[2].iov_len = used;*/
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100128 fflush_all();
129 writev(STDERR_FILENO, iov, 3);
Denis Vlasenko1b97efd2007-08-17 19:18:06 +0000130 }
131 if (logmode & LOGMODE_SYSLOG) {
132 syslog(LOG_ERR, "%s", msgc);
133 }
134 free(msgc);
135}
136#endif