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