summaryrefslogtreecommitdiff
path: root/src/ipcpd/ipcp.c
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2022-07-08 16:55:51 +0200
committerSander Vrijders <sander@ouroboros.rocks>2022-07-13 08:56:02 +0200
commit763227aee4a3d86e718c3665fe5fa13055f67f03 (patch)
tree837a603731335d950e45e7bc2aada9d2448d7a6b /src/ipcpd/ipcp.c
parentb9d674dc5a63b0206d96ff67366ed84c6d5d2962 (diff)
downloadouroboros-763227aee4a3d86e718c3665fe5fa13055f67f03.tar.gz
ouroboros-763227aee4a3d86e718c3665fe5fa13055f67f03.zip
ipcpd: Refactor main functions
The structure of main functions of the IPCPs was a bit strange with a ipcp_shutdown() call that combined waiting for a terminating signal with stopping the internal threads. This is now revised into a symmetrical design of ipcp_start(), which now includes the create response towards the IRMd. ipcp_sigwait(), which waits for a shutdown signal ipcp_stop() that then stops the internal threads. Now the main() functions of the IPCPs will make sense without checking what that ipcp_shutdown() functions actually does. Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks> Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
Diffstat (limited to 'src/ipcpd/ipcp.c')
-rw-r--r--src/ipcpd/ipcp.c57
1 files changed, 37 insertions, 20 deletions
diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c
index eefa72af..395e9b78 100644
--- a/src/ipcpd/ipcp.c
+++ b/src/ipcpd/ipcp.c
@@ -43,6 +43,7 @@
#include <ouroboros/sockets.h>
#include <ouroboros/errno.h>
#include <ouroboros/dev.h>
+#include <ouroboros/ipcp-dev.h>
#include <ouroboros/bitmap.h>
#include <ouroboros/np1_flow.h>
#include <ouroboros/rib.h>
@@ -624,7 +625,7 @@ static void * mainloop(void * o)
default:
ret_msg.has_result = true;
ret_msg.result = -1;
- log_err("Don't know that message code");
+ log_err("Unknown message code: %d.", msg->code);
break;
}
exit:
@@ -764,6 +765,15 @@ int ipcp_init(int argc,
goto fail_rib_init;
}
+ if (rib_reg(IPCP_INFO, &r_ops))
+ goto fail_rib_reg;
+
+ ipcpi.tpm = tpm_create(IPCP_MIN_THREADS, IPCP_ADD_THREADS,
+ mainloop, NULL);
+ if (ipcpi.tpm == NULL)
+ goto fail_tpm_create;
+
+
list_head_init(&ipcpi.cmds);
ipcpi.alloc_id = -1;
@@ -772,6 +782,10 @@ int ipcp_init(int argc,
return 0;
+ fail_tpm_create:
+ rib_unreg(IPCP_INFO);
+ fail_rib_reg:
+ rib_fini();
fail_rib_init:
pthread_cond_destroy(&ipcpi.cmd_cond);
fail_cmd_cond:
@@ -794,7 +808,7 @@ int ipcp_init(int argc,
return ret;
}
-int ipcp_boot()
+int ipcp_start()
{
sigset_t sigset;
sigemptyset(&sigset);
@@ -803,11 +817,6 @@ int ipcp_boot()
sigaddset(&sigset, SIGHUP);
sigaddset(&sigset, SIGPIPE);
- ipcpi.tpm = tpm_create(IPCP_MIN_THREADS, IPCP_ADD_THREADS,
- mainloop, NULL);
- if (ipcpi.tpm == NULL)
- goto fail_tpm_create;
-
pthread_sigmask(SIG_BLOCK, &sigset, NULL);
if (tpm_start(ipcpi.tpm))
@@ -815,29 +824,31 @@ int ipcp_boot()
ipcp_set_state(IPCP_INIT);
- if (rib_reg(IPCP_INFO, &r_ops))
- goto fail_rib_reg;
-
if (pthread_create(&ipcpi.acceptor, NULL, acceptloop, NULL)) {
log_err("Failed to create acceptor thread.");
- ipcp_set_state(IPCP_NULL);
goto fail_acceptor;
}
- return 0;
+ if (ipcp_create_r(0)) {
+ log_err("Failed to notify IRMd we are initialized.");
+ goto fail_create_r;
+ }
+ return 0;
+ fail_create_r:
+ pthread_cancel(ipcpi.acceptor);
+ pthread_join(ipcpi.acceptor, NULL);
fail_acceptor:
- rib_unreg(IPCP_INFO);
- fail_rib_reg:
+ ipcp_set_state(IPCP_NULL);
tpm_stop(ipcpi.tpm);
fail_tpm_start:
tpm_destroy(ipcpi.tpm);
- fail_tpm_create:
+ ipcp_create_r(-1);
return -1;
}
-void ipcp_shutdown()
+void ipcp_sigwait()
{
siginfo_t info;
@@ -891,19 +902,25 @@ void ipcp_shutdown()
continue;
}
}
+}
- pthread_cancel(ipcpi.acceptor);
+void ipcp_stop()
+{
+ log_info("IPCP %d shutting down.", getpid());
+ pthread_cancel(ipcpi.acceptor);
pthread_join(ipcpi.acceptor, NULL);
- tpm_stop(ipcpi.tpm);
- tpm_destroy(ipcpi.tpm);
- log_info("IPCP %d shutting down.", getpid());
+ tpm_stop(ipcpi.tpm);
}
void ipcp_fini()
{
+ tpm_destroy(ipcpi.tpm);
+
+ rib_unreg(IPCP_INFO);
+
rib_fini();
close(ipcpi.sockfd);