Simply close socket when application registration is refused
[lttng-tools.git] / lttng-sessiond / ust-app.c
index 0fc208311931846dcda77e3346c58f712dbb4079..ab65f2b1576f2d20ccbdff0bc89e488f81bc2e91 100644 (file)
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <urcu/compiler.h>
 #include <lttngerr.h>
 #include <lttng-share.h>
 
@@ -47,6 +48,7 @@ static void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
        //}
 
        ustctl_release_object(sock, ua_event->obj);
+       free(ua_event->obj);
        free(ua_event);
 }
 
@@ -56,11 +58,9 @@ static void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
  */
 static void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
 {
-       //TODO
-       //stream is used for passing to consumer.
-       //send_channel_streams is responsible for freeing the streams.
-       //note that this will not play well with flight recorder mode:
-       //we might need a criterion to discard the streams.
+       ustctl_release_object(sock, stream->obj);
+       free(stream->obj);
+       free(stream);
 }
 
 /*
@@ -75,6 +75,7 @@ static void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
        struct ltt_ust_stream *stream, *stmp;
 
        cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
+               cds_list_del(&stream->list);
                delete_ust_app_stream(sock, stream);
        }
 
@@ -95,6 +96,9 @@ static void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
                ERR("UST app destroy session hashtable failed");
                goto error;
        }
+       ustctl_release_object(sock, ua_chan->obj);
+       free(ua_chan->obj);
+       free(ua_chan);
 
 error:
        return;
@@ -112,10 +116,10 @@ static void delete_ust_app_session(int sock,
        struct ust_app_channel *ua_chan;
 
        if (ua_sess->metadata) {
-               /*
-                * We do NOT release the stream object and metadata object since they
-                * are release when fds are sent to the consumer.
-                */
+               ustctl_release_object(sock, ua_sess->metadata->stream_obj);
+               free(ua_sess->metadata->stream_obj);
+               ustctl_release_object(sock, ua_sess->metadata->obj);
+               free(ua_sess->metadata->obj);
        }
 
        cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
@@ -143,6 +147,7 @@ static void delete_ust_app(struct ust_app *app)
        struct cds_lfht_node *node;
        struct cds_lfht_iter iter;
        struct ust_app_session *ua_sess;
+       int sock;
 
        rcu_read_lock();
 
@@ -167,9 +172,8 @@ static void delete_ust_app(struct ust_app *app)
        /* Socket is already closed at this point */
 
        /* Delete ust app sessions info */
-       if (app->sock_closed) {
-               app->key.sock = -1;
-       }
+       sock = app->key.sock;
+       app->key.sock = -1;
 
        cds_lfht_for_each_entry(app->sessions, &iter, ua_sess, node) {
                hashtable_del(app->sessions, &iter);
@@ -182,9 +186,13 @@ static void delete_ust_app(struct ust_app *app)
                goto end;
        }
 
-       if (!app->sock_closed) {
-               close(app->key.sock);
-       }
+       /*
+        * Wait until we have removed the key from the sock hash table
+        * before closing this socket, otherwise an application could
+        * re-use the socket ID and race with the teardown, using the
+        * same hash table entry.
+        */
+       close(sock);
 
        DBG2("UST app pid %d deleted", app->key.pid);
        free(app);
@@ -237,236 +245,112 @@ error:
 }
 
 /*
- * Return pointer to traceable apps list.
- */
-struct cds_lfht *ust_app_get_ht(void)
-{
-       return ust_app_ht;
-}
-
-/*
- * Return ust app pointer or NULL if not found.
+ * Open metadata onto the UST tracer for a UST session.
  */
-struct ust_app *ust_app_find_by_pid(pid_t pid)
+static int open_ust_metadata(struct ust_app *app,
+               struct ust_app_session *ua_sess)
 {
-       struct cds_lfht_node *node;
-       struct cds_lfht_iter iter;
+       int ret;
+       struct lttng_ust_channel_attr uattr;
 
-       rcu_read_lock();
-       node = hashtable_lookup(ust_app_ht,
-                       (void *)((unsigned long) pid), sizeof(void *), &iter);
-       if (node == NULL) {
-               DBG2("UST app no found with pid %d", pid);
+       uattr.overwrite = ua_sess->metadata->attr.overwrite;
+       uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
+       uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
+       uattr.switch_timer_interval =
+               ua_sess->metadata->attr.switch_timer_interval;
+       uattr.read_timer_interval =
+               ua_sess->metadata->attr.read_timer_interval;
+       uattr.output = ua_sess->metadata->attr.output;
+
+       /* UST tracer metadata creation */
+       ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
+                       &ua_sess->metadata->obj);
+       if (ret < 0) {
+               ERR("UST app open metadata failed for app pid:%d",
+                               app->key.pid);
                goto error;
        }
-       rcu_read_unlock();
-
-       DBG2("Found UST app by pid %d", pid);
-
-       return caa_container_of(node, struct ust_app, node);
 
 error:
-       rcu_read_unlock();
-       return NULL;
-}
-
-/*
- * Using pid and uid (of the app), allocate a new ust_app struct and
- * add it to the global traceable app list.
- *
- * On success, return 0, else return malloc ENOMEM.
- */
-int ust_app_register(struct ust_register_msg *msg, int sock)
-{
-       struct ust_app *lta;
-
-       lta = zmalloc(sizeof(struct ust_app));
-       if (lta == NULL) {
-               PERROR("malloc");
-               return -ENOMEM;
-       }
-
-       lta->ppid = msg->ppid;
-       lta->uid = msg->uid;
-       lta->gid = msg->gid;
-       lta->v_major = msg->major;
-       lta->v_minor = msg->minor;
-       strncpy(lta->name, msg->name, sizeof(lta->name));
-       lta->name[16] = '\0';
-       lta->sessions = hashtable_new(0);
-
-       /* Set key map */
-       lta->key.pid = msg->pid;
-       hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
-                       sizeof(void *));
-       lta->key.sock = sock;
-       hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
-                       sizeof(void *));
-
-       rcu_read_lock();
-       hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
-       hashtable_add_unique(ust_app_ht, &lta->node);
-       rcu_read_unlock();
-
-       DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
-                       " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
-                       lta->key.sock, lta->name, lta->v_major, lta->v_minor);
-
-       return 0;
+       return ret;
 }
 
 /*
- * Unregister app by removing it from the global traceable app list and freeing
- * the data struct.
- *
- * The socket is already closed at this point so no close to sock.
+ * Create stream onto the UST tracer for a UST session.
  */
