3
0

verror_msg.c 3.2 KB

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