Test: Ensure wildcards may only appear last in a string literal
[lttng-tools.git] / src / bin / lttng-sessiond / jul.c
index 4f2250dff62588baf9828edd4205de896ae8e0b2..bf4669b239053f470807fb7110d8a5a230b6efd7 100644 (file)
 #include <common/common.h>
 #include <common/sessiond-comm/jul.h>
 
+#include <common/compat/endian.h>
+
 #include "jul.h"
 #include "ust-app.h"
 #include "utils.h"
 
 /*
- * URCU intermediate call to complete destroy a JUL event.
+ * Match function for the events hash table lookup by name.
+ */
+static int ht_match_event_by_name(struct cds_lfht_node *node,
+               const void *_key)
+{
+       struct jul_event *event;
+       const struct jul_ht_key *key;
+
+       assert(node);
+       assert(_key);
+
+       event = caa_container_of(node, struct jul_event, node.node);
+       key = _key;
+
+       /* Match 1 elements of the key: name. */
+
+       /* Event name */
+       if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
+               goto no_match;
+       }
+       /* Match. */
+       return 1;
+
+no_match:
+       return 0;
+}
+
+/*
+ * Match function for the events hash table lookup by name and loglevel.
+ */
+static int ht_match_event(struct cds_lfht_node *node,
+               const void *_key)
+{
+       struct jul_event *event;
+       const struct jul_ht_key *key;
+
+       assert(node);
+       assert(_key);
+
+       event = caa_container_of(node, struct jul_event, node.node);
+       key = _key;
+
+       /* Match 2 elements of the key: name and loglevel. */
+
+       /* Event name */
+       if (strncmp(event->name, key->name, sizeof(event->name)) != 0) {
+               goto no_match;
+       }
+
+       if (event->loglevel != key->loglevel) {
+               if (event->loglevel_type == LTTNG_EVENT_LOGLEVEL_ALL &&
+                               key->loglevel == 0 && event->loglevel == -1) {
+                       goto match;
+               }
+               goto no_match;
+       }
+match:
+       return 1;
+
+no_match:
+       return 0;
+}
+
+/*
+ * Add unique JUL event based on the event name and loglevel.
+ */
+static void add_unique_jul_event(struct lttng_ht *ht, struct jul_event *event)
+{
+       struct cds_lfht_node *node_ptr;
+       struct jul_ht_key key;
+
+       assert(ht);
+       assert(ht->ht);
+       assert(event);
+
+       key.name = event->name;
+       key.loglevel = event->loglevel;
+
+       node_ptr = cds_lfht_add_unique(ht->ht,
+                       ht->hash_fct(event->node.key, lttng_ht_seed),
+                       ht_match_event, &key, &event->node.node);
+       assert(node_ptr == &event->node.node);
+}
+
+/*
+ * URCU delayed JUL event reclaim.
  */
 static void destroy_event_jul_rcu(struct rcu_head *head)
 {
@@ -40,7 +127,7 @@ static void destroy_event_jul_rcu(struct rcu_head *head)
 }
 
 /*
- * URCU intermediate call to complete destroy a JUL event.
+ * URCU delayed JUL app reclaim.
  */
 static void destroy_app_jul_rcu(struct rcu_head *head)
 {
@@ -53,8 +140,8 @@ static void destroy_app_jul_rcu(struct rcu_head *head)
 }
 
 /*
- * Communication with Java agent call. Send the message header to the given
- * socket all in big endian.
+ * Communication with Java agent. Send the message header to the given
+ * socket in big endian.
  *
  * Return 0 on success or else a negative errno message of sendmsg() op.
  */
