Do not kill daemon, just not use pluggable kernel interface if initialization failed
authorMartin Willi <martin@revosec.ch>
Tue, 23 Feb 2010 16:49:34 +0000 (16:49 +0000)
committerMartin Willi <martin@revosec.ch>
Fri, 26 Feb 2010 10:44:33 +0000 (11:44 +0100)
src/charon/plugins/kernel_klips/kernel_klips_ipsec.c
src/charon/plugins/kernel_netlink/kernel_netlink_ipsec.c
src/charon/plugins/kernel_netlink/kernel_netlink_net.c
src/charon/plugins/kernel_netlink/kernel_netlink_shared.c
src/charon/plugins/kernel_pfkey/kernel_pfkey_ipsec.c
src/charon/plugins/kernel_pfroute/kernel_pfroute_net.c

index c99b721..01df4f7 100644 (file)
@@ -2566,9 +2566,18 @@ METHOD(kernel_ipsec_t, bypass_socket, bool,
 METHOD(kernel_ipsec_t, destroy, void,
        private_kernel_klips_ipsec_t *this)
 {
-       this->job->cancel(this->job);
-       close(this->socket);
-       close(this->socket_events);
+       if (this->job)
+       {
+               this->job->cancel(this->job);
+       }
+       if (this->socket > 0)
+       {
+               close(this->socket);
+       }
+       if (this->socket_evnets > 0)
+       {
+               close(this->socket_events);
+       }
        this->mutex_pfkey->destroy(this->mutex_pfkey);
        this->mutex->destroy(this->mutex);
        this->ipsec_devices->destroy_function(this->ipsec_devices, (void*)ipsec_dev_destroy);
@@ -2616,21 +2625,27 @@ kernel_klips_ipsec_t *kernel_klips_ipsec_create()
        this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
        if (this->socket <= 0)
        {
-               charon->kill(charon, "unable to create PF_KEY socket");
+               DBG1(DBG_KNL, "unable to create PF_KEY socket");
+               destroy(this);
+               return NULL;
        }
 
        /* create a PF_KEY socket for ACQUIRE & EXPIRE */
        this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
        if (this->socket_events <= 0)
        {
-               charon->kill(charon, "unable to create PF_KEY event socket");
+               DBG1(DBG_KNL, "unable to create PF_KEY event socket");
+               destroy(this);
+               return NULL;
        }
 
        /* register the event socket */
        if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
                register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
        {
-               charon->kill(charon, "unable to register PF_KEY event socket");
+               DBG1(DBG_KNL, "unable to register PF_KEY event socket");
+               destroy(this);
+               return NULL;
        }
 
        this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -2639,3 +2654,4 @@ kernel_klips_ipsec_t *kernel_klips_ipsec_create()
 
        return &this->public;
 }
+
index c8bcfd6..1b8c1b8 100644 (file)
@@ -1939,9 +1939,15 @@ METHOD(kernel_ipsec_t, destroy, void,
        enumerator_t *enumerator;
        policy_entry_t *policy;
 
-       this->job->cancel(this->job);
-       close(this->socket_xfrm_events);
-       this->socket_xfrm->destroy(this->socket_xfrm);
+       if (this->job)
+       {
+               this->job->cancel(this->job);
+       }
+       if (this->socket_xfrm_events > 0)
+       {
+               close(this->socket_xfrm_events);
+       }
+       DESTROY_IF(this->socket_xfrm);
        enumerator = this->policies->create_enumerator(this->policies);
        while (enumerator->enumerate(enumerator, &policy, &policy))
        {
@@ -1992,6 +1998,11 @@ kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
        }
 
        this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
+       if (!this->socket_xfrm)
+       {
+               destroy(this);
+               return NULL;
+       }
 
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
@@ -2000,13 +2011,17 @@ kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
        this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
        if (this->socket_xfrm_events <= 0)
        {
-               charon->kill(charon, "unable to create XFRM event socket");
+               DBG1(DBG_KNL, "unable to create XFRM event socket");
+               destroy(this);
+               return NULL;
        }
        addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
                                         XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
        if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
        {
-               charon->kill(charon, "unable to bind XFRM event socket");
+               DBG1(DBG_KNL, "unable to bind XFRM event socket");
+               destroy(this);
+               return NULL;
        }
        this->job = callback_job_create((callback_job_cb_t)receive_events,
                                                                        this, NULL, NULL);
index 4a9fdf6..e1ba4a8 100644 (file)
@@ -1336,10 +1336,15 @@ static void destroy(private_kernel_netlink_net_t *this)
                manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
                                        this->routing_table_prio);
        }
-
-       this->job->cancel(this->job);
-       close(this->socket_events);
-       this->socket->destroy(this->socket);
+       if (this->job)
+       {
+               this->job->cancel(this->job);
+       }
+       if (this->socket_events > 0)
+       {
+               close(this->socket_events);
+       }
+       DESTROY_IF(this->socket);
        this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
        this->condvar->destroy(this->condvar);
        this->mutex->destroy(this->mutex);
@@ -1380,21 +1385,26 @@ kernel_netlink_net_t *kernel_netlink_net_create()
                                        "charon.install_virtual_ip", TRUE);
 
        this->socket = netlink_socket_create(NETLINK_ROUTE);
+       this->job = NULL;
 
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
 
        /* create and bind RT socket for events (address/interface/route changes) */
        this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
