2 #include <sys/socket.h>
14 static volatile int lock[2];
15 static char log_ident[32];
17 static int log_facility = LOG_USER;
18 static int log_mask = 0xff;
19 static int log_fd = -1;
21 int setlogmask(int maskpri)
25 if (maskpri) log_mask = maskpri;
41 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
46 pthread_setcancelstate(cs, 0);
49 static void __openlog()
51 int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
53 if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
59 void openlog(const char *ident, int opt, int facility)
62 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
66 size_t n = strnlen(ident, sizeof log_ident - 1);
67 memcpy(log_ident, ident, n);
73 log_facility = facility;
75 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
78 pthread_setcancelstate(cs, 0);
81 static void _vsyslog(int priority, const char *message, va_list ap)
87 int errno_save = errno;
93 if (log_fd < 0) __openlog();
95 if (!(priority & LOG_FACMASK)) priority |= log_facility;
99 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
101 pid = (log_opt & LOG_PID) ? getpid() : 0;
102 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
103 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
105 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
107 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
109 if (buf[l-1] != '\n') buf[l++] = '\n';
110 if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
111 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
113 dprintf(fd, "%.*s", l-hlen, buf+hlen);
117 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
121 void __vsyslog(int priority, const char *message, va_list ap)
124 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
125 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
127 _vsyslog(priority, message, ap);
129 pthread_setcancelstate(cs, 0);
132 void syslog(int priority, const char *message, ...)
135 va_start(ap, message);
136 __vsyslog(priority, message, ap);
140 weak_alias(__vsyslog, vsyslog);