@@ -67,6 +154,7 @@ static int send_header(struct lttcomm_sock *sock, uint64_t data_size,
 
        assert(sock);
 
+       memset(&msg, 0, sizeof(msg));
        msg.data_size = htobe64(data_size);
        msg.cmd = htobe32(cmd);
        msg.cmd_version = htobe32(cmd_version);
@@ -135,9 +223,11 @@ error:
 
 
 /*
- * Internal call to list events on a given app. Populate events.
+ * Internal event listing for a given app. Populate events.
  *
  * Return number of element in the list or else a negative LTTNG_ERR* code.
+ * On success, the caller is responsible for freeing the memory
+ * allocated for "events".
  */
 static ssize_t list_events(struct jul_app *app, struct lttng_event **events)
 {
@@ -220,7 +310,7 @@ error:
 }
 
 /*
- * Internal enable JUL event call on a JUL application. This function
+ * Internal enable JUL event on a JUL application. This function
  * communicates with the Java agent to enable a given event (Logger name).
  *
  * Return LTTNG_OK on success or else a LTTNG_ERR* code.
@@ -246,6 +336,9 @@ static int enable_event(struct jul_app *app, struct jul_event *event)
                goto error_io;
        }
 
+       memset(&msg, 0, sizeof(msg));
+       msg.loglevel = event->loglevel;
+       msg.loglevel_type = event->loglevel_type;
        strncpy(msg.name, event->name, sizeof(msg.name));
        ret = send_payload(app->sock, &msg, sizeof(msg));
        if (ret < 0) {
@@ -305,6 +398,7 @@ static int disable_event(struct jul_app *app, struct jul_event *event)
                goto error_io;
        }
 
+       memset(&msg, 0, sizeof(msg));
        strncpy(msg.name, event->name, sizeof(msg.name));
        ret = send_payload(app->sock, &msg, sizeof(msg));
        if (ret < 0) {
@@ -337,6 +431,21 @@ error:
        return ret;
 }
 
+/*
+ * Send back the registration DONE command to a given JUL application.
+ *
+ * Return 0 on success or else a negative value.
+ */
+int jul_send_registration_done(struct jul_app *app)
+{
+       assert(app);
+       assert(app->sock);
+
+       DBG("JUL sending registration done to app socket %d", app->sock->fd);
+
+       return send_header(app->sock, 0, JUL_CMD_REG_DONE, 0);
+}
+
 /*
  * Enable JUL event on every JUL applications registered with the session
  * daemon.
@@ -414,7 +523,7 @@ int jul_list_events(struct lttng_event **events)
        int ret;
        size_t nbmem, count = 0;
        struct jul_app *app;
-       struct lttng_event *tmp_events;
+       struct lttng_event *tmp_events = NULL;
        struct lttng_ht_iter iter;
 
        assert(events);
@@ -436,29 +545,33 @@ int jul_list_events(struct lttng_event **events)
                nb_ev = list_events(app, &jul_events);
                if (nb_ev < 0) {
                        ret = nb_ev;
-                       rcu_read_unlock();
-                       goto error;
+                       goto error_unlock;
                }
 
-               if (count >= nbmem) {
+               if (count + nb_ev > nbmem) {
                        /* In case the realloc fails, we free the memory */
-                       void *ptr;
-
-                       DBG2("Reallocating JUL event list from %zu to %zu entries", nbmem,
-                                       2 * nbmem);
-                       nbmem *= 2;
-                       ptr = realloc(tmp_events, nbmem * sizeof(*tmp_events));
-                       if (!ptr) {
+                       struct lttng_event *new_tmp_events;
+                       size_t new_nbmem;
+
+                       new_nbmem = max_t(size_t, count + nb_ev, nbmem << 1);
+                       DBG2("Reallocating JUL event list from %zu to %zu entries",
+                                       nbmem, new_nbmem);
+                       new_tmp_events = realloc(tmp_events,
+                               new_nbmem * sizeof(*new_tmp_events));
+                       if (!new_tmp_events) {
                                PERROR("realloc JUL events");
-                               free(tmp_events);
                                ret = -ENOMEM;
-                               rcu_read_unlock();
-                               goto error;
+                               free(jul_events);
+                               goto error_unlock;
                        }
-                       tmp_events = ptr;
+                       /* Zero the new memory */
+                       memset(new_tmp_events + nbmem, 0,
+                               (new_nbmem - nbmem) * sizeof(*new_tmp_events));
+                       nbmem = new_nbmem;
+                       tmp_events = new_tmp_events;
                }
-               memcpy(tmp_events + (count * sizeof(*tmp_events)), jul_events,
-                               nb_ev * sizeof(*tmp_events));
+               memcpy(tmp_events + count, jul_events,
+                       nb_ev * sizeof(*tmp_events));
                free(jul_events);
                count += nb_ev;
        }
