Fix: missing RCU read side critical sections
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <inttypes.h>
13 #include <pthread.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <urcu/compiler.h>
22 #include <signal.h>
23
24 #include <common/bytecode/bytecode.h>
25 #include <common/compat/errno.h>
26 #include <common/common.h>
27 #include <common/hashtable/utils.h>
28 #include <lttng/event-rule/event-rule.h>
29 #include <lttng/event-rule/event-rule-internal.h>
30 #include <lttng/event-rule/user-tracepoint.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/condition/event-rule-matches-internal.h>
33 #include <lttng/condition/event-rule-matches.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36
37 #include "buffer-registry.h"
38 #include "condition-internal.h"
39 #include "fd-limit.h"
40 #include "health-sessiond.h"
41 #include "ust-app.h"
42 #include "ust-consumer.h"
43 #include "lttng-ust-ctl.h"
44 #include "lttng-ust-error.h"
45 #include "utils.h"
46 #include "session.h"
47 #include "lttng-sessiond.h"
48 #include "notification-thread-commands.h"
49 #include "rotate.h"
50 #include "event.h"
51 #include "event-notifier-error-accounting.h"
52 #include "ust-field-utils.h"
53
54 struct lttng_ht *ust_app_ht;
55 struct lttng_ht *ust_app_ht_by_sock;
56 struct lttng_ht *ust_app_ht_by_notify_sock;
57
58 static
59 int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
60
61 /* Next available channel key. Access under next_channel_key_lock. */
62 static uint64_t _next_channel_key;
63 static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
64
65 /* Next available session ID. Access under next_session_id_lock. */
66 static uint64_t _next_session_id;
67 static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
68
69 /*
70 * Return the incremented value of next_channel_key.
71 */
72 static uint64_t get_next_channel_key(void)
73 {
74 uint64_t ret;
75
76 pthread_mutex_lock(&next_channel_key_lock);
77 ret = ++_next_channel_key;
78 pthread_mutex_unlock(&next_channel_key_lock);
79 return ret;
80 }
81
82 /*
83 * Return the atomically incremented value of next_session_id.
84 */
85 static uint64_t get_next_session_id(void)
86 {
87 uint64_t ret;
88
89 pthread_mutex_lock(&next_session_id_lock);
90 ret = ++_next_session_id;
91 pthread_mutex_unlock(&next_session_id_lock);
92 return ret;
93 }
94
95 static void copy_channel_attr_to_ustctl(
96 struct lttng_ust_ctl_consumer_channel_attr *attr,
97 struct lttng_ust_abi_channel_attr *uattr)
98 {
99 /* Copy event attributes since the layout is different. */
100 attr->subbuf_size = uattr->subbuf_size;
101 attr->num_subbuf = uattr->num_subbuf;
102 attr->overwrite = uattr->overwrite;
103 attr->switch_timer_interval = uattr->switch_timer_interval;
104 attr->read_timer_interval = uattr->read_timer_interval;
105 attr->output = (lttng_ust_abi_output) uattr->output;
106 attr->blocking_timeout = uattr->u.s.blocking_timeout;
107 }
108
109 /*
110 * Match function for the hash table lookup.
111 *
112 * It matches an ust app event based on three attributes which are the event
113 * name, the filter bytecode and the loglevel.
114 */
115 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
116 {
117 struct ust_app_event *event;
118 const struct ust_app_ht_key *key;
119 int ev_loglevel_value;
120
121 LTTNG_ASSERT(node);
122 LTTNG_ASSERT(_key);
123
124 event = caa_container_of(node, struct ust_app_event, node.node);
125 key = (ust_app_ht_key *) _key;
126 ev_loglevel_value = event->attr.loglevel;
127
128 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
129
130 /* Event name */
131 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
132 goto no_match;
133 }
134
135 /* Event loglevel. */
136 if (ev_loglevel_value != key->loglevel_type) {
137 if (event->attr.loglevel_type == LTTNG_UST_ABI_LOGLEVEL_ALL
138 && key->loglevel_type == 0 &&
139 ev_loglevel_value == -1) {
140 /*
141 * Match is accepted. This is because on event creation, the
142 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
143 * -1 are accepted for this loglevel type since 0 is the one set by
144 * the API when receiving an enable event.
145 */
146 } else {
147 goto no_match;
148 }
149 }
150
151 /* One of the filters is NULL, fail. */
152 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
153 goto no_match;
154 }
155
156 if (key->filter && event->filter) {
157 /* Both filters exists, check length followed by the bytecode. */
158 if (event->filter->len != key->filter->len ||
159 memcmp(event->filter->data, key->filter->data,
160 event->filter->len) != 0) {
161 goto no_match;
162 }
163 }
164
165 /* One of the exclusions is NULL, fail. */
166 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
167 goto no_match;
168 }
169
170 if (key->exclusion && event->exclusion) {
171 /* Both exclusions exists, check count followed by the names. */
172 if (event->exclusion->count != key->exclusion->count ||
173 memcmp(event->exclusion->names, key->exclusion->names,
174 event->exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) != 0) {
175 goto no_match;
176 }
177 }
178
179
180 /* Match. */
181 return 1;
182
183 no_match:
184 return 0;
185 }
186
187 /*
188 * Unique add of an ust app event in the given ht. This uses the custom
189 * ht_match_ust_app_event match function and the event name as hash.
190 */
191 static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
192 struct ust_app_event *event)
193 {
194 struct cds_lfht_node *node_ptr;
195 struct ust_app_ht_key key;
196 struct lttng_ht *ht;
197
198 LTTNG_ASSERT(ua_chan);
199 LTTNG_ASSERT(ua_chan->events);
200 LTTNG_ASSERT(event);
201
202 ht = ua_chan->events;
203 key.name = event->attr.name;
204 key.filter = event->filter;
205 key.loglevel_type = (lttng_ust_abi_loglevel_type) event->attr.loglevel;
206 key.exclusion = event->exclusion;
207
208 node_ptr = cds_lfht_add_unique(ht->ht,
209 ht->hash_fct(event->node.key, lttng_ht_seed),
210 ht_match_ust_app_event, &key, &event->node.node);
211 LTTNG_ASSERT(node_ptr == &event->node.node);
212 }
213
214 /*
215 * Close the notify socket from the given RCU head object. This MUST be called
216 * through a call_rcu().
217 */
218 static void close_notify_sock_rcu(struct rcu_head *head)
219 {
220 int ret;
221 struct ust_app_notify_sock_obj *obj =
222 caa_container_of(head, struct ust_app_notify_sock_obj, head);
223
224 /* Must have a valid fd here. */
225 LTTNG_ASSERT(obj->fd >= 0);
226
227 ret = close(obj->fd);
228 if (ret) {
229 ERR("close notify sock %d RCU", obj->fd);
230 }
231 lttng_fd_put(LTTNG_FD_APPS, 1);
232
233 free(obj);
234 }
235
236 /*
237 * Return the session registry according to the buffer type of the given
238 * session.
239 *
240 * A registry per UID object MUST exists before calling this function or else
241 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
242 */
243 static struct ust_registry_session *get_session_registry(
244 struct ust_app_session *ua_sess)
245 {
246 struct ust_registry_session *registry = NULL;
247
248 LTTNG_ASSERT(ua_sess);
249
250 switch (ua_sess->buffer_type) {
251 case LTTNG_BUFFER_PER_PID:
252 {
253 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
254 if (!reg_pid) {
255 goto error;
256 }
257 registry = reg_pid->registry->reg.ust;
258 break;
259 }
260 case LTTNG_BUFFER_PER_UID:
261 {
262 struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
263 ua_sess->tracing_id, ua_sess->bits_per_long,
264 lttng_credentials_get_uid(&ua_sess->real_credentials));
265 if (!reg_uid) {
266 goto error;
267 }
268 registry = reg_uid->registry->reg.ust;
269 break;
270 }
271 default:
272 abort();
273 };
274
275 error:
276 return registry;
277 }
278
279 /*
280 * Delete ust context safely. RCU read lock must be held before calling
281 * this function.
282 */
283 static
284 void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx,
285 struct ust_app *app)
286 {
287 int ret;
288
289 LTTNG_ASSERT(ua_ctx);
290 ASSERT_RCU_READ_LOCKED();
291
292 if (ua_ctx->obj) {
293 pthread_mutex_lock(&app->sock_lock);
294 ret = lttng_ust_ctl_release_object(sock, ua_ctx->obj);
295 pthread_mutex_unlock(&app->sock_lock);
296 if (ret < 0) {
297 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
298 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
299 app->pid, app->sock);
300 } else if (ret == -EAGAIN) {
301 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
302 app->pid, app->sock);
303 } else {
304 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
305 ua_ctx->obj->handle, ret,
306 app->pid, app->sock);
307 }
308 }
309 free(ua_ctx->obj);
310 }
311 free(ua_ctx);
312 }
313
314 /*
315 * Delete ust app event safely. RCU read lock must be held before calling
316 * this function.
317 */
318 static
319 void delete_ust_app_event(int sock, struct ust_app_event *ua_event,
320 struct ust_app *app)
321 {
322 int ret;
323
324 LTTNG_ASSERT(ua_event);
325 ASSERT_RCU_READ_LOCKED();
326
327 free(ua_event->filter);
328 if (ua_event->exclusion != NULL)
329 free(ua_event->exclusion);
330 if (ua_event->obj != NULL) {
331 pthread_mutex_lock(&app->sock_lock);
332 ret = lttng_ust_ctl_release_object(sock, ua_event->obj);
333 pthread_mutex_unlock(&app->sock_lock);
334 if (ret < 0) {
335 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
336 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
337 app->pid, app->sock);
338 } else if (ret == -EAGAIN) {
339 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
340 app->pid, app->sock);
341 } else {
342 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
343 ret, app->pid, app->sock);
344 }
345 }
346 free(ua_event->obj);
347 }
348 free(ua_event);
349 }
350
351 /*
352 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
353 * through a call_rcu().
354 */
355 static
356 void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head)
357 {
358 struct ust_app_event_notifier_rule *obj = caa_container_of(
359 head, struct ust_app_event_notifier_rule, rcu_head);
360
361 free(obj);
362 }
363
364 /*
365 * Delete ust app event notifier rule safely.
366 */
367 static void delete_ust_app_event_notifier_rule(int sock,
368 struct ust_app_event_notifier_rule *ua_event_notifier_rule,
369 struct ust_app *app)
370 {
371 int ret;
372
373 LTTNG_ASSERT(ua_event_notifier_rule);
374
375 if (ua_event_notifier_rule->exclusion != NULL) {
376 free(ua_event_notifier_rule->exclusion);
377 }
378
379 if (ua_event_notifier_rule->obj != NULL) {
380 pthread_mutex_lock(&app->sock_lock);
381 ret = lttng_ust_ctl_release_object(sock, ua_event_notifier_rule->obj);
382 pthread_mutex_unlock(&app->sock_lock);
383 if (ret < 0) {
384 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
385 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
386 app->pid, app->sock);
387 } else if (ret == -EAGAIN) {
388 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
389 app->pid, app->sock);
390 } else {
391 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
392 ret, app->pid, app->sock);
393 }
394 }
395
396 free(ua_event_notifier_rule->obj);
397 }
398
399 lttng_trigger_put(ua_event_notifier_rule->trigger);
400 call_rcu(&ua_event_notifier_rule->rcu_head,
401 free_ust_app_event_notifier_rule_rcu);
402 }
403
404 /*
405 * Release ust data object of the given stream.
406 *
407 * Return 0 on success or else a negative value.
408 */
409 static int release_ust_app_stream(int sock, struct ust_app_stream *stream,
410 struct ust_app *app)
411 {
412 int ret = 0;
413
414 LTTNG_ASSERT(stream);
415
416 if (stream->obj) {
417 pthread_mutex_lock(&app->sock_lock);
418 ret = lttng_ust_ctl_release_object(sock, stream->obj);
419 pthread_mutex_unlock(&app->sock_lock);
420 if (ret < 0) {
421 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
422 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
423 app->pid, app->sock);
424 } else if (ret == -EAGAIN) {
425 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
426 app->pid, app->sock);
427 } else {
428 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
429 ret, app->pid, app->sock);
430 }
431 }
432 lttng_fd_put(LTTNG_FD_APPS, 2);
433 free(stream->obj);
434 }
435
436 return ret;
437 }
438
439 /*
440 * Delete ust app stream safely. RCU read lock must be held before calling
441 * this function.
442 */
443 static
444 void delete_ust_app_stream(int sock, struct ust_app_stream *stream,
445 struct ust_app *app)
446 {
447 LTTNG_ASSERT(stream);
448 ASSERT_RCU_READ_LOCKED();
449
450 (void) release_ust_app_stream(sock, stream, app);
451 free(stream);
452 }
453
454 static
455 void delete_ust_app_channel_rcu(struct rcu_head *head)
456 {
457 struct ust_app_channel *ua_chan =
458 caa_container_of(head, struct ust_app_channel, rcu_head);
459
460 lttng_ht_destroy(ua_chan->ctx);
461 lttng_ht_destroy(ua_chan->events);
462 free(ua_chan);
463 }
464
465 /*
466 * Extract the lost packet or discarded events counter when the channel is
467 * being deleted and store the value in the parent channel so we can
468 * access it from lttng list and at stop/destroy.
469 *
470 * The session list lock must be held by the caller.
471 */
472 static
473 void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
474 {
475 uint64_t discarded = 0, lost = 0;
476 struct ltt_session *session;
477 struct ltt_ust_channel *uchan;
478
479 if (ua_chan->attr.type != LTTNG_UST_ABI_CHAN_PER_CPU) {
480 return;
481 }
482
483 rcu_read_lock();
484 session = session_find_by_id(ua_chan->session->tracing_id);
485 if (!session || !session->ust_session) {
486 /*
487 * Not finding the session is not an error because there are
488 * multiple ways the channels can be torn down.
489 *
490 * 1) The session daemon can initiate the destruction of the
491 * ust app session after receiving a destroy command or
492 * during its shutdown/teardown.
493 * 2) The application, since we are in per-pid tracing, is
494 * unregistering and tearing down its ust app session.
495 *
496 * Both paths are protected by the session list lock which
497 * ensures that the accounting of lost packets and discarded
498 * events is done exactly once. The session is then unpublished
499 * from the session list, resulting in this condition.
500 */
501 goto end;
502 }
503
504 if (ua_chan->attr.overwrite) {
505 consumer_get_lost_packets(ua_chan->session->tracing_id,
506 ua_chan->key, session->ust_session->consumer,
507 &lost);
508 } else {
509 consumer_get_discarded_events(ua_chan->session->tracing_id,
510 ua_chan->key, session->ust_session->consumer,
511 &discarded);
512 }
513 uchan = trace_ust_find_channel_by_name(
514 session->ust_session->domain_global.channels,
515 ua_chan->name);
516 if (!uchan) {
517 ERR("Missing UST channel to store discarded counters");
518 goto end;
519 }
520
521 uchan->per_pid_closed_app_discarded += discarded;
522 uchan->per_pid_closed_app_lost += lost;
523
524 end:
525 rcu_read_unlock();
526 if (session) {
527 session_put(session);
528 }
529 }
530
531 /*
532 * Delete ust app channel safely. RCU read lock must be held before calling
533 * this function.
534 *
535 * The session list lock must be held by the caller.
536 */
537 static
538 void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
539 struct ust_app *app)
540 {
541 int ret;
542 struct lttng_ht_iter iter;
543 struct ust_app_event *ua_event;
544 struct ust_app_ctx *ua_ctx;
545 struct ust_app_stream *stream, *stmp;
546 struct ust_registry_session *registry;
547
548 LTTNG_ASSERT(ua_chan);
549 ASSERT_RCU_READ_LOCKED();
550
551 DBG3("UST app deleting channel %s", ua_chan->name);
552
553 /* Wipe stream */
554 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
555 cds_list_del(&stream->list);
556 delete_ust_app_stream(sock, stream, app);
557 }
558
559 /* Wipe context */
560 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
561 cds_list_del(&ua_ctx->list);
562 ret = lttng_ht_del(ua_chan->ctx, &iter);
563 LTTNG_ASSERT(!ret);
564 delete_ust_app_ctx(sock, ua_ctx, app);
565 }
566
567 /* Wipe events */
568 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
569 node.node) {
570 ret = lttng_ht_del(ua_chan->events, &iter);
571 LTTNG_ASSERT(!ret);
572 delete_ust_app_event(sock, ua_event, app);
573 }
574
575 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
576 /* Wipe and free registry from session registry. */
577 registry = get_session_registry(ua_chan->session);
578 if (registry) {
579 ust_registry_channel_del_free(registry, ua_chan->key,
580 sock >= 0);
581 }
582 /*
583 * A negative socket can be used by the caller when
584 * cleaning-up a ua_chan in an error path. Skip the
585 * accounting in this case.
586 */
587 if (sock >= 0) {
588 save_per_pid_lost_discarded_counters(ua_chan);
589 }
590 }
591
592 if (ua_chan->obj != NULL) {
593 /* Remove channel from application UST object descriptor. */
594 iter.iter.node = &ua_chan->ust_objd_node.node;
595 ret = lttng_ht_del(app->ust_objd, &iter);
596 LTTNG_ASSERT(!ret);
597 pthread_mutex_lock(&app->sock_lock);
598 ret = lttng_ust_ctl_release_object(sock, ua_chan->obj);
599 pthread_mutex_unlock(&app->sock_lock);
600 if (ret < 0) {
601 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
602 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
603 ua_chan->name, app->pid,
604 app->sock);
605 } else if (ret == -EAGAIN) {
606 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
607 ua_chan->name, app->pid,
608 app->sock);
609 } else {
610 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
611 ua_chan->name, ret, app->pid,
612 app->sock);
613 }
614 }
615 lttng_fd_put(LTTNG_FD_APPS, 1);
616 free(ua_chan->obj);
617 }
618 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
619 }
620
621 int ust_app_register_done(struct ust_app *app)
622 {
623 int ret;
624
625 pthread_mutex_lock(&app->sock_lock);
626 ret = lttng_ust_ctl_register_done(app->sock);
627 pthread_mutex_unlock(&app->sock_lock);
628 return ret;
629 }
630
631 int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data)
632 {
633 int ret, sock;
634
635 if (app) {
636 pthread_mutex_lock(&app->sock_lock);
637 sock = app->sock;
638 } else {
639 sock = -1;
640 }
641 ret = lttng_ust_ctl_release_object(sock, data);
642 if (app) {
643 pthread_mutex_unlock(&app->sock_lock);
644 }
645 return ret;
646 }
647
648 /*
649 * Push metadata to consumer socket.
650 *
651 * RCU read-side lock must be held to guarantee existence of socket.
652 * Must be called with the ust app session lock held.
653 * Must be called with the registry lock held.
654 *
655 * On success, return the len of metadata pushed or else a negative value.
656 * Returning a -EPIPE return value means we could not send the metadata,
657 * but it can be caused by recoverable errors (e.g. the application has
658 * terminated concurrently).
659 */
660 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
661 struct consumer_socket *socket, int send_zero_data)
662 {
663 int ret;
664 char *metadata_str = NULL;
665 size_t len, offset, new_metadata_len_sent;
666 ssize_t ret_val;
667 uint64_t metadata_key, metadata_version;
668
669 LTTNG_ASSERT(registry);
670 LTTNG_ASSERT(socket);
671 ASSERT_RCU_READ_LOCKED();
672
673 metadata_key = registry->metadata_key;
674
675 /*
676 * Means that no metadata was assigned to the session. This can
677 * happens if no start has been done previously.
678 */
679 if (!metadata_key) {
680 return 0;
681 }
682
683 offset = registry->metadata_len_sent;
684 len = registry->metadata_len - registry->metadata_len_sent;
685 new_metadata_len_sent = registry->metadata_len;
686 metadata_version = registry->metadata_version;
687 if (len == 0) {
688 DBG3("No metadata to push for metadata key %" PRIu64,
689 registry->metadata_key);
690 ret_val = len;
691 if (send_zero_data) {
692 DBG("No metadata to push");
693 goto push_data;
694 }
695 goto end;
696 }
697
698 /* Allocate only what we have to send. */
699 metadata_str = (char *) zmalloc(len);
700 if (!metadata_str) {
701 PERROR("zmalloc ust app metadata string");
702 ret_val = -ENOMEM;
703 goto error;
704 }
705 /* Copy what we haven't sent out. */
706 memcpy(metadata_str, registry->metadata + offset, len);
707
708 push_data:
709 pthread_mutex_unlock(&registry->lock);
710 /*
711 * We need to unlock the registry while we push metadata to
712 * break a circular dependency between the consumerd metadata
713 * lock and the sessiond registry lock. Indeed, pushing metadata
714 * to the consumerd awaits that it gets pushed all the way to
715 * relayd, but doing so requires grabbing the metadata lock. If
716 * a concurrent metadata request is being performed by
717 * consumerd, this can try to grab the registry lock on the
718 * sessiond while holding the metadata lock on the consumer
719 * daemon. Those push and pull schemes are performed on two
720 * different bidirectionnal communication sockets.
721 */
722 ret = consumer_push_metadata(socket, metadata_key,
723 metadata_str, len, offset, metadata_version);
724 pthread_mutex_lock(&registry->lock);
725 if (ret < 0) {
726 /*
727 * There is an acceptable race here between the registry
728 * metadata key assignment and the creation on the
729 * consumer. The session daemon can concurrently push
730 * metadata for this registry while being created on the
731 * consumer since the metadata key of the registry is
732 * assigned *before* it is setup to avoid the consumer
733 * to ask for metadata that could possibly be not found
734 * in the session daemon.
735 *
736 * The metadata will get pushed either by the session
737 * being stopped or the consumer requesting metadata if
738 * that race is triggered.
739 */
740 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
741 ret = 0;
742 } else {
743 ERR("Error pushing metadata to consumer");
744 }
745 ret_val = ret;
746 goto error_push;
747 } else {
748 /*
749 * Metadata may have been concurrently pushed, since
750 * we're not holding the registry lock while pushing to
751 * consumer. This is handled by the fact that we send
752 * the metadata content, size, and the offset at which
753 * that metadata belongs. This may arrive out of order
754 * on the consumer side, and the consumer is able to
755 * deal with overlapping fragments. The consumer
756 * supports overlapping fragments, which must be
757 * contiguous starting from offset 0. We keep the
758 * largest metadata_len_sent value of the concurrent
759 * send.
760 */
761 registry->metadata_len_sent =
762 std::max(registry->metadata_len_sent,
763 new_metadata_len_sent);
764 }
765 free(metadata_str);
766 return len;
767
768 end:
769 error:
770 if (ret_val) {
771 /*
772 * On error, flag the registry that the metadata is
773 * closed. We were unable to push anything and this
774 * means that either the consumer is not responding or
775 * the metadata cache has been destroyed on the
776 * consumer.
777 */
778 registry->metadata_closed = 1;
779 }
780 error_push:
781 free(metadata_str);
782 return ret_val;
783 }
784
785 /*
786 * For a given application and session, push metadata to consumer.
787 * Either sock or consumer is required : if sock is NULL, the default
788 * socket to send the metadata is retrieved from consumer, if sock
789 * is not NULL we use it to send the metadata.
790 * RCU read-side lock must be held while calling this function,
791 * therefore ensuring existence of registry. It also ensures existence
792 * of socket throughout this function.
793 *
794 * Return 0 on success else a negative error.
795 * Returning a -EPIPE return value means we could not send the metadata,
796 * but it can be caused by recoverable errors (e.g. the application has
797 * terminated concurrently).
798 */
799 static int push_metadata(struct ust_registry_session *registry,
800 struct consumer_output *consumer)
801 {
802 int ret_val;
803 ssize_t ret;
804 struct consumer_socket *socket;
805
806 LTTNG_ASSERT(registry);
807 LTTNG_ASSERT(consumer);
808 ASSERT_RCU_READ_LOCKED();
809
810 pthread_mutex_lock(&registry->lock);
811 if (registry->metadata_closed) {
812 ret_val = -EPIPE;
813 goto error;
814 }
815
816 /* Get consumer socket to use to push the metadata.*/
817 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
818 consumer);
819 if (!socket) {
820 ret_val = -1;
821 goto error;
822 }
823
824 ret = ust_app_push_metadata(registry, socket, 0);
825 if (ret < 0) {
826 ret_val = ret;
827 goto error;
828 }
829 pthread_mutex_unlock(&registry->lock);
830 return 0;
831
832 error:
833 pthread_mutex_unlock(&registry->lock);
834 return ret_val;
835 }
836
837 /*
838 * Send to the consumer a close metadata command for the given session. Once
839 * done, the metadata channel is deleted and the session metadata pointer is
840 * nullified. The session lock MUST be held unless the application is
841 * in the destroy path.
842 *
843 * Do not hold the registry lock while communicating with the consumerd, because
844 * doing so causes inter-process deadlocks between consumerd and sessiond with
845 * the metadata request notification.
846 *
847 * Return 0 on success else a negative value.
848 */
849 static int close_metadata(struct ust_registry_session *registry,
850 struct consumer_output *consumer)
851 {
852 int ret;
853 struct consumer_socket *socket;
854 uint64_t metadata_key;
855 bool registry_was_already_closed;
856
857 LTTNG_ASSERT(registry);
858 LTTNG_ASSERT(consumer);
859
860 rcu_read_lock();
861
862 pthread_mutex_lock(&registry->lock);
863 metadata_key = registry->metadata_key;
864 registry_was_already_closed = registry->metadata_closed;
865 if (metadata_key != 0) {
866 /*
867 * Metadata closed. Even on error this means that the consumer
868 * is not responding or not found so either way a second close
869 * should NOT be emit for this registry.
870 */
871 registry->metadata_closed = 1;
872 }
873 pthread_mutex_unlock(&registry->lock);
874
875 if (metadata_key == 0 || registry_was_already_closed) {
876 ret = 0;
877 goto end;
878 }
879
880 /* Get consumer socket to use to push the metadata.*/
881 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
882 consumer);
883 if (!socket) {
884 ret = -1;
885 goto end;
886 }
887
888 ret = consumer_close_metadata(socket, metadata_key);
889 if (ret < 0) {
890 goto end;
891 }
892
893 end:
894 rcu_read_unlock();
895 return ret;
896 }
897
898 static
899 void delete_ust_app_session_rcu(struct rcu_head *head)
900 {
901 struct ust_app_session *ua_sess =
902 caa_container_of(head, struct ust_app_session, rcu_head);
903
904 lttng_ht_destroy(ua_sess->channels);
905 free(ua_sess);
906 }
907
908 /*
909 * Delete ust app session safely. RCU read lock must be held before calling
910 * this function.
911 *
912 * The session list lock must be held by the caller.
913 */
914 static
915 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
916 struct ust_app *app)
917 {
918 int ret;
919 struct lttng_ht_iter iter;
920 struct ust_app_channel *ua_chan;
921 struct ust_registry_session *registry;
922
923 LTTNG_ASSERT(ua_sess);
924 ASSERT_RCU_READ_LOCKED();
925
926 pthread_mutex_lock(&ua_sess->lock);
927
928 LTTNG_ASSERT(!ua_sess->deleted);
929 ua_sess->deleted = true;
930
931 registry = get_session_registry(ua_sess);
932 /* Registry can be null on error path during initialization. */
933 if (registry) {
934 /* Push metadata for application before freeing the application. */
935 (void) push_metadata(registry, ua_sess->consumer);
936
937 /*
938 * Don't ask to close metadata for global per UID buffers. Close
939 * metadata only on destroy trace session in this case. Also, the
940 * previous push metadata could have flag the metadata registry to
941 * close so don't send a close command if closed.
942 */
943 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
944 /* And ask to close it for this session registry. */
945 (void) close_metadata(registry, ua_sess->consumer);
946 }
947 }
948
949 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
950 node.node) {
951 ret = lttng_ht_del(ua_sess->channels, &iter);
952 LTTNG_ASSERT(!ret);
953 delete_ust_app_channel(sock, ua_chan, app);
954 }
955
956 /* In case of per PID, the registry is kept in the session. */
957 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
958 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
959 if (reg_pid) {
960 /*
961 * Registry can be null on error path during
962 * initialization.
963 */
964 buffer_reg_pid_remove(reg_pid);
965 buffer_reg_pid_destroy(reg_pid);
966 }
967 }
968
969 if (ua_sess->handle != -1) {
970 pthread_mutex_lock(&app->sock_lock);
971 ret = lttng_ust_ctl_release_handle(sock, ua_sess->handle);
972 pthread_mutex_unlock(&app->sock_lock);
973 if (ret < 0) {
974 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
975 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
976 app->pid, app->sock);
977 } else if (ret == -EAGAIN) {
978 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
979 app->pid, app->sock);
980 } else {
981 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
982 ret, app->pid, app->sock);
983 }
984 }
985
986 /* Remove session from application UST object descriptor. */
987 iter.iter.node = &ua_sess->ust_objd_node.node;
988 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
989 LTTNG_ASSERT(!ret);
990 }
991
992 pthread_mutex_unlock(&ua_sess->lock);
993
994 consumer_output_put(ua_sess->consumer);
995
996 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
997 }
998
999 /*
1000 * Delete a traceable application structure from the global list. Never call
1001 * this function outside of a call_rcu call.
1002 */
1003 static
1004 void delete_ust_app(struct ust_app *app)
1005 {
1006 int ret, sock;
1007 struct ust_app_session *ua_sess, *tmp_ua_sess;
1008 struct lttng_ht_iter iter;
1009 struct ust_app_event_notifier_rule *event_notifier_rule;
1010 bool event_notifier_write_fd_is_open;
1011
1012 /*
1013 * The session list lock must be held during this function to guarantee
1014 * the existence of ua_sess.
1015 */
1016 session_lock_list();
1017 /* Delete ust app sessions info */
1018 sock = app->sock;
1019 app->sock = -1;
1020
1021 /* Wipe sessions */
1022 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
1023 teardown_node) {
1024 /* Free every object in the session and the session. */
1025 rcu_read_lock();
1026 delete_ust_app_session(sock, ua_sess, app);
1027 rcu_read_unlock();
1028 }
1029
1030 /* Remove the event notifier rules associated with this app. */
1031 rcu_read_lock();
1032 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
1033 &iter.iter, event_notifier_rule, node.node) {
1034 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter);
1035 LTTNG_ASSERT(!ret);
1036
1037 delete_ust_app_event_notifier_rule(
1038 app->sock, event_notifier_rule, app);
1039 }
1040
1041 rcu_read_unlock();
1042
1043 lttng_ht_destroy(app->sessions);
1044 lttng_ht_destroy(app->ust_sessions_objd);
1045 lttng_ht_destroy(app->ust_objd);
1046 lttng_ht_destroy(app->token_to_event_notifier_rule_ht);
1047
1048 /*
1049 * This could be NULL if the event notifier setup failed (e.g the app
1050 * was killed or the tracer does not support this feature).
1051 */
1052 if (app->event_notifier_group.object) {
1053 enum lttng_error_code ret_code;
1054 enum event_notifier_error_accounting_status status;
1055
1056 const int event_notifier_read_fd = lttng_pipe_get_readfd(
1057 app->event_notifier_group.event_pipe);
1058
1059 ret_code = notification_thread_command_remove_tracer_event_source(
1060 the_notification_thread_handle,
1061 event_notifier_read_fd);
1062 if (ret_code != LTTNG_OK) {
1063 ERR("Failed to remove application tracer event source from notification thread");
1064 }
1065
1066 status = event_notifier_error_accounting_unregister_app(app);
1067 if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
1068 ERR("Error unregistering app from event notifier error accounting");
1069 }
1070
1071 lttng_ust_ctl_release_object(sock, app->event_notifier_group.object);
1072 free(app->event_notifier_group.object);
1073 }
1074
1075 event_notifier_write_fd_is_open = lttng_pipe_is_write_open(
1076 app->event_notifier_group.event_pipe);
1077 lttng_pipe_destroy(app->event_notifier_group.event_pipe);
1078 /*
1079 * Release the file descriptors reserved for the event notifier pipe.
1080 * The app could be destroyed before the write end of the pipe could be
1081 * passed to the application (and closed). In that case, both file
1082 * descriptors must be released.
1083 */
1084 lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1);
1085
1086 /*
1087 * Wait until we have deleted the application from the sock hash table
1088 * before closing this socket, otherwise an application could re-use the
1089 * socket ID and race with the teardown, using the same hash table entry.
1090 *
1091 * It's OK to leave the close in call_rcu. We want it to stay unique for
1092 * all RCU readers that could run concurrently with unregister app,
1093 * therefore we _need_ to only close that socket after a grace period. So
1094 * it should stay in this RCU callback.
1095 *
1096 * This close() is a very important step of the synchronization model so
1097 * every modification to this function must be carefully reviewed.
1098 */
1099 ret = close(sock);
1100 if (ret) {
1101 PERROR("close");
1102 }
1103 lttng_fd_put(LTTNG_FD_APPS, 1);
1104
1105 DBG2("UST app pid %d deleted", app->pid);
1106 free(app);
1107 session_unlock_list();
1108 }
1109
1110 /*
1111 * URCU intermediate call to delete an UST app.
1112 */
1113 static
1114 void delete_ust_app_rcu(struct rcu_head *head)
1115 {
1116 struct lttng_ht_node_ulong *node =
1117 caa_container_of(head, struct lttng_ht_node_ulong, head);
1118 struct ust_app *app =
1119 caa_container_of(node, struct ust_app, pid_n);
1120
1121 DBG3("Call RCU deleting app PID %d", app->pid);
1122 delete_ust_app(app);
1123 }
1124
1125 /*
1126 * Delete the session from the application ht and delete the data structure by
1127 * freeing every object inside and releasing them.
1128 *
1129 * The session list lock must be held by the caller.
1130 */
1131 static void destroy_app_session(struct ust_app *app,
1132 struct ust_app_session *ua_sess)
1133 {
1134 int ret;
1135 struct lttng_ht_iter iter;
1136
1137 LTTNG_ASSERT(app);
1138 LTTNG_ASSERT(ua_sess);
1139
1140 iter.iter.node = &ua_sess->node.node;
1141 ret = lttng_ht_del(app->sessions, &iter);
1142 if (ret) {
1143 /* Already scheduled for teardown. */
1144 goto end;
1145 }
1146
1147 /* Once deleted, free the data structure. */
1148 delete_ust_app_session(app->sock, ua_sess, app);
1149
1150 end:
1151 return;
1152 }
1153
1154 /*
1155 * Alloc new UST app session.
1156 */
1157 static
1158 struct ust_app_session *alloc_ust_app_session(void)
1159 {
1160 struct ust_app_session *ua_sess;
1161
1162 /* Init most of the default value by allocating and zeroing */
1163 ua_sess = (ust_app_session *) zmalloc(sizeof(struct ust_app_session));
1164 if (ua_sess == NULL) {
1165 PERROR("malloc");
1166 goto error_free;
1167 }
1168
1169 ua_sess->handle = -1;
1170 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1171 ua_sess->metadata_attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1172 pthread_mutex_init(&ua_sess->lock, NULL);
1173
1174 return ua_sess;
1175
1176 error_free:
1177 return NULL;
1178 }
1179
1180 /*
1181 * Alloc new UST app channel.
1182 */
1183 static
1184 struct ust_app_channel *alloc_ust_app_channel(const char *name,
1185 struct ust_app_session *ua_sess,
1186 struct lttng_ust_abi_channel_attr *attr)
1187 {
1188 struct ust_app_channel *ua_chan;
1189
1190 /* Init most of the default value by allocating and zeroing */
1191 ua_chan = (ust_app_channel *) zmalloc(sizeof(struct ust_app_channel));
1192 if (ua_chan == NULL) {
1193 PERROR("malloc");
1194 goto error;
1195 }
1196
1197 /* Setup channel name */
1198 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
1199 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1200
1201 ua_chan->enabled = 1;
1202 ua_chan->handle = -1;
1203 ua_chan->session = ua_sess;
1204 ua_chan->key = get_next_channel_key();
1205 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1206 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1207 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
1208
1209 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
1210 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
1211
1212 /* Copy attributes */
1213 if (attr) {
1214 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1215 ua_chan->attr.subbuf_size = attr->subbuf_size;
1216 ua_chan->attr.num_subbuf = attr->num_subbuf;
1217 ua_chan->attr.overwrite = attr->overwrite;
1218 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
1219 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
1220 ua_chan->attr.output = (lttng_ust_abi_output) attr->output;
1221 ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout;
1222 }
1223 /* By default, the channel is a per cpu channel. */
1224 ua_chan->attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1225
1226 DBG3("UST app channel %s allocated", ua_chan->name);
1227
1228 return ua_chan;
1229
1230 error:
1231 return NULL;
1232 }
1233
1234 /*
1235 * Allocate and initialize a UST app stream.
1236 *
1237 * Return newly allocated stream pointer or NULL on error.
1238 */
1239 struct ust_app_stream *ust_app_alloc_stream(void)
1240 {
1241 struct ust_app_stream *stream = NULL;
1242
1243 stream = (ust_app_stream *) zmalloc(sizeof(*stream));
1244 if (stream == NULL) {
1245 PERROR("zmalloc ust app stream");
1246 goto error;
1247 }
1248
1249 /* Zero could be a valid value for a handle so flag it to -1. */
1250 stream->handle = -1;
1251
1252 error:
1253 return stream;
1254 }
1255
1256 /*
1257 * Alloc new UST app event.
1258 */
1259 static
1260 struct ust_app_event *alloc_ust_app_event(char *name,
1261 struct lttng_ust_abi_event *attr)
1262 {
1263 struct ust_app_event *ua_event;
1264
1265 /* Init most of the default value by allocating and zeroing */
1266 ua_event = (ust_app_event *) zmalloc(sizeof(struct ust_app_event));
1267 if (ua_event == NULL) {
1268 PERROR("Failed to allocate ust_app_event structure");
1269 goto error;
1270 }
1271
1272 ua_event->enabled = 1;
1273 strncpy(ua_event->name, name, sizeof(ua_event->name));
1274 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1275 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
1276
1277 /* Copy attributes */
1278 if (attr) {
1279 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
1280 }
1281
1282 DBG3("UST app event %s allocated", ua_event->name);
1283
1284 return ua_event;
1285
1286 error:
1287 return NULL;
1288 }
1289
1290 /*
1291 * Allocate a new UST app event notifier rule.
1292 */
1293 static struct ust_app_event_notifier_rule *alloc_ust_app_event_notifier_rule(
1294 struct lttng_trigger *trigger)
1295 {
1296 enum lttng_event_rule_generate_exclusions_status
1297 generate_exclusion_status;
1298 enum lttng_condition_status cond_status;
1299 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
1300 struct lttng_condition *condition = NULL;
1301 const struct lttng_event_rule *event_rule = NULL;
1302
1303 ua_event_notifier_rule = (ust_app_event_notifier_rule *) zmalloc(sizeof(struct ust_app_event_notifier_rule));
1304 if (ua_event_notifier_rule == NULL) {
1305 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1306 goto error;
1307 }
1308
1309 ua_event_notifier_rule->enabled = 1;
1310 ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger);
1311 lttng_ht_node_init_u64(&ua_event_notifier_rule->node,
1312 ua_event_notifier_rule->token);
1313
1314 condition = lttng_trigger_get_condition(trigger);
1315 LTTNG_ASSERT(condition);
1316 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
1317 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
1318
1319 cond_status = lttng_condition_event_rule_matches_get_rule(
1320 condition, &event_rule);
1321 LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK);
1322 LTTNG_ASSERT(event_rule);
1323
1324 ua_event_notifier_rule->error_counter_index =
1325 lttng_condition_event_rule_matches_get_error_counter_index(condition);
1326 /* Acquire the event notifier's reference to the trigger. */
1327 lttng_trigger_get(trigger);
1328
1329 ua_event_notifier_rule->trigger = trigger;
1330 ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule);
1331 generate_exclusion_status = lttng_event_rule_generate_exclusions(
1332 event_rule, &ua_event_notifier_rule->exclusion);
1333 switch (generate_exclusion_status) {
1334 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK:
1335 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE:
1336 break;
1337 default:
1338 /* Error occurred. */
1339 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1340 goto error_put_trigger;
1341 }
1342
1343 DBG3("UST app event notifier rule allocated: token = %" PRIu64,
1344 ua_event_notifier_rule->token);
1345
1346 return ua_event_notifier_rule;
1347
1348 error_put_trigger:
1349 lttng_trigger_put(trigger);
1350 error:
1351 free(ua_event_notifier_rule);
1352 return NULL;
1353 }
1354
1355 /*
1356 * Alloc new UST app context.
1357 */
1358 static
1359 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
1360 {
1361 struct ust_app_ctx *ua_ctx;
1362
1363 ua_ctx = (ust_app_ctx *) zmalloc(sizeof(struct ust_app_ctx));
1364 if (ua_ctx == NULL) {
1365 goto error;
1366 }
1367
1368 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1369
1370 if (uctx) {
1371 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
1372 if (uctx->ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) {
1373 char *provider_name = NULL, *ctx_name = NULL;
1374
1375 provider_name = strdup(uctx->u.app_ctx.provider_name);
1376 ctx_name = strdup(uctx->u.app_ctx.ctx_name);
1377 if (!provider_name || !ctx_name) {
1378 free(provider_name);
1379 free(ctx_name);
1380 goto error;
1381 }
1382
1383 ua_ctx->ctx.u.app_ctx.provider_name = provider_name;
1384 ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name;
1385 }
1386 }
1387
1388 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
1389 return ua_ctx;
1390 error:
1391 free(ua_ctx);
1392 return NULL;
1393 }
1394
1395 /*
1396 * Create a liblttng-ust filter bytecode from given bytecode.
1397 *
1398 * Return allocated filter or NULL on error.
1399 */
1400 static struct lttng_ust_abi_filter_bytecode *create_ust_filter_bytecode_from_bytecode(
1401 const struct lttng_bytecode *orig_f)
1402 {
1403 struct lttng_ust_abi_filter_bytecode *filter = NULL;
1404
1405 /* Copy filter bytecode. */
1406 filter = (lttng_ust_abi_filter_bytecode *) zmalloc(sizeof(*filter) + orig_f->len);
1407 if (!filter) {
1408 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len);
1409 goto error;
1410 }
1411
1412 LTTNG_ASSERT(sizeof(struct lttng_bytecode) ==
1413 sizeof(struct lttng_ust_abi_filter_bytecode));
1414 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1415 error:
1416 return filter;
1417 }
1418
1419 /*
1420 * Create a liblttng-ust capture bytecode from given bytecode.
1421 *
1422 * Return allocated filter or NULL on error.
1423 */
1424 static struct lttng_ust_abi_capture_bytecode *
1425 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f)
1426 {
1427 struct lttng_ust_abi_capture_bytecode *capture = NULL;
1428
1429 /* Copy capture bytecode. */
1430 capture = (lttng_ust_abi_capture_bytecode *) zmalloc(sizeof(*capture) + orig_f->len);
1431 if (!capture) {
1432 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len);
1433 goto error;
1434 }
1435
1436 LTTNG_ASSERT(sizeof(struct lttng_bytecode) ==
1437 sizeof(struct lttng_ust_abi_capture_bytecode));
1438 memcpy(capture, orig_f, sizeof(*capture) + orig_f->len);
1439 error:
1440 return capture;
1441 }
1442
1443 /*
1444 * Find an ust_app using the sock and return it. RCU read side lock must be
1445 * held before calling this helper function.
1446 */
1447 struct ust_app *ust_app_find_by_sock(int sock)
1448 {
1449 struct lttng_ht_node_ulong *node;
1450 struct lttng_ht_iter iter;
1451
1452 ASSERT_RCU_READ_LOCKED();
1453
1454 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1455 node = lttng_ht_iter_get_node_ulong(&iter);
1456 if (node == NULL) {
1457 DBG2("UST app find by sock %d not found", sock);
1458 goto error;
1459 }
1460
1461 return caa_container_of(node, struct ust_app, sock_n);
1462
1463 error:
1464 return NULL;
1465 }
1466
1467 /*
1468 * Find an ust_app using the notify sock and return it. RCU read side lock must
1469 * be held before calling this helper function.
1470 */
1471 static struct ust_app *find_app_by_notify_sock(int sock)
1472 {
1473 struct lttng_ht_node_ulong *node;
1474 struct lttng_ht_iter iter;
1475
1476 ASSERT_RCU_READ_LOCKED();
1477
1478 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1479 &iter);
1480 node = lttng_ht_iter_get_node_ulong(&iter);
1481 if (node == NULL) {
1482 DBG2("UST app find by notify sock %d not found", sock);
1483 goto error;
1484 }
1485
1486 return caa_container_of(node, struct ust_app, notify_sock_n);
1487
1488 error:
1489 return NULL;
1490 }
1491
1492 /*
1493 * Lookup for an ust app event based on event name, filter bytecode and the
1494 * event loglevel.
1495 *
1496 * Return an ust_app_event object or NULL on error.
1497 */
1498 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
1499 const char *name, const struct lttng_bytecode *filter,
1500 int loglevel_value,
1501 const struct lttng_event_exclusion *exclusion)
1502 {
1503 struct lttng_ht_iter iter;
1504 struct lttng_ht_node_str *node;
1505 struct ust_app_event *event = NULL;
1506 struct ust_app_ht_key key;
1507
1508 LTTNG_ASSERT(name);
1509 LTTNG_ASSERT(ht);
1510
1511 /* Setup key for event lookup. */
1512 key.name = name;
1513 key.filter = filter;
1514 key.loglevel_type = (lttng_ust_abi_loglevel_type) loglevel_value;
1515 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1516 key.exclusion = exclusion;
1517
1518 /* Lookup using the event name as hash and a custom match fct. */
1519 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1520 ht_match_ust_app_event, &key, &iter.iter);
1521 node = lttng_ht_iter_get_node_str(&iter);
1522 if (node == NULL) {
1523 goto end;
1524 }
1525
1526 event = caa_container_of(node, struct ust_app_event, node);
1527
1528 end:
1529 return event;
1530 }
1531
1532 /*
1533 * Look-up an event notifier rule based on its token id.
1534 *
1535 * Must be called with the RCU read lock held.
1536 * Return an ust_app_event_notifier_rule object or NULL on error.
1537 */
1538 static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule(
1539 struct lttng_ht *ht, uint64_t token)
1540 {
1541 struct lttng_ht_iter iter;
1542 struct lttng_ht_node_u64 *node;
1543 struct ust_app_event_notifier_rule *event_notifier_rule = NULL;
1544
1545 LTTNG_ASSERT(ht);
1546 ASSERT_RCU_READ_LOCKED();
1547
1548 lttng_ht_lookup(ht, &token, &iter);
1549 node = lttng_ht_iter_get_node_u64(&iter);
1550 if (node == NULL) {
1551 DBG2("UST app event notifier rule token not found: token = %" PRIu64,
1552 token);
1553 goto end;
1554 }
1555
1556 event_notifier_rule = caa_container_of(
1557 node, struct ust_app_event_notifier_rule, node);
1558 end:
1559 return event_notifier_rule;
1560 }
1561
1562 /*
1563 * Create the channel context on the tracer.
1564 *
1565 * Called with UST app session lock held.
1566 */
1567 static
1568 int create_ust_channel_context(struct ust_app_channel *ua_chan,
1569 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1570 {
1571 int ret;
1572
1573 health_code_update();
1574
1575 pthread_mutex_lock(&app->sock_lock);
1576 ret = lttng_ust_ctl_add_context(app->sock, &ua_ctx->ctx,
1577 ua_chan->obj, &ua_ctx->obj);
1578 pthread_mutex_unlock(&app->sock_lock);
1579 if (ret < 0) {
1580 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1581 ret = 0;
1582 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1583 app->pid, app->sock);
1584 } else if (ret == -EAGAIN) {
1585 ret = 0;
1586 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1587 app->pid, app->sock);
1588 } else {
1589 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1590 ret, app->pid, app->sock);
1591 }
1592 goto error;
1593 }
1594
1595 ua_ctx->handle = ua_ctx->obj->handle;
1596
1597 DBG2("UST app context handle %d created successfully for channel %s",
1598 ua_ctx->handle, ua_chan->name);
1599
1600 error:
1601 health_code_update();
1602 return ret;
1603 }
1604
1605 /*
1606 * Set the filter on the tracer.
1607 */
1608 static int set_ust_object_filter(struct ust_app *app,
1609 const struct lttng_bytecode *bytecode,
1610 struct lttng_ust_abi_object_data *ust_object)
1611 {
1612 int ret;
1613 struct lttng_ust_abi_filter_bytecode *ust_bytecode = NULL;
1614
1615 health_code_update();
1616
1617 ust_bytecode = create_ust_filter_bytecode_from_bytecode(bytecode);
1618 if (!ust_bytecode) {
1619 ret = -LTTNG_ERR_NOMEM;
1620 goto error;
1621 }
1622 pthread_mutex_lock(&app->sock_lock);
1623 ret = lttng_ust_ctl_set_filter(app->sock, ust_bytecode,
1624 ust_object);
1625 pthread_mutex_unlock(&app->sock_lock);
1626 if (ret < 0) {
1627 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1628 ret = 0;
1629 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1630 app->pid, app->sock);
1631 } else if (ret == -EAGAIN) {
1632 ret = 0;
1633 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1634 app->pid, app->sock);
1635 } else {
1636 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1637 ret, app->pid, app->sock, ust_object);
1638 }
1639 goto error;
1640 }
1641
1642 DBG2("UST filter successfully set: object = %p", ust_object);
1643
1644 error:
1645 health_code_update();
1646 free(ust_bytecode);
1647 return ret;
1648 }
1649
1650 /*
1651 * Set a capture bytecode for the passed object.
1652 * The sequence number enforces the ordering at runtime and on reception of
1653 * the captured payloads.
1654 */
1655 static int set_ust_capture(struct ust_app *app,
1656 const struct lttng_bytecode *bytecode,
1657 unsigned int capture_seqnum,
1658 struct lttng_ust_abi_object_data *ust_object)
1659 {
1660 int ret;
1661 struct lttng_ust_abi_capture_bytecode *ust_bytecode = NULL;
1662
1663 health_code_update();
1664
1665 ust_bytecode = create_ust_capture_bytecode_from_bytecode(bytecode);
1666 if (!ust_bytecode) {
1667 ret = -LTTNG_ERR_NOMEM;
1668 goto error;
1669 }
1670
1671 /*
1672 * Set the sequence number to ensure the capture of fields is ordered.
1673 */
1674 ust_bytecode->seqnum = capture_seqnum;
1675
1676 pthread_mutex_lock(&app->sock_lock);
1677 ret = lttng_ust_ctl_set_capture(app->sock, ust_bytecode,
1678 ust_object);
1679 pthread_mutex_unlock(&app->sock_lock);
1680 if (ret < 0) {
1681 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1682 ret = 0;
1683 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1684 app->pid, app->sock);
1685 } else if (ret == -EAGAIN) {
1686 ret = 0;
1687 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1688 app->pid, app->sock);
1689 } else {
1690 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1691 ret, app->pid,
1692 app->sock);
1693 }
1694
1695 goto error;
1696 }
1697
1698 DBG2("UST capture successfully set: object = %p", ust_object);
1699
1700 error:
1701 health_code_update();
1702 free(ust_bytecode);
1703 return ret;
1704 }
1705
1706 static
1707 struct lttng_ust_abi_event_exclusion *create_ust_exclusion_from_exclusion(
1708 const struct lttng_event_exclusion *exclusion)
1709 {
1710 struct lttng_ust_abi_event_exclusion *ust_exclusion = NULL;
1711 size_t exclusion_alloc_size = sizeof(struct lttng_ust_abi_event_exclusion) +
1712 LTTNG_UST_ABI_SYM_NAME_LEN * exclusion->count;
1713
1714 ust_exclusion = (lttng_ust_abi_event_exclusion *) zmalloc(exclusion_alloc_size);
1715 if (!ust_exclusion) {
1716 PERROR("malloc");
1717 goto end;
1718 }
1719
1720 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion) ==
1721 sizeof(struct lttng_ust_abi_event_exclusion));
1722 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1723 end:
1724 return ust_exclusion;
1725 }
1726
1727 /*
1728 * Set event exclusions on the tracer.
1729 */
1730 static int set_ust_object_exclusions(struct ust_app *app,
1731 const struct lttng_event_exclusion *exclusions,
1732 struct lttng_ust_abi_object_data *ust_object)
1733 {
1734 int ret;
1735 struct lttng_ust_abi_event_exclusion *ust_exclusions = NULL;
1736
1737 LTTNG_ASSERT(exclusions && exclusions->count > 0);
1738
1739 health_code_update();
1740
1741 ust_exclusions = create_ust_exclusion_from_exclusion(
1742 exclusions);
1743 if (!ust_exclusions) {
1744 ret = -LTTNG_ERR_NOMEM;
1745 goto error;
1746 }
1747 pthread_mutex_lock(&app->sock_lock);
1748 ret = lttng_ust_ctl_set_exclusion(app->sock, ust_exclusions, ust_object);
1749 pthread_mutex_unlock(&app->sock_lock);
1750 if (ret < 0) {
1751 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1752 ret = 0;
1753 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1754 app->pid, app->sock);
1755 } else if (ret == -EAGAIN) {
1756 ret = 0;
1757 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1758 app->pid, app->sock);
1759 } else {
1760 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1761 ret, app->pid, app->sock, ust_object);
1762 }
1763 goto error;
1764 }
1765
1766 DBG2("UST exclusions set successfully for object %p", ust_object);
1767
1768 error:
1769 health_code_update();
1770 free(ust_exclusions);
1771 return ret;
1772 }
1773
1774 /*
1775 * Disable the specified event on to UST tracer for the UST session.
1776 */
1777 static int disable_ust_object(struct ust_app *app,
1778 struct lttng_ust_abi_object_data *object)
1779 {
1780 int ret;
1781
1782 health_code_update();
1783
1784 pthread_mutex_lock(&app->sock_lock);
1785 ret = lttng_ust_ctl_disable(app->sock, object);
1786 pthread_mutex_unlock(&app->sock_lock);
1787 if (ret < 0) {
1788 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1789 ret = 0;
1790 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1791 app->pid, app->sock);
1792 } else if (ret == -EAGAIN) {
1793 ret = 0;
1794 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1795 app->pid, app->sock);
1796 } else {
1797 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1798 ret, app->pid, app->sock, object);
1799 }
1800 goto error;
1801 }
1802
1803 DBG2("UST app object %p disabled successfully for app: pid = %d",
1804 object, app->pid);
1805
1806 error:
1807 health_code_update();
1808 return ret;
1809 }
1810
1811 /*
1812 * Disable the specified channel on to UST tracer for the UST session.
1813 */
1814 static int disable_ust_channel(struct ust_app *app,
1815 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1816 {
1817 int ret;
1818
1819 health_code_update();
1820
1821 pthread_mutex_lock(&app->sock_lock);
1822 ret = lttng_ust_ctl_disable(app->sock, ua_chan->obj);
1823 pthread_mutex_unlock(&app->sock_lock);
1824 if (ret < 0) {
1825 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1826 ret = 0;
1827 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1828 app->pid, app->sock);
1829 } else if (ret == -EAGAIN) {
1830 ret = 0;
1831 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1832 app->pid, app->sock);
1833 } else {
1834 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1835 ua_chan->name, ua_sess->handle, ret,
1836 app->pid, app->sock);
1837 }
1838 goto error;
1839 }
1840
1841 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1842 ua_chan->name, app->pid);
1843
1844 error:
1845 health_code_update();
1846 return ret;
1847 }
1848
1849 /*
1850 * Enable the specified channel on to UST tracer for the UST session.
1851 */
1852 static int enable_ust_channel(struct ust_app *app,
1853 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1854 {
1855 int ret;
1856
1857 health_code_update();
1858
1859 pthread_mutex_lock(&app->sock_lock);
1860 ret = lttng_ust_ctl_enable(app->sock, ua_chan->obj);
1861 pthread_mutex_unlock(&app->sock_lock);
1862 if (ret < 0) {
1863 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1864 ret = 0;
1865 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1866 ua_chan->name, app->pid, app->sock);
1867 } else if (ret == -EAGAIN) {
1868 ret = 0;
1869 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1870 ua_chan->name, app->pid, app->sock);
1871 } else {
1872 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1873 ua_chan->name, ua_sess->handle, ret,
1874 app->pid, app->sock);
1875 }
1876 goto error;
1877 }
1878
1879 ua_chan->enabled = 1;
1880
1881 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1882 ua_chan->name, app->pid);
1883
1884 error:
1885 health_code_update();
1886 return ret;
1887 }
1888
1889 /*
1890 * Enable the specified event on to UST tracer for the UST session.
1891 */
1892 static int enable_ust_object(
1893 struct ust_app *app, struct lttng_ust_abi_object_data *ust_object)
1894 {
1895 int ret;
1896
1897 health_code_update();
1898
1899 pthread_mutex_lock(&app->sock_lock);
1900 ret = lttng_ust_ctl_enable(app->sock, ust_object);
1901 pthread_mutex_unlock(&app->sock_lock);
1902 if (ret < 0) {
1903 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1904 ret = 0;
1905 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1906 app->pid, app->sock);
1907 } else if (ret == -EAGAIN) {
1908 ret = 0;
1909 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1910 app->pid, app->sock);
1911 } else {
1912 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1913 ret, app->pid, app->sock, ust_object);
1914 }
1915 goto error;
1916 }
1917
1918 DBG2("UST app object %p enabled successfully for app: pid = %d",
1919 ust_object, app->pid);
1920
1921 error:
1922 health_code_update();
1923 return ret;
1924 }
1925
1926 /*
1927 * Send channel and stream buffer to application.
1928 *
1929 * Return 0 on success. On error, a negative value is returned.
1930 */
1931 static int send_channel_pid_to_ust(struct ust_app *app,
1932 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1933 {
1934 int ret;
1935 struct ust_app_stream *stream, *stmp;
1936
1937 LTTNG_ASSERT(app);
1938 LTTNG_ASSERT(ua_sess);
1939 LTTNG_ASSERT(ua_chan);
1940
1941 health_code_update();
1942
1943 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1944 app->sock);
1945
1946 /* Send channel to the application. */
1947 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
1948 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1949 ret = -ENOTCONN; /* Caused by app exiting. */
1950 goto error;
1951 } else if (ret == -EAGAIN) {
1952 /* Caused by timeout. */
1953 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".",
1954 app->pid, ua_chan->name, ua_sess->tracing_id);
1955 /* Treat this the same way as an application that is exiting. */
1956 ret = -ENOTCONN;
1957 goto error;
1958 } else if (ret < 0) {
1959 goto error;
1960 }
1961
1962 health_code_update();
1963
1964 /* Send all streams to application. */
1965 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1966 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1967 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1968 ret = -ENOTCONN; /* Caused by app exiting. */
1969 goto error;
1970 } else if (ret == -EAGAIN) {
1971 /* Caused by timeout. */
1972 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64 "\".",
1973 app->pid, stream->name, ua_chan->name,
1974 ua_sess->tracing_id);
1975 /*
1976 * Treat this the same way as an application that is
1977 * exiting.
1978 */
1979 ret = -ENOTCONN;
1980 } else if (ret < 0) {
1981 goto error;
1982 }
1983 /* We don't need the stream anymore once sent to the tracer. */
1984 cds_list_del(&stream->list);
1985 delete_ust_app_stream(-1, stream, app);
1986 }
1987 /* Flag the channel that it is sent to the application. */
1988 ua_chan->is_sent = 1;
1989
1990 error:
1991 health_code_update();
1992 return ret;
1993 }
1994
1995 /*
1996 * Create the specified event onto the UST tracer for a UST session.
1997 *
1998 * Should be called with session mutex held.
1999 */
2000 static
2001 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
2002 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
2003 {
2004 int ret = 0;
2005
2006 health_code_update();
2007
2008 /* Create UST event on tracer */
2009 pthread_mutex_lock(&app->sock_lock);
2010 ret = lttng_ust_ctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
2011 &ua_event->obj);
2012 pthread_mutex_unlock(&app->sock_lock);
2013 if (ret < 0) {
2014 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2015 ret = 0;
2016 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2017 app->pid, app->sock);
2018 } else if (ret == -EAGAIN) {
2019 ret = 0;
2020 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2021 app->pid, app->sock);
2022 } else {
2023 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2024 ua_event->attr.name, ret, app->pid,
2025 app->sock);
2026 }
2027 goto error;
2028 }
2029
2030 ua_event->handle = ua_event->obj->handle;
2031
2032 DBG2("UST app event %s created successfully for pid:%d object = %p",
2033 ua_event->attr.name, app->pid, ua_event->obj);
2034
2035 health_code_update();
2036
2037 /* Set filter if one is present. */
2038 if (ua_event->filter) {
2039 ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj);
2040 if (ret < 0) {
2041 goto error;
2042 }
2043 }
2044
2045 /* Set exclusions for the event */
2046 if (ua_event->exclusion) {
2047 ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj);
2048 if (ret < 0) {
2049 goto error;
2050 }
2051 }
2052
2053 /* If event not enabled, disable it on the tracer */
2054 if (ua_event->enabled) {
2055 /*
2056 * We now need to explicitly enable the event, since it
2057 * is now disabled at creation.
2058 */
2059 ret = enable_ust_object(app, ua_event->obj);
2060 if (ret < 0) {
2061 /*
2062 * If we hit an EPERM, something is wrong with our enable call. If
2063 * we get an EEXIST, there is a problem on the tracer side since we
2064 * just created it.
2065 */
2066 switch (ret) {
2067 case -LTTNG_UST_ERR_PERM:
2068 /* Code flow problem */
2069 abort();
2070 case -LTTNG_UST_ERR_EXIST:
2071 /* It's OK for our use case. */
2072 ret = 0;
2073 break;
2074 default:
2075 break;
2076 }
2077 goto error;
2078 }
2079 }
2080
2081 error:
2082 health_code_update();
2083 return ret;
2084 }
2085
2086 static int init_ust_event_notifier_from_event_rule(
2087 const struct lttng_event_rule *rule,
2088 struct lttng_ust_abi_event_notifier *event_notifier)
2089 {
2090 enum lttng_event_rule_status status;
2091 enum lttng_ust_abi_loglevel_type ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
2092 int loglevel = -1, ret = 0;
2093 const char *pattern;
2094
2095
2096 memset(event_notifier, 0, sizeof(*event_notifier));
2097
2098 if (lttng_event_rule_targets_agent_domain(rule)) {
2099 /*
2100 * Special event for agents
2101 * The actual meat of the event is in the filter that will be
2102 * attached later on.
2103 * Set the default values for the agent event.
2104 */
2105 pattern = event_get_default_agent_ust_name(
2106 lttng_event_rule_get_domain_type(rule));
2107 loglevel = 0;
2108 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
2109 } else {
2110 const struct lttng_log_level_rule *log_level_rule;
2111
2112 LTTNG_ASSERT(lttng_event_rule_get_type(rule) ==
2113 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT);
2114
2115 status = lttng_event_rule_user_tracepoint_get_name_pattern(rule, &pattern);
2116 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
2117 /* At this point, this is a fatal error. */
2118 abort();
2119 }
2120
2121 status = lttng_event_rule_user_tracepoint_get_log_level_rule(
2122 rule, &log_level_rule);
2123 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
2124 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
2125 } else if (status == LTTNG_EVENT_RULE_STATUS_OK) {
2126 enum lttng_log_level_rule_status llr_status;
2127
2128 switch (lttng_log_level_rule_get_type(log_level_rule)) {
2129 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
2130 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE;
2131 llr_status = lttng_log_level_rule_exactly_get_level(
2132 log_level_rule, &loglevel);
2133 break;
2134 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
2135 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE;
2136 llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
2137 log_level_rule, &loglevel);
2138 break;
2139 default:
2140 abort();
2141 }
2142
2143 LTTNG_ASSERT(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK);
2144 } else {
2145 /* At this point this is a fatal error. */
2146 abort();
2147 }
2148 }
2149
2150 event_notifier->event.instrumentation = LTTNG_UST_ABI_TRACEPOINT;
2151 ret = lttng_strncpy(event_notifier->event.name, pattern,
2152 LTTNG_UST_ABI_SYM_NAME_LEN - 1);
2153 if (ret) {
2154 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2155 pattern);
2156 goto end;
2157 }
2158
2159 event_notifier->event.loglevel_type = ust_loglevel_type;
2160 event_notifier->event.loglevel = loglevel;
2161 end:
2162 return ret;
2163 }
2164
2165 /*
2166 * Create the specified event notifier against the user space tracer of a
2167 * given application.
2168 */
2169 static int create_ust_event_notifier(struct ust_app *app,
2170 struct ust_app_event_notifier_rule *ua_event_notifier_rule)
2171 {
2172 int ret = 0;
2173 enum lttng_condition_status condition_status;
2174 const struct lttng_condition *condition = NULL;
2175 struct lttng_ust_abi_event_notifier event_notifier;
2176 const struct lttng_event_rule *event_rule = NULL;
2177 unsigned int capture_bytecode_count = 0, i;
2178 enum lttng_condition_status cond_status;
2179 enum lttng_event_rule_type event_rule_type;
2180
2181 health_code_update();
2182 LTTNG_ASSERT(app->event_notifier_group.object);
2183
2184 condition = lttng_trigger_get_const_condition(
2185 ua_event_notifier_rule->trigger);
2186 LTTNG_ASSERT(condition);
2187 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
2188 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
2189
2190 condition_status = lttng_condition_event_rule_matches_get_rule(
2191 condition, &event_rule);
2192 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
2193
2194 LTTNG_ASSERT(event_rule);
2195
2196 event_rule_type = lttng_event_rule_get_type(event_rule);
2197 LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT ||
2198 event_rule_type == LTTNG_EVENT_RULE_TYPE_JUL_LOGGING ||
2199 event_rule_type ==
2200 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING ||
2201 event_rule_type ==
2202 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING);
2203
2204 init_ust_event_notifier_from_event_rule(event_rule, &event_notifier);
2205 event_notifier.event.token = ua_event_notifier_rule->token;
2206 event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index;
2207
2208 /* Create UST event notifier against the tracer. */
2209 pthread_mutex_lock(&app->sock_lock);
2210 ret = lttng_ust_ctl_create_event_notifier(app->sock, &event_notifier,
2211 app->event_notifier_group.object,
2212 &ua_event_notifier_rule->obj);
2213 pthread_mutex_unlock(&app->sock_lock);
2214 if (ret < 0) {
2215 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2216 ret = 0;
2217 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2218 app->pid, app->sock);
2219 } else if (ret == -EAGAIN) {
2220 ret = 0;
2221 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2222 app->pid, app->sock);
2223 } else {
2224 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2225 event_notifier.event.name, ret, app->pid,
2226 app->sock);
2227 }
2228 goto error;
2229 }
2230
2231 ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle;
2232
2233 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2234 event_notifier.event.name, app->name, app->pid,
2235 ua_event_notifier_rule->obj);
2236
2237 health_code_update();
2238
2239 /* Set filter if one is present. */
2240 if (ua_event_notifier_rule->filter) {
2241 ret = set_ust_object_filter(app, ua_event_notifier_rule->filter,
2242 ua_event_notifier_rule->obj);
2243 if (ret < 0) {
2244 goto error;
2245 }
2246 }
2247
2248 /* Set exclusions for the event. */
2249 if (ua_event_notifier_rule->exclusion) {
2250 ret = set_ust_object_exclusions(app,
2251 ua_event_notifier_rule->exclusion,
2252 ua_event_notifier_rule->obj);
2253 if (ret < 0) {
2254 goto error;
2255 }
2256 }
2257
2258 /* Set the capture bytecodes. */
2259 cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count(
2260 condition, &capture_bytecode_count);
2261 LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK);
2262
2263 for (i = 0; i < capture_bytecode_count; i++) {
2264 const struct lttng_bytecode *capture_bytecode =
2265 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2266 condition, i);
2267
2268 ret = set_ust_capture(app, capture_bytecode, i,
2269 ua_event_notifier_rule->obj);
2270 if (ret < 0) {
2271 goto error;
2272 }
2273 }
2274
2275 /*
2276 * We now need to explicitly enable the event, since it
2277 * is disabled at creation.
2278 */
2279 ret = enable_ust_object(app, ua_event_notifier_rule->obj);
2280 if (ret < 0) {
2281 /*
2282 * If we hit an EPERM, something is wrong with our enable call.
2283 * If we get an EEXIST, there is a problem on the tracer side
2284 * since we just created it.
2285 */
2286 switch (ret) {
2287 case -LTTNG_UST_ERR_PERM:
2288 /* Code flow problem. */
2289 abort();
2290 case -LTTNG_UST_ERR_EXIST:
2291 /* It's OK for our use case. */
2292 ret = 0;
2293 break;
2294 default:
2295 break;
2296 }
2297
2298 goto error;
2299 }
2300
2301 ua_event_notifier_rule->enabled = true;
2302
2303 error:
2304 health_code_update();
2305 return ret;
2306 }
2307
2308 /*
2309 * Copy data between an UST app event and a LTT event.
2310 */
2311 static void shadow_copy_event(struct ust_app_event *ua_event,
2312 struct ltt_ust_event *uevent)
2313 {
2314 size_t exclusion_alloc_size;
2315
2316 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
2317 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
2318
2319 ua_event->enabled = uevent->enabled;
2320
2321 /* Copy event attributes */
2322 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
2323
2324 /* Copy filter bytecode */
2325 if (uevent->filter) {
2326 ua_event->filter = lttng_bytecode_copy(uevent->filter);
2327 /* Filter might be NULL here in case of ENONEM. */
2328 }
2329
2330 /* Copy exclusion data */
2331 if (uevent->exclusion) {
2332 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
2333 LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count;
2334 ua_event->exclusion = (lttng_event_exclusion *) zmalloc(exclusion_alloc_size);
2335 if (ua_event->exclusion == NULL) {
2336 PERROR("malloc");
2337 } else {
2338 memcpy(ua_event->exclusion, uevent->exclusion,
2339 exclusion_alloc_size);
2340 }
2341 }
2342 }
2343
2344 /*
2345 * Copy data between an UST app channel and a LTT channel.
2346 */
2347 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
2348 struct ltt_ust_channel *uchan)
2349 {
2350 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
2351
2352 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
2353 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
2354
2355 ua_chan->tracefile_size = uchan->tracefile_size;
2356 ua_chan->tracefile_count = uchan->tracefile_count;
2357
2358 /* Copy event attributes since the layout is different. */
2359 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
2360 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
2361 ua_chan->attr.overwrite = uchan->attr.overwrite;
2362 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
2363 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
2364 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
2365 ua_chan->attr.output = (lttng_ust_abi_output) uchan->attr.output;
2366 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
2367
2368 /*
2369 * Note that the attribute channel type is not set since the channel on the
2370 * tracing registry side does not have this information.
2371 */
2372
2373 ua_chan->enabled = uchan->enabled;
2374 ua_chan->tracing_channel_id = uchan->id;
2375
2376 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
2377 }
2378
2379 /*
2380 * Copy data between a UST app session and a regular LTT session.
2381 */
2382 static void shadow_copy_session(struct ust_app_session *ua_sess,
2383 struct ltt_ust_session *usess, struct ust_app *app)
2384 {
2385 struct tm *timeinfo;
2386 char datetime[16];
2387 int ret;
2388 char tmp_shm_path[PATH_MAX];
2389
2390 timeinfo = localtime(&app->registration_time);
2391 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
2392
2393 DBG2("Shadow copy of session handle %d", ua_sess->handle);
2394
2395 ua_sess->tracing_id = usess->id;
2396 ua_sess->id = get_next_session_id();
2397 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid);
2398 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid);
2399 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid);
2400 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid);
2401 ua_sess->buffer_type = usess->buffer_type;
2402 ua_sess->bits_per_long = app->bits_per_long;
2403
2404 /* There is only one consumer object per session possible. */
2405 consumer_output_get(usess->consumer);
2406 ua_sess->consumer = usess->consumer;
2407
2408 ua_sess->output_traces = usess->output_traces;
2409 ua_sess->live_timer_interval = usess->live_timer_interval;
2410 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
2411 &usess->metadata_attr);
2412
2413 switch (ua_sess->buffer_type) {
2414 case LTTNG_BUFFER_PER_PID:
2415 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
2416 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
2417 datetime);
2418 break;
2419 case LTTNG_BUFFER_PER_UID:
2420 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
2421 DEFAULT_UST_TRACE_UID_PATH,
2422 lttng_credentials_get_uid(&ua_sess->real_credentials),
2423 app->bits_per_long);
2424 break;
2425 default:
2426 abort();
2427 goto error;
2428 }
2429 if (ret < 0) {
2430 PERROR("asprintf UST shadow copy session");
2431 abort();
2432 goto error;
2433 }
2434
2435 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
2436 sizeof(ua_sess->root_shm_path));
2437 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
2438 strncpy(ua_sess->shm_path, usess->shm_path,
2439 sizeof(ua_sess->shm_path));
2440 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
2441 if (ua_sess->shm_path[0]) {
2442 switch (ua_sess->buffer_type) {
2443 case LTTNG_BUFFER_PER_PID:
2444 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
2445 "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
2446 app->name, app->pid, datetime);
2447 break;
2448 case LTTNG_BUFFER_PER_UID:
2449 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
2450 "/" DEFAULT_UST_TRACE_UID_PATH,
2451 app->uid, app->bits_per_long);
2452 break;
2453 default:
2454 abort();
2455 goto error;
2456 }
2457 if (ret < 0) {
2458 PERROR("sprintf UST shadow copy session");
2459 abort();
2460 goto error;
2461 }
2462 strncat(ua_sess->shm_path, tmp_shm_path,
2463 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
2464 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
2465 }
2466 return;
2467
2468 error:
2469 consumer_output_put(ua_sess->consumer);
2470 }
2471
2472 /*
2473 * Lookup sesison wrapper.
2474 */
2475 static
2476 void __lookup_session_by_app(const struct ltt_ust_session *usess,
2477 struct ust_app *app, struct lttng_ht_iter *iter)
2478 {
2479 /* Get right UST app session from app */
2480 lttng_ht_lookup(app->sessions, &usess->id, iter);
2481 }
2482
2483 /*
2484 * Return ust app session from the app session hashtable using the UST session
2485 * id.
2486 */
2487 static struct ust_app_session *lookup_session_by_app(
2488 const struct ltt_ust_session *usess, struct ust_app *app)
2489 {
2490 struct lttng_ht_iter iter;
2491 struct lttng_ht_node_u64 *node;
2492
2493 __lookup_session_by_app(usess, app, &iter);
2494 node = lttng_ht_iter_get_node_u64(&iter);
2495 if (node == NULL) {
2496 goto error;
2497 }
2498
2499 return caa_container_of(node, struct ust_app_session, node);
2500
2501 error:
2502 return NULL;
2503 }
2504
2505 /*
2506 * Setup buffer registry per PID for the given session and application. If none
2507 * is found, a new one is created, added to the global registry and
2508 * initialized. If regp is valid, it's set with the newly created object.
2509 *
2510 * Return 0 on success or else a negative value.
2511 */
2512 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
2513 struct ust_app *app, struct buffer_reg_pid **regp)
2514 {
2515 int ret = 0;
2516 struct buffer_reg_pid *reg_pid;
2517
2518 LTTNG_ASSERT(ua_sess);
2519 LTTNG_ASSERT(app);
2520
2521 rcu_read_lock();
2522
2523 reg_pid = buffer_reg_pid_find(ua_sess->id);
2524 if (!reg_pid) {
2525 /*
2526 * This is the create channel path meaning that if there is NO
2527 * registry available, we have to create one for this session.
2528 */
2529 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
2530 ua_sess->root_shm_path, ua_sess->shm_path);
2531 if (ret < 0) {
2532 goto error;
2533 }
2534 } else {
2535 goto end;
2536 }
2537
2538 /* Initialize registry. */
2539 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
2540 app->bits_per_long, app->uint8_t_alignment,
2541 app->uint16_t_alignment, app->uint32_t_alignment,
2542 app->uint64_t_alignment, app->long_alignment,
2543 app->byte_order, app->version.major, app->version.minor,
2544 reg_pid->root_shm_path, reg_pid->shm_path,
2545 lttng_credentials_get_uid(&ua_sess->effective_credentials),
2546 lttng_credentials_get_gid(&ua_sess->effective_credentials),
2547 ua_sess->tracing_id,
2548 app->uid);
2549 if (ret < 0) {
2550 /*
2551 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2552 * destroy the buffer registry, because it is always expected
2553 * that if the buffer registry can be found, its ust registry is
2554 * non-NULL.
2555 */
2556 buffer_reg_pid_destroy(reg_pid);
2557 goto error;
2558 }
2559
2560 buffer_reg_pid_add(reg_pid);
2561
2562 DBG3("UST app buffer registry per PID created successfully");
2563
2564 end:
2565 if (regp) {
2566 *regp = reg_pid;
2567 }
2568 error:
2569 rcu_read_unlock();
2570 return ret;
2571 }
2572
2573 /*
2574 * Setup buffer registry per UID for the given session and application. If none
2575 * is found, a new one is created, added to the global registry and
2576 * initialized. If regp is valid, it's set with the newly created object.
2577 *
2578 * Return 0 on success or else a negative value.
2579 */
2580 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
2581 struct ust_app_session *ua_sess,
2582 struct ust_app *app, struct buffer_reg_uid **regp)
2583 {
2584 int ret = 0;
2585 struct buffer_reg_uid *reg_uid;
2586
2587 LTTNG_ASSERT(usess);
2588 LTTNG_ASSERT(app);
2589
2590 rcu_read_lock();
2591
2592 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2593 if (!reg_uid) {
2594 /*
2595 * This is the create channel path meaning that if there is NO
2596 * registry available, we have to create one for this session.
2597 */
2598 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
2599 LTTNG_DOMAIN_UST, &reg_uid,
2600 ua_sess->root_shm_path, ua_sess->shm_path);
2601 if (ret < 0) {
2602 goto error;
2603 }
2604 } else {
2605 goto end;
2606 }
2607
2608 /* Initialize registry. */
2609 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
2610 app->bits_per_long, app->uint8_t_alignment,
2611 app->uint16_t_alignment, app->uint32_t_alignment,
2612 app->uint64_t_alignment, app->long_alignment,
2613 app->byte_order, app->version.major,
2614 app->version.minor, reg_uid->root_shm_path,
2615 reg_uid->shm_path, usess->uid, usess->gid,
2616 ua_sess->tracing_id, app->uid);
2617 if (ret < 0) {
2618 /*
2619 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2620 * destroy the buffer registry, because it is always expected
2621 * that if the buffer registry can be found, its ust registry is
2622 * non-NULL.
2623 */
2624 buffer_reg_uid_destroy(reg_uid, NULL);
2625 goto error;
2626 }
2627 /* Add node to teardown list of the session. */
2628 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2629
2630 buffer_reg_uid_add(reg_uid);
2631
2632 DBG3("UST app buffer registry per UID created successfully");
2633 end:
2634 if (regp) {
2635 *regp = reg_uid;
2636 }
2637 error:
2638 rcu_read_unlock();
2639 return ret;
2640 }
2641
2642 /*
2643 * Create a session on the tracer side for the given app.
2644 *
2645 * On success, ua_sess_ptr is populated with the session pointer or else left
2646 * untouched. If the session was created, is_created is set to 1. On error,
2647 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2648 * be NULL.
2649 *
2650 * Returns 0 on success or else a negative code which is either -ENOMEM or
2651 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2652 */
2653 static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
2654 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2655 int *is_created)
2656 {
2657 int ret, created = 0;
2658 struct ust_app_session *ua_sess;
2659
2660 LTTNG_ASSERT(usess);
2661 LTTNG_ASSERT(app);
2662 LTTNG_ASSERT(ua_sess_ptr);
2663
2664 health_code_update();
2665
2666 ua_sess = lookup_session_by_app(usess, app);
2667 if (ua_sess == NULL) {
2668 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
2669 app->pid, usess->id);
2670 ua_sess = alloc_ust_app_session();
2671 if (ua_sess == NULL) {
2672 /* Only malloc can failed so something is really wrong */
2673 ret = -ENOMEM;
2674 goto error;
2675 }
2676 shadow_copy_session(ua_sess, usess, app);
2677 created = 1;
2678 }
2679
2680 switch (usess->buffer_type) {
2681 case LTTNG_BUFFER_PER_PID:
2682 /* Init local registry. */
2683 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
2684 if (ret < 0) {
2685 delete_ust_app_session(-1, ua_sess, app);
2686 goto error;
2687 }
2688 break;
2689 case LTTNG_BUFFER_PER_UID:
2690 /* Look for a global registry. If none exists, create one. */
2691 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
2692 if (ret < 0) {
2693 delete_ust_app_session(-1, ua_sess, app);
2694 goto error;
2695 }
2696 break;
2697 default:
2698 abort();
2699 ret = -EINVAL;
2700 goto error;
2701 }
2702
2703 health_code_update();
2704
2705 if (ua_sess->handle == -1) {
2706 pthread_mutex_lock(&app->sock_lock);
2707 ret = lttng_ust_ctl_create_session(app->sock);
2708 pthread_mutex_unlock(&app->sock_lock);
2709 if (ret < 0) {
2710 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2711 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2712 app->pid, app->sock);
2713 ret = 0;
2714 } else if (ret == -EAGAIN) {
2715 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2716 app->pid, app->sock);
2717 ret = 0;
2718 } else {
2719 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2720 ret, app->pid, app->sock);
2721 }
2722 delete_ust_app_session(-1, ua_sess, app);
2723 if (ret != -ENOMEM) {
2724 /*
2725 * Tracer is probably gone or got an internal error so let's
2726 * behave like it will soon unregister or not usable.
2727 */
2728 ret = -ENOTCONN;
2729 }
2730 goto error;
2731 }
2732
2733 ua_sess->handle = ret;
2734
2735 /* Add ust app session to app's HT */
2736 lttng_ht_node_init_u64(&ua_sess->node,
2737 ua_sess->tracing_id);
2738 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
2739 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2740 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2741 &ua_sess->ust_objd_node);
2742
2743 DBG2("UST app session created successfully with handle %d", ret);
2744 }
2745
2746 *ua_sess_ptr = ua_sess;
2747 if (is_created) {
2748 *is_created = created;
2749 }
2750
2751 /* Everything went well. */
2752 ret = 0;
2753
2754 error:
2755 health_code_update();
2756 return ret;
2757 }
2758
2759 /*
2760 * Match function for a hash table lookup of ust_app_ctx.
2761 *
2762 * It matches an ust app context based on the context type and, in the case
2763 * of perf counters, their name.
2764 */
2765 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2766 {
2767 struct ust_app_ctx *ctx;
2768 const struct lttng_ust_context_attr *key;
2769
2770 LTTNG_ASSERT(node);
2771 LTTNG_ASSERT(_key);
2772
2773 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2774 key = (lttng_ust_context_attr *) _key;
2775
2776 /* Context type */
2777 if (ctx->ctx.ctx != key->ctx) {
2778 goto no_match;
2779 }
2780
2781 switch(key->ctx) {
2782 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
2783 if (strncmp(key->u.perf_counter.name,
2784 ctx->ctx.u.perf_counter.name,
2785 sizeof(key->u.perf_counter.name))) {
2786 goto no_match;
2787 }
2788 break;
2789 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
2790 if (strcmp(key->u.app_ctx.provider_name,
2791 ctx->ctx.u.app_ctx.provider_name) ||
2792 strcmp(key->u.app_ctx.ctx_name,
2793 ctx->ctx.u.app_ctx.ctx_name)) {
2794 goto no_match;
2795 }
2796 break;
2797 default:
2798 break;
2799 }
2800
2801 /* Match. */
2802 return 1;
2803
2804 no_match:
2805 return 0;
2806 }
2807
2808 /*
2809 * Lookup for an ust app context from an lttng_ust_context.
2810 *
2811 * Must be called while holding RCU read side lock.
2812 * Return an ust_app_ctx object or NULL on error.
2813 */
2814 static
2815 struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2816 struct lttng_ust_context_attr *uctx)
2817 {
2818 struct lttng_ht_iter iter;
2819 struct lttng_ht_node_ulong *node;
2820 struct ust_app_ctx *app_ctx = NULL;
2821
2822 LTTNG_ASSERT(uctx);
2823 LTTNG_ASSERT(ht);
2824 ASSERT_RCU_READ_LOCKED();
2825
2826 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2827 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2828 ht_match_ust_app_ctx, uctx, &iter.iter);
2829 node = lttng_ht_iter_get_node_ulong(&iter);
2830 if (!node) {
2831 goto end;
2832 }
2833
2834 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2835
2836 end:
2837 return app_ctx;
2838 }
2839
2840 /*
2841 * Create a context for the channel on the tracer.
2842 *
2843 * Called with UST app session lock held and a RCU read side lock.
2844 */
2845 static
2846 int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
2847 struct lttng_ust_context_attr *uctx,
2848 struct ust_app *app)
2849 {
2850 int ret = 0;
2851 struct ust_app_ctx *ua_ctx;
2852
2853 ASSERT_RCU_READ_LOCKED();
2854
2855 DBG2("UST app adding context to channel %s", ua_chan->name);
2856
2857 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2858 if (ua_ctx) {
2859 ret = -EEXIST;
2860 goto error;
2861 }
2862
2863 ua_ctx = alloc_ust_app_ctx(uctx);
2864 if (ua_ctx == NULL) {
2865 /* malloc failed */
2866 ret = -ENOMEM;
2867 goto error;
2868 }
2869
2870 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2871 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2872 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2873
2874 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2875 if (ret < 0) {
2876 goto error;
2877 }
2878
2879 error:
2880 return ret;
2881 }
2882
2883 /*
2884 * Enable on the tracer side a ust app event for the session and channel.
2885 *
2886 * Called with UST app session lock held.
2887 */
2888 static
2889 int enable_ust_app_event(struct ust_app_session *ua_sess,
2890 struct ust_app_event *ua_event, struct ust_app *app)
2891 {
2892 int ret;
2893
2894 ret = enable_ust_object(app, ua_event->obj);
2895 if (ret < 0) {
2896 goto error;
2897 }
2898
2899 ua_event->enabled = 1;
2900
2901 error:
2902 return ret;
2903 }
2904
2905 /*
2906 * Disable on the tracer side a ust app event for the session and channel.
2907 */
2908 static int disable_ust_app_event(struct ust_app_session *ua_sess,
2909 struct ust_app_event *ua_event, struct ust_app *app)
2910 {
2911 int ret;
2912
2913 ret = disable_ust_object(app, ua_event->obj);
2914 if (ret < 0) {
2915 goto error;
2916 }
2917
2918 ua_event->enabled = 0;
2919
2920 error:
2921 return ret;
2922 }
2923
2924 /*
2925 * Lookup ust app channel for session and disable it on the tracer side.
2926 */
2927 static
2928 int disable_ust_app_channel(struct ust_app_session *ua_sess,
2929 struct ust_app_channel *ua_chan, struct ust_app *app)
2930 {
2931 int ret;
2932
2933 ret = disable_ust_channel(app, ua_sess, ua_chan);
2934 if (ret < 0) {
2935 goto error;
2936 }
2937
2938 ua_chan->enabled = 0;
2939
2940 error:
2941 return ret;
2942 }
2943
2944 /*
2945 * Lookup ust app channel for session and enable it on the tracer side. This
2946 * MUST be called with a RCU read side lock acquired.
2947 */
2948 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2949 struct ltt_ust_channel *uchan, struct ust_app *app)
2950 {
2951 int ret = 0;
2952 struct lttng_ht_iter iter;
2953 struct lttng_ht_node_str *ua_chan_node;
2954 struct ust_app_channel *ua_chan;
2955
2956 ASSERT_RCU_READ_LOCKED();
2957
2958 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2959 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2960 if (ua_chan_node == NULL) {
2961 DBG2("Unable to find channel %s in ust session id %" PRIu64,
2962 uchan->name, ua_sess->tracing_id);
2963 goto error;
2964 }
2965
2966 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2967
2968 ret = enable_ust_channel(app, ua_sess, ua_chan);
2969 if (ret < 0) {
2970 goto error;
2971 }
2972
2973 error:
2974 return ret;
2975 }
2976
2977 /*
2978 * Ask the consumer to create a channel and get it if successful.
2979 *
2980 * Called with UST app session lock held.
2981 *
2982 * Return 0 on success or else a negative value.
2983 */
2984 static int do_consumer_create_channel(struct ltt_ust_session *usess,
2985 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2986 int bitness, struct ust_registry_session *registry,
2987 uint64_t trace_archive_id)
2988 {
2989 int ret;
2990 unsigned int nb_fd = 0;
2991 struct consumer_socket *socket;
2992
2993 LTTNG_ASSERT(usess);
2994 LTTNG_ASSERT(ua_sess);
2995 LTTNG_ASSERT(ua_chan);
2996 LTTNG_ASSERT(registry);
2997
2998 rcu_read_lock();
2999 health_code_update();
3000
3001 /* Get the right consumer socket for the application. */
3002 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
3003 if (!socket) {
3004 ret = -EINVAL;
3005 goto error;
3006 }
3007
3008 health_code_update();
3009
3010 /* Need one fd for the channel. */
3011 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3012 if (ret < 0) {
3013 ERR("Exhausted number of available FD upon create channel");
3014 goto error;
3015 }
3016
3017 /*
3018 * Ask consumer to create channel. The consumer will return the number of
3019 * stream we have to expect.
3020 */
3021 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
3022 registry, usess->current_trace_chunk);
3023 if (ret < 0) {
3024 goto error_ask;
3025 }
3026
3027 /*
3028 * Compute the number of fd needed before receiving them. It must be 2 per
3029 * stream (2 being the default value here).
3030 */
3031 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
3032
3033 /* Reserve the amount of file descriptor we need. */
3034 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
3035 if (ret < 0) {
3036 ERR("Exhausted number of available FD upon create channel");
3037 goto error_fd_get_stream;
3038 }
3039
3040 health_code_update();
3041
3042 /*
3043 * Now get the channel from the consumer. This call will populate the stream
3044 * list of that channel and set the ust objects.
3045 */
3046 if (usess->consumer->enabled) {
3047 ret = ust_consumer_get_channel(socket, ua_chan);
3048 if (ret < 0) {
3049 goto error_destroy;
3050 }
3051 }
3052
3053 rcu_read_unlock();
3054 return 0;
3055
3056 error_destroy:
3057 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
3058 error_fd_get_stream:
3059 /*
3060 * Initiate a destroy channel on the consumer since we had an error
3061 * handling it on our side. The return value is of no importance since we
3062 * already have a ret value set by the previous error that we need to
3063 * return.
3064 */
3065 (void) ust_consumer_destroy_channel(socket, ua_chan);
3066 error_ask:
3067 lttng_fd_put(LTTNG_FD_APPS, 1);
3068 error:
3069 health_code_update();
3070 rcu_read_unlock();
3071 return ret;
3072 }
3073
3074 /*
3075 * Duplicate the ust data object of the ust app stream and save it in the
3076 * buffer registry stream.
3077 *
3078 * Return 0 on success or else a negative value.
3079 */
3080 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
3081 struct ust_app_stream *stream)
3082 {
3083 int ret;
3084
3085 LTTNG_ASSERT(reg_stream);
3086 LTTNG_ASSERT(stream);
3087
3088 /* Duplicating a stream requires 2 new fds. Reserve them. */
3089 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
3090 if (ret < 0) {
3091 ERR("Exhausted number of available FD upon duplicate stream");
3092 goto error;
3093 }
3094
3095 /* Duplicate object for stream once the original is in the registry. */
3096 ret = lttng_ust_ctl_duplicate_ust_object_data(&stream->obj,
3097 reg_stream->obj.ust);
3098 if (ret < 0) {
3099 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3100 reg_stream->obj.ust, stream->obj, ret);
3101 lttng_fd_put(LTTNG_FD_APPS, 2);
3102 goto error;
3103 }
3104 stream->handle = stream->obj->handle;
3105
3106 error:
3107 return ret;
3108 }
3109
3110 /*
3111 * Duplicate the ust data object of the ust app. channel and save it in the
3112 * buffer registry channel.
3113 *
3114 * Return 0 on success or else a negative value.
3115 */
3116 static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan,
3117 struct ust_app_channel *ua_chan)
3118 {
3119 int ret;
3120
3121 LTTNG_ASSERT(buf_reg_chan);
3122 LTTNG_ASSERT(ua_chan);
3123
3124 /* Duplicating a channel requires 1 new fd. Reserve it. */
3125 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3126 if (ret < 0) {
3127 ERR("Exhausted number of available FD upon duplicate channel");
3128 goto error_fd_get;
3129 }
3130
3131 /* Duplicate object for stream once the original is in the registry. */
3132 ret = lttng_ust_ctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust);
3133 if (ret < 0) {
3134 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3135 buf_reg_chan->obj.ust, ua_chan->obj, ret);
3136 goto error;
3137 }
3138 ua_chan->handle = ua_chan->obj->handle;
3139
3140 return 0;
3141
3142 error:
3143 lttng_fd_put(LTTNG_FD_APPS, 1);
3144 error_fd_get:
3145 return ret;
3146 }
3147
3148 /*
3149 * For a given channel buffer registry, setup all streams of the given ust
3150 * application channel.
3151 *
3152 * Return 0 on success or else a negative value.
3153 */
3154 static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan,
3155 struct ust_app_channel *ua_chan,
3156 struct ust_app *app)
3157 {
3158 int ret = 0;
3159 struct ust_app_stream *stream, *stmp;
3160
3161 LTTNG_ASSERT(buf_reg_chan);
3162 LTTNG_ASSERT(ua_chan);
3163
3164 DBG2("UST app setup buffer registry stream");
3165
3166 /* Send all streams to application. */
3167 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
3168 struct buffer_reg_stream *reg_stream;
3169
3170 ret = buffer_reg_stream_create(&reg_stream);
3171 if (ret < 0) {
3172 goto error;
3173 }
3174
3175 /*
3176 * Keep original pointer and nullify it in the stream so the delete
3177 * stream call does not release the object.
3178 */
3179 reg_stream->obj.ust = stream->obj;
3180 stream->obj = NULL;
3181 buffer_reg_stream_add(reg_stream, buf_reg_chan);
3182
3183 /* We don't need the streams anymore. */
3184 cds_list_del(&stream->list);
3185 delete_ust_app_stream(-1, stream, app);
3186 }
3187
3188 error:
3189 return ret;
3190 }
3191
3192 /*
3193 * Create a buffer registry channel for the given session registry and
3194 * application channel object. If regp pointer is valid, it's set with the
3195 * created object. Important, the created object is NOT added to the session
3196 * registry hash table.
3197 *
3198 * Return 0 on success else a negative value.
3199 */
3200 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
3201 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
3202 {
3203 int ret;
3204 struct buffer_reg_channel *buf_reg_chan = NULL;
3205
3206 LTTNG_ASSERT(reg_sess);
3207 LTTNG_ASSERT(ua_chan);
3208
3209 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
3210
3211 /* Create buffer registry channel. */
3212 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan);
3213 if (ret < 0) {
3214 goto error_create;
3215 }
3216 LTTNG_ASSERT(buf_reg_chan);
3217 buf_reg_chan->consumer_key = ua_chan->key;
3218 buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
3219 buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
3220
3221 /* Create and add a channel registry to session. */
3222 ret = ust_registry_channel_add(reg_sess->reg.ust,
3223 ua_chan->tracing_channel_id);
3224 if (ret < 0) {
3225 goto error;
3226 }
3227 buffer_reg_channel_add(reg_sess, buf_reg_chan);
3228
3229 if (regp) {
3230 *regp = buf_reg_chan;
3231 }
3232
3233 return 0;
3234
3235 error:
3236 /* Safe because the registry channel object was not added to any HT. */
3237 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3238 error_create:
3239 return ret;
3240 }
3241
3242 /*
3243 * Setup buffer registry channel for the given session registry and application
3244 * channel object. If regp pointer is valid, it's set with the created object.
3245 *
3246 * Return 0 on success else a negative value.
3247 */
3248 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
3249 struct ust_app_channel *ua_chan, struct buffer_reg_channel *buf_reg_chan,
3250 struct ust_app *app)
3251 {
3252 int ret;
3253
3254 LTTNG_ASSERT(reg_sess);
3255 LTTNG_ASSERT(buf_reg_chan);
3256 LTTNG_ASSERT(ua_chan);
3257 LTTNG_ASSERT(ua_chan->obj);
3258
3259 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
3260
3261 /* Setup all streams for the registry. */
3262 ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app);
3263 if (ret < 0) {
3264 goto error;
3265 }
3266
3267 buf_reg_chan->obj.ust = ua_chan->obj;
3268 ua_chan->obj = NULL;
3269
3270 return 0;
3271
3272 error:
3273 buffer_reg_channel_remove(reg_sess, buf_reg_chan);
3274 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3275 return ret;
3276 }
3277
3278 /*
3279 * Send buffer registry channel to the application.
3280 *
3281 * Return 0 on success else a negative value.
3282 */
3283 static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan,
3284 struct ust_app *app, struct ust_app_session *ua_sess,
3285 struct ust_app_channel *ua_chan)
3286 {
3287 int ret;
3288 struct buffer_reg_stream *reg_stream;
3289
3290 LTTNG_ASSERT(buf_reg_chan);
3291 LTTNG_ASSERT(app);
3292 LTTNG_ASSERT(ua_sess);
3293 LTTNG_ASSERT(ua_chan);
3294
3295 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
3296
3297 ret = duplicate_channel_object(buf_reg_chan, ua_chan);
3298 if (ret < 0) {
3299 goto error;
3300 }
3301
3302 /* Send channel to the application. */
3303 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
3304 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3305 ret = -ENOTCONN; /* Caused by app exiting. */
3306 goto error;
3307 } else if (ret == -EAGAIN) {
3308 /* Caused by timeout. */
3309 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".",
3310 app->pid, ua_chan->name, ua_sess->tracing_id);
3311 /* Treat this the same way as an application that is exiting. */
3312 ret = -ENOTCONN;
3313 goto error;
3314 } else if (ret < 0) {
3315 goto error;
3316 }
3317
3318 health_code_update();
3319
3320 /* Send all streams to application. */
3321 pthread_mutex_lock(&buf_reg_chan->stream_list_lock);
3322 cds_list_for_each_entry(reg_stream, &buf_reg_chan->streams, lnode) {
3323 struct ust_app_stream stream;
3324
3325 ret = duplicate_stream_object(reg_stream, &stream);
3326 if (ret < 0) {
3327 goto error_stream_unlock;
3328 }
3329
3330 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
3331 if (ret < 0) {
3332 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3333 ret = -ENOTCONN; /* Caused by app exiting. */
3334 } else if (ret == -EAGAIN) {
3335 /*
3336 * Caused by timeout.
3337 * Treat this the same way as an application
3338 * that is exiting.
3339 */
3340 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64 "\".",
3341 app->pid, stream.name,
3342 ua_chan->name,
3343 ua_sess->tracing_id);
3344 ret = -ENOTCONN;
3345 }
3346 (void) release_ust_app_stream(-1, &stream, app);
3347 goto error_stream_unlock;
3348 }
3349
3350 /*
3351 * The return value is not important here. This function will output an
3352 * error if needed.
3353 */
3354 (void) release_ust_app_stream(-1, &stream, app);
3355 }
3356 ua_chan->is_sent = 1;
3357
3358 error_stream_unlock:
3359 pthread_mutex_unlock(&buf_reg_chan->stream_list_lock);
3360 error:
3361 return ret;
3362 }
3363
3364 /*
3365 * Create and send to the application the created buffers with per UID buffers.
3366 *
3367 * This MUST be called with a RCU read side lock acquired.
3368 * The session list lock and the session's lock must be acquired.
3369 *
3370 * Return 0 on success else a negative value.
3371 */
3372 static int create_channel_per_uid(struct ust_app *app,
3373 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3374 struct ust_app_channel *ua_chan)
3375 {
3376 int ret;
3377 struct buffer_reg_uid *reg_uid;
3378 struct buffer_reg_channel *buf_reg_chan;
3379 struct ltt_session *session = NULL;
3380 enum lttng_error_code notification_ret;
3381 struct ust_registry_channel *ust_reg_chan;
3382
3383 LTTNG_ASSERT(app);
3384 LTTNG_ASSERT(usess);
3385 LTTNG_ASSERT(ua_sess);
3386 LTTNG_ASSERT(ua_chan);
3387 ASSERT_RCU_READ_LOCKED();
3388
3389 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
3390
3391 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
3392 /*
3393 * The session creation handles the creation of this global registry
3394 * object. If none can be find, there is a code flow problem or a
3395 * teardown race.
3396 */
3397 LTTNG_ASSERT(reg_uid);
3398
3399 buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
3400 reg_uid);
3401 if (buf_reg_chan) {
3402 goto send_channel;
3403 }
3404
3405 /* Create the buffer registry channel object. */
3406 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan);
3407 if (ret < 0) {
3408 ERR("Error creating the UST channel \"%s\" registry instance",
3409 ua_chan->name);
3410 goto error;
3411 }
3412
3413 session = session_find_by_id(ua_sess->tracing_id);
3414 LTTNG_ASSERT(session);
3415 LTTNG_ASSERT(pthread_mutex_trylock(&session->lock));
3416 LTTNG_ASSERT(session_trylock_list());
3417
3418 /*
3419 * Create the buffers on the consumer side. This call populates the
3420 * ust app channel object with all streams and data object.
3421 */
3422 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
3423 app->bits_per_long, reg_uid->registry->reg.ust,
3424 session->most_recent_chunk_id.value);
3425 if (ret < 0) {
3426 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3427 ua_chan->name);
3428
3429 /*
3430 * Let's remove the previously created buffer registry channel so
3431 * it's not visible anymore in the session registry.
3432 */
3433 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
3434 ua_chan->tracing_channel_id, false);
3435 buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan);
3436 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3437 goto error;
3438 }
3439
3440 /*
3441 * Setup the streams and add it to the session registry.
3442 */
3443 ret = setup_buffer_reg_channel(reg_uid->registry,
3444 ua_chan, buf_reg_chan, app);
3445 if (ret < 0) {
3446 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
3447 goto error;
3448 }
3449
3450 /* Notify the notification subsystem of the channel's creation. */
3451 pthread_mutex_lock(&reg_uid->registry->reg.ust->lock);
3452 ust_reg_chan = ust_registry_channel_find(reg_uid->registry->reg.ust,
3453 ua_chan->tracing_channel_id);
3454 LTTNG_ASSERT(ust_reg_chan);
3455 ust_reg_chan->consumer_key = ua_chan->key;
3456 ust_reg_chan = NULL;
3457 pthread_mutex_unlock(&reg_uid->registry->reg.ust->lock);
3458
3459 notification_ret = notification_thread_command_add_channel(
3460 the_notification_thread_handle, session->name,
3461 lttng_credentials_get_uid(
3462 &ua_sess->effective_credentials),
3463 lttng_credentials_get_gid(
3464 &ua_sess->effective_credentials),
3465 ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST,
3466 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3467 if (notification_ret != LTTNG_OK) {
3468 ret = - (int) notification_ret;
3469 ERR("Failed to add channel to notification thread");
3470 goto error;
3471 }
3472
3473 send_channel:
3474 /* Send buffers to the application. */
3475 ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan);
3476 if (ret < 0) {
3477 if (ret != -ENOTCONN) {
3478 ERR("Error sending channel to application");
3479 }
3480 goto error;
3481 }
3482
3483 error:
3484 if (session) {
3485 session_put(session);
3486 }
3487 return ret;
3488 }
3489
3490 /*
3491 * Create and send to the application the created buffers with per PID buffers.
3492 *
3493 * Called with UST app session lock held.
3494 * The session list lock and the session's lock must be acquired.
3495 *
3496 * Return 0 on success else a negative value.
3497 */
3498 static int create_channel_per_pid(struct ust_app *app,
3499 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3500 struct ust_app_channel *ua_chan)
3501 {
3502 int ret;
3503 struct ust_registry_session *registry;
3504 enum lttng_error_code cmd_ret;
3505 struct ltt_session *session = NULL;
3506 uint64_t chan_reg_key;
3507 struct ust_registry_channel *ust_reg_chan;
3508
3509 LTTNG_ASSERT(app);
3510 LTTNG_ASSERT(usess);
3511 LTTNG_ASSERT(ua_sess);
3512 LTTNG_ASSERT(ua_chan);
3513
3514 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
3515
3516 rcu_read_lock();
3517
3518 registry = get_session_registry(ua_sess);
3519 /* The UST app session lock is held, registry shall not be null. */
3520 LTTNG_ASSERT(registry);
3521
3522 /* Create and add a new channel registry to session. */
3523 ret = ust_registry_channel_add(registry, ua_chan->key);
3524 if (ret < 0) {
3525 ERR("Error creating the UST channel \"%s\" registry instance",
3526 ua_chan->name);
3527 goto error;
3528 }
3529
3530 session = session_find_by_id(ua_sess->tracing_id);
3531 LTTNG_ASSERT(session);
3532
3533 LTTNG_ASSERT(pthread_mutex_trylock(&session->lock));
3534 LTTNG_ASSERT(session_trylock_list());
3535
3536 /* Create and get channel on the consumer side. */
3537 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
3538 app->bits_per_long, registry,
3539 session->most_recent_chunk_id.value);
3540 if (ret < 0) {
3541 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3542 ua_chan->name);
3543 goto error_remove_from_registry;
3544 }
3545
3546 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
3547 if (ret < 0) {
3548 if (ret != -ENOTCONN) {
3549 ERR("Error sending channel to application");
3550 }
3551 goto error_remove_from_registry;
3552 }
3553
3554 chan_reg_key = ua_chan->key;
3555 pthread_mutex_lock(&registry->lock);
3556 ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key);
3557 LTTNG_ASSERT(ust_reg_chan);
3558 ust_reg_chan->consumer_key = ua_chan->key;
3559 pthread_mutex_unlock(&registry->lock);
3560
3561 cmd_ret = notification_thread_command_add_channel(
3562 the_notification_thread_handle, session->name,
3563 lttng_credentials_get_uid(
3564 &ua_sess->effective_credentials),
3565 lttng_credentials_get_gid(
3566 &ua_sess->effective_credentials),
3567 ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST,
3568 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3569 if (cmd_ret != LTTNG_OK) {
3570 ret = - (int) cmd_ret;
3571 ERR("Failed to add channel to notification thread");
3572 goto error_remove_from_registry;
3573 }
3574
3575 error_remove_from_registry:
3576 if (ret) {
3577 ust_registry_channel_del_free(registry, ua_chan->key, false);
3578 }
3579 error:
3580 rcu_read_unlock();
3581 if (session) {
3582 session_put(session);
3583 }
3584 return ret;
3585 }
3586
3587 /*
3588 * From an already allocated ust app channel, create the channel buffers if
3589 * needed and send them to the application. This MUST be called with a RCU read
3590 * side lock acquired.
3591 *
3592 * Called with UST app session lock held.
3593 *
3594 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3595 * the application exited concurrently.
3596 */
3597 static int ust_app_channel_send(struct ust_app *app,
3598 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3599 struct ust_app_channel *ua_chan)
3600 {
3601 int ret;
3602
3603 LTTNG_ASSERT(app);
3604 LTTNG_ASSERT(usess);
3605 LTTNG_ASSERT(usess->active);
3606 LTTNG_ASSERT(ua_sess);
3607 LTTNG_ASSERT(ua_chan);
3608 ASSERT_RCU_READ_LOCKED();
3609
3610 /* Handle buffer type before sending the channel to the application. */
3611 switch (usess->buffer_type) {
3612 case LTTNG_BUFFER_PER_UID:
3613 {
3614 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3615 if (ret < 0) {
3616 goto error;
3617 }
3618 break;
3619 }
3620 case LTTNG_BUFFER_PER_PID:
3621 {
3622 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3623 if (ret < 0) {
3624 goto error;
3625 }
3626 break;
3627 }
3628 default:
3629 abort();
3630 ret = -EINVAL;
3631 goto error;
3632 }
3633
3634 /* Initialize ust objd object using the received handle and add it. */
3635 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3636 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
3637
3638 /* If channel is not enabled, disable it on the tracer */
3639 if (!ua_chan->enabled) {
3640 ret = disable_ust_channel(app, ua_sess, ua_chan);
3641 if (ret < 0) {
3642 goto error;
3643 }
3644 }
3645
3646 error:
3647 return ret;
3648 }
3649
3650 /*
3651 * Create UST app channel and return it through ua_chanp if not NULL.
3652 *
3653 * Called with UST app session lock and RCU read-side lock held.
3654 *
3655 * Return 0 on success or else a negative value.
3656 */
3657 static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3658 struct ltt_ust_channel *uchan,
3659 enum lttng_ust_abi_chan_type type, struct ltt_ust_session *usess,
3660 struct ust_app_channel **ua_chanp)
3661 {
3662 int ret = 0;
3663 struct lttng_ht_iter iter;
3664 struct lttng_ht_node_str *ua_chan_node;
3665 struct ust_app_channel *ua_chan;
3666
3667 ASSERT_RCU_READ_LOCKED();
3668
3669 /* Lookup channel in the ust app session */
3670 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3671 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3672 if (ua_chan_node != NULL) {
3673 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3674 goto end;
3675 }
3676
3677 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
3678 if (ua_chan == NULL) {
3679 /* Only malloc can fail here */
3680 ret = -ENOMEM;
3681 goto error;
3682 }
3683 shadow_copy_channel(ua_chan, uchan);
3684
3685 /* Set channel type. */
3686 ua_chan->attr.type = type;
3687
3688 /* Only add the channel if successful on the tracer side. */
3689 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
3690 end:
3691 if (ua_chanp) {
3692 *ua_chanp = ua_chan;
3693 }
3694
3695 /* Everything went well. */
3696 return 0;
3697
3698 error:
3699 return ret;
3700 }
3701
3702 /*
3703 * Create UST app event and create it on the tracer side.
3704 *
3705 * Must be called with the RCU read side lock held.
3706 * Called with ust app session mutex held.
3707 */
3708 static
3709 int create_ust_app_event(struct ust_app_session *ua_sess,
3710 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3711 struct ust_app *app)
3712 {
3713 int ret = 0;
3714 struct ust_app_event *ua_event;
3715
3716 ASSERT_RCU_READ_LOCKED();
3717
3718 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3719 if (ua_event == NULL) {
3720 /* Only failure mode of alloc_ust_app_event(). */
3721 ret = -ENOMEM;
3722 goto end;
3723 }
3724 shadow_copy_event(ua_event, uevent);
3725
3726 /* Create it on the tracer side */
3727 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
3728 if (ret < 0) {
3729 /*
3730 * Not found previously means that it does not exist on the
3731 * tracer. If the application reports that the event existed,
3732 * it means there is a bug in the sessiond or lttng-ust
3733 * (or corruption, etc.)
3734 */
3735 if (ret == -LTTNG_UST_ERR_EXIST) {
3736 ERR("Tracer for application reported that an event being created already existed: "
3737 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3738 uevent->attr.name,
3739 app->pid, app->ppid, app->uid,
3740 app->gid);
3741 }
3742 goto error;
3743 }
3744
3745 add_unique_ust_app_event(ua_chan, ua_event);
3746
3747 DBG2("UST app create event completed: app = '%s' pid = %d",
3748 app->name, app->pid);
3749
3750 end:
3751 return ret;
3752
3753 error:
3754 /* Valid. Calling here is already in a read side lock */
3755 delete_ust_app_event(-1, ua_event, app);
3756 return ret;
3757 }
3758
3759 /*
3760 * Create UST app event notifier rule and create it on the tracer side.
3761 *
3762 * Must be called with the RCU read side lock held.
3763 * Called with ust app session mutex held.
3764 */
3765 static
3766 int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger,
3767 struct ust_app *app)
3768 {
3769 int ret = 0;
3770 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
3771
3772 ASSERT_RCU_READ_LOCKED();
3773
3774 ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger);
3775 if (ua_event_notifier_rule == NULL) {
3776 ret = -ENOMEM;
3777 goto end;
3778 }
3779
3780 /* Create it on the tracer side. */
3781 ret = create_ust_event_notifier(app, ua_event_notifier_rule);
3782 if (ret < 0) {
3783 /*
3784 * Not found previously means that it does not exist on the
3785 * tracer. If the application reports that the event existed,
3786 * it means there is a bug in the sessiond or lttng-ust
3787 * (or corruption, etc.)
3788 */
3789 if (ret == -LTTNG_UST_ERR_EXIST) {
3790 ERR("Tracer for application reported that an event notifier being created already exists: "
3791 "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d",
3792 lttng_trigger_get_tracer_token(trigger),
3793 app->pid, app->ppid, app->uid,
3794 app->gid);
3795 }
3796 goto error;
3797 }
3798
3799 lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht,
3800 &ua_event_notifier_rule->node);
3801
3802 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64,
3803 app->name, app->pid, lttng_trigger_get_tracer_token(trigger));
3804
3805 goto end;
3806
3807 error:
3808 /* The RCU read side lock is already being held by the caller. */
3809 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app);
3810 end:
3811 return ret;
3812 }
3813
3814 /*
3815 * Create UST metadata and open it on the tracer side.
3816 *
3817 * Called with UST app session lock held and RCU read side lock.
3818 */
3819 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
3820 struct ust_app *app, struct consumer_output *consumer)
3821 {
3822 int ret = 0;
3823 struct ust_app_channel *metadata;
3824 struct consumer_socket *socket;
3825 struct ust_registry_session *registry;
3826 struct ltt_session *session = NULL;
3827
3828 LTTNG_ASSERT(ua_sess);
3829 LTTNG_ASSERT(app);
3830 LTTNG_ASSERT(consumer);
3831 ASSERT_RCU_READ_LOCKED();
3832
3833 registry = get_session_registry(ua_sess);
3834 /* The UST app session is held registry shall not be null. */
3835 LTTNG_ASSERT(registry);
3836
3837 pthread_mutex_lock(&registry->lock);
3838
3839 /* Metadata already exists for this registry or it was closed previously */
3840 if (registry->metadata_key || registry->metadata_closed) {
3841 ret = 0;
3842 goto error;
3843 }
3844
3845 /* Allocate UST metadata */
3846 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
3847 if (!metadata) {
3848 /* malloc() failed */
3849 ret = -ENOMEM;
3850 goto error;
3851 }
3852
3853 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
3854
3855 /* Need one fd for the channel. */
3856 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3857 if (ret < 0) {
3858 ERR("Exhausted number of available FD upon create metadata");
3859 goto error;
3860 }
3861
3862 /* Get the right consumer socket for the application. */
3863 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3864 if (!socket) {
3865 ret = -EINVAL;
3866 goto error_consumer;
3867 }
3868
3869 /*
3870 * Keep metadata key so we can identify it on the consumer side. Assign it
3871 * to the registry *before* we ask the consumer so we avoid the race of the
3872 * consumer requesting the metadata and the ask_channel call on our side
3873 * did not returned yet.
3874 */
3875 registry->metadata_key = metadata->key;
3876
3877 session = session_find_by_id(ua_sess->tracing_id);
3878 LTTNG_ASSERT(session);
3879
3880 LTTNG_ASSERT(pthread_mutex_trylock(&session->lock));
3881 LTTNG_ASSERT(session_trylock_list());
3882
3883 /*
3884 * Ask the metadata channel creation to the consumer. The metadata object
3885 * will be created by the consumer and kept their. However, the stream is
3886 * never added or monitored until we do a first push metadata to the
3887 * consumer.
3888 */
3889 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
3890 registry, session->current_trace_chunk);
3891 if (ret < 0) {
3892 /* Nullify the metadata key so we don't try to close it later on. */
3893 registry->metadata_key = 0;
3894 goto error_consumer;
3895 }
3896
3897 /*
3898 * The setup command will make the metadata stream be sent to the relayd,
3899 * if applicable, and the thread managing the metadatas. This is important
3900 * because after this point, if an error occurs, the only way the stream
3901 * can be deleted is to be monitored in the consumer.
3902 */
3903 ret = consumer_setup_metadata(socket, metadata->key);
3904 if (ret < 0) {
3905 /* Nullify the metadata key so we don't try to close it later on. */
3906 registry->metadata_key = 0;
3907 goto error_consumer;
3908 }
3909
3910 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3911 metadata->key, app->pid);
3912
3913 error_consumer:
3914 lttng_fd_put(LTTNG_FD_APPS, 1);
3915 delete_ust_app_channel(-1, metadata, app);
3916 error:
3917 pthread_mutex_unlock(&registry->lock);
3918 if (session) {
3919 session_put(session);
3920 }
3921 return ret;
3922 }
3923
3924 /*
3925 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3926 * acquired before calling this function.
3927 */
3928 struct ust_app *ust_app_find_by_pid(pid_t pid)
3929 {
3930 struct ust_app *app = NULL;
3931 struct lttng_ht_node_ulong *node;
3932 struct lttng_ht_iter iter;
3933
3934 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3935 node = lttng_ht_iter_get_node_ulong(&iter);
3936 if (node == NULL) {
3937 DBG2("UST app no found with pid %d", pid);
3938 goto error;
3939 }
3940
3941 DBG2("Found UST app by pid %d", pid);
3942
3943 app = caa_container_of(node, struct ust_app, pid_n);
3944
3945 error:
3946 return app;
3947 }
3948
3949 /*
3950 * Allocate and init an UST app object using the registration information and
3951 * the command socket. This is called when the command socket connects to the
3952 * session daemon.
3953 *
3954 * The object is returned on success or else NULL.
3955 */
3956 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
3957 {
3958 int ret;
3959 struct ust_app *lta = NULL;
3960 struct lttng_pipe *event_notifier_event_source_pipe = NULL;
3961
3962 LTTNG_ASSERT(msg);
3963 LTTNG_ASSERT(sock >= 0);
3964
3965 DBG3("UST app creating application for socket %d", sock);
3966
3967 if ((msg->bits_per_long == 64 &&
3968 (uatomic_read(&the_ust_consumerd64_fd) ==
3969 -EINVAL)) ||
3970 (msg->bits_per_long == 32 &&
3971 (uatomic_read(&the_ust_consumerd32_fd) ==
3972 -EINVAL))) {
3973 ERR("Registration failed: application \"%s\" (pid: %d) has "
3974 "%d-bit long, but no consumerd for this size is available.\n",
3975 msg->name, msg->pid, msg->bits_per_long);
3976 goto error;
3977 }
3978
3979 /*
3980 * Reserve the two file descriptors of the event source pipe. The write
3981 * end will be closed once it is passed to the application, at which
3982 * point a single 'put' will be performed.
3983 */
3984 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
3985 if (ret) {
3986 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3987 msg->name, (int) msg->pid);
3988 goto error;
3989 }
3990
3991 event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC);
3992 if (!event_notifier_event_source_pipe) {
3993 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3994 msg->name, msg->pid);
3995 goto error;
3996 }
3997
3998 lta = (ust_app *) zmalloc(sizeof(struct ust_app));
3999 if (lta == NULL) {
4000 PERROR("malloc");
4001 goto error_free_pipe;
4002 }
4003
4004 lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe;
4005
4006 lta->ppid = msg->ppid;
4007 lta->uid = msg->uid;
4008 lta->gid = msg->gid;
4009
4010 lta->bits_per_long = msg->bits_per_long;
4011 lta->uint8_t_alignment = msg->uint8_t_alignment;
4012 lta->uint16_t_alignment = msg->uint16_t_alignment;
4013 lta->uint32_t_alignment = msg->uint32_t_alignment;
4014 lta->uint64_t_alignment = msg->uint64_t_alignment;
4015 lta->long_alignment = msg->long_alignment;
4016 lta->byte_order = msg->byte_order;
4017
4018 lta->v_major = msg->major;
4019 lta->v_minor = msg->minor;
4020 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4021 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4022 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4023 lta->notify_sock = -1;
4024 lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4025
4026 /* Copy name and make sure it's NULL terminated. */
4027 strncpy(lta->name, msg->name, sizeof(lta->name));
4028 lta->name[UST_APP_PROCNAME_LEN] = '\0';
4029
4030 /*
4031 * Before this can be called, when receiving the registration information,
4032 * the application compatibility is checked. So, at this point, the
4033 * application can work with this session daemon.
4034 */
4035 lta->compatible = 1;
4036
4037 lta->pid = msg->pid;
4038 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
4039 lta->sock = sock;
4040 pthread_mutex_init(&lta->sock_lock, NULL);
4041 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
4042
4043 CDS_INIT_LIST_HEAD(&lta->teardown_head);
4044 return lta;
4045
4046 error_free_pipe:
4047 lttng_pipe_destroy(event_notifier_event_source_pipe);
4048 lttng_fd_put(LTTNG_FD_APPS, 2);
4049 error:
4050 return NULL;
4051 }
4052
4053 /*
4054 * For a given application object, add it to every hash table.
4055 */
4056 void ust_app_add(struct ust_app *app)
4057 {
4058 LTTNG_ASSERT(app);
4059 LTTNG_ASSERT(app->notify_sock >= 0);
4060
4061 app->registration_time = time(NULL);
4062
4063 rcu_read_lock();
4064
4065 /*
4066 * On a re-registration, we want to kick out the previous registration of
4067 * that pid
4068 */
4069 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
4070
4071 /*
4072 * The socket _should_ be unique until _we_ call close. So, a add_unique
4073 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4074 * already in the table.
4075 */
4076 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
4077
4078 /* Add application to the notify socket hash table. */
4079 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
4080 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
4081
4082 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4083 "notify_sock =%d (version %d.%d)", app->pid, app->ppid, app->uid,
4084 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
4085 app->v_minor);
4086
4087 rcu_read_unlock();
4088 }
4089
4090 /*
4091 * Set the application version into the object.
4092 *
4093 * Return 0 on success else a negative value either an errno code or a
4094 * LTTng-UST error code.
4095 */
4096 int ust_app_version(struct ust_app *app)
4097 {
4098 int ret;
4099
4100 LTTNG_ASSERT(app);
4101
4102 pthread_mutex_lock(&app->sock_lock);
4103 ret = lttng_ust_ctl_tracer_version(app->sock, &app->version);
4104 pthread_mutex_unlock(&app->sock_lock);
4105 if (ret < 0) {
4106 if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) {
4107 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4108 app->pid, app->sock);
4109 } else if (ret == -EAGAIN) {
4110 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4111 app->pid, app->sock);
4112 } else {
4113 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4114 ret, app->pid, app->sock);
4115 }
4116 }
4117
4118 return ret;
4119 }
4120
4121 bool ust_app_supports_notifiers(const struct ust_app *app)
4122 {
4123 return app->v_major >= 9;
4124 }
4125
4126 bool ust_app_supports_counters(const struct ust_app *app)
4127 {
4128 return app->v_major >= 9;
4129 }
4130
4131 /*
4132 * Setup the base event notifier group.
4133 *
4134 * Return 0 on success else a negative value either an errno code or a
4135 * LTTng-UST error code.
4136 */
4137 int ust_app_setup_event_notifier_group(struct ust_app *app)
4138 {
4139 int ret;
4140 int event_pipe_write_fd;
4141 struct lttng_ust_abi_object_data *event_notifier_group = NULL;
4142 enum lttng_error_code lttng_ret;
4143 enum event_notifier_error_accounting_status event_notifier_error_accounting_status;
4144
4145 LTTNG_ASSERT(app);
4146
4147 if (!ust_app_supports_notifiers(app)) {
4148 ret = -ENOSYS;
4149 goto error;
4150 }
4151
4152 /* Get the write side of the pipe. */
4153 event_pipe_write_fd = lttng_pipe_get_writefd(
4154 app->event_notifier_group.event_pipe);
4155
4156 pthread_mutex_lock(&app->sock_lock);
4157 ret = lttng_ust_ctl_create_event_notifier_group(app->sock,
4158 event_pipe_write_fd, &event_notifier_group);
4159 pthread_mutex_unlock(&app->sock_lock);
4160 if (ret < 0) {
4161 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4162 ret = 0;
4163 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4164 app->pid, app->sock);
4165 } else if (ret == -EAGAIN) {
4166 ret = 0;
4167 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4168 app->pid, app->sock);
4169 } else {
4170 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4171 ret, app->pid, app->sock, event_pipe_write_fd);
4172 }
4173 goto error;
4174 }
4175
4176 ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe);
4177 if (ret) {
4178 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4179 app->name, app->pid);
4180 goto error;
4181 }
4182
4183 /*
4184 * Release the file descriptor that was reserved for the write-end of
4185 * the pipe.
4186 */
4187 lttng_fd_put(LTTNG_FD_APPS, 1);
4188
4189 lttng_ret = notification_thread_command_add_tracer_event_source(
4190 the_notification_thread_handle,
4191 lttng_pipe_get_readfd(
4192 app->event_notifier_group.event_pipe),
4193 LTTNG_DOMAIN_UST);
4194 if (lttng_ret != LTTNG_OK) {
4195 ERR("Failed to add tracer event source to notification thread");
4196 ret = - 1;
4197 goto error;
4198 }
4199
4200 /* Assign handle only when the complete setup is valid. */
4201 app->event_notifier_group.object = event_notifier_group;
4202
4203 event_notifier_error_accounting_status =
4204 event_notifier_error_accounting_register_app(app);
4205 switch (event_notifier_error_accounting_status) {
4206 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK:
4207 break;
4208 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED:
4209 DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app pid = %d",
4210 app->sock, app->name, (int) app->pid);
4211 ret = 0;
4212 goto error_accounting;
4213 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD:
4214 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4215 app->sock, app->name, (int) app->pid);
4216 ret = 0;
4217 goto error_accounting;
4218 default:
4219 ERR("Failed to setup event notifier error accounting for app");
4220 ret = -1;
4221 goto error_accounting;
4222 }
4223
4224 return ret;
4225
4226 error_accounting:
4227 lttng_ret = notification_thread_command_remove_tracer_event_source(
4228 the_notification_thread_handle,
4229 lttng_pipe_get_readfd(
4230 app->event_notifier_group.event_pipe));
4231 if (lttng_ret != LTTNG_OK) {
4232 ERR("Failed to remove application tracer event source from notification thread");
4233 }
4234
4235 error:
4236 lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object);
4237 free(app->event_notifier_group.object);
4238 app->event_notifier_group.object = NULL;
4239 return ret;
4240 }
4241
4242 /*
4243 * Unregister app by removing it from the global traceable app list and freeing
4244 * the data struct.
4245 *
4246 * The socket is already closed at this point so no close to sock.
4247 */
4248 void ust_app_unregister(int sock)
4249 {
4250 struct ust_app *lta;
4251 struct lttng_ht_node_ulong *node;
4252 struct lttng_ht_iter ust_app_sock_iter;
4253 struct lttng_ht_iter iter;
4254 struct ust_app_session *ua_sess;
4255 int ret;
4256
4257 rcu_read_lock();
4258
4259 /* Get the node reference for a call_rcu */
4260 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
4261 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
4262 LTTNG_ASSERT(node);
4263
4264 lta = caa_container_of(node, struct ust_app, sock_n);
4265 DBG("PID %d unregistering with sock %d", lta->pid, sock);
4266
4267 /*
4268 * For per-PID buffers, perform "push metadata" and flush all
4269 * application streams before removing app from hash tables,
4270 * ensuring proper behavior of data_pending check.
4271 * Remove sessions so they are not visible during deletion.
4272 */
4273 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
4274 node.node) {
4275 struct ust_registry_session *registry;
4276
4277 ret = lttng_ht_del(lta->sessions, &iter);
4278 if (ret) {
4279 /* The session was already removed so scheduled for teardown. */
4280 continue;
4281 }
4282
4283 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
4284 (void) ust_app_flush_app_session(lta, ua_sess);
4285 }
4286
4287 /*
4288 * Add session to list for teardown. This is safe since at this point we
4289 * are the only one using this list.
4290 */
4291 pthread_mutex_lock(&ua_sess->lock);
4292
4293 if (ua_sess->deleted) {
4294 pthread_mutex_unlock(&ua_sess->lock);
4295 continue;
4296 }
4297
4298 /*
4299 * Normally, this is done in the delete session process which is
4300 * executed in the call rcu below. However, upon registration we can't
4301 * afford to wait for the grace period before pushing data or else the
4302 * data pending feature can race between the unregistration and stop
4303 * command where the data pending command is sent *before* the grace
4304 * period ended.
4305 *
4306 * The close metadata below nullifies the metadata pointer in the
4307 * session so the delete session will NOT push/close a second time.
4308 */
4309 registry = get_session_registry(ua_sess);
4310 if (registry) {
4311 /* Push metadata for application before freeing the application. */
4312 (void) push_metadata(registry, ua_sess->consumer);
4313
4314 /*
4315 * Don't ask to close metadata for global per UID buffers. Close
4316 * metadata only on destroy trace session in this case. Also, the
4317 * previous push metadata could have flag the metadata registry to
4318 * close so don't send a close command if closed.
4319 */
4320 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
4321 /* And ask to close it for this session registry. */
4322 (void) close_metadata(registry, ua_sess->consumer);
4323 }
4324 }
4325 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
4326
4327 pthread_mutex_unlock(&ua_sess->lock);
4328 }
4329
4330 /* Remove application from PID hash table */
4331 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
4332 LTTNG_ASSERT(!ret);
4333
4334 /*
4335 * Remove application from notify hash table. The thread handling the
4336 * notify socket could have deleted the node so ignore on error because
4337 * either way it's valid. The close of that socket is handled by the
4338 * apps_notify_thread.
4339 */
4340 iter.iter.node = &lta->notify_sock_n.node;
4341 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
4342
4343 /*
4344 * Ignore return value since the node might have been removed before by an
4345 * add replace during app registration because the PID can be reassigned by
4346 * the OS.
4347 */
4348 iter.iter.node = &lta->pid_n.node;
4349 ret = lttng_ht_del(ust_app_ht, &iter);
4350 if (ret) {
4351 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4352 lta->pid);
4353 }
4354
4355 /* Free memory */
4356 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
4357
4358 rcu_read_unlock();
4359 return;
4360 }
4361
4362 /*
4363 * Fill events array with all events name of all registered apps.
4364 */
4365 int ust_app_list_events(struct lttng_event **events)
4366 {
4367 int ret, handle;
4368 size_t nbmem, count = 0;
4369 struct lttng_ht_iter iter;
4370 struct ust_app *app;
4371 struct lttng_event *tmp_event;
4372
4373 nbmem = UST_APP_EVENT_LIST_SIZE;
4374 tmp_event = (lttng_event *) zmalloc(nbmem * sizeof(struct lttng_event));
4375 if (tmp_event == NULL) {
4376 PERROR("zmalloc ust app events");
4377 ret = -ENOMEM;
4378 goto error;
4379 }
4380
4381 rcu_read_lock();
4382
4383 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4384 struct lttng_ust_abi_tracepoint_iter uiter;
4385
4386 health_code_update();
4387
4388 if (!app->compatible) {
4389 /*
4390 * TODO: In time, we should notice the caller of this error by
4391 * telling him that this is a version error.
4392 */
4393 continue;
4394 }
4395 pthread_mutex_lock(&app->sock_lock);
4396 handle = lttng_ust_ctl_tracepoint_list(app->sock);
4397 if (handle < 0) {
4398 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4399 ERR("UST app list events getting handle failed for app pid %d",
4400 app->pid);
4401 }
4402 pthread_mutex_unlock(&app->sock_lock);
4403 continue;
4404 }
4405
4406 while ((ret = lttng_ust_ctl_tracepoint_list_get(app->sock, handle,
4407 &uiter)) != -LTTNG_UST_ERR_NOENT) {
4408 /* Handle ustctl error. */
4409 if (ret < 0) {
4410 int release_ret;
4411
4412 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4413 ERR("UST app tp list get failed for app %d with ret %d",
4414 app->sock, ret);
4415 } else {
4416 DBG3("UST app tp list get failed. Application is dead");
4417 break;
4418 }
4419 free(tmp_event);
4420 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4421 if (release_ret < 0 &&
4422 release_ret != -LTTNG_UST_ERR_EXITING &&
4423 release_ret != -EPIPE) {
4424 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4425 }
4426 pthread_mutex_unlock(&app->sock_lock);
4427 goto rcu_error;
4428 }
4429
4430 health_code_update();
4431 if (count >= nbmem) {
4432 /* In case the realloc fails, we free the memory */
4433 struct lttng_event *new_tmp_event;
4434 size_t new_nbmem;
4435
4436 new_nbmem = nbmem << 1;
4437 DBG2("Reallocating event list from %zu to %zu entries",
4438 nbmem, new_nbmem);
4439 new_tmp_event = (lttng_event *) realloc(tmp_event,
4440 new_nbmem * sizeof(struct lttng_event));
4441 if (new_tmp_event == NULL) {
4442 int release_ret;
4443
4444 PERROR("realloc ust app events");
4445 free(tmp_event);
4446 ret = -ENOMEM;
4447 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4448 if (release_ret < 0 &&
4449 release_ret != -LTTNG_UST_ERR_EXITING &&
4450 release_ret != -EPIPE) {
4451 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4452 }
4453 pthread_mutex_unlock(&app->sock_lock);
4454 goto rcu_error;
4455 }
4456 /* Zero the new memory */
4457 memset(new_tmp_event + nbmem, 0,
4458 (new_nbmem - nbmem) * sizeof(struct lttng_event));
4459 nbmem = new_nbmem;
4460 tmp_event = new_tmp_event;
4461 }
4462 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_ABI_SYM_NAME_LEN);
4463 tmp_event[count].loglevel = uiter.loglevel;
4464 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT;
4465 tmp_event[count].pid = app->pid;
4466 tmp_event[count].enabled = -1;
4467 count++;
4468 }
4469 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4470 pthread_mutex_unlock(&app->sock_lock);
4471 if (ret < 0) {
4472 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4473 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4474 app->pid, app->sock);
4475 } else if (ret == -EAGAIN) {
4476 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4477 app->pid, app->sock);
4478 } else {
4479 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4480 ret, app->pid, app->sock);
4481 }
4482 }
4483 }
4484
4485 ret = count;
4486 *events = tmp_event;
4487
4488 DBG2("UST app list events done (%zu events)", count);
4489
4490 rcu_error:
4491 rcu_read_unlock();
4492 error:
4493 health_code_update();
4494 return ret;
4495 }
4496
4497 /*
4498 * Fill events array with all events name of all registered apps.
4499 */
4500 int ust_app_list_event_fields(struct lttng_event_field **fields)
4501 {
4502 int ret, handle;
4503 size_t nbmem, count = 0;
4504 struct lttng_ht_iter iter;
4505 struct ust_app *app;
4506 struct lttng_event_field *tmp_event;
4507
4508 nbmem = UST_APP_EVENT_LIST_SIZE;
4509 tmp_event = (lttng_event_field *) zmalloc(nbmem * sizeof(struct lttng_event_field));
4510 if (tmp_event == NULL) {
4511 PERROR("zmalloc ust app event fields");
4512 ret = -ENOMEM;
4513 goto error;
4514 }
4515
4516 rcu_read_lock();
4517
4518 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4519 struct lttng_ust_abi_field_iter uiter;
4520
4521 health_code_update();
4522
4523 if (!app->compatible) {
4524 /*
4525 * TODO: In time, we should notice the caller of this error by
4526 * telling him that this is a version error.
4527 */
4528 continue;
4529 }
4530 pthread_mutex_lock(&app->sock_lock);
4531 handle = lttng_ust_ctl_tracepoint_field_list(app->sock);
4532 if (handle < 0) {
4533 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4534 ERR("UST app list field getting handle failed for app pid %d",
4535 app->pid);
4536 }
4537 pthread_mutex_unlock(&app->sock_lock);
4538 continue;
4539 }
4540
4541 while ((ret = lttng_ust_ctl_tracepoint_field_list_get(app->sock, handle,
4542 &uiter)) != -LTTNG_UST_ERR_NOENT) {
4543 /* Handle ustctl error. */
4544 if (ret < 0) {
4545 int release_ret;
4546
4547 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4548 ERR("UST app tp list field failed for app %d with ret %d",
4549 app->sock, ret);
4550 } else {
4551 DBG3("UST app tp list field failed. Application is dead");
4552 break;
4553 }
4554 free(tmp_event);
4555 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4556 pthread_mutex_unlock(&app->sock_lock);
4557 if (release_ret < 0 &&
4558 release_ret != -LTTNG_UST_ERR_EXITING &&
4559 release_ret != -EPIPE) {
4560 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4561 }
4562 goto rcu_error;
4563 }
4564
4565 health_code_update();
4566 if (count >= nbmem) {
4567 /* In case the realloc fails, we free the memory */
4568 struct lttng_event_field *new_tmp_event;
4569 size_t new_nbmem;
4570
4571 new_nbmem = nbmem << 1;
4572 DBG2("Reallocating event field list from %zu to %zu entries",
4573 nbmem, new_nbmem);
4574 new_tmp_event = (lttng_event_field *) realloc(tmp_event,
4575 new_nbmem * sizeof(struct lttng_event_field));
4576 if (new_tmp_event == NULL) {
4577 int release_ret;
4578
4579 PERROR("realloc ust app event fields");
4580 free(tmp_event);
4581 ret = -ENOMEM;
4582 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4583 pthread_mutex_unlock(&app->sock_lock);
4584 if (release_ret &&
4585 release_ret != -LTTNG_UST_ERR_EXITING &&
4586 release_ret != -EPIPE) {
4587 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4588 }
4589 goto rcu_error;
4590 }
4591 /* Zero the new memory */
4592 memset(new_tmp_event + nbmem, 0,
4593 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
4594 nbmem = new_nbmem;
4595 tmp_event = new_tmp_event;
4596 }
4597
4598 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
4599 /* Mapping between these enums matches 1 to 1. */
4600 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
4601 tmp_event[count].nowrite = uiter.nowrite;
4602
4603 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_ABI_SYM_NAME_LEN);
4604 tmp_event[count].event.loglevel = uiter.loglevel;
4605 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
4606 tmp_event[count].event.pid = app->pid;
4607 tmp_event[count].event.enabled = -1;
4608 count++;
4609 }
4610 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4611 pthread_mutex_unlock(&app->sock_lock);
4612 if (ret < 0 &&
4613 ret != -LTTNG_UST_ERR_EXITING &&
4614 ret != -EPIPE) {
4615 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
4616 }
4617 }
4618
4619 ret = count;
4620 *fields = tmp_event;
4621
4622 DBG2("UST app list event fields done (%zu events)", count);
4623
4624 rcu_error:
4625 rcu_read_unlock();
4626 error:
4627 health_code_update();
4628 return ret;
4629 }
4630
4631 /*
4632 * Free and clean all traceable apps of the global list.
4633 */
4634 void ust_app_clean_list(void)
4635 {
4636 int ret;
4637 struct ust_app *app;
4638 struct lttng_ht_iter iter;
4639
4640 DBG2("UST app cleaning registered apps hash table");
4641
4642 rcu_read_lock();
4643
4644 /* Cleanup notify socket hash table */
4645 if (ust_app_ht_by_notify_sock) {
4646 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
4647 notify_sock_n.node) {
4648 /*
4649 * Assert that all notifiers are gone as all triggers
4650 * are unregistered prior to this clean-up.
4651 */
4652 LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0);
4653
4654 ust_app_notify_sock_unregister(app->notify_sock);
4655 }
4656 }
4657
4658 if (ust_app_ht) {
4659 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4660 ret = lttng_ht_del(ust_app_ht, &iter);
4661 LTTNG_ASSERT(!ret);
4662 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
4663 }
4664 }
4665
4666 /* Cleanup socket hash table */
4667 if (ust_app_ht_by_sock) {
4668 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
4669 sock_n.node) {
4670 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
4671 LTTNG_ASSERT(!ret);
4672 }
4673 }
4674
4675 rcu_read_unlock();
4676
4677 /* Destroy is done only when the ht is empty */
4678 if (ust_app_ht) {
4679 lttng_ht_destroy(ust_app_ht);
4680 }
4681 if (ust_app_ht_by_sock) {
4682 lttng_ht_destroy(ust_app_ht_by_sock);
4683 }
4684 if (ust_app_ht_by_notify_sock) {
4685 lttng_ht_destroy(ust_app_ht_by_notify_sock);
4686 }
4687 }
4688
4689 /*
4690 * Init UST app hash table.
4691 */
4692 int ust_app_ht_alloc(void)
4693 {
4694 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4695 if (!ust_app_ht) {
4696 return -1;
4697 }
4698 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4699 if (!ust_app_ht_by_sock) {
4700 return -1;
4701 }
4702 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4703 if (!ust_app_ht_by_notify_sock) {
4704 return -1;
4705 }
4706 return 0;
4707 }
4708
4709 /*
4710 * For a specific UST session, disable the channel for all registered apps.
4711 */
4712 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
4713 struct ltt_ust_channel *uchan)
4714 {
4715 int ret = 0;
4716 struct lttng_ht_iter iter;
4717 struct lttng_ht_node_str *ua_chan_node;
4718 struct ust_app *app;
4719 struct ust_app_session *ua_sess;
4720 struct ust_app_channel *ua_chan;
4721
4722 LTTNG_ASSERT(usess->active);
4723 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
4724 uchan->name, usess->id);
4725
4726 rcu_read_lock();
4727
4728 /* For every registered applications */
4729 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4730 struct lttng_ht_iter uiter;
4731 if (!app->compatible) {
4732 /*
4733 * TODO: In time, we should notice the caller of this error by
4734 * telling him that this is a version error.
4735 */
4736 continue;
4737 }
4738 ua_sess = lookup_session_by_app(usess, app);
4739 if (ua_sess == NULL) {
4740 continue;
4741 }
4742
4743 /* Get channel */
4744 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4745 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4746 /* If the session if found for the app, the channel must be there */
4747 LTTNG_ASSERT(ua_chan_node);
4748
4749 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4750 /* The channel must not be already disabled */
4751 LTTNG_ASSERT(ua_chan->enabled == 1);
4752
4753 /* Disable channel onto application */
4754 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
4755 if (ret < 0) {
4756 /* XXX: We might want to report this error at some point... */
4757 continue;
4758 }
4759 }
4760
4761 rcu_read_unlock();
4762 return ret;
4763 }
4764
4765 /*
4766 * For a specific UST session, enable the channel for all registered apps.
4767 */
4768 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
4769 struct ltt_ust_channel *uchan)
4770 {
4771 int ret = 0;
4772 struct lttng_ht_iter iter;
4773 struct ust_app *app;
4774 struct ust_app_session *ua_sess;
4775
4776 LTTNG_ASSERT(usess->active);
4777 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
4778 uchan->name, usess->id);
4779
4780 rcu_read_lock();
4781
4782 /* For every registered applications */
4783 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4784 if (!app->compatible) {
4785 /*
4786 * TODO: In time, we should notice the caller of this error by
4787 * telling him that this is a version error.
4788 */
4789 continue;
4790 }
4791 ua_sess = lookup_session_by_app(usess, app);
4792 if (ua_sess == NULL) {
4793 continue;
4794 }
4795
4796 /* Enable channel onto application */
4797 ret = enable_ust_app_channel(ua_sess, uchan, app);
4798 if (ret < 0) {
4799 /* XXX: We might want to report this error at some point... */
4800 continue;
4801 }
4802 }
4803
4804 rcu_read_unlock();
4805 return ret;
4806 }
4807
4808 /*
4809 * Disable an event in a channel and for a specific session.
4810 */
4811 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4812 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4813 {
4814 int ret = 0;
4815 struct lttng_ht_iter iter, uiter;
4816 struct lttng_ht_node_str *ua_chan_node;
4817 struct ust_app *app;
4818 struct ust_app_session *ua_sess;
4819 struct ust_app_channel *ua_chan;
4820 struct ust_app_event *ua_event;
4821
4822 LTTNG_ASSERT(usess->active);
4823 DBG("UST app disabling event %s for all apps in channel "
4824 "%s for session id %" PRIu64,
4825 uevent->attr.name, uchan->name, usess->id);
4826
4827 rcu_read_lock();
4828
4829 /* For all registered applications */
4830 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4831 if (!app->compatible) {
4832 /*
4833 * TODO: In time, we should notice the caller of this error by
4834 * telling him that this is a version error.
4835 */
4836 continue;
4837 }
4838 ua_sess = lookup_session_by_app(usess, app);
4839 if (ua_sess == NULL) {
4840 /* Next app */
4841 continue;
4842 }
4843
4844 /* Lookup channel in the ust app session */
4845 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4846 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4847 if (ua_chan_node == NULL) {
4848 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
4849 "Skipping", uchan->name, usess->id, app->pid);
4850 continue;
4851 }
4852 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4853
4854 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4855 uevent->filter, uevent->attr.loglevel,
4856 uevent->exclusion);
4857 if (ua_event == NULL) {
4858 DBG2("Event %s not found in channel %s for app pid %d."
4859 "Skipping", uevent->attr.name, uchan->name, app->pid);
4860 continue;
4861 }
4862
4863 ret = disable_ust_app_event(ua_sess, ua_event, app);
4864 if (ret < 0) {
4865 /* XXX: Report error someday... */
4866 continue;
4867 }
4868 }
4869
4870 rcu_read_unlock();
4871 return ret;
4872 }
4873
4874 /* The ua_sess lock must be held by the caller. */
4875 static
4876 int ust_app_channel_create(struct ltt_ust_session *usess,
4877 struct ust_app_session *ua_sess,
4878 struct ltt_ust_channel *uchan, struct ust_app *app,
4879 struct ust_app_channel **_ua_chan)
4880 {
4881 int ret = 0;
4882 struct ust_app_channel *ua_chan = NULL;
4883
4884 LTTNG_ASSERT(ua_sess);
4885 ASSERT_LOCKED(ua_sess->lock);
4886
4887 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4888 sizeof(uchan->name))) {
4889 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
4890 &uchan->attr);
4891 ret = 0;
4892 } else {
4893 struct ltt_ust_context *uctx = NULL;
4894
4895 /*
4896 * Create channel onto application and synchronize its
4897 * configuration.
4898 */
4899 ret = ust_app_channel_allocate(ua_sess, uchan,
4900 LTTNG_UST_ABI_CHAN_PER_CPU, usess,
4901 &ua_chan);
4902 if (ret < 0) {
4903 goto error;
4904 }
4905
4906 ret = ust_app_channel_send(app, usess,
4907 ua_sess, ua_chan);
4908 if (ret) {
4909 goto error;
4910 }
4911
4912 /* Add contexts. */
4913 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
4914 ret = create_ust_app_channel_context(ua_chan,
4915 &uctx->ctx, app);
4916 if (ret) {
4917 goto error;
4918 }
4919 }
4920 }
4921
4922 error:
4923 if (ret < 0) {
4924 switch (ret) {
4925 case -ENOTCONN:
4926 /*
4927 * The application's socket is not valid. Either a bad socket
4928 * or a timeout on it. We can't inform the caller that for a
4929 * specific app, the session failed so lets continue here.
4930 */
4931 ret = 0; /* Not an error. */
4932 break;
4933 case -ENOMEM:
4934 default:
4935 break;
4936 }
4937 }
4938
4939 if (ret == 0 && _ua_chan) {
4940 /*
4941 * Only return the application's channel on success. Note
4942 * that the channel can still be part of the application's
4943 * channel hashtable on error.
4944 */
4945 *_ua_chan = ua_chan;
4946 }
4947 return ret;
4948 }
4949
4950 /*
4951 * Enable event for a specific session and channel on the tracer.
4952 */
4953 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
4954 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4955 {
4956 int ret = 0;
4957 struct lttng_ht_iter iter, uiter;
4958 struct lttng_ht_node_str *ua_chan_node;
4959 struct ust_app *app;
4960 struct ust_app_session *ua_sess;
4961 struct ust_app_channel *ua_chan;
4962 struct ust_app_event *ua_event;
4963
4964 LTTNG_ASSERT(usess->active);
4965 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
4966 uevent->attr.name, usess->id);
4967
4968 /*
4969 * NOTE: At this point, this function is called only if the session and
4970 * channel passed are already created for all apps. and enabled on the
4971 * tracer also.
4972 */
4973
4974 rcu_read_lock();
4975
4976 /* For all registered applications */
4977 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4978 if (!app->compatible) {
4979 /*
4980 * TODO: In time, we should notice the caller of this error by
4981 * telling him that this is a version error.
4982 */
4983 continue;
4984 }
4985 ua_sess = lookup_session_by_app(usess, app);
4986 if (!ua_sess) {
4987 /* The application has problem or is probably dead. */
4988 continue;
4989 }
4990
4991 pthread_mutex_lock(&ua_sess->lock);
4992
4993 if (ua_sess->deleted) {
4994 pthread_mutex_unlock(&ua_sess->lock);
4995 continue;
4996 }
4997
4998 /* Lookup channel in the ust app session */
4999 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5000 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5001 /*
5002 * It is possible that the channel cannot be found is
5003 * the channel/event creation occurs concurrently with
5004 * an application exit.
5005 */
5006 if (!ua_chan_node) {
5007 pthread_mutex_unlock(&ua_sess->lock);
5008 continue;
5009 }
5010
5011 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5012
5013 /* Get event node */
5014 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5015 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5016 if (ua_event == NULL) {
5017 DBG3("UST app enable event %s not found for app PID %d."
5018 "Skipping app", uevent->attr.name, app->pid);
5019 goto next_app;
5020 }
5021
5022 ret = enable_ust_app_event(ua_sess, ua_event, app);
5023 if (ret < 0) {
5024 pthread_mutex_unlock(&ua_sess->lock);
5025 goto error;
5026 }
5027 next_app:
5028 pthread_mutex_unlock(&ua_sess->lock);
5029 }
5030
5031 error:
5032 rcu_read_unlock();
5033 return ret;
5034 }
5035
5036 /*
5037 * For a specific existing UST session and UST channel, creates the event for
5038 * all registered apps.
5039 */
5040 int ust_app_create_event_glb(struct ltt_ust_session *usess,
5041 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
5042 {
5043 int ret = 0;
5044 struct lttng_ht_iter iter, uiter;
5045 struct lttng_ht_node_str *ua_chan_node;
5046 struct ust_app *app;
5047 struct ust_app_session *ua_sess;
5048 struct ust_app_channel *ua_chan;
5049
5050 LTTNG_ASSERT(usess->active);
5051 DBG("UST app creating event %s for all apps for session id %" PRIu64,
5052 uevent->attr.name, usess->id);
5053
5054 rcu_read_lock();
5055
5056 /* For all registered applications */
5057 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5058 if (!app->compatible) {
5059 /*
5060 * TODO: In time, we should notice the caller of this error by
5061 * telling him that this is a version error.
5062 */
5063 continue;
5064 }
5065 ua_sess = lookup_session_by_app(usess, app);
5066 if (!ua_sess) {
5067 /* The application has problem or is probably dead. */
5068 continue;
5069 }
5070
5071 pthread_mutex_lock(&ua_sess->lock);
5072
5073 if (ua_sess->deleted) {
5074 pthread_mutex_unlock(&ua_sess->lock);
5075 continue;
5076 }
5077
5078 /* Lookup channel in the ust app session */
5079 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5080 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5081 /* If the channel is not found, there is a code flow error */
5082 LTTNG_ASSERT(ua_chan_node);
5083
5084 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5085
5086 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
5087 pthread_mutex_unlock(&ua_sess->lock);
5088 if (ret < 0) {
5089 if (ret != -LTTNG_UST_ERR_EXIST) {
5090 /* Possible value at this point: -ENOMEM. If so, we stop! */
5091 break;
5092 }
5093 DBG2("UST app event %s already exist on app PID %d",
5094 uevent->attr.name, app->pid);
5095 continue;
5096 }
5097 }
5098
5099 rcu_read_unlock();
5100 return ret;
5101 }
5102
5103 /*
5104 * Start tracing for a specific UST session and app.
5105 *
5106 * Called with UST app session lock held.
5107 *
5108 */
5109 static
5110 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
5111 {
5112 int ret = 0;
5113 struct ust_app_session *ua_sess;
5114
5115 DBG("Starting tracing for ust app pid %d", app->pid);
5116
5117 rcu_read_lock();
5118
5119 if (!app->compatible) {
5120 goto end;
5121 }
5122
5123 ua_sess = lookup_session_by_app(usess, app);
5124 if (ua_sess == NULL) {
5125 /* The session is in teardown process. Ignore and continue. */
5126 goto end;
5127 }
5128
5129 pthread_mutex_lock(&ua_sess->lock);
5130
5131 if (ua_sess->deleted) {
5132 pthread_mutex_unlock(&ua_sess->lock);
5133 goto end;
5134 }
5135
5136 if (ua_sess->enabled) {
5137 pthread_mutex_unlock(&ua_sess->lock);
5138 goto end;
5139 }
5140
5141 /* Upon restart, we skip the setup, already done */
5142 if (ua_sess->started) {
5143 goto skip_setup;
5144 }
5145
5146 health_code_update();
5147
5148 skip_setup:
5149 /* This starts the UST tracing */
5150 pthread_mutex_lock(&app->sock_lock);
5151 ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle);
5152 pthread_mutex_unlock(&app->sock_lock);
5153 if (ret < 0) {
5154 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5155 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5156 app->pid, app->sock);
5157 pthread_mutex_unlock(&ua_sess->lock);
5158 goto end;
5159 } else if (ret == -EAGAIN) {
5160 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5161 app->pid, app->sock);
5162 pthread_mutex_unlock(&ua_sess->lock);
5163 goto end;
5164
5165 } else {
5166 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5167 ret, app->pid, app->sock);
5168 }
5169 goto error_unlock;
5170 }
5171
5172 /* Indicate that the session has been started once */
5173 ua_sess->started = 1;
5174 ua_sess->enabled = 1;
5175
5176 pthread_mutex_unlock(&ua_sess->lock);
5177
5178 health_code_update();
5179
5180 /* Quiescent wait after starting trace */
5181 pthread_mutex_lock(&app->sock_lock);
5182 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5183 pthread_mutex_unlock(&app->sock_lock);
5184 if (ret < 0) {
5185 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5186 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5187 app->pid, app->sock);
5188 } else if (ret == -EAGAIN) {
5189 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5190 app->pid, app->sock);
5191 } else {
5192 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5193 ret, app->pid, app->sock);
5194 }
5195 }
5196
5197 end:
5198 rcu_read_unlock();
5199 health_code_update();
5200 return 0;
5201
5202 error_unlock:
5203 pthread_mutex_unlock(&ua_sess->lock);
5204 rcu_read_unlock();
5205 health_code_update();
5206 return -1;
5207 }
5208
5209 /*
5210 * Stop tracing for a specific UST session and app.
5211 */
5212 static
5213 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
5214 {
5215 int ret = 0;
5216 struct ust_app_session *ua_sess;
5217 struct ust_registry_session *registry;
5218
5219 DBG("Stopping tracing for ust app pid %d", app->pid);
5220
5221 rcu_read_lock();
5222
5223 if (!app->compatible) {
5224 goto end_no_session;
5225 }
5226
5227 ua_sess = lookup_session_by_app(usess, app);
5228 if (ua_sess == NULL) {
5229 goto end_no_session;
5230 }
5231
5232 pthread_mutex_lock(&ua_sess->lock);
5233
5234 if (ua_sess->deleted) {
5235 pthread_mutex_unlock(&ua_sess->lock);
5236 goto end_no_session;
5237 }
5238
5239 /*
5240 * If started = 0, it means that stop trace has been called for a session
5241 * that was never started. It's possible since we can have a fail start
5242 * from either the application manager thread or the command thread. Simply
5243 * indicate that this is a stop error.
5244 */
5245 if (!ua_sess->started) {
5246 goto error_rcu_unlock;
5247 }
5248
5249 health_code_update();
5250
5251 /* This inhibits UST tracing */
5252 pthread_mutex_lock(&app->sock_lock);
5253 ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle);
5254 pthread_mutex_unlock(&app->sock_lock);
5255 if (ret < 0) {
5256 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5257 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5258 app->pid, app->sock);
5259 goto end_unlock;
5260 } else if (ret == -EAGAIN) {
5261 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5262 app->pid, app->sock);
5263 goto end_unlock;
5264
5265 } else {
5266 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5267 ret, app->pid, app->sock);
5268 }
5269 goto error_rcu_unlock;
5270 }
5271
5272 health_code_update();
5273 ua_sess->enabled = 0;
5274
5275 /* Quiescent wait after stopping trace */
5276 pthread_mutex_lock(&app->sock_lock);
5277 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5278 pthread_mutex_unlock(&app->sock_lock);
5279 if (ret < 0) {
5280 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5281 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5282 app->pid, app->sock);
5283 } else if (ret == -EAGAIN) {
5284 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5285 app->pid, app->sock);
5286 } else {
5287 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5288 ret, app->pid, app->sock);
5289 }
5290 }
5291
5292 health_code_update();
5293
5294 registry = get_session_registry(ua_sess);
5295
5296 /* The UST app session is held registry shall not be null. */
5297 LTTNG_ASSERT(registry);
5298
5299 /* Push metadata for application before freeing the application. */
5300 (void) push_metadata(registry, ua_sess->consumer);
5301
5302 end_unlock:
5303 pthread_mutex_unlock(&ua_sess->lock);
5304 end_no_session:
5305 rcu_read_unlock();
5306 health_code_update();
5307 return 0;
5308
5309 error_rcu_unlock:
5310 pthread_mutex_unlock(&ua_sess->lock);
5311 rcu_read_unlock();
5312 health_code_update();
5313 return -1;
5314 }
5315
5316 static
5317 int ust_app_flush_app_session(struct ust_app *app,
5318 struct ust_app_session *ua_sess)
5319 {
5320 int ret, retval = 0;
5321 struct lttng_ht_iter iter;
5322 struct ust_app_channel *ua_chan;
5323 struct consumer_socket *socket;
5324
5325 DBG("Flushing app session buffers for ust app pid %d", app->pid);
5326
5327 rcu_read_lock();
5328
5329 if (!app->compatible) {
5330 goto end_not_compatible;
5331 }
5332
5333 pthread_mutex_lock(&ua_sess->lock);
5334
5335 if (ua_sess->deleted) {
5336 goto end_deleted;
5337 }
5338
5339 health_code_update();
5340
5341 /* Flushing buffers */
5342 socket = consumer_find_socket_by_bitness(app->bits_per_long,
5343 ua_sess->consumer);
5344
5345 /* Flush buffers and push metadata. */
5346 switch (ua_sess->buffer_type) {
5347 case LTTNG_BUFFER_PER_PID:
5348 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
5349 node.node) {
5350 health_code_update();
5351 ret = consumer_flush_channel(socket, ua_chan->key);
5352 if (ret) {
5353 ERR("Error flushing consumer channel");
5354 retval = -1;
5355 continue;
5356 }
5357 }
5358 break;
5359 case LTTNG_BUFFER_PER_UID:
5360 default:
5361 abort();
5362 break;
5363 }
5364
5365 health_code_update();
5366
5367 end_deleted:
5368 pthread_mutex_unlock(&ua_sess->lock);
5369
5370 end_not_compatible:
5371 rcu_read_unlock();
5372 health_code_update();
5373 return retval;
5374 }
5375
5376 /*
5377 * Flush buffers for all applications for a specific UST session.
5378 * Called with UST session lock held.
5379 */
5380 static
5381 int ust_app_flush_session(struct ltt_ust_session *usess)
5382
5383 {
5384 int ret = 0;
5385
5386 DBG("Flushing session buffers for all ust apps");
5387
5388 rcu_read_lock();
5389
5390 /* Flush buffers and push metadata. */
5391 switch (usess->buffer_type) {
5392 case LTTNG_BUFFER_PER_UID:
5393 {
5394 struct buffer_reg_uid *reg;
5395 struct lttng_ht_iter iter;
5396
5397 /* Flush all per UID buffers associated to that session. */
5398 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5399 struct ust_registry_session *ust_session_reg;
5400 struct buffer_reg_channel *buf_reg_chan;
5401 struct consumer_socket *socket;
5402
5403 /* Get consumer socket to use to push the metadata.*/
5404 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5405 usess->consumer);
5406 if (!socket) {
5407 /* Ignore request if no consumer is found for the session. */
5408 continue;
5409 }
5410
5411 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5412 buf_reg_chan, node.node) {
5413 /*
5414 * The following call will print error values so the return
5415 * code is of little importance because whatever happens, we
5416 * have to try them all.
5417 */
5418 (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key);
5419 }
5420
5421 ust_session_reg = reg->registry->reg.ust;
5422 /* Push metadata. */
5423 (void) push_metadata(ust_session_reg, usess->consumer);
5424 }
5425 break;
5426 }
5427 case LTTNG_BUFFER_PER_PID:
5428 {
5429 struct ust_app_session *ua_sess;
5430 struct lttng_ht_iter iter;
5431 struct ust_app *app;
5432
5433 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5434 ua_sess = lookup_session_by_app(usess, app);
5435 if (ua_sess == NULL) {
5436 continue;
5437 }
5438 (void) ust_app_flush_app_session(app, ua_sess);
5439 }
5440 break;
5441 }
5442 default:
5443 ret = -1;
5444 abort();
5445 break;
5446 }
5447
5448 rcu_read_unlock();
5449 health_code_update();
5450 return ret;
5451 }
5452
5453 static
5454 int ust_app_clear_quiescent_app_session(struct ust_app *app,
5455 struct ust_app_session *ua_sess)
5456 {
5457 int ret = 0;
5458 struct lttng_ht_iter iter;
5459 struct ust_app_channel *ua_chan;
5460 struct consumer_socket *socket;
5461
5462 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
5463
5464 rcu_read_lock();
5465
5466 if (!app->compatible) {
5467 goto end_not_compatible;
5468 }
5469
5470 pthread_mutex_lock(&ua_sess->lock);
5471
5472 if (ua_sess->deleted) {
5473 goto end_unlock;
5474 }
5475
5476 health_code_update();
5477
5478 socket = consumer_find_socket_by_bitness(app->bits_per_long,
5479 ua_sess->consumer);
5480 if (!socket) {
5481 ERR("Failed to find consumer (%" PRIu32 ") socket",
5482 app->bits_per_long);
5483 ret = -1;
5484 goto end_unlock;
5485 }
5486
5487 /* Clear quiescent state. */
5488 switch (ua_sess->buffer_type) {
5489 case LTTNG_BUFFER_PER_PID:
5490 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter,
5491 ua_chan, node.node) {
5492 health_code_update();
5493 ret = consumer_clear_quiescent_channel(socket,
5494 ua_chan->key);
5495 if (ret) {
5496 ERR("Error clearing quiescent state for consumer channel");
5497 ret = -1;
5498 continue;
5499 }
5500 }
5501 break;
5502 case LTTNG_BUFFER_PER_UID:
5503 default:
5504 abort();
5505 ret = -1;
5506 break;
5507 }
5508
5509 health_code_update();
5510
5511 end_unlock:
5512 pthread_mutex_unlock(&ua_sess->lock);
5513
5514 end_not_compatible:
5515 rcu_read_unlock();
5516 health_code_update();
5517 return ret;
5518 }
5519
5520 /*
5521 * Clear quiescent state in each stream for all applications for a
5522 * specific UST session.
5523 * Called with UST session lock held.
5524 */
5525 static
5526 int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
5527
5528 {
5529 int ret = 0;
5530
5531 DBG("Clearing stream quiescent state for all ust apps");
5532
5533 rcu_read_lock();
5534
5535 switch (usess->buffer_type) {
5536 case LTTNG_BUFFER_PER_UID:
5537 {
5538 struct lttng_ht_iter iter;
5539 struct buffer_reg_uid *reg;
5540
5541 /*
5542 * Clear quiescent for all per UID buffers associated to
5543 * that session.
5544 */
5545 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5546 struct consumer_socket *socket;
5547 struct buffer_reg_channel *buf_reg_chan;
5548
5549 /* Get associated consumer socket.*/
5550 socket = consumer_find_socket_by_bitness(
5551 reg->bits_per_long, usess->consumer);
5552 if (!socket) {
5553 /*
5554 * Ignore request if no consumer is found for
5555 * the session.
5556 */
5557 continue;
5558 }
5559
5560 cds_lfht_for_each_entry(reg->registry->channels->ht,
5561 &iter.iter, buf_reg_chan, node.node) {
5562 /*
5563 * The following call will print error values so
5564 * the return code is of little importance
5565 * because whatever happens, we have to try them
5566 * all.
5567 */
5568 (void) consumer_clear_quiescent_channel(socket,
5569 buf_reg_chan->consumer_key);
5570 }
5571 }
5572 break;
5573 }
5574 case LTTNG_BUFFER_PER_PID:
5575 {
5576 struct ust_app_session *ua_sess;
5577 struct lttng_ht_iter iter;
5578 struct ust_app *app;
5579
5580 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
5581 pid_n.node) {
5582 ua_sess = lookup_session_by_app(usess, app);
5583 if (ua_sess == NULL) {
5584 continue;
5585 }
5586 (void) ust_app_clear_quiescent_app_session(app,
5587 ua_sess);
5588 }
5589 break;
5590 }
5591 default:
5592 ret = -1;
5593 abort();
5594 break;
5595 }
5596
5597 rcu_read_unlock();
5598 health_code_update();
5599 return ret;
5600 }
5601
5602 /*
5603 * Destroy a specific UST session in apps.
5604 */
5605 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
5606 {
5607 int ret;
5608 struct ust_app_session *ua_sess;
5609 struct lttng_ht_iter iter;
5610 struct lttng_ht_node_u64 *node;
5611
5612 DBG("Destroy tracing for ust app pid %d", app->pid);
5613
5614 rcu_read_lock();
5615
5616 if (!app->compatible) {
5617 goto end;
5618 }
5619
5620 __lookup_session_by_app(usess, app, &iter);
5621 node = lttng_ht_iter_get_node_u64(&iter);
5622 if (node == NULL) {
5623 /* Session is being or is deleted. */
5624 goto end;
5625 }
5626 ua_sess = caa_container_of(node, struct ust_app_session, node);
5627
5628 health_code_update();
5629 destroy_app_session(app, ua_sess);
5630
5631 health_code_update();
5632
5633 /* Quiescent wait after stopping trace */
5634 pthread_mutex_lock(&app->sock_lock);
5635 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5636 pthread_mutex_unlock(&app->sock_lock);
5637 if (ret < 0) {
5638 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5639 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5640 app->pid, app->sock);
5641 } else if (ret == -EAGAIN) {
5642 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5643 app->pid, app->sock);
5644 } else {
5645 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5646 ret, app->pid, app->sock);
5647 }
5648 }
5649 end:
5650 rcu_read_unlock();
5651 health_code_update();
5652 return 0;
5653 }
5654
5655 /*
5656 * Start tracing for the UST session.
5657 */
5658 int ust_app_start_trace_all(struct ltt_ust_session *usess)
5659 {
5660 struct lttng_ht_iter iter;
5661 struct ust_app *app;
5662
5663 DBG("Starting all UST traces");
5664
5665 /*
5666 * Even though the start trace might fail, flag this session active so
5667 * other application coming in are started by default.
5668 */
5669 usess->active = 1;
5670
5671 rcu_read_lock();
5672
5673 /*
5674 * In a start-stop-start use-case, we need to clear the quiescent state
5675 * of each channel set by the prior stop command, thus ensuring that a
5676 * following stop or destroy is sure to grab a timestamp_end near those
5677 * operations, even if the packet is empty.
5678 */
5679 (void) ust_app_clear_quiescent_session(usess);
5680
5681 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5682 ust_app_global_update(usess, app);
5683 }
5684
5685 rcu_read_unlock();
5686
5687 return 0;
5688 }
5689
5690 /*
5691 * Start tracing for the UST session.
5692 * Called with UST session lock held.
5693 */
5694 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
5695 {
5696 int ret = 0;
5697 struct lttng_ht_iter iter;
5698 struct ust_app *app;
5699
5700 DBG("Stopping all UST traces");
5701
5702 /*
5703 * Even though the stop trace might fail, flag this session inactive so
5704 * other application coming in are not started by default.
5705 */
5706 usess->active = 0;
5707
5708 rcu_read_lock();
5709
5710 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5711 ret = ust_app_stop_trace(usess, app);
5712 if (ret < 0) {
5713 /* Continue to next apps even on error */
5714 continue;
5715 }
5716 }
5717
5718 (void) ust_app_flush_session(usess);
5719
5720 rcu_read_unlock();
5721
5722 return 0;
5723 }
5724
5725 /*
5726 * Destroy app UST session.
5727 */
5728 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
5729 {
5730 int ret = 0;
5731 struct lttng_ht_iter iter;
5732 struct ust_app *app;
5733
5734 DBG("Destroy all UST traces");
5735
5736 rcu_read_lock();
5737
5738 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5739 ret = destroy_trace(usess, app);
5740 if (ret < 0) {
5741 /* Continue to next apps even on error */
5742 continue;
5743 }
5744 }
5745
5746 rcu_read_unlock();
5747
5748 return 0;
5749 }
5750
5751 /* The ua_sess lock must be held by the caller. */
5752 static
5753 int find_or_create_ust_app_channel(
5754 struct ltt_ust_session *usess,
5755 struct ust_app_session *ua_sess,
5756 struct ust_app *app,
5757 struct ltt_ust_channel *uchan,
5758 struct ust_app_channel **ua_chan)
5759 {
5760 int ret = 0;
5761 struct lttng_ht_iter iter;
5762 struct lttng_ht_node_str *ua_chan_node;
5763
5764 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5765 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5766 if (ua_chan_node) {
5767 *ua_chan = caa_container_of(ua_chan_node,
5768 struct ust_app_channel, node);
5769 goto end;
5770 }
5771
5772 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5773 if (ret) {
5774 goto end;
5775 }
5776 end:
5777 return ret;
5778 }
5779
5780 static
5781 int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5782 struct ltt_ust_event *uevent, struct ust_app_session *ua_sess,
5783 struct ust_app *app)
5784 {
5785 int ret = 0;
5786 struct ust_app_event *ua_event = NULL;
5787
5788 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5789 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5790 if (!ua_event) {
5791 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
5792 if (ret < 0) {
5793 goto end;
5794 }
5795 } else {
5796 if (ua_event->enabled != uevent->enabled) {
5797 ret = uevent->enabled ?
5798 enable_ust_app_event(ua_sess, ua_event, app) :
5799 disable_ust_app_event(ua_sess, ua_event, app);
5800 }
5801 }
5802
5803 end:
5804 return ret;
5805 }
5806
5807 /* Called with RCU read-side lock held. */
5808 static
5809 void ust_app_synchronize_event_notifier_rules(struct ust_app *app)
5810 {
5811 int ret = 0;
5812 enum lttng_error_code ret_code;
5813 enum lttng_trigger_status t_status;
5814 struct lttng_ht_iter app_trigger_iter;
5815 struct lttng_triggers *triggers = NULL;
5816 struct ust_app_event_notifier_rule *event_notifier_rule;
5817 unsigned int count, i;
5818
5819 ASSERT_RCU_READ_LOCKED();
5820
5821 if (!ust_app_supports_notifiers(app)) {
5822 goto end;
5823 }
5824
5825 /*
5826 * Currrently, registering or unregistering a trigger with an
5827 * event rule condition causes a full synchronization of the event
5828 * notifiers.
5829 *
5830 * The first step attempts to add an event notifier for all registered
5831 * triggers that apply to the user space tracers. Then, the
5832 * application's event notifiers rules are all checked against the list
5833 * of registered triggers. Any event notifier that doesn't have a
5834 * matching trigger can be assumed to have been disabled.
5835 *
5836 * All of this is inefficient, but is put in place to get the feature
5837 * rolling as it is simpler at this moment. It will be optimized Soon™
5838 * to allow the state of enabled
5839 * event notifiers to be synchronized in a piece-wise way.
5840 */
5841
5842 /* Get all triggers using uid 0 (root) */
5843 ret_code = notification_thread_command_list_triggers(
5844 the_notification_thread_handle, 0, &triggers);
5845 if (ret_code != LTTNG_OK) {
5846 goto end;
5847 }
5848
5849 LTTNG_ASSERT(triggers);
5850
5851 t_status = lttng_triggers_get_count(triggers, &count);
5852 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
5853 goto end;
5854 }
5855
5856 for (i = 0; i < count; i++) {
5857 struct lttng_condition *condition;
5858 struct lttng_event_rule *event_rule;
5859 struct lttng_trigger *trigger;
5860 const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule;
5861 enum lttng_condition_status condition_status;
5862 uint64_t token;
5863
5864 trigger = lttng_triggers_borrow_mutable_at_index(triggers, i);
5865 LTTNG_ASSERT(trigger);
5866
5867 token = lttng_trigger_get_tracer_token(trigger);
5868 condition = lttng_trigger_get_condition(trigger);
5869
5870 if (lttng_condition_get_type(condition) !=
5871 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) {
5872 /* Does not apply */
5873 continue;
5874 }
5875
5876 condition_status =
5877 lttng_condition_event_rule_matches_borrow_rule_mutable(
5878 condition, &event_rule);
5879 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
5880
5881 if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) {
5882 /* Skip kernel related triggers. */
5883 continue;
5884 }
5885
5886 /*
5887 * Find or create the associated token event rule. The caller
5888 * holds the RCU read lock, so this is safe to call without
5889 * explicitly acquiring it here.
5890 */
5891 looked_up_event_notifier_rule = find_ust_app_event_notifier_rule(
5892 app->token_to_event_notifier_rule_ht, token);
5893 if (!looked_up_event_notifier_rule) {
5894 ret = create_ust_app_event_notifier_rule(trigger, app);
5895 if (ret < 0) {
5896 goto end;
5897 }
5898 }
5899 }
5900
5901 rcu_read_lock();
5902 /* Remove all unknown event sources from the app. */
5903 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
5904 &app_trigger_iter.iter, event_notifier_rule,
5905 node.node) {
5906 const uint64_t app_token = event_notifier_rule->token;
5907 bool found = false;
5908
5909 /*
5910 * Check if the app event trigger still exists on the
5911 * notification side.
5912 */
5913 for (i = 0; i < count; i++) {
5914 uint64_t notification_thread_token;
5915 const struct lttng_trigger *trigger =
5916 lttng_triggers_get_at_index(
5917 triggers, i);
5918
5919 LTTNG_ASSERT(trigger);
5920
5921 notification_thread_token =
5922 lttng_trigger_get_tracer_token(trigger);
5923
5924 if (notification_thread_token == app_token) {
5925 found = true;
5926 break;
5927 }
5928 }
5929
5930 if (found) {
5931 /* Still valid. */
5932 continue;
5933 }
5934
5935 /*
5936 * This trigger was unregistered, disable it on the tracer's
5937 * side.
5938 */
5939 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht,
5940 &app_trigger_iter);
5941 LTTNG_ASSERT(ret == 0);
5942
5943 /* Callee logs errors. */
5944 (void) disable_ust_object(app, event_notifier_rule->obj);
5945
5946 delete_ust_app_event_notifier_rule(
5947 app->sock, event_notifier_rule, app);
5948 }
5949
5950 rcu_read_unlock();
5951
5952 end:
5953 lttng_triggers_destroy(triggers);
5954 return;
5955 }
5956
5957 /*
5958 * RCU read lock must be held by the caller.
5959 */
5960 static
5961 void ust_app_synchronize_all_channels(struct ltt_ust_session *usess,
5962 struct ust_app_session *ua_sess,
5963 struct ust_app *app)
5964 {
5965 int ret = 0;
5966 struct cds_lfht_iter uchan_iter;
5967 struct ltt_ust_channel *uchan;
5968
5969 LTTNG_ASSERT(usess);
5970 LTTNG_ASSERT(ua_sess);
5971 LTTNG_ASSERT(app);
5972 ASSERT_RCU_READ_LOCKED();
5973
5974 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
5975 uchan, node.node) {
5976 struct ust_app_channel *ua_chan;
5977 struct cds_lfht_iter uevent_iter;
5978 struct ltt_ust_event *uevent;
5979
5980 /*
5981 * Search for a matching ust_app_channel. If none is found,
5982 * create it. Creating the channel will cause the ua_chan
5983 * structure to be allocated, the channel buffers to be
5984 * allocated (if necessary) and sent to the application, and
5985 * all enabled contexts will be added to the channel.
5986 */
5987 ret = find_or_create_ust_app_channel(usess, ua_sess,
5988 app, uchan, &ua_chan);
5989 if (ret) {
5990 /* Tracer is probably gone or ENOMEM. */
5991 goto end;
5992 }
5993
5994 if (!ua_chan) {
5995 /* ua_chan will be NULL for the metadata channel */
5996 continue;
5997 }
5998
5999 cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent,
6000 node.node) {
6001 ret = ust_app_channel_synchronize_event(ua_chan,
6002 uevent, ua_sess, app);
6003 if (ret) {
6004 goto end;
6005 }
6006 }
6007
6008 if (ua_chan->enabled != uchan->enabled) {
6009 ret = uchan->enabled ?
6010 enable_ust_app_channel(ua_sess, uchan, app) :
6011 disable_ust_app_channel(ua_sess, ua_chan, app);
6012 if (ret) {
6013 goto end;
6014 }
6015 }
6016 }
6017 end:
6018 return;
6019 }
6020
6021 /*
6022 * The caller must ensure that the application is compatible and is tracked
6023 * by the process attribute trackers.
6024 */
6025 static
6026 void ust_app_synchronize(struct ltt_ust_session *usess,
6027 struct ust_app *app)
6028 {
6029 int ret = 0;
6030 struct ust_app_session *ua_sess = NULL;
6031
6032 /*
6033 * The application's configuration should only be synchronized for
6034 * active sessions.
6035 */
6036 LTTNG_ASSERT(usess->active);
6037
6038 ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL);
6039 if (ret < 0) {
6040 /* Tracer is probably gone or ENOMEM. */
6041 if (ua_sess) {
6042 destroy_app_session(app, ua_sess);
6043 }
6044 goto end;
6045 }
6046 LTTNG_ASSERT(ua_sess);
6047
6048 pthread_mutex_lock(&ua_sess->lock);
6049 if (ua_sess->deleted) {
6050 goto deleted_session;
6051 }
6052
6053 rcu_read_lock();
6054
6055 ust_app_synchronize_all_channels(usess, ua_sess, app);
6056
6057 /*
6058 * Create the metadata for the application. This returns gracefully if a
6059 * metadata was already set for the session.
6060 *
6061 * The metadata channel must be created after the data channels as the
6062 * consumer daemon assumes this ordering. When interacting with a relay
6063 * daemon, the consumer will use this assumption to send the
6064 * "STREAMS_SENT" message to the relay daemon.
6065 */
6066 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
6067 if (ret < 0) {
6068 ERR("Metadata creation failed for app sock %d for session id %" PRIu64,
6069 app->sock, usess->id);
6070 }
6071
6072 rcu_read_unlock();
6073
6074 deleted_session:
6075 pthread_mutex_unlock(&ua_sess->lock);
6076 end:
6077 return;
6078 }
6079
6080 static
6081 void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
6082 {
6083 struct ust_app_session *ua_sess;
6084
6085 ua_sess = lookup_session_by_app(usess, app);
6086 if (ua_sess == NULL) {
6087 return;
6088 }
6089 destroy_app_session(app, ua_sess);
6090 }
6091
6092 /*
6093 * Add channels/events from UST global domain to registered apps at sock.
6094 *
6095 * Called with session lock held.
6096 * Called with RCU read-side lock held.
6097 */
6098 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
6099 {
6100 LTTNG_ASSERT(usess);
6101 LTTNG_ASSERT(usess->active);
6102 ASSERT_RCU_READ_LOCKED();
6103
6104 DBG2("UST app global update for app sock %d for session id %" PRIu64,
6105 app->sock, usess->id);
6106
6107 if (!app->compatible) {
6108 return;
6109 }
6110 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID,
6111 usess, app->pid) &&
6112 trace_ust_id_tracker_lookup(
6113 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID,
6114 usess, app->uid) &&
6115 trace_ust_id_tracker_lookup(
6116 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID,
6117 usess, app->gid)) {
6118 /*
6119 * Synchronize the application's internal tracing configuration
6120 * and start tracing.
6121 */
6122 ust_app_synchronize(usess, app);
6123 ust_app_start_trace(usess, app);
6124 } else {
6125 ust_app_global_destroy(usess, app);
6126 }
6127 }
6128
6129 /*
6130 * Add all event notifiers to an application.
6131 *
6132 * Called with session lock held.
6133 * Called with RCU read-side lock held.
6134 */
6135 void ust_app_global_update_event_notifier_rules(struct ust_app *app)
6136 {
6137 ASSERT_RCU_READ_LOCKED();
6138
6139 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6140 app->name, app->pid);
6141
6142 if (!app->compatible || !ust_app_supports_notifiers(app)) {
6143 return;
6144 }
6145
6146 if (app->event_notifier_group.object == NULL) {
6147 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6148 app->name, app->pid);
6149 return;
6150 }
6151
6152 ust_app_synchronize_event_notifier_rules(app);
6153 }
6154
6155 /*
6156 * Called with session lock held.
6157 */
6158 void ust_app_global_update_all(struct ltt_ust_session *usess)
6159 {
6160 struct lttng_ht_iter iter;
6161 struct ust_app *app;
6162
6163 rcu_read_lock();
6164 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6165 ust_app_global_update(usess, app);
6166 }
6167 rcu_read_unlock();
6168 }
6169
6170 void ust_app_global_update_all_event_notifier_rules(void)
6171 {
6172 struct lttng_ht_iter iter;
6173 struct ust_app *app;
6174
6175 rcu_read_lock();
6176 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6177 ust_app_global_update_event_notifier_rules(app);
6178 }
6179
6180 rcu_read_unlock();
6181 }
6182
6183 /*
6184 * Add context to a specific channel for global UST domain.
6185 */
6186 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
6187 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
6188 {
6189 int ret = 0;
6190 struct lttng_ht_node_str *ua_chan_node;
6191 struct lttng_ht_iter iter, uiter;
6192 struct ust_app_channel *ua_chan = NULL;
6193 struct ust_app_session *ua_sess;
6194 struct ust_app *app;
6195
6196 LTTNG_ASSERT(usess->active);
6197
6198 rcu_read_lock();
6199 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6200 if (!app->compatible) {
6201 /*
6202 * TODO: In time, we should notice the caller of this error by
6203 * telling him that this is a version error.
6204 */
6205 continue;
6206 }
6207 ua_sess = lookup_session_by_app(usess, app);
6208 if (ua_sess == NULL) {
6209 continue;
6210 }
6211
6212 pthread_mutex_lock(&ua_sess->lock);
6213
6214 if (ua_sess->deleted) {
6215 pthread_mutex_unlock(&ua_sess->lock);
6216 continue;
6217 }
6218
6219 /* Lookup channel in the ust app session */
6220 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
6221 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6222 if (ua_chan_node == NULL) {
6223 goto next_app;
6224 }
6225 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
6226 node);
6227 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
6228 if (ret < 0) {
6229 goto next_app;
6230 }
6231 next_app:
6232 pthread_mutex_unlock(&ua_sess->lock);
6233 }
6234
6235 rcu_read_unlock();
6236 return ret;
6237 }
6238
6239 /*
6240 * Receive registration and populate the given msg structure.
6241 *
6242 * On success return 0 else a negative value returned by the ustctl call.
6243 */
6244 int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
6245 {
6246 int ret;
6247 uint32_t pid, ppid, uid, gid;
6248
6249 LTTNG_ASSERT(msg);
6250
6251 ret = lttng_ust_ctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
6252 &pid, &ppid, &uid, &gid,
6253 &msg->bits_per_long,
6254 &msg->uint8_t_alignment,
6255 &msg->uint16_t_alignment,
6256 &msg->uint32_t_alignment,
6257 &msg->uint64_t_alignment,
6258 &msg->long_alignment,
6259 &msg->byte_order,
6260 msg->name);
6261 if (ret < 0) {
6262 switch (-ret) {
6263 case EPIPE:
6264 case ECONNRESET:
6265 case LTTNG_UST_ERR_EXITING:
6266 DBG3("UST app recv reg message failed. Application died");
6267 break;
6268 case LTTNG_UST_ERR_UNSUP_MAJOR:
6269 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6270 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
6271 LTTNG_UST_ABI_MINOR_VERSION);
6272 break;
6273 default:
6274 ERR("UST app recv reg message failed with ret %d", ret);
6275 break;
6276 }
6277 goto error;
6278 }
6279 msg->pid = (pid_t) pid;
6280 msg->ppid = (pid_t) ppid;
6281 msg->uid = (uid_t) uid;
6282 msg->gid = (gid_t) gid;
6283
6284 error:
6285 return ret;
6286 }
6287
6288 /*
6289 * Return a ust app session object using the application object and the
6290 * session object descriptor has a key. If not found, NULL is returned.
6291 * A RCU read side lock MUST be acquired when calling this function.
6292 */
6293 static struct ust_app_session *find_session_by_objd(struct ust_app *app,
6294 int objd)
6295 {
6296 struct lttng_ht_node_ulong *node;
6297 struct lttng_ht_iter iter;
6298 struct ust_app_session *ua_sess = NULL;
6299
6300 LTTNG_ASSERT(app);
6301 ASSERT_RCU_READ_LOCKED();
6302
6303 lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter);
6304 node = lttng_ht_iter_get_node_ulong(&iter);
6305 if (node == NULL) {
6306 DBG2("UST app session find by objd %d not found", objd);
6307 goto error;
6308 }
6309
6310 ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node);
6311
6312 error:
6313 return ua_sess;
6314 }
6315
6316 /*
6317 * Return a ust app channel object using the application object and the channel
6318 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6319 * lock MUST be acquired before calling this function.
6320 */
6321 static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
6322 int objd)
6323 {
6324 struct lttng_ht_node_ulong *node;
6325 struct lttng_ht_iter iter;
6326 struct ust_app_channel *ua_chan = NULL;
6327
6328 LTTNG_ASSERT(app);
6329 ASSERT_RCU_READ_LOCKED();
6330
6331 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
6332 node = lttng_ht_iter_get_node_ulong(&iter);
6333 if (node == NULL) {
6334 DBG2("UST app channel find by objd %d not found", objd);
6335 goto error;
6336 }
6337
6338 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
6339
6340 error:
6341 return ua_chan;
6342 }
6343
6344 /*
6345 * Fixup legacy context fields for comparison:
6346 * - legacy array becomes array_nestable,
6347 * - legacy struct becomes struct_nestable,
6348 * - legacy variant becomes variant_nestable,
6349 * legacy sequences are not emitted in LTTng-UST contexts.
6350 */
6351 static int ust_app_fixup_legacy_context_fields(size_t *_nr_fields,
6352 struct lttng_ust_ctl_field **_fields)
6353 {
6354 struct lttng_ust_ctl_field *fields = *_fields, *new_fields = NULL;
6355 size_t nr_fields = *_nr_fields, new_nr_fields = 0, i, j;
6356 bool found = false;
6357 int ret = 0;
6358
6359 for (i = 0; i < nr_fields; i++) {
6360 const struct lttng_ust_ctl_field *field = &fields[i];
6361
6362 switch (field->type.atype) {
6363 case lttng_ust_ctl_atype_sequence:
6364 ERR("Unexpected legacy sequence context.");
6365 ret = -EINVAL;
6366 goto end;
6367 case lttng_ust_ctl_atype_array:
6368 switch (field->type.u.legacy.array.elem_type.atype) {
6369 case lttng_ust_ctl_atype_integer:
6370 break;
6371 default:
6372 ERR("Unexpected legacy array element type in context.");
6373 ret = -EINVAL;
6374 goto end;
6375 }
6376 found = true;
6377 /* One field for array_nested, one field for elem type. */
6378 new_nr_fields += 2;
6379 break;
6380
6381 case lttng_ust_ctl_atype_struct: /* Fallthrough */
6382 case lttng_ust_ctl_atype_variant:
6383 found = true;
6384 new_nr_fields++;
6385 break;
6386 default:
6387 new_nr_fields++;
6388 break;
6389 }
6390 }
6391 if (!found) {
6392 goto end;
6393 }
6394 new_fields = (struct lttng_ust_ctl_field *) zmalloc(sizeof(*new_fields) * new_nr_fields);
6395 if (!new_fields) {
6396 ret = -ENOMEM;
6397 goto end;
6398 }
6399 for (i = 0, j = 0; i < nr_fields; i++, j++) {
6400 const struct lttng_ust_ctl_field *field = &fields[i];
6401 struct lttng_ust_ctl_field *new_field = &new_fields[j];
6402
6403 switch (field->type.atype) {
6404 case lttng_ust_ctl_atype_array:
6405 /* One field for array_nested, one field for elem type. */
6406 strncpy(new_field->name, field->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
6407 new_field->type.atype = lttng_ust_ctl_atype_array_nestable;
6408 new_field->type.u.array_nestable.length = field->type.u.legacy.array.length;
6409 new_field->type.u.array_nestable.alignment = 0;
6410 new_field = &new_fields[++j]; /* elem type */
6411 new_field->type.atype = field->type.u.legacy.array.elem_type.atype;
6412 assert(new_field->type.atype == lttng_ust_ctl_atype_integer);
6413 new_field->type.u.integer = field->type.u.legacy.array.elem_type.u.basic.integer;
6414 break;
6415 case lttng_ust_ctl_atype_struct:
6416 strncpy(new_field->name, field->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
6417 new_field->type.atype = lttng_ust_ctl_atype_struct_nestable;
6418 new_field->type.u.struct_nestable.nr_fields = field->type.u.legacy._struct.nr_fields;
6419 new_field->type.u.struct_nestable.alignment = 0;
6420 break;
6421 case lttng_ust_ctl_atype_variant:
6422 strncpy(new_field->name, field->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1);
6423 new_field->type.atype = lttng_ust_ctl_atype_variant_nestable;
6424 new_field->type.u.variant_nestable.nr_choices = field->type.u.legacy.variant.nr_choices;
6425 strncpy(new_field->type.u.variant_nestable.tag_name,
6426 field->type.u.legacy.variant.tag_name,
6427 LTTNG_UST_ABI_SYM_NAME_LEN - 1);
6428 new_field->type.u.variant_nestable.alignment = 0;
6429 break;
6430 default:
6431 *new_field = *field;
6432 break;
6433 }
6434 }
6435 free(fields);
6436 *_fields = new_fields;
6437 *_nr_fields = new_nr_fields;
6438 end:
6439 return ret;
6440 }
6441
6442 /*
6443 * Reply to a register channel notification from an application on the notify
6444 * socket. The channel metadata is also created.
6445 *
6446 * The session UST registry lock is acquired in this function.
6447 *
6448 * On success 0 is returned else a negative value.
6449 */
6450 static int reply_ust_register_channel(int sock, int cobjd,
6451 size_t nr_fields, struct lttng_ust_ctl_field *fields)
6452 {
6453 int ret, ret_code = 0;
6454 uint32_t chan_id;
6455 uint64_t chan_reg_key;
6456 enum lttng_ust_ctl_channel_header type = LTTNG_UST_CTL_CHANNEL_HEADER_UNKNOWN;
6457 struct ust_app *app;
6458 struct ust_app_channel *ua_chan;
6459 struct ust_app_session *ua_sess;
6460 struct ust_registry_session *registry;
6461 struct ust_registry_channel *ust_reg_chan;
6462
6463 rcu_read_lock();
6464
6465 /* Lookup application. If not found, there is a code flow error. */
6466 app = find_app_by_notify_sock(sock);
6467 if (!app) {
6468 DBG("Application socket %d is being torn down. Abort event notify",
6469 sock);
6470 ret = -1;
6471 goto error_rcu_unlock;
6472 }
6473
6474 /* Lookup channel by UST object descriptor. */
6475 ua_chan = find_channel_by_objd(app, cobjd);
6476 if (!ua_chan) {
6477 DBG("Application channel is being torn down. Abort event notify");
6478 ret = 0;
6479 goto error_rcu_unlock;
6480 }
6481
6482 LTTNG_ASSERT(ua_chan->session);
6483 ua_sess = ua_chan->session;
6484
6485 /* Get right session registry depending on the session buffer type. */
6486 registry = get_session_registry(ua_sess);
6487 if (!registry) {
6488 DBG("Application session is being torn down. Abort event notify");
6489 ret = 0;
6490 goto error_rcu_unlock;
6491 };
6492
6493 /* Depending on the buffer type, a different channel key is used. */
6494 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6495 chan_reg_key = ua_chan->tracing_channel_id;
6496 } else {
6497 chan_reg_key = ua_chan->key;
6498 }
6499
6500 pthread_mutex_lock(&registry->lock);
6501
6502 ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key);
6503 LTTNG_ASSERT(ust_reg_chan);
6504
6505 /* Channel id is set during the object creation. */
6506 chan_id = ust_reg_chan->chan_id;
6507
6508 ret = ust_app_fixup_legacy_context_fields(&nr_fields, &fields);
6509 if (ret < 0) {
6510 ERR("Registering application channel due to legacy context fields fixup error: pid = %d, sock = %d",
6511 app->pid, app->sock);
6512 ret_code = -EINVAL;
6513 goto reply;
6514 }
6515 if (!ust_reg_chan->register_done) {
6516 /*
6517 * TODO: eventually use the registry event count for
6518 * this channel to better guess header type for per-pid
6519 * buffers.
6520 */
6521 type = LTTNG_UST_CTL_CHANNEL_HEADER_LARGE;
6522 ust_reg_chan->nr_ctx_fields = nr_fields;
6523 ust_reg_chan->ctx_fields = fields;
6524 fields = NULL;
6525 ust_reg_chan->header_type = type;
6526 } else {
6527 /* Get current already assigned values. */
6528 type = ust_reg_chan->header_type;
6529 /*
6530 * Validate that the context fields match between
6531 * registry and newcoming application.
6532 */
6533 if (!match_lttng_ust_ctl_field_array(ust_reg_chan->ctx_fields,
6534 ust_reg_chan->nr_ctx_fields,
6535 fields, nr_fields)) {
6536 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6537 app->pid, app->sock);
6538 ret_code = -EINVAL;
6539 goto reply;
6540 }
6541 }
6542
6543 /* Append to metadata */
6544 if (!ust_reg_chan->metadata_dumped) {
6545 ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan);
6546 if (ret_code) {
6547 ERR("Error appending channel metadata (errno = %d)", ret_code);
6548 goto reply;
6549 }
6550 }
6551
6552 reply:
6553 DBG3("UST app replying to register channel key %" PRIu64
6554 " with id %u, type = %d, ret = %d", chan_reg_key, chan_id, type,
6555 ret_code);
6556
6557 ret = lttng_ust_ctl_reply_register_channel(sock, chan_id, type, ret_code);
6558 if (ret < 0) {
6559 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6560 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6561 app->pid, app->sock);
6562 } else if (ret == -EAGAIN) {
6563 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6564 app->pid, app->sock);
6565 } else {
6566 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6567 ret, app->pid, app->sock);
6568 }
6569 goto error;
6570 }
6571
6572 /* This channel registry registration is completed. */
6573 ust_reg_chan->register_done = 1;
6574
6575 error:
6576 pthread_mutex_unlock(&registry->lock);
6577 error_rcu_unlock:
6578 rcu_read_unlock();
6579 free(fields);
6580 return ret;
6581 }
6582
6583 /*
6584 * Add event to the UST channel registry. When the event is added to the
6585 * registry, the metadata is also created. Once done, this replies to the
6586 * application with the appropriate error code.
6587 *
6588 * The session UST registry lock is acquired in the function.
6589 *
6590 * On success 0 is returned else a negative value.
6591 */
6592 static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
6593 char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields,
6594 int loglevel_value, char *model_emf_uri)
6595 {
6596 int ret, ret_code;
6597 uint32_t event_id = 0;
6598 uint64_t chan_reg_key;
6599 struct ust_app *app;
6600 struct ust_app_channel *ua_chan;
6601 struct ust_app_session *ua_sess;
6602 struct ust_registry_session *registry;
6603
6604 rcu_read_lock();
6605
6606 /* Lookup application. If not found, there is a code flow error. */
6607 app = find_app_by_notify_sock(sock);
6608 if (!app) {
6609 DBG("Application socket %d is being torn down. Abort event notify",
6610 sock);
6611 ret = -1;
6612 goto error_rcu_unlock;
6613 }
6614
6615 /* Lookup channel by UST object descriptor. */
6616 ua_chan = find_channel_by_objd(app, cobjd);
6617 if (!ua_chan) {
6618 DBG("Application channel is being torn down. Abort event notify");
6619 ret = 0;
6620 goto error_rcu_unlock;
6621 }
6622
6623 LTTNG_ASSERT(ua_chan->session);
6624 ua_sess = ua_chan->session;
6625
6626 registry = get_session_registry(ua_sess);
6627 if (!registry) {
6628 DBG("Application session is being torn down. Abort event notify");
6629 ret = 0;
6630 goto error_rcu_unlock;
6631 }
6632
6633 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6634 chan_reg_key = ua_chan->tracing_channel_id;
6635 } else {
6636 chan_reg_key = ua_chan->key;
6637 }
6638
6639 pthread_mutex_lock(&registry->lock);
6640
6641 /*
6642 * From this point on, this call acquires the ownership of the sig, fields
6643 * and model_emf_uri meaning any free are done inside it if needed. These
6644 * three variables MUST NOT be read/write after this.
6645 */
6646 ret_code = ust_registry_create_event(registry, chan_reg_key,
6647 sobjd, cobjd, name, sig, nr_fields, fields,
6648 loglevel_value, model_emf_uri, ua_sess->buffer_type,
6649 &event_id, app);
6650 sig = NULL;
6651 fields = NULL;
6652 model_emf_uri = NULL;
6653
6654 /*
6655 * The return value is returned to ustctl so in case of an error, the
6656 * application can be notified. In case of an error, it's important not to
6657 * return a negative error or else the application will get closed.
6658 */
6659 ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code);
6660 if (ret < 0) {
6661 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6662 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6663 app->pid, app->sock);
6664 } else if (ret == -EAGAIN) {
6665 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6666 app->pid, app->sock);
6667 } else {
6668 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6669 ret, app->pid, app->sock);
6670 }
6671 /*
6672 * No need to wipe the create event since the application socket will
6673 * get close on error hence cleaning up everything by itself.
6674 */
6675 goto error;
6676 }
6677
6678 DBG3("UST registry event %s with id %" PRId32 " added successfully",
6679 name, event_id);
6680
6681 error:
6682 pthread_mutex_unlock(&registry->lock);
6683 error_rcu_unlock:
6684 rcu_read_unlock();
6685 free(sig);
6686 free(fields);
6687 free(model_emf_uri);
6688 return ret;
6689 }
6690
6691 /*
6692 * Add enum to the UST session registry. Once done, this replies to the
6693 * application with the appropriate error code.
6694 *
6695 * The session UST registry lock is acquired within this function.
6696 *
6697 * On success 0 is returned else a negative value.
6698 */
6699 static int add_enum_ust_registry(int sock, int sobjd, char *name,
6700 struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries)
6701 {
6702 int ret = 0, ret_code;
6703 struct ust_app *app;
6704 struct ust_app_session *ua_sess;
6705 struct ust_registry_session *registry;
6706 uint64_t enum_id = -1ULL;
6707
6708 rcu_read_lock();
6709
6710 /* Lookup application. If not found, there is a code flow error. */
6711 app = find_app_by_notify_sock(sock);
6712 if (!app) {
6713 /* Return an error since this is not an error */
6714 DBG("Application socket %d is being torn down. Aborting enum registration",
6715 sock);
6716 free(entries);
6717 ret = -1;
6718 goto error_rcu_unlock;
6719 }
6720
6721 /* Lookup session by UST object descriptor. */
6722 ua_sess = find_session_by_objd(app, sobjd);
6723 if (!ua_sess) {
6724 /* Return an error since this is not an error */
6725 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6726 free(entries);
6727 goto error_rcu_unlock;
6728 }
6729
6730 registry = get_session_registry(ua_sess);
6731 if (!registry) {
6732 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6733 free(entries);
6734 goto error_rcu_unlock;
6735 }
6736
6737 pthread_mutex_lock(&registry->lock);
6738
6739 /*
6740 * From this point on, the callee acquires the ownership of
6741 * entries. The variable entries MUST NOT be read/written after
6742 * call.
6743 */
6744 ret_code = ust_registry_create_or_find_enum(registry, sobjd, name,
6745 entries, nr_entries, &enum_id);
6746 entries = NULL;
6747
6748 /*
6749 * The return value is returned to ustctl so in case of an error, the
6750 * application can be notified. In case of an error, it's important not to
6751 * return a negative error or else the application will get closed.
6752 */
6753 ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, ret_code);
6754 if (ret < 0) {
6755 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6756 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6757 app->pid, app->sock);
6758 } else if (ret == -EAGAIN) {
6759 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6760 app->pid, app->sock);
6761 } else {
6762 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6763 ret, app->pid, app->sock);
6764 }
6765 /*
6766 * No need to wipe the create enum since the application socket will
6767 * get close on error hence cleaning up everything by itself.
6768 */
6769 goto error;
6770 }
6771
6772 DBG3("UST registry enum %s added successfully or already found", name);
6773
6774 error:
6775 pthread_mutex_unlock(&registry->lock);
6776 error_rcu_unlock:
6777 rcu_read_unlock();
6778 return ret;
6779 }
6780
6781 /*
6782 * Handle application notification through the given notify socket.
6783 *
6784 * Return 0 on success or else a negative value.
6785 */
6786 int ust_app_recv_notify(int sock)
6787 {
6788 int ret;
6789 enum lttng_ust_ctl_notify_cmd cmd;
6790
6791 DBG3("UST app receiving notify from sock %d", sock);
6792
6793 ret = lttng_ust_ctl_recv_notify(sock, &cmd);
6794 if (ret < 0) {
6795 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6796 DBG3("UST app recv notify failed. Application died: sock = %d",
6797 sock);
6798 } else if (ret == -EAGAIN) {
6799 WARN("UST app recv notify failed. Communication time out: sock = %d",
6800 sock);
6801 } else {
6802 ERR("UST app recv notify failed with ret %d: sock = %d",
6803 ret, sock);
6804 }
6805 goto error;
6806 }
6807
6808 switch (cmd) {
6809 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT:
6810 {
6811 int sobjd, cobjd, loglevel_value;
6812 char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri;
6813 size_t nr_fields;
6814 struct lttng_ust_ctl_field *fields;
6815
6816 DBG2("UST app ustctl register event received");
6817
6818 ret = lttng_ust_ctl_recv_register_event(sock, &sobjd, &cobjd, name,
6819 &loglevel_value, &sig, &nr_fields, &fields,
6820 &model_emf_uri);
6821 if (ret < 0) {
6822 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6823 DBG3("UST app recv event failed. Application died: sock = %d",
6824 sock);
6825 } else if (ret == -EAGAIN) {
6826 WARN("UST app recv event failed. Communication time out: sock = %d",
6827 sock);
6828 } else {
6829 ERR("UST app recv event failed with ret %d: sock = %d",
6830 ret, sock);
6831 }
6832 goto error;
6833 }
6834
6835 /*
6836 * Add event to the UST registry coming from the notify socket. This
6837 * call will free if needed the sig, fields and model_emf_uri. This
6838 * code path loses the ownsership of these variables and transfer them
6839 * to the this function.
6840 */
6841 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
6842 fields, loglevel_value, model_emf_uri);
6843 if (ret < 0) {
6844 goto error;
6845 }
6846
6847 break;
6848 }
6849 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL:
6850 {
6851 int sobjd, cobjd;
6852 size_t nr_fields;
6853 struct lttng_ust_ctl_field *fields;
6854
6855 DBG2("UST app ustctl register channel received");
6856
6857 ret = lttng_ust_ctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
6858 &fields);
6859 if (ret < 0) {
6860 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6861 DBG3("UST app recv channel failed. Application died: sock = %d",
6862 sock);
6863 } else if (ret == -EAGAIN) {
6864 WARN("UST app recv channel failed. Communication time out: sock = %d",
6865 sock);
6866 } else {
6867 ERR("UST app recv channel failed with ret %d: sock = %d",
6868 ret, sock);
6869 }
6870 goto error;
6871 }
6872
6873 /*
6874 * The fields ownership are transfered to this function call meaning
6875 * that if needed it will be freed. After this, it's invalid to access
6876 * fields or clean it up.
6877 */
6878 ret = reply_ust_register_channel(sock, cobjd, nr_fields,
6879 fields);
6880 if (ret < 0) {
6881 goto error;
6882 }
6883
6884 break;
6885 }
6886 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM:
6887 {
6888 int sobjd;
6889 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
6890 size_t nr_entries;
6891 struct lttng_ust_ctl_enum_entry *entries;
6892
6893 DBG2("UST app ustctl register enum received");
6894
6895 ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name,
6896 &entries, &nr_entries);
6897 if (ret < 0) {
6898 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6899 DBG3("UST app recv enum failed. Application died: sock = %d",
6900 sock);
6901 } else if (ret == -EAGAIN) {
6902 WARN("UST app recv enum failed. Communication time out: sock = %d",
6903 sock);
6904 } else {
6905 ERR("UST app recv enum failed with ret %d: sock = %d",
6906 ret, sock);
6907 }
6908 goto error;
6909 }
6910
6911 /* Callee assumes ownership of entries */
6912 ret = add_enum_ust_registry(sock, sobjd, name,
6913 entries, nr_entries);
6914 if (ret < 0) {
6915 goto error;
6916 }
6917
6918 break;
6919 }
6920 default:
6921 /* Should NEVER happen. */
6922 abort();
6923 }
6924
6925 error:
6926 return ret;
6927 }
6928
6929 /*
6930 * Once the notify socket hangs up, this is called. First, it tries to find the
6931 * corresponding application. On failure, the call_rcu to close the socket is
6932 * executed. If an application is found, it tries to delete it from the notify
6933 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6934 *
6935 * Note that an object needs to be allocated here so on ENOMEM failure, the
6936 * call RCU is not done but the rest of the cleanup is.
6937 */
6938 void ust_app_notify_sock_unregister(int sock)
6939 {
6940 int err_enomem = 0;
6941 struct lttng_ht_iter iter;
6942 struct ust_app *app;
6943 struct ust_app_notify_sock_obj *obj;
6944
6945 LTTNG_ASSERT(sock >= 0);
6946
6947 rcu_read_lock();
6948
6949 obj = (ust_app_notify_sock_obj *) zmalloc(sizeof(*obj));
6950 if (!obj) {
6951 /*
6952 * An ENOMEM is kind of uncool. If this strikes we continue the
6953 * procedure but the call_rcu will not be called. In this case, we
6954 * accept the fd leak rather than possibly creating an unsynchronized
6955 * state between threads.
6956 *
6957 * TODO: The notify object should be created once the notify socket is
6958 * registered and stored independantely from the ust app object. The
6959 * tricky part is to synchronize the teardown of the application and
6960 * this notify object. Let's keep that in mind so we can avoid this
6961 * kind of shenanigans with ENOMEM in the teardown path.
6962 */
6963 err_enomem = 1;
6964 } else {
6965 obj->fd = sock;
6966 }
6967
6968 DBG("UST app notify socket unregister %d", sock);
6969
6970 /*
6971 * Lookup application by notify socket. If this fails, this means that the
6972 * hash table delete has already been done by the application
6973 * unregistration process so we can safely close the notify socket in a
6974 * call RCU.
6975 */
6976 app = find_app_by_notify_sock(sock);
6977 if (!app) {
6978 goto close_socket;
6979 }
6980
6981 iter.iter.node = &app->notify_sock_n.node;
6982
6983 /*
6984 * Whatever happens here either we fail or succeed, in both cases we have
6985 * to close the socket after a grace period to continue to the call RCU
6986 * here. If the deletion is successful, the application is not visible
6987 * anymore by other threads and is it fails it means that it was already
6988 * deleted from the hash table so either way we just have to close the
6989 * socket.
6990 */
6991 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
6992
6993 close_socket:
6994 rcu_read_unlock();
6995
6996 /*
6997 * Close socket after a grace period to avoid for the socket to be reused
6998 * before the application object is freed creating potential race between
6999 * threads trying to add unique in the global hash table.
7000 */
7001 if (!err_enomem) {
7002 call_rcu(&obj->head, close_notify_sock_rcu);
7003 }
7004 }
7005
7006 /*
7007 * Destroy a ust app data structure and free its memory.
7008 */
7009 void ust_app_destroy(struct ust_app *app)
7010 {
7011 if (!app) {
7012 return;
7013 }
7014
7015 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
7016 }
7017
7018 /*
7019 * Take a snapshot for a given UST session. The snapshot is sent to the given
7020 * output.
7021 *
7022 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7023 */
7024 enum lttng_error_code ust_app_snapshot_record(
7025 const struct ltt_ust_session *usess,
7026 const struct consumer_output *output, int wait,
7027 uint64_t nb_packets_per_stream)
7028 {
7029 int ret = 0;
7030 enum lttng_error_code status = LTTNG_OK;
7031 struct lttng_ht_iter iter;
7032 struct ust_app *app;
7033 char *trace_path = NULL;
7034
7035 LTTNG_ASSERT(usess);
7036 LTTNG_ASSERT(output);
7037
7038 rcu_read_lock();
7039
7040 switch (usess->buffer_type) {
7041 case LTTNG_BUFFER_PER_UID:
7042 {
7043 struct buffer_reg_uid *reg;
7044
7045 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7046 struct buffer_reg_channel *buf_reg_chan;
7047 struct consumer_socket *socket;
7048 char pathname[PATH_MAX];
7049 size_t consumer_path_offset = 0;
7050
7051 if (!reg->registry->reg.ust->metadata_key) {
7052 /* Skip since no metadata is present */
7053 continue;
7054 }
7055
7056 /* Get consumer socket to use to push the metadata.*/
7057 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7058 usess->consumer);
7059 if (!socket) {
7060 status = LTTNG_ERR_INVALID;
7061 goto error;
7062 }
7063
7064 memset(pathname, 0, sizeof(pathname));
7065 ret = snprintf(pathname, sizeof(pathname),
7066 DEFAULT_UST_TRACE_UID_PATH,
7067 reg->uid, reg->bits_per_long);
7068 if (ret < 0) {
7069 PERROR("snprintf snapshot path");
7070 status = LTTNG_ERR_INVALID;
7071 goto error;
7072 }
7073 /* Free path allowed on previous iteration. */
7074 free(trace_path);
7075 trace_path = setup_channel_trace_path(usess->consumer, pathname,
7076 &consumer_path_offset);
7077 if (!trace_path) {
7078 status = LTTNG_ERR_INVALID;
7079 goto error;
7080 }
7081 /* Add the UST default trace dir to path. */
7082 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7083 buf_reg_chan, node.node) {
7084 status = consumer_snapshot_channel(socket,
7085 buf_reg_chan->consumer_key,
7086 output, 0, usess->uid,
7087 usess->gid, &trace_path[consumer_path_offset], wait,
7088 nb_packets_per_stream);
7089 if (status != LTTNG_OK) {
7090 goto error;
7091 }
7092 }
7093 status = consumer_snapshot_channel(socket,
7094 reg->registry->reg.ust->metadata_key, output, 1,
7095 usess->uid, usess->gid, &trace_path[consumer_path_offset],
7096 wait, 0);
7097 if (status != LTTNG_OK) {
7098 goto error;
7099 }
7100 }
7101 break;
7102 }
7103 case LTTNG_BUFFER_PER_PID:
7104 {
7105 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7106 struct consumer_socket *socket;
7107 struct lttng_ht_iter chan_iter;
7108 struct ust_app_channel *ua_chan;
7109 struct ust_app_session *ua_sess;
7110 struct ust_registry_session *registry;
7111 char pathname[PATH_MAX];
7112 size_t consumer_path_offset = 0;
7113
7114 ua_sess = lookup_session_by_app(usess, app);
7115 if (!ua_sess) {
7116 /* Session not associated with this app. */
7117 continue;
7118 }
7119
7120 /* Get the right consumer socket for the application. */
7121 socket = consumer_find_socket_by_bitness(app->bits_per_long,
7122 output);
7123 if (!socket) {
7124 status = LTTNG_ERR_INVALID;
7125 goto error;
7126 }
7127
7128 /* Add the UST default trace dir to path. */
7129 memset(pathname, 0, sizeof(pathname));
7130 ret = snprintf(pathname, sizeof(pathname), "%s",
7131 ua_sess->path);
7132 if (ret < 0) {
7133 status = LTTNG_ERR_INVALID;
7134 PERROR("snprintf snapshot path");
7135 goto error;
7136 }
7137 /* Free path allowed on previous iteration. */
7138 free(trace_path);
7139 trace_path = setup_channel_trace_path(usess->consumer, pathname,
7140 &consumer_path_offset);
7141 if (!trace_path) {
7142 status = LTTNG_ERR_INVALID;
7143 goto error;
7144 }
7145 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7146 ua_chan, node.node) {
7147 status = consumer_snapshot_channel(socket,
7148 ua_chan->key, output, 0,
7149 lttng_credentials_get_uid(&ua_sess->effective_credentials),
7150 lttng_credentials_get_gid(&ua_sess->effective_credentials),
7151 &trace_path[consumer_path_offset], wait,
7152 nb_packets_per_stream);
7153 switch (status) {
7154 case LTTNG_OK:
7155 break;
7156 case LTTNG_ERR_CHAN_NOT_FOUND:
7157 continue;
7158 default:
7159 goto error;
7160 }
7161 }
7162
7163 registry = get_session_registry(ua_sess);
7164 if (!registry) {
7165 DBG("Application session is being torn down. Skip application.");
7166 continue;
7167 }
7168 status = consumer_snapshot_channel(socket,
7169 registry->metadata_key, output, 1,
7170 lttng_credentials_get_uid(&ua_sess->effective_credentials),
7171 lttng_credentials_get_gid(&ua_sess->effective_credentials),
7172 &trace_path[consumer_path_offset], wait, 0);
7173 switch (status) {
7174 case LTTNG_OK:
7175 break;
7176 case LTTNG_ERR_CHAN_NOT_FOUND:
7177 continue;
7178 default:
7179 goto error;
7180 }
7181 }
7182 break;
7183 }
7184 default:
7185 abort();
7186 break;
7187 }
7188
7189 error:
7190 free(trace_path);
7191 rcu_read_unlock();
7192 return status;
7193 }
7194
7195 /*
7196 * Return the size taken by one more packet per stream.
7197 */
7198 uint64_t ust_app_get_size_one_more_packet_per_stream(
7199 const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
7200 {
7201 uint64_t tot_size = 0;
7202 struct ust_app *app;
7203 struct lttng_ht_iter iter;
7204
7205 LTTNG_ASSERT(usess);
7206
7207 switch (usess->buffer_type) {
7208 case LTTNG_BUFFER_PER_UID:
7209 {
7210 struct buffer_reg_uid *reg;
7211
7212 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7213 struct buffer_reg_channel *buf_reg_chan;
7214
7215 rcu_read_lock();
7216 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7217 buf_reg_chan, node.node) {
7218 if (cur_nr_packets >= buf_reg_chan->num_subbuf) {
7219 /*
7220 * Don't take channel into account if we
7221 * already grab all its packets.
7222 */
7223 continue;
7224 }
7225 tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count;
7226 }
7227 rcu_read_unlock();
7228 }
7229 break;
7230 }
7231 case LTTNG_BUFFER_PER_PID:
7232 {
7233 rcu_read_lock();
7234 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7235 struct ust_app_channel *ua_chan;
7236 struct ust_app_session *ua_sess;
7237 struct lttng_ht_iter chan_iter;
7238
7239 ua_sess = lookup_session_by_app(usess, app);
7240 if (!ua_sess) {
7241 /* Session not associated with this app. */
7242 continue;
7243 }
7244
7245 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7246 ua_chan, node.node) {
7247 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
7248 /*
7249 * Don't take channel into account if we
7250 * already grab all its packets.
7251 */
7252 continue;
7253 }
7254 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
7255 }
7256 }
7257 rcu_read_unlock();
7258 break;
7259 }
7260 default:
7261 abort();
7262 break;
7263 }
7264
7265 return tot_size;
7266 }
7267
7268 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
7269 struct cds_list_head *buffer_reg_uid_list,
7270 struct consumer_output *consumer, uint64_t uchan_id,
7271 int overwrite, uint64_t *discarded, uint64_t *lost)
7272 {
7273 int ret;
7274 uint64_t consumer_chan_key;
7275
7276 *discarded = 0;
7277 *lost = 0;
7278
7279 ret = buffer_reg_uid_consumer_channel_key(
7280 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
7281 if (ret < 0) {
7282 /* Not found */
7283 ret = 0;
7284 goto end;
7285 }
7286
7287 if (overwrite) {
7288 ret = consumer_get_lost_packets(ust_session_id,
7289 consumer_chan_key, consumer, lost);
7290 } else {
7291 ret = consumer_get_discarded_events(ust_session_id,
7292 consumer_chan_key, consumer, discarded);
7293 }
7294
7295 end:
7296 return ret;
7297 }
7298
7299 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
7300 struct ltt_ust_channel *uchan,
7301 struct consumer_output *consumer, int overwrite,
7302 uint64_t *discarded, uint64_t *lost)
7303 {
7304 int ret = 0;
7305 struct lttng_ht_iter iter;
7306 struct lttng_ht_node_str *ua_chan_node;
7307 struct ust_app *app;
7308 struct ust_app_session *ua_sess;
7309 struct ust_app_channel *ua_chan;
7310
7311 *discarded = 0;
7312 *lost = 0;
7313
7314 rcu_read_lock();
7315 /*
7316 * Iterate over every registered applications. Sum counters for
7317 * all applications containing requested session and channel.
7318 */
7319 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7320 struct lttng_ht_iter uiter;
7321
7322 ua_sess = lookup_session_by_app(usess, app);
7323 if (ua_sess == NULL) {
7324 continue;
7325 }
7326
7327 /* Get channel */
7328 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
7329 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
7330 /* If the session is found for the app, the channel must be there */
7331 LTTNG_ASSERT(ua_chan_node);
7332
7333 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
7334
7335 if (overwrite) {
7336 uint64_t _lost;
7337
7338 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
7339 consumer, &_lost);
7340 if (ret < 0) {
7341 break;
7342 }
7343 (*lost) += _lost;
7344 } else {
7345 uint64_t _discarded;
7346
7347 ret = consumer_get_discarded_events(usess->id,
7348 ua_chan->key, consumer, &_discarded);
7349 if (ret < 0) {
7350 break;
7351 }
7352 (*discarded) += _discarded;
7353 }
7354 }
7355
7356 rcu_read_unlock();
7357 return ret;
7358 }
7359
7360 static
7361 int ust_app_regenerate_statedump(struct ltt_ust_session *usess,
7362 struct ust_app *app)
7363 {
7364 int ret = 0;
7365 struct ust_app_session *ua_sess;
7366
7367 DBG("Regenerating the metadata for ust app pid %d", app->pid);
7368
7369 rcu_read_lock();
7370
7371 ua_sess = lookup_session_by_app(usess, app);
7372 if (ua_sess == NULL) {
7373 /* The session is in teardown process. Ignore and continue. */
7374 goto end;
7375 }
7376
7377 pthread_mutex_lock(&ua_sess->lock);
7378
7379 if (ua_sess->deleted) {
7380 goto end_unlock;
7381 }
7382
7383 pthread_mutex_lock(&app->sock_lock);
7384 ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle);
7385 pthread_mutex_unlock(&app->sock_lock);
7386
7387 end_unlock:
7388 pthread_mutex_unlock(&ua_sess->lock);
7389
7390 end:
7391 rcu_read_unlock();
7392 health_code_update();
7393 return ret;
7394 }
7395
7396 /*
7397 * Regenerate the statedump for each app in the session.
7398 */
7399 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
7400 {
7401 int ret = 0;
7402 struct lttng_ht_iter iter;
7403 struct ust_app *app;
7404
7405 DBG("Regenerating the metadata for all UST apps");
7406
7407 rcu_read_lock();
7408
7409 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7410 if (!app->compatible) {
7411 continue;
7412 }
7413
7414 ret = ust_app_regenerate_statedump(usess, app);
7415 if (ret < 0) {
7416 /* Continue to the next app even on error */
7417 continue;
7418 }
7419 }
7420
7421 rcu_read_unlock();
7422
7423 return 0;
7424 }
7425
7426 /*
7427 * Rotate all the channels of a session.
7428 *
7429 * Return LTTNG_OK on success or else an LTTng error code.
7430 */
7431 enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
7432 {
7433 int ret;
7434 enum lttng_error_code cmd_ret = LTTNG_OK;
7435 struct lttng_ht_iter iter;
7436 struct ust_app *app;
7437 struct ltt_ust_session *usess = session->ust_session;
7438
7439 LTTNG_ASSERT(usess);
7440
7441 rcu_read_lock();
7442
7443 switch (usess->buffer_type) {
7444 case LTTNG_BUFFER_PER_UID:
7445 {
7446 struct buffer_reg_uid *reg;
7447
7448 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7449 struct buffer_reg_channel *buf_reg_chan;
7450 struct consumer_socket *socket;
7451
7452 /* Get consumer socket to use to push the metadata.*/
7453 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7454 usess->consumer);
7455 if (!socket) {
7456 cmd_ret = LTTNG_ERR_INVALID;
7457 goto error;
7458 }
7459
7460 /* Rotate the data channels. */
7461 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7462 buf_reg_chan, node.node) {
7463 ret = consumer_rotate_channel(socket,
7464 buf_reg_chan->consumer_key,
7465 usess->uid, usess->gid,
7466 usess->consumer,
7467 /* is_metadata_channel */ false);
7468 if (ret < 0) {
7469 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7470 goto error;
7471 }
7472 }
7473
7474 /*
7475 * The metadata channel might not be present.
7476 *
7477 * Consumer stream allocation can be done
7478 * asynchronously and can fail on intermediary
7479 * operations (i.e add context) and lead to data
7480 * channels created with no metadata channel.
7481 */
7482 if (!reg->registry->reg.ust->metadata_key) {
7483 /* Skip since no metadata is present. */
7484 continue;
7485 }
7486
7487 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
7488
7489 ret = consumer_rotate_channel(socket,
7490 reg->registry->reg.ust->metadata_key,
7491 usess->uid, usess->gid,
7492 usess->consumer,
7493 /* is_metadata_channel */ true);
7494 if (ret < 0) {
7495 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7496 goto error;
7497 }
7498 }
7499 break;
7500 }
7501 case LTTNG_BUFFER_PER_PID:
7502 {
7503 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7504 struct consumer_socket *socket;
7505 struct lttng_ht_iter chan_iter;
7506 struct ust_app_channel *ua_chan;
7507 struct ust_app_session *ua_sess;
7508 struct ust_registry_session *registry;
7509
7510 ua_sess = lookup_session_by_app(usess, app);
7511 if (!ua_sess) {
7512 /* Session not associated with this app. */
7513 continue;
7514 }
7515
7516 /* Get the right consumer socket for the application. */
7517 socket = consumer_find_socket_by_bitness(app->bits_per_long,
7518 usess->consumer);
7519 if (!socket) {
7520 cmd_ret = LTTNG_ERR_INVALID;
7521 goto error;
7522 }
7523
7524 registry = get_session_registry(ua_sess);
7525 if (!registry) {
7526 DBG("Application session is being torn down. Skip application.");
7527 continue;
7528 }
7529
7530 /* Rotate the data channels. */
7531 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7532 ua_chan, node.node) {
7533 ret = consumer_rotate_channel(socket,
7534 ua_chan->key,
7535 lttng_credentials_get_uid(&ua_sess->effective_credentials),
7536 lttng_credentials_get_gid(&ua_sess->effective_credentials),
7537 ua_sess->consumer,
7538 /* is_metadata_channel */ false);
7539 if (ret < 0) {
7540 /* Per-PID buffer and application going away. */
7541 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7542 continue;
7543 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7544 goto error;
7545 }
7546 }
7547
7548 /* Rotate the metadata channel. */
7549 (void) push_metadata(registry, usess->consumer);
7550 ret = consumer_rotate_channel(socket,
7551 registry->metadata_key,
7552 lttng_credentials_get_uid(&ua_sess->effective_credentials),
7553 lttng_credentials_get_gid(&ua_sess->effective_credentials),
7554 ua_sess->consumer,
7555 /* is_metadata_channel */ true);
7556 if (ret < 0) {
7557 /* Per-PID buffer and application going away. */
7558 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7559 continue;
7560 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7561 goto error;
7562 }
7563 }
7564 break;
7565 }
7566 default:
7567 abort();
7568 break;
7569 }
7570
7571 cmd_ret = LTTNG_OK;
7572
7573 error:
7574 rcu_read_unlock();
7575 return cmd_ret;
7576 }
7577
7578 enum lttng_error_code ust_app_create_channel_subdirectories(
7579 const struct ltt_ust_session *usess)
7580 {
7581 enum lttng_error_code ret = LTTNG_OK;
7582 struct lttng_ht_iter iter;
7583 enum lttng_trace_chunk_status chunk_status;
7584 char *pathname_index;
7585 int fmt_ret;
7586
7587 LTTNG_ASSERT(usess->current_trace_chunk);
7588 rcu_read_lock();
7589
7590 switch (usess->buffer_type) {
7591 case LTTNG_BUFFER_PER_UID:
7592 {
7593 struct buffer_reg_uid *reg;
7594
7595 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7596 fmt_ret = asprintf(&pathname_index,
7597 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
7598 reg->uid, reg->bits_per_long);
7599 if (fmt_ret < 0) {
7600 ERR("Failed to format channel index directory");
7601 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7602 goto error;
7603 }
7604
7605 /*
7606 * Create the index subdirectory which will take care
7607 * of implicitly creating the channel's path.
7608 */
7609 chunk_status = lttng_trace_chunk_create_subdirectory(
7610 usess->current_trace_chunk,
7611 pathname_index);
7612 free(pathname_index);
7613 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7614 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7615 goto error;
7616 }
7617 }
7618 break;
7619 }
7620 case LTTNG_BUFFER_PER_PID:
7621 {
7622 struct ust_app *app;
7623
7624 /*
7625 * Create the toplevel ust/ directory in case no apps are running.
7626 */
7627 chunk_status = lttng_trace_chunk_create_subdirectory(
7628 usess->current_trace_chunk,
7629 DEFAULT_UST_TRACE_DIR);
7630 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7631 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7632 goto error;
7633 }
7634
7635 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
7636 pid_n.node) {
7637 struct ust_app_session *ua_sess;
7638 struct ust_registry_session *registry;
7639
7640 ua_sess = lookup_session_by_app(usess, app);
7641 if (!ua_sess) {
7642 /* Session not associated with this app. */
7643 continue;
7644 }
7645
7646 registry = get_session_registry(ua_sess);
7647 if (!registry) {
7648 DBG("Application session is being torn down. Skip application.");
7649 continue;
7650 }
7651
7652 fmt_ret = asprintf(&pathname_index,
7653 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
7654 ua_sess->path);
7655 if (fmt_ret < 0) {
7656 ERR("Failed to format channel index directory");
7657 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7658 goto error;
7659 }
7660 /*
7661 * Create the index subdirectory which will take care
7662 * of implicitly creating the channel's path.
7663 */
7664 chunk_status = lttng_trace_chunk_create_subdirectory(
7665 usess->current_trace_chunk,
7666 pathname_index);
7667 free(pathname_index);
7668 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7669 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7670 goto error;
7671 }
7672 }
7673 break;
7674 }
7675 default:
7676 abort();
7677 }
7678
7679 ret = LTTNG_OK;
7680 error:
7681 rcu_read_unlock();
7682 return ret;
7683 }
7684
7685 /*
7686 * Clear all the channels of a session.
7687 *
7688 * Return LTTNG_OK on success or else an LTTng error code.
7689 */
7690 enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
7691 {
7692 int ret;
7693 enum lttng_error_code cmd_ret = LTTNG_OK;
7694 struct lttng_ht_iter iter;
7695 struct ust_app *app;
7696 struct ltt_ust_session *usess = session->ust_session;
7697
7698 LTTNG_ASSERT(usess);
7699
7700 rcu_read_lock();
7701
7702 if (usess->active) {
7703 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
7704 cmd_ret = LTTNG_ERR_FATAL;
7705 goto end;
7706 }
7707
7708 switch (usess->buffer_type) {
7709 case LTTNG_BUFFER_PER_UID:
7710 {
7711 struct buffer_reg_uid *reg;
7712
7713 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7714 struct buffer_reg_channel *buf_reg_chan;
7715 struct consumer_socket *socket;
7716
7717 /* Get consumer socket to use to push the metadata.*/
7718 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7719 usess->consumer);
7720 if (!socket) {
7721 cmd_ret = LTTNG_ERR_INVALID;
7722 goto error_socket;
7723 }
7724
7725 /* Clear the data channels. */
7726 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7727 buf_reg_chan, node.node) {
7728 ret = consumer_clear_channel(socket,
7729 buf_reg_chan->consumer_key);
7730 if (ret < 0) {
7731 goto error;
7732 }
7733 }
7734
7735 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
7736
7737 /*
7738 * Clear the metadata channel.
7739 * Metadata channel is not cleared per se but we still need to
7740 * perform a rotation operation on it behind the scene.
7741 */
7742 ret = consumer_clear_channel(socket,
7743 reg->registry->reg.ust->metadata_key);
7744 if (ret < 0) {
7745 goto error;
7746 }
7747 }
7748 break;
7749 }
7750 case LTTNG_BUFFER_PER_PID:
7751 {
7752 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7753 struct consumer_socket *socket;
7754 struct lttng_ht_iter chan_iter;
7755 struct ust_app_channel *ua_chan;
7756 struct ust_app_session *ua_sess;
7757 struct ust_registry_session *registry;
7758
7759 ua_sess = lookup_session_by_app(usess, app);
7760 if (!ua_sess) {
7761 /* Session not associated with this app. */
7762 continue;
7763 }
7764
7765 /* Get the right consumer socket for the application. */
7766 socket = consumer_find_socket_by_bitness(app->bits_per_long,
7767 usess->consumer);
7768 if (!socket) {
7769 cmd_ret = LTTNG_ERR_INVALID;
7770 goto error_socket;
7771 }
7772
7773 registry = get_session_registry(ua_sess);
7774 if (!registry) {
7775 DBG("Application session is being torn down. Skip application.");
7776 continue;
7777 }
7778
7779 /* Clear the data channels. */
7780 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7781 ua_chan, node.node) {
7782 ret = consumer_clear_channel(socket, ua_chan->key);
7783 if (ret < 0) {
7784 /* Per-PID buffer and application going away. */
7785 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7786 continue;
7787 }
7788 goto error;
7789 }
7790 }
7791
7792 (void) push_metadata(registry, usess->consumer);
7793
7794 /*
7795 * Clear the metadata channel.
7796 * Metadata channel is not cleared per se but we still need to
7797 * perform rotation operation on it behind the scene.
7798 */
7799 ret = consumer_clear_channel(socket, registry->metadata_key);
7800 if (ret < 0) {
7801 /* Per-PID buffer and application going away. */
7802 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7803 continue;
7804 }
7805 goto error;
7806 }
7807 }
7808 break;
7809 }
7810 default:
7811 abort();
7812 break;
7813 }
7814
7815 cmd_ret = LTTNG_OK;
7816 goto end;
7817
7818 error:
7819 switch (-ret) {
7820 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
7821 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
7822 break;
7823 default:
7824 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
7825 }
7826
7827 error_socket:
7828 end:
7829 rcu_read_unlock();
7830 return cmd_ret;
7831 }
7832
7833 /*
7834 * This function skips the metadata channel as the begin/end timestamps of a
7835 * metadata packet are useless.
7836 *
7837 * Moreover, opening a packet after a "clear" will cause problems for live
7838 * sessions as it will introduce padding that was not part of the first trace
7839 * chunk. The relay daemon expects the content of the metadata stream of
7840 * successive metadata trace chunks to be strict supersets of one another.
7841 *
7842 * For example, flushing a packet at the beginning of the metadata stream of
7843 * a trace chunk resulting from a "clear" session command will cause the
7844 * size of the metadata stream of the new trace chunk to not match the size of
7845 * the metadata stream of the original chunk. This will confuse the relay
7846 * daemon as the same "offset" in a metadata stream will no longer point
7847 * to the same content.
7848 */
7849 enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
7850 {
7851 enum lttng_error_code ret = LTTNG_OK;
7852 struct lttng_ht_iter iter;
7853 struct ltt_ust_session *usess = session->ust_session;
7854
7855 LTTNG_ASSERT(usess);
7856
7857 rcu_read_lock();
7858
7859 switch (usess->buffer_type) {
7860 case LTTNG_BUFFER_PER_UID:
7861 {
7862 struct buffer_reg_uid *reg;
7863
7864 cds_list_for_each_entry (
7865 reg, &usess->buffer_reg_uid_list, lnode) {
7866 struct buffer_reg_channel *buf_reg_chan;
7867 struct consumer_socket *socket;
7868
7869 socket = consumer_find_socket_by_bitness(
7870 reg->bits_per_long, usess->consumer);
7871 if (!socket) {
7872 ret = LTTNG_ERR_FATAL;
7873 goto error;
7874 }
7875
7876 cds_lfht_for_each_entry(reg->registry->channels->ht,
7877 &iter.iter, buf_reg_chan, node.node) {
7878 const int open_ret =
7879 consumer_open_channel_packets(
7880 socket,
7881 buf_reg_chan->consumer_key);
7882
7883 if (open_ret < 0) {
7884 ret = LTTNG_ERR_UNK;
7885 goto error;
7886 }
7887 }
7888 }
7889 break;
7890 }
7891 case LTTNG_BUFFER_PER_PID:
7892 {
7893 struct ust_app *app;
7894
7895 cds_lfht_for_each_entry (
7896 ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7897 struct consumer_socket *socket;
7898 struct lttng_ht_iter chan_iter;
7899 struct ust_app_channel *ua_chan;
7900 struct ust_app_session *ua_sess;
7901 struct ust_registry_session *registry;
7902
7903 ua_sess = lookup_session_by_app(usess, app);
7904 if (!ua_sess) {
7905 /* Session not associated with this app. */
7906 continue;
7907 }
7908
7909 /* Get the right consumer socket for the application. */
7910 socket = consumer_find_socket_by_bitness(
7911 app->bits_per_long, usess->consumer);
7912 if (!socket) {
7913 ret = LTTNG_ERR_FATAL;
7914 goto error;
7915 }
7916
7917 registry = get_session_registry(ua_sess);
7918 if (!registry) {
7919 DBG("Application session is being torn down. Skip application.");
7920 continue;
7921 }
7922
7923 cds_lfht_for_each_entry(ua_sess->channels->ht,
7924 &chan_iter.iter, ua_chan, node.node) {
7925 const int open_ret =
7926 consumer_open_channel_packets(
7927 socket,
7928 ua_chan->key);
7929
7930 if (open_ret < 0) {
7931 /*
7932 * Per-PID buffer and application going
7933 * away.
7934 */
7935 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7936 continue;
7937 }
7938
7939 ret = LTTNG_ERR_UNK;
7940 goto error;
7941 }
7942 }
7943 }
7944 break;
7945 }
7946 default:
7947 abort();
7948 break;
7949 }
7950
7951 error:
7952 rcu_read_unlock();
7953 return ret;
7954 }
This page took 0.282118 seconds and 4 git commands to generate.