blob: e510031342af62d9b67a6ed2c15088fe765f7a6a [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
Bernhard Reutner-Fischerb5f50ea2006-09-12 13:27:55 +000010#include <syslog.h>
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000011#include "libbb.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
16void bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000017{
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000018 char *msg;
19 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);
39 /* +3 is for ": " before strerr and for terminating NUL */
40 msg = xrealloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
41 /* TODO: maybe use writev instead of memmoving? Need full_writev? */
42 memmove(msg + applet_len, msg, used);
43 used += applet_len;
44 strcpy(msg, applet_name);
45 msg[applet_len - 2] = ':';
46 msg[applet_len - 1] = ' ';
47 if (strerr) {
48 msg[used++] = ':';
49 msg[used++] = ' ';
50 strcpy(&msg[used], strerr);
51 used += strerr_len;
52 }
53 strcpy(&msg[used], msg_eol);
54
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000055 if (logmode & LOGMODE_STDIO) {
56 fflush(stdout);
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000057 full_write(2, msg, used + msgeol_len);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000058 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000059 if (logmode & LOGMODE_SYSLOG) {
60 syslog(LOG_ERR, "%s", msg + applet_len);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000061 }
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000062 free(msg);
Eric Andersenb183dfa2001-03-19 19:24:06 +000063}
Denis Vlasenko1b97efd2007-08-17 19:18:06 +000064
65
66#ifdef VERSION_WITH_WRITEV
67
68/* Code size is approximately the same, but currently it's the only user
69 * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
70
71void bb_verror_msg(const char *s, va_list p, const char* strerr)
72{
73 int strerr_len, msgeol_len;
74 struct iovec iov[3];
75
76#define used (iov[2].iov_len)
77#define msgv (iov[2].iov_base)
78#define msgc ((char*)(iov[2].iov_base))
79#define msgptr (&(iov[2].iov_base))
80
81 if (!logmode)
82 return;
83
84 if (!s) /* nomsg[_and_die] uses NULL fmt */
85 s = ""; /* some libc don't like printf(NULL) */
86
87 /* Prevent "derefing type-punned ptr will break aliasing rules" */
88 used = vasprintf((char**)(void*)msgptr, s, p);
89 if (used < 0)
90 return;
91
92 /* This is ugly and costs +60 bytes compared to multiple
93 * fprintf's, but is guaranteed to do a single write.
94 * This is needed for e.g. httpd logging, when multiple
95 * children can produce log messages simultaneously. */
96
97 strerr_len = strerr ? strlen(strerr) : 0;
98 msgeol_len = strlen(msg_eol);
99 /* +3 is for ": " before strerr and for terminating NUL */
100 msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
101 if (strerr) {
102 msgc[used++] = ':';
103 msgc[used++] = ' ';
104 strcpy(msgc + used, strerr);
105 used += strerr_len;
106 }
107 strcpy(msgc + used, msg_eol);
108 used += msgeol_len;
109
110 if (logmode & LOGMODE_STDIO) {
111 iov[0].iov_base = (char*)applet_name;
112 iov[0].iov_len = strlen(applet_name);
113 iov[1].iov_base = (char*)": ";
114 iov[1].iov_len = 2;
115 /*iov[2].iov_base = msgc;*/
116 /*iov[2].iov_len = used;*/
117 fflush(stdout);
118 writev(2, iov, 3);
119 }
120 if (logmode & LOGMODE_SYSLOG) {
121 syslog(LOG_ERR, "%s", msgc);
122 }
123 free(msgc);
124}
125#endif