X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fagent.c;h=791538b77ea20a3b4db0294b1c2b985ea630691a;hb=560b51203b85d631023ff1be2b3f110453e8a205;hp=dbeec9dabf5a387a1a78fefd25c866cc61ee1b80;hpb=fefd409b002735b415c5f653cdb2587be454f145;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/agent.c b/src/bin/lttng-sessiond/agent.c index dbeec9dab..791538b77 100644 --- a/src/bin/lttng-sessiond/agent.c +++ b/src/bin/lttng-sessiond/agent.c @@ -124,7 +124,7 @@ static void destroy_event_agent_rcu(struct rcu_head *head) struct agent_event *event = caa_container_of(node, struct agent_event, node); - free(event); + agent_destroy_event(event); } /* @@ -288,8 +288,11 @@ static ssize_t list_events(struct agent_app *app, struct lttng_event **events) for (i = 0; i < nb_event; i++) { offset += len; - strncpy(tmp_events[i].name, reply->payload + offset, - sizeof(tmp_events[i].name)); + if (lttng_strncpy(tmp_events[i].name, reply->payload + offset, + sizeof(tmp_events[i].name))) { + ret = LTTNG_ERR_INVALID; + goto error; + } tmp_events[i].pid = app->pid; tmp_events[i].enabled = -1; len = strlen(reply->payload + offset) + 1; @@ -392,14 +395,17 @@ static int disable_event(struct agent_app *app, struct agent_event *event) app->pid, app->sock->fd); data_size = sizeof(msg); + memset(&msg, 0, sizeof(msg)); + if (lttng_strncpy(msg.name, event->name, sizeof(msg.name))) { + ret = LTTNG_ERR_INVALID; + goto error; + } ret = send_header(app->sock, data_size, AGENT_CMD_DISABLE, 0); if (ret < 0) { 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) { goto error_io; @@ -443,7 +449,7 @@ int agent_send_registration_done(struct agent_app *app) DBG("Agent sending registration done to app socket %d", app->sock->fd); - return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 0); + return send_header(app->sock, 0, AGENT_CMD_REG_DONE, 1); } /* @@ -493,11 +499,14 @@ error: int agent_disable_event(struct agent_event *event, enum lttng_domain_type domain) { - int ret; + int ret = LTTNG_OK; struct agent_app *app; struct lttng_ht_iter iter; assert(event); + if (!event->enabled) { + goto end; + } rcu_read_lock(); @@ -515,10 +524,10 @@ int agent_disable_event(struct agent_event *event, } event->enabled = 0; - ret = LTTNG_OK; error: rcu_read_unlock(); +end: return ret; } @@ -528,7 +537,8 @@ error: * * Return the number of events or else a negative value. */ -int agent_list_events(struct lttng_event **events) +int agent_list_events(struct lttng_event **events, + enum lttng_domain_type domain) { int ret; size_t nbmem, count = 0; @@ -552,6 +562,11 @@ int agent_list_events(struct lttng_event **events) ssize_t nb_ev; struct lttng_event *agent_events; + /* Skip domain not asked by the list. */ + if (app->domain != domain) { + continue; + } + nb_ev = list_events(app, &agent_events); if (nb_ev < 0) { ret = nb_ev; @@ -671,6 +686,8 @@ void agent_add_app(struct agent_app *app) /* * Delete agent application from the global hash table. + * + * rcu_read_lock() must be held by the caller. */ void agent_delete_app(struct agent_app *app) { @@ -682,9 +699,7 @@ void agent_delete_app(struct agent_app *app) DBG3("Agent deleting app pid: %d and sock: %d", app->pid, app->sock->fd); iter.iter.node = &app->node.node; - rcu_read_lock(); ret = lttng_ht_del(agent_apps_ht_by_sock, &iter); - rcu_read_unlock(); assert(!ret); } @@ -763,6 +778,7 @@ struct agent *agent_create(enum lttng_domain_type domain) ret = agent_init(agt); if (ret < 0) { free(agt); + agt = NULL; goto error; } @@ -890,7 +906,7 @@ struct agent_event *agent_find_event(const char *name, int loglevel, return caa_container_of(node, struct agent_event, node); error: - DBG3("Agent NOT found %s.", name); + DBG3("Agent event NOT found %s.", name); return NULL; } @@ -903,12 +919,13 @@ void agent_destroy_event(struct agent_event *event) { assert(event); + free(event->filter); + free(event->filter_expression); free(event); } /* - * Destroy an agent completely. Note that the given pointer is NOT freed - * thus a reference to static or stack data can be passed to this function. + * Destroy an agent completely. */ void agent_destroy(struct agent *agt) { @@ -946,20 +963,72 @@ void agent_destroy(struct agent *agt) } rcu_read_unlock(); - lttng_ht_destroy(agt->events); + ht_cleanup_push(agt->events); + free(agt); } /* - * Initialize agent subsystem. + * Allocate agent_apps_ht_by_sock. */ -int agent_setup(void) +int agent_app_ht_alloc(void) { + int ret = 0; + agent_apps_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); if (!agent_apps_ht_by_sock) { - return -1; + ret = -1; } - return 0; + return ret; +} + +/* + * Destroy a agent application by socket. + */ +void agent_destroy_app_by_sock(int sock) +{ + struct agent_app *app; + + assert(sock >= 0); + + /* + * Not finding an application is a very important error that should NEVER + * happen. The hash table deletion is ONLY done through this call when the + * main sessiond thread is torn down. + */ + rcu_read_lock(); + app = agent_find_app_by_sock(sock); + assert(app); + + /* RCU read side lock is assumed to be held by this function. */ + agent_delete_app(app); + + /* The application is freed in a RCU call but the socket is closed here. */ + agent_destroy_app(app); + rcu_read_unlock(); +} + +/* + * Clean-up the agent app hash table and destroy it. + */ +void agent_app_ht_clean(void) +{ + struct lttng_ht_node_ulong *node; + struct lttng_ht_iter iter; + + if (!agent_apps_ht_by_sock) { + return; + } + rcu_read_lock(); + cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) { + struct agent_app *app; + + app = caa_container_of(node, struct agent_app, node); + agent_destroy_app_by_sock(app->sock->fd); + } + rcu_read_unlock(); + + lttng_ht_destroy(agent_apps_ht_by_sock); } /*