-void ust_app_unregister(int sock)
+static int create_ust_stream(struct ust_app *app,
+               struct ust_app_session *ua_sess)
 {
-       struct ust_app *lta;
-       struct cds_lfht_node *node;
-       struct cds_lfht_iter iter;
-
-       rcu_read_lock();
-       lta = find_app_by_sock(sock);
-       if (lta == NULL) {
-               ERR("Unregister app sock %d not found!", sock);
-               goto error;
-       }
-
-       DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
+       int ret;
 
-       /* Get the node reference for a call_rcu */
-       node = hashtable_lookup(ust_app_ht,
-                       (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
-       if (node == NULL) {
-               ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
+       ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
+                       &ua_sess->metadata->stream_obj);
+       if (ret < 0) {
+               ERR("UST create metadata stream failed");
                goto error;
        }
 
-       /* We got called because the socket was closed on the remote end. */
-       close(sock);
-       /* Using a flag because we still need "sock" as a key. */
-       lta->sock_closed = 1;
-       hashtable_del(ust_app_ht, &iter);
-       call_rcu(&node->head, delete_ust_app_rcu);
 error:
-       rcu_read_unlock();
-       return;
-}
-
-/*
- * Return traceable_app_count
- */
-unsigned long ust_app_list_count(void)
-{
-       unsigned long count;
-
-       rcu_read_lock();
-       count = hashtable_get_count(ust_app_ht);
-       rcu_read_unlock();
-
-       return count;
+       return ret;
 }
 
 /*
- * Fill events array with all events name of all registered apps.
+ * Create the specified channel onto the UST tracer for a UST session.
  */
-int ust_app_list_events(struct lttng_event **events)
+static int create_ust_channel(struct ust_app *app,
+               struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
 {
-       int ret, handle;
-       size_t nbmem, count = 0;
-       struct cds_lfht_iter iter;
-       struct ust_app *app;
-       struct lttng_event *tmp;
+       int ret;
 
-       nbmem = UST_APP_EVENT_LIST_SIZE;
-       tmp = zmalloc(nbmem * sizeof(struct lttng_event));
-       if (tmp == NULL) {
-               PERROR("zmalloc ust app events");
-               ret = -ENOMEM;
+       /* TODO: remove cast and use lttng-ust-abi.h */
+       ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
+                       (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
+       if (ret < 0) {
+               DBG("Error creating channel %s for app (pid: %d, sock: %d) "
+                               "and session handle %d with ret %d",
+                               ua_chan->name, app->key.pid, app->key.sock,
+                               ua_sess->handle, ret);
                goto error;
        }
 
-       rcu_read_lock();
-
-       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
-               handle = ustctl_tracepoint_list(app->key.sock);
-               if (handle < 0) {
-                       ERR("UST app list events getting handle failed for app pid %d",
-                                       app->key.pid);
-                       continue;
-               }
-
-               while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
-                                               tmp[count].name)) != -ENOENT) {
-                       if (count > nbmem) {
-                               DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
-                                               nbmem + UST_APP_EVENT_LIST_SIZE);
-                               nbmem += UST_APP_EVENT_LIST_SIZE;
-                               tmp = realloc(tmp, nbmem);
-                               if (tmp == NULL) {
-                                       PERROR("realloc ust app events");
-                                       ret = -ENOMEM;
-                                       goto rcu_error;
-                               }
-                       }
-
-                       tmp[count].type = LTTNG_UST_TRACEPOINT;
-                       tmp[count].pid = app->key.pid;
-                       count++;
-               }
-       }
-
-       ret = count;
-       *events = tmp;
+       ua_chan->handle = ua_chan->obj->handle;
+       ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
+       ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
+       ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
 
-       DBG2("UST app list events done (%zu events)", count);
+       DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
+                       ua_chan->name, app->key.pid, app->key.sock);
 
