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