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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Denys Vlasenko | fe33683 | 2014-05-02 07:18:55 +0200 | [diff] [blame] | 14 | #if ENABLE_FEATURE_SYSLOG |
Denys Vlasenko | 3dfe0ae | 2014-04-30 14:48:28 +0200 | [diff] [blame] | 15 | smallint syslog_level = LOG_ERR; |
Denys Vlasenko | fe33683 | 2014-05-02 07:18:55 +0200 | [diff] [blame] | 16 | #endif |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 17 | smallint logmode = LOGMODE_STDIO; |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 18 | const char *msg_eol = "\n"; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 20 | 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] | 21 | { |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 22 | char *msg, *msg1; |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 23 | int applet_len, strerr_len, msgeol_len, used; |
| 24 | |
| 25 | if (!logmode) |
| 26 | return; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 4ef7d3a | 2007-05-29 21:41:28 +0000 | [diff] [blame] | 28 | if (!s) /* nomsg[_and_die] uses NULL fmt */ |
| 29 | s = ""; /* some libc don't like printf(NULL) */ |
| 30 | |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 31 | used = vasprintf(&msg, s, p); |
| 32 | if (used < 0) |
| 33 | return; |
| 34 | |
| 35 | /* This is ugly and costs +60 bytes compared to multiple |
| 36 | * fprintf's, but is guaranteed to do a single write. |
| 37 | * This is needed for e.g. httpd logging, when multiple |
| 38 | * children can produce log messages simultaneously. */ |
| 39 | |
| 40 | applet_len = strlen(applet_name) + 2; /* "applet: " */ |
| 41 | strerr_len = strerr ? strlen(strerr) : 0; |
| 42 | msgeol_len = strlen(msg_eol); |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 43 | /* can't use xrealloc: it calls error_msg on failure, |
| 44 | * that may result in a recursion */ |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 45 | /* +3 is for ": " before strerr and for terminating NUL */ |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 46 | msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3); |
| 47 | if (!msg1) { |
| 48 | msg[used++] = '\n'; /* overwrites NUL */ |
| 49 | applet_len = 0; |
| 50 | } else { |
| 51 | msg = msg1; |
| 52 | /* TODO: maybe use writev instead of memmoving? Need full_writev? */ |
| 53 | memmove(msg + applet_len, msg, used); |
| 54 | used += applet_len; |
| 55 | strcpy(msg, applet_name); |
| 56 | msg[applet_len - 2] = ':'; |
| 57 | msg[applet_len - 1] = ' '; |
| 58 | if (strerr) { |
| 59 | if (s[0]) { /* not perror_nomsg? */ |
| 60 | msg[used++] = ':'; |
| 61 | msg[used++] = ' '; |
| 62 | } |
| 63 | strcpy(&msg[used], strerr); |
| 64 | used += strerr_len; |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 65 | } |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 66 | strcpy(&msg[used], msg_eol); |
| 67 | used += msgeol_len; |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 68 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 70 | if (logmode & LOGMODE_STDIO) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 71 | fflush_all(); |
Denys Vlasenko | e2944af | 2009-08-01 06:53:03 +0200 | [diff] [blame] | 72 | full_write(STDERR_FILENO, msg, used); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 73 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 74 | #if ENABLE_FEATURE_SYSLOG |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 75 | if (logmode & LOGMODE_SYSLOG) { |
Denys Vlasenko | 3dfe0ae | 2014-04-30 14:48:28 +0200 | [diff] [blame] | 76 | syslog(syslog_level, "%s", msg + applet_len); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 77 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 78 | #endif |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 79 | free(msg); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 80 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 82 | #ifdef VERSION_WITH_WRITEV |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 83 | /* Code size is approximately the same, but currently it's the only user |
| 84 | * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 85 | 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] | 86 | { |
| 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 Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 131 | fflush_all(); |
| 132 | writev(STDERR_FILENO, iov, 3); |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 133 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 134 | # if ENABLE_FEATURE_SYSLOG |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 135 | if (logmode & LOGMODE_SYSLOG) { |
| 136 | syslog(LOG_ERR, "%s", msgc); |
| 137 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 138 | # endif |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 139 | free(msgc); |
| 140 | } |
| 141 | #endif |
Denys Vlasenko | 7e1bb4b | 2010-07-04 17:16:44 +0200 | [diff] [blame] | 142 | |
| 143 | |
| 144 | void FAST_FUNC bb_error_msg_and_die(const char *s, ...) |
| 145 | { |
| 146 | va_list p; |
| 147 | |
| 148 | va_start(p, s); |
| 149 | bb_verror_msg(s, p, NULL); |
| 150 | va_end(p); |
| 151 | xfunc_die(); |
| 152 | } |
| 153 | |
| 154 | void FAST_FUNC bb_error_msg(const char *s, ...) |
| 155 | { |
| 156 | va_list p; |
| 157 | |
| 158 | va_start(p, s); |
| 159 | bb_verror_msg(s, p, NULL); |
| 160 | va_end(p); |
| 161 | } |