Cleanup: privatize consumer_allocate_relayd_sock_pair
[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;
c585821b 443 size_t len, offset, new_metadata_len_sent;
331744e3 444 ssize_t ret_val;
c585821b 445 uint64_t metadata_key;
331744e3
JD
446
447 assert(registry);
448 assert(socket);
1b532a60 449
c585821b
MD
450 metadata_key = registry->metadata_key;
451
ce34fcd0 452 /*
dc2bbdae
MD
453 * Means that no metadata was assigned to the session. This can
454 * happens if no start has been done previously.
ce34fcd0 455 */
c585821b 456 if (!metadata_key) {
ce34fcd0
MD
457 return 0;
458 }
459
1b532a60 460 /*
dc2bbdae
MD
461 * On a push metadata error either the consumer is dead or the
462 * metadata channel has been destroyed because its endpoint
2c57e06d
MD
463 * might have died (e.g: relayd), or because the application has
464 * exited. If so, the metadata closed flag is set to 1 so we
465 * deny pushing metadata again which is not valid anymore on the
466 * consumer side.
1b532a60
DG
467 */
468 if (registry->metadata_closed) {
469 return -EPIPE;
470 }
331744e3 471
331744e3
JD
472 offset = registry->metadata_len_sent;
473 len = registry->metadata_len - registry->metadata_len_sent;
c585821b 474 new_metadata_len_sent = registry->metadata_len;
331744e3
JD
475 if (len == 0) {
476 DBG3("No metadata to push for metadata key %" PRIu64,
477 registry->metadata_key);
478 ret_val = len;
479 if (send_zero_data) {
480 DBG("No metadata to push");
481 goto push_data;
482 }
483 goto end;
484 }
485
486 /* Allocate only what we have to send. */
487 metadata_str = zmalloc(len);
488 if (!metadata_str) {
489 PERROR("zmalloc ust app metadata string");
490 ret_val = -ENOMEM;
491 goto error;
492 }
c585821b 493 /* Copy what we haven't sent out. */
331744e3 494 memcpy(metadata_str, registry->metadata + offset, len);
331744e3
JD
495
496push_data:
c585821b
MD
497 pthread_mutex_unlock(&registry->lock);
498 /*
499 * We need to unlock the registry while we push metadata to
500 * break a circular dependency between the consumerd metadata
501 * lock and the sessiond registry lock. Indeed, pushing metadata
502 * to the consumerd awaits that it gets pushed all the way to
503 * relayd, but doing so requires grabbing the metadata lock. If
504 * a concurrent metadata request is being performed by
505 * consumerd, this can try to grab the registry lock on the
506 * sessiond while holding the metadata lock on the consumer
507 * daemon. Those push and pull schemes are performed on two
508 * different bidirectionnal communication sockets.
509 */
510 ret = consumer_push_metadata(socket, metadata_key,
331744e3 511 metadata_str, len, offset);
c585821b 512 pthread_mutex_lock(&registry->lock);
331744e3 513 if (ret < 0) {
000baf6a 514 /*
dc2bbdae
MD
515 * There is an acceptable race here between the registry
516 * metadata key assignment and the creation on the
517 * consumer. The session daemon can concurrently push
518 * metadata for this registry while being created on the
519 * consumer since the metadata key of the registry is
520 * assigned *before* it is setup to avoid the consumer
521 * to ask for metadata that could possibly be not found
522 * in the session daemon.
000baf6a 523 *
dc2bbdae
MD
524 * The metadata will get pushed either by the session
525 * being stopped or the consumer requesting metadata if
526 * that race is triggered.
000baf6a
DG
527 */
528 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
529 ret = 0;
c585821b
MD
530 } else {
531 ERR("Error pushing metadata to consumer");
000baf6a 532 }
331744e3
JD
533 ret_val = ret;
534 goto error_push;
c585821b
MD
535 } else {
536 /*
537 * Metadata may have been concurrently pushed, since
538 * we're not holding the registry lock while pushing to
539 * consumer. This is handled by the fact that we send
540 * the metadata content, size, and the offset at which
541 * that metadata belongs. This may arrive out of order
542 * on the consumer side, and the consumer is able to
543 * deal with overlapping fragments. The consumer
544 * supports overlapping fragments, which must be
545 * contiguous starting from offset 0. We keep the
546 * largest metadata_len_sent value of the concurrent
547 * send.
548 */
549 registry->metadata_len_sent =
550 max_t(size_t, registry->metadata_len_sent,
551 new_metadata_len_sent);
331744e3 552 }
331744e3
JD
553 free(metadata_str);
554 return len;
555
556end:
557error:
ce34fcd0
MD
558 if (ret_val) {
559 /*
dc2bbdae
MD
560 * On error, flag the registry that the metadata is
561 * closed. We were unable to push anything and this
562 * means that either the consumer is not responding or
563 * the metadata cache has been destroyed on the
564 * consumer.
ce34fcd0
MD
565 */
566 registry->metadata_closed = 1;
567 }
331744e3
JD
568error_push:
569 free(metadata_str);
570 return ret_val;
571}
572
d88aee68 573/*
ce34fcd0 574 * For a given application and session, push metadata to consumer.
331744e3
JD
575 * Either sock or consumer is required : if sock is NULL, the default
576 * socket to send the metadata is retrieved from consumer, if sock
577 * is not NULL we use it to send the metadata.
ce34fcd0 578 * RCU read-side lock must be held while calling this function,
dc2bbdae
MD
579 * therefore ensuring existance of registry. It also ensures existance
580 * of socket throughout this function.
d88aee68
DG
581 *
582 * Return 0 on success else a negative error.
2c57e06d
MD
583 * Returning a -EPIPE return value means we could not send the metadata,
584 * but it can be caused by recoverable errors (e.g. the application has
585 * terminated concurrently).
d88aee68 586 */
7972aab2
DG
587static int push_metadata(struct ust_registry_session *registry,
588 struct consumer_output *consumer)
d88aee68 589{
331744e3
JD
590 int ret_val;
591 ssize_t ret;
d88aee68
DG
592 struct consumer_socket *socket;
593
7972aab2
DG
594 assert(registry);
595 assert(consumer);
596
ce34fcd0 597 pthread_mutex_lock(&registry->lock);
ce34fcd0 598 if (registry->metadata_closed) {
dc2bbdae
MD
599 ret_val = -EPIPE;
600 goto error;
d88aee68
DG
601 }
602
d88aee68 603 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
604 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
605 consumer);
d88aee68 606 if (!socket) {
331744e3 607 ret_val = -1;
ce34fcd0 608 goto error;
d88aee68
DG
609 }
610
331744e3 611 ret = ust_app_push_metadata(registry, socket, 0);
d88aee68 612 if (ret < 0) {
331744e3 613 ret_val = ret;
ce34fcd0 614 goto error;
d88aee68 615 }
dc2bbdae 616 pthread_mutex_unlock(&registry->lock);
d88aee68
DG
617 return 0;
618
ce34fcd0 619error:
dc2bbdae 620 pthread_mutex_unlock(&registry->lock);
331744e3 621 return ret_val;
d88aee68
DG
622}
623
624/*
625 * Send to the consumer a close metadata command for the given session. Once
626 * done, the metadata channel is deleted and the session metadata pointer is
dc2bbdae 627 * nullified. The session lock MUST be held unless the application is
d88aee68
DG
628 * in the destroy path.
629 *
630 * Return 0 on success else a negative value.
631 */
7972aab2
DG
632static int close_metadata(struct ust_registry_session *registry,
633 struct consumer_output *consumer)
d88aee68
DG
634{
635 int ret;
636 struct consumer_socket *socket;
637
7972aab2
DG
638 assert(registry);
639 assert(consumer);
d88aee68 640
7972aab2
DG
641 rcu_read_lock();
642
ce34fcd0
MD
643 pthread_mutex_lock(&registry->lock);
644
7972aab2 645 if (!registry->metadata_key || registry->metadata_closed) {
d88aee68 646 ret = 0;
1b532a60 647 goto end;
d88aee68
DG
648 }
649
d88aee68 650 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
651 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
652 consumer);
d88aee68
DG
653 if (!socket) {
654 ret = -1;
7972aab2 655 goto error;
d88aee68
DG
656 }
657
7972aab2 658 ret = consumer_close_metadata(socket, registry->metadata_key);
d88aee68 659 if (ret < 0) {
7972aab2 660 goto error;
d88aee68
DG
661 }
662
d88aee68 663error:
1b532a60
DG
664 /*
665 * Metadata closed. Even on error this means that the consumer is not
666 * responding or not found so either way a second close should NOT be emit
667 * for this registry.
668 */
669 registry->metadata_closed = 1;
670end:
ce34fcd0 671 pthread_mutex_unlock(&registry->lock);
7972aab2 672 rcu_read_unlock();
d88aee68
DG
673 return ret;
674}
675
36b588ed
MD
676/*
677 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
678 * section and outside of call_rcu thread, so we postpone its execution
679 * using ht_cleanup_push. It is simpler than to change the semantic of
680 * the many callers of delete_ust_app_session().
36b588ed
MD
681 */
682static
683void delete_ust_app_session_rcu(struct rcu_head *head)
684{
685 struct ust_app_session *ua_sess =
686 caa_container_of(head, struct ust_app_session, rcu_head);
687
0b2dc8df 688 ht_cleanup_push(ua_sess->channels);
36b588ed
MD
689 free(ua_sess);
690}
691
d80a6244
DG
692/*
693 * Delete ust app session safely. RCU read lock must be held before calling
694 * this function.
695 */
8b366481 696static
d0b96690
DG
697void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
698 struct ust_app *app)
d80a6244
DG
699{
700 int ret;
bec39940 701 struct lttng_ht_iter iter;
d80a6244 702 struct ust_app_channel *ua_chan;
7972aab2 703 struct ust_registry_session *registry;
d80a6244 704
d88aee68
DG
705 assert(ua_sess);
706
1b532a60
DG
707 pthread_mutex_lock(&ua_sess->lock);
708
b161602a
MD
709 assert(!ua_sess->deleted);
710 ua_sess->deleted = true;
711
7972aab2 712 registry = get_session_registry(ua_sess);
ce34fcd0 713 if (registry) {
d88aee68 714 /* Push metadata for application before freeing the application. */
7972aab2 715 (void) push_metadata(registry, ua_sess->consumer);
d88aee68 716
7972aab2
DG
717 /*
718 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
719 * metadata only on destroy trace session in this case. Also, the
720 * previous push metadata could have flag the metadata registry to
721 * close so don't send a close command if closed.
7972aab2 722 */
ce34fcd0 723 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
724 /* And ask to close it for this session registry. */
725 (void) close_metadata(registry, ua_sess->consumer);
726 }
d80a6244
DG
727 }
728
bec39940
DG
729 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
730 node.node) {
731 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 732 assert(!ret);
d0b96690 733 delete_ust_app_channel(sock, ua_chan, app);
d80a6244 734 }
d80a6244 735
7972aab2
DG
736 /* In case of per PID, the registry is kept in the session. */
737 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
738 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
739 if (reg_pid) {
740 buffer_reg_pid_remove(reg_pid);
741 buffer_reg_pid_destroy(reg_pid);
742 }
743 }
d0b96690 744
aee6bafd 745 if (ua_sess->handle != -1) {
ffe60014
DG
746 ret = ustctl_release_handle(sock, ua_sess->handle);
747 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
748 ERR("UST app sock %d release session handle failed with ret %d",
749 sock, ret);
750 }
aee6bafd 751 }
1b532a60
DG
752 pthread_mutex_unlock(&ua_sess->lock);
753
6addfa37
MD
754 consumer_output_put(ua_sess->consumer);
755
36b588ed 756 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
d80a6244 757}
91d76f53
DG
758
759/*
284d8f55
DG
760 * Delete a traceable application structure from the global list. Never call
761 * this function outside of a call_rcu call.
36b588ed
MD
762 *
763 * RCU read side lock should _NOT_ be held when calling this function.
91d76f53 764 */
8b366481
DG
765static
766void delete_ust_app(struct ust_app *app)
91d76f53 767{
8b366481 768 int ret, sock;
d42f20df 769 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 770
d80a6244 771 /* Delete ust app sessions info */
852d0037
DG
772 sock = app->sock;
773 app->sock = -1;
d80a6244 774
8b366481 775 /* Wipe sessions */
d42f20df
DG
776 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
777 teardown_node) {
778 /* Free every object in the session and the session. */
36b588ed 779 rcu_read_lock();
d0b96690 780 delete_ust_app_session(sock, ua_sess, app);
36b588ed 781 rcu_read_unlock();
d80a6244 782 }
36b588ed 783
0b2dc8df
MD
784 ht_cleanup_push(app->sessions);
785 ht_cleanup_push(app->ust_objd);
d80a6244 786
6414a713 787 /*
852d0037
DG
788 * Wait until we have deleted the application from the sock hash table
789 * before closing this socket, otherwise an application could re-use the
790 * socket ID and race with the teardown, using the same hash table entry.
791 *
792 * It's OK to leave the close in call_rcu. We want it to stay unique for
793 * all RCU readers that could run concurrently with unregister app,
794 * therefore we _need_ to only close that socket after a grace period. So
795 * it should stay in this RCU callback.
796 *
797 * This close() is a very important step of the synchronization model so
798 * every modification to this function must be carefully reviewed.
6414a713 799 */
799e2c4f
MD
800 ret = close(sock);
801 if (ret) {
802 PERROR("close");
803 }
4063050c 804 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 805
852d0037 806 DBG2("UST app pid %d deleted", app->pid);
284d8f55 807 free(app);
099e26bd
DG
808}
809
810/*
f6a9efaa 811 * URCU intermediate call to delete an UST app.
099e26bd 812 */
8b366481
DG
813static
814void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 815{
bec39940
DG
816 struct lttng_ht_node_ulong *node =
817 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 818 struct ust_app *app =
852d0037 819 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 820
852d0037 821 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 822 delete_ust_app(app);
099e26bd
DG
823}
824
ffe60014
DG
825/*
826 * Delete the session from the application ht and delete the data structure by
827 * freeing every object inside and releasing them.
828 */
d0b96690 829static void destroy_app_session(struct ust_app *app,
ffe60014
DG
830 struct ust_app_session *ua_sess)
831{
832 int ret;
833 struct lttng_ht_iter iter;
834
835 assert(app);
836 assert(ua_sess);
837
838 iter.iter.node = &ua_sess->node.node;
839 ret = lttng_ht_del(app->sessions, &iter);
840 if (ret) {
841 /* Already scheduled for teardown. */
842 goto end;
843 }
844
845 /* Once deleted, free the data structure. */
d0b96690 846 delete_ust_app_session(app->sock, ua_sess, app);
ffe60014
DG
847
848end:
849 return;
850}
851
8b366481
DG
852/*
853 * Alloc new UST app session.
854 */
855static
d0b96690 856struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
8b366481
DG
857{
858 struct ust_app_session *ua_sess;
859
860 /* Init most of the default value by allocating and zeroing */
861 ua_sess = zmalloc(sizeof(struct ust_app_session));
862 if (ua_sess == NULL) {
863 PERROR("malloc");
ffe60014 864 goto error_free;
8b366481
DG
865 }
866
867 ua_sess->handle = -1;
bec39940 868 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
ad7a9107 869 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
84ad93e8 870 pthread_mutex_init(&ua_sess->lock, NULL);
ad7a9107 871
8b366481
DG
872 return ua_sess;
873
ffe60014 874error_free:
8b366481
DG
875 return NULL;
876}
877
878/*
879 * Alloc new UST app channel.
880 */
881static
882struct ust_app_channel *alloc_ust_app_channel(char *name,
d0b96690 883 struct ust_app_session *ua_sess,
ffe60014 884 struct lttng_ust_channel_attr *attr)
8b366481
DG
885{
886 struct ust_app_channel *ua_chan;
887
888 /* Init most of the default value by allocating and zeroing */
889 ua_chan = zmalloc(sizeof(struct ust_app_channel));
890 if (ua_chan == NULL) {
891 PERROR("malloc");
892 goto error;
893 }
894
895 /* Setup channel name */
896 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
897 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
898
899 ua_chan->enabled = 1;
900 ua_chan->handle = -1;
45893984 901 ua_chan->session = ua_sess;
ffe60014 902 ua_chan->key = get_next_channel_key();
bec39940
DG
903 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
904 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
905 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
906
907 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
31746f93 908 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
8b366481
DG
909
910 /* Copy attributes */
911 if (attr) {
ffe60014 912 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
2fe6e7f5
DG
913 ua_chan->attr.subbuf_size = attr->subbuf_size;
914 ua_chan->attr.num_subbuf = attr->num_subbuf;
915 ua_chan->attr.overwrite = attr->overwrite;
916 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
917 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
918 ua_chan->attr.output = attr->output;
8b366481 919 }
ffe60014
DG
920 /* By default, the channel is a per cpu channel. */
921 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
8b366481
DG
922
923 DBG3("UST app channel %s allocated", ua_chan->name);
924
925 return ua_chan;
926
927error:
928 return NULL;
929}
930
37f1c236
DG
931/*
932 * Allocate and initialize a UST app stream.
933 *
934 * Return newly allocated stream pointer or NULL on error.
935 */
ffe60014 936struct ust_app_stream *ust_app_alloc_stream(void)
37f1c236
DG
937{
938 struct ust_app_stream *stream = NULL;
939
940 stream = zmalloc(sizeof(*stream));
941 if (stream == NULL) {
942 PERROR("zmalloc ust app stream");
943 goto error;
944 }
945
946 /* Zero could be a valid value for a handle so flag it to -1. */
947 stream->handle = -1;
948
949error:
950 return stream;
951}
952
8b366481
DG
953/*
954 * Alloc new UST app event.
955 */
956static
957struct ust_app_event *alloc_ust_app_event(char *name,
958 struct lttng_ust_event *attr)
959{
960 struct ust_app_event *ua_event;
961
962 /* Init most of the default value by allocating and zeroing */
963 ua_event = zmalloc(sizeof(struct ust_app_event));
964 if (ua_event == NULL) {
965 PERROR("malloc");
966 goto error;
967 }
968
969 ua_event->enabled = 1;
970 strncpy(ua_event->name, name, sizeof(ua_event->name));
971 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 972 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
973
974 /* Copy attributes */
975 if (attr) {
976 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
977 }
978
979 DBG3("UST app event %s allocated", ua_event->name);
980
981 return ua_event;
982
983error:
984 return NULL;
985}
986
987/*
988 * Alloc new UST app context.
989 */
990static
991struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
992{
993 struct ust_app_ctx *ua_ctx;
994
995 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
996 if (ua_ctx == NULL) {
997 goto error;
998 }
999
31746f93
DG
1000 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1001
8b366481
DG
1002 if (uctx) {
1003 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
1004 }
1005
1006 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
1007
1008error:
1009 return ua_ctx;
1010}
1011
025faf73
DG
1012/*
1013 * Allocate a filter and copy the given original filter.
1014 *
1015 * Return allocated filter or NULL on error.
1016 */
51755dc8
JG
1017static struct lttng_filter_bytecode *copy_filter_bytecode(
1018 struct lttng_filter_bytecode *orig_f)
025faf73 1019{
51755dc8 1020 struct lttng_filter_bytecode *filter = NULL;
025faf73
DG
1021
1022 /* Copy filter bytecode */
1023 filter = zmalloc(sizeof(*filter) + orig_f->len);
1024 if (!filter) {
51755dc8 1025 PERROR("zmalloc alloc filter bytecode");
025faf73
DG
1026 goto error;
1027 }
1028
1029 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1030
1031error:
1032 return filter;
1033}
1034
51755dc8
JG
1035/*
1036 * Create a liblttng-ust filter bytecode from given bytecode.
1037 *
1038 * Return allocated filter or NULL on error.
1039 */
1040static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
1041 struct lttng_filter_bytecode *orig_f)
1042{
1043 struct lttng_ust_filter_bytecode *filter = NULL;
1044
1045 /* Copy filter bytecode */
1046 filter = zmalloc(sizeof(*filter) + orig_f->len);
1047 if (!filter) {
1048 PERROR("zmalloc alloc ust filter bytecode");
1049 goto error;
1050 }
1051
1052 assert(sizeof(struct lttng_filter_bytecode) ==
1053 sizeof(struct lttng_ust_filter_bytecode));
1054 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1055error:
1056 return filter;
1057}
1058
099e26bd 1059/*
421cb601
DG
1060 * Find an ust_app using the sock and return it. RCU read side lock must be
1061 * held before calling this helper function.
099e26bd 1062 */
f20baf8e 1063struct ust_app *ust_app_find_by_sock(int sock)
099e26bd 1064{
bec39940 1065 struct lttng_ht_node_ulong *node;
bec39940 1066 struct lttng_ht_iter iter;
f6a9efaa 1067
852d0037 1068 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1069 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
1070 if (node == NULL) {
1071 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
1072 goto error;
1073 }
852d0037
DG
1074
1075 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
1076
1077error:
1078 return NULL;
099e26bd
DG
1079}
1080
d0b96690
DG
1081/*
1082 * Find an ust_app using the notify sock and return it. RCU read side lock must
1083 * be held before calling this helper function.
1084 */
1085static struct ust_app *find_app_by_notify_sock(int sock)
1086{
1087 struct lttng_ht_node_ulong *node;
1088 struct lttng_ht_iter iter;
1089
1090 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1091 &iter);
1092 node = lttng_ht_iter_get_node_ulong(&iter);
1093 if (node == NULL) {
1094 DBG2("UST app find by notify sock %d not found", sock);
1095 goto error;
1096 }
1097
1098 return caa_container_of(node, struct ust_app, notify_sock_n);
1099
1100error:
1101 return NULL;
1102}
1103
025faf73
DG
1104/*
1105 * Lookup for an ust app event based on event name, filter bytecode and the
1106 * event loglevel.
1107 *
1108 * Return an ust_app_event object or NULL on error.
1109 */
18eace3b 1110static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
51755dc8 1111 char *name, struct lttng_filter_bytecode *filter, int loglevel,
39c5a3a7 1112 const struct lttng_event_exclusion *exclusion)
18eace3b
DG
1113{
1114 struct lttng_ht_iter iter;
1115 struct lttng_ht_node_str *node;
1116 struct ust_app_event *event = NULL;
1117 struct ust_app_ht_key key;
18eace3b
DG
1118
1119 assert(name);
1120 assert(ht);
1121
1122 /* Setup key for event lookup. */
1123 key.name = name;
1124 key.filter = filter;
1125 key.loglevel = loglevel;
39c5a3a7 1126 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
51755dc8 1127 key.exclusion = exclusion;
18eace3b 1128
025faf73
DG
1129 /* Lookup using the event name as hash and a custom match fct. */
1130 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1131 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
1132 node = lttng_ht_iter_get_node_str(&iter);
1133 if (node == NULL) {
1134 goto end;
1135 }
1136
1137 event = caa_container_of(node, struct ust_app_event, node);
1138
1139end:
18eace3b
DG
1140 return event;
1141}
1142
55cc08a6
DG
1143/*
1144 * Create the channel context on the tracer.
d0b96690
DG
1145 *
1146 * Called with UST app session lock held.
55cc08a6
DG
1147 */
1148static
1149int create_ust_channel_context(struct ust_app_channel *ua_chan,
1150 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1151{
1152 int ret;
1153
840cb59c 1154 health_code_update();
86acf0da 1155
852d0037 1156 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6
DG
1157 ua_chan->obj, &ua_ctx->obj);
1158 if (ret < 0) {
ffe60014
DG
1159 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1160 ERR("UST app create channel context failed for app (pid: %d) "
1161 "with ret %d", app->pid, ret);
1162 } else {
3757b385
DG
1163 /*
1164 * This is normal behavior, an application can die during the
1165 * creation process. Don't report an error so the execution can
1166 * continue normally.
1167 */
1168 ret = 0;
ffe60014
DG
1169 DBG3("UST app disable event failed. Application is dead.");
1170 }
55cc08a6
DG
1171 goto error;
1172 }
1173
1174 ua_ctx->handle = ua_ctx->obj->handle;
1175
d0b96690
DG
1176 DBG2("UST app context handle %d created successfully for channel %s",
1177 ua_ctx->handle, ua_chan->name);
55cc08a6
DG
1178
1179error:
840cb59c 1180 health_code_update();
55cc08a6
DG
1181 return ret;
1182}
1183
53a80697
MD
1184/*
1185 * Set the filter on the tracer.
1186 */
1187static
1188int set_ust_event_filter(struct ust_app_event *ua_event,
1189 struct ust_app *app)
1190{
1191 int ret;
51755dc8 1192 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
53a80697 1193
840cb59c 1194 health_code_update();
86acf0da 1195
53a80697 1196 if (!ua_event->filter) {
86acf0da
DG
1197 ret = 0;
1198 goto error;
53a80697
MD
1199 }
1200
51755dc8
JG
1201 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1202 if (!ust_bytecode) {
1203 ret = -LTTNG_ERR_NOMEM;
1204 goto error;
1205 }
1206 ret = ustctl_set_filter(app->sock, ust_bytecode,
53a80697
MD
1207 ua_event->obj);
1208 if (ret < 0) {
ffe60014
DG
1209 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1210 ERR("UST app event %s filter failed for app (pid: %d) "
1211 "with ret %d", ua_event->attr.name, app->pid, ret);
1212 } else {
3757b385
DG
1213 /*
1214 * This is normal behavior, an application can die during the
1215 * creation process. Don't report an error so the execution can
1216 * continue normally.
1217 */
1218 ret = 0;
ffe60014
DG
1219 DBG3("UST app filter event failed. Application is dead.");
1220 }
53a80697
MD
1221 goto error;
1222 }
1223
1224 DBG2("UST filter set successfully for event %s", ua_event->name);
1225
1226error:
840cb59c 1227 health_code_update();
51755dc8 1228 free(ust_bytecode);
53a80697
MD
1229 return ret;
1230}
1231
51755dc8
JG
1232static
1233struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1234 struct lttng_event_exclusion *exclusion)
1235{
1236 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1237 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1238 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1239
1240 ust_exclusion = zmalloc(exclusion_alloc_size);
1241 if (!ust_exclusion) {
1242 PERROR("malloc");
1243 goto end;
1244 }
1245
1246 assert(sizeof(struct lttng_event_exclusion) ==
1247 sizeof(struct lttng_ust_event_exclusion));
1248 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1249end:
1250 return ust_exclusion;
1251}
1252
7cc9a73c
JI
1253/*
1254 * Set event exclusions on the tracer.
1255 */
1256static
1257int set_ust_event_exclusion(struct ust_app_event *ua_event,
1258 struct ust_app *app)
1259{
1260 int ret;
51755dc8 1261 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
7cc9a73c
JI
1262
1263 health_code_update();
1264
1265 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1266 ret = 0;
1267 goto error;
1268 }
1269
51755dc8
JG
1270 ust_exclusion = create_ust_exclusion_from_exclusion(
1271 ua_event->exclusion);
1272 if (!ust_exclusion) {
1273 ret = -LTTNG_ERR_NOMEM;
1274 goto error;
1275 }
1276 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
7cc9a73c
JI
1277 if (ret < 0) {
1278 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1279 ERR("UST app event %s exclusions failed for app (pid: %d) "
1280 "with ret %d", ua_event->attr.name, app->pid, ret);
1281 } else {
1282 /*
1283 * This is normal behavior, an application can die during the
1284 * creation process. Don't report an error so the execution can
1285 * continue normally.
1286 */
1287 ret = 0;
1288 DBG3("UST app event exclusion failed. Application is dead.");
1289 }
1290 goto error;
1291 }
1292
1293 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1294
1295error:
1296 health_code_update();
51755dc8 1297 free(ust_exclusion);
7cc9a73c
JI
1298 return ret;
1299}
1300
9730260e
DG
1301/*
1302 * Disable the specified event on to UST tracer for the UST session.
1303 */
1304static int disable_ust_event(struct ust_app *app,
1305 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1306{
1307 int ret;
1308
840cb59c 1309 health_code_update();
86acf0da 1310
852d0037 1311 ret = ustctl_disable(app->sock, ua_event->obj);
9730260e 1312 if (ret < 0) {
ffe60014
DG
1313 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1314 ERR("UST app event %s disable failed for app (pid: %d) "
1315 "and session handle %d with ret %d",
1316 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1317 } else {
3757b385
DG
1318 /*
1319 * This is normal behavior, an application can die during the
1320 * creation process. Don't report an error so the execution can
1321 * continue normally.
1322 */
1323 ret = 0;
ffe60014
DG
1324 DBG3("UST app disable event failed. Application is dead.");
1325 }
9730260e
DG
1326 goto error;
1327 }
1328
1329 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 1330 ua_event->attr.name, app->pid);
9730260e
DG
1331
1332error:
840cb59c 1333 health_code_update();
9730260e
DG
1334 return ret;
1335}
1336
78f0bacd
DG
1337/*
1338 * Disable the specified channel on to UST tracer for the UST session.
1339 */
1340static int disable_ust_channel(struct ust_app *app,
1341 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1342{
1343 int ret;
1344
840cb59c 1345 health_code_update();
86acf0da 1346
852d0037 1347 ret = ustctl_disable(app->sock, ua_chan->obj);
78f0bacd 1348 if (ret < 0) {
ffe60014
DG
1349 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1350 ERR("UST app channel %s disable failed for app (pid: %d) "
1351 "and session handle %d with ret %d",
1352 ua_chan->name, app->pid, ua_sess->handle, ret);
1353 } else {
3757b385
DG
1354 /*
1355 * This is normal behavior, an application can die during the
1356 * creation process. Don't report an error so the execution can
1357 * continue normally.
1358 */
1359 ret = 0;
ffe60014
DG
1360 DBG3("UST app disable channel failed. Application is dead.");
1361 }
78f0bacd
DG
1362 goto error;
1363 }
1364
78f0bacd 1365 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 1366 ua_chan->name, app->pid);
78f0bacd
DG
1367
1368error:
840cb59c 1369 health_code_update();
78f0bacd
DG
1370 return ret;
1371}
1372
1373/*
1374 * Enable the specified channel on to UST tracer for the UST session.
1375 */
1376static int enable_ust_channel(struct ust_app *app,
1377 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1378{
1379 int ret;
1380
840cb59c 1381 health_code_update();
86acf0da 1382
852d0037 1383 ret = ustctl_enable(app->sock, ua_chan->obj);
78f0bacd 1384 if (ret < 0) {
ffe60014
DG
1385 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1386 ERR("UST app channel %s enable failed for app (pid: %d) "
1387 "and session handle %d with ret %d",
1388 ua_chan->name, app->pid, ua_sess->handle, ret);
1389 } else {
3757b385
DG
1390 /*
1391 * This is normal behavior, an application can die during the
1392 * creation process. Don't report an error so the execution can
1393 * continue normally.
1394 */
1395 ret = 0;
ffe60014
DG
1396 DBG3("UST app enable channel failed. Application is dead.");
1397 }
78f0bacd
DG
1398 goto error;
1399 }
1400
1401 ua_chan->enabled = 1;
1402
1403 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 1404 ua_chan->name, app->pid);
78f0bacd
DG
1405
1406error:
840cb59c 1407 health_code_update();
78f0bacd
DG
1408 return ret;
1409}
1410
edb67388
DG
1411/*
1412 * Enable the specified event on to UST tracer for the UST session.
1413 */
1414static int enable_ust_event(struct ust_app *app,
1415 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1416{
1417 int ret;
1418
840cb59c 1419 health_code_update();
86acf0da 1420
852d0037 1421 ret = ustctl_enable(app->sock, ua_event->obj);
edb67388 1422 if (ret < 0) {
ffe60014
DG
1423 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1424 ERR("UST app event %s enable failed for app (pid: %d) "
1425 "and session handle %d with ret %d",
1426 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1427 } else {
3757b385
DG
1428 /*
1429 * This is normal behavior, an application can die during the
1430 * creation process. Don't report an error so the execution can
1431 * continue normally.
1432 */
1433 ret = 0;
ffe60014
DG
1434 DBG3("UST app enable event failed. Application is dead.");
1435 }
edb67388
DG
1436 goto error;
1437 }
1438
1439 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 1440 ua_event->attr.name, app->pid);
edb67388
DG
1441
1442error:
840cb59c 1443 health_code_update();
edb67388
DG
1444 return ret;
1445}
1446
099e26bd 1447/*
7972aab2 1448 * Send channel and stream buffer to application.
4f3ab6ee 1449 *
ffe60014 1450 * Return 0 on success. On error, a negative value is returned.
4f3ab6ee 1451 */
7972aab2
DG
1452static int send_channel_pid_to_ust(struct ust_app *app,
1453 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
4f3ab6ee
DG
1454{
1455 int ret;
ffe60014 1456 struct ust_app_stream *stream, *stmp;
4f3ab6ee
DG
1457
1458 assert(app);
ffe60014 1459 assert(ua_sess);
4f3ab6ee 1460 assert(ua_chan);
4f3ab6ee 1461
840cb59c 1462 health_code_update();
4f3ab6ee 1463
7972aab2
DG
1464 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1465 app->sock);
86acf0da 1466
ffe60014
DG
1467 /* Send channel to the application. */
1468 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
1469 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1470 ret = -ENOTCONN; /* Caused by app exiting. */
1471 goto error;
1472 } else if (ret < 0) {
b551a063
DG
1473 goto error;
1474 }
1475
d88aee68
DG
1476 health_code_update();
1477
ffe60014
DG
1478 /* Send all streams to application. */
1479 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1480 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
a7169585
MD
1481 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1482 ret = -ENOTCONN; /* Caused by app exiting. */
1483 goto error;
1484 } else if (ret < 0) {
ffe60014
DG
1485 goto error;
1486 }
1487 /* We don't need the stream anymore once sent to the tracer. */
1488 cds_list_del(&stream->list);
1489 delete_ust_app_stream(-1, stream);
1490 }
ffe60014
DG
1491 /* Flag the channel that it is sent to the application. */
1492 ua_chan->is_sent = 1;
ffe60014 1493
b551a063 1494error:
840cb59c 1495 health_code_update();
b551a063
DG
1496 return ret;
1497}
1498
91d76f53 1499/*
5b4a0ec0 1500 * Create the specified event onto the UST tracer for a UST session.
d0b96690
DG
1501 *
1502 * Should be called with session mutex held.
91d76f53 1503 */
edb67388
DG
1504static
1505int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1506 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 1507{
5b4a0ec0 1508 int ret = 0;
284d8f55 1509
840cb59c 1510 health_code_update();
86acf0da 1511
5b4a0ec0 1512 /* Create UST event on tracer */
852d0037 1513 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0
DG
1514 &ua_event->obj);
1515 if (ret < 0) {
ffe60014
DG
1516 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1517 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1518 ua_event->attr.name, app->pid, ret);
1519 } else {
3757b385
DG
1520 /*
1521 * This is normal behavior, an application can die during the
1522 * creation process. Don't report an error so the execution can
1523 * continue normally.
1524 */
1525 ret = 0;
ffe60014
DG
1526 DBG3("UST app create event failed. Application is dead.");
1527 }
5b4a0ec0 1528 goto error;
91d76f53 1529 }
f6a9efaa 1530
5b4a0ec0 1531 ua_event->handle = ua_event->obj->handle;
284d8f55 1532
5b4a0ec0 1533 DBG2("UST app event %s created successfully for pid:%d",
852d0037 1534 ua_event->attr.name, app->pid);
f6a9efaa 1535
840cb59c 1536 health_code_update();
86acf0da 1537
025faf73
DG
1538 /* Set filter if one is present. */
1539 if (ua_event->filter) {
1540 ret = set_ust_event_filter(ua_event, app);
1541 if (ret < 0) {
1542 goto error;
1543 }
1544 }
1545
7cc9a73c
JI
1546 /* Set exclusions for the event */
1547 if (ua_event->exclusion) {
1548 ret = set_ust_event_exclusion(ua_event, app);
1549 if (ret < 0) {
1550 goto error;
1551 }
1552 }
1553
8535a6d9 1554 /* If event not enabled, disable it on the tracer */
40113787
MD
1555 if (ua_event->enabled) {
1556 /*
1557 * We now need to explicitly enable the event, since it
1558 * is now disabled at creation.
1559 */
1560 ret = enable_ust_event(app, ua_sess, ua_event);
1561 if (ret < 0) {
1562 /*
1563 * If we hit an EPERM, something is wrong with our enable call. If
1564 * we get an EEXIST, there is a problem on the tracer side since we
1565 * just created it.
1566 */
1567 switch (ret) {
1568 case -LTTNG_UST_ERR_PERM:
1569 /* Code flow problem */
1570 assert(0);
1571 case -LTTNG_UST_ERR_EXIST:
1572 /* It's OK for our use case. */
1573 ret = 0;
1574 break;
1575 default:
1576 break;
1577 }
1578 goto error;
1579 }
8535a6d9
DG
1580 }
1581
5b4a0ec0 1582error:
840cb59c 1583 health_code_update();
5b4a0ec0 1584 return ret;
91d76f53 1585}
48842b30 1586
5b4a0ec0
DG
1587/*
1588 * Copy data between an UST app event and a LTT event.
1589 */
421cb601 1590static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
1591 struct ltt_ust_event *uevent)
1592{
b4ffad32
JI
1593 size_t exclusion_alloc_size;
1594
48842b30
DG
1595 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1596 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1597
fc34caaa
DG
1598 ua_event->enabled = uevent->enabled;
1599
5b4a0ec0
DG
1600 /* Copy event attributes */
1601 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1602
53a80697
MD
1603 /* Copy filter bytecode */
1604 if (uevent->filter) {
51755dc8 1605 ua_event->filter = copy_filter_bytecode(uevent->filter);
025faf73 1606 /* Filter might be NULL here in case of ENONEM. */
53a80697 1607 }
b4ffad32
JI
1608
1609 /* Copy exclusion data */
1610 if (uevent->exclusion) {
51755dc8 1611 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
b4ffad32
JI
1612 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1613 ua_event->exclusion = zmalloc(exclusion_alloc_size);
5f8df26c
JI
1614 if (ua_event->exclusion == NULL) {
1615 PERROR("malloc");
1616 } else {
1617 memcpy(ua_event->exclusion, uevent->exclusion,
1618 exclusion_alloc_size);
b4ffad32
JI
1619 }
1620 }
48842b30
DG
1621}
1622
5b4a0ec0
DG
1623/*
1624 * Copy data between an UST app channel and a LTT channel.
1625 */
421cb601 1626static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
1627 struct ltt_ust_channel *uchan)
1628{
bec39940 1629 struct lttng_ht_iter iter;
48842b30 1630 struct ltt_ust_event *uevent;
55cc08a6 1631 struct ltt_ust_context *uctx;
48842b30 1632 struct ust_app_event *ua_event;
55cc08a6 1633 struct ust_app_ctx *ua_ctx;
48842b30 1634
fc34caaa 1635 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
1636
1637 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1638 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014 1639
1624d5b7
JD
1640 ua_chan->tracefile_size = uchan->tracefile_size;
1641 ua_chan->tracefile_count = uchan->tracefile_count;
1642
ffe60014
DG
1643 /* Copy event attributes since the layout is different. */
1644 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1645 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1646 ua_chan->attr.overwrite = uchan->attr.overwrite;
1647 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1648 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1649 ua_chan->attr.output = uchan->attr.output;
1650 /*
1651 * Note that the attribute channel type is not set since the channel on the
1652 * tracing registry side does not have this information.
1653 */
48842b30 1654
fc34caaa 1655 ua_chan->enabled = uchan->enabled;
7972aab2 1656 ua_chan->tracing_channel_id = uchan->id;
fc34caaa 1657
31746f93 1658 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
55cc08a6
DG
1659 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1660 if (ua_ctx == NULL) {
1661 continue;
1662 }
bec39940
DG
1663 lttng_ht_node_init_ulong(&ua_ctx->node,
1664 (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 1665 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 1666 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
55cc08a6 1667 }
48842b30 1668
421cb601 1669 /* Copy all events from ltt ust channel to ust app channel */
bec39940 1670 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
18eace3b 1671 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 1672 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 1673 if (ua_event == NULL) {
421cb601 1674 DBG2("UST event %s not found on shadow copy channel",
48842b30 1675 uevent->attr.name);
284d8f55 1676 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 1677 if (ua_event == NULL) {
5b4a0ec0 1678 continue;
48842b30 1679 }
421cb601 1680 shadow_copy_event(ua_event, uevent);
d0b96690 1681 add_unique_ust_app_event(ua_chan, ua_event);
48842b30 1682 }
48842b30
DG
1683 }
1684
fc34caaa 1685 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1686}
1687
5b4a0ec0
DG
1688/*
1689 * Copy data between a UST app session and a regular LTT session.
1690 */
421cb601 1691static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1692 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1693{
bec39940
DG
1694 struct lttng_ht_node_str *ua_chan_node;
1695 struct lttng_ht_iter iter;
48842b30
DG
1696 struct ltt_ust_channel *uchan;
1697 struct ust_app_channel *ua_chan;
477d7741
MD
1698 time_t rawtime;
1699 struct tm *timeinfo;
1700 char datetime[16];
1701 int ret;
d7ba1388 1702 char tmp_shm_path[PATH_MAX];
477d7741
MD
1703
1704 /* Get date and time for unique app path */
1705 time(&rawtime);
1706 timeinfo = localtime(&rawtime);
1707 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1708
421cb601 1709 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1710
7972aab2
DG
1711 ua_sess->tracing_id = usess->id;
1712 ua_sess->id = get_next_session_id();
1713 ua_sess->uid = app->uid;
1714 ua_sess->gid = app->gid;
1715 ua_sess->euid = usess->uid;
1716 ua_sess->egid = usess->gid;
1717 ua_sess->buffer_type = usess->buffer_type;
1718 ua_sess->bits_per_long = app->bits_per_long;
6addfa37 1719
7972aab2 1720 /* There is only one consumer object per session possible. */
6addfa37 1721 consumer_output_get(usess->consumer);
7972aab2 1722 ua_sess->consumer = usess->consumer;
6addfa37 1723
2bba9e53 1724 ua_sess->output_traces = usess->output_traces;
ecc48a90 1725 ua_sess->live_timer_interval = usess->live_timer_interval;
84ad93e8
DG
1726 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1727 &usess->metadata_attr);
7972aab2
DG
1728
1729 switch (ua_sess->buffer_type) {
1730 case LTTNG_BUFFER_PER_PID:
1731 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
dec56f6c 1732 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
7972aab2
DG
1733 datetime);
1734 break;
1735 case LTTNG_BUFFER_PER_UID:
1736 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1737 DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long);
1738 break;
1739 default:
1740 assert(0);
1741 goto error;
1742 }
477d7741
MD
1743 if (ret < 0) {
1744 PERROR("asprintf UST shadow copy session");
477d7741 1745 assert(0);
7972aab2 1746 goto error;
477d7741
MD
1747 }
1748
3d071855
MD
1749 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1750 sizeof(ua_sess->root_shm_path));
1751 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
d7ba1388
MD
1752 strncpy(ua_sess->shm_path, usess->shm_path,
1753 sizeof(ua_sess->shm_path));
1754 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1755 if (ua_sess->shm_path[0]) {
1756 switch (ua_sess->buffer_type) {
1757 case LTTNG_BUFFER_PER_PID:
1758 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1759 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1760 app->name, app->pid, datetime);
1761 break;
1762 case LTTNG_BUFFER_PER_UID:
1763 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1764 DEFAULT_UST_TRACE_UID_PATH,
1765 app->uid, app->bits_per_long);
1766 break;
1767 default:
1768 assert(0);
1769 goto error;
1770 }
1771 if (ret < 0) {
1772 PERROR("sprintf UST shadow copy session");
1773 assert(0);
1774 goto error;
1775 }
1776 strncat(ua_sess->shm_path, tmp_shm_path,
1777 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1778 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1779 }
1780
48842b30 1781 /* Iterate over all channels in global domain. */
bec39940
DG
1782 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1783 uchan, node.node) {
1784 struct lttng_ht_iter uiter;
ba767faf 1785
bec39940
DG
1786 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1787 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5b4a0ec0 1788 if (ua_chan_node != NULL) {
fc34caaa 1789 /* Session exist. Contiuing. */
5b4a0ec0
DG
1790 continue;
1791 }
421cb601 1792
5b4a0ec0
DG
1793 DBG2("Channel %s not found on shadow session copy, creating it",
1794 uchan->name);
d0b96690 1795 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
5b4a0ec0 1796 if (ua_chan == NULL) {
fc34caaa 1797 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
5b4a0ec0 1798 continue;
48842b30 1799 }
5b4a0ec0 1800 shadow_copy_channel(ua_chan, uchan);
ffe60014
DG
1801 /*
1802 * The concept of metadata channel does not exist on the tracing
1803 * registry side of the session daemon so this can only be a per CPU
1804 * channel and not metadata.
1805 */
1806 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1807
bec39940 1808 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
48842b30 1809 }
6addfa37 1810 return;
7972aab2
DG
1811
1812error:
6addfa37 1813 consumer_output_put(ua_sess->consumer);
48842b30
DG
1814}
1815
78f0bacd
DG
1816/*
1817 * Lookup sesison wrapper.
1818 */
84cd17c6
MD
1819static
1820void __lookup_session_by_app(struct ltt_ust_session *usess,
bec39940 1821 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
1822{
1823 /* Get right UST app session from app */
d9bf3ca4 1824 lttng_ht_lookup(app->sessions, &usess->id, iter);
84cd17c6
MD
1825}
1826
421cb601
DG
1827/*
1828 * Return ust app session from the app session hashtable using the UST session
a991f516 1829 * id.
421cb601 1830 */
48842b30
DG
1831static struct ust_app_session *lookup_session_by_app(
1832 struct ltt_ust_session *usess, struct ust_app *app)
1833{
bec39940 1834 struct lttng_ht_iter iter;
d9bf3ca4 1835 struct lttng_ht_node_u64 *node;
48842b30 1836
84cd17c6 1837 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 1838 node = lttng_ht_iter_get_node_u64(&iter);
48842b30
DG
1839 if (node == NULL) {
1840 goto error;
1841 }
1842
1843 return caa_container_of(node, struct ust_app_session, node);
1844
1845error:
1846 return NULL;
1847}
1848
7972aab2
DG
1849/*
1850 * Setup buffer registry per PID for the given session and application. If none
1851 * is found, a new one is created, added to the global registry and
1852 * initialized. If regp is valid, it's set with the newly created object.
1853 *
1854 * Return 0 on success or else a negative value.
1855 */
1856static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1857 struct ust_app *app, struct buffer_reg_pid **regp)
1858{
1859 int ret = 0;
1860 struct buffer_reg_pid *reg_pid;
1861
1862 assert(ua_sess);
1863 assert(app);
1864
1865 rcu_read_lock();
1866
1867 reg_pid = buffer_reg_pid_find(ua_sess->id);
1868 if (!reg_pid) {
1869 /*
1870 * This is the create channel path meaning that if there is NO
1871 * registry available, we have to create one for this session.
1872 */
d7ba1388 1873 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
3d071855 1874 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
1875 if (ret < 0) {
1876 goto error;
1877 }
7972aab2
DG
1878 } else {
1879 goto end;
1880 }
1881
1882 /* Initialize registry. */
1883 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1884 app->bits_per_long, app->uint8_t_alignment,
1885 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
1886 app->uint64_t_alignment, app->long_alignment,
1887 app->byte_order, app->version.major,
3d071855
MD
1888 app->version.minor, reg_pid->root_shm_path,
1889 reg_pid->shm_path,
d7ba1388 1890 ua_sess->euid, ua_sess->egid);
7972aab2 1891 if (ret < 0) {
286c991a
MD
1892 /*
1893 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1894 * destroy the buffer registry, because it is always expected
1895 * that if the buffer registry can be found, its ust registry is
1896 * non-NULL.
1897 */
1898 buffer_reg_pid_destroy(reg_pid);
7972aab2
DG
1899 goto error;
1900 }
1901
286c991a
MD
1902 buffer_reg_pid_add(reg_pid);
1903
7972aab2
DG
1904 DBG3("UST app buffer registry per PID created successfully");
1905
1906end:
1907 if (regp) {
1908 *regp = reg_pid;
1909 }
1910error:
1911 rcu_read_unlock();
1912 return ret;
1913}
1914
1915/*
1916 * Setup buffer registry per UID for the given session and application. If none
1917 * is found, a new one is created, added to the global registry and
1918 * initialized. If regp is valid, it's set with the newly created object.
1919 *
1920 * Return 0 on success or else a negative value.
1921 */
1922static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
d7ba1388 1923 struct ust_app_session *ua_sess,
7972aab2
DG
1924 struct ust_app *app, struct buffer_reg_uid **regp)
1925{
1926 int ret = 0;
1927 struct buffer_reg_uid *reg_uid;
1928
1929 assert(usess);
1930 assert(app);
1931
1932 rcu_read_lock();
1933
1934 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
1935 if (!reg_uid) {
1936 /*
1937 * This is the create channel path meaning that if there is NO
1938 * registry available, we have to create one for this session.
1939 */
1940 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
3d071855
MD
1941 LTTNG_DOMAIN_UST, &reg_uid,
1942 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
1943 if (ret < 0) {
1944 goto error;
1945 }
7972aab2
DG
1946 } else {
1947 goto end;
1948 }
1949
1950 /* Initialize registry. */
af6142cf 1951 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
7972aab2
DG
1952 app->bits_per_long, app->uint8_t_alignment,
1953 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
1954 app->uint64_t_alignment, app->long_alignment,
1955 app->byte_order, app->version.major,
3d071855
MD
1956 app->version.minor, reg_uid->root_shm_path,
1957 reg_uid->shm_path, usess->uid, usess->gid);
7972aab2 1958 if (ret < 0) {
286c991a
MD
1959 /*
1960 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1961 * destroy the buffer registry, because it is always expected
1962 * that if the buffer registry can be found, its ust registry is
1963 * non-NULL.
1964 */
1965 buffer_reg_uid_destroy(reg_uid, NULL);
7972aab2
DG
1966 goto error;
1967 }
1968 /* Add node to teardown list of the session. */
1969 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
1970
286c991a 1971 buffer_reg_uid_add(reg_uid);
7972aab2 1972
286c991a 1973 DBG3("UST app buffer registry per UID created successfully");
7972aab2
DG
1974end:
1975 if (regp) {
1976 *regp = reg_uid;
1977 }
1978error:
1979 rcu_read_unlock();
1980 return ret;
1981}
1982
421cb601 1983/*
3d8ca23b 1984 * Create a session on the tracer side for the given app.
421cb601 1985 *
3d8ca23b
DG
1986 * On success, ua_sess_ptr is populated with the session pointer or else left
1987 * untouched. If the session was created, is_created is set to 1. On error,
1988 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1989 * be NULL.
1990 *
1991 * Returns 0 on success or else a negative code which is either -ENOMEM or
1992 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 1993 */
3d8ca23b
DG
1994static int create_ust_app_session(struct ltt_ust_session *usess,
1995 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1996 int *is_created)
421cb601 1997{
3d8ca23b 1998 int ret, created = 0;
421cb601
DG
1999 struct ust_app_session *ua_sess;
2000
3d8ca23b
DG
2001 assert(usess);
2002 assert(app);
2003 assert(ua_sess_ptr);
2004
840cb59c 2005 health_code_update();
86acf0da 2006
421cb601
DG
2007 ua_sess = lookup_session_by_app(usess, app);
2008 if (ua_sess == NULL) {
d9bf3ca4 2009 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
852d0037 2010 app->pid, usess->id);
d0b96690 2011 ua_sess = alloc_ust_app_session(app);
421cb601
DG
2012 if (ua_sess == NULL) {
2013 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
2014 ret = -ENOMEM;
2015 goto error;
421cb601 2016 }
477d7741 2017 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 2018 created = 1;
421cb601
DG
2019 }
2020
7972aab2
DG
2021 switch (usess->buffer_type) {
2022 case LTTNG_BUFFER_PER_PID:
2023 /* Init local registry. */
2024 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
421cb601 2025 if (ret < 0) {
e64207cf 2026 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2027 goto error;
2028 }
2029 break;
2030 case LTTNG_BUFFER_PER_UID:
2031 /* Look for a global registry. If none exists, create one. */
d7ba1388 2032 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
7972aab2 2033 if (ret < 0) {
e64207cf 2034 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2035 goto error;
2036 }
2037 break;
2038 default:
2039 assert(0);
2040 ret = -EINVAL;
2041 goto error;
2042 }
2043
2044 health_code_update();
2045
2046 if (ua_sess->handle == -1) {
2047 ret = ustctl_create_session(app->sock);
2048 if (ret < 0) {
2049 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2050 ERR("Creating session for app pid %d with ret %d",
ffe60014
DG
2051 app->pid, ret);
2052 } else {
2053 DBG("UST app creating session failed. Application is dead");
3757b385
DG
2054 /*
2055 * This is normal behavior, an application can die during the
2056 * creation process. Don't report an error so the execution can
2057 * continue normally. This will get flagged ENOTCONN and the
2058 * caller will handle it.
2059 */
2060 ret = 0;
ffe60014 2061 }
d0b96690 2062 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
2063 if (ret != -ENOMEM) {
2064 /*
2065 * Tracer is probably gone or got an internal error so let's
2066 * behave like it will soon unregister or not usable.
2067 */
2068 ret = -ENOTCONN;
2069 }
2070 goto error;
421cb601
DG
2071 }
2072
7972aab2
DG
2073 ua_sess->handle = ret;
2074
2075 /* Add ust app session to app's HT */
d9bf3ca4
MD
2076 lttng_ht_node_init_u64(&ua_sess->node,
2077 ua_sess->tracing_id);
2078 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
7972aab2
DG
2079
2080 DBG2("UST app session created successfully with handle %d", ret);
2081 }
2082
2083 *ua_sess_ptr = ua_sess;
2084 if (is_created) {
2085 *is_created = created;
2086 }
2087
2088 /* Everything went well. */
2089 ret = 0;
2090
2091error:
2092 health_code_update();
2093 return ret;
2094}
2095
6a6b2068
JG
2096/*
2097 * Match function for a hash table lookup of ust_app_ctx.
2098 *
2099 * It matches an ust app context based on the context type and, in the case
2100 * of perf counters, their name.
2101 */
2102static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2103{
2104 struct ust_app_ctx *ctx;
2105 const struct lttng_ust_context *key;
2106
2107 assert(node);
2108 assert(_key);
2109
2110 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2111 key = _key;
2112
2113 /* Context type */
2114 if (ctx->ctx.ctx != key->ctx) {
2115 goto no_match;
2116 }
2117
2118 /* Check the name in the case of perf thread counters. */
2119 if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) {
2120 if (strncmp(key->u.perf_counter.name,
2121 ctx->ctx.u.perf_counter.name,
2122 sizeof(key->u.perf_counter.name))) {
2123 goto no_match;
2124 }
2125 }
2126
2127 /* Match. */
2128 return 1;
2129
2130no_match:
2131 return 0;
2132}
2133
2134/*
2135 * Lookup for an ust app context from an lttng_ust_context.
2136 *
be184a0f 2137 * Must be called while holding RCU read side lock.
6a6b2068
JG
2138 * Return an ust_app_ctx object or NULL on error.
2139 */
2140static
2141struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2142 struct lttng_ust_context *uctx)
2143{
2144 struct lttng_ht_iter iter;
2145 struct lttng_ht_node_ulong *node;
2146 struct ust_app_ctx *app_ctx = NULL;
2147
2148 assert(uctx);
2149 assert(ht);
2150
2151 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2152 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2153 ht_match_ust_app_ctx, uctx, &iter.iter);
2154 node = lttng_ht_iter_get_node_ulong(&iter);
2155 if (!node) {
2156 goto end;
2157 }
2158
2159 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2160
2161end:
2162 return app_ctx;
2163}
2164
7972aab2
DG
2165/*
2166 * Create a context for the channel on the tracer.
2167 *
2168 * Called with UST app session lock held and a RCU read side lock.
2169 */
2170static
2171int create_ust_app_channel_context(struct ust_app_session *ua_sess,
2172 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
2173 struct ust_app *app)
2174{
2175 int ret = 0;
7972aab2
DG
2176 struct ust_app_ctx *ua_ctx;
2177
2178 DBG2("UST app adding context to channel %s", ua_chan->name);
2179
6a6b2068
JG
2180 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2181 if (ua_ctx) {
7972aab2
DG
2182 ret = -EEXIST;
2183 goto error;
2184 }
2185
2186 ua_ctx = alloc_ust_app_ctx(uctx);
2187 if (ua_ctx == NULL) {
2188 /* malloc failed */
2189 ret = -1;
2190 goto error;
2191 }
2192
2193 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 2194 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 2195 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
7972aab2
DG
2196
2197 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2198 if (ret < 0) {
2199 goto error;
2200 }
2201
2202error:
2203 return ret;
2204}
2205
2206/*
2207 * Enable on the tracer side a ust app event for the session and channel.
2208 *
2209 * Called with UST app session lock held.
2210 */
2211static
2212int enable_ust_app_event(struct ust_app_session *ua_sess,
2213 struct ust_app_event *ua_event, struct ust_app *app)
2214{
2215 int ret;
2216
2217 ret = enable_ust_event(app, ua_sess, ua_event);
2218 if (ret < 0) {
2219 goto error;
2220 }
2221
2222 ua_event->enabled = 1;
2223
2224error:
2225 return ret;
2226}
2227
2228/*
2229 * Disable on the tracer side a ust app event for the session and channel.
2230 */
2231static int disable_ust_app_event(struct ust_app_session *ua_sess,
2232 struct ust_app_event *ua_event, struct ust_app *app)
2233{
2234 int ret;
2235
2236 ret = disable_ust_event(app, ua_sess, ua_event);
2237 if (ret < 0) {
2238 goto error;
2239 }
2240
2241 ua_event->enabled = 0;
2242
2243error:
2244 return ret;
2245}
2246
2247/*
2248 * Lookup ust app channel for session and disable it on the tracer side.
2249 */
2250static
2251int disable_ust_app_channel(struct ust_app_session *ua_sess,
2252 struct ust_app_channel *ua_chan, struct ust_app *app)
2253{
2254 int ret;
2255
2256 ret = disable_ust_channel(app, ua_sess, ua_chan);
2257 if (ret < 0) {
2258 goto error;
2259 }
2260
2261 ua_chan->enabled = 0;
2262
2263error:
2264 return ret;
2265}
2266
2267/*
2268 * Lookup ust app channel for session and enable it on the tracer side. This
2269 * MUST be called with a RCU read side lock acquired.
2270 */
2271static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2272 struct ltt_ust_channel *uchan, struct ust_app *app)
2273{
2274 int ret = 0;
2275 struct lttng_ht_iter iter;
2276 struct lttng_ht_node_str *ua_chan_node;
2277 struct ust_app_channel *ua_chan;
2278
2279 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2280 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2281 if (ua_chan_node == NULL) {
d9bf3ca4 2282 DBG2("Unable to find channel %s in ust session id %" PRIu64,
7972aab2
DG
2283 uchan->name, ua_sess->tracing_id);
2284 goto error;
2285 }
2286
2287 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2288
2289 ret = enable_ust_channel(app, ua_sess, ua_chan);
2290 if (ret < 0) {
2291 goto error;
2292 }
2293
2294error:
2295 return ret;
2296}
2297
2298/*
2299 * Ask the consumer to create a channel and get it if successful.
2300 *
2301 * Return 0 on success or else a negative value.
2302 */
2303static int do_consumer_create_channel(struct ltt_ust_session *usess,
2304 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2305 int bitness, struct ust_registry_session *registry)
2306{
2307 int ret;
2308 unsigned int nb_fd = 0;
2309 struct consumer_socket *socket;
2310
2311 assert(usess);
2312 assert(ua_sess);
2313 assert(ua_chan);
2314 assert(registry);
2315
2316 rcu_read_lock();
2317 health_code_update();
2318
2319 /* Get the right consumer socket for the application. */
2320 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2321 if (!socket) {
2322 ret = -EINVAL;
2323 goto error;
2324 }
2325
2326 health_code_update();
2327
2328 /* Need one fd for the channel. */
2329 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2330 if (ret < 0) {
2331 ERR("Exhausted number of available FD upon create channel");
2332 goto error;
2333 }
2334
2335 /*
2336 * Ask consumer to create channel. The consumer will return the number of
2337 * stream we have to expect.
2338 */
2339 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2340 registry);
2341 if (ret < 0) {
2342 goto error_ask;
2343 }
2344
2345 /*
2346 * Compute the number of fd needed before receiving them. It must be 2 per
2347 * stream (2 being the default value here).
2348 */
2349 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2350
2351 /* Reserve the amount of file descriptor we need. */
2352 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2353 if (ret < 0) {
2354 ERR("Exhausted number of available FD upon create channel");
2355 goto error_fd_get_stream;
2356 }
2357
2358 health_code_update();
2359
2360 /*
2361 * Now get the channel from the consumer. This call wil populate the stream
2362 * list of that channel and set the ust objects.
2363 */
d9078d0c
DG
2364 if (usess->consumer->enabled) {
2365 ret = ust_consumer_get_channel(socket, ua_chan);
2366 if (ret < 0) {
2367 goto error_destroy;
2368 }
7972aab2
DG
2369 }
2370
2371 rcu_read_unlock();
2372 return 0;
2373
2374error_destroy:
2375 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2376error_fd_get_stream:
2377 /*
2378 * Initiate a destroy channel on the consumer since we had an error
2379 * handling it on our side. The return value is of no importance since we
2380 * already have a ret value set by the previous error that we need to
2381 * return.
2382 */
2383 (void) ust_consumer_destroy_channel(socket, ua_chan);
2384error_ask:
2385 lttng_fd_put(LTTNG_FD_APPS, 1);
2386error:
2387 health_code_update();
2388 rcu_read_unlock();
2389 return ret;
2390}
2391
2392/*
2393 * Duplicate the ust data object of the ust app stream and save it in the
2394 * buffer registry stream.
2395 *
2396 * Return 0 on success or else a negative value.
2397 */
2398static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2399 struct ust_app_stream *stream)
2400{
2401 int ret;
2402
2403 assert(reg_stream);
2404 assert(stream);
2405
2406 /* Reserve the amount of file descriptor we need. */
2407 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2408 if (ret < 0) {
2409 ERR("Exhausted number of available FD upon duplicate stream");
2410 goto error;
2411 }
2412
2413 /* Duplicate object for stream once the original is in the registry. */
2414 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2415 reg_stream->obj.ust);
2416 if (ret < 0) {
2417 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2418 reg_stream->obj.ust, stream->obj, ret);
2419 lttng_fd_put(LTTNG_FD_APPS, 2);
2420 goto error;
2421 }
2422 stream->handle = stream->obj->handle;
2423
2424error:
2425 return ret;
2426}
2427
2428/*
2429 * Duplicate the ust data object of the ust app. channel and save it in the
2430 * buffer registry channel.
2431 *
2432 * Return 0 on success or else a negative value.
2433 */
2434static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2435 struct ust_app_channel *ua_chan)
2436{
2437 int ret;
2438
2439 assert(reg_chan);
2440 assert(ua_chan);
2441
2442 /* Need two fds for the channel. */
2443 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2444 if (ret < 0) {
2445 ERR("Exhausted number of available FD upon duplicate channel");
2446 goto error_fd_get;
2447 }
2448
2449 /* Duplicate object for stream once the original is in the registry. */
2450 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2451 if (ret < 0) {
2452 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2453 reg_chan->obj.ust, ua_chan->obj, ret);
2454 goto error;
2455 }
2456 ua_chan->handle = ua_chan->obj->handle;
2457
2458 return 0;
2459
2460error:
2461 lttng_fd_put(LTTNG_FD_APPS, 1);
2462error_fd_get:
2463 return ret;
2464}
2465
2466/*
2467 * For a given channel buffer registry, setup all streams of the given ust
2468 * application channel.
2469 *
2470 * Return 0 on success or else a negative value.
2471 */
2472static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
2473 struct ust_app_channel *ua_chan)
2474{
2475 int ret = 0;
2476 struct ust_app_stream *stream, *stmp;
2477
2478 assert(reg_chan);
2479 assert(ua_chan);
2480
2481 DBG2("UST app setup buffer registry stream");
2482
2483 /* Send all streams to application. */
2484 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2485 struct buffer_reg_stream *reg_stream;
2486
2487 ret = buffer_reg_stream_create(&reg_stream);
2488 if (ret < 0) {
2489 goto error;
2490 }
2491
2492 /*
2493 * Keep original pointer and nullify it in the stream so the delete
2494 * stream call does not release the object.
2495 */
2496 reg_stream->obj.ust = stream->obj;
2497 stream->obj = NULL;
2498 buffer_reg_stream_add(reg_stream, reg_chan);
421cb601 2499
7972aab2
DG
2500 /* We don't need the streams anymore. */
2501 cds_list_del(&stream->list);
2502 delete_ust_app_stream(-1, stream);
2503 }
421cb601 2504
7972aab2
DG
2505error:
2506 return ret;
2507}
2508
2509/*
2510 * Create a buffer registry channel for the given session registry and
2511 * application channel object. If regp pointer is valid, it's set with the
2512 * created object. Important, the created object is NOT added to the session
2513 * registry hash table.
2514 *
2515 * Return 0 on success else a negative value.
2516 */
2517static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2518 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2519{
2520 int ret;
2521 struct buffer_reg_channel *reg_chan = NULL;
2522
2523 assert(reg_sess);
2524 assert(ua_chan);
2525
2526 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2527
2528 /* Create buffer registry channel. */
2529 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2530 if (ret < 0) {
2531 goto error_create;
421cb601 2532 }
7972aab2
DG
2533 assert(reg_chan);
2534 reg_chan->consumer_key = ua_chan->key;
8c924c7b 2535 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
d07ceecd 2536 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
421cb601 2537
7972aab2
DG
2538 /* Create and add a channel registry to session. */
2539 ret = ust_registry_channel_add(reg_sess->reg.ust,
2540 ua_chan->tracing_channel_id);
2541 if (ret < 0) {
2542 goto error;
d88aee68 2543 }
7972aab2 2544 buffer_reg_channel_add(reg_sess, reg_chan);
d88aee68 2545
7972aab2
DG
2546 if (regp) {
2547 *regp = reg_chan;
3d8ca23b 2548 }
d88aee68 2549
7972aab2 2550 return 0;
3d8ca23b
DG
2551
2552error:
7972aab2
DG
2553 /* Safe because the registry channel object was not added to any HT. */
2554 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2555error_create:
3d8ca23b 2556 return ret;
421cb601
DG
2557}
2558
55cc08a6 2559/*
7972aab2
DG
2560 * Setup buffer registry channel for the given session registry and application
2561 * channel object. If regp pointer is valid, it's set with the created object.
d0b96690 2562 *
7972aab2 2563 * Return 0 on success else a negative value.
55cc08a6 2564 */
7972aab2
DG
2565static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2566 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan)
55cc08a6 2567{
7972aab2 2568 int ret;
55cc08a6 2569
7972aab2
DG
2570 assert(reg_sess);
2571 assert(reg_chan);
2572 assert(ua_chan);
2573 assert(ua_chan->obj);
55cc08a6 2574
7972aab2 2575 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
55cc08a6 2576
7972aab2
DG
2577 /* Setup all streams for the registry. */
2578 ret = setup_buffer_reg_streams(reg_chan, ua_chan);
2579 if (ret < 0) {
55cc08a6
DG
2580 goto error;
2581 }
2582
7972aab2
DG
2583 reg_chan->obj.ust = ua_chan->obj;
2584 ua_chan->obj = NULL;
55cc08a6 2585
7972aab2 2586 return 0;
55cc08a6
DG
2587
2588error:
7972aab2
DG
2589 buffer_reg_channel_remove(reg_sess, reg_chan);
2590 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
55cc08a6
DG
2591 return ret;
2592}
2593
edb67388 2594/*
7972aab2 2595 * Send buffer registry channel to the application.
d0b96690 2596 *
7972aab2 2597 * Return 0 on success else a negative value.
edb67388 2598 */
7972aab2
DG
2599static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2600 struct ust_app *app, struct ust_app_session *ua_sess,
2601 struct ust_app_channel *ua_chan)
edb67388
DG
2602{
2603 int ret;
7972aab2 2604 struct buffer_reg_stream *reg_stream;
edb67388 2605
7972aab2
DG
2606 assert(reg_chan);
2607 assert(app);
2608 assert(ua_sess);
2609 assert(ua_chan);
2610
2611 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2612
2613 ret = duplicate_channel_object(reg_chan, ua_chan);
edb67388
DG
2614 if (ret < 0) {
2615 goto error;
2616 }
2617
7972aab2
DG
2618 /* Send channel to the application. */
2619 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
2620 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2621 ret = -ENOTCONN; /* Caused by app exiting. */
2622 goto error;
2623 } else if (ret < 0) {
7972aab2
DG
2624 goto error;
2625 }
2626
2627 health_code_update();
2628
2629 /* Send all streams to application. */
2630 pthread_mutex_lock(&reg_chan->stream_list_lock);
2631 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2632 struct ust_app_stream stream;
2633
2634 ret = duplicate_stream_object(reg_stream, &stream);
2635 if (ret < 0) {
2636 goto error_stream_unlock;
2637 }
2638
2639 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2640 if (ret < 0) {
8c067051 2641 (void) release_ust_app_stream(-1, &stream);
a7169585
MD
2642 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2643 ret = -ENOTCONN; /* Caused by app exiting. */
2644 goto error_stream_unlock;
2645 } else if (ret < 0) {
2646 goto error_stream_unlock;
2647 }
7972aab2
DG
2648 goto error_stream_unlock;
2649 }
edb67388 2650
7972aab2
DG
2651 /*
2652 * The return value is not important here. This function will output an
2653 * error if needed.
2654 */
2655 (void) release_ust_app_stream(-1, &stream);
2656 }
2657 ua_chan->is_sent = 1;
2658
2659error_stream_unlock:
2660 pthread_mutex_unlock(&reg_chan->stream_list_lock);
edb67388
DG
2661error:
2662 return ret;
2663}
2664
9730260e 2665/*
7972aab2
DG
2666 * Create and send to the application the created buffers with per UID buffers.
2667 *
2668 * Return 0 on success else a negative value.
9730260e 2669 */
7972aab2
DG
2670static int create_channel_per_uid(struct ust_app *app,
2671 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2672 struct ust_app_channel *ua_chan)
9730260e
DG
2673{
2674 int ret;
7972aab2
DG
2675 struct buffer_reg_uid *reg_uid;
2676 struct buffer_reg_channel *reg_chan;
9730260e 2677
7972aab2
DG
2678 assert(app);
2679 assert(usess);
2680 assert(ua_sess);
2681 assert(ua_chan);
2682
2683 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2684
2685 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2686 /*
2687 * The session creation handles the creation of this global registry
2688 * object. If none can be find, there is a code flow problem or a
2689 * teardown race.
2690 */
2691 assert(reg_uid);
2692
2693 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2694 reg_uid);
2695 if (!reg_chan) {
2696 /* Create the buffer registry channel object. */
2697 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2698 if (ret < 0) {
f14256d6
MD
2699 ERR("Error creating the UST channel \"%s\" registry instance",
2700 ua_chan->name);
7972aab2
DG
2701 goto error;
2702 }
2703 assert(reg_chan);
2704
2705 /*
2706 * Create the buffers on the consumer side. This call populates the
2707 * ust app channel object with all streams and data object.
2708 */
2709 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2710 app->bits_per_long, reg_uid->registry->reg.ust);
2711 if (ret < 0) {
f14256d6
MD
2712 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2713 ua_chan->name);
2714
07d2ae95
DG
2715 /*
2716 * Let's remove the previously created buffer registry channel so
2717 * it's not visible anymore in the session registry.
2718 */
2719 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2720 ua_chan->tracing_channel_id);
2721 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2722 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
7972aab2
DG
2723 goto error;
2724 }
2725
2726 /*
2727 * Setup the streams and add it to the session registry.
2728 */
2729 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan);
2730 if (ret < 0) {
f14256d6
MD
2731 ERR("Error setting up UST channel \"%s\"",
2732 ua_chan->name);
7972aab2
DG
2733 goto error;
2734 }
2735
2736 }
2737
2738 /* Send buffers to the application. */
2739 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
9730260e 2740 if (ret < 0) {
a7169585
MD
2741 if (ret != -ENOTCONN) {
2742 ERR("Error sending channel to application");
2743 }
9730260e
DG
2744 goto error;
2745 }
2746
9730260e
DG
2747error:
2748 return ret;
2749}
2750
78f0bacd 2751/*
7972aab2
DG
2752 * Create and send to the application the created buffers with per PID buffers.
2753 *
2754 * Return 0 on success else a negative value.
78f0bacd 2755 */
7972aab2
DG
2756static int create_channel_per_pid(struct ust_app *app,
2757 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2758 struct ust_app_channel *ua_chan)
78f0bacd 2759{
8535a6d9 2760 int ret;
7972aab2 2761 struct ust_registry_session *registry;
78f0bacd 2762
7972aab2
DG
2763 assert(app);
2764 assert(usess);
2765 assert(ua_sess);
2766 assert(ua_chan);
2767
2768 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2769
2770 rcu_read_lock();
2771
2772 registry = get_session_registry(ua_sess);
2773 assert(registry);
2774
2775 /* Create and add a new channel registry to session. */
2776 ret = ust_registry_channel_add(registry, ua_chan->key);
78f0bacd 2777 if (ret < 0) {
f14256d6
MD
2778 ERR("Error creating the UST channel \"%s\" registry instance",
2779 ua_chan->name);
78f0bacd
DG
2780 goto error;
2781 }
2782
7972aab2
DG
2783 /* Create and get channel on the consumer side. */
2784 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2785 app->bits_per_long, registry);
2786 if (ret < 0) {
f14256d6
MD
2787 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2788 ua_chan->name);
7972aab2
DG
2789 goto error;
2790 }
2791
2792 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2793 if (ret < 0) {
a7169585
MD
2794 if (ret != -ENOTCONN) {
2795 ERR("Error sending channel to application");
2796 }
7972aab2
DG
2797 goto error;
2798 }
8535a6d9 2799
78f0bacd 2800error:
7972aab2 2801 rcu_read_unlock();
78f0bacd
DG
2802 return ret;
2803}
2804
2805/*
7972aab2
DG
2806 * From an already allocated ust app channel, create the channel buffers if
2807 * need and send it to the application. This MUST be called with a RCU read
2808 * side lock acquired.
2809 *
a7169585
MD
2810 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2811 * the application exited concurrently.
78f0bacd 2812 */
7972aab2
DG
2813static int do_create_channel(struct ust_app *app,
2814 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2815 struct ust_app_channel *ua_chan)
78f0bacd 2816{
7972aab2 2817 int ret;
78f0bacd 2818
7972aab2
DG
2819 assert(app);
2820 assert(usess);
2821 assert(ua_sess);
2822 assert(ua_chan);
2823
2824 /* Handle buffer type before sending the channel to the application. */
2825 switch (usess->buffer_type) {
2826 case LTTNG_BUFFER_PER_UID:
2827 {
2828 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
2829 if (ret < 0) {
2830 goto error;
2831 }
2832 break;
2833 }
2834 case LTTNG_BUFFER_PER_PID:
2835 {
2836 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
2837 if (ret < 0) {
2838 goto error;
2839 }
2840 break;
2841 }
2842 default:
2843 assert(0);
2844 ret = -EINVAL;
78f0bacd
DG
2845 goto error;
2846 }
2847
7972aab2
DG
2848 /* Initialize ust objd object using the received handle and add it. */
2849 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
2850 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
78f0bacd 2851
7972aab2
DG
2852 /* If channel is not enabled, disable it on the tracer */
2853 if (!ua_chan->enabled) {
2854 ret = disable_ust_channel(app, ua_sess, ua_chan);
2855 if (ret < 0) {
2856 goto error;
2857 }
78f0bacd
DG
2858 }
2859
2860error:
2861 return ret;
2862}
2863
284d8f55 2864/*
4d710ac2
DG
2865 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2866 * newly created channel if not NULL.
d0b96690 2867 *
36b588ed 2868 * Called with UST app session lock and RCU read-side lock held.
7972aab2 2869 *
a7169585
MD
2870 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2871 * the application exited concurrently.
284d8f55 2872 */
4d710ac2
DG
2873static int create_ust_app_channel(struct ust_app_session *ua_sess,
2874 struct ltt_ust_channel *uchan, struct ust_app *app,
7972aab2 2875 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
4d710ac2 2876 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
2877{
2878 int ret = 0;
bec39940
DG
2879 struct lttng_ht_iter iter;
2880 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
2881 struct ust_app_channel *ua_chan;
2882
2883 /* Lookup channel in the ust app session */
bec39940
DG
2884 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2885 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 2886 if (ua_chan_node != NULL) {
5b4a0ec0 2887 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 2888 goto end;
5b4a0ec0
DG
2889 }
2890
d0b96690 2891 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
2892 if (ua_chan == NULL) {
2893 /* Only malloc can fail here */
4d710ac2 2894 ret = -ENOMEM;
094d1690 2895 goto error_alloc;
fc34caaa
DG
2896 }
2897 shadow_copy_channel(ua_chan, uchan);
2898
ffe60014
DG
2899 /* Set channel type. */
2900 ua_chan->attr.type = type;
2901
7972aab2 2902 ret = do_create_channel(app, usess, ua_sess, ua_chan);
5b4a0ec0
DG
2903 if (ret < 0) {
2904 goto error;
2905 }
2906
fc34caaa 2907 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
852d0037 2908 app->pid);
fc34caaa 2909
d0b96690
DG
2910 /* Only add the channel if successful on the tracer side. */
2911 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
2912
fc34caaa 2913end:
4d710ac2
DG
2914 if (ua_chanp) {
2915 *ua_chanp = ua_chan;
2916 }
2917
2918 /* Everything went well. */
2919 return 0;
5b4a0ec0
DG
2920
2921error:
d0b96690 2922 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
094d1690 2923error_alloc:
4d710ac2 2924 return ret;
5b4a0ec0
DG
2925}
2926
2927/*
2928 * Create UST app event and create it on the tracer side.
d0b96690
DG
2929 *
2930 * Called with ust app session mutex held.
5b4a0ec0 2931 */
edb67388
DG
2932static
2933int create_ust_app_event(struct ust_app_session *ua_sess,
2934 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
2935 struct ust_app *app)
284d8f55 2936{
edb67388 2937 int ret = 0;
5b4a0ec0 2938 struct ust_app_event *ua_event;
284d8f55 2939
5b4a0ec0 2940 /* Get event node */
18eace3b 2941 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 2942 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 2943 if (ua_event != NULL) {
fc34caaa 2944 ret = -EEXIST;
edb67388
DG
2945 goto end;
2946 }
5b4a0ec0 2947
edb67388
DG
2948 /* Does not exist so create one */
2949 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
2950 if (ua_event == NULL) {
2951 /* Only malloc can failed so something is really wrong */
2952 ret = -ENOMEM;
fc34caaa 2953 goto end;
5b4a0ec0 2954 }
edb67388 2955 shadow_copy_event(ua_event, uevent);
5b4a0ec0 2956
edb67388 2957 /* Create it on the tracer side */
5b4a0ec0 2958 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 2959 if (ret < 0) {
fc34caaa 2960 /* Not found previously means that it does not exist on the tracer */
76f66f63 2961 assert(ret != -LTTNG_UST_ERR_EXIST);
284d8f55
DG
2962 goto error;
2963 }
2964
d0b96690 2965 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 2966
fc34caaa 2967 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 2968 app->pid);
7f79d3a1 2969
edb67388 2970end:
fc34caaa
DG
2971 return ret;
2972
5b4a0ec0 2973error:
fc34caaa
DG
2974 /* Valid. Calling here is already in a read side lock */
2975 delete_ust_app_event(-1, ua_event);
edb67388 2976 return ret;
5b4a0ec0
DG
2977}
2978
2979/*
2980 * Create UST metadata and open it on the tracer side.
d0b96690 2981 *
7972aab2 2982 * Called with UST app session lock held and RCU read side lock.
5b4a0ec0
DG
2983 */
2984static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ad7a9107 2985 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
2986{
2987 int ret = 0;
ffe60014 2988 struct ust_app_channel *metadata;
d88aee68 2989 struct consumer_socket *socket;
7972aab2 2990 struct ust_registry_session *registry;
5b4a0ec0 2991
ffe60014
DG
2992 assert(ua_sess);
2993 assert(app);
d88aee68 2994 assert(consumer);
5b4a0ec0 2995
7972aab2
DG
2996 registry = get_session_registry(ua_sess);
2997 assert(registry);
2998
ce34fcd0
MD
2999 pthread_mutex_lock(&registry->lock);
3000
1b532a60
DG
3001 /* Metadata already exists for this registry or it was closed previously */
3002 if (registry->metadata_key || registry->metadata_closed) {
7972aab2
DG
3003 ret = 0;
3004 goto error;
5b4a0ec0
DG
3005 }
3006
ffe60014 3007 /* Allocate UST metadata */
d0b96690 3008 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
3009 if (!metadata) {
3010 /* malloc() failed */
3011 ret = -ENOMEM;
3012 goto error;
3013 }
5b4a0ec0 3014
ad7a9107 3015 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
5b4a0ec0 3016
7972aab2
DG
3017 /* Need one fd for the channel. */
3018 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3019 if (ret < 0) {
3020 ERR("Exhausted number of available FD upon create metadata");
3021 goto error;
3022 }
3023
4dc3dfc5
DG
3024 /* Get the right consumer socket for the application. */
3025 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3026 if (!socket) {
3027 ret = -EINVAL;
3028 goto error_consumer;
3029 }
3030
331744e3
JD
3031 /*
3032 * Keep metadata key so we can identify it on the consumer side. Assign it
3033 * to the registry *before* we ask the consumer so we avoid the race of the
3034 * consumer requesting the metadata and the ask_channel call on our side
3035 * did not returned yet.
3036 */
3037 registry->metadata_key = metadata->key;
3038
d88aee68
DG
3039 /*
3040 * Ask the metadata channel creation to the consumer. The metadata object
3041 * will be created by the consumer and kept their. However, the stream is
3042 * never added or monitored until we do a first push metadata to the
3043 * consumer.
3044 */
7972aab2
DG
3045 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
3046 registry);
d88aee68 3047 if (ret < 0) {
f2a444f1
DG
3048 /* Nullify the metadata key so we don't try to close it later on. */
3049 registry->metadata_key = 0;
d88aee68
DG
3050 goto error_consumer;
3051 }
3052
3053 /*
3054 * The setup command will make the metadata stream be sent to the relayd,
3055 * if applicable, and the thread managing the metadatas. This is important
3056 * because after this point, if an error occurs, the only way the stream
3057 * can be deleted is to be monitored in the consumer.
3058 */
7972aab2 3059 ret = consumer_setup_metadata(socket, metadata->key);
ffe60014 3060 if (ret < 0) {
f2a444f1
DG
3061 /* Nullify the metadata key so we don't try to close it later on. */
3062 registry->metadata_key = 0;
d88aee68 3063 goto error_consumer;
5b4a0ec0
DG
3064 }
3065
7972aab2
DG
3066 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3067 metadata->key, app->pid);
5b4a0ec0 3068
d88aee68 3069error_consumer:
b80f0b6c 3070 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 3071 delete_ust_app_channel(-1, metadata, app);
5b4a0ec0 3072error:
ce34fcd0 3073 pthread_mutex_unlock(&registry->lock);
ffe60014 3074 return ret;
5b4a0ec0
DG
3075}
3076
5b4a0ec0 3077/*
d88aee68
DG
3078 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3079 * acquired before calling this function.
5b4a0ec0
DG
3080 */
3081struct ust_app *ust_app_find_by_pid(pid_t pid)
3082{
d88aee68 3083 struct ust_app *app = NULL;
bec39940
DG
3084 struct lttng_ht_node_ulong *node;
3085 struct lttng_ht_iter iter;
5b4a0ec0 3086
bec39940
DG
3087 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3088 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
3089 if (node == NULL) {
3090 DBG2("UST app no found with pid %d", pid);
3091 goto error;
3092 }
5b4a0ec0
DG
3093
3094 DBG2("Found UST app by pid %d", pid);
3095
d88aee68 3096 app = caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
3097
3098error:
d88aee68 3099 return app;
5b4a0ec0
DG
3100}
3101
d88aee68
DG
3102/*
3103 * Allocate and init an UST app object using the registration information and
3104 * the command socket. This is called when the command socket connects to the
3105 * session daemon.
3106 *
3107 * The object is returned on success or else NULL.
3108 */
d0b96690 3109struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
5b4a0ec0 3110{
d0b96690
DG
3111 struct ust_app *lta = NULL;
3112
3113 assert(msg);
3114 assert(sock >= 0);
3115
3116 DBG3("UST app creating application for socket %d", sock);
5b4a0ec0 3117
173af62f
DG
3118 if ((msg->bits_per_long == 64 &&
3119 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3120 || (msg->bits_per_long == 32 &&
3121 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 3122 ERR("Registration failed: application \"%s\" (pid: %d) has "
d0b96690
DG
3123 "%d-bit long, but no consumerd for this size is available.\n",
3124 msg->name, msg->pid, msg->bits_per_long);
3125 goto error;
3f2c5fcc 3126 }
d0b96690 3127
5b4a0ec0
DG
3128 lta = zmalloc(sizeof(struct ust_app));
3129 if (lta == NULL) {
3130 PERROR("malloc");
d0b96690 3131 goto error;
5b4a0ec0
DG
3132 }
3133
3134 lta->ppid = msg->ppid;
3135 lta->uid = msg->uid;
3136 lta->gid = msg->gid;
d0b96690 3137
7753dea8 3138 lta->bits_per_long = msg->bits_per_long;
d0b96690
DG
3139 lta->uint8_t_alignment = msg->uint8_t_alignment;
3140 lta->uint16_t_alignment = msg->uint16_t_alignment;
3141 lta->uint32_t_alignment = msg->uint32_t_alignment;
3142 lta->uint64_t_alignment = msg->uint64_t_alignment;
3143 lta->long_alignment = msg->long_alignment;
3144 lta->byte_order = msg->byte_order;
3145
5b4a0ec0
DG
3146 lta->v_major = msg->major;
3147 lta->v_minor = msg->minor;
d9bf3ca4 3148 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d0b96690
DG
3149 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3150 lta->notify_sock = -1;
d88aee68
DG
3151
3152 /* Copy name and make sure it's NULL terminated. */
3153 strncpy(lta->name, msg->name, sizeof(lta->name));
3154 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3155
3156 /*
3157 * Before this can be called, when receiving the registration information,
3158 * the application compatibility is checked. So, at this point, the
3159 * application can work with this session daemon.
3160 */
d0b96690 3161 lta->compatible = 1;
5b4a0ec0 3162
852d0037 3163 lta->pid = msg->pid;
d0b96690 3164 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
852d0037 3165 lta->sock = sock;
d0b96690 3166 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
5b4a0ec0 3167
d42f20df
DG
3168 CDS_INIT_LIST_HEAD(&lta->teardown_head);
3169
d0b96690
DG
3170error:
3171 return lta;
3172}
3173
d88aee68
DG
3174/*
3175 * For a given application object, add it to every hash table.
3176 */
d0b96690
DG
3177void ust_app_add(struct ust_app *app)
3178{
3179 assert(app);
3180 assert(app->notify_sock >= 0);
3181
5b4a0ec0 3182 rcu_read_lock();
852d0037
DG
3183
3184 /*
3185 * On a re-registration, we want to kick out the previous registration of
3186 * that pid
3187 */
d0b96690 3188 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
852d0037
DG
3189
3190 /*
3191 * The socket _should_ be unique until _we_ call close. So, a add_unique
3192 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3193 * already in the table.
3194 */
d0b96690 3195 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
852d0037 3196
d0b96690
DG
3197 /* Add application to the notify socket hash table. */
3198 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3199 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
5b4a0ec0 3200
d0b96690 3201 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
d88aee68
DG
3202 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3203 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3204 app->v_minor);
5b4a0ec0 3205
d0b96690
DG
3206 rcu_read_unlock();
3207}
3208
d88aee68
DG
3209/*
3210 * Set the application version into the object.
3211 *
3212 * Return 0 on success else a negative value either an errno code or a
3213 * LTTng-UST error code.
3214 */
d0b96690
DG
3215int ust_app_version(struct ust_app *app)
3216{
d88aee68
DG
3217 int ret;
3218
d0b96690 3219 assert(app);
d88aee68
DG
3220
3221 ret = ustctl_tracer_version(app->sock, &app->version);
3222 if (ret < 0) {
3223 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
5368d366 3224 ERR("UST app %d version failed with ret %d", app->sock, ret);
d88aee68 3225 } else {
5368d366 3226 DBG3("UST app %d version failed. Application is dead", app->sock);
d88aee68
DG
3227 }
3228 }
3229
3230 return ret;
5b4a0ec0
DG
3231}
3232
3233/*
3234 * Unregister app by removing it from the global traceable app list and freeing
3235 * the data struct.
3236 *
3237 * The socket is already closed at this point so no close to sock.
3238 */
3239void ust_app_unregister(int sock)
3240{
3241 struct ust_app *lta;
bec39940 3242 struct lttng_ht_node_ulong *node;
c4b88406 3243 struct lttng_ht_iter ust_app_sock_iter;
bec39940 3244 struct lttng_ht_iter iter;
d42f20df 3245 struct ust_app_session *ua_sess;
525b0740 3246 int ret;
5b4a0ec0
DG
3247
3248 rcu_read_lock();
886459c6 3249
5b4a0ec0 3250 /* Get the node reference for a call_rcu */
c4b88406
MD
3251 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3252 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
d0b96690 3253 assert(node);
284d8f55 3254
852d0037 3255 lta = caa_container_of(node, struct ust_app, sock_n);
852d0037
DG
3256 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3257
d88aee68 3258 /*
ce34fcd0
MD
3259 * For per-PID buffers, perform "push metadata" and flush all
3260 * application streams before removing app from hash tables,
3261 * ensuring proper behavior of data_pending check.
c4b88406 3262 * Remove sessions so they are not visible during deletion.
d88aee68 3263 */
d42f20df
DG
3264 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3265 node.node) {
7972aab2
DG
3266 struct ust_registry_session *registry;
3267