From 8cac99017b835d215e0b90e54ddf478000fe559a Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sat, 9 Aug 2008 21:40:04 +0200 Subject: [PATCH] Added option to kill old mctap. --- src/mctap.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/mctap.c b/src/mctap.c index 9fce9ac..9d183f9 100644 --- a/src/mctap.c +++ b/src/mctap.c @@ -20,7 +20,7 @@ static int quit = 0; static void usage(FILE *out) { - fprintf(out, "usage: mctap [-hdp] [-P PIDFILE] [-D TAPNAME] MCASTGROUP PORT\n"); + fprintf(out, "usage: mctap [-hdpk] [-P PIDFILE] [-D TAPNAME] MCASTGROUP PORT\n"); } static __attribute__ ((unused)) char *formataddress(struct sockaddr *arg, socklen_t arglen) @@ -204,6 +204,23 @@ static void sighand(int sig) } } +static void killrunning(char *pidfile) +{ + FILE *pidfd; + int pid; + + if((pidfd = fopen(pidfile, "r")) == NULL) { + fprintf(stderr, "mctab -k: could not read PID file %s: %s\n", pidfile, strerror(errno)); + exit(1); + } + fscanf(pidfd, "%i", &pid); + if(kill(pid, SIGTERM)) { + fprintf(stderr, "mctab -k: could not kill %i: %s\n", pid, strerror(errno)); + exit(1); + } + fclose(pidfd); +} + int main(int argc, char **argv) { int c; @@ -212,14 +229,14 @@ int main(int argc, char **argv) int port; char *tapname; char *pidfile; - int daemonize; + int daemonize, killold; struct sockaddr_in dst; FILE *pidfd; tapname = "mctap"; - daemonize = 0; + daemonize = killold = 0; pidfile = NULL; - while((c = getopt(argc, argv, "hD:dpP:")) >= 0) { + while((c = getopt(argc, argv, "hD:dpP:k")) >= 0) { switch(c) { case 'D': tapname = optarg; @@ -233,6 +250,11 @@ int main(int argc, char **argv) case 'P': pidfile = optarg; break; + case 'k': + killold = 1; + if(pidfile == NULL) + pidfile = (void *)-1; + break; case 'h': usage(stdout); return(0); @@ -243,6 +265,10 @@ int main(int argc, char **argv) } if(pidfile == (void *)-1) pidfile = sprintf2("/var/run/mctap.%s.pid", tapname); + if(killold) { + killrunning(pidfile); + return(0); + } if(argc - optind < 2) { usage(stderr); exit(1); -- 2.11.0