#include #include #include #include #include #include #include #include #include #define BUFSIZE 4096 #define DEBUG(...) //#define DEBUG(...) printf(__VA_ARGS__) static char pri[8][7] = { "emerg", "alert", "crit", "error", "warn", "notice", "info", "debug" }; static char fac[24][9] = { "kern", "user", "mail", "daemon", "auth", "syslog", "lpr", "news", "uucp", "cron", "authpriv", "ftp", "kern1", "kern2", "kern3", "kern4", "local0", "local1", "local2", "local3", "local4", "local5", "local6", "local7" }; void failerr(char *what, int when) { if (when == -1) { perror(what); exit(1); } } void handle_log(char *entry) { char *p, c; int facpriv = 0; p = entry; if (*p == '<') { while(isdigit(*++p)) { facpriv = 10 * facpriv + *p -'0'; } if (*p == '>') { ++p; } } printf("<%s.%s> %s\n", fac[facpriv >> 3], pri[facpriv & 7], p); } int main() { unsigned int fd_listen; struct sockaddr_un local; char buf[BUFSIZE]; local.sun_family = AF_UNIX; strcpy(local.sun_path, "/dev/log"); unlink(local.sun_path); failerr("socket", (fd_listen = socket(AF_UNIX, SOCK_STREAM, 0))); failerr("bind", bind(fd_listen, (struct sockaddr *)&local, strlen(local.sun_path) + sizeof(local.sun_family))); failerr("listen", listen(fd_listen, 5)); for(;;) { int done, fd_conn, n, t; struct sockaddr_un remote; t = sizeof(remote); failerr("accept", (fd_conn = accept(fd_listen, (struct sockaddr *)&remote, &t))); DEBUG("Accepted connection\n"); done = 0; do { n = recv(fd_conn, buf, BUFSIZE, 0); DEBUG("Received %d bytes of input\n", n); if (n <= 0) { if (n < 0) perror("recv"); done = 1; DEBUG("Want to close\n"); } else { buf[n] = '\0'; handle_log(buf); } } while (!done); close(fd_conn); DEBUG("Closed connection\n"); } }