-rcu_error:
-       rcu_read_unlock();
 error:
        return ret;
 }
 
 /*
- * Free and clean all traceable apps of the global list.
+ * Create the specified event onto the UST tracer for a UST session.
  */
-void ust_app_clean_list(void)
+static int create_ust_event(struct ust_app *app,
+               struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
+               struct ust_app_event *ua_event)
 {
-       int ret;
-       struct cds_lfht_node *node;
-       struct cds_lfht_iter iter;
-       struct ust_app *app;
-
-       DBG2("UST app cleaning registered apps hash table");
-
-       rcu_read_lock();
-
-       cds_lfht_for_each(ust_app_ht, &iter, node) {
-               app = caa_container_of(node, struct ust_app, node);
-               close(app->key.sock);
-               app->sock_closed = 1;
+       int ret = 0;
 
-               ret = hashtable_del(ust_app_ht, &iter);
-               if (!ret) {
-                       call_rcu(&node->head, delete_ust_app_rcu);
-               }
+       /* Create UST event on tracer */
+       ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
+                       &ua_event->obj);
+       if (ret < 0) {
+               ERR("Error ustctl create event %s for app pid: %d with ret %d",
+                               ua_event->attr.name, app->key.pid, ret);
+               goto error;
        }
 
-       hashtable_destroy(ust_app_ht);
-       hashtable_destroy(ust_app_sock_key_map);
+       ua_event->handle = ua_event->obj->handle;
+       ua_event->enabled = 1;
 
-       rcu_read_unlock();
-}
+       DBG2("UST app event %s created successfully for pid:%d",
+                       ua_event->attr.name, app->key.pid);
 
-/*
- * Init UST app hash table.
- */
-void ust_app_ht_alloc(void)
-{
-       ust_app_ht = hashtable_new(0);
-       ust_app_sock_key_map = hashtable_new(0);
+error:
+       return ret;
 }
 
 /*
@@ -566,20 +450,29 @@ error:
        return NULL;
 }
 
+/*
+ * Copy data between an UST app event and a LTT event.
+ */
 static void shadow_copy_event(struct ust_app_event *ua_event,
                struct ltt_ust_event *uevent)
 {
        strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
        ua_event->name[sizeof(ua_event->name) - 1] = '\0';
 
+       /* Copy event attributes */
+       memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
+
        /* TODO: support copy context */
 }
 
+/*
+ * Copy data between an UST app channel and a LTT channel.
+ */
 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
                struct ltt_ust_channel *uchan)
 {
        struct cds_lfht_iter iter;
-       struct cds_lfht_node *node, *ua_event_node;
+       struct cds_lfht_node *ua_event_node;
        struct ltt_ust_event *uevent;
        struct ust_app_event *ua_event;
 
@@ -587,75 +480,105 @@ static void shadow_copy_channel(struct ust_app_channel *ua_chan,
 
        strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
        ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
+       /* Copy event attributes */
+       memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
 
        /* TODO: support copy context */
 
        /* Copy all events from ltt ust channel to ust app channel */
-       hashtable_get_first(uchan->events, &iter);
-       while ((node = hashtable_iter_get_node(&iter)) != NULL) {
-               uevent = caa_container_of(node, struct ltt_ust_event, node);
+       cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
+               struct cds_lfht_iter uiter;
 
                ua_event_node = hashtable_lookup(ua_chan->events,
-                               (void *) uevent->attr.name, strlen(uevent->attr.name), &iter);
+                               (void *) uevent->attr.name, strlen(uevent->attr.name),
+                               &uiter);
                if (ua_event_node == NULL) {
                        DBG2("UST event %s not found on shadow copy channel",
                                        uevent->attr.name);
                        ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
                        if (ua_event == NULL) {
-                               goto next;
+                               continue;
                        }
                        shadow_copy_event(ua_event, uevent);
                        hashtable_add_unique(ua_chan->events, &ua_event->node);
                }
-
-next:
-               /* Get next UST events */
-               hashtable_get_next(uchan->events, &iter);
        }
 
        DBG3("Shadow copy channel done");
 }
 
