remove invalid PLT calls from or1k asm
[musl] / src / misc / syslog.c
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
14 static volatile 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
21 int setlogmask(int maskpri)
22 {
23         LOCK(lock);
24         int ret = log_mask;
25         if (maskpri) log_mask = maskpri;
26         UNLOCK(lock);
27         return ret;
28 }
29
30 static const struct {
31         short sun_family;
32         char sun_path[9];
33 } log_addr = {
34         AF_UNIX,
35         "/dev/log"
36 };
37
38 void closelog(void)
39 {
40         int cs;
41         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
42         LOCK(lock);
43         close(log_fd);
44         log_fd = -1;
45         UNLOCK(lock);
46         pthread_setcancelstate(cs, 0);
47 }
48
49 static void __openlog()
50 {
51         int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
52         if (fd < 0) return;
53         if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
54                 close(fd);
55         else
56                 log_fd = fd;
57 }
58
59 void openlog(const char *ident, int opt, int facility)
60 {
61         int cs;
62         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
63         LOCK(lock);
64
65         if (ident) {
66                 size_t n = strnlen(ident, sizeof log_ident - 1);
67                 memcpy(log_ident, ident, n);
68                 log_ident[n] = 0;
69         } else {
70                 log_ident[0] = 0;
71         }
72         log_opt = opt;
73         log_facility = facility;
74
75         if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
76
77         UNLOCK(lock);
78         pthread_setcancelstate(cs, 0);
79 }
80
81 static void _vsyslog(int priority, const char *message, va_list ap)
82 {
83         char timebuf[16];
84         time_t now;
85         struct tm tm;
86         char buf[1024];
87         int errno_save = errno;
88         int pid;
89         int l, l2;
90         int hlen;
91         int fd;
92
93         if (log_fd < 0) __openlog();
94
95         if (!(priority & LOG_FACMASK)) priority |= log_facility;
96
97         now = time(NULL);
98         gmtime_r(&now, &tm);
99         strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
100
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);
104         errno = errno_save;
105         l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
106         if (l2 >= 0) {
107                 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
108                 else l += l2;
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);
112                         if (fd >= 0) {
113                                 dprintf(fd, "%.*s", l-hlen, buf+hlen);
114                                 close(fd);
115                         }
116                 }
117                 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
118         }
119 }
120
121 void __vsyslog(int priority, const char *message, va_list ap)
122 {
123         int cs;
124         if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
125         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
126         LOCK(lock);
127         _vsyslog(priority, message, ap);
128         UNLOCK(lock);
129         pthread_setcancelstate(cs, 0);
130 }
131
132 void syslog(int priority, const char *message, ...)
133 {
134         va_list ap;
135         va_start(ap, message);
136         __vsyslog(priority, message, ap);
137         va_end(ap);
138 }
139
140 weak_alias(__vsyslog, vsyslog);