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