1570af302Sopenharmony_ci#include <stdarg.h> 2570af302Sopenharmony_ci#include <sys/socket.h> 3570af302Sopenharmony_ci#include <stdio.h> 4570af302Sopenharmony_ci#include <unistd.h> 5570af302Sopenharmony_ci#include <time.h> 6570af302Sopenharmony_ci#include <signal.h> 7570af302Sopenharmony_ci#include <string.h> 8570af302Sopenharmony_ci#include <pthread.h> 9570af302Sopenharmony_ci#include <errno.h> 10570af302Sopenharmony_ci#include <fcntl.h> 11570af302Sopenharmony_ci#include "lock.h" 12570af302Sopenharmony_ci#include "fork_impl.h" 13570af302Sopenharmony_ci#include <unsupported_api.h> 14570af302Sopenharmony_ci#include "locale_impl.h" 15570af302Sopenharmony_ci#include <syslog.h> 16570af302Sopenharmony_ci 17570af302Sopenharmony_cistatic volatile int lock[1]; 18570af302Sopenharmony_cistatic char log_ident[32]; 19570af302Sopenharmony_cistatic int log_opt; 20570af302Sopenharmony_cistatic int log_facility = LOG_USER; 21570af302Sopenharmony_cistatic int log_mask = 0xff; 22570af302Sopenharmony_cistatic int log_fd = -1; 23570af302Sopenharmony_civolatile int *const __syslog_lockptr = lock; 24570af302Sopenharmony_ci 25570af302Sopenharmony_ciint setlogmask(int maskpri) 26570af302Sopenharmony_ci{ 27570af302Sopenharmony_ci UNSUPPORTED_API_VOID(LITEOS_A); 28570af302Sopenharmony_ci LOCK(lock); 29570af302Sopenharmony_ci int ret = log_mask; 30570af302Sopenharmony_ci if (maskpri) log_mask = maskpri; 31570af302Sopenharmony_ci UNLOCK(lock); 32570af302Sopenharmony_ci return ret; 33570af302Sopenharmony_ci} 34570af302Sopenharmony_ci 35570af302Sopenharmony_cistatic const struct { 36570af302Sopenharmony_ci short sun_family; 37570af302Sopenharmony_ci char sun_path[9]; 38570af302Sopenharmony_ci} log_addr = { 39570af302Sopenharmony_ci AF_UNIX, 40570af302Sopenharmony_ci "/dev/log" 41570af302Sopenharmony_ci}; 42570af302Sopenharmony_ci 43570af302Sopenharmony_civoid closelog(void) 44570af302Sopenharmony_ci{ 45570af302Sopenharmony_ci int cs; 46570af302Sopenharmony_ci UNSUPPORTED_API_VOID(LITEOS_A); 47570af302Sopenharmony_ci pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 48570af302Sopenharmony_ci LOCK(lock); 49570af302Sopenharmony_ci close(log_fd); 50570af302Sopenharmony_ci log_fd = -1; 51570af302Sopenharmony_ci UNLOCK(lock); 52570af302Sopenharmony_ci pthread_setcancelstate(cs, 0); 53570af302Sopenharmony_ci} 54570af302Sopenharmony_ci 55570af302Sopenharmony_cistatic void __openlog() 56570af302Sopenharmony_ci{ 57570af302Sopenharmony_ci UNSUPPORTED_API_VOID(LITEOS_A); 58570af302Sopenharmony_ci log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); 59570af302Sopenharmony_ci if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr); 60570af302Sopenharmony_ci} 61570af302Sopenharmony_ci 62570af302Sopenharmony_civoid openlog(const char *ident, int opt, int facility) 63570af302Sopenharmony_ci{ 64570af302Sopenharmony_ci int cs; 65570af302Sopenharmony_ci pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 66570af302Sopenharmony_ci LOCK(lock); 67570af302Sopenharmony_ci 68570af302Sopenharmony_ci if (ident) { 69570af302Sopenharmony_ci size_t n = strnlen(ident, sizeof log_ident - 1); 70570af302Sopenharmony_ci memcpy(log_ident, ident, n); 71570af302Sopenharmony_ci log_ident[n] = 0; 72570af302Sopenharmony_ci } else { 73570af302Sopenharmony_ci log_ident[0] = 0; 74570af302Sopenharmony_ci } 75570af302Sopenharmony_ci log_opt = opt; 76570af302Sopenharmony_ci log_facility = facility; 77570af302Sopenharmony_ci 78570af302Sopenharmony_ci if ((opt & LOG_NDELAY) && log_fd<0) __openlog(); 79570af302Sopenharmony_ci 80570af302Sopenharmony_ci UNLOCK(lock); 81570af302Sopenharmony_ci pthread_setcancelstate(cs, 0); 82570af302Sopenharmony_ci} 83570af302Sopenharmony_ci 84570af302Sopenharmony_cistatic int is_lost_conn(int e) 85570af302Sopenharmony_ci{ 86570af302Sopenharmony_ci return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE; 87570af302Sopenharmony_ci} 88570af302Sopenharmony_ci 89570af302Sopenharmony_cistatic void _vsyslog(int priority, const char *message, va_list ap) 90570af302Sopenharmony_ci{ 91570af302Sopenharmony_ci char timebuf[16]; 92570af302Sopenharmony_ci time_t now; 93570af302Sopenharmony_ci struct tm tm; 94570af302Sopenharmony_ci char buf[1024]; 95570af302Sopenharmony_ci int errno_save = errno; 96570af302Sopenharmony_ci int pid; 97570af302Sopenharmony_ci int l, l2; 98570af302Sopenharmony_ci int hlen; 99570af302Sopenharmony_ci int fd; 100570af302Sopenharmony_ci 101570af302Sopenharmony_ci if (log_fd < 0) __openlog(); 102570af302Sopenharmony_ci 103570af302Sopenharmony_ci if (!(priority & LOG_FACMASK)) priority |= log_facility; 104570af302Sopenharmony_ci 105570af302Sopenharmony_ci now = time(NULL); 106570af302Sopenharmony_ci gmtime_r(&now, &tm); 107570af302Sopenharmony_ci strftime_l(timebuf, sizeof timebuf, "%b %e %T", &tm, C_LOCALE); 108570af302Sopenharmony_ci 109570af302Sopenharmony_ci pid = (log_opt & LOG_PID) ? getpid() : 0; 110570af302Sopenharmony_ci l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ", 111570af302Sopenharmony_ci priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid); 112570af302Sopenharmony_ci errno = errno_save; 113570af302Sopenharmony_ci l2 = vsnprintf(buf+l, sizeof buf - l, message, ap); 114570af302Sopenharmony_ci if (l2 >= 0) { 115570af302Sopenharmony_ci if (l2 >= sizeof buf - l) l = sizeof buf - 1; 116570af302Sopenharmony_ci else l += l2; 117570af302Sopenharmony_ci if (buf[l-1] != '\n') buf[l++] = '\n'; 118570af302Sopenharmony_ci if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno) 119570af302Sopenharmony_ci || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0 120570af302Sopenharmony_ci || send(log_fd, buf, l, 0) < 0) 121570af302Sopenharmony_ci && (log_opt & LOG_CONS)) { 122570af302Sopenharmony_ci fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); 123570af302Sopenharmony_ci if (fd >= 0) { 124570af302Sopenharmony_ci dprintf(fd, "%.*s", l-hlen, buf+hlen); 125570af302Sopenharmony_ci close(fd); 126570af302Sopenharmony_ci } 127570af302Sopenharmony_ci } 128570af302Sopenharmony_ci if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen); 129570af302Sopenharmony_ci } 130570af302Sopenharmony_ci} 131570af302Sopenharmony_ci 132570af302Sopenharmony_cistatic void __vsyslog(int priority, const char *message, va_list ap) 133570af302Sopenharmony_ci{ 134570af302Sopenharmony_ci int cs; 135570af302Sopenharmony_ci if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return; 136570af302Sopenharmony_ci pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); 137570af302Sopenharmony_ci LOCK(lock); 138570af302Sopenharmony_ci _vsyslog(priority, message, ap); 139570af302Sopenharmony_ci UNLOCK(lock); 140570af302Sopenharmony_ci pthread_setcancelstate(cs, 0); 141570af302Sopenharmony_ci} 142570af302Sopenharmony_ci 143570af302Sopenharmony_civoid syslog(int priority, const char *message, ...) 144570af302Sopenharmony_ci{ 145570af302Sopenharmony_ci UNSUPPORTED_API_VOID(LITEOS_A); 146570af302Sopenharmony_ci va_list ap; 147570af302Sopenharmony_ci va_start(ap, message); 148570af302Sopenharmony_ci __vsyslog(priority, message, ap); 149570af302Sopenharmony_ci va_end(ap); 150570af302Sopenharmony_ci} 151570af302Sopenharmony_ci 152570af302Sopenharmony_ciweak_alias(__vsyslog, vsyslog); 153