3
0

verror_msg.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. 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. if (!s) /* nomsg[_and_die] uses NULL fmt */
  26. s = ""; /* some libc don't like printf(NULL) */
  27. applet_len = strlen(applet_name) + 2; /* "applet: " */
  28. strerr_len = strerr ? strlen(strerr) : 0;
  29. msgeol_len = strlen(msg_eol);
  30. /* This costs ~90 bytes of code, but avoids costly
  31. * malloc()[in vasprintf]+realloc()+memmove()+free() in 99% of cases.
  32. * ~40% speedup.
  33. */
  34. if ((int)sizeof(stack_msg) - applet_len > 0) {
  35. va_list p2;
  36. /* It is not portable to use va_list twice, need to va_copy it */
  37. va_copy(p2, p);
  38. used = vsnprintf(stack_msg + applet_len, (int)sizeof(stack_msg) - applet_len, s, p2);
  39. va_end(p2);
  40. msg = stack_msg;
  41. used += applet_len;
  42. if (used < (int)sizeof(stack_msg) - 3 - msgeol_len - strerr_len)
  43. goto add_pfx_and_sfx;
  44. }
  45. used = vasprintf(&msg, s, p);
  46. if (used < 0)
  47. return;
  48. /* This is ugly and costs +60 bytes compared to multiple
  49. * fprintf's, but is guaranteed to do a single write.
  50. * This is needed for e.g. httpd logging, when multiple
  51. * children can produce log messages simultaneously. */
  52. /* can't use xrealloc: it calls error_msg on failure,
  53. * that may result in a recursion */
  54. /* +3 is for ": " before strerr and for terminating NUL */
  55. msg1 = realloc(msg, applet_len + used + strerr_len + msgeol_len + 3);
  56. if (!msg1) {
  57. msg[used++] = '\n'; /* overwrites NUL */
  58. applet_len = 0;
  59. } else {
  60. msg = msg1;
  61. /* TODO: maybe use writev instead of memmoving? Need full_writev? */
  62. memmove(msg + applet_len, msg, used);
  63. used += applet_len;
  64. add_pfx_and_sfx:
  65. strcpy(msg, applet_name);
  66. msg[applet_len - 2] = ':';
  67. msg[applet_len - 1] = ' ';
  68. if (strerr) {
  69. if (s[0]) { /* not perror_nomsg? */
  70. msg[used++] = ':';
  71. msg[used++] = ' ';
  72. }
  73. strcpy(&msg[used], strerr);
  74. used += strerr_len;
  75. }
  76. strcpy(&msg[used], msg_eol);
  77. used += msgeol_len;
  78. }
  79. if (logmode & LOGMODE_STDIO) {
  80. fflush_all();
  81. full_write(STDERR_FILENO, msg, used);
  82. }
  83. #if ENABLE_FEATURE_SYSLOG
  84. if (logmode & LOGMODE_SYSLOG) {
  85. syslog(syslog_level, "%s", msg + applet_len);
  86. }
  87. #endif
  88. if (msg != stack_msg)
  89. free(msg);
  90. }
  91. #ifdef VERSION_WITH_WRITEV
  92. /* Code size is approximately the same, but currently it's the only user
  93. * of writev in entire bbox. __libc_writev in uclibc is ~50 bytes. */
  94. void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr)
  95. {
  96. int strerr_len, msgeol_len;
  97. struct iovec iov[3];
  98. #define used (iov[2].iov_len)
  99. #define msgv (iov[2].iov_base)
  100. #define msgc ((char*)(iov[2].iov_base))
  101. #define msgptr (&(iov[2].iov_base))
  102. if (!logmode)
  103. return;
  104. if (!s) /* nomsg[_and_die] uses NULL fmt */
  105. s = ""; /* some libc don't like printf(NULL) */
  106. /* Prevent "derefing type-punned ptr will break aliasing rules" */
  107. used = vasprintf((char**)(void*)msgptr, s, p);
  108. if (used < 0)
  109. return;
  110. /* This is ugly and costs +60 bytes compared to multiple
  111. * fprintf's, but is guaranteed to do a single write.
  112. * This is needed for e.g. httpd logging, when multiple
  113. * children can produce log messages simultaneously. */
  114. strerr_len = strerr ? strlen(strerr) : 0;
  115. msgeol_len = strlen(msg_eol);
  116. /* +3 is for ": " before strerr and for terminating NUL */
  117. msgv = xrealloc(msgv, used + strerr_len + msgeol_len + 3);
  118. if (strerr) {
  119. msgc[used++] = ':';
  120. msgc[used++] = ' ';
  121. strcpy(msgc + used, strerr);
  122. used += strerr_len;
  123. }
  124. strcpy(msgc + used, msg_eol);
  125. used += msgeol_len;
  126. if (logmode & LOGMODE_STDIO) {
  127. iov[0].iov_base = (char*)applet_name;
  128. iov[0].iov_len = strlen(applet_name);
  129. iov[1].iov_base = (char*)": ";
  130. iov[1].iov_len = 2;
  131. /*iov[2].iov_base = msgc;*/
  132. /*iov[2].iov_len = used;*/
  133. fflush_all();
  134. writev(STDERR_FILENO, iov, 3);
  135. }
  136. # if ENABLE_FEATURE_SYSLOG
  137. if (logmode & LOGMODE_SYSLOG) {
  138. syslog(LOG_ERR, "%s", msgc);
  139. }
  140. # endif
  141. free(msgc);
  142. }
  143. #endif
  144. void FAST_FUNC bb_error_msg_and_die(const char *s, ...)
  145. {
  146. va_list p;
  147. va_start(p, s);
  148. bb_verror_msg(s, p, NULL);
  149. va_end(p);
  150. xfunc_die();
  151. }
  152. void FAST_FUNC bb_error_msg(const char *s, ...)
  153. {
  154. va_list p;
  155. va_start(p, s);
  156. bb_verror_msg(s, p, NULL);
  157. va_end(p);
  158. }