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