verror_msg.c 3.3 KB

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