syslog.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdarg.h>
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <syslog.h>
  6. #include <time.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include "libc.h"
  13. static volatile int lock[2];
  14. static char log_ident[32];
  15. static int log_opt;
  16. static int log_facility = LOG_USER;
  17. static int log_mask = 0xff;
  18. static int log_fd = -1;
  19. int setlogmask(int maskpri)
  20. {
  21. LOCK(lock);
  22. int ret = log_mask;
  23. if (maskpri) log_mask = maskpri;
  24. UNLOCK(lock);
  25. return ret;
  26. }
  27. static const struct {
  28. short sun_family;
  29. char sun_path[9];
  30. } log_addr = {
  31. AF_UNIX,
  32. "/dev/log"
  33. };
  34. void closelog(void)
  35. {
  36. int cs;
  37. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  38. LOCK(lock);
  39. close(log_fd);
  40. log_fd = -1;
  41. UNLOCK(lock);
  42. pthread_setcancelstate(cs, 0);
  43. }
  44. static void __openlog()
  45. {
  46. log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
  47. if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
  48. }
  49. void openlog(const char *ident, int opt, int facility)
  50. {
  51. int cs;
  52. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  53. LOCK(lock);
  54. if (ident) {
  55. size_t n = strnlen(ident, sizeof log_ident - 1);
  56. memcpy(log_ident, ident, n);
  57. log_ident[n] = 0;
  58. } else {
  59. log_ident[0] = 0;
  60. }
  61. log_opt = opt;
  62. log_facility = facility;
  63. if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
  64. UNLOCK(lock);
  65. pthread_setcancelstate(cs, 0);
  66. }
  67. static int is_lost_conn(int e)
  68. {
  69. return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE;
  70. }
  71. static void _vsyslog(int priority, const char *message, va_list ap)
  72. {
  73. char timebuf[16];
  74. time_t now;
  75. struct tm tm;
  76. char buf[1024];
  77. int errno_save = errno;
  78. int pid;
  79. int l, l2;
  80. int hlen;
  81. int fd;
  82. if (log_fd < 0) __openlog();
  83. if (!(priority & LOG_FACMASK)) priority |= log_facility;
  84. now = time(NULL);
  85. gmtime_r(&now, &tm);
  86. strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
  87. pid = (log_opt & LOG_PID) ? getpid() : 0;
  88. l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
  89. priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
  90. errno = errno_save;
  91. l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
  92. if (l2 >= 0) {
  93. if (l2 >= sizeof buf - l) l = sizeof buf - 1;
  94. else l += l2;
  95. if (buf[l-1] != '\n') buf[l++] = '\n';
  96. if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno)
  97. || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0
  98. || send(log_fd, buf, l, 0) < 0)
  99. && (log_opt & LOG_CONS)) {
  100. fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
  101. if (fd >= 0) {
  102. dprintf(fd, "%.*s", l-hlen, buf+hlen);
  103. close(fd);
  104. }
  105. }
  106. if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
  107. }
  108. }
  109. void __vsyslog(int priority, const char *message, va_list ap)
  110. {
  111. int cs;
  112. if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
  113. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  114. LOCK(lock);
  115. _vsyslog(priority, message, ap);
  116. UNLOCK(lock);
  117. pthread_setcancelstate(cs, 0);
  118. }
  119. void syslog(int priority, const char *message, ...)
  120. {
  121. va_list ap;
  122. va_start(ap, message);
  123. __vsyslog(priority, message, ap);
  124. va_end(ap);
  125. }
  126. weak_alias(__vsyslog, vsyslog);