Save filter expression as part of agent events and save them
[lttng-tools.git] / src / bin / lttng-sessiond / agent.c
index d7bc1b55c698a8d4e490bb8c06df4128da1b9bb2..4342c94db96adc2955c1070d92cf8d6e81592dd6 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <assert.h>
 #include <urcu/uatomic.h>
 
@@ -493,11 +494,14 @@ error:
 int agent_disable_event(struct agent_event *event,
                enum lttng_domain_type domain)
 {
-       int ret;
+       int ret = LTTNG_OK;
        struct agent_app *app;
        struct lttng_ht_iter iter;
 
        assert(event);
+       if (!event->enabled) {
+               goto end;
+       }
 
        rcu_read_lock();
 
@@ -515,10 +519,10 @@ int agent_disable_event(struct agent_event *event,
        }
 
        event->enabled = 0;
-       ret = LTTNG_OK;
 
 error:
        rcu_read_unlock();
+end:
        return ret;
 }
 
@@ -679,6 +683,8 @@ void agent_add_app(struct agent_app *app)
 
 /*
  * Delete agent application from the global hash table.
+ *
+ * rcu_read_lock() must be held by the caller.
  */
 void agent_delete_app(struct agent_app *app)
 {
@@ -690,9 +696,7 @@ void agent_delete_app(struct agent_app *app)
        DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd);
 
        iter.iter.node = &app->node.node;
-       rcu_read_lock();
        ret = lttng_ht_del(agent_apps_ht_by_sock, &iter);
-       rcu_read_unlock();
        assert(!ret);
 }
 
@@ -780,33 +784,37 @@ error:
 }
 
 /*
- * Create a newly allocated agent event data structure. If name is valid, it's
- * copied into the created event.
+ * Create a newly allocated agent event data structure.
+ * Ownership of filter_expression is taken.
  *
  * Return a new object else NULL on error.
  */
 struct agent_event *agent_create_event(const char *name,
-               struct lttng_filter_bytecode *filter)
+               int loglevel, enum lttng_loglevel_type loglevel_type,
+               struct lttng_filter_bytecode *filter, char *filter_expression)
 {
-       struct agent_event *event;
+       struct agent_event *event = NULL;
 
        DBG3("Agent create new event with name %s", name);
 
-       event = zmalloc(sizeof(*event));
-       if (!event) {
+       if (!name) {
+               ERR("Failed to create agent event; no name provided.");
                goto error;
        }
 
-       if (name) {
-               strncpy(event->name, name, sizeof(event->name));
-               event->name[sizeof(event->name) - 1] = '\0';
-               lttng_ht_node_init_str(&event->node, event->name);
+       event = zmalloc(sizeof(*event));
+       if (!event) {
+               goto error;
        }
 
-       if (filter) {
-               event->filter = filter;
-       }
+       strncpy(event->name, name, sizeof(event->name));
+       event->name[sizeof(event->name) - 1] = '\0';
+       lttng_ht_node_init_str(&event->node, event->name);
 
+       event->loglevel = loglevel;
+       event->loglevel_type = loglevel_type;
+       event->filter = filter;
+       event->filter_expression = filter_expression;
 error:
        return event;
 }
@@ -899,7 +907,7 @@ struct agent_event *agent_find_event(const char *name, int loglevel,
        return caa_container_of(node, struct agent_event, node);
 
 error:
-       DBG3("Agent NOT found %s.", name);
+       DBG3("Agent event NOT found %s.", name);
        return NULL;
 }
 
@@ -912,6 +920,9 @@ void agent_destroy_event(struct agent_event *event)
 {
        assert(event);
 
+       free(event->filter);
+       free(event->filter_expression);
+       free(event->exclusion);
        free(event);
 }
 
@@ -959,16 +970,67 @@ void agent_destroy(struct agent *agt)
 }
 
 /*
- * Initialize agent subsystem.
+ * Allocate agent_apps_ht_by_sock.
  */
-int agent_setup(void)
+int agent_app_ht_alloc(void)
 {
+       int ret = 0;
+
        agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
        if (!agent_apps_ht_by_sock) {
-               return -1;
+               ret = -1;
        }
 
-       return 0;
+       return ret;
+}
+
+/*
+ * Destroy a agent application by socket.
+ */
+void agent_destroy_app_by_sock(int sock)
+{
+       struct agent_app *app;
+
+       assert(sock >= 0);
+
+       /*
+        * Not finding an application is a very important error that should NEVER
+        * happen. The hash table deletion is ONLY done through this call when the
+        * main sessiond thread is torn down.
+        */
+       rcu_read_lock();
+       app = agent_find_app_by_sock(sock);
+       assert(app);
+
+       /* RCU read side lock is assumed to be held by this function. */
+       agent_delete_app(app);
+
+       /* The application is freed in a RCU call but the socket is closed here. */
+       agent_destroy_app(app);
+       rcu_read_unlock();
+}
+
+/*
+ * Clean-up the agent app hash table and destroy it.
+ */
+void agent_app_ht_clean(void)
+{
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+
+       if (!agent_apps_ht_by_sock) {
+               return;
+       }
+       rcu_read_lock();
+       cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
+               struct agent_app *app;
+
+               app = caa_container_of(node, struct agent_app, node);
+               agent_destroy_app_by_sock(app->sock->fd);
+       }
+       rcu_read_unlock();
+
+       lttng_ht_destroy(agent_apps_ht_by_sock);
 }
 
 /*
This page took 0.027133 seconds and 4 git commands to generate.