syslog.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
  47. if (fd < 0) return;
  48. if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
  49. close(fd);
  50. else
  51. log_fd = fd;
  52. }
  53. void openlog(const char *ident, int opt, int facility)
  54. {
  55. int cs;
  56. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  57. LOCK(lock);
  58. if (ident) {
  59. size_t n = strnlen(ident, sizeof log_ident - 1);
  60. memcpy(log_ident, ident, n);
  61. log_ident[n] = 0;
  62. } else {
  63. log_ident[0] = 0;
  64. }
  65. log_opt = opt;
  66. log_facility = facility;
  67. if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
  68. UNLOCK(lock);
  69. pthread_setcancelstate(cs, 0);
  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 && (log_opt & LOG_CONS)) {
  97. fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
  98. if (fd >= 0) {
  99. dprintf(fd, "%.*s", l-hlen, buf+hlen);
  100. close(fd);
  101. }
  102. }
  103. if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
  104. }
  105. }
  106. void __vsyslog(int priority, const char *message, va_list ap)
  107. {
  108. int cs;
  109. if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
  110. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  111. LOCK(lock);
  112. _vsyslog(priority, message, ap);
  113. UNLOCK(lock);
  114. pthread_setcancelstate(cs, 0);
  115. }
  116. void syslog(int priority, const char *message, ...)
  117. {
  118. va_list ap;
  119. va_start(ap, message);
  120. __vsyslog(priority, message, ap);
  121. va_end(ap);
  122. }
  123. weak_alias(__vsyslog, vsyslog);