3
0

verror_msg.c 3.5 KB

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