]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/misc/syslog.c
bed11b53e607660743bb382e9e9880a01852ce60
2 #include <sys/socket.h>
9 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
16 static volatile int lock
[1];
17 static char log_ident
[32];
19 static int log_facility
= LOG_USER
;
20 static int log_mask
= 0xff;
21 static int log_fd
= -1;
23 int setlogmask(int maskpri
)
27 if (maskpri
) log_mask
= maskpri
;
42 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
44 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
50 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
51 pthread_setcancelstate(cs
, 0);
55 static void __openlog()
57 log_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
58 if (log_fd
>= 0) connect(log_fd
, (void *)&log_addr
, sizeof log_addr
);
61 void openlog(const char *ident
, int opt
, int facility
)
63 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
65 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
70 size_t n
= strnlen(ident
, sizeof log_ident
- 1);
71 memcpy(log_ident
, ident
, n
);
77 log_facility
= facility
;
79 if ((opt
& LOG_NDELAY
) && log_fd
<0) __openlog();
82 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
83 pthread_setcancelstate(cs
, 0);
87 static int is_lost_conn(int e
)
89 return e
==ECONNREFUSED
|| e
==ECONNRESET
|| e
==ENOTCONN
|| e
==EPIPE
;
92 static void _vsyslog(int priority
, const char *message
, va_list ap
)
98 int errno_save
= errno
;
104 if (log_fd
< 0) __openlog();
106 if (!(priority
& LOG_FACMASK
)) priority
|= log_facility
;
110 strftime(timebuf
, sizeof timebuf
, "%b %e %T", &tm
);
112 pid
= (log_opt
& LOG_PID
) ? getpid() : 0;
113 l
= snprintf(buf
, sizeof buf
, "<%d>%s %n%s%s%.0d%s: ",
114 priority
, timebuf
, &hlen
, log_ident
, "["+!pid
, pid
, "]"+!pid
);
116 l2
= vsnprintf(buf
+l
, sizeof buf
- l
, message
, ap
);
118 if (l2
>= sizeof buf
- l
) l
= sizeof buf
- 1;
120 if (buf
[l
-1] != '\n') buf
[l
++] = '\n';
121 if (send(log_fd
, buf
, l
, 0) < 0 && (!is_lost_conn(errno
)
122 || connect(log_fd
, (void *)&log_addr
, sizeof log_addr
) < 0
123 || send(log_fd
, buf
, l
, 0) < 0)
124 && (log_opt
& LOG_CONS
)) {
125 fd
= open("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
127 dprintf(fd
, "%.*s", l
-hlen
, buf
+hlen
);
131 if (log_opt
& LOG_PERROR
) dprintf(2, "%.*s", l
-hlen
, buf
+hlen
);
135 static void __vsyslog(int priority
, const char *message
, va_list ap
)
137 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
140 if (!(log_mask
& LOG_MASK(priority
&7)) || (priority
&~0x3ff)) return;
141 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
142 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
145 _vsyslog(priority
, message
, ap
);
147 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
148 pthread_setcancelstate(cs
, 0);
152 void syslog(int priority
, const char *message
, ...)
155 va_start(ap
, message
);
156 __vsyslog(priority
, message
, ap
);
160 weak_alias(__vsyslog
, vsyslog
);