@@ -466,8 +579,12 @@ int jul_list_events(struct lttng_event **events)
 
        ret = count;
        *events = tmp_events;
+       return ret;
 
+error_unlock:
+       rcu_read_unlock();
 error:
+       free(tmp_events);
        return ret;
 }
 
@@ -490,8 +607,6 @@ struct jul_app *jul_create_app(pid_t pid, struct lttcomm_sock *sock)
 
        app->pid = pid;
        app->sock = sock;
-       /* Flag it invalid until assignation. */
-       app->ust_app_sock = -1;
        lttng_ht_node_init_ulong(&app->node, (unsigned long) app->sock->fd);
 
 error:
@@ -542,59 +657,6 @@ void jul_add_app(struct jul_app *app)
        rcu_read_unlock();
 }
 
-/*
- * Attach a given JUL application to an UST app object. This is done by copying
- * the socket fd value into the ust app obj. atomically.
- */
-void jul_attach_app(struct jul_app *japp)
-{
-       struct ust_app *uapp;
-
-       assert(japp);
-
-       rcu_read_lock();
-       uapp = ust_app_find_by_pid(japp->pid);
-       if (!uapp) {
-               goto end;
-       }
-
-       uatomic_set(&uapp->jul_app_sock, japp->sock->fd);
-
-       DBG3("JUL app pid: %d, sock: %d attached to UST app.", japp->pid,
-                       japp->sock->fd);
-
-end:
-       rcu_read_unlock();
-       return;
-}
-
-/*
- * Remove JUL app. reference from an UST app object and set it to NULL.
- */
-void jul_detach_app(struct jul_app *japp)
-{
-       struct ust_app *uapp;
-
-       assert(japp);
-
-       rcu_read_lock();
-
-       if (japp->ust_app_sock < 0) {
-               goto end;
-       }
-
-       uapp = ust_app_find_by_sock(japp->ust_app_sock);
-       if (!uapp) {
-               goto end;
-       }
-
-       uapp->jul_app_sock = -1;
-
-end:
-       rcu_read_unlock();
-       return;
-}
-
 /*
  * Delete JUL application from the global hash table.
  */
@@ -616,7 +678,8 @@ void jul_delete_app(struct jul_app *app)
 
 /*
  * Destroy a JUL application object by detaching it from its corresponding UST
- * app if one, closing the socket and freeing the memory.
+ * app if one is connected by closing the socket. Finally, perform a
+ * delayed memory reclaim.
  */
 void jul_destroy_app(struct jul_app *app)
 {
@@ -659,7 +722,8 @@ error:
  *
  * Return a new object else NULL on error.
  */
-struct jul_event *jul_create_event(const char *name)
+struct jul_event *jul_create_event(const char *name,
+               struct lttng_filter_bytecode *filter)
 {
        struct jul_event *event;
 
@@ -676,6 +740,10 @@ struct jul_event *jul_create_event(const char *name)
                lttng_ht_node_init_str(&event->node, event->name);
        }
 
+       if (filter) {
+               event->filter = filter;
+       }
+
 error:
        return event;
 }
@@ -692,64 +760,90 @@ void jul_add_event(struct jul_event *event, struct jul_domain *dom)
        DBG3("JUL adding event %s to domain", event->name);
 
        rcu_read_lock();
-       lttng_ht_add_unique_str(dom->events, &event->node);
+       add_unique_jul_event(dom->events, event);
        rcu_read_unlock();
        dom->being_used = 1;
 }
 
 /*
- * Find a JUL event in the given domain using name.
+ * Find a JUL event in the given domain using name and loglevel.
  *
  * RCU read side lock MUST be acquired.
  *
  * Return object if found else NULL.
  */
