From 852d003742c637d479f91767b853aa85eb0ef258 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Tue, 13 Mar 2012 11:13:25 -0400 Subject: [PATCH] Fix double PID registration race Introduce a second hash table indexed by application socket which have the exact same content as the hash table indexed by PID. On unregister, we now use a direct lookup per socket instead of using the key map between sock and PID. This prevents the PID-sock lookup race when the unregister happens just after the replace and before the close(fd). We also use an add_replace call on application registration for the PID hash table and kept the add_unique for the socket hash table. (closes #7) Acked-by: Mathieu Desnoyers Signed-off-by: David Goulet --- src/bin/lttng-sessiond/ust-app.c | 266 ++++++++++++++++--------------- src/bin/lttng-sessiond/ust-app.h | 21 +-- src/common/hashtable/hashtable.h | 4 +- 3 files changed, 152 insertions(+), 139 deletions(-) diff --git a/src/bin/lttng-sessiond/ust-app.c b/src/bin/lttng-sessiond/ust-app.c index 8e6d20e86..10ffe403b 100644 --- a/src/bin/lttng-sessiond/ust-app.c +++ b/src/bin/lttng-sessiond/ust-app.c @@ -180,29 +180,37 @@ void delete_ust_app(struct ust_app *app) rcu_read_lock(); /* Delete ust app sessions info */ - sock = app->key.sock; - app->key.sock = -1; + sock = app->sock; + app->sock = -1; /* Wipe sessions */ cds_lfht_for_each_entry(app->sessions->ht, &iter.iter, ua_sess, node.node) { ret = lttng_ht_del(app->sessions, &iter); assert(!ret); - delete_ust_app_session(app->key.sock, ua_sess); + delete_ust_app_session(app->sock, ua_sess); } lttng_ht_destroy(app->sessions); /* - * 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. + * Wait until we have deleted the application 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. + * + * It's OK to leave the close in call_rcu. We want it to stay unique for + * all RCU readers that could run concurrently with unregister app, + * therefore we _need_ to only close that socket after a grace period. So + * it should stay in this RCU callback. + * + * This close() is a very important step of the synchronization model so + * every modification to this function must be carefully reviewed. */ ret = close(sock); if (ret) { PERROR("close"); } - DBG2("UST app pid %d deleted", app->key.pid); + DBG2("UST app pid %d deleted", app->pid); free(app); rcu_read_unlock(); @@ -217,9 +225,9 @@ void delete_ust_app_rcu(struct rcu_head *head) struct lttng_ht_node_ulong *node = caa_container_of(head, struct lttng_ht_node_ulong, head); struct ust_app *app = - caa_container_of(node, struct ust_app, node); + caa_container_of(node, struct ust_app, pid_n); - DBG3("Call RCU deleting app PID %d", app->key.pid); + DBG3("Call RCU deleting app PID %d", app->pid); delete_ust_app(app); } @@ -354,25 +362,16 @@ static struct ust_app *find_app_by_sock(int sock) { struct lttng_ht_node_ulong *node; - struct ust_app_key *key; struct lttng_ht_iter iter; - lttng_ht_lookup(ust_app_sock_key_map, (void *)((unsigned long) sock), - &iter); - node = lttng_ht_iter_get_node_ulong(&iter); - if (node == NULL) { - DBG2("UST app find by sock %d key not found", sock); - goto error; - } - key = caa_container_of(node, struct ust_app_key, node); - - lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) key->pid), &iter); + lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); node = lttng_ht_iter_get_node_ulong(&iter); if (node == NULL) { DBG2("UST app find by sock %d not found", sock); goto error; } - return caa_container_of(node, struct ust_app, node); + + return caa_container_of(node, struct ust_app, sock_n); error: return NULL; @@ -387,7 +386,7 @@ int create_ust_channel_context(struct ust_app_channel *ua_chan, { int ret; - ret = ustctl_add_context(app->key.sock, &ua_ctx->ctx, + ret = ustctl_add_context(app->sock, &ua_ctx->ctx, ua_chan->obj, &ua_ctx->obj); if (ret < 0) { goto error; @@ -410,7 +409,7 @@ int create_ust_event_context(struct ust_app_event *ua_event, { int ret; - ret = ustctl_add_context(app->key.sock, &ua_ctx->ctx, + ret = ustctl_add_context(app->sock, &ua_ctx->ctx, ua_event->obj, &ua_ctx->obj); if (ret < 0) { goto error; @@ -432,16 +431,16 @@ static int disable_ust_event(struct ust_app *app, { int ret; - ret = ustctl_disable(app->key.sock, ua_event->obj); + ret = ustctl_disable(app->sock, ua_event->obj); if (ret < 0) { ERR("UST app event %s disable failed for app (pid: %d) " "and session handle %d with ret %d", - ua_event->attr.name, app->key.pid, ua_sess->handle, ret); + ua_event->attr.name, app->pid, ua_sess->handle, ret); goto error; } DBG2("UST app event %s disabled successfully for app (pid: %d)", - ua_event->attr.name, app->key.pid); + ua_event->attr.name, app->pid); error: return ret; @@ -455,16 +454,16 @@ static int disable_ust_channel(struct ust_app *app, { int ret; - ret = ustctl_disable(app->key.sock, ua_chan->obj); + ret = ustctl_disable(app->sock, ua_chan->obj); if (ret < 0) { ERR("UST app channel %s disable failed for app (pid: %d) " "and session handle %d with ret %d", - ua_chan->name, app->key.pid, ua_sess->handle, ret); + ua_chan->name, app->pid, ua_sess->handle, ret); goto error; } DBG2("UST app channel %s disabled successfully for app (pid: %d)", - ua_chan->name, app->key.pid); + ua_chan->name, app->pid); error: return ret; @@ -478,18 +477,18 @@ static int enable_ust_channel(struct ust_app *app, { int ret; - ret = ustctl_enable(app->key.sock, ua_chan->obj); + ret = ustctl_enable(app->sock, ua_chan->obj); if (ret < 0) { ERR("UST app channel %s enable failed for app (pid: %d) " "and session handle %d with ret %d", - ua_chan->name, app->key.pid, ua_sess->handle, ret); + ua_chan->name, app->pid, ua_sess->handle, ret); goto error; } ua_chan->enabled = 1; DBG2("UST app channel %s enabled successfully for app (pid: %d)", - ua_chan->name, app->key.pid); + ua_chan->name, app->pid); error: return ret; @@ -503,16 +502,16 @@ static int enable_ust_event(struct ust_app *app, { int ret; - ret = ustctl_enable(app->key.sock, ua_event->obj); + ret = ustctl_enable(app->sock, ua_event->obj); if (ret < 0) { ERR("UST app event %s enable failed for app (pid: %d) " "and session handle %d with ret %d", - ua_event->attr.name, app->key.pid, ua_sess->handle, ret); + ua_event->attr.name, app->pid, ua_sess->handle, ret); goto error; } DBG2("UST app event %s enabled successfully for app (pid: %d)", - ua_event->attr.name, app->key.pid); + ua_event->attr.name, app->pid); error: return ret; @@ -537,11 +536,11 @@ static int open_ust_metadata(struct ust_app *app, uattr.output = ua_sess->metadata->attr.output; /* UST tracer metadata creation */ - ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr, + ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr, &ua_sess->metadata->obj); if (ret < 0) { ERR("UST app open metadata failed for app pid:%d with ret %d", - app->key.pid, ret); + app->pid, ret); goto error; } @@ -559,7 +558,7 @@ static int create_ust_stream(struct ust_app *app, { int ret; - ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj, + ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj, &ua_sess->metadata->stream_obj); if (ret < 0) { ERR("UST create metadata stream failed"); @@ -579,12 +578,12 @@ static int create_ust_channel(struct ust_app *app, int ret; /* TODO: remove cast and use lttng-ust-abi.h */ - ret = ustctl_create_channel(app->key.sock, ua_sess->handle, + ret = ustctl_create_channel(app->sock, ua_sess->handle, (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj); if (ret < 0) { ERR("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_chan->name, app->pid, app->sock, ua_sess->handle, ret); goto error; } @@ -592,7 +591,7 @@ static int create_ust_channel(struct ust_app *app, ua_chan->handle = ua_chan->obj->handle; DBG2("UST app channel %s created successfully for pid:%d and sock:%d", - ua_chan->name, app->key.pid, app->key.sock); + ua_chan->name, app->pid, app->sock); /* If channel is not enabled, disable it on the tracer */ if (!ua_chan->enabled) { @@ -616,7 +615,7 @@ int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, int ret = 0; /* Create UST event on tracer */ - ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj, + ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, &ua_event->obj); if (ret < 0) { if (ret == -EEXIST) { @@ -624,14 +623,14 @@ int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, goto error; } ERR("Error ustctl create event %s for app pid: %d with ret %d", - ua_event->attr.name, app->key.pid, ret); + ua_event->attr.name, app->pid, ret); goto error; } ua_event->handle = ua_event->obj->handle; DBG2("UST app event %s created successfully for pid:%d", - ua_event->attr.name, app->key.pid); + ua_event->attr.name, app->pid); /* If event not enabled, disable it on the tracer */ if (ua_event->enabled == 0) { @@ -772,7 +771,7 @@ static void shadow_copy_session(struct ust_app_session *ua_sess, ua_sess->gid = usess->gid; ret = snprintf(ua_sess->path, PATH_MAX, "%s/%s-%d-%s", usess->pathname, - app->name, app->key.pid, datetime); + app->name, app->pid, datetime); if (ret < 0) { PERROR("asprintf UST shadow copy session"); /* TODO: We cannot return an error from here.. */ @@ -854,7 +853,7 @@ static struct ust_app_session *create_ust_app_session( ua_sess = lookup_session_by_app(usess, app); if (ua_sess == NULL) { DBG2("UST app pid: %d session id %d not found, creating it", - app->key.pid, usess->id); + app->pid, usess->id); ua_sess = alloc_ust_app_session(); if (ua_sess == NULL) { /* Only malloc can failed so something is really wrong */ @@ -864,9 +863,9 @@ static struct ust_app_session *create_ust_app_session( } if (ua_sess->handle == -1) { - ret = ustctl_create_session(app->key.sock); + ret = ustctl_create_session(app->sock); if (ret < 0) { - ERR("Creating session for app pid %d", app->key.pid); + ERR("Creating session for app pid %d", app->pid); /* This means that the tracer is gone... */ ua_sess = (void*) -1UL; goto error; @@ -1097,7 +1096,7 @@ static struct ust_app_channel *create_ust_app_channel( lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); DBG2("UST app create channel %s for PID %d completed", ua_chan->name, - app->key.pid); + app->pid); end: return ua_chan; @@ -1148,7 +1147,7 @@ int create_ust_app_event(struct ust_app_session *ua_sess, lttng_ht_add_unique_str(ua_chan->events, &ua_event->node); DBG2("UST app create event %s for PID %d completed", ua_event->name, - app->key.pid); + app->pid); end: return ret; @@ -1189,7 +1188,7 @@ static int create_ust_app_metadata(struct ust_app_session *ua_sess, goto error; } - DBG2("UST metadata opened for app pid %d", app->key.pid); + DBG2("UST metadata opened for app pid %d", app->pid); } /* Open UST metadata stream */ @@ -1214,7 +1213,7 @@ 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); + app->pid); } else { ERR("Attempting to create stream without metadata opened"); goto error; @@ -1253,7 +1252,7 @@ struct ust_app *ust_app_find_by_pid(pid_t pid) DBG2("Found UST app by pid %d", pid); - return caa_container_of(node, struct ust_app, node); + return caa_container_of(node, struct ust_app, pid_n); error: rcu_read_unlock(); @@ -1310,20 +1309,31 @@ int ust_app_register(struct ust_register_msg *msg, int sock) 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); + lta->pid = msg->pid; + lttng_ht_node_init_ulong(<a->pid_n, (unsigned long)lta->pid); + lta->sock = sock; + lttng_ht_node_init_ulong(<a->sock_n, (unsigned long)lta->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); + + /* + * On a re-registration, we want to kick out the previous registration of + * that pid + */ + lttng_ht_add_replace_ulong(ust_app_ht, <a->pid_n); + + /* + * The socket _should_ be unique until _we_ call close. So, a add_unique + * for the ust_app_ht_by_sock is used which asserts fail if the entry was + * already in the table. + */ + lttng_ht_add_unique_ulong(ust_app_ht_by_sock, <a->sock_n); + 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); + " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid, + lta->sock, lta->name, lta->v_major, lta->v_minor); return 0; } @@ -1342,31 +1352,32 @@ void ust_app_unregister(int sock) 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); + lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &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); + ERR("Unable to find app by sock %d", sock); goto error; } + lta = caa_container_of(node, struct ust_app, sock_n); + + DBG("PID %d unregistering with sock %d", lta->pid, sock); + /* Remove application from PID hash table */ + ret = lttng_ht_del(ust_app_ht_by_sock, &iter); + assert(!ret); + + /* Assign second node for deletion */ + iter.iter.node = <a->pid_n.node; + ret = lttng_ht_del(ust_app_ht, &iter); assert(!ret); - call_rcu(&node->head, delete_ust_app_rcu); + + /* Free memory */ + call_rcu(<a->pid_n.head, delete_ust_app_rcu); + error: rcu_read_unlock(); return; @@ -1407,7 +1418,7 @@ int ust_app_list_events(struct lttng_event **events) rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { struct lttng_ust_tracepoint_iter uiter; if (!app->compatible) { @@ -1417,14 +1428,14 @@ int ust_app_list_events(struct lttng_event **events) */ continue; } - handle = ustctl_tracepoint_list(app->key.sock); + handle = ustctl_tracepoint_list(app->sock); if (handle < 0) { ERR("UST app list events getting handle failed for app pid %d", - app->key.pid); + app->pid); continue; } - while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle, + while ((ret = ustctl_tracepoint_list_get(app->sock, handle, &uiter)) != -ENOENT) { if (count >= nbmem) { DBG2("Reallocating event list from %zu to %zu entries", nbmem, @@ -1440,7 +1451,7 @@ int ust_app_list_events(struct lttng_event **events) memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); tmp[count].loglevel = uiter.loglevel; tmp[count].type = LTTNG_UST_TRACEPOINT; - tmp[count].pid = app->key.pid; + tmp[count].pid = app->pid; tmp[count].enabled = -1; count++; } @@ -1475,15 +1486,16 @@ void ust_app_clean_list(void) 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); + /* Cleanup socket hash table */ + cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, node, node) { + ret = lttng_ht_del(ust_app_ht_by_sock, &iter); assert(!ret); } + /* Destroy is done only when the ht is empty */ - lttng_ht_destroy(ust_app_sock_key_map); + lttng_ht_destroy(ust_app_ht); + lttng_ht_destroy(ust_app_ht_by_sock); rcu_read_unlock(); } @@ -1494,7 +1506,7 @@ void ust_app_clean_list(void) void ust_app_ht_alloc(void) { ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); - ust_app_sock_key_map = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); + ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); } /* @@ -1522,7 +1534,7 @@ int ust_app_disable_channel_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For every registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { struct lttng_ht_iter uiter; if (!app->compatible) { /* @@ -1583,7 +1595,7 @@ int ust_app_enable_channel_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For every registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -1630,7 +1642,7 @@ int ust_app_disable_event_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For all registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -1649,7 +1661,7 @@ int ust_app_disable_event_glb(struct ltt_ust_session *usess, ua_chan_node = lttng_ht_iter_get_node_str(&uiter); if (ua_chan_node == NULL) { DBG2("Channel %s not found in session id %d for app pid %d." - "Skipping", uchan->name, usess->id, app->key.pid); + "Skipping", uchan->name, usess->id, app->pid); continue; } ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); @@ -1658,7 +1670,7 @@ int ust_app_disable_event_glb(struct ltt_ust_session *usess, ua_event_node = lttng_ht_iter_get_node_str(&uiter); if (ua_event_node == NULL) { DBG2("Event %s not found in channel %s for app pid %d." - "Skipping", uevent->attr.name, uchan->name, app->key.pid); + "Skipping", uevent->attr.name, uchan->name, app->pid); continue; } ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); @@ -1696,7 +1708,7 @@ int ust_app_disable_all_event_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For all registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -1753,7 +1765,7 @@ int ust_app_create_channel_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For every registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -1817,7 +1829,7 @@ int ust_app_enable_event_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For all registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -1841,7 +1853,7 @@ int ust_app_enable_event_glb(struct ltt_ust_session *usess, ua_event_node = lttng_ht_iter_get_node_str(&uiter); if (ua_event_node == NULL) { DBG3("UST app enable event %s not found for app PID %d." - "Skipping app", uevent->attr.name, app->key.pid); + "Skipping app", uevent->attr.name, app->pid); continue; } ua_event = caa_container_of(ua_event_node, struct ust_app_event, node); @@ -1877,7 +1889,7 @@ int ust_app_create_event_glb(struct ltt_ust_session *usess, rcu_read_lock(); /* For all registered applications */ - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -1904,7 +1916,7 @@ int ust_app_create_event_glb(struct ltt_ust_session *usess, break; } DBG2("UST app event %s already exist on app PID %d", - uevent->attr.name, app->key.pid); + uevent->attr.name, app->pid); continue; } } @@ -1926,7 +1938,7 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) struct ltt_ust_stream *ustream; int consumerd_fd; - DBG("Starting tracing for ust app pid %d", app->key.pid); + DBG("Starting tracing for ust app pid %d", app->pid); rcu_read_lock(); @@ -1964,7 +1976,7 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) goto error_rcu_unlock; } - ret = ustctl_create_stream(app->key.sock, ua_chan->obj, + ret = ustctl_create_stream(app->sock, ua_chan->obj, &ustream->obj); if (ret < 0) { /* Got all streams */ @@ -2006,14 +2018,14 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) skip_setup: /* This start the UST tracing */ - ret = ustctl_start_session(app->key.sock, ua_sess->handle); + ret = ustctl_start_session(app->sock, ua_sess->handle); if (ret < 0) { - ERR("Error starting tracing for app pid: %d", app->key.pid); + ERR("Error starting tracing for app pid: %d", app->pid); goto error_rcu_unlock; } /* Quiescent wait after starting trace */ - ustctl_wait_quiescent(app->key.sock); + ustctl_wait_quiescent(app->sock); end: rcu_read_unlock(); @@ -2034,7 +2046,7 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) struct ust_app_session *ua_sess; struct ust_app_channel *ua_chan; - DBG("Stopping tracing for ust app pid %d", app->key.pid); + DBG("Stopping tracing for ust app pid %d", app->pid); rcu_read_lock(); @@ -2056,31 +2068,31 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) assert(ua_sess->started == 1); /* This inhibits UST tracing */ - ret = ustctl_stop_session(app->key.sock, ua_sess->handle); + ret = ustctl_stop_session(app->sock, ua_sess->handle); if (ret < 0) { - ERR("Error stopping tracing for app pid: %d", app->key.pid); + ERR("Error stopping tracing for app pid: %d", app->pid); goto error_rcu_unlock; } /* Quiescent wait after stopping trace */ - ustctl_wait_quiescent(app->key.sock); + ustctl_wait_quiescent(app->sock); /* Flushing buffers */ cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, node.node) { - ret = ustctl_sock_flush_buffer(app->key.sock, ua_chan->obj); + ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj); if (ret < 0) { ERR("UST app PID %d channel %s flush failed with ret %d", - app->key.pid, ua_chan->name, ret); + app->pid, ua_chan->name, ret); /* Continuing flushing all buffers */ continue; } } /* Flush all buffers before stopping */ - ret = ustctl_sock_flush_buffer(app->key.sock, ua_sess->metadata->obj); + ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj); if (ret < 0) { - ERR("UST app PID %d metadata flush failed with ret %d", app->key.pid, + ERR("UST app PID %d metadata flush failed with ret %d", app->pid, ret); } @@ -2104,7 +2116,7 @@ int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) struct lttng_ht_node_ulong *node; int ret; - DBG("Destroy tracing for ust app pid %d", app->key.pid); + DBG("Destroy tracing for ust app pid %d", app->pid); rcu_read_lock(); @@ -2125,12 +2137,12 @@ int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) obj.shm_fd = -1; obj.wait_fd = -1; obj.memory_map_size = 0; - ustctl_release_object(app->key.sock, &obj); + ustctl_release_object(app->sock, &obj); - delete_ust_app_session(app->key.sock, ua_sess); + delete_ust_app_session(app->sock, ua_sess); /* Quiescent wait after stopping trace */ - ustctl_wait_quiescent(app->key.sock); + ustctl_wait_quiescent(app->sock); end: rcu_read_unlock(); @@ -2154,7 +2166,7 @@ int ust_app_start_trace_all(struct ltt_ust_session *usess) rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { ret = ust_app_start_trace(usess, app); if (ret < 0) { /* Continue to next apps even on error */ @@ -2180,7 +2192,7 @@ int ust_app_stop_trace_all(struct ltt_ust_session *usess) rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { ret = ust_app_stop_trace(usess, app); if (ret < 0) { /* Continue to next apps even on error */ @@ -2206,7 +2218,7 @@ int ust_app_destroy_trace_all(struct ltt_ust_session *usess) rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { ret = ust_app_destroy_trace(usess, app); if (ret < 0) { /* Continue to next apps even on error */ @@ -2307,7 +2319,7 @@ void ust_app_global_update(struct ltt_ust_session *usess, int sock) goto error; } - DBG2("UST trace started for app pid %d", app->key.pid); + DBG2("UST trace started for app pid %d", app->pid); } error: @@ -2330,7 +2342,7 @@ int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -2379,7 +2391,7 @@ int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess, rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -2567,7 +2579,7 @@ int ust_app_validate_version(int sock) } DBG2("UST app PID %d is compatible with major version %d " - "(supporting <= %d)", app->key.pid, app->version.major, + "(supporting <= %d)", app->pid, app->version.major, UST_APP_MAJOR_VERSION); app->compatible = 1; rcu_read_unlock(); @@ -2575,7 +2587,7 @@ int ust_app_validate_version(int sock) error: DBG2("UST app PID %d is not compatible with major version %d " - "(supporting <= %d)", app->key.pid, app->version.major, + "(supporting <= %d)", app->pid, app->version.major, UST_APP_MAJOR_VERSION); app->compatible = 0; rcu_read_unlock(); @@ -2593,7 +2605,7 @@ int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) rcu_read_lock(); - cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) { + cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { if (!app->compatible) { /* * TODO: In time, we should notice the caller of this error by @@ -2602,7 +2614,7 @@ int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) continue; } - ret = ustctl_calibrate(app->key.sock, calibrate); + ret = ustctl_calibrate(app->sock, calibrate); if (ret < 0) { switch (ret) { case -ENOSYS: @@ -2612,7 +2624,7 @@ int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) default: /* TODO: Report error to user */ DBG2("Calibrate app PID %d returned with error %d", - app->key.pid, ret); + app->pid, ret); break; } } diff --git a/src/bin/lttng-sessiond/ust-app.h b/src/bin/lttng-sessiond/ust-app.h index 906a6a209..8e9808220 100644 --- a/src/bin/lttng-sessiond/ust-app.h +++ b/src/bin/lttng-sessiond/ust-app.h @@ -45,17 +45,16 @@ struct ust_register_msg { }; /* - * Global applications HT used by the session daemon. + * Global applications HT used by the session daemon. This table is indexed by + * PID using the pid_n node and pid value of an ust_app. */ struct lttng_ht *ust_app_ht; -struct lttng_ht *ust_app_sock_key_map; - -struct ust_app_key { - pid_t pid; - int sock; - struct lttng_ht_node_ulong node; -}; +/* + * Global applications HT used by the session daemon. This table is indexed by + * socket using the sock_n node and sock value of an ust_app. + */ +struct lttng_ht *ust_app_ht_by_sock; struct ust_app_ctx { int handle; @@ -106,6 +105,8 @@ struct ust_app_session { * and a linked list is kept of all running traceable app. */ struct ust_app { + int sock; + pid_t pid; pid_t ppid; uid_t uid; /* User ID that owns the apps */ gid_t gid; /* Group ID that owns the apps */ @@ -118,8 +119,8 @@ struct ust_app { uint32_t v_minor; /* Verion minor number */ char name[17]; /* Process name (short) */ struct lttng_ht *sessions; - struct lttng_ht_node_ulong node; - struct ust_app_key key; + struct lttng_ht_node_ulong pid_n; + struct lttng_ht_node_ulong sock_n; }; #ifdef HAVE_LIBLTTNG_UST_CTL diff --git a/src/common/hashtable/hashtable.h b/src/common/hashtable/hashtable.h index d2d6979ba..f242e75e8 100644 --- a/src/common/hashtable/hashtable.h +++ b/src/common/hashtable/hashtable.h @@ -72,8 +72,8 @@ extern void lttng_ht_add_unique_str(struct lttng_ht *ht, struct lttng_ht_node_str *node); extern void lttng_ht_add_unique_ulong(struct lttng_ht *ht, struct lttng_ht_node_ulong *node); -struct lttng_ht_node_ulong *lttng_ht_add_replace_ulong(struct lttng_ht *ht, - struct lttng_ht_node_ulong *node); +extern struct lttng_ht_node_ulong *lttng_ht_add_replace_ulong( + struct lttng_ht *ht, struct lttng_ht_node_ulong *node); extern int lttng_ht_del(struct lttng_ht *ht, struct lttng_ht_iter *iter); -- 2.34.1