+/*
+ * Copy data between a UST app session and a regular LTT session.
+ */
 static void shadow_copy_session(struct ust_app_session *ua_sess,
-               struct ltt_ust_session *usess)
+               struct ltt_ust_session *usess,
+               struct ust_app *app)
 {
-       struct cds_lfht_node *node, *ua_chan_node;
+       struct cds_lfht_node *ua_chan_node;
        struct cds_lfht_iter iter;
        struct ltt_ust_channel *uchan;
        struct ust_app_channel *ua_chan;
+       time_t rawtime;
+       struct tm *timeinfo;
+       char datetime[16];
+       int ret;
+
+       /* Get date and time for unique app path */
+       time(&rawtime);
+       timeinfo = localtime(&rawtime);
+       strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
 
        DBG2("Shadow copy of session handle %d", ua_sess->handle);
 
        ua_sess->uid = usess->uid;
 
+       ret = snprintf(ua_sess->path, PATH_MAX,
+                       "%s/%s-%d-%s",
+                       usess->pathname, app->name, app->key.pid,
+                       datetime);
+       if (ret < 0) {
+               PERROR("asprintf UST shadow copy session");
+               /* TODO: We cannot return an error from here.. */
+               assert(0);
+       }
+
        /* TODO: support all UST domain */
 
        /* Iterate over all channels in global domain. */
-       hashtable_get_first(usess->domain_global.channels, &iter);
-       while ((node = hashtable_iter_get_node(&iter)) != NULL) {
-               uchan = caa_container_of(node, struct ltt_ust_channel, node);
+       cds_lfht_for_each_entry(usess->domain_global.channels, &iter,
+                       uchan, node) {
+               struct cds_lfht_iter uiter;
 
                ua_chan_node = hashtable_lookup(ua_sess->channels,
-                               (void *)uchan->name, strlen(uchan->name), &iter);
-               if (ua_chan_node == NULL) {
-                       DBG2("Channel %s not found on shadow session copy, creating it",
-                                       uchan->name);
-                       ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
-                       if (ua_chan == NULL) {
-                               /* malloc failed... continuing */
-                               goto next;
-                       }
-
-                       shadow_copy_channel(ua_chan, uchan);
-                       hashtable_add_unique(ua_sess->channels, &ua_chan->node);
+                               (void *)uchan->name, strlen(uchan->name),
+                               &uiter);
+               if (ua_chan_node != NULL) {
+                       continue;
                }
 
-next:
-               /* Next item in hash table */
-               hashtable_get_next(usess->domain_global.channels, &iter);
+               DBG2("Channel %s not found on shadow session copy, creating it",
+                               uchan->name);
+               ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
+               if (ua_chan == NULL) {
+                       /* malloc failed... continuing */
+                       continue;
+               }
+
+               shadow_copy_channel(ua_chan, uchan);
+               hashtable_add_unique(ua_sess->channels, &ua_chan->node);
        }
 }
 
+static
+void __lookup_session_by_app(struct ltt_ust_session *usess,
+                       struct ust_app *app, struct cds_lfht_iter *iter)
+{
+       /* Get right UST app session from app */
+       (void) hashtable_lookup(app->sessions,
+                       (void *) ((unsigned long) usess->uid), sizeof(void *),
+                       iter);
+}
+
 /*
  * Return ust app session from the app session hashtable using the UST session
  * uid.
@@ -666,9 +589,8 @@ static struct ust_app_session *lookup_session_by_app(
        struct cds_lfht_iter iter;
        struct cds_lfht_node *node;
 
-       /* Get right UST app session from app */
-       node = hashtable_lookup(app->sessions,
-                       (void *) ((unsigned long) usess->uid), sizeof(void *), &iter);
+       __lookup_session_by_app(usess, app, &iter);
+       node = hashtable_iter_get_node(&iter);
        if (node == NULL) {
                goto error;
        }
@@ -700,7 +622,7 @@ static struct ust_app_session *create_ust_app_session(
                        /* Only malloc can failed so something is really wrong */
                        goto error;
                }
-               shadow_copy_session(ua_sess, usess);
+               shadow_copy_session(ua_sess, usess, app);
        }
 
        if (ua_sess->handle == -1) {
@@ -730,64 +652,8 @@ error:
 }
 
 /*
- * Create the specified channel onto the UST tracer for a UST session.
+ * Create UST app channel and create it on the tracer.
  */
-static int create_ust_channel(struct ust_app *app,
-               struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
-{
-       int ret;
-
-       /* TODO: remove cast and use lttng-ust-abi.h */
-       ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
-                       (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
-       if (ret < 0) {
-               DBG("Error creating channel %s for app (pid: %d, sock: %d) "
-                               "and session handle %d with ret %d",
-                               ua_chan->name, app->key.pid, app->key.sock,
-                               ua_sess->handle, ret);
-               goto error;
-       }
-
-       ua_chan->handle = ua_chan->obj->handle;
-       ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
-       ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
-       ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
-
-       DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
-                       ua_chan->name, app->key.pid, app->key.sock);
-
-error:
-       return ret;
-}
-
-/*
- * Create the specified event onto the UST tracer for a UST session.
- */
-static int create_ust_event(struct ust_app *app,
-               struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
-               struct ust_app_event *ua_event)
-{
-       int ret = 0;
-
-       /* Create UST event on tracer */
-       ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
-                       &ua_event->obj);
-       if (ret < 0) {
-               ERR("Error ustctl create event %s for app pid: %d with ret %d",
-                               ua_event->attr.name, app->key.pid, ret);
-               goto error;
-       }
-
-       ua_event->handle = ua_event->obj->handle;
-       ua_event->enabled = 1;
-
-       DBG2("UST app event %s created successfully for pid:%d",
-                       ua_event->attr.name, app->key.pid);
-
-error:
-       return ret;
-}
-
 static struct ust_app_channel *create_ust_app_channel(
                struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
                struct ust_app *app)
@@ -825,6 +691,9 @@ error:
        return NULL;
 }
 
+/*
+ * Create UST app event and create it on the tracer side.
+ */
 static struct ust_app_event *create_ust_app_event(
                struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
                struct ltt_ust_event *uevent, struct ust_app *app)
@@ -863,11 +732,13 @@ error:
        return NULL;
 }
 
