syslog.c raw

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