syslog.c 2.8 KB

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