+/*
+ * Create UST metadata and open it on the tracer side.
+ */
 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
                char *pathname, struct ust_app *app)
 {
        int ret = 0;
-       struct lttng_ust_channel_attr uattr;
 
        if (ua_sess->metadata == NULL) {
                /* Allocate UST metadata */
@@ -878,21 +749,8 @@ static int create_ust_app_metadata(struct ust_app_session *ua_sess,
                        goto error;
                }
 
-               uattr.overwrite = ua_sess->metadata->attr.overwrite;
-               uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
-               uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
-               uattr.switch_timer_interval =
-                       ua_sess->metadata->attr.switch_timer_interval;
-               uattr.read_timer_interval =
-                       ua_sess->metadata->attr.read_timer_interval;
-               uattr.output = ua_sess->metadata->attr.output;
-
-               /* UST tracer metadata creation */
-               ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
-                               &ua_sess->metadata->obj);
+               ret = open_ust_metadata(app, ua_sess);
                if (ret < 0) {
-                       ERR("UST app open metadata failed for app pid:%d",
-                                       app->key.pid);
                        goto error;
                }
 
@@ -901,28 +759,19 @@ static int create_ust_app_metadata(struct ust_app_session *ua_sess,
 
        /* Open UST metadata stream */
        if (ua_sess->metadata->stream_obj == NULL) {
-               ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
-                               &ua_sess->metadata->stream_obj);
-               if (ret < 0) {
-                       ERR("UST create metadata stream failed");
-                       goto error;
-               }
-
-               ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, "%s/%s-%d",
-                               pathname, app->name, app->key.pid);
+               ret = create_ust_stream(app, ua_sess);
                if (ret < 0) {
-                       PERROR("asprintf UST create stream");
                        goto error;
                }
 
-               ret = mkdir(ua_sess->metadata->pathname, S_IRWXU | S_IRWXG);
+               ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
                if (ret < 0) {
                        PERROR("mkdir UST metadata");
                        goto error;
                }
 
-               ret = snprintf(ua_sess->metadata->pathname, PATH_MAX, "%s/%s-%d/metadata",
-                               pathname, app->name, app->key.pid);
+               ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
+                               "%s/metadata", ua_sess->path);
                if (ret < 0) {
                        PERROR("asprintf UST create stream");
                        goto error;
@@ -930,6 +779,9 @@ static int create_ust_app_metadata(struct ust_app_session *ua_sess,
 
                DBG2("UST metadata stream object created for app pid %d",
                                app->key.pid);
+       } else {
+               ERR("Attempting to create stream without metadata opened");
+               goto error;
        }
 
        return 0;
@@ -939,14 +791,257 @@ error:
 }
 
 /*
- * Add channel to all ust app session.
+ * Return pointer to traceable apps list.
+ */
+struct cds_lfht *ust_app_get_ht(void)
+{
+       return ust_app_ht;
+}
+
+/*
+ * Return ust app pointer or NULL if not found.
+ */
+struct ust_app *ust_app_find_by_pid(pid_t pid)
+{
+       struct cds_lfht_node *node;
+       struct cds_lfht_iter iter;
+
+       rcu_read_lock();
+       node = hashtable_lookup(ust_app_ht,
+                       (void *)((unsigned long) pid), sizeof(void *), &iter);
+       if (node == NULL) {
+               DBG2("UST app no found with pid %d", pid);
+               goto error;
+       }
+       rcu_read_unlock();
+
+       DBG2("Found UST app by pid %d", pid);
+
+       return caa_container_of(node, struct ust_app, node);
+
+error:
+       rcu_read_unlock();
+       return NULL;
+}
+
+/*
+ * Using pid and uid (of the app), allocate a new ust_app struct and
+ * add it to the global traceable app list.
+ *
+ * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
+ * bitness is not supported.
+ */
+int ust_app_register(struct ust_register_msg *msg, int sock)
+{
+       struct ust_app *lta;
+
+       /*
+        * Currently support only tracing of application which share the
+        * same bitness as the consumer. Eventually implement dispatch
+        * to specific compat32 consumer.
+        */
+       if (msg->bits_per_long != CAA_BITS_PER_LONG) {
+               ERR("Registration failed: application \"%s\" (pid: %d) has "
+                       "%d-bit long, but only "
+                       "%d-bit lttng-consumerd is available.\n",
+                       msg->name, msg->pid, msg->bits_per_long,
+                       CAA_BITS_PER_LONG);
+               close(sock);
+               return -EINVAL;
+       }
+
+       lta = zmalloc(sizeof(struct ust_app));
+       if (lta == NULL) {
+               PERROR("malloc");
+               return -ENOMEM;
+       }
+
+       lta->ppid = msg->ppid;
+       lta->uid = msg->uid;
+       lta->gid = msg->gid;
+       lta->v_major = msg->major;
+       lta->v_minor = msg->minor;
+       strncpy(lta->name, msg->name, sizeof(lta->name));
+       lta->name[16] = '\0';
+       lta->sessions = hashtable_new(0);
+
+       /* Set key map */
+       lta->key.pid = msg->pid;
+       hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
+                       sizeof(void *));
+       lta->key.sock = sock;
+       hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
+                       sizeof(void *));
+
+       rcu_read_lock();
+       hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
+       hashtable_add_unique(ust_app_ht, &lta->node);
+       rcu_read_unlock();
+
+       DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
+                       " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
+                       lta->key.sock, lta->name, lta->v_major, lta->v_minor);
+
+       return 0;
+}
+
+/*
+ * Unregister app by removing it from the global traceable app list and freeing
+ * the data struct.
+ *
+ * The socket is already closed at this point so no close to sock.
+ */
+void ust_app_unregister(int sock)
+{
+       struct ust_app *lta;
+       struct cds_lfht_node *node;
+       struct cds_lfht_iter iter;
+
+       rcu_read_lock();
+       lta = find_app_by_sock(sock);
+       if (lta == NULL) {
+               ERR("Unregister app sock %d not found!", sock);
+               goto error;
+       }
+
+       DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
+
+       /* Get the node reference for a call_rcu */
+       node = hashtable_lookup(ust_app_ht,
+                       (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
+       if (node == NULL) {
+               ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
+               goto error;
+       }
+
+       hashtable_del(ust_app_ht, &iter);
+       call_rcu(&node->head, delete_ust_app_rcu);
+error:
+       rcu_read_unlock();
+       return;
+}
+
+/*
+ * Return traceable_app_count
+ */
+unsigned long ust_app_list_count(void)
+{
+       unsigned long count;
+
+       rcu_read_lock();
+       count = hashtable_get_count(ust_app_ht);
+       rcu_read_unlock();
+
+       return count;
+}
+
+/*
+ * Fill events array with all events name of all registered apps.
+ */
+int ust_app_list_events(struct lttng_event **events)
+{
+       int ret, handle;
+       size_t nbmem, count = 0;
+       struct cds_lfht_iter iter;
+       struct ust_app *app;
+       struct lttng_event *tmp;
+
+       nbmem = UST_APP_EVENT_LIST_SIZE;
+       tmp = zmalloc(nbmem * sizeof(struct lttng_event));
+       if (tmp == NULL) {
+               PERROR("zmalloc ust app events");
+               ret = -ENOMEM;
+               goto error;
+       }
+
+       rcu_read_lock();
+
+       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
+               handle = ustctl_tracepoint_list(app->key.sock);
+               if (handle < 0) {
+                       ERR("UST app list events getting handle failed for app pid %d",
+                                       app->key.pid);
+                       continue;
+               }
+
+               while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
+                                               tmp[count].name)) != -ENOENT) {
+                       if (count > nbmem) {
+                               DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
+                                               nbmem + UST_APP_EVENT_LIST_SIZE);
+                               nbmem += UST_APP_EVENT_LIST_SIZE;
+                               tmp = realloc(tmp, nbmem);
+                               if (tmp == NULL) {
+                                       PERROR("realloc ust app events");
+                                       ret = -ENOMEM;
+                                       goto rcu_error;
+                               }
+                       }
+
+                       tmp[count].type = LTTNG_UST_TRACEPOINT;
+                       tmp[count].pid = app->key.pid;
+                       tmp[count].enabled = -1;
+                       count++;
+               }
+       }
+
+       ret = count;
+       *events = tmp;
+
+       DBG2("UST app list events done (%zu events)", count);
+
+rcu_error:
+       rcu_read_unlock();
+error:
+       return ret;
+}
+
+/*
+ * Free and clean all traceable apps of the global list.
  */
