Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 10 | #if ENABLE_FEATURE_SYSLOG |
| 11 | # include <syslog.h> |
| 12 | #endif |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 14 | smallint logmode = LOGMODE_STDIO; |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 15 | const char *msg_eol = "\n"; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 17 | void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 18 | { |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 19 | char *msg, *msg1; |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 20 | int applet_len, strerr_len, msgeol_len, used; |
| 21 | |
| 22 | if (!logmode) |
| 23 | return; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 4ef7d3a | 2007-05-29 21:41:28 +0000 | [diff] [blame] | 25 | if (!s) /* nomsg[_and_die] uses NULL fmt */ |
| 26 | s = ""; /* some libc don't like printf(NULL) */ |
| 27 | |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 28 | 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 Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 40 | /* can't use xrealloc: it calls error_msg on failure, |
| 41 | * that may result in a recursion */ |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 42 | /* +3 is for ": " before strerr and for terminating NUL */ |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 43 | 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 Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 62 | } |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 63 | strcpy(&msg[used], msg_eol); |
| 64 | used += msgeol_len; |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 65 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 67 | if (logmode & LOGMODE_STDIO) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 68 | fflush_all(); |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 69 | full_write(STDERR_FILENO, msg, used); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 70 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 71 | #if ENABLE_FEATURE_SYSLOG |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 72 | if (logmode & LOGMODE_SYSLOG) { |
| 73 | syslog(LOG_ERR, "%s", msg + applet_len); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 74 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 75 | #endif |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 76 | free(msg); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 77 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 79 | #ifdef VERSION_WITH_WRITEV |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 80 | /* Code size is approximately the same, but currently it's the only user |
| 81 | * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 82 | void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 83 | { |
| 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 Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 128 | fflush_all(); |
| 129 | writev(STDERR_FILENO, iov, 3); |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 130 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 131 | # if ENABLE_FEATURE_SYSLOG |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 132 | if (logmode & LOGMODE_SYSLOG) { |
| 133 | syslog(LOG_ERR, "%s", msgc); |
| 134 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 135 | # endif |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 136 | free(msgc); |
| 137 | } |
| 138 | #endif |
Denys Vlasenko | 7e1bb4b | 2010-07-04 17:16:44 +0200 | [diff] [blame] | 139 | |
| 140 | |
| 141 | void FAST_FUNC bb_error_msg_and_die(const char *s, ...) |
| 142 | { |
| 143 | va_list p; |
| 144 | |
| 145 | va_start(p, s); |
| 146 | bb_verror_msg(s, p, NULL); |
| 147 | va_end(p); |
| 148 | xfunc_die(); |
| 149 | } |
| 150 | |
| 151 | void FAST_FUNC bb_error_msg(const char *s, ...) |
| 152 | { |
| 153 | va_list p; |
| 154 | |
| 155 | va_start(p, s); |
| 156 | bb_verror_msg(s, p, NULL); |
| 157 | va_end(p); |
| 158 | } |