]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/misc/syslog.c
2 #include <sys/socket.h>
9 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
15 #include "fork_impl.h"
17 static volatile int lock
[1];
18 static char log_ident
[32];
20 static int log_facility
= LOG_USER
;
21 static int log_mask
= 0xff;
22 static int log_fd
= -1;
23 volatile int *const __syslog_lockptr
= lock
;
25 int setlogmask(int maskpri
)
29 if (maskpri
) log_mask
= maskpri
;
44 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
46 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
52 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
53 pthread_setcancelstate(cs
, 0);
57 static void __openlog()
59 log_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
60 if (log_fd
>= 0) connect(log_fd
, (void *)&log_addr
, sizeof log_addr
);
63 void openlog(const char *ident
, int opt
, int facility
)
65 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
67 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
72 size_t n
= strnlen(ident
, sizeof log_ident
- 1);
73 memcpy(log_ident
, ident
, n
);
79 log_facility
= facility
;
81 if ((opt
& LOG_NDELAY
) && log_fd
<0) __openlog();
84 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
85 pthread_setcancelstate(cs
, 0);
89 static int is_lost_conn(int e
)
91 return e
==ECONNREFUSED
|| e
==ECONNRESET
|| e
==ENOTCONN
|| e
==EPIPE
;
94 static void _vsyslog(int priority
, const char *message
, va_list ap
)
100 int errno_save
= errno
;
106 if (log_fd
< 0) __openlog();
108 if (!(priority
& LOG_FACMASK
)) priority
|= log_facility
;
112 strftime(timebuf
, sizeof timebuf
, "%b %e %T", &tm
);
114 pid
= (log_opt
& LOG_PID
) ? getpid() : 0;
115 l
= snprintf(buf
, sizeof buf
, "<%d>%s %n%s%s%.0d%s: ",
116 priority
, timebuf
, &hlen
, log_ident
, "["+!pid
, pid
, "]"+!pid
);
118 l2
= vsnprintf(buf
+l
, sizeof buf
- l
, message
, ap
);
120 if (l2
>= sizeof buf
- l
) l
= sizeof buf
- 1;
122 if (buf
[l
-1] != '\n') buf
[l
++] = '\n';
123 if (send(log_fd
, buf
, l
, 0) < 0 && (!is_lost_conn(errno
)
124 || connect(log_fd
, (void *)&log_addr
, sizeof log_addr
) < 0
125 || send(log_fd
, buf
, l
, 0) < 0)
126 && (log_opt
& LOG_CONS
)) {
127 fd
= open("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
129 dprintf(fd
, "%.*s", l
-hlen
, buf
+hlen
);
133 if (log_opt
& LOG_PERROR
) dprintf(2, "%.*s", l
-hlen
, buf
+hlen
);
137 static void __vsyslog(int priority
, const char *message
, va_list ap
)
139 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
142 if (!(log_mask
& LOG_MASK(priority
&7)) || (priority
&~0x3ff)) return;
143 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
144 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
147 _vsyslog(priority
, message
, ap
);
149 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
150 pthread_setcancelstate(cs
, 0);
154 void syslog(int priority
, const char *message
, ...)
157 va_start(ap
, message
);
158 __vsyslog(priority
, message
, ap
);
162 weak_alias(__vsyslog
, vsyslog
);