-       if (this->socket_events <= 0)
+       if (this->socket_events < 0)
        {
-               charon->kill(charon, "unable to create RT event socket");
+               DBG1(DBG_KNL, "unable to create RT event socket");
+               destroy(this);
+               return NULL;
        }
        addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
                                         RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
        if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
        {
-               charon->kill(charon, "unable to bind RT event socket");
+               DBG1(DBG_KNL, "unable to bind RT event socket");
+               destroy(this);
+               return NULL;
        }
 
        this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -1403,7 +1413,9 @@ kernel_netlink_net_t *kernel_netlink_net_create()
 
        if (init_address_list(this) != SUCCESS)
        {
-               charon->kill(charon, "unable to get interface list");
+               DBG1(DBG_KNL, "unable to get interface list");
+               destroy(this);
+               return NULL;
        }
 
        if (this->routing_table)
index b96186a..5ed5681 100644 (file)
@@ -236,7 +236,10 @@ static status_t netlink_send_ack(private_netlink_socket_t *this, struct nlmsghdr
  */
 static void destroy(private_netlink_socket_t *this)
 {
-       close(this->socket);
+       if (this->socket > 0)
+       {
+               close(this->socket);
+       }
        this->mutex->destroy(this->mutex);
        free(this);
 }
@@ -244,7 +247,8 @@ static void destroy(private_netlink_socket_t *this)
 /**
  * Described in header.
  */
-netlink_socket_t *netlink_socket_create(int protocol) {
+netlink_socket_t *netlink_socket_create(int protocol)
+{
        private_netlink_socket_t *this = malloc_thing(private_netlink_socket_t);
        struct sockaddr_nl addr;
 
@@ -262,15 +266,19 @@ netlink_socket_t *netlink_socket_create(int protocol) {
 
        this->protocol = protocol;
        this->socket = socket(AF_NETLINK, SOCK_RAW, protocol);
-       if (this->socket <= 0)
+       if (this->socket < 0)
        {
-               charon->kill(charon, "unable to create netlink socket");
+               DBG1(DBG_KNL, "unable to create netlink socket");
+               destroy(this);
+               return NULL;
        }
 
        addr.nl_groups = 0;
        if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)))
        {
-               charon->kill(charon, "unable to bind netlink socket");
+               DBG1(DBG_KNL, "unable to bind netlink socket");
+               destroy(this);
+               return NULL;
        }
 
        return &this->public;
index b64e41c..8a7883c 100644 (file)
@@ -2093,9 +2093,18 @@ METHOD(kernel_ipsec_t, bypass_socket, bool,
 METHOD(kernel_ipsec_t, destroy, void,
        private_kernel_pfkey_ipsec_t *this)
 {
-       this->job->cancel(this->job);
-       close(this->socket);
-       close(this->socket_events);
+       if (this->job)
+       {
+               this->job->cancel(this->job);
+       }
+       if (this->socket > 0)
+       {
+               close(this->socket);
+       }
+       if (this->socket_events > 0)
+       {
+               close(this->socket_events);
+       }
        this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
        this->mutex->destroy(this->mutex);
        this->mutex_pfkey->destroy(this->mutex_pfkey);
@@ -2134,21 +2143,27 @@ kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
        this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
        if (this->socket <= 0)
        {
-               charon->kill(charon, "unable to create PF_KEY socket");
+               DBG1(DBG_KNL, "unable to create PF_KEY socket");
+               destroy(this);
+               return NULL;
        }
 
        /* create a PF_KEY socket for ACQUIRE & EXPIRE */
        this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
        if (this->socket_events <= 0)
        {
-               charon->kill(charon, "unable to create PF_KEY event socket");
+               DBG1(DBG_KNL, "unable to create PF_KEY event socket");
+               destroy(this);
+               return NULL;
        }
 
        /* register the event socket */
        if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
                register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
        {
-               charon->kill(charon, "unable to register PF_KEY event socket");
+               DBG1(DBG_KNL, "unable to register PF_KEY event socket");
+               destroy(this);
+               return NULL;
        }
 
        this->job = callback_job_create((callback_job_cb_t)receive_events,
index 9f1baf5..97c019b 100644 (file)
@@ -650,9 +650,18 @@ static status_t init_address_list(private_kernel_pfroute_net_t *this)
  */
 static void destroy(private_kernel_pfroute_net_t *this)
 {
-       this->job->cancel(this->job);
-       close(this->socket);
-       close(this->socket_events);
+       if (this->job)
+       {
+               this->job->cancel(this->job);
+       }
+       if (this->socket > 0)
+       {
+               close(this->socket);
+       }
+       if (this->socket_events)
+       {
+               close(this->socket_events);
+       }
        this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
        this->mutex->destroy(this->mutex);
        this->mutex_pfroute->destroy(this->mutex_pfroute);
@@ -684,19 +693,25 @@ kernel_pfroute_net_t *kernel_pfroute_net_create()
        this->mutex_pfroute = mutex_create(MUTEX_TYPE_DEFAULT);
 
        this->seq = 0;
+       this->socket_events = 0;
+       this->job = NULL;
 
        /* create a PF_ROUTE socket to communicate with the kernel */
        this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
-       if (this->socket <= 0)
+       if (this->socket < 0)
        {
-               charon->kill(charon, "unable to create PF_ROUTE socket");
+               DBG1(DBG_KNL, "unable to create PF_ROUTE socket");
+               destroy(this);
+               return NULL;
        }
 
        /* create a PF_ROUTE socket to receive events */
        this->socket_events = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
-       if (this->socket_events <= 0)
+       if (this->socket_events < 0)
        {
-               charon->kill(charon, "unable to create PF_ROUTE event socket");
+               DBG1(DBG_KNL, "unable to create PF_ROUTE event socket");
+               destroy(this);
+               return NULL;
        }
 
        this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -705,7 +720,9 @@ kernel_pfroute_net_t *kernel_pfroute_net_create()
 
        if (init_address_list(this) != SUCCESS)
        {
-               charon->kill(charon, "unable to get interface list");
+               DBG1(DBG_KNL, "unable to get interface list");
+               destroy(this);
+               return NULL;
        }
 
        return &this->public;