capabilities: Some plugins don't actually require capabilities at runtime
[strongswan.git] / src / libcharon / plugins / load_tester / load_tester_plugin.c
index 38148fc..7f2d425 100644 (file)
@@ -18,6 +18,7 @@
 #include "load_tester_creds.h"
 #include "load_tester_ipsec.h"
 #include "load_tester_listener.h"
+#include "load_tester_control.h"
 #include "load_tester_diffie_hellman.h"
 
 #include <unistd.h>
@@ -51,6 +52,11 @@ struct private_load_tester_plugin_t {
        load_tester_creds_t *creds;
 
        /**
+        * Unix control socket to initiate load-tests
+        */
+       load_tester_control_t *control;
+
+       /**
         * event handler, listens on bus
         */
        load_tester_listener_t *listener;
@@ -181,6 +187,7 @@ static bool register_load_tester(private_load_tester_plugin_t *this,
 
                this->config = load_tester_config_create();
                this->creds = load_tester_creds_create();
+               this->control = load_tester_control_create();
 
                charon->backends->add_backend(charon->backends, &this->config->backend);
                lib->credmgr->add_set(lib->credmgr, &this->creds->credential_set);
@@ -190,15 +197,9 @@ static bool register_load_tester(private_load_tester_plugin_t *this,
                {
                        shutdown_on = this->iterations * this->initiators;
                }
-               this->listener = load_tester_listener_create(shutdown_on);
+               this->listener = load_tester_listener_create(shutdown_on, this->config);
                charon->bus->add_listener(charon->bus, &this->listener->listener);
 
-               if (lib->settings->get_bool(lib->settings,
-                               "%s.plugins.load-tester.fake_kernel", FALSE, charon->name))
-               {
-                       hydra->kernel_interface->add_ipsec_interface(hydra->kernel_interface,
-                                                       (kernel_ipsec_constructor_t)load_tester_ipsec_create);
-               }
                for (i = 0; i < this->initiators; i++)
                {
                        lib->processor->queue_job(lib->processor, (job_t*)
@@ -215,14 +216,13 @@ static bool register_load_tester(private_load_tester_plugin_t *this,
                        this->condvar->wait(this->condvar, this->mutex);
                }
                this->mutex->unlock(this->mutex);
-               hydra->kernel_interface->remove_ipsec_interface(hydra->kernel_interface,
-                                                       (kernel_ipsec_constructor_t)load_tester_ipsec_create);
                charon->backends->remove_backend(charon->backends, &this->config->backend);
                lib->credmgr->remove_set(lib->credmgr, &this->creds->credential_set);
                charon->bus->remove_listener(charon->bus, &this->listener->listener);
                this->config->destroy(this->config);
                this->creds->destroy(this->creds);
                this->listener->destroy(this->listener);
+               this->control->destroy(this->control);
        }
        return TRUE;
 }
@@ -236,6 +236,7 @@ METHOD(plugin_t, get_features, int,
                                PLUGIN_DEPENDS(CUSTOM, "load-tester"),
                PLUGIN_CALLBACK((plugin_feature_callback_t)register_load_tester, NULL),
                        PLUGIN_PROVIDE(CUSTOM, "load-tester"),
+                               PLUGIN_DEPENDS(CUSTOM, "kernel-net"),
                                PLUGIN_SDEPEND(PRIVKEY, KEY_RSA),
                                PLUGIN_SDEPEND(CERT_DECODE, CERT_ANY),
                                PLUGIN_SDEPEND(CERT_DECODE, CERT_X509),
@@ -247,6 +248,8 @@ METHOD(plugin_t, get_features, int,
 METHOD(plugin_t, destroy, void,
        private_load_tester_plugin_t *this)
 {
+       hydra->kernel_interface->remove_ipsec_interface(hydra->kernel_interface,
+                                               (kernel_ipsec_constructor_t)load_tester_ipsec_create);
        this->mutex->destroy(this->mutex);
        this->condvar->destroy(this->condvar);
        free(this);
@@ -266,6 +269,12 @@ plugin_t *load_tester_plugin_create()
                return NULL;
        }
 
+       if (!lib->caps->check(lib->caps, CAP_CHOWN))
+       {       /* required to chown(2) control socket */
+               DBG1(DBG_CFG, "load-tester plugin requires CAP_CHOWN capability");
+               return NULL;
+       }
+
        INIT(this,
                .public = {
                        .plugin = {
@@ -286,6 +295,13 @@ plugin_t *load_tester_plugin_create()
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
                .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
        );
+
+       if (lib->settings->get_bool(lib->settings,
+                       "%s.plugins.load-tester.fake_kernel", FALSE, charon->name))
+       {
+               hydra->kernel_interface->add_ipsec_interface(hydra->kernel_interface,
+                                               (kernel_ipsec_constructor_t)load_tester_ipsec_create);
+       }
        return &this->public.plugin;
 }