Blob Blame History Raw
diff --git a/amprd.c b/amprd.c
index 4eee8d2..5a3eb99 100644
--- a/amprd.c
+++ b/amprd.c
@@ -68,6 +68,7 @@
 #include "commons.h"
 
 #define MAXPAYLOAD 		(2048)
+#define PIDFILE "/var/run/amprd.pid"
 
 char *passwd = NULL;
 int debug = FALSE;
@@ -83,6 +84,7 @@ uint32_t general_ampr_gw;
 
 uint32_t myips[MYIPSIZE];
 
+void (*sigterm_defhnd)(int);
 
 void on_alarm(int sig)
 {
@@ -146,6 +148,16 @@ void on_hup(int sig)
     verbose = FALSE;
 }
 
+void on_term(int sig)
+{
+  signal(SIGTERM, SIG_IGN);
+
+  unlink(PIDFILE);
+
+  signal(SIGTERM, sigterm_defhnd);
+  raise(SIGTERM);
+}
+
 uint32_t getip(const char *dev)
 {
     struct ifreq ifr;
@@ -260,6 +272,7 @@ int main(int argc, char**argv)
     struct pollfd *pollfd;
     int i, j, payload, best, mask, bmask;
     Tunnel *tunnel;
+    FILE *pidfile;
 
     char *ip = malloc(MAXPAYLOAD + 18);   /* eth header + tcp/ip frame + checksum */
     char *rcv = malloc(MAXPAYLOAD + 20);  /* ip header + tcp/ip frame */
@@ -343,7 +356,6 @@ int main(int argc, char**argv)
     sa.sa_handler = on_hup;
     sigaction(SIGHUP, &sa, 0);
 
-
     if (FALSE == debug)
     {
 	pid_t fork_res = -1;
@@ -361,6 +373,12 @@ int main(int argc, char**argv)
 	}
     }
 
+    sa.sa_handler = on_term;
+    sigaction(SIGTERM, &sa, 0);
+    pidfile = fopen(PIDFILE, "w");
+    fprintf(pidfile, "%d\n", (int)getpid());
+    fclose(pidfile);
+
     pollfd = malloc(sizeof(struct pollfd) * (numtunnels + 1));
 
     pollfd[0].fd = raw_socket;