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