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