verror_msg.c 3.9 KB

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