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