2 #include <sys/socket.h>
14 static volatile int lock
[1];
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 log_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
52 if (log_fd
>= 0) connect(log_fd
, (void *)&log_addr
, sizeof log_addr
);
55 void openlog(const char *ident
, int opt
, int facility
)
58 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
62 size_t n
= strnlen(ident
, sizeof log_ident
- 1);
63 memcpy(log_ident
, ident
, n
);
69 log_facility
= facility
;
71 if ((opt
& LOG_NDELAY
) && log_fd
<0) __openlog();
74 pthread_setcancelstate(cs
, 0);
77 static int is_lost_conn(int e
)
79 return e
==ECONNREFUSED
|| e
==ECONNRESET
|| e
==ENOTCONN
|| e
==EPIPE
;
82 static void _vsyslog(int priority
, const char *message
, va_list ap
)
88 int errno_save
= errno
;
94 if (log_fd
< 0) __openlog();
96 if (!(priority
& LOG_FACMASK
)) priority
|= log_facility
;
100 strftime(timebuf
, sizeof timebuf
, "%b %e %T", &tm
);
102 pid
= (log_opt
& LOG_PID
) ? getpid() : 0;
103 l
= snprintf(buf
, sizeof buf
, "<%d>%s %n%s%s%.0d%s: ",
104 priority
, timebuf
, &hlen
, log_ident
, "["+!pid
, pid
, "]"+!pid
);
106 l2
= vsnprintf(buf
+l
, sizeof buf
- l
, message
, ap
);
108 if (l2
>= sizeof buf
- l
) l
= sizeof buf
- 1;
110 if (buf
[l
-1] != '\n') buf
[l
++] = '\n';
111 if (send(log_fd
, buf
, l
, 0) < 0 && (!is_lost_conn(errno
)
112 || connect(log_fd
, (void *)&log_addr
, sizeof log_addr
) < 0
113 || send(log_fd
, buf
, l
, 0) < 0)
114 && (log_opt
& LOG_CONS
)) {
115 fd
= open("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
117 dprintf(fd
, "%.*s", l
-hlen
, buf
+hlen
);
121 if (log_opt
& LOG_PERROR
) dprintf(2, "%.*s", l
-hlen
, buf
+hlen
);
125 static void __vsyslog(int priority
, const char *message
, va_list ap
)
128 if (!(log_mask
& LOG_MASK(priority
&7)) || (priority
&~0x3ff)) return;
129 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
131 _vsyslog(priority
, message
, ap
);
133 pthread_setcancelstate(cs
, 0);
136 void syslog(int priority
, const char *message
, ...)
139 va_start(ap
, message
);
140 __vsyslog(priority
, message
, ap
);
144 weak_alias(__vsyslog
, vsyslog
);