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