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