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 | */ |
| 9 | |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 11 | #include <syslog.h> |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 13 | smallint logmode = LOGMODE_STDIO; |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 14 | const char *msg_eol = "\n"; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 16 | 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] | 17 | { |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 18 | char *msg; |
| 19 | int applet_len, strerr_len, msgeol_len, used; |
| 20 | |
| 21 | if (!logmode) |
| 22 | return; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 4ef7d3a | 2007-05-29 21:41:28 +0000 | [diff] [blame] | 24 | if (!s) /* nomsg[_and_die] uses NULL fmt */ |
| 25 | s = ""; /* some libc don't like printf(NULL) */ |
| 26 | |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 27 | 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) { |
Denis Vlasenko | 94e3365 | 2007-12-22 15:44:23 +0000 | [diff] [blame] | 48 | if (s[0]) { /* not perror_nomsg? */ |
| 49 | msg[used++] = ':'; |
| 50 | msg[used++] = ' '; |
| 51 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 52 | strcpy(&msg[used], strerr); |
| 53 | used += strerr_len; |
| 54 | } |
| 55 | strcpy(&msg[used], msg_eol); |
| 56 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 57 | if (logmode & LOGMODE_STDIO) { |
| 58 | fflush(stdout); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 59 | full_write(STDERR_FILENO, msg, used + msgeol_len); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 60 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 61 | if (logmode & LOGMODE_SYSLOG) { |
| 62 | syslog(LOG_ERR, "%s", msg + applet_len); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 63 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 64 | free(msg); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 65 | } |
Denis Vlasenko | 1b97efd | 2007-08-17 19:18:06 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | #ifdef VERSION_WITH_WRITEV |
| 69 | |
| 70 | /* Code size is approximately the same, but currently it's the only user |
| 71 | * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */ |
| 72 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 73 | 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] | 74 | { |
| 75 | int strerr_len, msgeol_len; |
| 76 | struct iovec iov[3]; |
| 77 | |
| 78 | #define used (iov[2].iov_len) |
| 79 | #define msgv (iov[2].iov_base) |
| 80 | #define msgc ((char*)(iov[2].iov_base)) |
| 81 | #define msgptr (&(iov[2].iov_base)) |
| 82 | |
| 83 | if (!logmode) |
| 84 | return; |
| 85 | |
| 86 | if (!s) /* nomsg[_and_die] uses NULL fmt */ |
| 87 | s = ""; /* some libc don't like printf(NULL) */ |
| 88 | |
| 89 | /* Prevent "derefing type-punned ptr will break aliasing rules" */ |
| 90 | used = vasprintf((char**)(void*)msgptr, s, p); |
| 91 | if (used < 0) |
| 92 | return; |
| 93 | |
| 94 | /* This is ugly and costs +60 bytes compared to multiple |
| 95 | * fprintf's, but is guaranteed to do a single write. |
| 96 | * This is needed for e.g. httpd logging, when multiple |
| 97 | * children can produce log messages simultaneously. */ |
| 98 | |
| 99 | strerr_len = strerr ? strlen(strerr) : 0; |
| 100 | msgeol_len = strlen(msg_eol); |
| 101 | /* +3 is for ": " before strerr and for terminating NUL */ |
| 102 | msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3); |
| 103 | if (strerr) { |
| 104 | msgc[used++] = ':'; |
| 105 | msgc[used++] = ' '; |
| 106 | strcpy(msgc + used, strerr); |
| 107 | used += strerr_len; |
| 108 | } |
| 109 | strcpy(msgc + used, msg_eol); |
| 110 | used += msgeol_len; |
| 111 | |
| 112 | if (logmode & LOGMODE_STDIO) { |
| 113 | iov[0].iov_base = (char*)applet_name; |
| 114 | iov[0].iov_len = strlen(applet_name); |
| 115 | iov[1].iov_base = (char*)": "; |
| 116 | iov[1].iov_len = 2; |
| 117 | /*iov[2].iov_base = msgc;*/ |
| 118 | /*iov[2].iov_len = used;*/ |
| 119 | fflush(stdout); |
| 120 | writev(2, iov, 3); |
| 121 | } |
| 122 | if (logmode & LOGMODE_SYSLOG) { |
| 123 | syslog(LOG_ERR, "%s", msgc); |
| 124 | } |
| 125 | free(msgc); |
| 126 | } |
| 127 | #endif |