verror_msg.c 3.9 KB

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