1#include <stdarg.h> 2#include <sys/socket.h> 3#include <stdio.h> 4#include <unistd.h> 5#include <time.h> 6#include <signal.h> 7#include <string.h> 8#include <pthread.h> 9#include <errno.h> 10#include <fcntl.h> 11#include "lock.h" 12#include "fork_impl.h" 13#include <unsupported_api.h> 14#include "locale_impl.h" 15#include <syslog.h> 16 17static volatile int lock[1]; 18static char log_ident[32]; 19static int log_opt; 20static int log_facility = LOG_USER; 21static int log_mask = 0xff; 22static int log_fd = -1; 23volatile int *const __syslog_lockptr = lock; 24 25int setlogmask(int maskpri) 26{ 27 UNSUPPORTED_API_VOID(LITEOS_A); 28 LOCK(lock); 29 int ret = log_mask; 30 if (maskpri) log_mask = maskpri; 31 UNLOCK(lock); 32 return ret; 33} 34 35static const struct { 36 short sun_family; 37 char sun_path[9]; 38} log_addr = { 39 AF_UNIX, 40 "/dev/log" 41}; 42 43void closelog(void) 44{ 45 int cs; 46 UNSUPPORTED_API_VOID(LITEOS_A); 47 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 48 LOCK(lock); 49 close(log_fd); 50 log_fd = -1; 51 UNLOCK(lock); 52 pthread_setcancelstate(cs, 0); 53} 54 55static void __openlog() 56{ 57 UNSUPPORTED_API_VOID(LITEOS_A); 58 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); 59 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr); 60} 61 62void openlog(const char *ident, int opt, int facility) 63{ 64 int cs; 65 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 66 LOCK(lock); 67 68 if (ident) { 69 size_t n = strnlen(ident, sizeof log_ident - 1); 70 memcpy(log_ident, ident, n); 71 log_ident[n] = 0; 72 } else { 73 log_ident[0] = 0; 74 } 75 log_opt = opt; 76 log_facility = facility; 77 78 if ((opt & LOG_NDELAY) && log_fd<0) __openlog(); 79 80 UNLOCK(lock); 81 pthread_setcancelstate(cs, 0); 82} 83 84static int is_lost_conn(int e) 85{ 86 return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE; 87} 88 89static void _vsyslog(int priority, const char *message, va_list ap) 90{ 91 char timebuf[16]; 92 time_t now; 93 struct tm tm; 94 char buf[1024]; 95 int errno_save = errno; 96 int pid; 97 int l, l2; 98 int hlen; 99 int fd; 100 101 if (log_fd < 0) __openlog(); 102 103 if (!(priority & LOG_FACMASK)) priority |= log_facility; 104 105 now = time(NULL); 106 gmtime_r(&now, &tm); 107 strftime_l(timebuf, sizeof timebuf, "%b %e %T", &tm, C_LOCALE); 108 109 pid = (log_opt & LOG_PID) ? getpid() : 0; 110 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ", 111 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid); 112 errno = errno_save; 113 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap); 114 if (l2 >= 0) { 115 if (l2 >= sizeof buf - l) l = sizeof buf - 1; 116 else l += l2; 117 if (buf[l-1] != '\n') buf[l++] = '\n'; 118 if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno) 119 || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0 120 || send(log_fd, buf, l, 0) < 0) 121 && (log_opt & LOG_CONS)) { 122 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); 123 if (fd >= 0) { 124 dprintf(fd, "%.*s", l-hlen, buf+hlen); 125 close(fd); 126 } 127 } 128 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen); 129 } 130} 131 132static void __vsyslog(int priority, const char *message, va_list ap) 133{ 134 int cs; 135 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return; 136 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 137 LOCK(lock); 138 _vsyslog(priority, message, ap); 139 UNLOCK(lock); 140 pthread_setcancelstate(cs, 0); 141} 142 143void syslog(int priority, const char *message, ...) 144{ 145 UNSUPPORTED_API_VOID(LITEOS_A); 146 va_list ap; 147 va_start(ap, message); 148 __vsyslog(priority, message, ap); 149 va_end(ap); 150} 151 152weak_alias(__vsyslog, vsyslog); 153