Fix: ust: app stuck on recv message during UST comm timeout scenario
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
CommitLineData
91d76f53
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
91d76f53 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
91d76f53
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
91d76f53
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
91d76f53 20#include <errno.h>
7972aab2 21#include <inttypes.h>
91d76f53
DG
22#include <pthread.h>
23#include <stdio.h>
24#include <stdlib.h>
099e26bd 25#include <string.h>
aba8e916
DG
26#include <sys/stat.h>
27#include <sys/types.h>
099e26bd 28#include <unistd.h>
0df502fd 29#include <urcu/compiler.h>
331744e3 30#include <signal.h>
bec39940 31
990570ed 32#include <common/common.h>
86acf0da 33#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 34
7972aab2 35#include "buffer-registry.h"
86acf0da 36#include "fd-limit.h"
8782cc74 37#include "health-sessiond.h"
56fff090 38#include "ust-app.h"
48842b30 39#include "ust-consumer.h"
b7340b1d
JG
40#include "lttng-ust-ctl.h"
41#include "lttng-ust-error.h"
0b2dc8df 42#include "utils.h"
fb83fe64 43#include "session.h"
e9404c27
JG
44#include "lttng-sessiond.h"
45#include "notification-thread-commands.h"
5c408ad8 46#include "rotate.h"
d80a6244 47
e1d64b5c
MJ
48struct lttng_ht *ust_app_ht;
49struct lttng_ht *ust_app_ht_by_sock;
50struct lttng_ht *ust_app_ht_by_notify_sock;
51
c4b88406
MD
52static
53int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
54
d9bf3ca4
MD
55/* Next available channel key. Access under next_channel_key_lock. */
56static uint64_t _next_channel_key;
57static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
58
59/* Next available session ID. Access under next_session_id_lock. */
60static uint64_t _next_session_id;
61static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
ffe60014
DG
62
63/*
d9bf3ca4 64 * Return the incremented value of next_channel_key.
ffe60014 65 */
d9bf3ca4 66static uint64_t get_next_channel_key(void)
ffe60014 67{
d9bf3ca4
MD
68 uint64_t ret;
69
70 pthread_mutex_lock(&next_channel_key_lock);
71 ret = ++_next_channel_key;
72 pthread_mutex_unlock(&next_channel_key_lock);
73 return ret;
ffe60014
DG
74}
75
76/*
7972aab2 77 * Return the atomically incremented value of next_session_id.
ffe60014 78 */
d9bf3ca4 79static uint64_t get_next_session_id(void)
ffe60014 80{
d9bf3ca4
MD
81 uint64_t ret;
82
83 pthread_mutex_lock(&next_session_id_lock);
84 ret = ++_next_session_id;
85 pthread_mutex_unlock(&next_session_id_lock);
86 return ret;
ffe60014
DG
87}
88
d65d2de8
DG
89static void copy_channel_attr_to_ustctl(
90 struct ustctl_consumer_channel_attr *attr,
91 struct lttng_ust_channel_attr *uattr)
92{
93 /* Copy event attributes since the layout is different. */
94 attr->subbuf_size = uattr->subbuf_size;
95 attr->num_subbuf = uattr->num_subbuf;
96 attr->overwrite = uattr->overwrite;
97 attr->switch_timer_interval = uattr->switch_timer_interval;
98 attr->read_timer_interval = uattr->read_timer_interval;
99 attr->output = uattr->output;
491d1539 100 attr->blocking_timeout = uattr->u.s.blocking_timeout;
d65d2de8
DG
101}
102
025faf73
DG
103/*
104 * Match function for the hash table lookup.
105 *
106 * It matches an ust app event based on three attributes which are the event
107 * name, the filter bytecode and the loglevel.
108 */
18eace3b
DG
109static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
110{
111 struct ust_app_event *event;
112 const struct ust_app_ht_key *key;
2106efa0 113 int ev_loglevel_value;
18eace3b
DG
114
115 assert(node);
116 assert(_key);
117
118 event = caa_container_of(node, struct ust_app_event, node.node);
119 key = _key;
2106efa0 120 ev_loglevel_value = event->attr.loglevel;
18eace3b 121
1af53eb5 122 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
18eace3b
DG
123
124 /* Event name */
125 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
126 goto no_match;
127 }
128
129 /* Event loglevel. */
2106efa0 130 if (ev_loglevel_value != key->loglevel_type) {
025faf73 131 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
2106efa0
PP
132 && key->loglevel_type == 0 &&
133 ev_loglevel_value == -1) {
025faf73
DG
134 /*
135 * Match is accepted. This is because on event creation, the
136 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
137 * -1 are accepted for this loglevel type since 0 is the one set by
138 * the API when receiving an enable event.
139 */
140 } else {
141 goto no_match;
142 }
18eace3b
DG
143 }
144
145 /* One of the filters is NULL, fail. */
146 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
147 goto no_match;
148 }
149
025faf73
DG
150 if (key->filter && event->filter) {
151 /* Both filters exists, check length followed by the bytecode. */
152 if (event->filter->len != key->filter->len ||
153 memcmp(event->filter->data, key->filter->data,
154 event->filter->len) != 0) {
155 goto no_match;
156 }
18eace3b
DG
157 }
158
1af53eb5
JI
159 /* One of the exclusions is NULL, fail. */
160 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
161 goto no_match;
162 }
163
164 if (key->exclusion && event->exclusion) {
165 /* Both exclusions exists, check count followed by the names. */
166 if (event->exclusion->count != key->exclusion->count ||
167 memcmp(event->exclusion->names, key->exclusion->names,
168 event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) {
169 goto no_match;
170 }
171 }
172
173
025faf73 174 /* Match. */
18eace3b
DG
175 return 1;
176
177no_match:
178 return 0;
18eace3b
DG
179}
180
025faf73
DG
181/*
182 * Unique add of an ust app event in the given ht. This uses the custom
183 * ht_match_ust_app_event match function and the event name as hash.
184 */
d0b96690 185static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
18eace3b
DG
186 struct ust_app_event *event)
187{
188 struct cds_lfht_node *node_ptr;
189 struct ust_app_ht_key key;
d0b96690 190 struct lttng_ht *ht;
18eace3b 191
d0b96690
DG
192 assert(ua_chan);
193 assert(ua_chan->events);
18eace3b
DG
194 assert(event);
195
d0b96690 196 ht = ua_chan->events;
18eace3b
DG
197 key.name = event->attr.name;
198 key.filter = event->filter;
2106efa0 199 key.loglevel_type = event->attr.loglevel;
91c89f23 200 key.exclusion = event->exclusion;
18eace3b
DG
201
202 node_ptr = cds_lfht_add_unique(ht->ht,
203 ht->hash_fct(event->node.key, lttng_ht_seed),
204 ht_match_ust_app_event, &key, &event->node.node);
205 assert(node_ptr == &event->node.node);
206}
207
d88aee68
DG
208/*
209 * Close the notify socket from the given RCU head object. This MUST be called
210 * through a call_rcu().
211 */
212static void close_notify_sock_rcu(struct rcu_head *head)
213{
214 int ret;
215 struct ust_app_notify_sock_obj *obj =
216 caa_container_of(head, struct ust_app_notify_sock_obj, head);
217
218 /* Must have a valid fd here. */
219 assert(obj->fd >= 0);
220
221 ret = close(obj->fd);
222 if (ret) {
223 ERR("close notify sock %d RCU", obj->fd);
224 }
225 lttng_fd_put(LTTNG_FD_APPS, 1);
226
227 free(obj);
228}
229
7972aab2
DG
230/*
231 * Return the session registry according to the buffer type of the given
232 * session.
233 *
234 * A registry per UID object MUST exists before calling this function or else
235 * it assert() if not found. RCU read side lock must be acquired.
236 */
237static struct ust_registry_session *get_session_registry(
238 struct ust_app_session *ua_sess)
239{
240 struct ust_registry_session *registry = NULL;
241
242 assert(ua_sess);
243
244 switch (ua_sess->buffer_type) {
245 case LTTNG_BUFFER_PER_PID:
246 {
247 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
248 if (!reg_pid) {
249 goto error;
250 }
251 registry = reg_pid->registry->reg.ust;
252 break;
253 }
254 case LTTNG_BUFFER_PER_UID:
255 {
256 struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
c51311d6
JG
257 ua_sess->tracing_id, ua_sess->bits_per_long,
258 ua_sess->real_credentials.uid);
7972aab2
DG
259 if (!reg_uid) {
260 goto error;
261 }
262 registry = reg_uid->registry->reg.ust;
263 break;
264 }
265 default:
266 assert(0);
267 };
268
269error:
270 return registry;
271}
272
55cc08a6
DG
273/*
274 * Delete ust context safely. RCU read lock must be held before calling
275 * this function.
276 */
277static
fb45065e
MD
278void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx,
279 struct ust_app *app)
55cc08a6 280{
ffe60014
DG
281 int ret;
282
283 assert(ua_ctx);
284
55cc08a6 285 if (ua_ctx->obj) {
fb45065e 286 pthread_mutex_lock(&app->sock_lock);
ffe60014 287 ret = ustctl_release_object(sock, ua_ctx->obj);
fb45065e 288 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
289 if (ret < 0) {
290 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
291 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
292 app->pid, app->sock);
293 } else if (ret == -EAGAIN) {
294 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
295 app->pid, app->sock);
296 } else {
297 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
298 ua_ctx->obj->handle, ret,
299 app->pid, app->sock);
300 }
ffe60014 301 }
55cc08a6
DG
302 free(ua_ctx->obj);
303 }
304 free(ua_ctx);
305}
306
d80a6244
DG
307/*
308 * Delete ust app event safely. RCU read lock must be held before calling
309 * this function.
310 */
8b366481 311static
fb45065e
MD
312void delete_ust_app_event(int sock, struct ust_app_event *ua_event,
313 struct ust_app *app)
d80a6244 314{
ffe60014
DG
315 int ret;
316
317 assert(ua_event);
318
53a80697 319 free(ua_event->filter);
951f0b71
JI
320 if (ua_event->exclusion != NULL)
321 free(ua_event->exclusion);
edb67388 322 if (ua_event->obj != NULL) {
fb45065e 323 pthread_mutex_lock(&app->sock_lock);
ffe60014 324 ret = ustctl_release_object(sock, ua_event->obj);
fb45065e 325 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
326 if (ret < 0) {
327 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
328 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
329 app->pid, app->sock);
330 } else if (ret == -EAGAIN) {
331 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
332 app->pid, app->sock);
333 } else {
334 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
335 ret, app->pid, app->sock);
336 }
ffe60014 337 }
edb67388
DG
338 free(ua_event->obj);
339 }
d80a6244
DG
340 free(ua_event);
341}
342
343/*
7972aab2
DG
344 * Release ust data object of the given stream.
345 *
346 * Return 0 on success or else a negative value.
d80a6244 347 */
fb45065e
MD
348static int release_ust_app_stream(int sock, struct ust_app_stream *stream,
349 struct ust_app *app)
d80a6244 350{
7972aab2 351 int ret = 0;
ffe60014
DG
352
353 assert(stream);
354
8b366481 355 if (stream->obj) {
fb45065e 356 pthread_mutex_lock(&app->sock_lock);
ffe60014 357 ret = ustctl_release_object(sock, stream->obj);
fb45065e 358 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
359 if (ret < 0) {
360 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
361 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
362 app->pid, app->sock);
363 } else if (ret == -EAGAIN) {
364 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
365 app->pid, app->sock);
366 } else {
367 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
368 ret, app->pid, app->sock);
369 }
ffe60014 370 }
4063050c 371 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
372 free(stream->obj);
373 }
7972aab2
DG
374
375 return ret;
376}
377
378/*
379 * Delete ust app stream safely. RCU read lock must be held before calling
380 * this function.
381 */
382static
fb45065e
MD
383void delete_ust_app_stream(int sock, struct ust_app_stream *stream,
384 struct ust_app *app)
7972aab2
DG
385{
386 assert(stream);
387
fb45065e 388 (void) release_ust_app_stream(sock, stream, app);
84cd17c6 389 free(stream);
d80a6244
DG
390}
391
36b588ed
MD
392/*
393 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
394 * section and outside of call_rcu thread, so we postpone its execution
395 * using ht_cleanup_push. It is simpler than to change the semantic of
396 * the many callers of delete_ust_app_session().
36b588ed
MD
397 */
398static
399void delete_ust_app_channel_rcu(struct rcu_head *head)
400{
401 struct ust_app_channel *ua_chan =
402 caa_container_of(head, struct ust_app_channel, rcu_head);
403
0b2dc8df
MD
404 ht_cleanup_push(ua_chan->ctx);
405 ht_cleanup_push(ua_chan->events);
36b588ed
MD
406 free(ua_chan);
407}
408
fb83fe64
JD
409/*
410 * Extract the lost packet or discarded events counter when the channel is
411 * being deleted and store the value in the parent channel so we can
412 * access it from lttng list and at stop/destroy.
82cac6d2
JG
413 *
414 * The session list lock must be held by the caller.
fb83fe64
JD
415 */
416static
417void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
418{
419 uint64_t discarded = 0, lost = 0;
420 struct ltt_session *session;
421 struct ltt_ust_channel *uchan;
422
423 if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) {
424 return;
425 }
426
427 rcu_read_lock();
428 session = session_find_by_id(ua_chan->session->tracing_id);
d68ec974
JG
429 if (!session || !session->ust_session) {
430 /*
431 * Not finding the session is not an error because there are
432 * multiple ways the channels can be torn down.
433 *
434 * 1) The session daemon can initiate the destruction of the
435 * ust app session after receiving a destroy command or
436 * during its shutdown/teardown.
437 * 2) The application, since we are in per-pid tracing, is
438 * unregistering and tearing down its ust app session.
439 *
440 * Both paths are protected by the session list lock which
441 * ensures that the accounting of lost packets and discarded
442 * events is done exactly once. The session is then unpublished
443 * from the session list, resulting in this condition.
444 */
fb83fe64
JD
445 goto end;
446 }
447
448 if (ua_chan->attr.overwrite) {
449 consumer_get_lost_packets(ua_chan->session->tracing_id,
450 ua_chan->key, session->ust_session->consumer,
451 &lost);
452 } else {
453 consumer_get_discarded_events(ua_chan->session->tracing_id,
454 ua_chan->key, session->ust_session->consumer,
455 &discarded);
456 }
457 uchan = trace_ust_find_channel_by_name(
458 session->ust_session->domain_global.channels,
459 ua_chan->name);
460 if (!uchan) {
461 ERR("Missing UST channel to store discarded counters");
462 goto end;
463 }
464
465 uchan->per_pid_closed_app_discarded += discarded;
466 uchan->per_pid_closed_app_lost += lost;
467
468end:
469 rcu_read_unlock();
48a86f68
JG
470 if (session) {
471 session_put(session);
472 }
fb83fe64
JD
473}
474
d80a6244
DG
475/*
476 * Delete ust app channel safely. RCU read lock must be held before calling
477 * this function.
82cac6d2
JG
478 *
479 * The session list lock must be held by the caller.
d80a6244 480 */
8b366481 481static
d0b96690
DG
482void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
483 struct ust_app *app)
d80a6244
DG
484{
485 int ret;
bec39940 486 struct lttng_ht_iter iter;
d80a6244 487 struct ust_app_event *ua_event;
55cc08a6 488 struct ust_app_ctx *ua_ctx;
030a66fa 489 struct ust_app_stream *stream, *stmp;
7972aab2 490 struct ust_registry_session *registry;
d80a6244 491
ffe60014
DG
492 assert(ua_chan);
493
494 DBG3("UST app deleting channel %s", ua_chan->name);
495
55cc08a6 496 /* Wipe stream */
d80a6244 497 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 498 cds_list_del(&stream->list);
fb45065e 499 delete_ust_app_stream(sock, stream, app);
d80a6244
DG
500 }
501
55cc08a6 502 /* Wipe context */
bec39940 503 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
31746f93 504 cds_list_del(&ua_ctx->list);
bec39940 505 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6 506 assert(!ret);
fb45065e 507 delete_ust_app_ctx(sock, ua_ctx, app);
55cc08a6 508 }
d80a6244 509
55cc08a6 510 /* Wipe events */
bec39940
DG
511 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
512 node.node) {
513 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 514 assert(!ret);
fb45065e 515 delete_ust_app_event(sock, ua_event, app);
d80a6244 516 }
edb67388 517
c8335706
MD
518 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
519 /* Wipe and free registry from session registry. */
520 registry = get_session_registry(ua_chan->session);
521 if (registry) {
e9404c27 522 ust_registry_channel_del_free(registry, ua_chan->key,
8a5340bc
MD
523 sock >= 0);
524 }
c1cd9f61
JG
525 /*
526 * A negative socket can be used by the caller when
527 * cleaning-up a ua_chan in an error path. Skip the
528 * accounting in this case.
529 */
8a5340bc
MD
530 if (sock >= 0) {
531 save_per_pid_lost_discarded_counters(ua_chan);
c8335706 532 }
7972aab2 533 }
d0b96690 534
edb67388 535 if (ua_chan->obj != NULL) {
d0b96690
DG
536 /* Remove channel from application UST object descriptor. */
537 iter.iter.node = &ua_chan->ust_objd_node.node;
c6e62271
DG
538 ret = lttng_ht_del(app->ust_objd, &iter);
539 assert(!ret);
fb45065e 540 pthread_mutex_lock(&app->sock_lock);
ffe60014 541 ret = ustctl_release_object(sock, ua_chan->obj);
fb45065e 542 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
543 if (ret < 0) {
544 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
545 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
546 ua_chan->name, app->pid,
547 app->sock);
548 } else if (ret == -EAGAIN) {
549 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
550 ua_chan->name, app->pid,
551 app->sock);
552 } else {
553 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
554 ua_chan->name, ret, app->pid,
555 app->sock);
556 }
ffe60014 557 }
7972aab2 558 lttng_fd_put(LTTNG_FD_APPS, 1);
edb67388
DG
559 free(ua_chan->obj);
560 }
36b588ed 561 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
d80a6244
DG
562}
563
fb45065e
MD
564int ust_app_register_done(struct ust_app *app)
565{
566 int ret;
567
568 pthread_mutex_lock(&app->sock_lock);
569 ret = ustctl_register_done(app->sock);
570 pthread_mutex_unlock(&app->sock_lock);
571 return ret;
572}
573
574int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data)
575{
576 int ret, sock;
577
578 if (app) {
579 pthread_mutex_lock(&app->sock_lock);
580 sock = app->sock;
581 } else {
582 sock = -1;
583 }
584 ret = ustctl_release_object(sock, data);
585 if (app) {
586 pthread_mutex_unlock(&app->sock_lock);
587 }
588 return ret;
589}
590
331744e3 591/*
1b532a60
DG
592 * Push metadata to consumer socket.
593 *
dc2bbdae
MD
594 * RCU read-side lock must be held to guarantee existance of socket.
595 * Must be called with the ust app session lock held.
596 * Must be called with the registry lock held.
331744e3
JD
597 *
598 * On success, return the len of metadata pushed or else a negative value.
2c57e06d
MD
599 * Returning a -EPIPE return value means we could not send the metadata,
600 * but it can be caused by recoverable errors (e.g. the application has
601 * terminated concurrently).
331744e3
JD
602 */
603ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
604 struct consumer_socket *socket, int send_zero_data)
605{
606 int ret;
607 char *metadata_str = NULL;
c585821b 608 size_t len, offset, new_metadata_len_sent;
331744e3 609 ssize_t ret_val;
93ec662e 610 uint64_t metadata_key, metadata_version;
331744e3
JD
611
612 assert(registry);
613 assert(socket);
1b532a60 614
c585821b
MD
615 metadata_key = registry->metadata_key;
616
ce34fcd0 617 /*
dc2bbdae
MD
618 * Means that no metadata was assigned to the session. This can
619 * happens if no start has been done previously.
ce34fcd0 620 */
c585821b 621 if (!metadata_key) {
ce34fcd0
MD
622 return 0;
623 }
624
331744e3
JD
625 offset = registry->metadata_len_sent;
626 len = registry->metadata_len - registry->metadata_len_sent;
c585821b 627 new_metadata_len_sent = registry->metadata_len;
93ec662e 628 metadata_version = registry->metadata_version;
331744e3
JD
629 if (len == 0) {
630 DBG3("No metadata to push for metadata key %" PRIu64,
631 registry->metadata_key);
632 ret_val = len;
633 if (send_zero_data) {
634 DBG("No metadata to push");
635 goto push_data;
636 }
637 goto end;
638 }
639
640 /* Allocate only what we have to send. */
641 metadata_str = zmalloc(len);
642 if (!metadata_str) {
643 PERROR("zmalloc ust app metadata string");
644 ret_val = -ENOMEM;
645 goto error;
646 }
c585821b 647 /* Copy what we haven't sent out. */
331744e3 648 memcpy(metadata_str, registry->metadata + offset, len);
331744e3
JD
649
650push_data:
c585821b
MD
651 pthread_mutex_unlock(&registry->lock);
652 /*
653 * We need to unlock the registry while we push metadata to
654 * break a circular dependency between the consumerd metadata
655 * lock and the sessiond registry lock. Indeed, pushing metadata
656 * to the consumerd awaits that it gets pushed all the way to
657 * relayd, but doing so requires grabbing the metadata lock. If
658 * a concurrent metadata request is being performed by
659 * consumerd, this can try to grab the registry lock on the
660 * sessiond while holding the metadata lock on the consumer
661 * daemon. Those push and pull schemes are performed on two
662 * different bidirectionnal communication sockets.
663 */
664 ret = consumer_push_metadata(socket, metadata_key,
93ec662e 665 metadata_str, len, offset, metadata_version);
c585821b 666 pthread_mutex_lock(&registry->lock);
331744e3 667 if (ret < 0) {
000baf6a 668 /*
dc2bbdae
MD
669 * There is an acceptable race here between the registry
670 * metadata key assignment and the creation on the
671 * consumer. The session daemon can concurrently push
672 * metadata for this registry while being created on the
673 * consumer since the metadata key of the registry is
674 * assigned *before* it is setup to avoid the consumer
675 * to ask for metadata that could possibly be not found
676 * in the session daemon.
000baf6a 677 *
dc2bbdae
MD
678 * The metadata will get pushed either by the session
679 * being stopped or the consumer requesting metadata if
680 * that race is triggered.
000baf6a
DG
681 */
682 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
683 ret = 0;
c585821b
MD
684 } else {
685 ERR("Error pushing metadata to consumer");
000baf6a 686 }
331744e3
JD
687 ret_val = ret;
688 goto error_push;
c585821b
MD
689 } else {
690 /*
691 * Metadata may have been concurrently pushed, since
692 * we're not holding the registry lock while pushing to
693 * consumer. This is handled by the fact that we send
694 * the metadata content, size, and the offset at which
695 * that metadata belongs. This may arrive out of order
696 * on the consumer side, and the consumer is able to
697 * deal with overlapping fragments. The consumer
698 * supports overlapping fragments, which must be
699 * contiguous starting from offset 0. We keep the
700 * largest metadata_len_sent value of the concurrent
701 * send.
702 */
703 registry->metadata_len_sent =
704 max_t(size_t, registry->metadata_len_sent,
705 new_metadata_len_sent);
331744e3 706 }
331744e3
JD
707 free(metadata_str);
708 return len;
709
710end:
711error:
ce34fcd0
MD
712 if (ret_val) {
713 /*
dc2bbdae
MD
714 * On error, flag the registry that the metadata is
715 * closed. We were unable to push anything and this
716 * means that either the consumer is not responding or
717 * the metadata cache has been destroyed on the
718 * consumer.
ce34fcd0
MD
719 */
720 registry->metadata_closed = 1;
721 }
331744e3
JD
722error_push:
723 free(metadata_str);
724 return ret_val;
725}
726
d88aee68 727/*
ce34fcd0 728 * For a given application and session, push metadata to consumer.
331744e3
JD
729 * Either sock or consumer is required : if sock is NULL, the default
730 * socket to send the metadata is retrieved from consumer, if sock
731 * is not NULL we use it to send the metadata.
ce34fcd0 732 * RCU read-side lock must be held while calling this function,
dc2bbdae
MD
733 * therefore ensuring existance of registry. It also ensures existance
734 * of socket throughout this function.
d88aee68
DG
735 *
736 * Return 0 on success else a negative error.
2c57e06d
MD
737 * Returning a -EPIPE return value means we could not send the metadata,
738 * but it can be caused by recoverable errors (e.g. the application has
739 * terminated concurrently).
d88aee68 740 */
7972aab2
DG
741static int push_metadata(struct ust_registry_session *registry,
742 struct consumer_output *consumer)
d88aee68 743{
331744e3
JD
744 int ret_val;
745 ssize_t ret;
d88aee68
DG
746 struct consumer_socket *socket;
747
7972aab2
DG
748 assert(registry);
749 assert(consumer);
750
ce34fcd0 751 pthread_mutex_lock(&registry->lock);
ce34fcd0 752 if (registry->metadata_closed) {
dc2bbdae
MD
753 ret_val = -EPIPE;
754 goto error;
d88aee68
DG
755 }
756
d88aee68 757 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
758 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
759 consumer);
d88aee68 760 if (!socket) {
331744e3 761 ret_val = -1;
ce34fcd0 762 goto error;
d88aee68
DG
763 }
764
331744e3 765 ret = ust_app_push_metadata(registry, socket, 0);
d88aee68 766 if (ret < 0) {
331744e3 767 ret_val = ret;
ce34fcd0 768 goto error;
d88aee68 769 }
dc2bbdae 770 pthread_mutex_unlock(&registry->lock);
d88aee68
DG
771 return 0;
772
ce34fcd0 773error:
dc2bbdae 774 pthread_mutex_unlock(&registry->lock);
331744e3 775 return ret_val;
d88aee68
DG
776}
777
778/*
779 * Send to the consumer a close metadata command for the given session. Once
780 * done, the metadata channel is deleted and the session metadata pointer is
dc2bbdae 781 * nullified. The session lock MUST be held unless the application is
d88aee68
DG
782 * in the destroy path.
783 *
7ca90d1f
MD
784 * Do not hold the registry lock while communicating with the consumerd, because
785 * doing so causes inter-process deadlocks between consumerd and sessiond with
786 * the metadata request notification.
787 *
d88aee68
DG
788 * Return 0 on success else a negative value.
789 */
7972aab2
DG
790static int close_metadata(struct ust_registry_session *registry,
791 struct consumer_output *consumer)
d88aee68
DG
792{
793 int ret;
794 struct consumer_socket *socket;
7ca90d1f
MD
795 uint64_t metadata_key;
796 bool registry_was_already_closed;
d88aee68 797
7972aab2
DG
798 assert(registry);
799 assert(consumer);
d88aee68 800
7972aab2
DG
801 rcu_read_lock();
802
ce34fcd0 803 pthread_mutex_lock(&registry->lock);
7ca90d1f
MD
804 metadata_key = registry->metadata_key;
805 registry_was_already_closed = registry->metadata_closed;
806 if (metadata_key != 0) {
807 /*
808 * Metadata closed. Even on error this means that the consumer
809 * is not responding or not found so either way a second close
810 * should NOT be emit for this registry.
811 */
812 registry->metadata_closed = 1;
813 }
814 pthread_mutex_unlock(&registry->lock);
ce34fcd0 815
7ca90d1f 816 if (metadata_key == 0 || registry_was_already_closed) {
d88aee68 817 ret = 0;
1b532a60 818 goto end;
d88aee68
DG
819 }
820
d88aee68 821 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
822 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
823 consumer);
d88aee68
DG
824 if (!socket) {
825 ret = -1;
7ca90d1f 826 goto end;
d88aee68
DG
827 }
828
7ca90d1f 829 ret = consumer_close_metadata(socket, metadata_key);
d88aee68 830 if (ret < 0) {
7ca90d1f 831 goto end;
d88aee68
DG
832 }
833
1b532a60 834end:
7972aab2 835 rcu_read_unlock();
d88aee68
DG
836 return ret;
837}
838
36b588ed
MD
839/*
840 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
841 * section and outside of call_rcu thread, so we postpone its execution
842 * using ht_cleanup_push. It is simpler than to change the semantic of
843 * the many callers of delete_ust_app_session().
36b588ed
MD
844 */
845static
846void delete_ust_app_session_rcu(struct rcu_head *head)
847{
848 struct ust_app_session *ua_sess =
849 caa_container_of(head, struct ust_app_session, rcu_head);
850
0b2dc8df 851 ht_cleanup_push(ua_sess->channels);
36b588ed
MD
852 free(ua_sess);
853}
854
d80a6244
DG
855/*
856 * Delete ust app session safely. RCU read lock must be held before calling
857 * this function.
82cac6d2
JG
858 *
859 * The session list lock must be held by the caller.
d80a6244 860 */
8b366481 861static
d0b96690
DG
862void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
863 struct ust_app *app)
d80a6244
DG
864{
865 int ret;
bec39940 866 struct lttng_ht_iter iter;
d80a6244 867 struct ust_app_channel *ua_chan;
7972aab2 868 struct ust_registry_session *registry;
d80a6244 869
d88aee68
DG
870 assert(ua_sess);
871
1b532a60
DG
872 pthread_mutex_lock(&ua_sess->lock);
873
b161602a
MD
874 assert(!ua_sess->deleted);
875 ua_sess->deleted = true;
876
7972aab2 877 registry = get_session_registry(ua_sess);
fad1ed2f 878 /* Registry can be null on error path during initialization. */
ce34fcd0 879 if (registry) {
d88aee68 880 /* Push metadata for application before freeing the application. */
7972aab2 881 (void) push_metadata(registry, ua_sess->consumer);
d88aee68 882
7972aab2
DG
883 /*
884 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
885 * metadata only on destroy trace session in this case. Also, the
886 * previous push metadata could have flag the metadata registry to
887 * close so don't send a close command if closed.
7972aab2 888 */
ce34fcd0 889 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
890 /* And ask to close it for this session registry. */
891 (void) close_metadata(registry, ua_sess->consumer);
892 }
d80a6244
DG
893 }
894
bec39940
DG
895 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
896 node.node) {
897 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 898 assert(!ret);
d0b96690 899 delete_ust_app_channel(sock, ua_chan, app);
d80a6244 900 }
d80a6244 901
7972aab2
DG
902 /* In case of per PID, the registry is kept in the session. */
903 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
904 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
905 if (reg_pid) {
fad1ed2f
JR
906 /*
907 * Registry can be null on error path during
908 * initialization.
909 */
7972aab2
DG
910 buffer_reg_pid_remove(reg_pid);
911 buffer_reg_pid_destroy(reg_pid);
912 }
913 }
d0b96690 914
aee6bafd 915 if (ua_sess->handle != -1) {
fb45065e 916 pthread_mutex_lock(&app->sock_lock);
ffe60014 917 ret = ustctl_release_handle(sock, ua_sess->handle);
fb45065e 918 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
919 if (ret < 0) {
920 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
921 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
922 app->pid, app->sock);
923 } else if (ret == -EAGAIN) {
924 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
925 app->pid, app->sock);
926 } else {
927 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
928 ret, app->pid, app->sock);
929 }
ffe60014 930 }
ee3f5169 931
10b56aef
MD
932 /* Remove session from application UST object descriptor. */
933 iter.iter.node = &ua_sess->ust_objd_node.node;
934 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
935 assert(!ret);
aee6bafd 936 }
10b56aef 937
1b532a60
DG
938 pthread_mutex_unlock(&ua_sess->lock);
939
6addfa37
MD
940 consumer_output_put(ua_sess->consumer);
941
36b588ed 942 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
d80a6244 943}
91d76f53
DG
944
945/*
284d8f55
DG
946 * Delete a traceable application structure from the global list. Never call
947 * this function outside of a call_rcu call.
36b588ed
MD
948 *
949 * RCU read side lock should _NOT_ be held when calling this function.
91d76f53 950 */
8b366481
DG
951static
952void delete_ust_app(struct ust_app *app)
91d76f53 953{
8b366481 954 int ret, sock;
d42f20df 955 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 956
82cac6d2
JG
957 /*
958 * The session list lock must be held during this function to guarantee
959 * the existence of ua_sess.
960 */
961 session_lock_list();
d80a6244 962 /* Delete ust app sessions info */
852d0037
DG
963 sock = app->sock;
964 app->sock = -1;
d80a6244 965
8b366481 966 /* Wipe sessions */
d42f20df
DG
967 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
968 teardown_node) {
969 /* Free every object in the session and the session. */
36b588ed 970 rcu_read_lock();
d0b96690 971 delete_ust_app_session(sock, ua_sess, app);
36b588ed 972 rcu_read_unlock();
d80a6244 973 }
36b588ed 974
0b2dc8df 975 ht_cleanup_push(app->sessions);
10b56aef 976 ht_cleanup_push(app->ust_sessions_objd);
0b2dc8df 977 ht_cleanup_push(app->ust_objd);
d80a6244 978
6414a713 979 /*
852d0037
DG
980 * Wait until we have deleted the application from the sock hash table
981 * before closing this socket, otherwise an application could re-use the
982 * socket ID and race with the teardown, using the same hash table entry.
983 *
984 * It's OK to leave the close in call_rcu. We want it to stay unique for
985 * all RCU readers that could run concurrently with unregister app,
986 * therefore we _need_ to only close that socket after a grace period. So
987 * it should stay in this RCU callback.
988 *
989 * This close() is a very important step of the synchronization model so
990 * every modification to this function must be carefully reviewed.
6414a713 991 */
799e2c4f
MD
992 ret = close(sock);
993 if (ret) {
994 PERROR("close");
995 }
4063050c 996 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 997
852d0037 998 DBG2("UST app pid %d deleted", app->pid);
284d8f55 999 free(app);
82cac6d2 1000 session_unlock_list();
099e26bd
DG
1001}
1002
1003/*
f6a9efaa 1004 * URCU intermediate call to delete an UST app.
099e26bd 1005 */
8b366481
DG
1006static
1007void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 1008{
bec39940
DG
1009 struct lttng_ht_node_ulong *node =
1010 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 1011 struct ust_app *app =
852d0037 1012 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 1013
852d0037 1014 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 1015 delete_ust_app(app);
099e26bd
DG
1016}
1017
ffe60014
DG
1018/*
1019 * Delete the session from the application ht and delete the data structure by
1020 * freeing every object inside and releasing them.
82cac6d2
JG
1021 *
1022 * The session list lock must be held by the caller.
ffe60014 1023 */
d0b96690 1024static void destroy_app_session(struct ust_app *app,
ffe60014
DG
1025 struct ust_app_session *ua_sess)
1026{
1027 int ret;
1028 struct lttng_ht_iter iter;
1029
1030 assert(app);
1031 assert(ua_sess);
1032
1033 iter.iter.node = &ua_sess->node.node;
1034 ret = lttng_ht_del(app->sessions, &iter);
1035 if (ret) {
1036 /* Already scheduled for teardown. */
1037 goto end;
1038 }
1039
1040 /* Once deleted, free the data structure. */
d0b96690 1041 delete_ust_app_session(app->sock, ua_sess, app);
ffe60014
DG
1042
1043end:
1044 return;
1045}
1046
8b366481
DG
1047/*
1048 * Alloc new UST app session.
1049 */
1050static
40bbd087 1051struct ust_app_session *alloc_ust_app_session(void)
8b366481
DG
1052{
1053 struct ust_app_session *ua_sess;
1054
1055 /* Init most of the default value by allocating and zeroing */
1056 ua_sess = zmalloc(sizeof(struct ust_app_session));
1057 if (ua_sess == NULL) {
1058 PERROR("malloc");
ffe60014 1059 goto error_free;
8b366481
DG
1060 }
1061
1062 ua_sess->handle = -1;
bec39940 1063 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
ad7a9107 1064 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
84ad93e8 1065 pthread_mutex_init(&ua_sess->lock, NULL);
ad7a9107 1066
8b366481
DG
1067 return ua_sess;
1068
ffe60014 1069error_free:
8b366481
DG
1070 return NULL;
1071}
1072
1073/*
1074 * Alloc new UST app channel.
1075 */
1076static
1077struct ust_app_channel *alloc_ust_app_channel(char *name,
d0b96690 1078 struct ust_app_session *ua_sess,
ffe60014 1079 struct lttng_ust_channel_attr *attr)
8b366481
DG
1080{
1081 struct ust_app_channel *ua_chan;
1082
1083 /* Init most of the default value by allocating and zeroing */
1084 ua_chan = zmalloc(sizeof(struct ust_app_channel));
1085 if (ua_chan == NULL) {
1086 PERROR("malloc");
1087 goto error;
1088 }
1089
1090 /* Setup channel name */
1091 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
1092 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1093
1094 ua_chan->enabled = 1;
1095 ua_chan->handle = -1;
45893984 1096 ua_chan->session = ua_sess;
ffe60014 1097 ua_chan->key = get_next_channel_key();
bec39940
DG
1098 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1099 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1100 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
1101
1102 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
31746f93 1103 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
8b366481
DG
1104
1105 /* Copy attributes */
1106 if (attr) {
ffe60014 1107 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
2fe6e7f5
DG
1108 ua_chan->attr.subbuf_size = attr->subbuf_size;
1109 ua_chan->attr.num_subbuf = attr->num_subbuf;
1110 ua_chan->attr.overwrite = attr->overwrite;
1111 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
1112 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
1113 ua_chan->attr.output = attr->output;
491d1539 1114 ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout;
8b366481 1115 }
ffe60014
DG
1116 /* By default, the channel is a per cpu channel. */
1117 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
8b366481
DG
1118
1119 DBG3("UST app channel %s allocated", ua_chan->name);
1120
1121 return ua_chan;
1122
1123error:
1124 return NULL;
1125}
1126
37f1c236
DG
1127/*
1128 * Allocate and initialize a UST app stream.
1129 *
1130 * Return newly allocated stream pointer or NULL on error.
1131 */
ffe60014 1132struct ust_app_stream *ust_app_alloc_stream(void)
37f1c236
DG
1133{
1134 struct ust_app_stream *stream = NULL;
1135
1136 stream = zmalloc(sizeof(*stream));
1137 if (stream == NULL) {
1138 PERROR("zmalloc ust app stream");
1139 goto error;
1140 }
1141
1142 /* Zero could be a valid value for a handle so flag it to -1. */
1143 stream->handle = -1;
1144
1145error:
1146 return stream;
1147}
1148
8b366481
DG
1149/*
1150 * Alloc new UST app event.
1151 */
1152static
1153struct ust_app_event *alloc_ust_app_event(char *name,
1154 struct lttng_ust_event *attr)
1155{
1156 struct ust_app_event *ua_event;
1157
1158 /* Init most of the default value by allocating and zeroing */
1159 ua_event = zmalloc(sizeof(struct ust_app_event));
1160 if (ua_event == NULL) {
48a0b866 1161 PERROR("Failed to allocate ust_app_event structure");
8b366481
DG
1162 goto error;
1163 }
1164
1165 ua_event->enabled = 1;
1166 strncpy(ua_event->name, name, sizeof(ua_event->name));
1167 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 1168 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
1169
1170 /* Copy attributes */
1171 if (attr) {
1172 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
1173 }
1174
1175 DBG3("UST app event %s allocated", ua_event->name);
1176
1177 return ua_event;
1178
1179error:
1180 return NULL;
1181}
1182
1183/*
1184 * Alloc new UST app context.
1185 */
1186static
bdf64013 1187struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
8b366481
DG
1188{
1189 struct ust_app_ctx *ua_ctx;
1190
1191 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
1192 if (ua_ctx == NULL) {
1193 goto error;
1194 }
1195
31746f93
DG
1196 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1197
8b366481
DG
1198 if (uctx) {
1199 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
bdf64013
JG
1200 if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1201 char *provider_name = NULL, *ctx_name = NULL;
1202
1203 provider_name = strdup(uctx->u.app_ctx.provider_name);
1204 ctx_name = strdup(uctx->u.app_ctx.ctx_name);
1205 if (!provider_name || !ctx_name) {
1206 free(provider_name);
1207 free(ctx_name);
1208 goto error;
1209 }
1210
1211 ua_ctx->ctx.u.app_ctx.provider_name = provider_name;
1212 ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name;
1213 }
8b366481
DG
1214 }
1215
1216 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
8b366481 1217 return ua_ctx;
bdf64013
JG
1218error:
1219 free(ua_ctx);
1220 return NULL;
8b366481
DG
1221}
1222
025faf73
DG
1223/*
1224 * Allocate a filter and copy the given original filter.
1225 *
1226 * Return allocated filter or NULL on error.
1227 */
51755dc8
JG
1228static struct lttng_filter_bytecode *copy_filter_bytecode(
1229 struct lttng_filter_bytecode *orig_f)
025faf73 1230{
51755dc8 1231 struct lttng_filter_bytecode *filter = NULL;
025faf73
DG
1232
1233 /* Copy filter bytecode */
1234 filter = zmalloc(sizeof(*filter) + orig_f->len);
1235 if (!filter) {
51755dc8 1236 PERROR("zmalloc alloc filter bytecode");
025faf73
DG
1237 goto error;
1238 }
1239
1240 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1241
1242error:
1243 return filter;
1244}
1245
51755dc8
JG
1246/*
1247 * Create a liblttng-ust filter bytecode from given bytecode.
1248 *
1249 * Return allocated filter or NULL on error.
1250 */
1251static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
1252 struct lttng_filter_bytecode *orig_f)
1253{
1254 struct lttng_ust_filter_bytecode *filter = NULL;
1255
1256 /* Copy filter bytecode */
1257 filter = zmalloc(sizeof(*filter) + orig_f->len);
1258 if (!filter) {
1259 PERROR("zmalloc alloc ust filter bytecode");
1260 goto error;
1261 }
1262
1263 assert(sizeof(struct lttng_filter_bytecode) ==
1264 sizeof(struct lttng_ust_filter_bytecode));
1265 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1266error:
1267 return filter;
1268}
1269
099e26bd 1270/*
421cb601
DG
1271 * Find an ust_app using the sock and return it. RCU read side lock must be
1272 * held before calling this helper function.
099e26bd 1273 */
f20baf8e 1274struct ust_app *ust_app_find_by_sock(int sock)
099e26bd 1275{
bec39940 1276 struct lttng_ht_node_ulong *node;
bec39940 1277 struct lttng_ht_iter iter;
f6a9efaa 1278
852d0037 1279 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1280 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
1281 if (node == NULL) {
1282 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
1283 goto error;
1284 }
852d0037
DG
1285
1286 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
1287
1288error:
1289 return NULL;
099e26bd
DG
1290}
1291
d0b96690
DG
1292/*
1293 * Find an ust_app using the notify sock and return it. RCU read side lock must
1294 * be held before calling this helper function.
1295 */
1296static struct ust_app *find_app_by_notify_sock(int sock)
1297{
1298 struct lttng_ht_node_ulong *node;
1299 struct lttng_ht_iter iter;
1300
1301 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1302 &iter);
1303 node = lttng_ht_iter_get_node_ulong(&iter);
1304 if (node == NULL) {
1305 DBG2("UST app find by notify sock %d not found", sock);
1306 goto error;
1307 }
1308
1309 return caa_container_of(node, struct ust_app, notify_sock_n);
1310
1311error:
1312 return NULL;
1313}
1314
025faf73
DG
1315/*
1316 * Lookup for an ust app event based on event name, filter bytecode and the
1317 * event loglevel.
1318 *
1319 * Return an ust_app_event object or NULL on error.
1320 */
18eace3b 1321static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
0ce9aa93 1322 const char *name, const struct lttng_filter_bytecode *filter,
2106efa0 1323 int loglevel_value,
39c5a3a7 1324 const struct lttng_event_exclusion *exclusion)
18eace3b
DG
1325{
1326 struct lttng_ht_iter iter;
1327 struct lttng_ht_node_str *node;
1328 struct ust_app_event *event = NULL;
1329 struct ust_app_ht_key key;
18eace3b
DG
1330
1331 assert(name);
1332 assert(ht);
1333
1334 /* Setup key for event lookup. */
1335 key.name = name;
1336 key.filter = filter;
2106efa0 1337 key.loglevel_type = loglevel_value;
39c5a3a7 1338 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
51755dc8 1339 key.exclusion = exclusion;
18eace3b 1340
025faf73
DG
1341 /* Lookup using the event name as hash and a custom match fct. */
1342 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1343 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
1344 node = lttng_ht_iter_get_node_str(&iter);
1345 if (node == NULL) {
1346 goto end;
1347 }
1348
1349 event = caa_container_of(node, struct ust_app_event, node);
1350
1351end:
18eace3b
DG
1352 return event;
1353}
1354
55cc08a6
DG
1355/*
1356 * Create the channel context on the tracer.
d0b96690
DG
1357 *
1358 * Called with UST app session lock held.
55cc08a6
DG
1359 */
1360static
1361int create_ust_channel_context(struct ust_app_channel *ua_chan,
1362 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1363{
1364 int ret;
1365
840cb59c 1366 health_code_update();
86acf0da 1367
fb45065e 1368 pthread_mutex_lock(&app->sock_lock);
852d0037 1369 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6 1370 ua_chan->obj, &ua_ctx->obj);
fb45065e 1371 pthread_mutex_unlock(&app->sock_lock);
55cc08a6 1372 if (ret < 0) {
ee3f5169
JR
1373 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1374 ret = 0;
1375 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1376 app->pid, app->sock);
1377 } else if (ret == -EAGAIN) {
3757b385 1378 ret = 0;
ee3f5169
JR
1379 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1380 app->pid, app->sock);
1381 } else {
1382 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1383 ret, app->pid, app->sock);
ffe60014 1384 }
55cc08a6
DG
1385 goto error;
1386 }
1387
1388 ua_ctx->handle = ua_ctx->obj->handle;
1389
d0b96690
DG
1390 DBG2("UST app context handle %d created successfully for channel %s",
1391 ua_ctx->handle, ua_chan->name);
55cc08a6
DG
1392
1393error:
840cb59c 1394 health_code_update();
55cc08a6
DG
1395 return ret;
1396}
1397
53a80697
MD
1398/*
1399 * Set the filter on the tracer.
1400 */
1401static
1402int set_ust_event_filter(struct ust_app_event *ua_event,
1403 struct ust_app *app)
1404{
1405 int ret;
51755dc8 1406 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
53a80697 1407
840cb59c 1408 health_code_update();
86acf0da 1409
53a80697 1410 if (!ua_event->filter) {
86acf0da
DG
1411 ret = 0;
1412 goto error;
53a80697
MD
1413 }
1414
51755dc8
JG
1415 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1416 if (!ust_bytecode) {
1417 ret = -LTTNG_ERR_NOMEM;
1418 goto error;
1419 }
fb45065e 1420 pthread_mutex_lock(&app->sock_lock);
51755dc8 1421 ret = ustctl_set_filter(app->sock, ust_bytecode,
53a80697 1422 ua_event->obj);
fb45065e 1423 pthread_mutex_unlock(&app->sock_lock);
53a80697 1424 if (ret < 0) {
ee3f5169 1425 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3757b385 1426 ret = 0;
ee3f5169
JR
1427 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1428 app->pid, app->sock);
1429 } else if (ret == -EAGAIN) {
1430 ret = 0;
1431 DBG3("UST app set filter failed. Communication timeout: pid = %d, sock = %d",
1432 app->pid, app->sock);
1433 } else {
1434 ERR("UST app event set filter failed with ret %d: pid = %d, sock = %d",
1435 ret, app->pid,
1436 app->sock);
ffe60014 1437 }
53a80697
MD
1438 goto error;
1439 }
1440
1441 DBG2("UST filter set successfully for event %s", ua_event->name);
1442
1443error:
840cb59c 1444 health_code_update();
51755dc8 1445 free(ust_bytecode);
53a80697
MD
1446 return ret;
1447}
1448
51755dc8
JG
1449static
1450struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1451 struct lttng_event_exclusion *exclusion)
1452{
1453 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1454 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1455 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1456
1457 ust_exclusion = zmalloc(exclusion_alloc_size);
1458 if (!ust_exclusion) {
1459 PERROR("malloc");
1460 goto end;
1461 }
1462
1463 assert(sizeof(struct lttng_event_exclusion) ==
1464 sizeof(struct lttng_ust_event_exclusion));
1465 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1466end:
1467 return ust_exclusion;
1468}
1469
7cc9a73c
JI
1470/*
1471 * Set event exclusions on the tracer.
1472 */
1473static
1474int set_ust_event_exclusion(struct ust_app_event *ua_event,
1475 struct ust_app *app)
1476{
1477 int ret;
51755dc8 1478 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
7cc9a73c
JI
1479
1480 health_code_update();
1481
1482 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1483 ret = 0;
1484 goto error;
1485 }
1486
51755dc8
JG
1487 ust_exclusion = create_ust_exclusion_from_exclusion(
1488 ua_event->exclusion);
1489 if (!ust_exclusion) {
1490 ret = -LTTNG_ERR_NOMEM;
1491 goto error;
1492 }
fb45065e 1493 pthread_mutex_lock(&app->sock_lock);
51755dc8 1494 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
fb45065e 1495 pthread_mutex_unlock(&app->sock_lock);
7cc9a73c 1496 if (ret < 0) {
ee3f5169 1497 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
7cc9a73c 1498 ret = 0;
ee3f5169
JR
1499 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1500 app->pid, app->sock);
1501 } else if (ret == -EAGAIN) {
1502 ret = 0;
1503 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1504 app->pid, app->sock);
1505 } else {
1506 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, event = %s",
1507 ret, app->pid, app->sock, ua_event->name);
7cc9a73c
JI
1508 }
1509 goto error;
1510 }
1511
1512 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1513
1514error:
1515 health_code_update();
51755dc8 1516 free(ust_exclusion);
7cc9a73c
JI
1517 return ret;
1518}
1519
9730260e
DG
1520/*
1521 * Disable the specified event on to UST tracer for the UST session.
1522 */
1523static int disable_ust_event(struct ust_app *app,
1524 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1525{
1526 int ret;
1527
840cb59c 1528 health_code_update();
86acf0da 1529
fb45065e 1530 pthread_mutex_lock(&app->sock_lock);
852d0037 1531 ret = ustctl_disable(app->sock, ua_event->obj);
fb45065e 1532 pthread_mutex_unlock(&app->sock_lock);
9730260e 1533 if (ret < 0) {
ee3f5169 1534 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3757b385 1535 ret = 0;
ee3f5169
JR
1536 DBG3("UST app disable event failed. Application is dead: pid = %d, sock = %d",
1537 app->pid, app->sock);
1538 } else if (ret == -EAGAIN) {
1539 ret = 0;
1540 WARN("UST app disable event failed. Communication time out: pid = %d, sock = %d",
1541 app->pid, app->sock);
1542 } else {
1543 ERR("UST app disable event failed with ret %d: pid = %d, sock = %d , name = %s",
1544 ret, app->pid, app->sock, ua_event->attr.name);
ffe60014 1545 }
9730260e
DG
1546 goto error;
1547 }
1548
1549 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 1550 ua_event->attr.name, app->pid);
9730260e
DG
1551
1552error:
840cb59c 1553 health_code_update();
9730260e
DG
1554 return ret;
1555}
1556
78f0bacd
DG
1557/*
1558 * Disable the specified channel on to UST tracer for the UST session.
1559 */
1560static int disable_ust_channel(struct ust_app *app,
1561 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1562{
1563 int ret;
1564
840cb59c 1565 health_code_update();
86acf0da 1566
fb45065e 1567 pthread_mutex_lock(&app->sock_lock);
852d0037 1568 ret = ustctl_disable(app->sock, ua_chan->obj);
fb45065e 1569 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1570 if (ret < 0) {
ee3f5169 1571 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3757b385 1572 ret = 0;
ee3f5169
JR
1573 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1574 app->pid, app->sock);
1575 } else if (ret == -EAGAIN) {
1576 ret = 0;
1577 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1578 app->pid, app->sock);
1579 } else {
1580 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1581 ua_chan->name, ua_sess->handle, ret,
1582 app->pid, app->sock);
ffe60014 1583 }
78f0bacd
DG
1584 goto error;
1585 }
1586
ee3f5169 1587 DBG2("UST app channel %s disabled successfully for app: pid = %d",
852d0037 1588 ua_chan->name, app->pid);
78f0bacd
DG
1589
1590error:
840cb59c 1591 health_code_update();
78f0bacd
DG
1592 return ret;
1593}
1594
1595/*
1596 * Enable the specified channel on to UST tracer for the UST session.
1597 */
1598static int enable_ust_channel(struct ust_app *app,
1599 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1600{
1601 int ret;
1602
840cb59c 1603 health_code_update();
86acf0da 1604
fb45065e 1605 pthread_mutex_lock(&app->sock_lock);
852d0037 1606 ret = ustctl_enable(app->sock, ua_chan->obj);
fb45065e 1607 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1608 if (ret < 0) {
ee3f5169 1609 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3757b385 1610 ret = 0;
ee3f5169
JR
1611 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1612 ua_chan->name, app->pid, app->sock);
1613 } else if (ret == -EAGAIN) {
1614 ret = 0;
1615 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1616 ua_chan->name, app->pid, app->sock);
1617 } else {
1618 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1619 ua_chan->name, ua_sess->handle, ret,
1620 app->pid, app->sock);
ffe60014 1621 }
78f0bacd
DG
1622 goto error;
1623 }
1624
1625 ua_chan->enabled = 1;
1626
ee3f5169 1627 DBG2("UST app channel %s enabled successfully for app: pid = %d",
852d0037 1628 ua_chan->name, app->pid);
78f0bacd
DG
1629
1630error:
840cb59c 1631 health_code_update();
78f0bacd
DG
1632 return ret;
1633}
1634
edb67388
DG
1635/*
1636 * Enable the specified event on to UST tracer for the UST session.
1637 */
1638static int enable_ust_event(struct ust_app *app,
1639 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1640{
1641 int ret;
1642
840cb59c 1643 health_code_update();
86acf0da 1644
fb45065e 1645 pthread_mutex_lock(&app->sock_lock);
852d0037 1646 ret = ustctl_enable(app->sock, ua_event->obj);
fb45065e 1647 pthread_mutex_unlock(&app->sock_lock);
edb67388 1648 if (ret < 0) {
ee3f5169
JR
1649 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1650 ret = 0;
1651 DBG3("UST app enable event failed. Application is dead: pid = %d, sock = %d",
1652 app->pid, app->sock);
1653 } else if (ret == -EAGAIN) {
3757b385 1654 ret = 0;
ee3f5169
JR
1655 WARN("UST app enable event failed. Communication time out: pid = %d, sock = %d",
1656 app->pid, app->sock);
1657 } else {
1658 ERR("UST app enable event failed with ret %d: pid = %d, sock = %d, event = %s",
1659 ret, app->pid, app->sock, ua_event->attr.name);
ffe60014 1660 }
edb67388
DG
1661 goto error;
1662 }
1663
1664 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 1665 ua_event->attr.name, app->pid);
edb67388
DG
1666
1667error:
840cb59c 1668 health_code_update();
edb67388
DG
1669 return ret;
1670}
1671
099e26bd 1672/*
7972aab2 1673 * Send channel and stream buffer to application.
4f3ab6ee 1674 *
ffe60014 1675 * Return 0 on success. On error, a negative value is returned.
4f3ab6ee 1676 */
7972aab2
DG
1677static int send_channel_pid_to_ust(struct ust_app *app,
1678 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
4f3ab6ee
DG
1679{
1680 int ret;
ffe60014 1681 struct ust_app_stream *stream, *stmp;
4f3ab6ee
DG
1682
1683 assert(app);
ffe60014 1684 assert(ua_sess);
4f3ab6ee 1685 assert(ua_chan);
4f3ab6ee 1686
840cb59c 1687 health_code_update();
4f3ab6ee 1688
7972aab2
DG
1689 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1690 app->sock);
86acf0da 1691
ffe60014
DG
1692 /* Send channel to the application. */
1693 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
1694 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1695 ret = -ENOTCONN; /* Caused by app exiting. */
1696 goto error;
ee3f5169
JR
1697 } else if (ret == -EAGAIN) {
1698 /* Caused by timeout. */
1699 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".",
1700 app->pid, ua_chan->name, ua_sess->tracing_id);
1701 /* Treat this the same way as an application that is exiting. */
1702 ret = -ENOTCONN;
1703 goto error;
a7169585 1704 } else if (ret < 0) {
b551a063
DG
1705 goto error;
1706 }
1707
d88aee68
DG
1708 health_code_update();
1709
ffe60014
DG
1710 /* Send all streams to application. */
1711 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1712 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
a7169585 1713 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
ee3f5169 1714 ret = -ENOTCONN; /* Caused by app exiting. */
a7169585 1715 goto error;
ee3f5169
JR
1716 } else if (ret == -EAGAIN) {
1717 /* Caused by timeout. */
1718 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64 "\".",
1719 app->pid, stream->name, ua_chan->name,
1720 ua_sess->tracing_id);
1721 /*
1722 * Treat this the same way as an application that is
1723 * exiting.
1724 */
1725 ret = -ENOTCONN;
a7169585 1726 } else if (ret < 0) {
ffe60014
DG
1727 goto error;
1728 }
1729 /* We don't need the stream anymore once sent to the tracer. */
1730 cds_list_del(&stream->list);
fb45065e 1731 delete_ust_app_stream(-1, stream, app);
ffe60014 1732 }
ffe60014
DG
1733 /* Flag the channel that it is sent to the application. */
1734 ua_chan->is_sent = 1;
ffe60014 1735
b551a063 1736error:
840cb59c 1737 health_code_update();
b551a063
DG
1738 return ret;
1739}
1740
91d76f53 1741/*
5b4a0ec0 1742 * Create the specified event onto the UST tracer for a UST session.
d0b96690
DG
1743 *
1744 * Should be called with session mutex held.
91d76f53 1745 */
edb67388
DG
1746static
1747int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1748 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 1749{
5b4a0ec0 1750 int ret = 0;
284d8f55 1751
840cb59c 1752 health_code_update();
86acf0da 1753
5b4a0ec0 1754 /* Create UST event on tracer */
fb45065e 1755 pthread_mutex_lock(&app->sock_lock);
852d0037 1756 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0 1757 &ua_event->obj);
fb45065e 1758 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0 1759 if (ret < 0) {
ee3f5169 1760 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3757b385 1761 ret = 0;
ee3f5169
JR
1762 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
1763 app->pid, app->sock);
1764 } else if (ret == -EAGAIN) {
1765 ret = 0;
1766 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
1767 app->pid, app->sock);
1768 } else {
1769 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
1770 ua_event->attr.name, ret, app->pid,
1771 app->sock);
ffe60014 1772 }
5b4a0ec0 1773 goto error;
91d76f53 1774 }
f6a9efaa 1775
5b4a0ec0 1776 ua_event->handle = ua_event->obj->handle;
284d8f55 1777
5b4a0ec0 1778 DBG2("UST app event %s created successfully for pid:%d",
852d0037 1779 ua_event->attr.name, app->pid);
f6a9efaa 1780
840cb59c 1781 health_code_update();
86acf0da 1782
025faf73
DG
1783 /* Set filter if one is present. */
1784 if (ua_event->filter) {
1785 ret = set_ust_event_filter(ua_event, app);
1786 if (ret < 0) {
1787 goto error;
1788 }
1789 }
1790
7cc9a73c
JI
1791 /* Set exclusions for the event */
1792 if (ua_event->exclusion) {
1793 ret = set_ust_event_exclusion(ua_event, app);
1794 if (ret < 0) {
1795 goto error;
1796 }
1797 }
1798
8535a6d9 1799 /* If event not enabled, disable it on the tracer */
40113787
MD
1800 if (ua_event->enabled) {
1801 /*
1802 * We now need to explicitly enable the event, since it
1803 * is now disabled at creation.
1804 */
1805 ret = enable_ust_event(app, ua_sess, ua_event);
1806 if (ret < 0) {
1807 /*
1808 * If we hit an EPERM, something is wrong with our enable call. If
1809 * we get an EEXIST, there is a problem on the tracer side since we
1810 * just created it.
1811 */
1812 switch (ret) {
1813 case -LTTNG_UST_ERR_PERM:
1814 /* Code flow problem */
1815 assert(0);
1816 case -LTTNG_UST_ERR_EXIST:
1817 /* It's OK for our use case. */
1818 ret = 0;
1819 break;
1820 default:
1821 break;
1822 }
1823 goto error;
1824 }
8535a6d9
DG
1825 }
1826
5b4a0ec0 1827error:
840cb59c 1828 health_code_update();
5b4a0ec0 1829 return ret;
91d76f53 1830}
48842b30 1831
5b4a0ec0
DG
1832/*
1833 * Copy data between an UST app event and a LTT event.
1834 */
421cb601 1835static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
1836 struct ltt_ust_event *uevent)
1837{
b4ffad32
JI
1838 size_t exclusion_alloc_size;
1839
48842b30
DG
1840 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1841 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1842
fc34caaa
DG
1843 ua_event->enabled = uevent->enabled;
1844
5b4a0ec0
DG
1845 /* Copy event attributes */
1846 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1847
53a80697
MD
1848 /* Copy filter bytecode */
1849 if (uevent->filter) {
51755dc8 1850 ua_event->filter = copy_filter_bytecode(uevent->filter);
025faf73 1851 /* Filter might be NULL here in case of ENONEM. */
53a80697 1852 }
b4ffad32
JI
1853
1854 /* Copy exclusion data */
1855 if (uevent->exclusion) {
51755dc8 1856 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
b4ffad32
JI
1857 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1858 ua_event->exclusion = zmalloc(exclusion_alloc_size);
5f8df26c
JI
1859 if (ua_event->exclusion == NULL) {
1860 PERROR("malloc");
1861 } else {
1862 memcpy(ua_event->exclusion, uevent->exclusion,
1863 exclusion_alloc_size);
b4ffad32
JI
1864 }
1865 }
48842b30
DG
1866}
1867
5b4a0ec0
DG
1868/*
1869 * Copy data between an UST app channel and a LTT channel.
1870 */
421cb601 1871static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
1872 struct ltt_ust_channel *uchan)
1873{
fc34caaa 1874 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
1875
1876 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1877 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014 1878
1624d5b7
JD
1879 ua_chan->tracefile_size = uchan->tracefile_size;
1880 ua_chan->tracefile_count = uchan->tracefile_count;
1881
ffe60014
DG
1882 /* Copy event attributes since the layout is different. */
1883 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1884 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1885 ua_chan->attr.overwrite = uchan->attr.overwrite;
1886 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1887 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
e9404c27 1888 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
ffe60014 1889 ua_chan->attr.output = uchan->attr.output;
491d1539
MD
1890 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
1891
ffe60014
DG
1892 /*
1893 * Note that the attribute channel type is not set since the channel on the
1894 * tracing registry side does not have this information.
1895 */
48842b30 1896
fc34caaa 1897 ua_chan->enabled = uchan->enabled;
7972aab2 1898 ua_chan->tracing_channel_id = uchan->id;
fc34caaa 1899
fc34caaa 1900 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1901}
1902
5b4a0ec0
DG
1903/*
1904 * Copy data between a UST app session and a regular LTT session.
1905 */
421cb601 1906static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1907 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1908{
477d7741
MD
1909 struct tm *timeinfo;
1910 char datetime[16];
1911 int ret;
d7ba1388 1912 char tmp_shm_path[PATH_MAX];
477d7741 1913
cd82d919 1914 timeinfo = localtime(&app->registration_time);
477d7741 1915 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1916
421cb601 1917 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1918
7972aab2
DG
1919 ua_sess->tracing_id = usess->id;
1920 ua_sess->id = get_next_session_id();
c51311d6
JG
1921 ua_sess->real_credentials.uid = app->uid;
1922 ua_sess->real_credentials.gid = app->gid;
1923 ua_sess->effective_credentials.uid = usess->uid;
1924 ua_sess->effective_credentials.gid = usess->gid;
7972aab2
DG
1925 ua_sess->buffer_type = usess->buffer_type;
1926 ua_sess->bits_per_long = app->bits_per_long;
6addfa37 1927
7972aab2 1928 /* There is only one consumer object per session possible. */
6addfa37 1929 consumer_output_get(usess->consumer);
7972aab2 1930 ua_sess->consumer = usess->consumer;
6addfa37 1931
2bba9e53 1932 ua_sess->output_traces = usess->output_traces;
ecc48a90 1933 ua_sess->live_timer_interval = usess->live_timer_interval;
84ad93e8
DG
1934 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1935 &usess->metadata_attr);
7972aab2
DG
1936
1937 switch (ua_sess->buffer_type) {
1938 case LTTNG_BUFFER_PER_PID:
1939 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
dec56f6c 1940 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
7972aab2
DG
1941 datetime);
1942 break;
1943 case LTTNG_BUFFER_PER_UID:
1944 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
c51311d6
JG
1945 DEFAULT_UST_TRACE_UID_PATH,
1946 ua_sess->real_credentials.uid,
1947 app->bits_per_long);
7972aab2
DG
1948 break;
1949 default:
1950 assert(0);
1951 goto error;
1952 }
477d7741
MD
1953 if (ret < 0) {
1954 PERROR("asprintf UST shadow copy session");
477d7741 1955 assert(0);
7972aab2 1956 goto error;
477d7741
MD
1957 }
1958
3d071855
MD
1959 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1960 sizeof(ua_sess->root_shm_path));
1961 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
d7ba1388
MD
1962 strncpy(ua_sess->shm_path, usess->shm_path,
1963 sizeof(ua_sess->shm_path));
1964 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1965 if (ua_sess->shm_path[0]) {
1966 switch (ua_sess->buffer_type) {
1967 case LTTNG_BUFFER_PER_PID:
1968 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1969 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1970 app->name, app->pid, datetime);
1971 break;
1972 case LTTNG_BUFFER_PER_UID:
1973 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1974 DEFAULT_UST_TRACE_UID_PATH,
1975 app->uid, app->bits_per_long);
1976 break;
1977 default:
1978 assert(0);
1979 goto error;
1980 }
1981 if (ret < 0) {
1982 PERROR("sprintf UST shadow copy session");
1983 assert(0);
1984 goto error;
1985 }
1986 strncat(ua_sess->shm_path, tmp_shm_path,
1987 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1988 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1989 }
6addfa37 1990 return;
7972aab2
DG
1991
1992error:
6addfa37 1993 consumer_output_put(ua_sess->consumer);
48842b30
DG
1994}
1995
78f0bacd
DG
1996/*
1997 * Lookup sesison wrapper.
1998 */
84cd17c6 1999static
65ff8ea3 2000void __lookup_session_by_app(const struct ltt_ust_session *usess,
bec39940 2001 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
2002{
2003 /* Get right UST app session from app */
d9bf3ca4 2004 lttng_ht_lookup(app->sessions, &usess->id, iter);
84cd17c6
MD
2005}
2006
421cb601
DG
2007/*
2008 * Return ust app session from the app session hashtable using the UST session
a991f516 2009 * id.
421cb601 2010 */
48842b30 2011static struct ust_app_session *lookup_session_by_app(
65ff8ea3 2012 const struct ltt_ust_session *usess, struct ust_app *app)
48842b30 2013{
bec39940 2014 struct lttng_ht_iter iter;
d9bf3ca4 2015 struct lttng_ht_node_u64 *node;
48842b30 2016
84cd17c6 2017 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 2018 node = lttng_ht_iter_get_node_u64(&iter);
48842b30
DG
2019 if (node == NULL) {
2020 goto error;
2021 }
2022
2023 return caa_container_of(node, struct ust_app_session, node);
2024
2025error:
2026 return NULL;
2027}
2028
7972aab2
DG
2029/*
2030 * Setup buffer registry per PID for the given session and application. If none
2031 * is found, a new one is created, added to the global registry and
2032 * initialized. If regp is valid, it's set with the newly created object.
2033 *
2034 * Return 0 on success or else a negative value.
2035 */
2036static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
2037 struct ust_app *app, struct buffer_reg_pid **regp)
2038{
2039 int ret = 0;
2040 struct buffer_reg_pid *reg_pid;
2041
2042 assert(ua_sess);
2043 assert(app);
2044
2045 rcu_read_lock();
2046
2047 reg_pid = buffer_reg_pid_find(ua_sess->id);
2048 if (!reg_pid) {
2049 /*
2050 * This is the create channel path meaning that if there is NO
2051 * registry available, we have to create one for this session.
2052 */
d7ba1388 2053 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
3d071855 2054 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2055 if (ret < 0) {
2056 goto error;
2057 }
7972aab2
DG
2058 } else {
2059 goto end;
2060 }
2061
2062 /* Initialize registry. */
2063 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
2064 app->bits_per_long, app->uint8_t_alignment,
2065 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf 2066 app->uint64_t_alignment, app->long_alignment,
c51311d6
JG
2067 app->byte_order, app->version.major, app->version.minor,
2068 reg_pid->root_shm_path, reg_pid->shm_path,
2069 ua_sess->effective_credentials.uid,
6e78b10e
JR
2070 ua_sess->effective_credentials.gid, ua_sess->tracing_id,
2071 app->uid);
7972aab2 2072 if (ret < 0) {
286c991a
MD
2073 /*
2074 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2075 * destroy the buffer registry, because it is always expected
2076 * that if the buffer registry can be found, its ust registry is
2077 * non-NULL.
2078 */
2079 buffer_reg_pid_destroy(reg_pid);
7972aab2
DG
2080 goto error;
2081 }
2082
286c991a
MD
2083 buffer_reg_pid_add(reg_pid);
2084
7972aab2
DG
2085 DBG3("UST app buffer registry per PID created successfully");
2086
2087end:
2088 if (regp) {
2089 *regp = reg_pid;
2090 }
2091error:
2092 rcu_read_unlock();
2093 return ret;
2094}
2095
2096/*
2097 * Setup buffer registry per UID for the given session and application. If none
2098 * is found, a new one is created, added to the global registry and
2099 * initialized. If regp is valid, it's set with the newly created object.
2100 *
2101 * Return 0 on success or else a negative value.
2102 */
2103static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
d7ba1388 2104 struct ust_app_session *ua_sess,
7972aab2
DG
2105 struct ust_app *app, struct buffer_reg_uid **regp)
2106{
2107 int ret = 0;
2108 struct buffer_reg_uid *reg_uid;
2109
2110 assert(usess);
2111 assert(app);
2112
2113 rcu_read_lock();
2114
2115 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2116 if (!reg_uid) {
2117 /*
2118 * This is the create channel path meaning that if there is NO
2119 * registry available, we have to create one for this session.
2120 */
2121 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
3d071855
MD
2122 LTTNG_DOMAIN_UST, &reg_uid,
2123 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2124 if (ret < 0) {
2125 goto error;
2126 }
7972aab2
DG
2127 } else {
2128 goto end;
2129 }
2130
2131 /* Initialize registry. */
af6142cf 2132 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
7972aab2
DG
2133 app->bits_per_long, app->uint8_t_alignment,
2134 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
2135 app->uint64_t_alignment, app->long_alignment,
2136 app->byte_order, app->version.major,
3d071855 2137 app->version.minor, reg_uid->root_shm_path,
6e78b10e
JR
2138 reg_uid->shm_path, usess->uid, usess->gid,
2139 ua_sess->tracing_id, app->uid);
7972aab2 2140 if (ret < 0) {
286c991a
MD
2141 /*
2142 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2143 * destroy the buffer registry, because it is always expected
2144 * that if the buffer registry can be found, its ust registry is
2145 * non-NULL.
2146 */
2147 buffer_reg_uid_destroy(reg_uid, NULL);
7972aab2
DG
2148 goto error;
2149 }
2150 /* Add node to teardown list of the session. */
2151 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2152
286c991a 2153 buffer_reg_uid_add(reg_uid);
7972aab2 2154
286c991a 2155 DBG3("UST app buffer registry per UID created successfully");
7972aab2
DG
2156end:
2157 if (regp) {
2158 *regp = reg_uid;
2159 }
2160error:
2161 rcu_read_unlock();
2162 return ret;
2163}
2164
421cb601 2165/*
3d8ca23b 2166 * Create a session on the tracer side for the given app.
421cb601 2167 *
3d8ca23b
DG
2168 * On success, ua_sess_ptr is populated with the session pointer or else left
2169 * untouched. If the session was created, is_created is set to 1. On error,
2170 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2171 * be NULL.
2172 *
2173 * Returns 0 on success or else a negative code which is either -ENOMEM or
2174 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 2175 */
03f91eaa 2176static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
3d8ca23b
DG
2177 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2178 int *is_created)
421cb601 2179{
3d8ca23b 2180 int ret, created = 0;
421cb601
DG
2181 struct ust_app_session *ua_sess;
2182
3d8ca23b
DG
2183 assert(usess);
2184 assert(app);
2185 assert(ua_sess_ptr);
2186
840cb59c 2187 health_code_update();
86acf0da 2188
421cb601
DG
2189 ua_sess = lookup_session_by_app(usess, app);
2190 if (ua_sess == NULL) {
d9bf3ca4 2191 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
852d0037 2192 app->pid, usess->id);
40bbd087 2193 ua_sess = alloc_ust_app_session();
421cb601
DG
2194 if (ua_sess == NULL) {
2195 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
2196 ret = -ENOMEM;
2197 goto error;
421cb601 2198 }
477d7741 2199 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 2200 created = 1;
421cb601
DG
2201 }
2202
7972aab2
DG
2203 switch (usess->buffer_type) {
2204 case LTTNG_BUFFER_PER_PID:
2205 /* Init local registry. */
2206 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
421cb601 2207 if (ret < 0) {
e64207cf 2208 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2209 goto error;
2210 }
2211 break;
2212 case LTTNG_BUFFER_PER_UID:
2213 /* Look for a global registry. If none exists, create one. */
d7ba1388 2214 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
7972aab2 2215 if (ret < 0) {
e64207cf 2216 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2217 goto error;
2218 }
2219 break;
2220 default:
2221 assert(0);
2222 ret = -EINVAL;
2223 goto error;
2224 }
2225
2226 health_code_update();
2227
2228 if (ua_sess->handle == -1) {
fb45065e 2229 pthread_mutex_lock(&app->sock_lock);
7972aab2 2230 ret = ustctl_create_session(app->sock);
fb45065e 2231 pthread_mutex_unlock(&app->sock_lock);
7972aab2 2232 if (ret < 0) {
ee3f5169
JR
2233 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2234 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2235 app->pid, app->sock);
3757b385 2236 ret = 0;
ee3f5169
JR
2237 } else if (ret == -EAGAIN) {
2238 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2239 app->pid, app->sock);
2240 ret = 0;
2241 } else {
2242 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2243 ret, app->pid, app->sock);
ffe60014 2244 }
d0b96690 2245 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
2246 if (ret != -ENOMEM) {
2247 /*
2248 * Tracer is probably gone or got an internal error so let's
2249 * behave like it will soon unregister or not usable.
2250 */
2251 ret = -ENOTCONN;
2252 }
2253 goto error;
421cb601
DG
2254 }
2255
7972aab2
DG
2256 ua_sess->handle = ret;
2257
2258 /* Add ust app session to app's HT */
d9bf3ca4
MD
2259 lttng_ht_node_init_u64(&ua_sess->node,
2260 ua_sess->tracing_id);
2261 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
10b56aef
MD
2262 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2263 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2264 &ua_sess->ust_objd_node);
7972aab2
DG
2265
2266 DBG2("UST app session created successfully with handle %d", ret);
2267 }
2268
2269 *ua_sess_ptr = ua_sess;
2270 if (is_created) {
2271 *is_created = created;
2272 }
2273
2274 /* Everything went well. */
2275 ret = 0;
2276
2277error:
2278 health_code_update();
2279 return ret;
2280}
2281
6a6b2068
JG
2282/*
2283 * Match function for a hash table lookup of ust_app_ctx.
2284 *
2285 * It matches an ust app context based on the context type and, in the case
2286 * of perf counters, their name.
2287 */
2288static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2289{
2290 struct ust_app_ctx *ctx;
bdf64013 2291 const struct lttng_ust_context_attr *key;
6a6b2068
JG
2292
2293 assert(node);
2294 assert(_key);
2295
2296 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2297 key = _key;
2298
2299 /* Context type */
2300 if (ctx->ctx.ctx != key->ctx) {
2301 goto no_match;
2302 }
2303
bdf64013
JG
2304 switch(key->ctx) {
2305 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
6a6b2068 2306 if (strncmp(key->u.perf_counter.name,
bdf64013
JG
2307 ctx->ctx.u.perf_counter.name,
2308 sizeof(key->u.perf_counter.name))) {
2309 goto no_match;
2310 }
2311 break;
2312 case LTTNG_UST_CONTEXT_APP_CONTEXT:
2313 if (strcmp(key->u.app_ctx.provider_name,
2314 ctx->ctx.u.app_ctx.provider_name) ||
2315 strcmp(key->u.app_ctx.ctx_name,
2316 ctx->ctx.u.app_ctx.ctx_name)) {
6a6b2068
JG
2317 goto no_match;
2318 }
bdf64013
JG
2319 break;
2320 default:
2321 break;
6a6b2068
JG
2322 }
2323
2324 /* Match. */
2325 return 1;
2326
2327no_match:
2328 return 0;
2329}
2330
2331/*
2332 * Lookup for an ust app context from an lttng_ust_context.
2333 *
be184a0f 2334 * Must be called while holding RCU read side lock.
6a6b2068
JG
2335 * Return an ust_app_ctx object or NULL on error.
2336 */
2337static
2338struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
bdf64013 2339 struct lttng_ust_context_attr *uctx)
6a6b2068
JG
2340{
2341 struct lttng_ht_iter iter;
2342 struct lttng_ht_node_ulong *node;
2343 struct ust_app_ctx *app_ctx = NULL;
2344
2345 assert(uctx);
2346 assert(ht);
2347
2348 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2349 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2350 ht_match_ust_app_ctx, uctx, &iter.iter);
2351 node = lttng_ht_iter_get_node_ulong(&iter);
2352 if (!node) {
2353 goto end;
2354 }
2355
2356 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2357
2358end:
2359 return app_ctx;
2360}
2361
7972aab2
DG
2362/*
2363 * Create a context for the channel on the tracer.
2364 *
2365 * Called with UST app session lock held and a RCU read side lock.
2366 */
2367static
c9edf082 2368int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
bdf64013 2369 struct lttng_ust_context_attr *uctx,
7972aab2
DG
2370 struct ust_app *app)
2371{
2372 int ret = 0;
7972aab2
DG
2373 struct ust_app_ctx *ua_ctx;
2374
2375 DBG2("UST app adding context to channel %s", ua_chan->name);
2376
6a6b2068
JG
2377 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2378 if (ua_ctx) {
7972aab2
DG
2379 ret = -EEXIST;
2380 goto error;
2381 }
2382
2383 ua_ctx = alloc_ust_app_ctx(uctx);
2384 if (ua_ctx == NULL) {
2385 /* malloc failed */
7682f304 2386 ret = -ENOMEM;
7972aab2
DG
2387 goto error;
2388 }
2389
2390 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 2391 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 2392 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
7972aab2
DG
2393
2394 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2395 if (ret < 0) {
2396 goto error;
2397 }
2398
2399error:
2400 return ret;
2401}
2402
2403/*
2404 * Enable on the tracer side a ust app event for the session and channel.
2405 *
2406 * Called with UST app session lock held.
2407 */
2408static
2409int enable_ust_app_event(struct ust_app_session *ua_sess,
2410 struct ust_app_event *ua_event, struct ust_app *app)
2411{
2412 int ret;
2413
2414 ret = enable_ust_event(app, ua_sess, ua_event);
2415 if (ret < 0) {
2416 goto error;
2417 }
2418
2419 ua_event->enabled = 1;
2420
2421error:
2422 return ret;
2423}
2424
2425/*
2426 * Disable on the tracer side a ust app event for the session and channel.
2427 */
2428static int disable_ust_app_event(struct ust_app_session *ua_sess,
2429 struct ust_app_event *ua_event, struct ust_app *app)
2430{
2431 int ret;
2432
2433 ret = disable_ust_event(app, ua_sess, ua_event);
2434 if (ret < 0) {
2435 goto error;
2436 }
2437
2438 ua_event->enabled = 0;
2439
2440error:
2441 return ret;
2442}
2443
2444/*
2445 * Lookup ust app channel for session and disable it on the tracer side.
2446 */
2447static
2448int disable_ust_app_channel(struct ust_app_session *ua_sess,
2449 struct ust_app_channel *ua_chan, struct ust_app *app)
2450{
2451 int ret;
2452
2453 ret = disable_ust_channel(app, ua_sess, ua_chan);
2454 if (ret < 0) {
2455 goto error;
2456 }
2457
2458 ua_chan->enabled = 0;
2459
2460error:
2461 return ret;
2462}
2463
2464/*
2465 * Lookup ust app channel for session and enable it on the tracer side. This
2466 * MUST be called with a RCU read side lock acquired.
2467 */
2468static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2469 struct ltt_ust_channel *uchan, struct ust_app *app)
2470{
2471 int ret = 0;
2472 struct lttng_ht_iter iter;
2473 struct lttng_ht_node_str *ua_chan_node;
2474 struct ust_app_channel *ua_chan;
2475
2476 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2477 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2478 if (ua_chan_node == NULL) {
d9bf3ca4 2479 DBG2("Unable to find channel %s in ust session id %" PRIu64,
7972aab2
DG
2480 uchan->name, ua_sess->tracing_id);
2481 goto error;
2482 }
2483
2484 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2485
2486 ret = enable_ust_channel(app, ua_sess, ua_chan);
2487 if (ret < 0) {
2488 goto error;
2489 }
2490
2491error:
2492 return ret;
2493}
2494
2495/*
2496 * Ask the consumer to create a channel and get it if successful.
2497 *
fad1ed2f
JR
2498 * Called with UST app session lock held.
2499 *
7972aab2
DG
2500 * Return 0 on success or else a negative value.
2501 */
2502static int do_consumer_create_channel(struct ltt_ust_session *usess,
2503 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
e098433c
JG
2504 int bitness, struct ust_registry_session *registry,
2505 uint64_t trace_archive_id)
7972aab2
DG
2506{
2507 int ret;
2508 unsigned int nb_fd = 0;
2509 struct consumer_socket *socket;
2510
2511 assert(usess);
2512 assert(ua_sess);
2513 assert(ua_chan);
2514 assert(registry);
2515
2516 rcu_read_lock();
2517 health_code_update();
2518
2519 /* Get the right consumer socket for the application. */
2520 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2521 if (!socket) {
2522 ret = -EINVAL;
2523 goto error;
2524 }
2525
2526 health_code_update();
2527
2528 /* Need one fd for the channel. */
2529 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2530 if (ret < 0) {
2531 ERR("Exhausted number of available FD upon create channel");
2532 goto error;
2533 }
2534
2535 /*
2536 * Ask consumer to create channel. The consumer will return the number of
2537 * stream we have to expect.
2538 */
2539 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
e5148e25 2540 registry, usess->current_trace_chunk);
7972aab2
DG
2541 if (ret < 0) {
2542 goto error_ask;
2543 }
2544
2545 /*
2546 * Compute the number of fd needed before receiving them. It must be 2 per
2547 * stream (2 being the default value here).
2548 */
2549 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2550
2551 /* Reserve the amount of file descriptor we need. */
2552 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2553 if (ret < 0) {
2554 ERR("Exhausted number of available FD upon create channel");
2555 goto error_fd_get_stream;
2556 }
2557
2558 health_code_update();
2559
2560 /*
e1ac6bb9 2561 * Now get the channel from the consumer. This call will populate the stream
7972aab2
DG
2562 * list of that channel and set the ust objects.
2563 */
d9078d0c
DG
2564 if (usess->consumer->enabled) {
2565 ret = ust_consumer_get_channel(socket, ua_chan);
2566 if (ret < 0) {
2567 goto error_destroy;
2568 }
7972aab2
DG
2569 }
2570
2571 rcu_read_unlock();
2572 return 0;
2573
2574error_destroy:
2575 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2576error_fd_get_stream:
2577 /*
2578 * Initiate a destroy channel on the consumer since we had an error
2579 * handling it on our side. The return value is of no importance since we
2580 * already have a ret value set by the previous error that we need to
2581 * return.
2582 */
2583 (void) ust_consumer_destroy_channel(socket, ua_chan);
2584error_ask:
2585 lttng_fd_put(LTTNG_FD_APPS, 1);
2586error:
2587 health_code_update();
2588 rcu_read_unlock();
2589 return ret;
2590}
2591
2592/*
2593 * Duplicate the ust data object of the ust app stream and save it in the
2594 * buffer registry stream.
2595 *
2596 * Return 0 on success or else a negative value.
2597 */
2598static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2599 struct ust_app_stream *stream)
2600{
2601 int ret;
2602
2603 assert(reg_stream);
2604 assert(stream);
2605
2606 /* Reserve the amount of file descriptor we need. */
2607 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2608 if (ret < 0) {
2609 ERR("Exhausted number of available FD upon duplicate stream");
2610 goto error;
2611 }
2612
2613 /* Duplicate object for stream once the original is in the registry. */
2614 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2615 reg_stream->obj.ust);
2616 if (ret < 0) {
2617 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2618 reg_stream->obj.ust, stream->obj, ret);
2619 lttng_fd_put(LTTNG_FD_APPS, 2);
2620 goto error;
2621 }
2622 stream->handle = stream->obj->handle;
2623
2624error:
2625 return ret;
2626}
2627
2628/*
2629 * Duplicate the ust data object of the ust app. channel and save it in the
2630 * buffer registry channel.
2631 *
2632 * Return 0 on success or else a negative value.
2633 */
2634static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2635 struct ust_app_channel *ua_chan)
2636{
2637 int ret;
2638
2639 assert(reg_chan);
2640 assert(ua_chan);
2641
2642 /* Need two fds for the channel. */
2643 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2644 if (ret < 0) {
2645 ERR("Exhausted number of available FD upon duplicate channel");
2646 goto error_fd_get;
2647 }
2648
2649 /* Duplicate object for stream once the original is in the registry. */
2650 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2651 if (ret < 0) {
2652 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2653 reg_chan->obj.ust, ua_chan->obj, ret);
2654 goto error;
2655 }
2656 ua_chan->handle = ua_chan->obj->handle;
2657
2658 return 0;
2659
2660error:
2661 lttng_fd_put(LTTNG_FD_APPS, 1);
2662error_fd_get:
2663 return ret;
2664}
2665
2666/*
2667 * For a given channel buffer registry, setup all streams of the given ust
2668 * application channel.
2669 *
2670 * Return 0 on success or else a negative value.
2671 */
2672static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
fb45065e
MD
2673 struct ust_app_channel *ua_chan,
2674 struct ust_app *app)
7972aab2
DG
2675{
2676 int ret = 0;
2677 struct ust_app_stream *stream, *stmp;
2678
2679 assert(reg_chan);
2680 assert(ua_chan);
2681
2682 DBG2("UST app setup buffer registry stream");
2683
2684 /* Send all streams to application. */
2685 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2686 struct buffer_reg_stream *reg_stream;
2687
2688 ret = buffer_reg_stream_create(&reg_stream);
2689 if (ret < 0) {
2690 goto error;
2691 }
2692
2693 /*
2694 * Keep original pointer and nullify it in the stream so the delete
2695 * stream call does not release the object.
2696 */
2697 reg_stream->obj.ust = stream->obj;
2698 stream->obj = NULL;
2699 buffer_reg_stream_add(reg_stream, reg_chan);
421cb601 2700
7972aab2
DG
2701 /* We don't need the streams anymore. */
2702 cds_list_del(&stream->list);
fb45065e 2703 delete_ust_app_stream(-1, stream, app);
7972aab2 2704 }
421cb601 2705
7972aab2
DG
2706error:
2707 return ret;
2708}
2709
2710/*
2711 * Create a buffer registry channel for the given session registry and
2712 * application channel object. If regp pointer is valid, it's set with the
2713 * created object. Important, the created object is NOT added to the session
2714 * registry hash table.
2715 *
2716 * Return 0 on success else a negative value.
2717 */
2718static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2719 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2720{
2721 int ret;
2722 struct buffer_reg_channel *reg_chan = NULL;
2723
2724 assert(reg_sess);
2725 assert(ua_chan);
2726
2727 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2728
2729 /* Create buffer registry channel. */
2730 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2731 if (ret < 0) {
2732 goto error_create;
421cb601 2733 }
7972aab2
DG
2734 assert(reg_chan);
2735 reg_chan->consumer_key = ua_chan->key;
8c924c7b 2736 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
d07ceecd 2737 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
421cb601 2738
7972aab2
DG
2739 /* Create and add a channel registry to session. */
2740 ret = ust_registry_channel_add(reg_sess->reg.ust,
2741 ua_chan->tracing_channel_id);
2742 if (ret < 0) {
2743 goto error;
d88aee68 2744 }
7972aab2 2745 buffer_reg_channel_add(reg_sess, reg_chan);
d88aee68 2746
7972aab2
DG
2747 if (regp) {
2748 *regp = reg_chan;
3d8ca23b 2749 }
d88aee68 2750
7972aab2 2751 return 0;
3d8ca23b
DG
2752
2753error:
7972aab2
DG
2754 /* Safe because the registry channel object was not added to any HT. */
2755 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2756error_create:
3d8ca23b 2757 return ret;
421cb601
DG
2758}
2759
55cc08a6 2760/*
7972aab2
DG
2761 * Setup buffer registry channel for the given session registry and application
2762 * channel object. If regp pointer is valid, it's set with the created object.
d0b96690 2763 *
7972aab2 2764 * Return 0 on success else a negative value.
55cc08a6 2765 */
7972aab2 2766static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
fb45065e
MD
2767 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan,
2768 struct ust_app *app)
55cc08a6 2769{
7972aab2 2770 int ret;
55cc08a6 2771
7972aab2
DG
2772 assert(reg_sess);
2773 assert(reg_chan);
2774 assert(ua_chan);
2775 assert(ua_chan->obj);
55cc08a6 2776
7972aab2 2777 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
55cc08a6 2778
7972aab2 2779 /* Setup all streams for the registry. */
fb45065e 2780 ret = setup_buffer_reg_streams(reg_chan, ua_chan, app);
7972aab2 2781 if (ret < 0) {
55cc08a6
DG
2782 goto error;
2783 }
2784
7972aab2
DG
2785 reg_chan->obj.ust = ua_chan->obj;
2786 ua_chan->obj = NULL;
55cc08a6 2787
7972aab2 2788 return 0;
55cc08a6
DG
2789
2790error:
7972aab2
DG
2791 buffer_reg_channel_remove(reg_sess, reg_chan);
2792 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
55cc08a6
DG
2793 return ret;
2794}
2795
edb67388 2796/*
7972aab2 2797 * Send buffer registry channel to the application.
d0b96690 2798 *
7972aab2 2799 * Return 0 on success else a negative value.
edb67388 2800 */
7972aab2
DG
2801static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2802 struct ust_app *app, struct ust_app_session *ua_sess,
2803 struct ust_app_channel *ua_chan)
edb67388
DG
2804{
2805 int ret;
7972aab2 2806 struct buffer_reg_stream *reg_stream;
edb67388 2807
7972aab2
DG
2808 assert(reg_chan);
2809 assert(app);
2810 assert(ua_sess);
2811 assert(ua_chan);
2812
2813 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2814
2815 ret = duplicate_channel_object(reg_chan, ua_chan);
edb67388
DG
2816 if (ret < 0) {
2817 goto error;
2818 }
2819
7972aab2
DG
2820 /* Send channel to the application. */
2821 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
2822 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2823 ret = -ENOTCONN; /* Caused by app exiting. */
2824 goto error;
ee3f5169
JR
2825 } else if (ret == -EAGAIN) {
2826 /* Caused by timeout. */
2827 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".",
2828 app->pid, ua_chan->name, ua_sess->tracing_id);
2829 /* Treat this the same way as an application that is exiting. */
2830 ret = -ENOTCONN;
2831 goto error;
a7169585 2832 } else if (ret < 0) {
7972aab2
DG
2833 goto error;
2834 }
2835
2836 health_code_update();
2837
2838 /* Send all streams to application. */
2839 pthread_mutex_lock(&reg_chan->stream_list_lock);
2840 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2841 struct ust_app_stream stream;
2842
2843 ret = duplicate_stream_object(reg_stream, &stream);
2844 if (ret < 0) {
2845 goto error_stream_unlock;
2846 }
2847
2848 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2849 if (ret < 0) {
a7169585
MD
2850 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2851 ret = -ENOTCONN; /* Caused by app exiting. */
ee3f5169
JR
2852 } else if (ret == -EAGAIN) {
2853 /*
2854 * Caused by timeout.
2855 * Treat this the same way as an application
2856 * that is exiting.
2857 */
2858 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64 "\".",
2859 app->pid, stream.name,
2860 ua_chan->name,
2861 ua_sess->tracing_id);
2862 ret = -ENOTCONN;
a7169585 2863 }
ee3f5169 2864 (void) release_ust_app_stream(-1, &stream, app);
7972aab2
DG
2865 goto error_stream_unlock;
2866 }
edb67388 2867
7972aab2
DG
2868 /*
2869 * The return value is not important here. This function will output an
2870 * error if needed.
2871 */
fb45065e 2872 (void) release_ust_app_stream(-1, &stream, app);
7972aab2
DG
2873 }
2874 ua_chan->is_sent = 1;
2875
2876error_stream_unlock:
2877 pthread_mutex_unlock(&reg_chan->stream_list_lock);
edb67388
DG
2878error:
2879 return ret;
2880}
2881
9730260e 2882/*
7972aab2
DG
2883 * Create and send to the application the created buffers with per UID buffers.
2884 *
9acdc1d6 2885 * This MUST be called with a RCU read side lock acquired.
71e0a100 2886 * The session list lock and the session's lock must be acquired.
9acdc1d6 2887 *
7972aab2 2888 * Return 0 on success else a negative value.
9730260e 2889 */
7972aab2
DG
2890static int create_channel_per_uid(struct ust_app *app,
2891 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2892 struct ust_app_channel *ua_chan)
9730260e
DG
2893{
2894 int ret;
7972aab2
DG
2895 struct buffer_reg_uid *reg_uid;
2896 struct buffer_reg_channel *reg_chan;
48a86f68 2897 struct ltt_session *session = NULL;
e098433c
JG
2898 enum lttng_error_code notification_ret;
2899 struct ust_registry_channel *chan_reg;
9730260e 2900
7972aab2
DG
2901 assert(app);
2902 assert(usess);
2903 assert(ua_sess);
2904 assert(ua_chan);
2905
2906 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2907
2908 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2909 /*
2910 * The session creation handles the creation of this global registry
2911 * object. If none can be find, there is a code flow problem or a
2912 * teardown race.
2913 */
2914 assert(reg_uid);
2915
2916 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2917 reg_uid);
2721f7ea
JG
2918 if (reg_chan) {
2919 goto send_channel;
2920 }
7972aab2 2921
2721f7ea
JG
2922 /* Create the buffer registry channel object. */
2923 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2924 if (ret < 0) {
2925 ERR("Error creating the UST channel \"%s\" registry instance",
f14256d6 2926 ua_chan->name);
2721f7ea
JG
2927 goto error;
2928 }
f14256d6 2929
e098433c
JG
2930 session = session_find_by_id(ua_sess->tracing_id);
2931 assert(session);
2932 assert(pthread_mutex_trylock(&session->lock));
2933 assert(session_trylock_list());
2934
2721f7ea
JG
2935 /*
2936 * Create the buffers on the consumer side. This call populates the
2937 * ust app channel object with all streams and data object.
2938 */
2939 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 2940 app->bits_per_long, reg_uid->registry->reg.ust,
e5148e25 2941 session->most_recent_chunk_id.value);
2721f7ea
JG
2942 if (ret < 0) {
2943 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2944 ua_chan->name);
7972aab2
DG
2945
2946 /*
2721f7ea
JG
2947 * Let's remove the previously created buffer registry channel so
2948 * it's not visible anymore in the session registry.
7972aab2 2949 */
2721f7ea
JG
2950 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2951 ua_chan->tracing_channel_id, false);
2952 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2953 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2954 goto error;
7972aab2
DG
2955 }
2956
2721f7ea
JG
2957 /*
2958 * Setup the streams and add it to the session registry.
2959 */
2960 ret = setup_buffer_reg_channel(reg_uid->registry,
2961 ua_chan, reg_chan, app);
2962 if (ret < 0) {
2963 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
2964 goto error;
2965 }
2966
e098433c
JG
2967 /* Notify the notification subsystem of the channel's creation. */
2968 pthread_mutex_lock(&reg_uid->registry->reg.ust->lock);
2969 chan_reg = ust_registry_channel_find(reg_uid->registry->reg.ust,
2970 ua_chan->tracing_channel_id);
2971 assert(chan_reg);
2972 chan_reg->consumer_key = ua_chan->key;
2973 chan_reg = NULL;
2974 pthread_mutex_unlock(&reg_uid->registry->reg.ust->lock);
e9404c27 2975
e098433c
JG
2976 notification_ret = notification_thread_command_add_channel(
2977 notification_thread_handle, session->name,
c51311d6
JG
2978 ua_sess->effective_credentials.uid,
2979 ua_sess->effective_credentials.gid, ua_chan->name,
2980 ua_chan->key, LTTNG_DOMAIN_UST,
e098433c
JG
2981 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
2982 if (notification_ret != LTTNG_OK) {
2983 ret = - (int) notification_ret;
2984 ERR("Failed to add channel to notification thread");
2985 goto error;
e9404c27
JG
2986 }
2987
2721f7ea 2988send_channel:
66ff8e3f
JG
2989 /* Send buffers to the application. */
2990 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2991 if (ret < 0) {
2992 if (ret != -ENOTCONN) {
2993 ERR("Error sending channel to application");
2994 }
2995 goto error;
2996 }
2997
9730260e 2998error:
48a86f68
JG
2999 if (session) {
3000 session_put(session);
3001 }
9730260e
DG
3002 return ret;
3003}
3004
78f0bacd 3005/*
7972aab2
DG
3006 * Create and send to the application the created buffers with per PID buffers.
3007 *
fad1ed2f 3008 * Called with UST app session lock held.
71e0a100 3009 * The session list lock and the session's lock must be acquired.
fad1ed2f 3010 *
7972aab2 3011 * Return 0 on success else a negative value.
78f0bacd 3012 */
7972aab2
DG
3013static int create_channel_per_pid(struct ust_app *app,
3014 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3015 struct ust_app_channel *ua_chan)
78f0bacd 3016{
8535a6d9 3017 int ret;
7972aab2 3018 struct ust_registry_session *registry;
e9404c27 3019 enum lttng_error_code cmd_ret;
48a86f68 3020 struct ltt_session *session = NULL;
e9404c27
JG
3021 uint64_t chan_reg_key;
3022 struct ust_registry_channel *chan_reg;
78f0bacd 3023
7972aab2
DG
3024 assert(app);
3025 assert(usess);
3026 assert(ua_sess);
3027 assert(ua_chan);
3028
3029 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
3030
3031 rcu_read_lock();
3032
3033 registry = get_session_registry(ua_sess);
fad1ed2f 3034 /* The UST app session lock is held, registry shall not be null. */
7972aab2
DG
3035 assert(registry);
3036
3037 /* Create and add a new channel registry to session. */
3038 ret = ust_registry_channel_add(registry, ua_chan->key);
78f0bacd 3039 if (ret < 0) {
f14256d6
MD
3040 ERR("Error creating the UST channel \"%s\" registry instance",
3041 ua_chan->name);
78f0bacd
DG
3042 goto error;
3043 }
3044
e098433c
JG
3045 session = session_find_by_id(ua_sess->tracing_id);
3046 assert(session);
3047
3048 assert(pthread_mutex_trylock(&session->lock));
3049 assert(session_trylock_list());
3050
7972aab2
DG
3051 /* Create and get channel on the consumer side. */
3052 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 3053 app->bits_per_long, registry,
e5148e25 3054 session->most_recent_chunk_id.value);
7972aab2 3055 if (ret < 0) {
f14256d6
MD
3056 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3057 ua_chan->name);
7f560f1d 3058 goto error_remove_from_registry;
7972aab2
DG
3059 }
3060
3061 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
3062 if (ret < 0) {
a7169585
MD
3063 if (ret != -ENOTCONN) {
3064 ERR("Error sending channel to application");
3065 }
7f560f1d 3066 goto error_remove_from_registry;
7972aab2 3067 }
8535a6d9 3068
e9404c27
JG
3069 chan_reg_key = ua_chan->key;
3070 pthread_mutex_lock(&registry->lock);
3071 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
3072 assert(chan_reg);
3073 chan_reg->consumer_key = ua_chan->key;
3074 pthread_mutex_unlock(&registry->lock);
3075
3076 cmd_ret = notification_thread_command_add_channel(
3077 notification_thread_handle, session->name,
c51311d6
JG
3078 ua_sess->effective_credentials.uid,
3079 ua_sess->effective_credentials.gid, ua_chan->name,
3080 ua_chan->key, LTTNG_DOMAIN_UST,
e9404c27
JG
3081 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3082 if (cmd_ret != LTTNG_OK) {
3083 ret = - (int) cmd_ret;
3084 ERR("Failed to add channel to notification thread");
7f560f1d 3085 goto error_remove_from_registry;
e9404c27
JG
3086 }
3087
7f560f1d
MD
3088error_remove_from_registry:
3089 if (ret) {
3090 ust_registry_channel_del_free(registry, ua_chan->key, false);
3091 }
78f0bacd 3092error:
7972aab2 3093 rcu_read_unlock();
48a86f68
JG
3094 if (session) {
3095 session_put(session);
3096 }
78f0bacd
DG
3097 return ret;
3098}
3099
3100/*
7972aab2 3101 * From an already allocated ust app channel, create the channel buffers if
0ce9aa93 3102 * needed and send them to the application. This MUST be called with a RCU read
7972aab2
DG
3103 * side lock acquired.
3104 *
fad1ed2f
JR
3105 * Called with UST app session lock held.
3106 *
a7169585
MD
3107 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3108 * the application exited concurrently.
78f0bacd 3109 */
0ce9aa93 3110static int ust_app_channel_send(struct ust_app *app,
7972aab2
DG
3111 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3112 struct ust_app_channel *ua_chan)
78f0bacd 3113{
7972aab2 3114 int ret;
78f0bacd 3115
7972aab2
DG
3116 assert(app);
3117 assert(usess);
0ce9aa93 3118 assert(usess->active);
7972aab2
DG
3119 assert(ua_sess);
3120 assert(ua_chan);
3121
3122 /* Handle buffer type before sending the channel to the application. */
3123 switch (usess->buffer_type) {
3124 case LTTNG_BUFFER_PER_UID:
3125 {
3126 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3127 if (ret < 0) {
3128 goto error;
3129 }
3130 break;
3131 }
3132 case LTTNG_BUFFER_PER_PID:
3133 {
3134 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3135 if (ret < 0) {
3136 goto error;
3137 }
3138 break;
3139 }
3140 default:
3141 assert(0);
3142 ret = -EINVAL;
78f0bacd
DG
3143 goto error;
3144 }
3145
7972aab2
DG
3146 /* Initialize ust objd object using the received handle and add it. */
3147 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3148 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
78f0bacd 3149
7972aab2
DG
3150 /* If channel is not enabled, disable it on the tracer */
3151 if (!ua_chan->enabled) {
3152 ret = disable_ust_channel(app, ua_sess, ua_chan);
3153 if (ret < 0) {
3154 goto error;
3155 }
78f0bacd
DG
3156 }
3157
3158error:
3159 return ret;
3160}
3161
284d8f55 3162/*
0ce9aa93 3163 * Create UST app channel and return it through ua_chanp if not NULL.
d0b96690 3164 *
36b588ed 3165 * Called with UST app session lock and RCU read-side lock held.
7972aab2 3166 *
0ce9aa93 3167 * Return 0 on success or else a negative value.
284d8f55 3168 */
0ce9aa93
JG
3169static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3170 struct ltt_ust_channel *uchan,
7972aab2 3171 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
4d710ac2 3172 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
3173{
3174 int ret = 0;
bec39940
DG
3175 struct lttng_ht_iter iter;
3176 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
3177 struct ust_app_channel *ua_chan;
3178
3179 /* Lookup channel in the ust app session */
bec39940
DG
3180 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3181 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 3182 if (ua_chan_node != NULL) {
5b4a0ec0 3183 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 3184 goto end;
5b4a0ec0
DG
3185 }
3186
d0b96690 3187 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
3188 if (ua_chan == NULL) {
3189 /* Only malloc can fail here */
4d710ac2 3190 ret = -ENOMEM;
0ce9aa93 3191 goto error;
fc34caaa
DG
3192 }
3193 shadow_copy_channel(ua_chan, uchan);
3194
ffe60014
DG
3195 /* Set channel type. */
3196 ua_chan->attr.type = type;
3197
d0b96690
DG
3198 /* Only add the channel if successful on the tracer side. */
3199 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
fc34caaa 3200end:
4d710ac2
DG
3201 if (ua_chanp) {
3202 *ua_chanp = ua_chan;
3203 }
3204
3205 /* Everything went well. */
3206 return 0;
5b4a0ec0
DG
3207
3208error:
4d710ac2 3209 return ret;
5b4a0ec0
DG
3210}
3211
3212/*
3213 * Create UST app event and create it on the tracer side.
d0b96690
DG
3214 *
3215 * Called with ust app session mutex held.
5b4a0ec0 3216 */
edb67388
DG
3217static
3218int create_ust_app_event(struct ust_app_session *ua_sess,
3219 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3220 struct ust_app *app)
284d8f55 3221{
edb67388 3222 int ret = 0;
5b4a0ec0 3223 struct ust_app_event *ua_event;
284d8f55 3224
edb67388
DG
3225 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3226 if (ua_event == NULL) {
48a0b866 3227 /* Only failure mode of alloc_ust_app_event(). */
edb67388 3228 ret = -ENOMEM;
fc34caaa 3229 goto end;
5b4a0ec0 3230 }
edb67388 3231 shadow_copy_event(ua_event, uevent);
5b4a0ec0 3232
edb67388 3233 /* Create it on the tracer side */
5b4a0ec0 3234 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 3235 if (ret < 0) {
3177a074
JG
3236 /*
3237 * Not found previously means that it does not exist on the
3238 * tracer. If the application reports that the event existed,
3239 * it means there is a bug in the sessiond or lttng-ust
3240 * (or corruption, etc.)
3241 */
3242 if (ret == -LTTNG_UST_ERR_EXIST) {
3243 ERR("Tracer for application reported that an event being created already existed: "
3244 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3245 uevent->attr.name,
3246 app->pid, app->ppid, app->uid,
3247 app->gid);
3248 }
284d8f55
DG
3249 goto error;
3250 }
3251
d0b96690 3252 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 3253
fc34caaa 3254 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 3255 app->pid);
7f79d3a1 3256
edb67388 3257end:
fc34caaa
DG
3258 return ret;
3259
5b4a0ec0 3260error:
fc34caaa 3261 /* Valid. Calling here is already in a read side lock */
fb45065e 3262 delete_ust_app_event(-1, ua_event, app);
edb67388 3263 return ret;
5b4a0ec0
DG
3264}
3265
3266/*
3267 * Create UST metadata and open it on the tracer side.
d0b96690 3268 *
7972aab2 3269 * Called with UST app session lock held and RCU read side lock.
5b4a0ec0
DG
3270 */
3271static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ad7a9107 3272 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
3273{
3274 int ret = 0;
ffe60014 3275 struct ust_app_channel *metadata;
d88aee68 3276 struct consumer_socket *socket;
7972aab2 3277 struct ust_registry_session *registry;
48a86f68 3278 struct ltt_session *session = NULL;
5b4a0ec0 3279
ffe60014
DG
3280 assert(ua_sess);
3281 assert(app);
d88aee68 3282 assert(consumer);
5b4a0ec0 3283
7972aab2 3284 registry = get_session_registry(ua_sess);
fad1ed2f 3285 /* The UST app session is held registry shall not be null. */
7972aab2
DG
3286 assert(registry);
3287
ce34fcd0
MD
3288 pthread_mutex_lock(&registry->lock);
3289
1b532a60
DG
3290 /* Metadata already exists for this registry or it was closed previously */
3291 if (registry->metadata_key || registry->metadata_closed) {
7972aab2
DG
3292 ret = 0;
3293 goto error;
5b4a0ec0
DG
3294 }
3295
ffe60014 3296 /* Allocate UST metadata */
d0b96690 3297 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
3298 if (!metadata) {
3299 /* malloc() failed */
3300 ret = -ENOMEM;
3301 goto error;
3302 }
5b4a0ec0 3303
ad7a9107 3304 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
5b4a0ec0 3305
7972aab2
DG
3306 /* Need one fd for the channel. */
3307 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3308 if (ret < 0) {
3309 ERR("Exhausted number of available FD upon create metadata");
3310 goto error;
3311 }
3312
4dc3dfc5
DG
3313 /* Get the right consumer socket for the application. */
3314 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3315 if (!socket) {
3316 ret = -EINVAL;
3317 goto error_consumer;
3318 }
3319
331744e3
JD
3320 /*
3321 * Keep metadata key so we can identify it on the consumer side. Assign it
3322 * to the registry *before* we ask the consumer so we avoid the race of the
3323 * consumer requesting the metadata and the ask_channel call on our side
3324 * did not returned yet.
3325 */
3326 registry->metadata_key = metadata->key;
3327
e098433c
JG
3328 session = session_find_by_id(ua_sess->tracing_id);
3329 assert(session);
3330
3331 assert(pthread_mutex_trylock(&session->lock));
3332 assert(session_trylock_list());
3333
d88aee68
DG
3334 /*
3335 * Ask the metadata channel creation to the consumer. The metadata object
3336 * will be created by the consumer and kept their. However, the stream is
3337 * never added or monitored until we do a first push metadata to the
3338 * consumer.
3339 */
7972aab2 3340 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
e5148e25 3341 registry, session->current_trace_chunk);
d88aee68 3342 if (ret < 0) {
f2a444f1
DG
3343 /* Nullify the metadata key so we don't try to close it later on. */
3344 registry->metadata_key = 0;
d88aee68
DG
3345 goto error_consumer;
3346 }
3347
3348 /*
3349 * The setup command will make the metadata stream be sent to the relayd,
3350 * if applicable, and the thread managing the metadatas. This is important
3351 * because after this point, if an error occurs, the only way the stream
3352 * can be deleted is to be monitored in the consumer.
3353 */
7972aab2 3354 ret = consumer_setup_metadata(socket, metadata->key);
ffe60014 3355 if (ret < 0) {
f2a444f1
DG
3356 /* Nullify the metadata key so we don't try to close it later on. */
3357 registry->metadata_key = 0;
d88aee68 3358 goto error_consumer;
5b4a0ec0
DG
3359 }
3360
7972aab2
DG
3361 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3362 metadata->key, app->pid);
5b4a0ec0 3363
d88aee68 3364error_consumer:
b80f0b6c 3365 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 3366 delete_ust_app_channel(-1, metadata, app);
5b4a0ec0 3367error:
ce34fcd0 3368 pthread_mutex_unlock(&registry->lock);
48a86f68
JG
3369 if (session) {
3370 session_put(session);
3371 }
ffe60014 3372 return ret;
5b4a0ec0
DG
3373}
3374
5b4a0ec0 3375/*
d88aee68
DG
3376 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3377 * acquired before calling this function.
5b4a0ec0
DG
3378 */
3379struct ust_app *ust_app_find_by_pid(pid_t pid)
3380{
d88aee68 3381 struct ust_app *app = NULL;
bec39940
DG
3382 struct lttng_ht_node_ulong *node;
3383 struct lttng_ht_iter iter;
5b4a0ec0 3384
bec39940
DG
3385 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3386 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
3387 if (node == NULL) {
3388 DBG2("UST app no found with pid %d", pid);
3389 goto error;
3390 }
5b4a0ec0
DG
3391
3392 DBG2("Found UST app by pid %d", pid);
3393
d88aee68 3394 app = caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
3395
3396error:
d88aee68 3397 return app;
5b4a0ec0
DG
3398}
3399
d88aee68
DG
3400/*
3401 * Allocate and init an UST app object using the registration information and
3402 * the command socket. This is called when the command socket connects to the
3403 * session daemon.
3404 *
3405 * The object is returned on success or else NULL.
3406 */
d0b96690 3407struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
5b4a0ec0 3408{
d0b96690
DG
3409 struct ust_app *lta = NULL;
3410
3411 assert(msg);
3412 assert(sock >= 0);
3413
3414 DBG3("UST app creating application for socket %d", sock);
5b4a0ec0 3415
173af62f
DG
3416 if ((msg->bits_per_long == 64 &&
3417 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3418 || (msg->bits_per_long == 32 &&
3419 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 3420 ERR("Registration failed: application \"%s\" (pid: %d) has "
d0b96690
DG
3421 "%d-bit long, but no consumerd for this size is available.\n",
3422 msg->name, msg->pid, msg->bits_per_long);
3423 goto error;
3f2c5fcc 3424 }
d0b96690 3425
5b4a0ec0
DG
3426 lta = zmalloc(sizeof(struct ust_app));
3427 if (lta == NULL) {
3428 PERROR("malloc");
d0b96690 3429 goto error;
5b4a0ec0
DG
3430 }
3431
3432 lta->ppid = msg->ppid;
3433 lta->uid = msg->uid;
3434 lta->gid = msg->gid;
d0b96690 3435
7753dea8 3436 lta->bits_per_long = msg->bits_per_long;
d0b96690
DG
3437 lta->uint8_t_alignment = msg->uint8_t_alignment;
3438 lta->uint16_t_alignment = msg->uint16_t_alignment;
3439 lta->uint32_t_alignment = msg->uint32_t_alignment;
3440 lta->uint64_t_alignment = msg->uint64_t_alignment;
3441 lta->long_alignment = msg->long_alignment;
3442 lta->byte_order = msg->byte_order;
3443
5b4a0ec0
DG
3444 lta->v_major = msg->major;
3445 lta->v_minor = msg->minor;
d9bf3ca4 3446 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d0b96690 3447 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
10b56aef 3448 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
d0b96690 3449 lta->notify_sock = -1;
d88aee68
DG
3450
3451 /* Copy name and make sure it's NULL terminated. */
3452 strncpy(lta->name, msg->name, sizeof(lta->name));
3453 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3454
3455 /*
3456 * Before this can be called, when receiving the registration information,
3457 * the application compatibility is checked. So, at this point, the
3458 * application can work with this session daemon.
3459 */
d0b96690 3460 lta->compatible = 1;
5b4a0ec0 3461
852d0037 3462 lta->pid = msg->pid;
d0b96690 3463 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
852d0037 3464 lta->sock = sock;
fb45065e 3465 pthread_mutex_init(&lta->sock_lock, NULL);
d0b96690 3466 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
5b4a0ec0 3467
d42f20df 3468 CDS_INIT_LIST_HEAD(&lta->teardown_head);
d0b96690
DG
3469error:
3470 return lta;
3471}
3472
d88aee68
DG
3473/*
3474 * For a given application object, add it to every hash table.
3475 */
d0b96690
DG
3476void ust_app_add(struct ust_app *app)
3477{
3478 assert(app);
3479 assert(app->notify_sock >= 0);
3480
cd82d919
JR
3481 app->registration_time = time(NULL);
3482
5b4a0ec0 3483 rcu_read_lock();
852d0037
DG
3484
3485 /*
3486 * On a re-registration, we want to kick out the previous registration of
3487 * that pid
3488 */
d0b96690 3489 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
852d0037
DG
3490
3491 /*
3492 * The socket _should_ be unique until _we_ call close. So, a add_unique
3493 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3494 * already in the table.
3495 */
d0b96690 3496 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
852d0037 3497
d0b96690
DG
3498 /* Add application to the notify socket hash table. */
3499 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3500 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
5b4a0ec0 3501
ee3f5169
JR
3502 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
3503 "notify_sock =%d (version %d.%d)", app->pid, app->ppid, app->uid,
d88aee68
DG
3504 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3505 app->v_minor);
5b4a0ec0 3506
d0b96690
DG
3507 rcu_read_unlock();
3508}
3509
d88aee68
DG
3510/*
3511 * Set the application version into the object.
3512 *
3513 * Return 0 on success else a negative value either an errno code or a
3514 * LTTng-UST error code.
3515 */
d0b96690
DG
3516int ust_app_version(struct ust_app *app)
3517{
d88aee68
DG
3518 int ret;
3519
d0b96690 3520 assert(app);
d88aee68 3521
fb45065e 3522 pthread_mutex_lock(&app->sock_lock);
d88aee68 3523 ret = ustctl_tracer_version(app->sock, &app->version);
fb45065e 3524 pthread_mutex_unlock(&app->sock_lock);
d88aee68 3525 if (ret < 0) {
ee3f5169
JR
3526 if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) {
3527 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
3528 app->pid, app->sock);
3529 } else if (ret == -EAGAIN) {
3530 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
3531 app->pid, app->sock);
d88aee68 3532 } else {
ee3f5169
JR
3533 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
3534 ret, app->pid, app->sock);
d88aee68
DG
3535 }
3536 }
3537
3538 return ret;
5b4a0ec0
DG
3539}
3540
3541/*
3542 * Unregister app by removing it from the global traceable app list and freeing
3543 * the data struct.
3544 *
3545 * The socket is already closed at this point so no close to sock.
3546 */
3547void ust_app_unregister(int sock)
3548{
3549 struct ust_app *lta;
bec39940 3550 struct lttng_ht_node_ulong *node;
c4b88406 3551 struct lttng_ht_iter ust_app_sock_iter;
bec39940 3552 struct lttng_ht_iter iter;
d42f20df 3553 struct ust_app_session *ua_sess;
525b0740 3554 int ret;
5b4a0ec0
DG
3555
3556 rcu_read_lock();
886459c6 3557
5b4a0ec0 3558 /* Get the node reference for a call_rcu */
c4b88406
MD
3559 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3560 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
d0b96690 3561 assert(node);
284d8f55 3562
852d0037 3563 lta = caa_container_of(node, struct ust_app, sock_n);
852d0037
DG
3564 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3565
d88aee68 3566 /*
ce34fcd0
MD
3567 * For per-PID buffers, perform "push metadata" and flush all
3568 * application streams before removing app from hash tables,
3569 * ensuring proper behavior of data_pending check.
c4b88406 3570 * Remove sessions so they are not visible during deletion.
d88aee68 3571 */
d42f20df
DG
3572 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3573 node.node) {
7972aab2
DG
3574 struct ust_registry_session *registry;
3575
d42f20df
DG
3576 ret = lttng_ht_del(lta->sessions, &iter);
3577 if (ret) {
3578 /* The session was already removed so scheduled for teardown. */
3579 continue;
3580 }
3581
ce34fcd0
MD
3582 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
3583 (void) ust_app_flush_app_session(lta, ua_sess);
3584 }
c4b88406 3585
d42f20df
DG
3586 /*
3587 * Add session to list for teardown. This is safe since at this point we
3588 * are the only one using this list.
3589 */
d88aee68
DG
3590 pthread_mutex_lock(&ua_sess->lock);
3591
b161602a
MD
3592 if (ua_sess->deleted) {
3593 pthread_mutex_unlock(&ua_sess->lock);
3594 continue;
3595 }
3596
d88aee68
DG
3597 /*
3598 * Normally, this is done in the delete session process which is
3599 * executed in the call rcu below. However, upon registration we can't
3600 * afford to wait for the grace period before pushing data or else the
3601 * data pending feature can race between the unregistration and stop
3602 * command where the data pending command is sent *before* the grace
3603 * period ended.
3604 *
3605 * The close metadata below nullifies the metadata pointer in the
3606 * session so the delete session will NOT push/close a second time.
3607 */
7972aab2 3608 registry = get_session_registry(ua_sess);
ce34fcd0 3609 if (registry) {
7972aab2
DG
3610 /* Push metadata for application before freeing the application. */
3611 (void) push_metadata(registry, ua_sess->consumer);
3612
3613 /*
3614 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
3615 * metadata only on destroy trace session in this case. Also, the
3616 * previous push metadata could have flag the metadata registry to
3617 * close so don't send a close command if closed.
7972aab2 3618 */
ce34fcd0 3619 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
3620 /* And ask to close it for this session registry. */
3621 (void) close_metadata(registry, ua_sess->consumer);
3622 }
3623 }
d42f20df 3624 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
c4b88406 3625
d88aee68 3626 pthread_mutex_unlock(&ua_sess->lock);
d42f20df
DG
3627 }
3628
c4b88406
MD
3629 /* Remove application from PID hash table */
3630 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3631 assert(!ret);
3632
3633 /*
3634 * Remove application from notify hash table. The thread handling the
3635 * notify socket could have deleted the node so ignore on error because
c48239ca
JG
3636 * either way it's valid. The close of that socket is handled by the
3637 * apps_notify_thread.
c4b88406
MD
3638 */
3639 iter.iter.node = &lta->notify_sock_n.node;
3640 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3641
3642 /*
3643 * Ignore return value since the node might have been removed before by an
3644 * add replace during app registration because the PID can be reassigned by
3645 * the OS.
3646 */
3647 iter.iter.node = &lta->pid_n.node;
3648 ret = lttng_ht_del(ust_app_ht, &iter);
3649 if (ret) {
3650 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3651 lta->pid);
3652 }
3653
852d0037
DG
3654 /* Free memory */
3655 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3656
5b4a0ec0
DG
3657 rcu_read_unlock();
3658 return;
284d8f55
DG
3659}
3660
5b4a0ec0
DG
3661/*
3662 * Fill events array with all events name of all registered apps.
3663 */
3664int ust_app_list_events(struct lttng_event **events)
421cb601 3665{
5b4a0ec0
DG
3666 int ret, handle;
3667 size_t nbmem, count = 0;
bec39940 3668 struct lttng_ht_iter iter;
5b4a0ec0 3669 struct ust_app *app;
c617c0c6 3670 struct lttng_event *tmp_event;
421cb601 3671
5b4a0ec0 3672 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
3673 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3674 if (tmp_event == NULL) {
5b4a0ec0
DG
3675 PERROR("zmalloc ust app events");
3676 ret = -ENOMEM;
421cb601
DG
3677 goto error;
3678 }
3679
5b4a0ec0 3680 rcu_read_lock();
421cb601 3681
852d0037 3682 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
90eaa0d2 3683 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 3684
840cb59c 3685 health_code_update();
86acf0da 3686
e0c7ec2b
DG
3687 if (!app->compatible) {
3688 /*
3689 * TODO: In time, we should notice the caller of this error by
3690 * telling him that this is a version error.
3691 */
3692 continue;
3693 }
fb45065e 3694 pthread_mutex_lock(&app->sock_lock);
852d0037 3695 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0 3696 if (handle < 0) {
ffe60014
DG
3697 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3698 ERR("UST app list events getting handle failed for app pid %d",
3699 app->pid);
3700 }
fb45065e 3701 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
3702 continue;
3703 }
421cb601 3704
852d0037 3705 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
fb54cdbf 3706 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
3707 /* Handle ustctl error. */
3708 if (ret < 0) {
fb45065e
MD
3709 int release_ret;
3710
a2ba1ab0 3711 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
3712 ERR("UST app tp list get failed for app %d with ret %d",
3713 app->sock, ret);
3714 } else {
3715 DBG3("UST app tp list get failed. Application is dead");
3757b385 3716 break;
ffe60014 3717 }
98f595d4 3718 free(tmp_event);
fb45065e 3719 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
3720 if (release_ret < 0 &&
3721 release_ret != -LTTNG_UST_ERR_EXITING &&
3722 release_ret != -EPIPE) {
fb45065e
MD
3723 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3724 }
3725 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
3726 goto rcu_error;
3727 }
3728
840cb59c 3729 health_code_update();
815564d8 3730 if (count >= nbmem) {
d7b3776f 3731 /* In case the realloc fails, we free the memory */
53efb85a
MD
3732 struct lttng_event *new_tmp_event;
3733 size_t new_nbmem;
3734
3735 new_nbmem = nbmem << 1;
3736 DBG2("Reallocating event list from %zu to %zu entries",
3737 nbmem, new_nbmem);
3738 new_tmp_event = realloc(tmp_event,
3739 new_nbmem * sizeof(struct lttng_event));
3740 if (new_tmp_event == NULL) {
fb45065e
MD
3741 int release_ret;
3742
5b4a0ec0 3743 PERROR("realloc ust app events");
c617c0c6 3744 free(tmp_event);
5b4a0ec0 3745 ret = -ENOMEM;
fb45065e 3746 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
3747 if (release_ret < 0 &&
3748 release_ret != -LTTNG_UST_ERR_EXITING &&
3749 release_ret != -EPIPE) {
fb45065e
MD
3750 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3751 }
3752 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
3753 goto rcu_error;
3754 }
53efb85a
MD
3755 /* Zero the new memory */
3756 memset(new_tmp_event + nbmem, 0,
3757 (new_nbmem - nbmem) * sizeof(struct lttng_event));
3758 nbmem = new_nbmem;
3759 tmp_event = new_tmp_event;
5b4a0ec0 3760 }
c617c0c6
MD
3761 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
3762 tmp_event[count].loglevel = uiter.loglevel;
3763 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
3764 tmp_event[count].pid = app->pid;
3765 tmp_event[count].enabled = -1;
5b4a0ec0 3766 count++;
421cb601 3767 }
fb45065e
MD
3768 ret = ustctl_release_handle(app->sock, handle);
3769 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
3770 if (ret < 0) {
3771 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3772 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
3773 app->pid, app->sock);
3774 } else if (ret == -EAGAIN) {
3775 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
3776 app->pid, app->sock);
3777 } else {
3778 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
3779 ret, app->pid, app->sock);
3780 }
fb45065e 3781 }
421cb601
DG
3782 }
3783
5b4a0ec0 3784 ret = count;
c617c0c6 3785 *events = tmp_event;
421cb601 3786
5b4a0ec0 3787 DBG2("UST app list events done (%zu events)", count);
421cb601 3788
5b4a0ec0
DG
3789rcu_error:
3790 rcu_read_unlock();
421cb601 3791error:
840cb59c 3792 health_code_update();
5b4a0ec0 3793 return ret;
421cb601
DG
3794}
3795
f37d259d
MD
3796/*
3797 * Fill events array with all events name of all registered apps.
3798 */
3799int ust_app_list_event_fields(struct lttng_event_field **fields)
3800{
3801 int ret, handle;
3802 size_t nbmem, count = 0;
3803 struct lttng_ht_iter iter;
3804 struct ust_app *app;
c617c0c6 3805 struct lttng_event_field *tmp_event;
f37d259d
MD
3806
3807 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
3808 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
3809 if (tmp_event == NULL) {
f37d259d
MD
3810 PERROR("zmalloc ust app event fields");
3811 ret = -ENOMEM;
3812 goto error;
3813 }
3814
3815 rcu_read_lock();
3816
3817 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3818 struct lttng_ust_field_iter uiter;
3819
840cb59c 3820 health_code_update();
86acf0da 3821
f37d259d
MD
3822 if (!app->compatible) {
3823 /*
3824 * TODO: In time, we should notice the caller of this error by
3825 * telling him that this is a version error.
3826 */
3827 continue;
3828 }
fb45065e 3829 pthread_mutex_lock(&app->sock_lock);
f37d259d
MD
3830 handle = ustctl_tracepoint_field_list(app->sock);
3831 if (handle < 0) {
ffe60014
DG
3832 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3833 ERR("UST app list field getting handle failed for app pid %d",
3834 app->pid);
3835 }
fb45065e 3836 pthread_mutex_unlock(&app->sock_lock);
f37d259d
MD
3837 continue;
3838 }
3839
3840 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
fb54cdbf 3841 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
3842 /* Handle ustctl error. */
3843 if (ret < 0) {
fb45065e
MD
3844 int release_ret;
3845
a2ba1ab0 3846 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
3847 ERR("UST app tp list field failed for app %d with ret %d",
3848 app->sock, ret);
3849 } else {
3850 DBG3("UST app tp list field failed. Application is dead");
3757b385 3851 break;
ffe60014 3852 }
98f595d4 3853 free(tmp_event);
fb45065e
MD
3854 release_ret = ustctl_release_handle(app->sock, handle);
3855 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3856 if (release_ret < 0 &&
3857 release_ret != -LTTNG_UST_ERR_EXITING &&
3858 release_ret != -EPIPE) {
fb45065e
MD
3859 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3860 }
ffe60014
DG
3861 goto rcu_error;
3862 }
3863
840cb59c 3864 health_code_update();
f37d259d 3865 if (count >= nbmem) {
d7b3776f 3866 /* In case the realloc fails, we free the memory */
53efb85a
MD
3867 struct lttng_event_field *new_tmp_event;
3868 size_t new_nbmem;
3869
3870 new_nbmem = nbmem << 1;
3871 DBG2("Reallocating event field list from %zu to %zu entries",
3872 nbmem, new_nbmem);
3873 new_tmp_event = realloc(tmp_event,
3874 new_nbmem * sizeof(struct lttng_event_field));
3875 if (new_tmp_event == NULL) {
fb45065e
MD
3876 int release_ret;
3877
f37d259d 3878 PERROR("realloc ust app event fields");
c617c0c6 3879 free(tmp_event);
f37d259d 3880 ret = -ENOMEM;
fb45065e
MD
3881 release_ret = ustctl_release_handle(app->sock, handle);
3882 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3883 if (release_ret &&
3884 release_ret != -LTTNG_UST_ERR_EXITING &&
3885 release_ret != -EPIPE) {
fb45065e
MD
3886 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3887 }
f37d259d
MD
3888 goto rcu_error;
3889 }
53efb85a
MD
3890 /* Zero the new memory */
3891 memset(new_tmp_event + nbmem, 0,
3892 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
3893 nbmem = new_nbmem;
3894 tmp_event = new_tmp_event;
f37d259d 3895 }
f37d259d 3896
c617c0c6 3897 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
2e84128e
DG
3898 /* Mapping between these enums matches 1 to 1. */
3899 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
c617c0c6 3900 tmp_event[count].nowrite = uiter.nowrite;
f37d259d 3901
c617c0c6
MD
3902 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
3903 tmp_event[count].event.loglevel = uiter.loglevel;
2e84128e 3904 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
c617c0c6
MD
3905 tmp_event[count].event.pid = app->pid;
3906 tmp_event[count].event.enabled = -1;
f37d259d
MD
3907 count++;
3908 }
fb45065e
MD
3909 ret = ustctl_release_handle(app->sock, handle);
3910 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3911 if (ret < 0 &&
3912 ret != -LTTNG_UST_ERR_EXITING &&
3913 ret != -EPIPE) {
fb45065e
MD
3914 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3915 }
f37d259d
MD
3916 }
3917
3918 ret = count;
c617c0c6 3919 *fields = tmp_event;
f37d259d
MD
3920
3921 DBG2("UST app list event fields done (%zu events)", count);
3922
3923rcu_error:
3924 rcu_read_unlock();
3925error:
840cb59c 3926 health_code_update();
f37d259d
MD
3927 return ret;
3928}
3929
5b4a0ec0
DG
3930/*
3931 * Free and clean all traceable apps of the global list.
36b588ed
MD
3932 *
3933 * Should _NOT_ be called with RCU read-side lock held.
5b4a0ec0
DG
3934 */
3935void ust_app_clean_list(void)
421cb601 3936{
5b4a0ec0 3937 int ret;
659ed79f 3938 struct ust_app *app;
bec39940 3939 struct lttng_ht_iter iter;
421cb601 3940
5b4a0ec0 3941 DBG2("UST app cleaning registered apps hash table");
421cb601 3942
5b4a0ec0 3943 rcu_read_lock();
421cb601 3944
44975bc9
JG
3945 /* Cleanup notify socket hash table */
3946 if (ust_app_ht_by_notify_sock) {
3947 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
3948 notify_sock_n.node) {
3949 struct cds_lfht_node *node;
3950 struct ust_app *app;
3951
3952 node = cds_lfht_iter_get_node(&iter.iter);
3953 if (!node) {
3954 continue;
3955 }
3956
3957 app = container_of(node, struct ust_app,
3958 notify_sock_n.node);
3959 ust_app_notify_sock_unregister(app->notify_sock);
3960 }
3961 }
3962
f1b711c4
MD
3963 if (ust_app_ht) {
3964 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3965 ret = lttng_ht_del(ust_app_ht, &iter);
3966 assert(!ret);
3967 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
3968 }
421cb601
DG
3969 }
3970
852d0037 3971 /* Cleanup socket hash table */
f1b711c4
MD
3972 if (ust_app_ht_by_sock) {
3973 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
3974 sock_n.node) {
3975 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
3976 assert(!ret);
3977 }
bec39940 3978 }
852d0037 3979
36b588ed 3980 rcu_read_unlock();
d88aee68 3981
bec39940 3982 /* Destroy is done only when the ht is empty */
f1b711c4
MD
3983 if (ust_app_ht) {
3984 ht_cleanup_push(ust_app_ht);
3985 }
3986 if (ust_app_ht_by_sock) {
3987 ht_cleanup_push(ust_app_ht_by_sock);
3988 }
3989 if (ust_app_ht_by_notify_sock) {
3990 ht_cleanup_push(ust_app_ht_by_notify_sock);
3991 }
5b4a0ec0
DG
3992}
3993
3994/*
3995 * Init UST app hash table.
3996 */
57703f6e 3997int ust_app_ht_alloc(void)
5b4a0ec0 3998{
bec39940 3999 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4000 if (!ust_app_ht) {
4001 return -1;
4002 }
852d0037 4003 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4004 if (!ust_app_ht_by_sock) {
4005 return -1;
4006 }
d0b96690 4007 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4008 if (!ust_app_ht_by_notify_sock) {
4009 return -1;
4010 }
4011 return 0;
421cb601
DG
4012}
4013
78f0bacd
DG
4014/*
4015 * For a specific UST session, disable the channel for all registered apps.
4016 */
35a9059d 4017int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
4018 struct ltt_ust_channel *uchan)
4019{
4020 int ret = 0;
bec39940
DG
4021 struct lttng_ht_iter iter;
4022 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
4023 struct ust_app *app;
4024 struct ust_app_session *ua_sess;
8535a6d9 4025 struct ust_app_channel *ua_chan;
78f0bacd 4026
0ce9aa93 4027 assert(usess->active);
d9bf3ca4 4028 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
a991f516 4029 uchan->name, usess->id);
78f0bacd
DG
4030
4031 rcu_read_lock();
4032
4033 /* For every registered applications */
852d0037 4034 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 4035 struct lttng_ht_iter uiter;
e0c7ec2b
DG
4036 if (!app->compatible) {
4037 /*
4038 * TODO: In time, we should notice the caller of this error by
4039 * telling him that this is a version error.
4040 */
4041 continue;
4042 }
78f0bacd
DG
4043 ua_sess = lookup_session_by_app(usess, app);
4044 if (ua_sess == NULL) {
4045 continue;
4046 }
4047
8535a6d9 4048 /* Get channel */
bec39940
DG
4049 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4050 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
4051 /* If the session if found for the app, the channel must be there */
4052 assert(ua_chan_node);
4053
4054 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4055 /* The channel must not be already disabled */
4056 assert(ua_chan->enabled == 1);
4057
4058 /* Disable channel onto application */
4059 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
4060 if (ret < 0) {
4061 /* XXX: We might want to report this error at some point... */
4062 continue;
4063 }
4064 }
4065
4066 rcu_read_unlock();
78f0bacd
DG
4067 return ret;
4068}
4069
4070/*
4071 * For a specific UST session, enable the channel for all registered apps.
4072 */
35a9059d 4073int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
4074 struct ltt_ust_channel *uchan)
4075{
4076 int ret = 0;
bec39940 4077 struct lttng_ht_iter iter;
78f0bacd
DG
4078 struct ust_app *app;
4079 struct ust_app_session *ua_sess;
4080
0ce9aa93 4081 assert(usess->active);
d9bf3ca4 4082 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
a991f516 4083 uchan->name, usess->id);
78f0bacd
DG
4084
4085 rcu_read_lock();
4086
4087 /* For every registered applications */
852d0037 4088 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4089 if (!app->compatible) {
4090 /*
4091 * TODO: In time, we should notice the caller of this error by
4092 * telling him that this is a version error.
4093 */
4094 continue;
4095 }
78f0bacd
DG
4096 ua_sess = lookup_session_by_app(usess, app);
4097 if (ua_sess == NULL) {
4098 continue;
4099 }
4100
4101 /* Enable channel onto application */
4102 ret = enable_ust_app_channel(ua_sess, uchan, app);
4103 if (ret < 0) {
4104 /* XXX: We might want to report this error at some point... */
4105 continue;
4106 }
4107 }
4108
4109 rcu_read_unlock();
78f0bacd
DG
4110 return ret;
4111}
4112
b0a40d28
DG
4113/*
4114 * Disable an event in a channel and for a specific session.
4115 */
35a9059d
DG
4116int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4117 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
4118{
4119 int ret = 0;
bec39940 4120 struct lttng_ht_iter iter, uiter;
700c5a9d 4121 struct lttng_ht_node_str *ua_chan_node;
b0a40d28
DG
4122 struct ust_app *app;
4123 struct ust_app_session *ua_sess;
4124 struct ust_app_channel *ua_chan;
4125 struct ust_app_event *ua_event;
4126
0ce9aa93 4127 assert(usess->active);
b0a40d28 4128 DBG("UST app disabling event %s for all apps in channel "
d9bf3ca4
MD
4129 "%s for session id %" PRIu64,
4130 uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
4131
4132 rcu_read_lock();
4133
4134 /* For all registered applications */
852d0037 4135 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4136 if (!app->compatible) {
4137 /*
4138 * TODO: In time, we should notice the caller of this error by
4139 * telling him that this is a version error.
4140 */
4141 continue;
4142 }
b0a40d28
DG
4143 ua_sess = lookup_session_by_app(usess, app);
4144 if (ua_sess == NULL) {
4145 /* Next app */
4146 continue;
4147 }
4148
4149 /* Lookup channel in the ust app session */
bec39940
DG
4150 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4151 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 4152 if (ua_chan_node == NULL) {
d9bf3ca4 4153 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
852d0037 4154 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
4155 continue;
4156 }
4157 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4158
700c5a9d
JR
4159 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4160 uevent->filter, uevent->attr.loglevel,
4161 uevent->exclusion);
4162 if (ua_event == NULL) {
b0a40d28 4163 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 4164 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
4165 continue;
4166 }
b0a40d28 4167
7f79d3a1 4168 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
4169 if (ret < 0) {
4170 /* XXX: Report error someday... */
4171 continue;
4172 }
4173 }
4174
4175 rcu_read_unlock();
0ce9aa93
JG
4176 return ret;
4177}
4178
4179/* The ua_sess lock must be held by the caller. */
4180static
4181int ust_app_channel_create(struct ltt_ust_session *usess,
4182 struct ust_app_session *ua_sess,
4183 struct ltt_ust_channel *uchan, struct ust_app *app,
4184 struct ust_app_channel **_ua_chan)
4185{
4186 int ret = 0;
4187 struct ust_app_channel *ua_chan = NULL;
4188
4189 assert(ua_sess);
4190 ASSERT_LOCKED(ua_sess->lock);
4191
4192 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4193 sizeof(uchan->name))) {
4194 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
4195 &uchan->attr);
4196 ret = 0;
4197 } else {
4198 struct ltt_ust_context *uctx = NULL;
4199
4200 /*
4201 * Create channel onto application and synchronize its
4202 * configuration.
4203 */
4204 ret = ust_app_channel_allocate(ua_sess, uchan,
4205 LTTNG_UST_CHAN_PER_CPU, usess,
4206 &ua_chan);
14a5bf28
JR
4207 if (ret < 0) {
4208 goto error;
4209 }
4210
4211 ret = ust_app_channel_send(app, usess,
4212 ua_sess, ua_chan);
4213 if (ret) {
4214 goto error;
0ce9aa93
JG
4215 }
4216
4217 /* Add contexts. */
4218 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
4219 ret = create_ust_app_channel_context(ua_chan,
4220 &uctx->ctx, app);
4221 if (ret) {
14a5bf28 4222 goto error;
0ce9aa93
JG
4223 }
4224 }
4225 }
14a5bf28
JR
4226
4227error:
0ce9aa93
JG
4228 if (ret < 0) {
4229 switch (ret) {
4230 case -ENOTCONN:
4231 /*
4232 * The application's socket is not valid. Either a bad socket
4233 * or a timeout on it. We can't inform the caller that for a
4234 * specific app, the session failed so lets continue here.
4235 */
4236 ret = 0; /* Not an error. */
4237 break;
4238 case -ENOMEM:
4239 default:
4240 break;
4241 }
4242 }
14a5bf28 4243
0ce9aa93
JG
4244 if (ret == 0 && _ua_chan) {
4245 /*
4246 * Only return the application's channel on success. Note
4247 * that the channel can still be part of the application's
4248 * channel hashtable on error.
4249 */
4250 *_ua_chan = ua_chan;
4251 }
b0a40d28
DG
4252 return ret;
4253}
4254
421cb601 4255/*
5b4a0ec0 4256 * For a specific UST session, create the channel for all registered apps.
421cb601 4257 */
35a9059d 4258int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
4259 struct ltt_ust_channel *uchan)
4260{
0ce9aa93
JG
4261 int ret = 0;
4262 struct cds_lfht_iter iter;
48842b30 4263 struct ust_app *app;
48842b30 4264
fc34caaa 4265 assert(usess);
0ce9aa93 4266 assert(usess->active);
fc34caaa 4267 assert(uchan);
421cb601 4268
d9bf3ca4 4269 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64,
a991f516 4270 uchan->name, usess->id);
48842b30
DG
4271
4272 rcu_read_lock();
5b4a0ec0 4273 /* For every registered applications */
0ce9aa93
JG
4274 cds_lfht_for_each_entry(ust_app_ht->ht, &iter, app, pid_n.node) {
4275 struct ust_app_session *ua_sess;
4276 int session_was_created = 0;
4277
4278 if (!app->compatible ||
4279 !trace_ust_pid_tracker_lookup(usess, app->pid)) {
4280 goto error_rcu_unlock;
a9ad0c8f
MD
4281 }
4282
edb67388
DG
4283 /*
4284 * Create session on the tracer side and add it to app session HT. Note
4285 * that if session exist, it will simply return a pointer to the ust
4286 * app session.
4287 */
0ce9aa93
JG
4288 ret = find_or_create_ust_app_session(usess, app, &ua_sess,
4289 &session_was_created);
3d8ca23b
DG
4290 if (ret < 0) {
4291 switch (ret) {
4292 case -ENOTCONN:
4293 /*
0ce9aa93
JG
4294 * The application's socket is not valid. Either a bad
4295 * socket or a timeout on it. We can't inform the caller
4296 * that for a specific app, the session failed so lets
4297 * continue here; it is not an error.
3d8ca23b 4298 */
0ce9aa93
JG
4299 ret = 0;
4300 goto error_rcu_unlock;
3d8ca23b
DG
4301 case -ENOMEM:
4302 default:
4303 goto error_rcu_unlock;
4304 }
48842b30 4305 }
b161602a
MD
4306
4307 if (ua_sess->deleted) {
b161602a
MD
4308 continue;
4309 }
0ce9aa93
JG
4310 ret = ust_app_channel_create(usess, ua_sess, uchan, app, NULL);
4311 if (ret) {
4312 if (session_was_created) {
d0b96690 4313 destroy_app_session(app, ua_sess);
3d8ca23b 4314 }
56c23a67 4315 /* Continue to the next application. */
48842b30 4316 }
48842b30 4317 }
5b4a0ec0 4318
95e047ff 4319error_rcu_unlock:
48842b30 4320 rcu_read_unlock();
3c14c33f 4321 return ret;
48842b30
DG
4322}
4323
5b4a0ec0 4324/*
edb67388 4325 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 4326 */
35a9059d 4327int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
4328 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4329{
4330 int ret = 0;
bec39940 4331 struct lttng_ht_iter iter, uiter;
18eace3b 4332 struct lttng_ht_node_str *ua_chan_node;
48842b30
DG
4333 struct ust_app *app;
4334 struct ust_app_session *ua_sess;
4335 struct ust_app_channel *ua_chan;
4336 struct ust_app_event *ua_event;
48842b30 4337
0ce9aa93 4338 assert(usess->active);
d9bf3ca4 4339 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
a991f516 4340 uevent->attr.name, usess->id);
48842b30 4341
edb67388
DG
4342 /*
4343 * NOTE: At this point, this function is called only if the session and
4344 * channel passed are already created for all apps. and enabled on the
4345 * tracer also.
4346 */
4347
48842b30 4348 rcu_read_lock();
421cb601
DG
4349
4350 /* For all registered applications */
852d0037 4351 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4352 if (!app->compatible) {
4353 /*
4354 * TODO: In time, we should notice the caller of this error by
4355 * telling him that this is a version error.
4356 */
4357 continue;
4358 }
edb67388 4359 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4360 if (!ua_sess) {
4361 /* The application has problem or is probably dead. */
4362 continue;
4363 }
ba767faf 4364
d0b96690
DG
4365 pthread_mutex_lock(&ua_sess->lock);
4366
b161602a
MD
4367 if (ua_sess->deleted) {
4368 pthread_mutex_unlock(&ua_sess->lock);
4369 continue;
4370 }
4371
edb67388 4372 /* Lookup channel in the ust app session */
bec39940
DG
4373 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4374 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
a7169585
MD
4375 /*
4376 * It is possible that the channel cannot be found is
4377 * the channel/event creation occurs concurrently with
4378 * an application exit.
4379 */
4380 if (!ua_chan_node) {
4381 pthread_mutex_unlock(&ua_sess->lock);
4382 continue;
4383 }
edb67388
DG
4384
4385 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4386
18eace3b
DG
4387 /* Get event node */
4388 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 4389 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 4390 if (ua_event == NULL) {
7f79d3a1 4391 DBG3("UST app enable event %s not found for app PID %d."
852d0037 4392 "Skipping app", uevent->attr.name, app->pid);
d0b96690 4393 goto next_app;
35a9059d 4394 }
35a9059d
DG
4395
4396 ret = enable_ust_app_event(ua_sess, ua_event, app);
4397 if (ret < 0) {
d0b96690 4398 pthread_mutex_unlock(&ua_sess->lock);
7f79d3a1 4399 goto error;
48842b30 4400 }
d0b96690
DG
4401 next_app:
4402 pthread_mutex_unlock(&ua_sess->lock);
edb67388
DG
4403 }
4404
7f79d3a1 4405error:
edb67388 4406 rcu_read_unlock();
edb67388
DG
4407 return ret;
4408}
4409
4410/*
4411 * For a specific existing UST session and UST channel, creates the event for
4412 * all registered apps.
4413 */
35a9059d 4414int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
4415 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4416{
4417 int ret = 0;
bec39940
DG
4418 struct lttng_ht_iter iter, uiter;
4419 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
4420 struct ust_app *app;
4421 struct ust_app_session *ua_sess;
4422 struct ust_app_channel *ua_chan;
4423
0ce9aa93 4424 assert(usess->active);
d9bf3ca4 4425 DBG("UST app creating event %s for all apps for session id %" PRIu64,
a991f516 4426 uevent->attr.name, usess->id);
edb67388 4427
edb67388
DG
4428 rcu_read_lock();
4429
4430 /* For all registered applications */
852d0037 4431 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4432 if (!app->compatible) {
4433 /*
4434 * TODO: In time, we should notice the caller of this error by
4435 * telling him that this is a version error.
4436 */
4437 continue;
4438 }
edb67388 4439 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4440 if (!ua_sess) {
4441 /* The application has problem or is probably dead. */
4442 continue;
4443 }
48842b30 4444
d0b96690 4445 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4446
4447 if (ua_sess->deleted) {
4448 pthread_mutex_unlock(&ua_sess->lock);
4449 continue;
4450 }
4451
48842b30 4452 /* Lookup channel in the ust app session */
bec39940
DG
4453 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4454 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
4455 /* If the channel is not found, there is a code flow error */
4456 assert(ua_chan_node);
4457
48842b30
DG
4458 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4459
edb67388 4460 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
d0b96690 4461 pthread_mutex_unlock(&ua_sess->lock);
edb67388 4462 if (ret < 0) {
49c336c1 4463 if (ret != -LTTNG_UST_ERR_EXIST) {
fc34caaa
DG
4464 /* Possible value at this point: -ENOMEM. If so, we stop! */
4465 break;
4466 }
4467 DBG2("UST app event %s already exist on app PID %d",
852d0037 4468 uevent->attr.name, app->pid);
5b4a0ec0 4469 continue;
48842b30 4470 }
48842b30 4471 }
5b4a0ec0 4472
48842b30 4473 rcu_read_unlock();
48842b30
DG
4474 return ret;
4475}
4476
5b4a0ec0
DG
4477/*
4478 * Start tracing for a specific UST session and app.
fad1ed2f
JR
4479 *
4480 * Called with UST app session lock held.
4481 *
5b4a0ec0 4482 */
b34cbebf 4483static
421cb601 4484int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
4485{
4486 int ret = 0;
48842b30 4487 struct ust_app_session *ua_sess;
48842b30 4488
852d0037 4489 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 4490
509cbaf8
MD
4491 rcu_read_lock();
4492
e0c7ec2b
DG
4493 if (!app->compatible) {
4494 goto end;
4495 }
4496
421cb601
DG
4497 ua_sess = lookup_session_by_app(usess, app);
4498 if (ua_sess == NULL) {
d42f20df
DG
4499 /* The session is in teardown process. Ignore and continue. */
4500 goto end;
421cb601 4501 }
48842b30 4502
d0b96690
DG
4503 pthread_mutex_lock(&ua_sess->lock);
4504
b161602a
MD
4505 if (ua_sess->deleted) {
4506 pthread_mutex_unlock(&ua_sess->lock);
4507 goto end;
4508 }
4509
aea829b3
DG
4510 /* Upon restart, we skip the setup, already done */
4511 if (ua_sess->started) {
8be98f9a 4512 goto skip_setup;
aea829b3 4513 }
8be98f9a 4514
840cb59c 4515 health_code_update();
86acf0da 4516
8be98f9a 4517skip_setup:
421cb601 4518 /* This start the UST tracing */
fb45065e 4519 pthread_mutex_lock(&app->sock_lock);
852d0037 4520 ret = ustctl_start_session(app->sock, ua_sess->handle);
fb45065e 4521 pthread_mutex_unlock(&app->sock_lock);
421cb601 4522 if (ret < 0) {
ee3f5169
JR
4523 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4524 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
4525 app->pid, app->sock);
3757b385
DG
4526 pthread_mutex_unlock(&ua_sess->lock);
4527 goto end;
ee3f5169
JR
4528 } else if (ret == -EAGAIN) {
4529 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
4530 app->pid, app->sock);
4531 pthread_mutex_unlock(&ua_sess->lock);
4532 goto end;
4533
4534 } else {
4535 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
4536 ret, app->pid, app->sock);
ffe60014 4537 }
d0b96690 4538 goto error_unlock;
421cb601 4539 }
5b4a0ec0 4540
55c3953d
DG
4541 /* Indicate that the session has been started once */
4542 ua_sess->started = 1;
4543
d0b96690
DG
4544 pthread_mutex_unlock(&ua_sess->lock);
4545
840cb59c 4546 health_code_update();
86acf0da 4547
421cb601 4548 /* Quiescent wait after starting trace */
fb45065e 4549 pthread_mutex_lock(&app->sock_lock);
ffe60014 4550 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4551 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
4552 if (ret < 0) {
4553 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4554 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
4555 app->pid, app->sock);
4556 } else if (ret == -EAGAIN) {
4557 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
4558 app->pid, app->sock);
4559 } else {
4560 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
4561 ret, app->pid, app->sock);
4562 }
ffe60014 4563 }
48842b30 4564
e0c7ec2b
DG
4565end:
4566 rcu_read_unlock();
840cb59c 4567 health_code_update();
421cb601 4568 return 0;
48842b30 4569
d0b96690
DG
4570error_unlock:
4571 pthread_mutex_unlock(&ua_sess->lock);
509cbaf8 4572 rcu_read_unlock();
840cb59c 4573 health_code_update();
421cb601
DG
4574 return -1;
4575}
48842b30 4576
8be98f9a
MD
4577/*
4578 * Stop tracing for a specific UST session and app.
4579 */
b34cbebf 4580static
8be98f9a
MD
4581int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
4582{
4583 int ret = 0;
4584 struct ust_app_session *ua_sess;
7972aab2 4585 struct ust_registry_session *registry;
8be98f9a 4586
852d0037 4587 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
4588
4589 rcu_read_lock();
4590
e0c7ec2b 4591 if (!app->compatible) {
d88aee68 4592 goto end_no_session;
e0c7ec2b
DG
4593 }
4594
8be98f9a
MD
4595 ua_sess = lookup_session_by_app(usess, app);
4596 if (ua_sess == NULL) {
d88aee68 4597 goto end_no_session;
8be98f9a
MD
4598 }
4599
d88aee68
DG
4600 pthread_mutex_lock(&ua_sess->lock);
4601
b161602a
MD
4602 if (ua_sess->deleted) {
4603 pthread_mutex_unlock(&ua_sess->lock);
4604 goto end_no_session;
4605 }
4606
9bc07046
DG
4607 /*
4608 * If started = 0, it means that stop trace has been called for a session
c45536e1
DG
4609 * that was never started. It's possible since we can have a fail start
4610 * from either the application manager thread or the command thread. Simply
4611 * indicate that this is a stop error.
9bc07046 4612 */
f9dfc3d9 4613 if (!ua_sess->started) {
c45536e1
DG
4614 goto error_rcu_unlock;
4615 }
7db205b5 4616
840cb59c 4617 health_code_update();
86acf0da 4618
9d6c7d3f 4619 /* This inhibits UST tracing */
fb45065e 4620 pthread_mutex_lock(&app->sock_lock);
852d0037 4621 ret = ustctl_stop_session(app->sock, ua_sess->handle);
fb45065e 4622 pthread_mutex_unlock(&app->sock_lock);
9d6c7d3f 4623 if (ret < 0) {
ee3f5169
JR
4624 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4625 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
4626 app->pid, app->sock);
3757b385 4627 goto end_unlock;
ee3f5169
JR
4628 } else if (ret == -EAGAIN) {
4629 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
4630 app->pid, app->sock);
4631 goto end_unlock;
4632
4633 } else {
4634 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
4635 ret, app->pid, app->sock);
ffe60014 4636 }
9d6c7d3f
DG
4637 goto error_rcu_unlock;
4638 }
4639
840cb59c 4640 health_code_update();
86acf0da 4641
9d6c7d3f 4642 /* Quiescent wait after stopping trace */
fb45065e 4643 pthread_mutex_lock(&app->sock_lock);
ffe60014 4644 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4645 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
4646 if (ret < 0) {
4647 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4648 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
4649 app->pid, app->sock);
4650 } else if (ret == -EAGAIN) {
4651 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
4652 app->pid, app->sock);
4653 } else {
4654 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
4655 ret, app->pid, app->sock);
4656 }
ffe60014 4657 }
9d6c7d3f 4658
840cb59c 4659 health_code_update();
86acf0da 4660
b34cbebf 4661 registry = get_session_registry(ua_sess);
fad1ed2f
JR
4662
4663 /* The UST app session is held registry shall not be null. */
b34cbebf 4664 assert(registry);
1b532a60 4665
ce34fcd0
MD
4666 /* Push metadata for application before freeing the application. */
4667 (void) push_metadata(registry, ua_sess->consumer);
b34cbebf 4668
3757b385 4669end_unlock:
b34cbebf
MD
4670 pthread_mutex_unlock(&ua_sess->lock);
4671end_no_session:
4672 rcu_read_unlock();
4673 health_code_update();
4674 return 0;
4675
4676error_rcu_unlock:
4677 pthread_mutex_unlock(&ua_sess->lock);
4678 rcu_read_unlock();
4679 health_code_update();
4680 return -1;
4681}
4682
b34cbebf 4683static
c4b88406
MD
4684int ust_app_flush_app_session(struct ust_app *app,
4685 struct ust_app_session *ua_sess)
b34cbebf 4686{
c4b88406 4687 int ret, retval = 0;
b34cbebf 4688 struct lttng_ht_iter iter;
b34cbebf 4689 struct ust_app_channel *ua_chan;
c4b88406 4690 struct consumer_socket *socket;
b34cbebf 4691
c4b88406 4692 DBG("Flushing app session buffers for ust app pid %d", app->pid);
b34cbebf
MD
4693
4694 rcu_read_lock();
4695
4696 if (!app->compatible) {
c4b88406 4697 goto end_not_compatible;
b34cbebf
MD
4698 }
4699
4700 pthread_mutex_lock(&ua_sess->lock);
4701
b161602a
MD
4702 if (ua_sess->deleted) {
4703 goto end_deleted;
4704 }
4705
b34cbebf
MD
4706 health_code_update();
4707
9d6c7d3f 4708 /* Flushing buffers */
c4b88406
MD
4709 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4710 ua_sess->consumer);
ce34fcd0
MD
4711
4712 /* Flush buffers and push metadata. */
4713 switch (ua_sess->buffer_type) {
4714 case LTTNG_BUFFER_PER_PID:
4715 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4716 node.node) {
4717 health_code_update();
ce34fcd0
MD
4718 ret = consumer_flush_channel(socket, ua_chan->key);
4719 if (ret) {
4720 ERR("Error flushing consumer channel");
4721 retval = -1;
4722 continue;
4723 }
8be98f9a 4724 }
ce34fcd0
MD
4725 break;
4726 case LTTNG_BUFFER_PER_UID:
4727 default:
4728 assert(0);
4729 break;
8be98f9a 4730 }
8be98f9a 4731
840cb59c 4732 health_code_update();
86acf0da 4733
b161602a 4734end_deleted:
d88aee68 4735 pthread_mutex_unlock(&ua_sess->lock);
ce34fcd0 4736
c4b88406
MD
4737end_not_compatible:
4738 rcu_read_unlock();
4739 health_code_update();
4740 return retval;
4741}
4742
4743/*
ce34fcd0
MD
4744 * Flush buffers for all applications for a specific UST session.
4745 * Called with UST session lock held.
c4b88406
MD
4746 */
4747static
ce34fcd0 4748int ust_app_flush_session(struct ltt_ust_session *usess)
c4b88406
MD
4749
4750{
99b1411c 4751 int ret = 0;
c4b88406 4752
ce34fcd0 4753 DBG("Flushing session buffers for all ust apps");
c4b88406
MD
4754
4755 rcu_read_lock();
4756
ce34fcd0
MD
4757 /* Flush buffers and push metadata. */
4758 switch (usess->buffer_type) {
4759 case LTTNG_BUFFER_PER_UID:
4760 {
4761 struct buffer_reg_uid *reg;
4762 struct lttng_ht_iter iter;
4763
4764 /* Flush all per UID buffers associated to that session. */
4765 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4766 struct ust_registry_session *ust_session_reg;
4767 struct buffer_reg_channel *reg_chan;
4768 struct consumer_socket *socket;
4769
4770 /* Get consumer socket to use to push the metadata.*/
4771 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
4772 usess->consumer);
4773 if (!socket) {
4774 /* Ignore request if no consumer is found for the session. */
4775 continue;
4776 }
4777
4778 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
4779 reg_chan, node.node) {
4780 /*
4781 * The following call will print error values so the return
4782 * code is of little importance because whatever happens, we
4783 * have to try them all.
4784 */
4785 (void) consumer_flush_channel(socket, reg_chan->consumer_key);
4786 }
4787
4788 ust_session_reg = reg->registry->reg.ust;
4789 /* Push metadata. */
4790 (void) push_metadata(ust_session_reg, usess->consumer);
4791 }
ce34fcd0
MD
4792 break;
4793 }
4794 case LTTNG_BUFFER_PER_PID:
4795 {
4796 struct ust_app_session *ua_sess;
4797 struct lttng_ht_iter iter;
4798 struct ust_app *app;
4799
4800 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4801 ua_sess = lookup_session_by_app(usess, app);
4802 if (ua_sess == NULL) {
4803 continue;
4804 }
4805 (void) ust_app_flush_app_session(app, ua_sess);
4806 }
4807 break;
4808 }
4809 default:
99b1411c 4810 ret = -1;
ce34fcd0
MD
4811 assert(0);
4812 break;
c4b88406 4813 }
c4b88406 4814
7db205b5 4815 rcu_read_unlock();
840cb59c 4816 health_code_update();
c4b88406 4817 return ret;
8be98f9a
MD
4818}
4819
0dd01979
MD
4820static
4821int ust_app_clear_quiescent_app_session(struct ust_app *app,
4822 struct ust_app_session *ua_sess)
4823{
4824 int ret = 0;
4825 struct lttng_ht_iter iter;
4826 struct ust_app_channel *ua_chan;
4827 struct consumer_socket *socket;
4828
4829 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
4830
4831 rcu_read_lock();
4832
4833 if (!app->compatible) {
4834 goto end_not_compatible;
4835 }
4836
4837 pthread_mutex_lock(&ua_sess->lock);
4838
4839 if (ua_sess->deleted) {
4840 goto end_unlock;
4841 }
4842
4843 health_code_update();
4844
4845 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4846 ua_sess->consumer);
4847 if (!socket) {
4848 ERR("Failed to find consumer (%" PRIu32 ") socket",
4849 app->bits_per_long);
4850 ret = -1;
4851 goto end_unlock;
4852 }
4853
4854 /* Clear quiescent state. */
4855 switch (ua_sess->buffer_type) {
4856 case LTTNG_BUFFER_PER_PID:
4857 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter,
4858 ua_chan, node.node) {
4859 health_code_update();
4860 ret = consumer_clear_quiescent_channel(socket,
4861 ua_chan->key);
4862 if (ret) {
4863 ERR("Error clearing quiescent state for consumer channel");
4864 ret = -1;
4865 continue;
4866 }
4867 }
4868 break;
4869 case LTTNG_BUFFER_PER_UID:
4870 default:
4871 assert(0);
4872 ret = -1;
4873 break;
4874 }
4875
4876 health_code_update();
4877
4878end_unlock:
4879 pthread_mutex_unlock(&ua_sess->lock);
4880
4881end_not_compatible:
4882 rcu_read_unlock();
4883 health_code_update();
4884 return ret;
4885}
4886
4887/*
4888 * Clear quiescent state in each stream for all applications for a
4889 * specific UST session.
4890 * Called with UST session lock held.
4891 */
4892static
4893int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
4894
4895{
4896 int ret = 0;
4897
4898 DBG("Clearing stream quiescent state for all ust apps");
4899
4900 rcu_read_lock();
4901
4902 switch (usess->buffer_type) {
4903 case LTTNG_BUFFER_PER_UID:
4904 {
4905 struct lttng_ht_iter iter;
4906 struct buffer_reg_uid *reg;
4907
4908 /*
4909 * Clear quiescent for all per UID buffers associated to
4910 * that session.
4911 */
4912 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4913 struct consumer_socket *socket;
4914 struct buffer_reg_channel *reg_chan;
4915
4916 /* Get associated consumer socket.*/
4917 socket = consumer_find_socket_by_bitness(
4918 reg->bits_per_long, usess->consumer);
4919 if (!socket) {
4920 /*
4921 * Ignore request if no consumer is found for
4922 * the session.
4923 */
4924 continue;
4925 }
4926
4927 cds_lfht_for_each_entry(reg->registry->channels->ht,
4928 &iter.iter, reg_chan, node.node) {
4929 /*
4930 * The following call will print error values so
4931 * the return code is of little importance
4932 * because whatever happens, we have to try them
4933 * all.
4934 */
4935 (void) consumer_clear_quiescent_channel(socket,
4936 reg_chan->consumer_key);
4937 }
4938 }
4939 break;
4940 }
4941 case LTTNG_BUFFER_PER_PID:
4942 {
4943 struct ust_app_session *ua_sess;
4944 struct lttng_ht_iter iter;
4945 struct ust_app *app;
4946
4947 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
4948 pid_n.node) {
4949 ua_sess = lookup_session_by_app(usess, app);
4950 if (ua_sess == NULL) {
4951 continue;
4952 }
4953 (void) ust_app_clear_quiescent_app_session(app,
4954 ua_sess);
4955 }
4956 break;
4957 }
4958 default:
4959 ret = -1;
4960 assert(0);
4961 break;
4962 }
4963
4964 rcu_read_unlock();
4965 health_code_update();
4966 return ret;
4967}
4968
84cd17c6
MD
4969/*
4970 * Destroy a specific UST session in apps.
4971 */
3353de95 4972static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
84cd17c6 4973{
ffe60014 4974 int ret;
84cd17c6 4975 struct ust_app_session *ua_sess;
bec39940 4976 struct lttng_ht_iter iter;
d9bf3ca4 4977 struct lttng_ht_node_u64 *node;
84cd17c6 4978
852d0037 4979 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
4980
4981 rcu_read_lock();
4982
e0c7ec2b
DG
4983 if (!app->compatible) {
4984 goto end;
4985 }
4986
84cd17c6 4987 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 4988 node = lttng_ht_iter_get_node_u64(&iter);
84cd17c6 4989 if (node == NULL) {
d42f20df
DG
4990 /* Session is being or is deleted. */
4991 goto end;
84cd17c6
MD
4992 }
4993 ua_sess = caa_container_of(node, struct ust_app_session, node);
c4a1715b 4994
840cb59c 4995 health_code_update();
d0b96690 4996 destroy_app_session(app, ua_sess);
84cd17c6 4997
840cb59c 4998 health_code_update();
7db205b5 4999
84cd17c6 5000 /* Quiescent wait after stopping trace */
fb45065e 5001 pthread_mutex_lock(&app->sock_lock);
ffe60014 5002 ret = ustctl_wait_quiescent(app->sock);
fb45065e 5003 pthread_mutex_unlock(&app->sock_lock);
ee3f5169
JR
5004 if (ret < 0) {
5005 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5006 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
5007 app->pid, app->sock);
5008 } else if (ret == -EAGAIN) {
5009 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
5010 app->pid, app->sock);
5011 } else {
5012 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
5013 ret, app->pid, app->sock);
5014 }
ffe60014 5015 }
e0c7ec2b
DG
5016end:
5017 rcu_read_unlock();
840cb59c 5018 health_code_update();
84cd17c6 5019 return 0;
84cd17c6
MD
5020}
5021
5b4a0ec0
DG
5022/*
5023 * Start tracing for the UST session.
5024 */
421cb601
DG
5025int ust_app_start_trace_all(struct ltt_ust_session *usess)
5026{
bec39940 5027 struct lttng_ht_iter iter;
421cb601 5028 struct ust_app *app;
48842b30 5029
421cb601
DG
5030 DBG("Starting all UST traces");
5031
1053a273
MD
5032 /*
5033 * Even though the start trace might fail, flag this session active so
5034 * other application coming in are started by default.
5035 */
5036 usess->active = 1;
5037
421cb601 5038 rcu_read_lock();
421cb601 5039
0dd01979
MD
5040 /*
5041 * In a start-stop-start use-case, we need to clear the quiescent state
5042 * of each channel set by the prior stop command, thus ensuring that a
5043 * following stop or destroy is sure to grab a timestamp_end near those
5044 * operations, even if the packet is empty.
5045 */
5046 (void) ust_app_clear_quiescent_session(usess);
5047
60235eb8
MD
5048 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5049 ust_app_global_update(usess, app);
5050 }
5051
48842b30
DG
5052 rcu_read_unlock();
5053
5054 return 0;
5055}
487cf67c 5056
8be98f9a
MD
5057/*
5058 * Start tracing for the UST session.
ce34fcd0 5059 * Called with UST session lock held.
8be98f9a
MD
5060 */
5061int ust_app_stop_trace_all(struct ltt_ust_session *usess)
5062{
5063 int ret = 0;
bec39940 5064 struct lttng_ht_iter iter;
8be98f9a
MD
5065 struct ust_app *app;
5066
5067 DBG("Stopping all UST traces");
5068
1053a273
MD
5069 /*
5070 * Even though the stop trace might fail, flag this session inactive so
5071 * other application coming in are not started by default.
5072 */
5073 usess->active = 0;
5074
8be98f9a
MD
5075 rcu_read_lock();
5076
b34cbebf
MD
5077 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5078 ret = ust_app_stop_trace(usess, app);
5079 if (ret < 0) {
5080 /* Continue to next apps even on error */
5081 continue;
5082 }
5083 }
5084
ce34fcd0 5085 (void) ust_app_flush_session(usess);
8be98f9a
MD
5086
5087 rcu_read_unlock();
5088
5089 return 0;
5090}
5091
84cd17c6
MD
5092/*
5093 * Destroy app UST session.
5094 */
5095int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
5096{
5097 int ret = 0;
bec39940 5098 struct lttng_ht_iter iter;
84cd17c6
MD
5099 struct ust_app *app;
5100
5101 DBG("Destroy all UST traces");
5102
5103 rcu_read_lock();
5104
852d0037 5105 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3353de95 5106 ret = destroy_trace(usess, app);
84cd17c6
MD
5107 if (ret < 0) {
5108 /* Continue to next apps even on error */
5109 continue;
5110 }
5111 }
5112
5113 rcu_read_unlock();
5114
5115 return 0;
5116}
5117
0ce9aa93 5118/* The ua_sess lock must be held by the caller. */
a9ad0c8f 5119static
0ce9aa93
JG
5120int find_or_create_ust_app_channel(
5121 struct ltt_ust_session *usess,
5122 struct ust_app_session *ua_sess,
5123 struct ust_app *app,
5124 struct ltt_ust_channel *uchan,
5125 struct ust_app_channel **ua_chan)
487cf67c 5126{
55c54cce 5127 int ret = 0;
0ce9aa93
JG
5128 struct lttng_ht_iter iter;
5129 struct lttng_ht_node_str *ua_chan_node;
5130
5131 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5132 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5133 if (ua_chan_node) {
5134 *ua_chan = caa_container_of(ua_chan_node,
5135 struct ust_app_channel, node);
5136 goto end;
5137 }
5138
5139 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5140 if (ret) {
5141 goto end;
5142 }
5143end:
5144 return ret;
5145}
5146
5147static
5148int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5149 struct ltt_ust_event *uevent, struct ust_app_session *ua_sess,
5150 struct ust_app *app)
5151{
5152 int ret = 0;
5153 struct ust_app_event *ua_event = NULL;
5154
5155 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5156 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5157 if (!ua_event) {
5158 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
5159 if (ret < 0) {
5160 goto end;
5161 }
5162 } else {
5163 if (ua_event->enabled != uevent->enabled) {
5164 ret = uevent->enabled ?
5165 enable_ust_app_event(ua_sess, ua_event, app) :
5166 disable_ust_app_event(ua_sess, ua_event, app);
5167 }
5168 }
5169
5170end:
5171 return ret;
5172}
5173
5174/*
5175 * The caller must ensure that the application is compatible and is tracked
5176 * by the PID tracker.
5177 */
5178static
5179void ust_app_synchronize(struct ltt_ust_session *usess,
5180 struct ust_app *app)
5181{
5182 int ret = 0;
5183 struct cds_lfht_iter uchan_iter;
5184 struct ltt_ust_channel *uchan;
3d8ca23b 5185 struct ust_app_session *ua_sess = NULL;
1f3580c7 5186
0ce9aa93
JG
5187 /*
5188 * The application's configuration should only be synchronized for
5189 * active sessions.
5190 */
5191 assert(usess->active);
5192
5193 ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL);
3d8ca23b
DG
5194 if (ret < 0) {
5195 /* Tracer is probably gone or ENOMEM. */
487cf67c
DG
5196 goto error;
5197 }
3d8ca23b 5198 assert(ua_sess);
487cf67c 5199
d0b96690 5200 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
5201 if (ua_sess->deleted) {
5202 pthread_mutex_unlock(&ua_sess->lock);
5203 goto end;
5204 }
5205
0ce9aa93 5206 rcu_read_lock();
d02e041f 5207
0ce9aa93
JG
5208 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
5209 uchan, node.node) {
5210 struct ust_app_channel *ua_chan;
5211 struct cds_lfht_iter uevent_iter;
5212 struct ltt_ust_event *uevent;
487cf67c 5213
31746f93 5214 /*
0ce9aa93
JG
5215 * Search for a matching ust_app_channel. If none is found,
5216 * create it. Creating the channel will cause the ua_chan
5217 * structure to be allocated, the channel buffers to be
5218 * allocated (if necessary) and sent to the application, and
5219 * all enabled contexts will be added to the channel.
31746f93 5220 */
0ce9aa93
JG
5221 ret = find_or_create_ust_app_channel(usess, ua_sess,
5222 app, uchan, &ua_chan);
5223 if (ret) {
5224 /* Tracer is probably gone or ENOMEM. */
5225 goto error_unlock;
727d5404
DG
5226 }
5227
0ce9aa93
JG
5228 if (!ua_chan) {
5229 /* ua_chan will be NULL for the metadata channel */
5230 continue;
5231 }
727d5404 5232
0ce9aa93 5233 cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent,
bec39940 5234 node.node) {
0ce9aa93
JG
5235 ret = ust_app_channel_synchronize_event(ua_chan,
5236 uevent, ua_sess, app);
5237 if (ret) {
d0b96690 5238 goto error_unlock;
487cf67c 5239 }
36dc12cc 5240 }
d0b96690 5241
0ce9aa93
JG
5242 if (ua_chan->enabled != uchan->enabled) {
5243 ret = uchan->enabled ?
5244 enable_ust_app_channel(ua_sess, uchan, app) :
5245 disable_ust_app_channel(ua_sess, ua_chan, app);
5246 if (ret) {
5247 goto error_unlock;
5248 }
5249 }
36dc12cc 5250 }
d02e041f
JG
5251
5252 /*
5253 * Create the metadata for the application. This returns gracefully if a
5254 * metadata was already set for the session.
5255 *
5256 * The metadata channel must be created after the data channels as the
5257 * consumer daemon assumes this ordering. When interacting with a relay
5258 * daemon, the consumer will use this assumption to send the
5259 * "STREAMS_SENT" message to the relay daemon.
5260 */
5261 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
5262 if (ret < 0) {
5263 goto error_unlock;
5264 }
5265
0ce9aa93 5266 rcu_read_unlock();
60235eb8 5267
a9ad0c8f 5268end:
0ce9aa93 5269 pthread_mutex_unlock(&ua_sess->lock);
ffe60014 5270 /* Everything went well at this point. */
ffe60014
DG
5271 return;
5272
d0b96690 5273error_unlock:
0ce9aa93 5274 rcu_read_unlock();
d0b96690 5275 pthread_mutex_unlock(&ua_sess->lock);
487cf67c 5276error:
ffe60014 5277 if (ua_sess) {
d0b96690 5278 destroy_app_session(app, ua_sess);
ffe60014 5279 }
487cf67c
DG
5280 return;
5281}
55cc08a6 5282
a9ad0c8f
MD
5283static
5284void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
5285{
5286 struct ust_app_session *ua_sess;
5287
5288 ua_sess = lookup_session_by_app(usess, app);
5289 if (ua_sess == NULL) {
5290 return;
5291 }
5292 destroy_app_session(app, ua_sess);
5293}
5294
5295/*
5296 * Add channels/events from UST global domain to registered apps at sock.
5297 *
5298 * Called with session lock held.
5299 * Called with RCU read-side lock held.
5300 */
5301void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
5302{
5303 assert(usess);
0ce9aa93 5304 assert(usess->active);
a9ad0c8f
MD
5305
5306 DBG2("UST app global update for app sock %d for session id %" PRIu64,
5307 app->sock, usess->id);
5308
5309 if (!app->compatible) {
5310 return;
5311 }
a9ad0c8f 5312 if (trace_ust_pid_tracker_lookup(usess, app->pid)) {
0ce9aa93
JG
5313 /*
5314 * Synchronize the application's internal tracing configuration
5315 * and start tracing.
5316 */
5317 ust_app_synchronize(usess, app);
5318 ust_app_start_trace(usess, app);
a9ad0c8f
MD
5319 } else {
5320 ust_app_global_destroy(usess, app);
5321 }
5322}
5323
5324/*
5325 * Called with session lock held.
5326 */
5327void ust_app_global_update_all(struct ltt_ust_session *usess)
5328{
5329 struct lttng_ht_iter iter;
5330 struct ust_app *app;
5331
5332 rcu_read_lock();
5333 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5334 ust_app_global_update(usess, app);
5335 }
5336 rcu_read_unlock();
5337}
5338
55cc08a6
DG
5339/*
5340 * Add context to a specific channel for global UST domain.
5341 */
5342int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
5343 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
5344{
5345 int ret = 0;
bec39940
DG
5346 struct lttng_ht_node_str *ua_chan_node;
5347 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
5348 struct ust_app_channel *ua_chan = NULL;
5349 struct ust_app_session *ua_sess;
5350 struct ust_app *app;
5351
0ce9aa93 5352 assert(usess->active);
60235eb8 5353
55cc08a6 5354 rcu_read_lock();
852d0037 5355 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
5356 if (!app->compatible) {
5357 /*
5358 * TODO: In time, we should notice the caller of this error by
5359 * telling him that this is a version error.
5360 */
5361 continue;
5362 }
55cc08a6
DG
5363 ua_sess = lookup_session_by_app(usess, app);
5364 if (ua_sess == NULL) {
5365 continue;
5366 }
5367
d0b96690 5368 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
5369
5370 if (ua_sess->deleted) {
5371 pthread_mutex_unlock(&ua_sess->lock);
5372 continue;
5373 }
5374
55cc08a6 5375 /* Lookup channel in the ust app session */
bec39940
DG
5376 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5377 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6 5378 if (ua_chan_node == NULL) {
d0b96690 5379 goto next_app;
55cc08a6
DG
5380 }
5381 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
5382 node);
c9edf082 5383 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
55cc08a6 5384 if (ret < 0) {
d0b96690 5385 goto next_app;
55cc08a6 5386 }
d0b96690
DG
5387 next_app:
5388 pthread_mutex_unlock(&ua_sess->lock);
55cc08a6
DG
5389 }
5390
55cc08a6 5391 rcu_read_unlock();
76d45b40
DG
5392 return ret;
5393}
7f79d3a1 5394
d0b96690
DG
5395/*
5396 * Receive registration and populate the given msg structure.
5397 *
5398 * On success return 0 else a negative value returned by the ustctl call.
5399 */
5400int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
5401{
5402 int ret;
5403 uint32_t pid, ppid, uid, gid;
5404
5405 assert(msg);
5406
5407 ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
5408 &pid, &ppid, &uid, &gid,
5409 &msg->bits_per_long,
5410 &msg->uint8_t_alignment,
5411 &msg->uint16_t_alignment,
5412 &msg->uint32_t_alignment,
5413 &msg->uint64_t_alignment,
5414 &msg->long_alignment,
5415 &msg->byte_order,
5416 msg->name);
5417 if (ret < 0) {
5418 switch (-ret) {
5419 case EPIPE:
5420 case ECONNRESET:
5421 case LTTNG_UST_ERR_EXITING:
5422 DBG3("UST app recv reg message failed. Application died");
5423 break;
5424 case LTTNG_UST_ERR_UNSUP_MAJOR:
5425 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5426 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
5427 LTTNG_UST_ABI_MINOR_VERSION);
5428 break;
5429 default:
5430 ERR("UST app recv reg message failed with ret %d", ret);
5431 break;
5432 }
5433 goto error;
5434 }
5435 msg->pid = (pid_t) pid;
5436 msg->ppid = (pid_t) ppid;
5437 msg->uid = (uid_t) uid;
5438 msg->gid = (gid_t) gid;
5439
5440error:
5441 return ret;
5442}
5443
10b56aef
MD
5444/*
5445 * Return a ust app session object using the application object and the
5446 * session object descriptor has a key. If not found, NULL is returned.
5447 * A RCU read side lock MUST be acquired when calling this function.
5448*/
5449static struct ust_app_session *find_session_by_objd(struct ust_app *app,
5450 int objd)
5451{
5452 struct lttng_ht_node_ulong *node;
5453 struct lttng_ht_iter iter;
5454 struct ust_app_session *ua_sess = NULL;
5455
5456 assert(app);
5457
5458 lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter);
5459 node = lttng_ht_iter_get_node_ulong(&iter);
5460 if (node == NULL) {
5461 DBG2("UST app session find by objd %d not found", objd);
5462 goto error;
5463 }
5464
5465 ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node);
5466
5467error:
5468 return ua_sess;
5469}
5470
d88aee68
DG
5471/*
5472 * Return a ust app channel object using the application object and the channel
5473 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5474 * lock MUST be acquired before calling this function.
5475 */
d0b96690
DG
5476static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
5477 int objd)
5478{
5479 struct lttng_ht_node_ulong *node;
5480 struct lttng_ht_iter iter;
5481 struct ust_app_channel *ua_chan = NULL;
5482
5483 assert(app);
5484
5485 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
5486 node = lttng_ht_iter_get_node_ulong(&iter);
5487 if (node == NULL) {
5488 DBG2("UST app channel find by objd %d not found", objd);
5489 goto error;
5490 }
5491
5492 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
5493
5494error:
5495 return ua_chan;
5496}
5497
d88aee68
DG
5498/*
5499 * Reply to a register channel notification from an application on the notify
5500 * socket. The channel metadata is also created.
5501 *
5502 * The session UST registry lock is acquired in this function.
5503 *
5504 * On success 0 is returned else a negative value.
5505 */
8eede835 5506static int reply_ust_register_channel(int sock, int cobjd,
d0b96690
DG
5507 size_t nr_fields, struct ustctl_field *fields)
5508{
5509 int ret, ret_code = 0;
dcdd8e41 5510 uint32_t chan_id;
7972aab2 5511 uint64_t chan_reg_key;
d0b96690
DG
5512 enum ustctl_channel_header type;
5513 struct ust_app *app;
5514 struct ust_app_channel *ua_chan;
5515 struct ust_app_session *ua_sess;
7972aab2 5516 struct ust_registry_session *registry;
45893984 5517 struct ust_registry_channel *chan_reg;
d0b96690
DG
5518
5519 rcu_read_lock();
5520
5521 /* Lookup application. If not found, there is a code flow error. */
5522 app = find_app_by_notify_sock(sock);
d88aee68 5523 if (!app) {
fad1ed2f 5524 DBG("Application socket %d is being torn down. Abort event notify",
d88aee68 5525 sock);
77ef9153 5526 ret = -1;
d88aee68
DG
5527 goto error_rcu_unlock;
5528 }
d0b96690 5529
4950b860 5530 /* Lookup channel by UST object descriptor. */
d0b96690 5531 ua_chan = find_channel_by_objd(app, cobjd);
4950b860 5532 if (!ua_chan) {
fad1ed2f 5533 DBG("Application channel is being torn down. Abort event notify");
4950b860
MD
5534 ret = 0;
5535 goto error_rcu_unlock;
5536 }
5537
d0b96690
DG
5538 assert(ua_chan->session);
5539 ua_sess = ua_chan->session;
d0b96690 5540
7972aab2
DG
5541 /* Get right session registry depending on the session buffer type. */
5542 registry = get_session_registry(ua_sess);
fad1ed2f
JR
5543 if (!registry) {
5544 DBG("Application session is being torn down. Abort event notify");
5545 ret = 0;
5546 goto error_rcu_unlock;
5547 };
45893984 5548
7972aab2
DG
5549 /* Depending on the buffer type, a different channel key is used. */
5550 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5551 chan_reg_key = ua_chan->tracing_channel_id;
d0b96690 5552 } else {
7972aab2 5553 chan_reg_key = ua_chan->key;
d0b96690
DG
5554 }
5555
7972aab2
DG
5556 pthread_mutex_lock(&registry->lock);
5557
5558 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
5559 assert(chan_reg);
5560
5561 if (!chan_reg->register_done) {
dcdd8e41
MD
5562 /*
5563 * TODO: eventually use the registry event count for
5564 * this channel to better guess header type for per-pid
5565 * buffers.
5566 */
5567 type = USTCTL_CHANNEL_HEADER_LARGE;
7972aab2
DG
5568 chan_reg->nr_ctx_fields = nr_fields;
5569 chan_reg->ctx_fields = fields;
fad1ed2f 5570 fields = NULL;
7972aab2 5571 chan_reg->header_type = type;
d0b96690 5572 } else {
7972aab2
DG
5573 /* Get current already assigned values. */
5574 type = chan_reg->header_type;
d0b96690 5575 }
7972aab2
DG
5576 /* Channel id is set during the object creation. */
5577 chan_id = chan_reg->chan_id;
d0b96690
DG
5578
5579 /* Append to metadata */
7972aab2
DG
5580 if (!chan_reg->metadata_dumped) {
5581 ret_code = ust_metadata_channel_statedump(registry, chan_reg);
d0b96690
DG
5582 if (ret_code) {
5583 ERR("Error appending channel metadata (errno = %d)", ret_code);
5584 goto reply;
5585 }
5586 }
5587
5588reply:
7972aab2 5589 DBG3("UST app replying to register channel key %" PRIu64
ee3f5169 5590 " with id %u, type = %d, ret = %d", chan_reg_key, chan_id, type,
7972aab2 5591 ret_code);
d0b96690
DG
5592
5593 ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code);
5594 if (ret < 0) {
ee3f5169
JR
5595 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5596 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
5597 app->pid, app->sock);
5598 } else if (ret == -EAGAIN) {
5599 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
5600 app->pid, app->sock);
d0b96690 5601 } else {
ee3f5169
JR
5602 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
5603 ret, app->pid, app->sock);
d0b96690
DG
5604 }
5605 goto error;
5606 }
5607
7972aab2
DG
5608 /* This channel registry registration is completed. */
5609 chan_reg->register_done = 1;
5610
d0b96690 5611error:
7972aab2 5612 pthread_mutex_unlock(&registry->lock);
d88aee68 5613error_rcu_unlock:
d0b96690 5614 rcu_read_unlock();
fad1ed2f 5615 free(fields);
d0b96690
DG
5616 return ret;
5617}
5618
d88aee68
DG
5619/*
5620 * Add event to the UST channel registry. When the event is added to the
5621 * registry, the metadata is also created. Once done, this replies to the
5622 * application with the appropriate error code.
5623 *
5624 * The session UST registry lock is acquired in the function.
5625 *
5626 * On success 0 is returned else a negative value.
5627 */
d0b96690 5628static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
2106efa0
PP
5629 char *sig, size_t nr_fields, struct ustctl_field *fields,
5630 int loglevel_value, char *model_emf_uri)
d0b96690
DG
5631{
5632 int ret, ret_code;
5633 uint32_t event_id = 0;
7972aab2 5634 uint64_t chan_reg_key;
d0b96690
DG
5635 struct ust_app *app;
5636 struct ust_app_channel *ua_chan;
5637 struct ust_app_session *ua_sess;
7972aab2 5638 struct ust_registry_session *registry;
d0b96690
DG
5639
5640 rcu_read_lock();
5641
5642 /* Lookup application. If not found, there is a code flow error. */
5643 app = find_app_by_notify_sock(sock);
d88aee68 5644 if (!app) {
fad1ed2f 5645 DBG("Application socket %d is being torn down. Abort event notify",
d88aee68 5646 sock);
77ef9153 5647 ret = -1;
d88aee68
DG
5648 goto error_rcu_unlock;
5649 }
d0b96690 5650
4950b860 5651 /* Lookup channel by UST object descriptor. */
d0b96690 5652 ua_chan = find_channel_by_objd(app, cobjd);
4950b860 5653 if (!ua_chan) {
fad1ed2f 5654 DBG("Application channel is being torn down. Abort event notify");
4950b860
MD
5655 ret = 0;
5656 goto error_rcu_unlock;
5657 }
5658
d0b96690
DG
5659 assert(ua_chan->session);
5660 ua_sess = ua_chan->session;
5661
7972aab2 5662 registry = get_session_registry(ua_sess);
fad1ed2f
JR
5663 if (!registry) {
5664 DBG("Application session is being torn down. Abort event notify");
5665 ret = 0;
5666 goto error_rcu_unlock;
5667 }
7972aab2
DG
5668
5669 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5670 chan_reg_key = ua_chan->tracing_channel_id;
5671 } else {
5672 chan_reg_key = ua_chan->key;
5673 }
5674
5675 pthread_mutex_lock(&registry->lock);
d0b96690 5676
d5d629b5
DG
5677 /*
5678 * From this point on, this call acquires the ownership of the sig, fields
5679 * and model_emf_uri meaning any free are done inside it if needed. These
5680 * three variables MUST NOT be read/write after this.
5681 */
7972aab2 5682 ret_code = ust_registry_create_event(registry, chan_reg_key,
2106efa0
PP
5683 sobjd, cobjd, name, sig, nr_fields, fields,
5684 loglevel_value, model_emf_uri, ua_sess->buffer_type,
5685 &event_id, app);
fad1ed2f
JR
5686 sig = NULL;
5687 fields = NULL;
5688 model_emf_uri = NULL;
d0b96690
DG
5689
5690 /*
5691 * The return value is returned to ustctl so in case of an error, the
5692 * application can be notified. In case of an error, it's important not to
5693 * return a negative error or else the application will get closed.
5694 */
5695 ret = ustctl_reply_register_event(sock, event_id, ret_code);
5696 if (ret < 0) {
ee3f5169
JR
5697 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5698 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
5699 app->pid, app->sock);
5700 } else if (ret == -EAGAIN) {
5701 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
5702 app->pid, app->sock);
d0b96690 5703 } else {
ee3f5169
JR
5704 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
5705 ret, app->pid, app->sock);
d0b96690
DG
5706 }
5707 /*
5708 * No need to wipe the create event since the application socket will
5709 * get close on error hence cleaning up everything by itself.
5710 */
5711 goto error;
5712 }
5713
7972aab2
DG
5714 DBG3("UST registry event %s with id %" PRId32 " added successfully",
5715 name, event_id);
d88aee68 5716
d0b96690 5717error:
7972aab2 5718 pthread_mutex_unlock(&registry->lock);
d88aee68 5719error_rcu_unlock:
d0b96690 5720 rcu_read_unlock();
fad1ed2f
JR
5721 free(sig);
5722 free(fields);
5723 free(model_emf_uri);
d0b96690
DG
5724 return ret;
5725}
5726
10b56aef
MD
5727/*
5728 * Add enum to the UST session registry. Once done, this replies to the
5729 * application with the appropriate error code.
5730 *
5731 * The session UST registry lock is acquired within this function.
5732 *
5733 * On success 0 is returned else a negative value.
5734 */
5735static int add_enum_ust_registry(int sock, int sobjd, char *name,
5736 struct ustctl_enum_entry *entries, size_t nr_entries)
5737{
5738 int ret = 0, ret_code;
5739 struct ust_app *app;
5740 struct ust_app_session *ua_sess;
5741 struct ust_registry_session *registry;
5742 uint64_t enum_id = -1ULL;
5743
5744 rcu_read_lock();
5745
5746 /* Lookup application. If not found, there is a code flow error. */
5747 app = find_app_by_notify_sock(sock);
5748 if (!app) {
5749 /* Return an error since this is not an error */
5750 DBG("Application socket %d is being torn down. Aborting enum registration",
5751 sock);
5752 free(entries);
77ef9153 5753 ret = -1;
10b56aef
MD
5754 goto error_rcu_unlock;
5755 }
5756
5757 /* Lookup session by UST object descriptor. */
5758 ua_sess = find_session_by_objd(app, sobjd);
5759 if (!ua_sess) {
5760 /* Return an error since this is not an error */
ee3f5169 5761 DBG("Application session is being torn down (session not found Aborting enum registration.");
10b56aef
MD
5762 free(entries);
5763 goto error_rcu_unlock;
5764 }
5765
5766 registry = get_session_registry(ua_sess);
fad1ed2f 5767 if (!registry) {
ee3f5169 5768 DBG("Application session is being torn down (registry not found Aborting enum registration.");
fad1ed2f
JR
5769 free(entries);
5770 goto error_rcu_unlock;
5771 }
10b56aef
MD
5772
5773 pthread_mutex_lock(&registry->lock);
5774
5775 /*
5776 * From this point on, the callee acquires the ownership of
5777 * entries. The variable entries MUST NOT be read/written after
5778 * call.
5779 */
5780 ret_code = ust_registry_create_or_find_enum(registry, sobjd, name,
5781 entries, nr_entries, &enum_id);
5782 entries = NULL;
5783
5784 /*
5785 * The return value is returned to ustctl so in case of an error, the
5786 * application can be notified. In case of an error, it's important not to
5787 * return a negative error or else the application will get closed.
5788 */
5789 ret = ustctl_reply_register_enum(sock, enum_id, ret_code);
5790 if (ret < 0) {
ee3f5169
JR
5791 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5792 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
5793 app->pid, app->sock);
5794 } else if (ret == -EAGAIN) {
5795 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
5796 app->pid, app->sock);
10b56aef 5797 } else {
ee3f5169
JR
5798 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
5799 ret, app->pid, app->sock);
10b56aef
MD
5800 }
5801 /*
5802 * No need to wipe the create enum since the application socket will
5803 * get close on error hence cleaning up everything by itself.
5804 */
5805 goto error;
5806 }
5807
5808 DBG3("UST registry enum %s added successfully or already found", name);
5809
5810error:
5811 pthread_mutex_unlock(&registry->lock);
5812error_rcu_unlock:
5813 rcu_read_unlock();
5814 return ret;
5815}
5816
d88aee68
DG
5817/*
5818 * Handle application notification through the given notify socket.
5819 *
5820 * Return 0 on success or else a negative value.
5821 */
d0b96690
DG
5822int ust_app_recv_notify(int sock)
5823{
5824 int ret;
5825 enum ustctl_notify_cmd cmd;
5826
5827 DBG3("UST app receiving notify from sock %d", sock);
5828
5829 ret = ustctl_recv_notify(sock, &cmd);
5830 if (ret < 0) {
ee3f5169
JR
5831 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5832 DBG3("UST app recv notify failed. Application died: sock = %d",
5833 sock);
5834 } else if (ret == -EAGAIN) {
5835 WARN("UST app recv notify failed. Communication time out: sock = %d",
5836 sock);
d0b96690 5837 } else {
ee3f5169
JR
5838 ERR("UST app recv notify failed with ret %d: sock = %d",
5839 ret, sock);
d0b96690
DG
5840 }
5841 goto error;
5842 }
5843
5844 switch (cmd) {
5845 case USTCTL_NOTIFY_CMD_EVENT:
5846 {
2106efa0 5847 int sobjd, cobjd, loglevel_value;
d0b96690
DG
5848 char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri;
5849 size_t nr_fields;
5850 struct ustctl_field *fields;
5851
5852 DBG2("UST app ustctl register event received");
5853
2106efa0
PP
5854 ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name,
5855 &loglevel_value, &sig, &nr_fields, &fields,
5856 &model_emf_uri);
d0b96690 5857 if (ret < 0) {
ee3f5169
JR
5858 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5859 DBG3("UST app recv event failed. Application died: sock = %d",
5860 sock);
5861 } else if (ret == -EAGAIN) {
5862 WARN("UST app recv event failed. Communication time out: sock = %d",
5863 sock);
d0b96690 5864 } else {
ee3f5169
JR
5865 ERR("UST app recv event failed with ret %d: sock = %d",
5866 ret, sock);
d0b96690
DG
5867 }
5868 goto error;
5869 }
5870
d5d629b5
DG
5871 /*
5872 * Add event to the UST registry coming from the notify socket. This
5873 * call will free if needed the sig, fields and model_emf_uri. This
5874 * code path loses the ownsership of these variables and transfer them
5875 * to the this function.
5876 */
d0b96690 5877 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
2106efa0 5878 fields, loglevel_value, model_emf_uri);
d0b96690
DG
5879 if (ret < 0) {
5880 goto error;
5881 }
5882
5883 break;
5884 }
5885 case USTCTL_NOTIFY_CMD_CHANNEL:
5886 {
5887 int sobjd, cobjd;
5888 size_t nr_fields;
5889 struct ustctl_field *fields;
5890
5891 DBG2("UST app ustctl register channel received");
5892
5893 ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
5894 &fields);
5895 if (ret < 0) {
ee3f5169
JR
5896 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5897 DBG3("UST app recv channel failed. Application died: sock = %d",
5898 sock);
5899 } else if (ret == -EAGAIN) {
5900 WARN("UST app recv channel failed. Communication time out: sock = %d",
5901 sock);
d0b96690 5902 } else {
ee3f5169
JR
5903 ERR("UST app recv channel failed with ret %d: sock = %d)",
5904 ret, sock);
d0b96690
DG
5905 }
5906 goto error;
5907 }
5908
d5d629b5
DG
5909 /*
5910 * The fields ownership are transfered to this function call meaning
5911 * that if needed it will be freed. After this, it's invalid to access
5912 * fields or clean it up.
5913 */
8eede835 5914 ret = reply_ust_register_channel(sock, cobjd, nr_fields,
d0b96690
DG
5915 fields);
5916 if (ret < 0) {
5917 goto error;
5918 }
5919
5920 break;
5921 }
10b56aef
MD
5922 case USTCTL_NOTIFY_CMD_ENUM:
5923 {
5924 int sobjd;
5925 char name[LTTNG_UST_SYM_NAME_LEN];
5926 size_t nr_entries;
5927 struct ustctl_enum_entry *entries;
5928
5929 DBG2("UST app ustctl register enum received");
5930
5931 ret = ustctl_recv_register_enum(sock, &sobjd, name,
5932 &entries, &nr_entries);
5933 if (ret < 0) {
ee3f5169
JR
5934 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5935 DBG3("UST app recv enum failed. Application died: sock = %d",
5936 sock);
5937 } else if (ret == -EAGAIN) {
5938 WARN("UST app recv enum failed. Communication time out: sock = %d",
5939 sock);
10b56aef 5940 } else {
ee3f5169
JR
5941 ERR("UST app recv enum failed with ret %d: sock = %d",
5942 ret, sock);
10b56aef
MD
5943 }
5944 goto error;
5945 }
5946
5947 /* Callee assumes ownership of entries */
5948 ret = add_enum_ust_registry(sock, sobjd, name,
5949 entries, nr_entries);
5950 if (ret < 0) {
5951 goto error;
5952 }
5953
5954 break;
5955 }
d0b96690
DG
5956 default:
5957 /* Should NEVER happen. */
5958 assert(0);
5959 }
5960
5961error:
5962 return ret;
5963}
d88aee68
DG
5964
5965/*
5966 * Once the notify socket hangs up, this is called. First, it tries to find the
5967 * corresponding application. On failure, the call_rcu to close the socket is
5968 * executed. If an application is found, it tries to delete it from the notify
5969 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5970 *
5971 * Note that an object needs to be allocated here so on ENOMEM failure, the
5972 * call RCU is not done but the rest of the cleanup is.
5973 */
5974void ust_app_notify_sock_unregister(int sock)
5975{
5976 int err_enomem = 0;
5977 struct lttng_ht_iter iter;
5978 struct ust_app *app;
5979 struct ust_app_notify_sock_obj *obj;
5980
5981 assert(sock >= 0);
5982
5983 rcu_read_lock();
5984
5985 obj = zmalloc(sizeof(*obj));
5986 if (!obj) {
5987 /*
5988 * An ENOMEM is kind of uncool. If this strikes we continue the
5989 * procedure but the call_rcu will not be called. In this case, we
5990 * accept the fd leak rather than possibly creating an unsynchronized
5991 * state between threads.
5992 *
5993 * TODO: The notify object should be created once the notify socket is
5994 * registered and stored independantely from the ust app object. The
5995 * tricky part is to synchronize the teardown of the application and
5996 * this notify object. Let's keep that in mind so we can avoid this
5997 * kind of shenanigans with ENOMEM in the teardown path.
5998 */
5999 err_enomem = 1;
6000 } else {
6001 obj->fd = sock;
6002 }
6003
6004 DBG("UST app notify socket unregister %d", sock);
6005
6006 /*
6007 * Lookup application by notify socket. If this fails, this means that the
6008 * hash table delete has already been done by the application
6009 * unregistration process so we can safely close the notify socket in a
6010 * call RCU.
6011 */
6012 app = find_app_by_notify_sock(sock);
6013 if (!app) {
6014 goto close_socket;
6015 }
6016
6017 iter.iter.node = &app->notify_sock_n.node;
6018
6019 /*
6020 * Whatever happens here either we fail or succeed, in both cases we have
6021 * to close the socket after a grace period to continue to the call RCU
6022 * here. If the deletion is successful, the application is not visible
6023 * anymore by other threads and is it fails it means that it was already
6024 * deleted from the hash table so either way we just have to close the
6025 * socket.
6026 */
6027 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
6028
6029close_socket:
6030 rcu_read_unlock();
6031
6032 /*
6033 * Close socket after a grace period to avoid for the socket to be reused
6034 * before the application object is freed creating potential race between
6035 * threads trying to add unique in the global hash table.
6036 */
6037 if (!err_enomem) {
6038 call_rcu(&obj->head, close_notify_sock_rcu);
6039 }
6040}
f45e313d
DG
6041
6042/*
6043 * Destroy a ust app data structure and free its memory.
6044 */
6045void ust_app_destroy(struct ust_app *app)
6046{
6047 if (!app) {
6048 return;
6049 }
6050
6051 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
6052}
6dc3064a
DG
6053
6054/*
6055 * Take a snapshot for a given UST session. The snapshot is sent to the given
6056 * output.
6057 *
8f07cd01 6058 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 6059 */
65ff8ea3
JG
6060enum lttng_error_code ust_app_snapshot_record(
6061 const struct ltt_ust_session *usess,
61ace1d3 6062 const struct consumer_output *output, int wait,
d07ceecd 6063 uint64_t nb_packets_per_stream)
6dc3064a
DG
6064{
6065 int ret = 0;
8f07cd01 6066 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
6067 struct lttng_ht_iter iter;
6068 struct ust_app *app;
9cb4b50c 6069 char *trace_path = NULL;
6dc3064a
DG
6070
6071 assert(usess);
6072 assert(output);
6073
6074 rcu_read_lock();
6075
8c924c7b
MD
6076 switch (usess->buffer_type) {
6077 case LTTNG_BUFFER_PER_UID:
6078 {
6079 struct buffer_reg_uid *reg;
6dc3064a 6080
8c924c7b
MD
6081 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6082 struct buffer_reg_channel *reg_chan;
6083 struct consumer_socket *socket;
1819b04a 6084 char pathname[PATH_MAX];
6dc3064a 6085
f6567e8d
JR
6086 if (!reg->registry->reg.ust->metadata_key) {
6087 /* Skip since no metadata is present */
6088 continue;
6089 }
6090
8c924c7b
MD
6091 /* Get consumer socket to use to push the metadata.*/
6092 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6093 usess->consumer);
6094 if (!socket) {
8f07cd01 6095 status = LTTNG_ERR_INVALID;
8c924c7b
MD
6096 goto error;
6097 }
6dc3064a 6098
8c924c7b 6099 memset(pathname, 0, sizeof(pathname));
e5148e25
JG
6100 /*
6101 * DEFAULT_UST_TRACE_UID_PATH already contains a path
6102 * separator.
6103 */
8c924c7b 6104 ret = snprintf(pathname, sizeof(pathname),
e5148e25 6105 DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH,
8c924c7b
MD
6106 reg->uid, reg->bits_per_long);
6107 if (ret < 0) {
6108 PERROR("snprintf snapshot path");
8f07cd01 6109 status = LTTNG_ERR_INVALID;
8c924c7b
MD
6110 goto error;
6111 }
9cb4b50c
JG
6112 /* Free path allowed on previous iteration. */
6113 free(trace_path);
1819b04a
MD
6114 trace_path = setup_channel_trace_path(usess->consumer, pathname);
6115 if (!trace_path) {
6116 status = LTTNG_ERR_INVALID;
6117 goto error;
6118 }
e5148e25 6119 /* Add the UST default trace dir to path. */
8c924c7b
MD
6120 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6121 reg_chan, node.node) {
8f07cd01 6122 status = consumer_snapshot_channel(socket,
e098433c
JG
6123 reg_chan->consumer_key,
6124 output, 0, usess->uid,
1819b04a 6125 usess->gid, trace_path, wait,
e5148e25 6126 nb_packets_per_stream);
8f07cd01 6127 if (status != LTTNG_OK) {
8c924c7b
MD
6128 goto error;
6129 }
6130 }
8f07cd01 6131 status = consumer_snapshot_channel(socket,
68808f4e 6132 reg->registry->reg.ust->metadata_key, output, 1,
1819b04a 6133 usess->uid, usess->gid, trace_path, wait, 0);
8f07cd01 6134 if (status != LTTNG_OK) {
8c924c7b
MD
6135 goto error;
6136 }
af706bb7 6137 }
8c924c7b
MD
6138 break;
6139 }
6140 case LTTNG_BUFFER_PER_PID:
6141 {
6142 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6143 struct consumer_socket *socket;
6144 struct lttng_ht_iter chan_iter;
6145 struct ust_app_channel *ua_chan;
6146 struct ust_app_session *ua_sess;
6147 struct ust_registry_session *registry;
1819b04a 6148 char pathname[PATH_MAX];
8c924c7b
MD
6149
6150 ua_sess = lookup_session_by_app(usess, app);
6151 if (!ua_sess) {
6152 /* Session not associated with this app. */
6153 continue;
6154 }
af706bb7 6155
8c924c7b
MD
6156 /* Get the right consumer socket for the application. */
6157 socket = consumer_find_socket_by_bitness(app->bits_per_long,
61ace1d3 6158 output);
8c924c7b 6159 if (!socket) {
8f07cd01 6160 status = LTTNG_ERR_INVALID;
5c786ded
JD
6161 goto error;
6162 }
6163
8c924c7b
MD
6164 /* Add the UST default trace dir to path. */
6165 memset(pathname, 0, sizeof(pathname));
e5148e25 6166 ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "%s",
8c924c7b 6167 ua_sess->path);
6dc3064a 6168 if (ret < 0) {
8f07cd01 6169 status = LTTNG_ERR_INVALID;
8c924c7b 6170 PERROR("snprintf snapshot path");
6dc3064a
DG
6171 goto error;
6172 }
9cb4b50c
JG
6173 /* Free path allowed on previous iteration. */
6174 free(trace_path);
1819b04a
MD
6175 trace_path = setup_channel_trace_path(usess->consumer, pathname);
6176 if (!trace_path) {
6177 status = LTTNG_ERR_INVALID;
6178 goto error;
6179 }
e5148e25 6180 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
8c924c7b 6181 ua_chan, node.node) {
8f07cd01 6182 status = consumer_snapshot_channel(socket,
c51311d6
JG
6183 ua_chan->key, output, 0,
6184 ua_sess->effective_credentials
6185 .uid,
6186 ua_sess->effective_credentials
6187 .gid,
1819b04a 6188 trace_path, wait,
e5148e25 6189 nb_packets_per_stream);
8f07cd01
JG
6190 switch (status) {
6191 case LTTNG_OK:
6192 break;
6193 case LTTNG_ERR_CHAN_NOT_FOUND:
6194 continue;
6195 default:
8c924c7b
MD
6196 goto error;
6197 }
6198 }
6199
6200 registry = get_session_registry(ua_sess);
fad1ed2f 6201 if (!registry) {
d5f19c28
MD
6202 DBG("Application session is being torn down. Skip application.");
6203 continue;
fad1ed2f 6204 }
8f07cd01 6205 status = consumer_snapshot_channel(socket,
c51311d6
JG
6206 registry->metadata_key, output, 1,
6207 ua_sess->effective_credentials.uid,
6208 ua_sess->effective_credentials.gid,
1819b04a 6209 trace_path, wait, 0);
8f07cd01
JG
6210 switch (status) {
6211 case LTTNG_OK:
6212 break;
6213 case LTTNG_ERR_CHAN_NOT_FOUND:
6214 continue;
6215 default:
8c924c7b
MD
6216 goto error;
6217 }
6218 }
6219 break;
6220 }
6221 default:
6222 assert(0);
6223 break;
6dc3064a
DG
6224 }
6225
6226error:
9cb4b50c 6227 free(trace_path);
6dc3064a 6228 rcu_read_unlock();
8f07cd01 6229 return status;
6dc3064a 6230}
5c786ded
JD
6231
6232/*
d07ceecd 6233 * Return the size taken by one more packet per stream.
5c786ded 6234 */
65ff8ea3
JG
6235uint64_t ust_app_get_size_one_more_packet_per_stream(
6236 const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
5c786ded 6237{
d07ceecd 6238 uint64_t tot_size = 0;
5c786ded
JD
6239 struct ust_app *app;
6240 struct lttng_ht_iter iter;
6241
6242 assert(usess);
6243
6244 switch (usess->buffer_type) {
6245 case LTTNG_BUFFER_PER_UID:
6246 {
6247 struct buffer_reg_uid *reg;
6248
6249 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6250 struct buffer_reg_channel *reg_chan;
6251
b7064eaa 6252 rcu_read_lock();
5c786ded
JD
6253 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6254 reg_chan, node.node) {
d07ceecd
MD
6255 if (cur_nr_packets >= reg_chan->num_subbuf) {
6256 /*
6257 * Don't take channel into account if we
6258 * already grab all its packets.
6259 */
6260 continue;
6261 }
6262 tot_size += reg_chan->subbuf_size * reg_chan->stream_count;
5c786ded 6263 }
b7064eaa 6264 rcu_read_unlock();
5c786ded
JD
6265 }
6266 break;
6267 }
6268 case LTTNG_BUFFER_PER_PID:
6269 {
6270 rcu_read_lock();
6271 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6272 struct ust_app_channel *ua_chan;
6273 struct ust_app_session *ua_sess;
6274 struct lttng_ht_iter chan_iter;
6275
6276 ua_sess = lookup_session_by_app(usess, app);
6277 if (!ua_sess) {
6278 /* Session not associated with this app. */
6279 continue;
6280 }
6281
6282 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6283 ua_chan, node.node) {
d07ceecd
MD
6284 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
6285 /*
6286 * Don't take channel into account if we
6287 * already grab all its packets.
6288 */
6289 continue;
6290 }
6291 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
5c786ded
JD
6292 }
6293 }
6294 rcu_read_unlock();
6295 break;
6296 }
6297 default:
6298 assert(0);
6299 break;
6300 }
6301
d07ceecd 6302 return tot_size;
5c786ded 6303}
fb83fe64
JD
6304
6305int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
6306 struct cds_list_head *buffer_reg_uid_list,
6307 struct consumer_output *consumer, uint64_t uchan_id,
6308 int overwrite, uint64_t *discarded, uint64_t *lost)
6309{
6310 int ret;
6311 uint64_t consumer_chan_key;
6312
70dd8162
MD
6313 *discarded = 0;
6314 *lost = 0;
6315
fb83fe64 6316 ret = buffer_reg_uid_consumer_channel_key(
76604852 6317 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
fb83fe64 6318 if (ret < 0) {
70dd8162
MD
6319 /* Not found */
6320 ret = 0;
fb83fe64
JD
6321 goto end;
6322 }
6323
6324 if (overwrite) {
6325 ret = consumer_get_lost_packets(ust_session_id,
6326 consumer_chan_key, consumer, lost);
6327 } else {
6328 ret = consumer_get_discarded_events(ust_session_id,
6329 consumer_chan_key, consumer, discarded);
6330 }
6331
6332end:
6333 return ret;
6334}
6335
6336int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
6337 struct ltt_ust_channel *uchan,
6338 struct consumer_output *consumer, int overwrite,
6339 uint64_t *discarded, uint64_t *lost)
6340{
6341 int ret = 0;
6342 struct lttng_ht_iter iter;
6343 struct lttng_ht_node_str *ua_chan_node;
6344 struct ust_app *app;
6345 struct ust_app_session *ua_sess;
6346 struct ust_app_channel *ua_chan;
6347
70dd8162
MD
6348 *discarded = 0;
6349 *lost = 0;
6350
fb83fe64
JD
6351 rcu_read_lock();
6352 /*
70dd8162
MD
6353 * Iterate over every registered applications. Sum counters for
6354 * all applications containing requested session and channel.
fb83fe64
JD
6355 */
6356 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6357 struct lttng_ht_iter uiter;
6358
6359 ua_sess = lookup_session_by_app(usess, app);
6360 if (ua_sess == NULL) {
6361 continue;
6362 }
6363
6364 /* Get channel */
ee022399 6365 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
fb83fe64
JD
6366 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6367 /* If the session is found for the app, the channel must be there */
6368 assert(ua_chan_node);
6369
6370 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
6371
6372 if (overwrite) {
70dd8162
MD
6373 uint64_t _lost;
6374
fb83fe64 6375 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
70dd8162
MD
6376 consumer, &_lost);
6377 if (ret < 0) {
6378 break;
6379 }
6380 (*lost) += _lost;
fb83fe64 6381 } else {
70dd8162
MD
6382 uint64_t _discarded;
6383
fb83fe64 6384 ret = consumer_get_discarded_events(usess->id,
70dd8162
MD
6385 ua_chan->key, consumer, &_discarded);
6386 if (ret < 0) {
6387 break;
6388 }
6389 (*discarded) += _discarded;
fb83fe64 6390 }
fb83fe64
JD
6391 }
6392
fb83fe64
JD
6393 rcu_read_unlock();
6394 return ret;
6395}
c2561365
JD
6396
6397static
6398int ust_app_regenerate_statedump(struct ltt_ust_session *usess,
6399 struct ust_app *app)
6400{
6401 int ret = 0;
6402 struct ust_app_session *ua_sess;
6403
6404 DBG("Regenerating the metadata for ust app pid %d", app->pid);
6405
6406 rcu_read_lock();
6407
6408 ua_sess = lookup_session_by_app(usess, app);
6409 if (ua_sess == NULL) {
6410 /* The session is in teardown process. Ignore and continue. */
6411 goto end;
6412 }
6413
6414 pthread_mutex_lock(&ua_sess->lock);
6415
6416 if (ua_sess->deleted) {
6417 goto end_unlock;
6418 }
6419
6420 pthread_mutex_lock(&app->sock_lock);
6421 ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle);
6422 pthread_mutex_unlock(&app->sock_lock);
6423
6424end_unlock:
6425 pthread_mutex_unlock(&ua_sess->lock);
6426
6427end:
6428 rcu_read_unlock();
6429 health_code_update();
6430 return ret;
6431}
6432
6433/*
6434 * Regenerate the statedump for each app in the session.
6435 */
6436int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
6437{
6438 int ret = 0;
6439 struct lttng_ht_iter iter;
6440 struct ust_app *app;
6441
6442 DBG("Regenerating the metadata for all UST apps");
6443
6444 rcu_read_lock();
6445
6446 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6447 if (!app->compatible) {
6448 continue;
6449 }
6450
6451 ret = ust_app_regenerate_statedump(usess, app);
6452 if (ret < 0) {
6453 /* Continue to the next app even on error */
6454 continue;
6455 }
6456 }
6457
6458 rcu_read_unlock();
6459
6460 return 0;
6461}
5c408ad8
JD
6462
6463/*
6464 * Rotate all the channels of a session.
6465 *
fbb8cd76 6466 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 6467 */
fbb8cd76 6468enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
5c408ad8 6469{
fbb8cd76
MD
6470 int ret;
6471 enum lttng_error_code cmd_ret = LTTNG_OK;
5c408ad8
JD
6472 struct lttng_ht_iter iter;
6473 struct ust_app *app;
6474 struct ltt_ust_session *usess = session->ust_session;
5c408ad8
JD
6475
6476 assert(usess);
6477
6478 rcu_read_lock();
6479
6480 switch (usess->buffer_type) {
6481 case LTTNG_BUFFER_PER_UID:
6482 {
6483 struct buffer_reg_uid *reg;
6484
6485 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6486 struct buffer_reg_channel *reg_chan;
6487 struct consumer_socket *socket;
6488
6489 /* Get consumer socket to use to push the metadata.*/
6490 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6491 usess->consumer);
6492 if (!socket) {
fbb8cd76 6493 cmd_ret = LTTNG_ERR_INVALID;
5c408ad8
JD
6494 goto error;
6495 }
6496
5c408ad8
JD
6497 /* Rotate the data channels. */
6498 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6499 reg_chan, node.node) {
5c408ad8
JD
6500 ret = consumer_rotate_channel(socket,
6501 reg_chan->consumer_key,
6502 usess->uid, usess->gid,
e5148e25
JG
6503 usess->consumer,
6504 /* is_metadata_channel */ false);
5c408ad8 6505 if (ret < 0) {
fbb8cd76 6506 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6507 goto error;
6508 }
6509 }
6510
e1ac6bb9
JR
6511 /*
6512 * The metadata channel might not be present.
6513 *
6514 * Consumer stream allocation can be done
6515 * asynchronously and can fail on intermediary
6516 * operations (i.e add context) and lead to data
6517 * channels created with no metadata channel.
6518 */
6519 if (!reg->registry->reg.ust->metadata_key) {
6520 /* Skip since no metadata is present. */
6521 continue;
6522 }
6523
5c408ad8
JD
6524 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
6525
6526 ret = consumer_rotate_channel(socket,
6527 reg->registry->reg.ust->metadata_key,
6528 usess->uid, usess->gid,
e5148e25
JG
6529 usess->consumer,
6530 /* is_metadata_channel */ true);
5c408ad8 6531 if (ret < 0) {
fbb8cd76 6532 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6533 goto error;
6534 }
5c408ad8
JD
6535 }
6536 break;
6537 }
6538 case LTTNG_BUFFER_PER_PID:
6539 {
6540 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6541 struct consumer_socket *socket;
6542 struct lttng_ht_iter chan_iter;
6543 struct ust_app_channel *ua_chan;
6544 struct ust_app_session *ua_sess;
6545 struct ust_registry_session *registry;
6546
6547 ua_sess = lookup_session_by_app(usess, app);
6548 if (!ua_sess) {
6549 /* Session not associated with this app. */
6550 continue;
6551 }
5c408ad8
JD
6552
6553 /* Get the right consumer socket for the application. */
6554 socket = consumer_find_socket_by_bitness(app->bits_per_long,
6555 usess->consumer);
6556 if (!socket) {
fbb8cd76 6557 cmd_ret = LTTNG_ERR_INVALID;
5c408ad8
JD
6558 goto error;
6559 }
6560
6561 registry = get_session_registry(ua_sess);
6562 if (!registry) {
fbb8cd76
MD
6563 DBG("Application session is being torn down. Skip application.");
6564 continue;
5c408ad8
JD
6565 }
6566
5c408ad8
JD
6567 /* Rotate the data channels. */
6568 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6569 ua_chan, node.node) {
c51311d6
JG
6570 ret = consumer_rotate_channel(socket,
6571 ua_chan->key,
6572 ua_sess->effective_credentials
6573 .uid,
6574 ua_sess->effective_credentials
6575 .gid,
e5148e25
JG
6576 ua_sess->consumer,
6577 /* is_metadata_channel */ false);
5c408ad8 6578 if (ret < 0) {
fbb8cd76
MD
6579 /* Per-PID buffer and application going away. */
6580 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
6581 continue;
6582 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6583 goto error;
6584 }
6585 }
6586
6587 /* Rotate the metadata channel. */
6588 (void) push_metadata(registry, usess->consumer);
c51311d6
JG
6589 ret = consumer_rotate_channel(socket,
6590 registry->metadata_key,
6591 ua_sess->effective_credentials.uid,
6592 ua_sess->effective_credentials.gid,
e5148e25
JG
6593 ua_sess->consumer,
6594 /* is_metadata_channel */ true);
5c408ad8 6595 if (ret < 0) {
fbb8cd76
MD
6596 /* Per-PID buffer and application going away. */
6597 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
6598 continue;
6599 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6600 goto error;
6601 }
5c408ad8
JD
6602 }
6603 break;
6604 }
6605 default:
6606 assert(0);
6607 break;
6608 }
6609
fbb8cd76 6610 cmd_ret = LTTNG_OK;
5c408ad8
JD
6611
6612error:
6613 rcu_read_unlock();
fbb8cd76 6614 return cmd_ret;
5c408ad8 6615}
e5148e25
JG
6616
6617enum lttng_error_code ust_app_create_channel_subdirectories(
6618 const struct ltt_ust_session *usess)
6619{
6620 enum lttng_error_code ret = LTTNG_OK;
6621 struct lttng_ht_iter iter;
6622 enum lttng_trace_chunk_status chunk_status;
6623 char *pathname_index;
6624 int fmt_ret;
6625
6626 assert(usess->current_trace_chunk);
6627 rcu_read_lock();
6628
6629 switch (usess->buffer_type) {
6630 case LTTNG_BUFFER_PER_UID:
6631 {
6632 struct buffer_reg_uid *reg;
6633
6634 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6635 fmt_ret = asprintf(&pathname_index,
6636 DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
6637 reg->uid, reg->bits_per_long);
6638 if (fmt_ret < 0) {
6639 ERR("Failed to format channel index directory");
6640 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6641 goto error;
6642 }
6643
6644 /*
6645 * Create the index subdirectory which will take care
6646 * of implicitly creating the channel's path.
6647 */
6648 chunk_status = lttng_trace_chunk_create_subdirectory(
6649 usess->current_trace_chunk,
6650 pathname_index);
6651 free(pathname_index);
6652 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6653 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6654 goto error;
6655 }
6656 }
6657 break;
6658 }
6659 case LTTNG_BUFFER_PER_PID:
6660 {
6661 struct ust_app *app;
6662
8366341d
MD
6663 /*
6664 * Create the toplevel ust/ directory in case no apps are running.
6665 */
6666 chunk_status = lttng_trace_chunk_create_subdirectory(
6667 usess->current_trace_chunk,
6668 DEFAULT_UST_TRACE_DIR);
6669 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6670 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6671 goto error;
6672 }
6673
e5148e25
JG
6674 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
6675 pid_n.node) {
6676 struct ust_app_session *ua_sess;
6677 struct ust_registry_session *registry;
6678
6679 ua_sess = lookup_session_by_app(usess, app);
6680 if (!ua_sess) {
6681 /* Session not associated with this app. */
6682 continue;
6683 }
6684
6685 registry = get_session_registry(ua_sess);
6686 if (!registry) {
6687 DBG("Application session is being torn down. Skip application.");
6688 continue;
6689 }
6690
6691 fmt_ret = asprintf(&pathname_index,
6692 DEFAULT_UST_TRACE_DIR "%s/" DEFAULT_INDEX_DIR,
6693 ua_sess->path);
6694 if (fmt_ret < 0) {
6695 ERR("Failed to format channel index directory");
6696 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6697 goto error;
6698 }
6699 /*
6700 * Create the index subdirectory which will take care
6701 * of implicitly creating the channel's path.
6702 */
6703 chunk_status = lttng_trace_chunk_create_subdirectory(
6704 usess->current_trace_chunk,
6705 pathname_index);
6706 free(pathname_index);
6707 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6708 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6709 goto error;
6710 }
6711 }
6712 break;
6713 }
6714 default:
6715 abort();
6716 }
6717
6718 ret = LTTNG_OK;
6719error:
6720 rcu_read_unlock();
6721 return ret;
6722}
This page took 0.510781 seconds and 4 git commands to generate.