+ * 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);
+ 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(<a->node, (void *)((unsigned long)lta->key.pid),
+ sizeof(void *));
+ lta->key.sock = sock;
+ hashtable_node_init(<a->key.node, (void *)((unsigned long)lta->key.sock),
+ sizeof(void *));
+
+ rcu_read_lock();
+ hashtable_add_unique(ust_app_sock_key_map, <a->key.node);
+ hashtable_add_unique(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 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.
+ */
+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.