[PATCH] openggsn[master]: Signal handler for SIGTERM, SIGHUP and SIGINT added to SGSNE...

Jovan Bunjevacki gerrit-no-reply at lists.osmocom.org
Fri Feb 24 16:41:02 UTC 2017


Review at  https://gerrit.osmocom.org/1902

Signal handler for SIGTERM, SIGHUP and SIGINT added to SGSNEMU to properly delete PDP context and gracefully exit in case of interrupt.

Change-Id: I546dc0a8d814527b9a96943a52f2f6b700285167
---
M sgsnemu/sgsnemu.c
1 file changed, 32 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openggsn refs/changes/02/1902/1

diff --git a/sgsnemu/sgsnemu.c b/sgsnemu/sgsnemu.c
index a4d6461..68e8521 100644
--- a/sgsnemu/sgsnemu.c
+++ b/sgsnemu/sgsnemu.c
@@ -70,7 +70,7 @@
 /* 3: Done                       */
 /* 4: Wait_disconnect            */
 /* 5: Disconnected               */
-int state = 0;
+volatile sig_atomic_t state = 0;
 
 struct gsn_t *gsn = NULL;	/* GSN instance */
 struct tun_t *tun = NULL;	/* TUN instance */
@@ -153,6 +153,11 @@
 int tsum = 0;
 int pingseq = 0;		/* Ping sequence counter */
 struct timeval firstping;
+
+void signal_handler(int signo)
+{
+	if(state == 2) state = 3;  /* Tell main loop to finish. */
+}
 
 int ipset(struct iphash_t *ipaddr, struct in_addr *addr)
 {
@@ -1417,10 +1422,15 @@
 	int starttime = time(NULL);	/* Time program was started */
 	int stoptime = 0;	/* Time to exit */
 	int pingtimeout = 0;	/* Time to print ping statistics */
+	int signal_received;	/* If select() on fd_set is interrupted by signal. */
 
 	struct timezone tz;	/* Used for calculating ping times */
 	struct timeval tv;
 	int diff;
+
+	signal(SIGTERM, signal_handler);
+	signal(SIGHUP,  signal_handler);
+	signal(SIGINT,  signal_handler);
 
 	osmo_init_logging(&log_info);
 
@@ -1676,10 +1686,13 @@
 			printf("idletime.tv_sec %d, idleTime.tv_usec %d\n",
 			       (int)idleTime.tv_sec, (int)idleTime.tv_usec);
 
+		signal_received=0;
 		switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
 		case -1:
-			SYS_ERR(DSGSN, LOGL_ERROR, 0,
-				"Select returned -1");
+			if(errno == EINTR) signal_received=1;
+			else 
+				SYS_ERR(DSGSN, LOGL_ERROR, 0,
+					"Select returned -1");
 			break;
 		case 0:
 			gtp_retrans(gsn);	/* Only retransmit if nothing else */
@@ -1688,19 +1701,23 @@
 			break;
 		}
 
-		if ((tun) && FD_ISSET(tun->fd, &fds) && tun_decaps(tun) < 0) {
-			SYS_ERR(DSGSN, LOGL_ERROR, 0,
-				"TUN decaps failed");
+		if(!signal_received) {
+
+			if ((tun) && FD_ISSET(tun->fd, &fds) && tun_decaps(tun) < 0) {
+				SYS_ERR(DSGSN, LOGL_ERROR, 0,
+					"TUN decaps failed");
+			}
+
+			if (FD_ISSET(gsn->fd0, &fds))
+				gtp_decaps0(gsn);
+
+			if (FD_ISSET(gsn->fd1c, &fds))
+				gtp_decaps1c(gsn);
+
+			if (FD_ISSET(gsn->fd1u, &fds))
+				gtp_decaps1u(gsn);
+
 		}
-
-		if (FD_ISSET(gsn->fd0, &fds))
-			gtp_decaps0(gsn);
-
-		if (FD_ISSET(gsn->fd1c, &fds))
-			gtp_decaps1c(gsn);
-
-		if (FD_ISSET(gsn->fd1u, &fds))
-			gtp_decaps1u(gsn);
 	}
 
 	gtp_free(gsn);		/* Clean up the gsn instance */

-- 
To view, visit https://gerrit.osmocom.org/1902
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I546dc0a8d814527b9a96943a52f2f6b700285167
Gerrit-PatchSet: 1
Gerrit-Project: openggsn
Gerrit-Branch: master
Gerrit-Owner: Jovan Bunjevacki <j.bunjevacki at vipmobile.rs>


More information about the gerrit-log mailing list