00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifdef SYSLOG_ASYNC
00045
00046 #include <sys/types.h>
00047 #include <sys/socket.h>
00048 #include <sys/un.h>
00049 #include <sys/file.h>
00050 #include <sys/syslog.h>
00051
00052 #include <sys/uio.h>
00053 #include <sys/wait.h>
00054 #include <netdb.h>
00055 #include <string.h>
00056 #include <time.h>
00057 #include <unistd.h>
00058 #include <errno.h>
00059 #include <stdarg.h>
00060 #include <stdlib.h>
00061 #include <paths.h>
00062 #include <stdio.h>
00063 #include <ctype.h>
00064
00065 #include "syslog_async.h"
00066
00067 #include "mem/mem.h"
00068
00069
00070 #define MAX_MESSAGE 1024
00071
00072 #define DEF_BACKLOG 5
00073 #define DEF_DELAY 1000
00074
00075 static int log_fac = LOG_USER;
00076 static int log_opts = LOG_ODELAY;
00077
00078 static const char *log_tag = NAME;
00079 static int log_mask = 0xff;
00080 static int log_backlog = DEF_BACKLOG;
00081 static int log_delay = DEF_DELAY;
00082
00083 static int log_fd = -1;
00084 static int entries_alloced = 0;
00085 static int entries_lost = 0;
00086 static int connection_good = 1;
00087
00088 struct log_entry {
00089 int offset, length;
00090 struct log_entry *next;
00091 char payload[MAX_MESSAGE];
00092 };
00093
00094 static struct log_entry *entries = NULL;
00095 static struct log_entry *free_entries = NULL;
00096
00097
00098 static int mksock(int type)
00099 {
00100 int flags;
00101 int fd = socket(AF_UNIX, type, 0);
00102
00103 if (fd != -1)
00104 {
00105 if ((flags = fcntl(fd, F_GETFL)) == -1 ||
00106 fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1 ||
00107 (flags = fcntl(fd, F_GETFD)) == -1 ||
00108 fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
00109 {
00110 close(fd);
00111 fd = -1;
00112 }
00113 }
00114 return fd;
00115 }
00116
00117
00118 void openlog_async(const char *ident, int option, int facility)
00119 {
00120 if (ident)
00121 log_tag = ident;
00122
00123 log_opts = option;
00124
00125 if (facility != 0 && (facility &~ LOG_FACMASK) == 0)
00126 log_fac = facility;
00127
00128 if (log_opts & LOG_NDELAY)
00129 log_fd = mksock(SOCK_DGRAM);
00130 }
00131
00132
00133 int setlogmask_async(int mask)
00134 {
00135 int old = log_mask;
00136
00137 if (mask != 0)
00138 log_mask = mask;
00139
00140 return old;
00141 }
00142
00143
00144 void tunelog_async(int backlog, int delay)
00145 {
00146
00147
00148 if (backlog < 1)
00149 backlog = 1;
00150 else if (backlog > 99)
00151 backlog = 99;
00152
00153
00154 if (backlog < entries_alloced)
00155 log_backlog = entries_alloced;
00156 else
00157 log_backlog = backlog;
00158
00159 if (delay < 0)
00160 log_delay = 0;
00161 else if (delay > 1000)
00162 log_delay = 1000;
00163 else
00164 log_delay = delay;
00165 }
00166
00167
00168 void closelog_async(void)
00169 {
00170
00171 log_write_async();
00172
00173 if (log_fd != -1)
00174 {
00175 close(log_fd);
00176 log_fd = -1;
00177 }
00178
00179
00180 log_fac = LOG_USER;
00181 log_opts = LOG_ODELAY;
00182 log_tag = NAME;
00183 log_mask = 0xff;
00184
00185 log_delay = DEF_DELAY;
00186
00187 if (entries_alloced < DEF_BACKLOG)
00188 log_backlog = entries_alloced;
00189 else
00190 log_backlog = DEF_BACKLOG;
00191 }
00192
00193
00194 int log_fd_async(void)
00195 {
00196 if (!entries || !connection_good)
00197 return -1;
00198
00199 return log_fd;
00200 }
00201
00202
00203 void log_write_async(void)
00204 {
00205 ssize_t rc;
00206 int fd, tried_stream = 0;
00207 struct log_entry *tmp;
00208
00209 while (entries)
00210 {
00211 if (log_fd == -1 && (log_fd = mksock(SOCK_DGRAM)) == -1)
00212 goto fail;
00213
00214 connection_good = 1;
00215
00216 if ((rc = send(log_fd, entries->payload + entries->offset,
00217 entries->length, MSG_NOSIGNAL)) != -1)
00218 {
00219 entries->length -= rc;
00220 entries->offset += rc;
00221 connection_good = 1;
00222
00223 if (entries->length == 0)
00224 goto free;
00225
00226 continue;
00227 }
00228
00229 if (errno == EINTR)
00230 continue;
00231
00232 if (errno == EAGAIN)
00233 return;
00234
00235
00236 if (errno == ENOBUFS)
00237 {
00238 connection_good = 0;
00239 return;
00240 }
00241
00242
00243
00244 if (errno == EPIPE)
00245 goto reopen_stream;
00246
00247 if (errno == ECONNREFUSED || errno == ENOTCONN ||
00248 errno == EDESTADDRREQ || errno == ECONNRESET)
00249 {
00250
00251
00252
00253
00254
00255
00256 struct sockaddr_un logaddr;
00257
00258 logaddr.sun_family = AF_LOCAL;
00259 strncpy(logaddr.sun_path, _PATH_LOG, sizeof(logaddr.sun_path));
00260
00261
00262 if (connect(log_fd, (struct sockaddr *)&logaddr, sizeof(logaddr)) != -1)
00263 continue;
00264
00265
00266 if (errno == ENOENT || errno == EALREADY || errno == ECONNREFUSED ||
00267 errno == EISCONN || errno == EINTR || errno == EAGAIN)
00268 {
00269
00270 connection_good = 0;
00271 return;
00272 }
00273
00274
00275 if (!tried_stream && errno == EPROTOTYPE)
00276 {
00277 reopen_stream:
00278 tried_stream = 1;
00279 close(log_fd);
00280 if ((log_fd = mksock(SOCK_STREAM)) != -1)
00281 continue;
00282 }
00283 }
00284
00285 fail:
00286 tried_stream = 0;
00287
00288
00289
00290 if ((log_opts & LOG_CONS) &&
00291 (fd = open(_PATH_CONSOLE, O_WRONLY | O_NONBLOCK, 0)) != -1)
00292 {
00293 char *start = strchr(entries->payload, '>') + 1;
00294 int flags = fcntl(fd, F_GETFL);
00295
00296 if (flags != -1)
00297 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
00298
00299 entries->length -= start - entries->payload;
00300
00301 memmove(entries->payload, start, entries->length);
00302 entries->payload[entries->length - 1] = '\r';
00303 entries->payload[entries->length] = '\n';
00304 write(fd, entries->payload, entries->length + 1);
00305 close(fd);
00306 }
00307
00308 free:
00309 tmp = entries;
00310 entries = tmp->next;
00311 tmp->next = free_entries;
00312 free_entries = tmp;
00313
00314 if (entries_lost != 0)
00315 {
00316 int e = entries_lost;
00317 entries_lost = 0;
00318 syslog_async(LOG_WARNING, "async_syslog overflow: %d log entries lost", e);
00319 }
00320 continue;
00321 }
00322 }
00323
00324 void syslog_async(int priority, const char *format, ...)
00325 {
00326 va_list ap;
00327
00328 va_start(ap, format);
00329 vsyslog_async(priority, format, ap);
00330 va_end(ap);
00331 }
00332
00333 void vsyslog_async(int priority, const char *format, va_list ap)
00334 {
00335 struct log_entry *entry;
00336 time_t time_now;
00337 char *p, *q, *r;
00338 size_t len;
00339
00340 if (!(log_mask & LOG_MASK(LOG_PRI(priority))) || (priority &~ (LOG_PRIMASK|LOG_FACMASK)))
00341 return;
00342
00343 if ((entry = free_entries))
00344 free_entries = entry->next;
00345 else if (entries_alloced < log_backlog && (entry = pkg_malloc(sizeof(struct log_entry))))
00346 entries_alloced++;
00347
00348 if (!entry)
00349 entries_lost++;
00350 else
00351 {
00352
00353 entry->next = NULL;
00354 if (!entries)
00355 entries = entry;
00356 else
00357 {
00358 struct log_entry *tmp;
00359 for (tmp = entries; tmp->next; tmp = tmp->next);
00360 tmp->next = entry;
00361 }
00362
00363 time(&time_now);
00364 p = entry->payload;
00365 p += sprintf(p, "<%d>", priority | log_fac);
00366
00367 q = p;
00368
00369 if (log_opts & LOG_PID)
00370 p += sprintf(p, "%.15s %s[%d]: ", ctime(&time_now) + 4, log_tag, getpid());
00371 else
00372 p += sprintf(p, "%.15s %s: ", ctime(&time_now) + 4, log_tag);
00373
00374 len = p - entry->payload;
00375 len += vsnprintf(p, MAX_MESSAGE - len, format, ap) + 1;
00376 entry->length = len > MAX_MESSAGE ? MAX_MESSAGE : len;
00377
00378
00379 for (r = &entry->payload[entry->length - 2]; r >= entry->payload; r--)
00380 if (*r == '\n')
00381 entry->length--;
00382 else
00383 break;
00384
00385 entry->offset = 0;
00386
00387 if (log_opts & LOG_PERROR)
00388 {
00389 ssize_t rc, s = entry->length - (q - entry->payload);
00390
00391 entry->payload[entry->length - 1] = '\n';
00392
00393 while (s != 0)
00394 if ((rc = write(STDERR_FILENO, q, s)) != -1)
00395 {
00396 s -= rc;
00397 q += rc;
00398 continue;
00399 }
00400 else if (errno == EINTR)
00401 continue;
00402 else
00403 break;
00404 }
00405 entry->payload[entry->length - 1] = 0;
00406 }
00407
00408
00409
00410 log_write_async();
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 if (entries && log_delay != 0)
00421 {
00422 struct timespec waiter;
00423 int d;
00424
00425 for (d = 1,entry = entries; entry->next; entry = entry->next)
00426 {
00427 d *= 2;
00428 if (d >= log_delay)
00429 {
00430 d = log_delay - 1;
00431 break;
00432 }
00433 }
00434
00435 waiter.tv_sec = 0;
00436 waiter.tv_nsec = d * 1000000;
00437 nanosleep(&waiter, NULL);
00438
00439
00440 log_write_async();
00441 }
00442 }
00443
00444 #endif