verror_msg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #if ENABLE_FEATURE_SYSLOG
  11. # include <syslog.h>
  12. #endif
  13. #if ENABLE_FEATURE_SYSLOG
  14. static smallint syslog_level = LOG_ERR;
  15. #endif
  16. smallint logmode = LOGMODE_STDIO;
  17. const char *msg_eol = "\n";
  18. void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
  19. {
  20. char *msg, *msg1;
  21. char stack_msg[80];
  22. int applet_len, strerr_len, msgeol_len, used;
  23. if (!logmode)
  24. return;
  25. applet_len = strlen(applet_name) + 2; /* "applet: " */
  26. strerr_len = strerr ? strlen(strerr) : 0;
  27. msgeol_len = strlen(msg_eol);
  28. /* This costs ~90 bytes of code, but avoids costly
  29. * malloc()[in vasprintf]+realloc()+memmove()+free() in 99% of cases.
  30. * ~40% speedup.
  31. */
  32. if ((int)sizeof(stack_msg) - applet_len > 0) {
  33. va_list p2;
  34. /* It is not portable to use va_list twice, need to va_copy it */
  35. va_copy(p2, p);
  36. used = vsnprintf(stack_msg + applet_len, (int)sizeof(stack_msg) - applet_len, s, p2);
  37. va_end(p2);
  38. msg = stack_msg;
  39. used += applet_len;
  40. if (used < (int)sizeof(stack_msg) - 3 - msgeol_len - strerr_len)
  41. goto add_pfx_and_sfx;
  42. }
  43. used = vasprintf(&msg, s, p);
  44. if (used < 0)
  45. return;
  46. /* This is ugly and costs +60 bytes compared to multiple
  47. * fprintf's, but is guaranteed to do a single write.
  48. * This is needed for e.g. httpd logging, when multiple
  49. * children can produce log messages simultaneously. */
  50. /* can't use xrealloc: it calls error_msg on failure,
  51. * that may result in a recursion */
  52. /* +3 is for ": " before strerr and for terminating NUL */
  53. msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
  54. if (!msg1) {
  55. msg[used++] = '\n'; /* overwrites NUL */
  56. applet_len = 0;
  57. } else {
  58. msg = msg1;
  59. /* TODO: maybe use writev instead of memmoving? Need full_writev? */
  60. memmove(msg + applet_len, msg, used);
  61. used += applet_len;
  62. add_pfx_and_sfx:
  63. strcpy(msg, applet_name);
  64. msg[applet_len - 2] = ':';
  65. msg[applet_len - 1] = ' ';
  66. if (strerr) {
  67. if (s[0]) { /* not perror_nomsg? */
  68. msg[used++] = ':';
  69. msg[used++] = ' ';
  70. }
  71. strcpy(&msg[used], strerr);
  72. used += strerr_len;
  73. }
  74. strcpy(&msg[used], msg_eol);
  75. used += msgeol_len;
  76. }
  77. if (logmode & LOGMODE_STDIO) {
  78. fflush_all();
  79. full_write(STDERR_FILENO, msg, used);
  80. }
  81. #if ENABLE_FEATURE_SYSLOG
  82. if (logmode & LOGMODE_SYSLOG) {
  83. syslog(syslog_level, "%s", msg + applet_len);
  84. }
  85. #endif
  86. if (msg != stack_msg)
  87. free(msg);
  88. }
  89. #ifdef VERSION_WITH_WRITEV
  90. /* Code size is approximately the same, but currently it's the only user
  91. * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
  92. void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
  93. {
  94. int strerr_len, msgeol_len;
  95. struct iovec iov[3];
  96. #define used (iov[2].iov_len)
  97. #define msgv (iov[2].iov_base)
  98. #define msgc ((char*)(iov[2].iov_base))
  99. #define msgptr (&(iov[2].iov_base))
  100. if (!logmode)
  101. return;
  102. /* Prevent "derefing type-punned ptr will break aliasing rules" */
  103. used = vasprintf((char**)(void*)msgptr, s, p);
  104. if (used < 0)
  105. return;
  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. strerr_len = strerr ? strlen(strerr) : 0;
  111. msgeol_len = strlen(msg_eol);
  112. /* +3 is for ": " before strerr and for terminating NUL */
  113. msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
  114. if (strerr) {
  115. msgc[used++] = ':';
  116. msgc[used++] = ' ';
  117. strcpy(msgc + used, strerr);
  118. used += strerr_len;
  119. }
  120. strcpy(msgc + used, msg_eol);
  121. used += msgeol_len;
  122. if (logmode & LOGMODE_STDIO) {
  123. iov[0].iov_base = (char*)applet_name;
  124. iov[0].iov_len = strlen(applet_name);
  125. iov[1].iov_base = (char*)": ";
  126. iov[1].iov_len = 2;
  127. /*iov[2].iov_base = msgc;*/
  128. /*iov[2].iov_len = used;*/
  129. fflush_all();
  130. writev(STDERR_FILENO, iov, 3);
  131. }
  132. # if ENABLE_FEATURE_SYSLOG
  133. if (logmode & LOGMODE_SYSLOG) {
  134. syslog(syslog_level, "%s", msgc);
  135. }
  136. # endif
  137. free(msgc);
  138. }
  139. #endif
  140. void FAST_FUNC bb_error_msg_and_die(const char *s, ...)
  141. {
  142. va_list p;
  143. va_start(p, s);
  144. bb_verror_msg(s, p, NULL);
  145. va_end(p);
  146. xfunc_die();
  147. }
  148. void FAST_FUNC bb_error_msg(const char *s, ...)
  149. {
  150. va_list p;
  151. va_start(p, s);
  152. bb_verror_msg(s, p, NULL);
  153. va_end(p);
  154. }
  155. #if ENABLE_FEATURE_SYSLOG_INFO
  156. void FAST_FUNC bb_vinfo_msg(const char *s, va_list p)
  157. {
  158. syslog_level = LOG_INFO;
  159. bb_verror_msg(s, p, NULL);
  160. syslog_level = LOG_ERR;
  161. }
  162. void FAST_FUNC bb_info_msg(const char *s, ...)
  163. {
  164. va_list p;
  165. va_start(p, s);
  166. bb_vinfo_msg(s, p);
  167. va_end(p);
  168. }
  169. void FAST_FUNC bb_simple_info_msg(const char *s)
  170. {
  171. bb_info_msg("%s", s);
  172. }
  173. #endif
  174. void FAST_FUNC bb_simple_error_msg(const char *s)
  175. {
  176. bb_error_msg("%s", s);
  177. }
  178. void FAST_FUNC bb_simple_error_msg_and_die(const char *s)
  179. {
  180. bb_error_msg_and_die("%s", s);
  181. }