From: Fredrik Tolf Date: Sat, 9 Aug 2008 18:45:15 +0000 (+0200) Subject: Added daemonization and syslogging. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=a13b233b0ac23495df52fa80dddd02f24ce71491;p=mctap.git Added daemonization and syslogging. --- diff --git a/src/mctap.c b/src/mctap.c index 2bec00f..842a9d6 100644 --- a/src/mctap.c +++ b/src/mctap.c @@ -11,12 +11,13 @@ #include #include #include +#include #include "utils.h" static void usage(FILE *out) { - fprintf(out, "usage: mctap [-h] [-D TAPNAME] MCASTGROUP PORT\n"); + fprintf(out, "usage: mctap [-hd] [-D TAPNAME] MCASTGROUP PORT\n"); } static __attribute__ ((unused)) char *formataddress(struct sockaddr *arg, socklen_t arglen) @@ -126,7 +127,7 @@ static void bridge(int sock, int tap, struct sockaddr *dst, socklen_t dstlen) ret = poll(pfds, 2, -1); if(ret < 0) { if(errno != EINTR) { - fprintf(stderr, "mctap: poll: %s\n", strerror(errno)); + syslog(LOG_ERR, "mctap: poll: %s", strerror(errno)); exit(1); } continue; @@ -135,7 +136,7 @@ static void bridge(int sock, int tap, struct sockaddr *dst, socklen_t dstlen) ret = read(sock, buf, sizeof(buf)); if(ret < 0) { if((errno != EINTR) && (errno != EAGAIN)) { - fprintf(stderr, "mctap: mcast packet: %s\n", strerror(errno)); + syslog(LOG_ERR, "mctap: mcast packet: %s", strerror(errno)); exit(1); } } else { @@ -154,7 +155,7 @@ static void bridge(int sock, int tap, struct sockaddr *dst, socklen_t dstlen) ret = read(tap, buf, sizeof(buf)); if(ret < 0) { if((errno != EINTR) && (errno != EAGAIN)) { - fprintf(stderr, "mctap: mcast packet: %s\n", strerror(errno)); + syslog(LOG_ERR, "mctap: mcast packet: %s", strerror(errno)); exit(1); } } else { @@ -195,14 +196,19 @@ int main(int argc, char **argv) struct in_addr group; int port; char *tapname; + int daemonize; struct sockaddr_in dst; tapname = "mctap"; - while((c = getopt(argc, argv, "hD:")) >= 0) { + daemonize = 0; + while((c = getopt(argc, argv, "hD:d")) >= 0) { switch(c) { case 'D': tapname = optarg; break; + case 'd': + daemonize = 1; + break; case 'h': usage(stdout); return(0); @@ -228,6 +234,10 @@ int main(int argc, char **argv) fprintf(stderr, "mctap: could not create TAP device: %s\n", strerror(errno)); exit(1); } + openlog(sprintf2("mctap-%s", tapname), LOG_PID, LOG_DAEMON); + + if(daemonize) + daemon(0, 0); memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET;