syslog.c 2.4 KB

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