-struct jul_event *jul_find_by_name(const char *name, struct jul_domain *dom)
+struct jul_event *jul_find_event_by_name(const char *name,
+               struct jul_domain *dom)
 {
        struct lttng_ht_node_str *node;
        struct lttng_ht_iter iter;
+       struct lttng_ht *ht;
+       struct jul_ht_key key;
 
        assert(name);
        assert(dom);
        assert(dom->events);
 
-       lttng_ht_lookup(dom->events, (void *)name, &iter);
+       ht = dom->events;
+       key.name = name;
+
+       cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
+                       ht_match_event_by_name, &key, &iter.iter);
        node = lttng_ht_iter_get_node_str(&iter);
        if (node == NULL) {
                goto error;
        }
 
-       DBG3("JUL found by name %s in domain.", name);
+       DBG3("JUL event found %s by name.", name);
        return caa_container_of(node, struct jul_event, node);
 
 error:
-       DBG3("JUL NOT found by name %s in domain.", name);
+       DBG3("JUL NOT found by name %s.", name);
        return NULL;
 }
 
 /*
- * Delete JUL event from given domain. Events hash table MUST be initialized.
+ * Find a JUL event in the given domain using name and loglevel.
+ *
+ * RCU read side lock MUST be acquired.
+ *
+ * Return object if found else NULL.
  */
-void jul_delete_event(struct jul_event *event, struct jul_domain *dom)
+struct jul_event *jul_find_event(const char *name,
+               enum lttng_loglevel_jul loglevel, struct jul_domain *dom)
 {
-       int ret;
+       struct lttng_ht_node_str *node;
        struct lttng_ht_iter iter;
+       struct lttng_ht *ht;
+       struct jul_ht_key key;
 
-       assert(event);
+       assert(name);
        assert(dom);
        assert(dom->events);
 
-       DBG3("JUL deleting event %s from domain", event->name);
+       ht = dom->events;
+       key.name = name;
+       key.loglevel = loglevel;
 
-       iter.iter.node = &event->node.node;
-       rcu_read_lock();
-       ret = lttng_ht_del(dom->events, &iter);
-       rcu_read_unlock();
-       assert(!ret);
+       cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
+                       ht_match_event, &key, &iter.iter);
+       node = lttng_ht_iter_get_node_str(&iter);
+       if (node == NULL) {
+               goto error;
+       }
+
+       DBG3("JUL event found %s.", name);
+       return caa_container_of(node, struct jul_event, node);
+
+error:
+       DBG3("JUL NOT found %s.", name);
+       return NULL;
 }
 
 /*
- * Free given JUl event. After this call, the pointer is not usable anymore.
+ * Free given JUL event. This event must not be globally visible at this
+ * point (only expected to be used on failure just after event
+ * creation). After this call, the pointer is not usable anymore.
  */
 void jul_destroy_event(struct jul_event *event)
 {
@@ -760,7 +854,7 @@ void jul_destroy_event(struct jul_event *event)
 
 /*
  * Destroy a JUL domain completely. Note that the given pointer is NOT freed
- * thus a reference can be passed to this function.
+ * thus a reference to static or stack data can be passed to this function.
  */
 void jul_destroy_domain(struct jul_domain *dom)
 {
@@ -782,6 +876,15 @@ void jul_destroy_domain(struct jul_domain *dom)
        rcu_read_lock();
        cds_lfht_for_each_entry(dom->events->ht, &iter.iter, node, node) {
                int ret;
+               struct jul_event *event;
+
+               /*
+                * When destroying an event, we have to try to disable it on the agent
+                * side so the event stops generating data. The return value is not
+                * important since we have to continue anyway destroying the object.
+                */
+               event = caa_container_of(node, struct jul_event, node);
+               (void) jul_disable_event(event);
 
                ret = lttng_ht_del(dom->events, &iter);
                assert(!ret);
This page took 0.029989 seconds and 5 git commands to generate.