logger.c revision b52429d4e0de52b89c7d043164aa1ac720e9179f
/*-*- Mode: C; c-basic-offset: 8 -*-*/
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include "util.h"
#include "log.h"
#include "list.h"
#define STREAM_BUFFER 2048
#define STREAMS_MAX 256
#define SERVER_FD_START 3
#define SERVER_FD_MAX 16
typedef struct Server {
int log_fd;
int epoll_fd;
unsigned n_server_fd;
unsigned n_streams;
} Server;
typedef enum StreamState {
} StreamState;
struct Stream {
int fd;
int priority;
char *process;
char buffer[STREAM_BUFFER];
};
#define IOVEC_SET_STRING(iovec, s) \
do { \
} while(false);
time_t t;
assert(s);
assert(p);
if (*p == 0)
return 0;
/*
* The format glibc uses is:
*
* <priority>time process[pid]: msg
*/
return -EINVAL;
return -EINVAL;
return -errno;
return 0;
}
int r;
assert(s);
assert(p);
p = strstrip(p);
switch (s->state) {
case STREAM_PRIORITY:
return r;
if (s->priority < 0)
return -ERANGE;
s->state = STREAM_PROCESS;
return 0;
case STREAM_PROCESS:
return -ENOMEM;
s->state = STREAM_RUNNING;
return 0;
case STREAM_RUNNING:
return stream_log(s, p, timestamp);
}
assert_not_reached("Unknown stream state");
}
char *p;
int r = 0;
assert(s);
p = s->buffer;
for (;;) {
char *newline;
break;
*newline = 0;
if ((r = stream_line(s, p, timestamp)) >= 0) {
p = newline+1;
}
}
if (p > s->buffer) {
}
return r;
}
ssize_t l;
int r;
assert(s);
return 0;
return -1;
}
if (l == 0)
return 0;
s->length += l;
r = stream_scan(s, timestamp);
if (r < 0)
return r;
return 1;
}
static void stream_free(Stream *s) {
assert(s);
if (s->server) {
}
if (s->fd >= 0) {
if (s->server)
}
free(s);
}
int fd;
struct epoll_event ev;
int r;
assert(s);
return -errno;
if (s->n_streams >= STREAMS_MAX) {
log_warning("Too many connections, refusing connection.");
return 0;
}
return -ENOMEM;
}
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
s->n_streams ++;
return 0;
fail:
return r;
}
static int verify_environment(unsigned *n_sockets) {
unsigned long long pid;
const char *e;
int r;
unsigned ns;
if (!(e = getenv("LISTEN_PID"))) {
log_error("Missing $LISTEN_PID environment variable.");
return -ENOENT;
}
if ((r = safe_atollu(e, &pid)) < 0) {
return r;
}
log_error("Socket nor for me.");
return -ENOENT;
}
if (!(e = getenv("LISTEN_FDS"))) {
log_error("Missing $LISTEN_FDS environment variable.");
return -ENOENT;
}
return -E2BIG;
}
log_error("Wrong number of file descriptors passed: %s", e);
return -E2BIG;
}
return 0;
}
static void server_done(Server *s) {
unsigned i;
assert(s);
while (s->streams)
stream_free(s->streams);
for (i = 0; i < s->n_server_fd; i++)
if (s->log_fd >= 0)
if (s->epoll_fd >= 0)
}
int r;
unsigned i;
union {
struct sockaddr_un un;
} sa;
assert(s);
zero(*s);
s->n_server_fd = n_sockets;
s->log_fd = -1;
r = -errno;
goto fail;
}
for (i = 0; i < n_sockets; i++) {
struct epoll_event ev;
r = -errno;
goto fail;
}
}
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
return 0;
fail:
server_done(s);
return r;
}
int r;
assert(s);
/* Yes, this is a bit ugly, we assume that that valid pointers
* are > SERVER_FD_START+SERVER_FD_MAX. Which is certainly
* true on Linux (and probably most other OSes, too, since the
* first 4k usually are part of a seperate null pointer
* dereference page. */
log_info("Got invalid event from epoll. (1)");
return -EIO;
}
return r;
}
} else {
log_info("Got invalid event from epoll. (3)");
return 0;
}
if (r < 0)
return 0;
}
}
return 0;
}
int r = 3;
unsigned n;
if (verify_environment(&n) < 0)
return 1;
if (server_init(&server, n) < 0)
return 2;
for (;;) {
struct epoll_event event;
int n;
&event, 1,
continue;
goto fail;
}
if (n <= 0)
break;
goto fail;
}
r = 0;
fail:
return r;
}