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 | |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | b5f50ea | 2006-09-12 13:27:55 +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 | |
| 16 | void 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 | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 18 | /* va_copy is used because it is not portable |
| 19 | * to use va_list p twice */ |
| 20 | va_list p2; |
| 21 | va_copy(p2, p); |
| 22 | |
Denis Vlasenko | 4ef7d3a | 2007-05-29 21:41:28 +0000 | [diff] [blame] | 23 | if (!s) /* nomsg[_and_die] uses NULL fmt */ |
| 24 | s = ""; /* some libc don't like printf(NULL) */ |
| 25 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 26 | if (logmode & LOGMODE_STDIO) { |
| 27 | fflush(stdout); |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 28 | fprintf(stderr, "%s: ", applet_name); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 29 | vfprintf(stderr, s, p); |
| 30 | if (!strerr) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 31 | fputs(msg_eol, stderr); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 32 | else |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 33 | fprintf(stderr, "%s%s%s", |
Denis Vlasenko | 4ef7d3a | 2007-05-29 21:41:28 +0000 | [diff] [blame] | 34 | s[0] ? ": " : "", |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 35 | strerr, msg_eol); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 36 | } |
Denis Vlasenko | b750dec | 2006-09-07 16:03:45 +0000 | [diff] [blame] | 37 | if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 38 | if (!strerr) |
| 39 | vsyslog(LOG_ERR, s, p2); |
| 40 | else { |
| 41 | char *msg; |
Denis Vlasenko | 4ef7d3a | 2007-05-29 21:41:28 +0000 | [diff] [blame] | 42 | if (vasprintf(&msg, s, p2) < 0) { |
| 43 | fprintf(stderr, "%s: %s\n", applet_name, bb_msg_memory_exhausted); |
| 44 | xfunc_die(); |
| 45 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 46 | syslog(LOG_ERR, "%s: %s", msg, strerr); |
| 47 | free(msg); |
| 48 | } |
| 49 | } |
| 50 | va_end(p2); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 51 | } |