syslog.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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|SOCK_CLOEXEC, 0);
  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. __openlog(ident, opt, facility);
  55. UNLOCK(lock);
  56. pthread_setcancelstate(cs, 0);
  57. }
  58. static void _vsyslog(int priority, const char *message, va_list ap)
  59. {
  60. char timebuf[16];
  61. time_t now;
  62. struct tm tm;
  63. char buf[256];
  64. int pid;
  65. int l, l2;
  66. if (log_fd < 0) {
  67. __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
  68. if (log_fd < 0) {
  69. UNLOCK(lock);
  70. return;
  71. }
  72. }
  73. now = time(NULL);
  74. gmtime_r(&now, &tm);
  75. strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
  76. pid = (log_opt & LOG_PID) ? getpid() : 0;
  77. l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
  78. priority, timebuf,
  79. log_ident ? log_ident : "",
  80. "["+!pid, pid, "]"+!pid);
  81. l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
  82. if (l2 >= 0) {
  83. l += l2;
  84. if (buf[l-1] != '\n') buf[l++] = '\n';
  85. sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
  86. }
  87. UNLOCK(lock);
  88. }
  89. void __vsyslog(int priority, const char *message, va_list ap)
  90. {
  91. int cs;
  92. if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
  93. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  94. LOCK(lock);
  95. _vsyslog(priority, message, ap);
  96. UNLOCK(lock);
  97. pthread_setcancelstate(cs, 0);
  98. }
  99. void syslog(int priority, const char *message, ...)
  100. {
  101. va_list ap;
  102. va_start(ap, message);
  103. __vsyslog(priority, message, ap);
  104. va_end(ap);
  105. }
  106. weak_alias(__vsyslog, vsyslog);