Test fix: use temp file sync. of test app
[lttng-tools.git] / src / bin / lttng-sessiond / jul.c
index 87ab654a34f3c109ce7c477fe6b51eb1892ade0d..3c251998ef560d13d965337515746d9ada832cf1 100644 (file)
@@ -27,7 +27,7 @@
 #include "utils.h"
 
 /*
- * URCU intermediate call to complete destroy a JUL event.
+ * URCU delayed JUL event reclaim.
  */
 static void destroy_event_jul_rcu(struct rcu_head *head)
 {
@@ -40,7 +40,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 +53,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 +67,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);
@@ -133,8 +134,96 @@ error:
        return ret;
 }
 
+
+/*
+ * 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)
+{
+       int ret, i, len = 0, offset = 0;
+       uint32_t nb_event;
+       size_t data_size;
+       struct lttng_event *tmp_events = NULL;
+       struct lttcomm_jul_list_reply *reply = NULL;
+       struct lttcomm_jul_list_reply_hdr reply_hdr;
+
+       assert(app);
+       assert(app->sock);
+       assert(events);
+
+       DBG2("JUL listing events for app pid: %d and socket %d", app->pid,
+                       app->sock->fd);
+
+       ret = send_header(app->sock, 0, JUL_CMD_LIST, 0);
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       /* Get list header so we know how much we'll receive. */
+       ret = recv_reply(app->sock, &reply_hdr, sizeof(reply_hdr));
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       switch (be32toh(reply_hdr.ret_code)) {
+       case JUL_RET_CODE_SUCCESS:
+               data_size = be32toh(reply_hdr.data_size) + sizeof(*reply);
+               break;
+       default:
+               ERR("Java agent returned an unknown code: %" PRIu32,
+                               be32toh(reply_hdr.ret_code));
+               ret = LTTNG_ERR_FATAL;
+               goto error;
+       }
+
+       reply = zmalloc(data_size);
+       if (!reply) {
+               ret = LTTNG_ERR_NOMEM;
+               goto error;
+       }
+
+       /* Get the list with the appropriate data size. */
+       ret = recv_reply(app->sock, reply, data_size);
+       if (ret < 0) {
+               goto error_io;
+       }
+
+       nb_event = be32toh(reply->nb_event);
+       tmp_events = zmalloc(sizeof(*tmp_events) * nb_event);
+       if (!tmp_events) {
+               ret = LTTNG_ERR_NOMEM;
+               goto error;
+       }
+
+       for (i = 0; i < nb_event; i++) {
+               offset += len;
+               strncpy(tmp_events[i].name, reply->payload + offset,
+                               sizeof(tmp_events[i].name));
+               tmp_events[i].pid = app->pid;
+               tmp_events[i].enabled = -1;
+               len = strlen(reply->payload + offset) + 1;
+       }
+
+       *events = tmp_events;
+
+       free(reply);
+       return nb_event;
+
+error_io:
+       ret = LTTNG_ERR_UST_LIST_FAIL;
+error:
+       free(reply);
+       free(tmp_events);
+       return -ret;
+
+}
+
 /*
- * 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.
@@ -160,6 +249,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) {
@@ -219,6 +311,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) {
@@ -274,9 +367,9 @@ int jul_enable_event(struct jul_event *event)
                if (ret != LTTNG_OK) {
                        goto error;
                }
-               event->enabled = 1;
        }
 
+       event->enabled = 1;
        ret = LTTNG_OK;
 
 error:
@@ -307,9 +400,9 @@ int jul_disable_event(struct jul_event *event)
                if (ret != LTTNG_OK) {
                        goto error;
                }
-               event->enabled = 0;
        }
 
+       event->enabled = 0;
        ret = LTTNG_OK;
 
 error:
@@ -328,7 +421,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);
@@ -350,29 +443,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;
        }
@@ -380,8 +477,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;
 }
 
@@ -404,8 +505,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:
@@ -456,59 +555,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.
  */
@@ -530,7 +576,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)
 {
@@ -608,6 +655,7 @@ void jul_add_event(struct jul_event *event, struct jul_domain *dom)
        rcu_read_lock();
        lttng_ht_add_unique_str(dom->events, &event->node);
        rcu_read_unlock();
+       dom->being_used = 1;
 }
 
 /*
@@ -641,28 +689,9 @@ error:
 }
 
 /*
- * Delete JUL event from given domain. Events hash table MUST be initialized.
- */
-void jul_delete_event(struct jul_event *event, struct jul_domain *dom)
-{
-       int ret;
-       struct lttng_ht_iter iter;
-
-       assert(event);
-       assert(dom);
-       assert(dom->events);
-
-       DBG3("JUL deleting event %s from domain", event->name);
-
-       iter.iter.node = &event->node.node;
-       rcu_read_lock();
-       ret = lttng_ht_del(dom->events, &iter);
-       rcu_read_unlock();
-       assert(!ret);
-}
-
-/*
- * 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)
 {
@@ -673,7 +702,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)
 {
This page took 0.027014 seconds and 4 git commands to generate.