[OpenWrt-Devel] [PATCH 3/4] netifd: Make possible state transitions more clear when handling teardown event

Hans Dedecker dedeckeh at gmail.com
Wed Dec 3 05:15:19 EST 2014


Improves code readibility regarding state transitions when handling teardown event

Signed-off-by: Hans Dedecker <dedeckeh at gmail.com>
---
 proto-shell.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/proto-shell.c b/proto-shell.c
index 86933b0..977cdbc 100644
--- a/proto-shell.c
+++ b/proto-shell.c
@@ -184,28 +184,35 @@ proto_shell_handler(struct interface_proto_state *proto,
 		state->renew_pending = false;
 		action = "renew";
 	} else {
-		if (state->sm == S_TEARDOWN)
-			return 0;
-
-		state->renew_pending = false;
-		if (state->script_task.uloop.pending) {
-			if (state->sm != S_SETUP_ABORT) {
+		switch (state->sm) {
+		case S_SETUP:
+			if (state->script_task.uloop.pending) {
 				uloop_timeout_set(&state->teardown_timeout, 1000);
 				kill(state->script_task.uloop.pid, SIGTERM);
 				if (state->proto_task.uloop.pending)
 					kill(state->proto_task.uloop.pid, SIGTERM);
+				state->renew_pending = false;
 				state->sm = S_SETUP_ABORT;
+				return 0;
+			}
+		/* fall through if no script task is running */
+		case S_IDLE:
+			action = "teardown";
+			state->renew_pending = false;
+			state->sm = S_TEARDOWN;
+			if (state->last_error >= 0) {
+				snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
+				envp[j++] = error_buf;
 			}
+			uloop_timeout_set(&state->teardown_timeout, 5000);
+			break;
+
+		case S_TEARDOWN:
 			return 0;
-		}
 
-		action = "teardown";
-		state->sm = S_TEARDOWN;
-		if (state->last_error >= 0) {
-			snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
-			envp[j++] = error_buf;
+		default:
+			return -1;
 		}
-		uloop_timeout_set(&state->teardown_timeout, 5000);
 	}
 
 	D(INTERFACE, "run %s for interface '%s'\n", action, proto->iface->name);
-- 
1.9.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list