-int ust_app_add_channel_all(struct ltt_ust_session *usess,
+void ust_app_clean_list(void)
+{
+       int ret;
+       struct cds_lfht_node *node;
+       struct cds_lfht_iter iter;
+       struct ust_app *app;
+
+       DBG2("UST app cleaning registered apps hash table");
+
+       rcu_read_lock();
+
+       cds_lfht_for_each(ust_app_ht, &iter, node) {
+               app = caa_container_of(node, struct ust_app, node);
+
+               ret = hashtable_del(ust_app_ht, &iter);
+               if (!ret) {
+                       call_rcu(&node->head, delete_ust_app_rcu);
+               }
+       }
+
+       hashtable_destroy(ust_app_ht);
+       hashtable_destroy(ust_app_sock_key_map);
+
+       rcu_read_unlock();
+}
+
+/*
+ * Init UST app hash table.
+ */
+void ust_app_ht_alloc(void)
+{
+       ust_app_ht = hashtable_new(0);
+       ust_app_sock_key_map = hashtable_new(0);
+}
+
+/*
+ * For a specific UST session, create the channel for all registered apps.
+ */
+int ust_app_create_channel_all(struct ltt_ust_session *usess,
                struct ltt_ust_channel *uchan)
 {
        int ret = 0;
        struct cds_lfht_iter iter;
-       struct cds_lfht_node *node;
        struct ust_app *app;
        struct ust_app_session *ua_sess;
        struct ust_app_channel *ua_chan;
@@ -962,39 +1057,37 @@ int ust_app_add_channel_all(struct ltt_ust_session *usess,
 
        rcu_read_lock();
 
-       /* For every UST applications registered */
-       hashtable_get_first(ust_app_ht, &iter);
-       while ((node = hashtable_iter_get_node(&iter)) != NULL) {
-               app = caa_container_of(node, struct ust_app, node);
-
+       /* For every registered applications */
+       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
                /* Create session on the tracer side and add it to app session HT */
                ua_sess = create_ust_app_session(usess, app);
                if (ua_sess == NULL) {
-                       goto next;
+                       continue;
                }
 
                /* Create channel onto application */
                ua_chan = create_ust_app_channel(ua_sess, uchan, app);
                if (ua_chan == NULL) {
-                       goto next;
+                       continue;
                }
-
-next:
-               /* Next applications */
-               hashtable_get_next(ust_app_ht, &iter);
        }
+
        rcu_read_unlock();
 
 error:
        return ret;
 }
 
-int ust_app_add_event_all(struct ltt_ust_session *usess,
+/*
+ * For a specific UST session and UST channel, create the event for all
+ * registered apps.
+ */
+int ust_app_create_event_all(struct ltt_ust_session *usess,
                struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
 {
        int ret = 0;
        struct cds_lfht_iter iter;
-       struct cds_lfht_node *node, *ua_chan_node;
+       struct cds_lfht_node *ua_chan_node;
        struct ust_app *app;
        struct ust_app_session *ua_sess;
        struct ust_app_channel *ua_chan;
@@ -1006,47 +1099,47 @@ int ust_app_add_event_all(struct ltt_ust_session *usess,
        rcu_read_lock();
 
        /* For all registered applications */
-       hashtable_get_first(ust_app_ht, &iter);
-       while ((node = hashtable_iter_get_node(&iter)) != NULL) {
-               app = caa_container_of(node, struct ust_app, node);
+       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
+               struct cds_lfht_iter uiter;
 
                /* Create session on the tracer side and add it to app session HT */
                ua_sess = create_ust_app_session(usess, app);
                if (ua_sess == NULL) {
-                       goto next;
+                       continue;
                }
 
                /* Lookup channel in the ust app session */
                ua_chan_node = hashtable_lookup(ua_sess->channels,
-                               (void *)uchan->name, strlen(uchan->name), &iter);
+                               (void *)uchan->name, strlen(uchan->name),
+                               &uiter);
                if (ua_chan_node == NULL) {
                        ERR("Channel %s not found in session uid %d. Skipping",
                                        uchan->name, usess->uid);
-                       goto next;
+                       continue;
                }
                ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
 
                ua_event = create_ust_app_event(ua_sess, ua_chan, uevent, app);
                if (ua_event == NULL) {
-                       goto next;
+                       continue;
                }
-
-next:
-               /* Next applications */
-               hashtable_get_next(ust_app_ht, &iter);
        }
+
        rcu_read_unlock();
 
        return ret;
 }
 
+/*
+ * Start tracing for a specific UST session and app.
+ */
 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
 {
        int ret = 0;
        struct cds_lfht_iter iter;
-       struct cds_lfht_node *node;
        struct ust_app_session *ua_sess;
        struct ust_app_channel *ua_chan;
+       struct ltt_ust_stream *ustream;
 
        DBG("Starting tracing for ust app pid %d", app->key.pid);
 
@@ -1058,20 +1151,20 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
                goto error_rcu_unlock;
        }
 
+       /* upon restart, we skip the setup, already done */
+       if (ua_sess->started)
+               goto skip_setup;
+
        ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
        if (ret < 0) {
                goto error_rcu_unlock;
        }
 
        /* For each channel */
-       hashtable_get_first(ua_sess->channels, &iter);
-       while ((node = hashtable_iter_get_node(&iter)) != NULL) {
-               ua_chan = caa_container_of(node, struct ust_app_channel, node);
-
+       cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
                /* Create all streams */
                while (1) {
-                       struct ltt_ust_stream *ustream;
-
+                       /* Create UST stream */
                        ustream = zmalloc(sizeof(*ustream));
                        if (ustream == NULL) {
                                PERROR("zmalloc ust stream");
@@ -1088,9 +1181,9 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
 
                        /* Order is important */
                        cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
-                       ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s-%d/%s_%u",
-                                       usess->pathname, app->name, app->key.pid,
-                                       ua_chan->name, ua_chan->streams.count++);
+                       ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
+                                       ua_sess->path, ua_chan->name,
+                                       ua_chan->streams.count++);
                        if (ret < 0) {
                                PERROR("asprintf UST create stream");
                                continue;
@@ -1098,23 +1191,23 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
                        DBG2("UST stream %d ready at %s", ua_chan->streams.count,
                                        ustream->pathname);
                }
-
-               /* Next applications */
-               hashtable_get_next(ua_sess->channels, &iter);
        }
 
        /* Setup UST consumer socket and send fds to it */
-       ret = ust_consumer_send_session(usess->consumer_fd, ua_sess);
+       ret = ust_consumer_send_session(ust_consumer_fd, ua_sess);
        if (ret < 0) {
                goto error_rcu_unlock;
        }
+       ua_sess->started = 1;
 
+skip_setup:
        /* This start the UST tracing */
        ret = ustctl_start_session(app->key.sock, ua_sess->handle);
        if (ret < 0) {
                ERR("Error starting tracing for app pid: %d", app->key.pid);
                goto error_rcu_unlock;
        }
+
        rcu_read_unlock();
 
        /* Quiescent wait after starting trace */
@@ -1127,34 +1220,181 @@ error_rcu_unlock:
        return -1;
 }
 
-int ust_app_start_trace_all(struct ltt_ust_session *usess)
+/*
+ * Stop tracing for a specific UST session and app.
+ */
+int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
 {
        int ret = 0;
+       struct ust_app_session *ua_sess;
+
+       DBG("Stopping tracing for ust app pid %d", app->key.pid);
+
+       rcu_read_lock();
+
+       ua_sess = lookup_session_by_app(usess, app);
+       if (ua_sess == NULL) {
+               /* Only malloc can failed so something is really wrong */
+               goto error_rcu_unlock;
+       }
+
+#if 0  /* only useful when periodical flush will be supported */
+       /* need to keep a handle on shm in session for this. */
+       /* Flush all buffers before stopping */
+       ret = ustctl_flush_buffer(usess->sock, usess->metadata->obj);
+       if (ret < 0) {
+               ERR("UST metadata flush failed");
+       }
+
+       cds_list_for_each_entry(ustchan, &usess->channels.head, list) {
+               ret = ustctl_flush_buffer(usess->sock, ustchan->obj);
+               if (ret < 0) {
+                       ERR("UST flush buffer error");
+               }
+       }
+#endif
+
+       /* This inhibits UST tracing */
+       ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
+       if (ret < 0) {
+               ERR("Error stopping tracing for app pid: %d", app->key.pid);
+               goto error_rcu_unlock;
+       }
+
+       rcu_read_unlock();
+
+       /* Quiescent wait after stopping trace */
+       ustctl_wait_quiescent(app->key.sock);
+
+       return 0;
+
+error_rcu_unlock:
+       rcu_read_unlock();
+       return -1;
+}
+
+/*
+ * Destroy a specific UST session in apps.
+ */
+int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
+{
+       struct ust_app_session *ua_sess;
+       struct lttng_ust_object_data obj;
        struct cds_lfht_iter iter;
        struct cds_lfht_node *node;
+
+       DBG("Destroy tracing for ust app pid %d", app->key.pid);
+
+       rcu_read_lock();
+
+       __lookup_session_by_app(usess, app, &iter);
+       node = hashtable_iter_get_node(&iter);
+       if (node == NULL) {
+               /* Only malloc can failed so something is really wrong */
+               goto error_rcu_unlock;
+       }
+       ua_sess = caa_container_of(node, struct ust_app_session, node);
+       hashtable_del(app->sessions, &iter);
+       delete_ust_app_session(app->key.sock, ua_sess);
+       obj.handle = ua_sess->handle;
+       obj.shm_fd = -1;
+       obj.wait_fd = -1;
+       obj.memory_map_size = 0;
+       ustctl_release_object(app->key.sock, &obj);
+
+       rcu_read_unlock();
+
+       /* Quiescent wait after stopping trace */
+       ustctl_wait_quiescent(app->key.sock);
+
+       return 0;
+
+error_rcu_unlock:
+       rcu_read_unlock();
+       return -1;
+}
+
+/*
+ * Start tracing for the UST session.
+ */
+int ust_app_start_trace_all(struct ltt_ust_session *usess)
+{
+       int ret = 0;
+       struct cds_lfht_iter iter;
        struct ust_app *app;
 
        DBG("Starting all UST traces");
 
        rcu_read_lock();
-       hashtable_get_first(ust_app_ht, &iter);
-       while ((node = hashtable_iter_get_node(&iter)) != NULL) {
-               app = caa_container_of(node, struct ust_app, node);
 
+       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
                ret = ust_app_start_trace(usess, app);
                if (ret < 0) {
-                       goto next;
+                       /* Continue to next apps even on error */
+                       continue;
+               }
+       }
+
+       rcu_read_unlock();
+
+       return 0;
+}
+
+/*
+ * Start tracing for the UST session.
+ */
+int ust_app_stop_trace_all(struct ltt_ust_session *usess)
+{
+       int ret = 0;
+       struct cds_lfht_iter iter;
+       struct ust_app *app;
+
+       DBG("Stopping all UST traces");
+
+       rcu_read_lock();
+
+       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
+               ret = ust_app_stop_trace(usess, app);
+               if (ret < 0) {
+                       /* Continue to next apps even on error */
+                       continue;
                }
+       }
+
+       rcu_read_unlock();
+
+       return 0;
+}
+
+/*
+ * Destroy app UST session.
+ */
+int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
+{
+       int ret = 0;
+       struct cds_lfht_iter iter;
+       struct ust_app *app;
+
+       DBG("Destroy all UST traces");
+
+       rcu_read_lock();
 
-next:
-               /* Next applications */
-               hashtable_get_next(ust_app_ht, &iter);
+       cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
+               ret = ust_app_destroy_trace(usess, app);
+               if (ret < 0) {
+                       /* Continue to next apps even on error */
+                       continue;
+               }
        }
+
        rcu_read_unlock();
 
        return 0;
 }
 
+/*
+ * Add channels/events from UST global domain to registered apps at sock.
+ */
 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
 {
        int ret = 0;
@@ -1165,7 +1405,7 @@ void ust_app_global_update(struct ltt_ust_session *usess, int sock)
        struct ust_app_event *ua_event;
 
        if (usess == NULL) {
-               DBG2("No UST session on global update. Returning");
+               ERR("No UST session on global update. Returning");
                goto error;
        }
 
This page took 0.038148 seconds and 4 git commands to generate.