+ * Return pointer to traceable apps list.
+ */
+struct lttng_ht *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 lttng_ht_node_ulong *node;
+ struct lttng_ht_iter iter;
+
+ rcu_read_lock();
+ lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
+ node = lttng_ht_iter_get_node_ulong(&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;
+
+ if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
+ || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
+ ERR("Registration failed: application \"%s\" (pid: %d) has "
+ "%d-bit long, but no consumerd for this long size is available.\n",
+ msg->name, msg->pid, msg->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->bits_per_long = msg->bits_per_long;
+ lta->v_major = msg->major;
+ lta->v_minor = msg->minor;
+ strncpy(lta->name, msg->name, sizeof(lta->name));
+ lta->name[16] = '\0';
+ lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+
+ /* Set key map */
+ lta->key.pid = msg->pid;
+ lttng_ht_node_init_ulong(<a->node, (unsigned long)lta->key.pid);
+ lta->key.sock = sock;
+ lttng_ht_node_init_ulong(<a->key.node, (unsigned long)lta->key.sock);
+
+ rcu_read_lock();
+ lttng_ht_add_unique_ulong(ust_app_sock_key_map, <a->key.node);
+ lttng_ht_add_unique_ulong(ust_app_ht, <a->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 lttng_ht_node_ulong *node;
+ struct lttng_ht_iter iter;
+ int ret;
+
+ 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);
+
+ /* Remove application from socket hash table */
+ lttng_ht_lookup(ust_app_sock_key_map, (void *)((unsigned long) sock), &iter);
+ ret = lttng_ht_del(ust_app_sock_key_map, &iter);
+ assert(!ret);
+
+ /* Get the node reference for a call_rcu */
+ lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) lta->key.pid), &iter);
+ node = lttng_ht_iter_get_node_ulong(&iter);
+ if (node == NULL) {
+ ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
+ goto error;
+ }
+
+ /* Remove application from PID hash table */
+ ret = lttng_ht_del(ust_app_ht, &iter);
+ assert(!ret);
+ 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 = lttng_ht_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 lttng_ht_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->ht, &iter.iter, app, node.node) {
+ struct lttng_ust_tracepoint_iter uiter;
+
+ 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,
+ &uiter)) != -ENOENT) {
+ if (count >= nbmem) {
+ DBG2("Reallocating event list from %zu to %zu entries", nbmem,
+ 2 * nbmem);
+ nbmem *= 2;
+ tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
+ if (tmp == NULL) {
+ PERROR("realloc ust app events");
+ ret = -ENOMEM;
+ goto rcu_error;
+ }
+ }
+ memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
+ memcpy(tmp[count].loglevel, uiter.loglevel, LTTNG_UST_SYM_NAME_LEN);
+ tmp[count].loglevel_value = uiter.loglevel_value;
+ 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.
+ */
+void ust_app_clean_list(void)
+{
+ int ret;
+ struct lttng_ht_iter iter;
+ struct lttng_ht_node_ulong *node;
+
+ DBG2("UST app cleaning registered apps hash table");
+
+ rcu_read_lock();
+
+ cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, node, node) {
+ ret = lttng_ht_del(ust_app_ht, &iter);
+ assert(!ret);
+ call_rcu(&node->head, delete_ust_app_rcu);
+ }
+ /* Destroy is done only when the ht is empty */
+ lttng_ht_destroy(ust_app_ht);
+
+ cds_lfht_for_each_entry(ust_app_sock_key_map->ht, &iter.iter, node, node) {
+ ret = lttng_ht_del(ust_app_sock_key_map, &iter);
+ assert(!ret);
+ }
+ /* Destroy is done only when the ht is empty */
+ lttng_ht_destroy(ust_app_sock_key_map);
+
+ rcu_read_unlock();
+}
+
+/*
+ * Init UST app hash table.