Making One Exit Point
diff --git a/nlsr.c b/nlsr.c
index c896fb3..b2b7eb9 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -30,6 +30,20 @@
#include "nlsr_route.h"
+#define ON_ERROR_DESTROY(resval) \
+{ \
+ if ((resval) < 0) { \
+ nlsr_destroy(); \
+ } \
+}
+
+
+#define ON_ERROR_EXIT(resval) \
+{ \
+ if ((resval) < 0) { \
+ exit(-1); \
+ } \
+}
struct option longopts[] =
{
@@ -73,7 +87,6 @@
{
signal(sig, SIG_IGN);
nlsr_destroy();
- //exit(0);
}
void
@@ -382,23 +395,23 @@
}
-void
+int
init_nlsr(void)
{
if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
{
perror("SIGQUIT install error\n");
- exit(1);
+ return -1;
}
if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
{
perror("SIGTERM install error\n");
- exit(1);
+ return -1;
}
if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
{
perror("SIGTERM install error\n");
- exit(1);
+ return -1;
}
nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
@@ -447,18 +460,21 @@
nlsr->interest_resend_time = INTEREST_RESEND_TIME;
nlsr->semaphor=0;
+
+ return 0;
}
int
main(int argc, char *argv[])
{
- int res;
+ int res, ret;
char *config_file;
int daemon_mode;
- init_nlsr();
-
+ ret=init_nlsr();
+ ON_ERROR_EXIT(ret);
+
while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
{
switch (res)
@@ -481,7 +497,7 @@
if(ccn_connect(nlsr->ccn, NULL) == -1)
{
fprintf(stderr,"Could not connect to ccnd\n");
- exit(1);
+ ON_ERROR_DESTROY(-1);
}
struct ccn_charbuf *router_prefix;
router_prefix=ccn_charbuf_create();
@@ -489,7 +505,7 @@
if(res<0)
{
fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
- exit(1);
+ ON_ERROR_DESTROY(res);
}
ccn_name_append_str(router_prefix,"nlsr");
@@ -498,7 +514,7 @@
if ( res < 0 )
{
fprintf(stderr,"Failed to register interest for router\n");
- exit(1);
+ ON_ERROR_DESTROY(res);
}
ccn_charbuf_destroy(&router_prefix);
diff --git a/nlsr.h b/nlsr.h
index 1bfa8a1..1bfc5b6 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -79,7 +79,7 @@
void process_conf_command(char *command);
int readConfigFile(const char *filename);
-void init_nlsr(void);
+int init_nlsr(void);
void nlsr_destroy( void );
void nlsr_stop_signal_handler(int sig);