From db06068169b794e82b7d8b133dc58026c5be1bbb Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Fri, 11 Feb 2011 01:30:45 +0100 Subject: [PATCH] Added a program for redirecting stderr to syslog. --- src/.gitignore | 1 + src/Makefile.am | 3 +- src/errlogger.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/errlogger.c diff --git a/src/.gitignore b/src/.gitignore index a0dd3fe..c1afb8c 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -10,3 +10,4 @@ /htextauth /callfcgi /multifscgi +/errlogger diff --git a/src/Makefile.am b/src/Makefile.am index b53232a..0a1a7d9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,8 @@ SUBDIRS = dirplex bin_PROGRAMS = htparser sendfile callcgi patplex userplex htls \ - callscgi accesslog htextauth callfcgi multifscgi + callscgi accesslog htextauth callfcgi multifscgi \ + errlogger noinst_PROGRAMS = debugsink htparser_SOURCES = htparser.c htparser.h plaintcp.c ssl-gnutls.c diff --git a/src/errlogger.c b/src/errlogger.c new file mode 100644 index 0000000..7a50c2c --- /dev/null +++ b/src/errlogger.c @@ -0,0 +1,146 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include + +static int prio; + +static void logloop(int fd) +{ + FILE *in; + char buf[1024]; + size_t len; + + in = fdopen(fd, "r"); + while(fgets(buf, sizeof(buf), in) != NULL) { + len = strlen(buf); + if(buf[len - 1] == '\n') + buf[len - 1] = 0; + syslog(prio, "%s", buf); + } + fclose(in); +} + +static void usage(FILE *out) +{ + fprintf(out, "usage: errlogger [-h] [-n NAME] [-f FACILITY] [-p PRIO] PROGRAM [ARGS...]\n"); +} + +int main(int argc, char **argv) +{ + int c; + int pfd[2]; + pid_t ch; + char *name; + int fac; + + name = NULL; + prio = LOG_WARNING; + fac = LOG_DAEMON; + while((c = getopt(argc, argv, "hn:p:f:")) >= 0) { + switch(c) { + case 'n': + name = optarg; + break; + case 'f': + if(!strcmp(optarg, "auth")) { + fac = LOG_AUTH; + } else if(!strcmp(optarg, "authpriv")) { + fac = LOG_AUTHPRIV; + } else if(!strcmp(optarg, "cron")) { + fac = LOG_CRON; + } else if(!strcmp(optarg, "daemon")) { + fac = LOG_DAEMON; + } else if(!strcmp(optarg, "ftp")) { + fac = LOG_FTP; + } else if(!strcmp(optarg, "kern")) { + fac = LOG_KERN; + } else if(!strcmp(optarg, "lpr")) { + fac = LOG_LPR; + } else if(!strcmp(optarg, "mail")) { + fac = LOG_MAIL; + } else if(!strcmp(optarg, "news")) { + fac = LOG_NEWS; + } else if(!strcmp(optarg, "user")) { + fac = LOG_USER; + } else if(!strcmp(optarg, "uucp")) { + fac = LOG_UUCP; + } else if(!strncmp(optarg, "local", 5) && (optarg[5] >= '0') && (optarg[5] <= '7') && !optarg[6]) { + fac = LOG_LOCAL0 + (optarg[5] - '0'); + } else { + fprintf(stderr, "errlogger: unknown facility %s\n", optarg); + exit(1); + } + break; + case 'p': + if(!strcmp(optarg, "emerg")) { + fac = LOG_EMERG; + } else if(!strcmp(optarg, "alert")) { + fac = LOG_ALERT; + } else if(!strcmp(optarg, "crit")) { + fac = LOG_CRIT; + } else if(!strcmp(optarg, "err")) { + fac = LOG_ERR; + } else if(!strcmp(optarg, "warning")) { + fac = LOG_WARNING; + } else if(!strcmp(optarg, "notice")) { + fac = LOG_NOTICE; + } else if(!strcmp(optarg, "info")) { + fac = LOG_INFO; + } else if(!strcmp(optarg, "debug")) { + fac = LOG_DEBUG; + } else { + fprintf(stderr, "errlogger: unknown priorty %s\n", optarg); + exit(1); + } + break; + case 'h': + usage(stdout); + exit(0); + default: + usage(stderr); + exit(1); + } + } + if(argc - optind < 1) { + usage(stderr); + exit(1); + } + if(name == NULL) + name = argv[optind]; + openlog(name, 0, fac); + pipe(pfd); + if((ch = fork()) == 0) { + close(pfd[0]); + if(pfd[1] != 2) { + dup2(pfd[1], 2); + close(pfd[1]); + } + execvp(argv[optind], argv + optind); + fprintf(stderr, "errlogger: %s: %s", argv[optind], strerror(errno)); + exit(127); + } + close(pfd[1]); + logloop(pfd[0]); + return(0); +} -- 2.11.0