Fix double PID registration race
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
CommitLineData
91d76f53
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
91d76f53
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
91d76f53
DG
16 */
17
18#define _GNU_SOURCE
19#include <errno.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
099e26bd 23#include <string.h>
aba8e916
DG
24#include <sys/stat.h>
25#include <sys/types.h>
099e26bd 26#include <unistd.h>
0df502fd 27#include <urcu/compiler.h>
bec39940 28
990570ed 29#include <common/common.h>
1e307fab 30
56fff090 31#include "ust-app.h"
48842b30 32#include "ust-consumer.h"
d80a6244
DG
33#include "ust-ctl.h"
34
55cc08a6
DG
35/*
36 * Delete ust context safely. RCU read lock must be held before calling
37 * this function.
38 */
39static
40void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
41{
42 if (ua_ctx->obj) {
43 ustctl_release_object(sock, ua_ctx->obj);
44 free(ua_ctx->obj);
45 }
46 free(ua_ctx);
47}
48
d80a6244
DG
49/*
50 * Delete ust app event safely. RCU read lock must be held before calling
51 * this function.
52 */
8b366481
DG
53static
54void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
d80a6244 55{
55cc08a6 56 int ret;
bec39940 57 struct lttng_ht_iter iter;
55cc08a6
DG
58 struct ust_app_ctx *ua_ctx;
59
fc34caaa 60 /* Destroy each context of event */
bec39940
DG
61 cds_lfht_for_each_entry(ua_event->ctx->ht, &iter.iter, ua_ctx,
62 node.node) {
63 ret = lttng_ht_del(ua_event->ctx, &iter);
55cc08a6
DG
64 assert(!ret);
65 delete_ust_app_ctx(sock, ua_ctx);
66 }
bec39940 67 lttng_ht_destroy(ua_event->ctx);
d80a6244 68
edb67388
DG
69 if (ua_event->obj != NULL) {
70 ustctl_release_object(sock, ua_event->obj);
71 free(ua_event->obj);
72 }
d80a6244
DG
73 free(ua_event);
74}
75
76/*
77 * Delete ust app stream safely. RCU read lock must be held before calling
78 * this function.
79 */
8b366481
DG
80static
81void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
d80a6244 82{
8b366481
DG
83 if (stream->obj) {
84 ustctl_release_object(sock, stream->obj);
85 free(stream->obj);
86 }
84cd17c6 87 free(stream);
d80a6244
DG
88}
89
90/*
91 * Delete ust app channel safely. RCU read lock must be held before calling
92 * this function.
93 */
8b366481
DG
94static
95void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
d80a6244
DG
96{
97 int ret;
bec39940 98 struct lttng_ht_iter iter;
d80a6244 99 struct ust_app_event *ua_event;
55cc08a6 100 struct ust_app_ctx *ua_ctx;
d80a6244
DG
101 struct ltt_ust_stream *stream, *stmp;
102
55cc08a6 103 /* Wipe stream */
d80a6244 104 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 105 cds_list_del(&stream->list);
d80a6244
DG
106 delete_ust_app_stream(sock, stream);
107 }
108
55cc08a6 109 /* Wipe context */
bec39940
DG
110 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
111 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6
DG
112 assert(!ret);
113 delete_ust_app_ctx(sock, ua_ctx);
114 }
bec39940 115 lttng_ht_destroy(ua_chan->ctx);
d80a6244 116
55cc08a6 117 /* Wipe events */
bec39940
DG
118 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
119 node.node) {
120 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 121 assert(!ret);
d80a6244
DG
122 delete_ust_app_event(sock, ua_event);
123 }
bec39940 124 lttng_ht_destroy(ua_chan->events);
edb67388
DG
125
126 if (ua_chan->obj != NULL) {
127 ustctl_release_object(sock, ua_chan->obj);
128 free(ua_chan->obj);
129 }
84cd17c6 130 free(ua_chan);
d80a6244
DG
131}
132
133/*
134 * Delete ust app session safely. RCU read lock must be held before calling
135 * this function.
136 */
8b366481
DG
137static
138void delete_ust_app_session(int sock, struct ust_app_session *ua_sess)
d80a6244
DG
139{
140 int ret;
bec39940 141 struct lttng_ht_iter iter;
d80a6244
DG
142 struct ust_app_channel *ua_chan;
143
144 if (ua_sess->metadata) {
8b366481
DG
145 if (ua_sess->metadata->stream_obj) {
146 ustctl_release_object(sock, ua_sess->metadata->stream_obj);
147 free(ua_sess->metadata->stream_obj);
148 }
149 if (ua_sess->metadata->obj) {
150 ustctl_release_object(sock, ua_sess->metadata->obj);
151 free(ua_sess->metadata->obj);
152 }
d80a6244
DG
153 }
154
bec39940
DG
155 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
156 node.node) {
157 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 158 assert(!ret);
d80a6244
DG
159 delete_ust_app_channel(sock, ua_chan);
160 }
bec39940 161 lttng_ht_destroy(ua_sess->channels);
d80a6244 162
aee6bafd
MD
163 if (ua_sess->handle != -1) {
164 ustctl_release_handle(sock, ua_sess->handle);
165 }
8b366481 166 free(ua_sess);
d80a6244 167}
91d76f53
DG
168
169/*
284d8f55
DG
170 * Delete a traceable application structure from the global list. Never call
171 * this function outside of a call_rcu call.
91d76f53 172 */
8b366481
DG
173static
174void delete_ust_app(struct ust_app *app)
91d76f53 175{
8b366481 176 int ret, sock;
bec39940 177 struct lttng_ht_iter iter;
284d8f55 178 struct ust_app_session *ua_sess;
44d3bd01 179
f6a9efaa 180 rcu_read_lock();
44d3bd01 181
d80a6244 182 /* Delete ust app sessions info */
852d0037
DG
183 sock = app->sock;
184 app->sock = -1;
d80a6244 185
8b366481 186 /* Wipe sessions */
bec39940
DG
187 cds_lfht_for_each_entry(app->sessions->ht, &iter.iter, ua_sess,
188 node.node) {
189 ret = lttng_ht_del(app->sessions, &iter);
525b0740 190 assert(!ret);
852d0037 191 delete_ust_app_session(app->sock, ua_sess);
d80a6244 192 }
bec39940 193 lttng_ht_destroy(app->sessions);
d80a6244 194
6414a713 195 /*
852d0037
DG
196 * Wait until we have deleted the application from the sock hash table
197 * before closing this socket, otherwise an application could re-use the
198 * socket ID and race with the teardown, using the same hash table entry.
199 *
200 * It's OK to leave the close in call_rcu. We want it to stay unique for
201 * all RCU readers that could run concurrently with unregister app,
202 * therefore we _need_ to only close that socket after a grace period. So
203 * it should stay in this RCU callback.
204 *
205 * This close() is a very important step of the synchronization model so
206 * every modification to this function must be carefully reviewed.
6414a713 207 */
799e2c4f
MD
208 ret = close(sock);
209 if (ret) {
210 PERROR("close");
211 }
d80a6244 212
852d0037 213 DBG2("UST app pid %d deleted", app->pid);
284d8f55 214 free(app);
bec39940 215
f6a9efaa 216 rcu_read_unlock();
099e26bd
DG
217}
218
219/*
f6a9efaa 220 * URCU intermediate call to delete an UST app.
099e26bd 221 */
8b366481
DG
222static
223void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 224{
bec39940
DG
225 struct lttng_ht_node_ulong *node =
226 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 227 struct ust_app *app =
852d0037 228 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 229
852d0037 230 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 231 delete_ust_app(app);
099e26bd
DG
232}
233
8b366481
DG
234/*
235 * Alloc new UST app session.
236 */
237static
238struct ust_app_session *alloc_ust_app_session(void)
239{
240 struct ust_app_session *ua_sess;
241
242 /* Init most of the default value by allocating and zeroing */
243 ua_sess = zmalloc(sizeof(struct ust_app_session));
244 if (ua_sess == NULL) {
245 PERROR("malloc");
246 goto error;
247 }
248
249 ua_sess->handle = -1;
bec39940 250 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
8b366481
DG
251
252 return ua_sess;
253
254error:
255 return NULL;
256}
257
258/*
259 * Alloc new UST app channel.
260 */
261static
262struct ust_app_channel *alloc_ust_app_channel(char *name,
263 struct lttng_ust_channel *attr)
264{
265 struct ust_app_channel *ua_chan;
266
267 /* Init most of the default value by allocating and zeroing */
268 ua_chan = zmalloc(sizeof(struct ust_app_channel));
269 if (ua_chan == NULL) {
270 PERROR("malloc");
271 goto error;
272 }
273
274 /* Setup channel name */
275 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
276 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
277
278 ua_chan->enabled = 1;
279 ua_chan->handle = -1;
bec39940
DG
280 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
281 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
282 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
283
284 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
285
286 /* Copy attributes */
287 if (attr) {
288 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
289 }
290
291 DBG3("UST app channel %s allocated", ua_chan->name);
292
293 return ua_chan;
294
295error:
296 return NULL;
297}
298
299/*
300 * Alloc new UST app event.
301 */
302static
303struct ust_app_event *alloc_ust_app_event(char *name,
304 struct lttng_ust_event *attr)
305{
306 struct ust_app_event *ua_event;
307
308 /* Init most of the default value by allocating and zeroing */
309 ua_event = zmalloc(sizeof(struct ust_app_event));
310 if (ua_event == NULL) {
311 PERROR("malloc");
312 goto error;
313 }
314
315 ua_event->enabled = 1;
316 strncpy(ua_event->name, name, sizeof(ua_event->name));
317 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940
DG
318 ua_event->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
319 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
320
321 /* Copy attributes */
322 if (attr) {
323 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
324 }
325
326 DBG3("UST app event %s allocated", ua_event->name);
327
328 return ua_event;
329
330error:
331 return NULL;
332}
333
334/*
335 * Alloc new UST app context.
336 */
337static
338struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
339{
340 struct ust_app_ctx *ua_ctx;
341
342 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
343 if (ua_ctx == NULL) {
344 goto error;
345 }
346
347 if (uctx) {
348 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
349 }
350
351 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
352
353error:
354 return ua_ctx;
355}
356
099e26bd 357/*
421cb601
DG
358 * Find an ust_app using the sock and return it. RCU read side lock must be
359 * held before calling this helper function.
099e26bd 360 */
8b366481
DG
361static
362struct ust_app *find_app_by_sock(int sock)
099e26bd 363{
bec39940 364 struct lttng_ht_node_ulong *node;
bec39940 365 struct lttng_ht_iter iter;
f6a9efaa 366
852d0037 367 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 368 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
369 if (node == NULL) {
370 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
371 goto error;
372 }
852d0037
DG
373
374 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
375
376error:
377 return NULL;
099e26bd
DG
378}
379
55cc08a6
DG
380/*
381 * Create the channel context on the tracer.
382 */
383static
384int create_ust_channel_context(struct ust_app_channel *ua_chan,
385 struct ust_app_ctx *ua_ctx, struct ust_app *app)
386{
387 int ret;
388
852d0037 389 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6
DG
390 ua_chan->obj, &ua_ctx->obj);
391 if (ret < 0) {
392 goto error;
393 }
394
395 ua_ctx->handle = ua_ctx->obj->handle;
396
727d5404 397 DBG2("UST app context created successfully for channel %s", ua_chan->name);
55cc08a6
DG
398
399error:
400 return ret;
401}
402
403/*
404 * Create the event context on the tracer.
405 */
406static
407int create_ust_event_context(struct ust_app_event *ua_event,
408 struct ust_app_ctx *ua_ctx, struct ust_app *app)
409{
410 int ret;
411
852d0037 412 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6
DG
413 ua_event->obj, &ua_ctx->obj);
414 if (ret < 0) {
415 goto error;
416 }
417
418 ua_ctx->handle = ua_ctx->obj->handle;
419
727d5404 420 DBG2("UST app context created successfully for event %s", ua_event->name);
55cc08a6
DG
421
422error:
423 return ret;
424}
425
9730260e
DG
426/*
427 * Disable the specified event on to UST tracer for the UST session.
428 */
429static int disable_ust_event(struct ust_app *app,
430 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
431{
432 int ret;
433
852d0037 434 ret = ustctl_disable(app->sock, ua_event->obj);
9730260e
DG
435 if (ret < 0) {
436 ERR("UST app event %s disable failed for app (pid: %d) "
437 "and session handle %d with ret %d",
852d0037 438 ua_event->attr.name, app->pid, ua_sess->handle, ret);
9730260e
DG
439 goto error;
440 }
441
442 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 443 ua_event->attr.name, app->pid);
9730260e
DG
444
445error:
446 return ret;
447}
448
78f0bacd
DG
449/*
450 * Disable the specified channel on to UST tracer for the UST session.
451 */
452static int disable_ust_channel(struct ust_app *app,
453 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
454{
455 int ret;
456
852d0037 457 ret = ustctl_disable(app->sock, ua_chan->obj);
78f0bacd
DG
458 if (ret < 0) {
459 ERR("UST app channel %s disable failed for app (pid: %d) "
460 "and session handle %d with ret %d",
852d0037 461 ua_chan->name, app->pid, ua_sess->handle, ret);
78f0bacd
DG
462 goto error;
463 }
464
78f0bacd 465 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 466 ua_chan->name, app->pid);
78f0bacd
DG
467
468error:
469 return ret;
470}
471
472/*
473 * Enable the specified channel on to UST tracer for the UST session.
474 */
475static int enable_ust_channel(struct ust_app *app,
476 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
477{
478 int ret;
479
852d0037 480 ret = ustctl_enable(app->sock, ua_chan->obj);
78f0bacd
DG
481 if (ret < 0) {
482 ERR("UST app channel %s enable failed for app (pid: %d) "
483 "and session handle %d with ret %d",
852d0037 484 ua_chan->name, app->pid, ua_sess->handle, ret);
78f0bacd
DG
485 goto error;
486 }
487
488 ua_chan->enabled = 1;
489
490 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 491 ua_chan->name, app->pid);
78f0bacd
DG
492
493error:
494 return ret;
495}
496
edb67388
DG
497/*
498 * Enable the specified event on to UST tracer for the UST session.
499 */
500static int enable_ust_event(struct ust_app *app,
501 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
502{
503 int ret;
504
852d0037 505 ret = ustctl_enable(app->sock, ua_event->obj);
edb67388
DG
506 if (ret < 0) {
507 ERR("UST app event %s enable failed for app (pid: %d) "
508 "and session handle %d with ret %d",
852d0037 509 ua_event->attr.name, app->pid, ua_sess->handle, ret);
edb67388
DG
510 goto error;
511 }
512
513 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 514 ua_event->attr.name, app->pid);
edb67388
DG
515
516error:
517 return ret;
518}
519
099e26bd 520/*
5b4a0ec0 521 * Open metadata onto the UST tracer for a UST session.
0177d773 522 */
5b4a0ec0
DG
523static int open_ust_metadata(struct ust_app *app,
524 struct ust_app_session *ua_sess)
0177d773 525{
5b4a0ec0
DG
526 int ret;
527 struct lttng_ust_channel_attr uattr;
0177d773 528
5b4a0ec0
DG
529 uattr.overwrite = ua_sess->metadata->attr.overwrite;
530 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
531 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
532 uattr.switch_timer_interval =
533 ua_sess->metadata->attr.switch_timer_interval;
534 uattr.read_timer_interval =
535 ua_sess->metadata->attr.read_timer_interval;
536 uattr.output = ua_sess->metadata->attr.output;
537
538 /* UST tracer metadata creation */
852d0037 539 ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr,
5b4a0ec0
DG
540 &ua_sess->metadata->obj);
541 if (ret < 0) {
fc34caaa 542 ERR("UST app open metadata failed for app pid:%d with ret %d",
852d0037 543 app->pid, ret);
f6a9efaa 544 goto error;
0177d773 545 }
f6a9efaa 546
6d3686da
DG
547 ua_sess->metadata->handle = ua_sess->metadata->obj->handle;
548
f6a9efaa 549error:
5b4a0ec0 550 return ret;
91d76f53
DG
551}
552
553/*
5b4a0ec0 554 * Create stream onto the UST tracer for a UST session.
91d76f53 555 */
5b4a0ec0
DG
556static int create_ust_stream(struct ust_app *app,
557 struct ust_app_session *ua_sess)
91d76f53 558{
5b4a0ec0 559 int ret;
421cb601 560
852d0037 561 ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj,
5b4a0ec0
DG
562 &ua_sess->metadata->stream_obj);
563 if (ret < 0) {
564 ERR("UST create metadata stream failed");
421cb601 565 goto error;
91d76f53 566 }
421cb601 567
421cb601 568error:
5b4a0ec0 569 return ret;
91d76f53
DG
570}
571
b551a063 572/*
5b4a0ec0 573 * Create the specified channel onto the UST tracer for a UST session.
b551a063 574 */
5b4a0ec0
DG
575static int create_ust_channel(struct ust_app *app,
576 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
b551a063 577{
5b4a0ec0 578 int ret;
b551a063 579
5b4a0ec0 580 /* TODO: remove cast and use lttng-ust-abi.h */
852d0037 581 ret = ustctl_create_channel(app->sock, ua_sess->handle,
5b4a0ec0
DG
582 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
583 if (ret < 0) {
58f3ca76 584 ERR("Creating channel %s for app (pid: %d, sock: %d) "
5b4a0ec0 585 "and session handle %d with ret %d",
852d0037 586 ua_chan->name, app->pid, app->sock,
5b4a0ec0 587 ua_sess->handle, ret);
b551a063
DG
588 goto error;
589 }
590
5b4a0ec0 591 ua_chan->handle = ua_chan->obj->handle;
b551a063 592
5b4a0ec0 593 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
852d0037 594 ua_chan->name, app->pid, app->sock);
b551a063 595
8535a6d9
DG
596 /* If channel is not enabled, disable it on the tracer */
597 if (!ua_chan->enabled) {
598 ret = disable_ust_channel(app, ua_sess, ua_chan);
599 if (ret < 0) {
600 goto error;
601 }
602 }
603
b551a063
DG
604error:
605 return ret;
606}
607
91d76f53 608/*
5b4a0ec0 609 * Create the specified event onto the UST tracer for a UST session.
91d76f53 610 */
edb67388
DG
611static
612int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
613 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 614{
5b4a0ec0 615 int ret = 0;
284d8f55 616
5b4a0ec0 617 /* Create UST event on tracer */
852d0037 618 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0
DG
619 &ua_event->obj);
620 if (ret < 0) {
fc34caaa
DG
621 if (ret == -EEXIST) {
622 ret = 0;
623 goto error;
624 }
5b4a0ec0 625 ERR("Error ustctl create event %s for app pid: %d with ret %d",
852d0037 626 ua_event->attr.name, app->pid, ret);
5b4a0ec0 627 goto error;
91d76f53 628 }
f6a9efaa 629
5b4a0ec0 630 ua_event->handle = ua_event->obj->handle;
284d8f55 631
5b4a0ec0 632 DBG2("UST app event %s created successfully for pid:%d",
852d0037 633 ua_event->attr.name, app->pid);
f6a9efaa 634
8535a6d9 635 /* If event not enabled, disable it on the tracer */
fc34caaa 636 if (ua_event->enabled == 0) {
8535a6d9
DG
637 ret = disable_ust_event(app, ua_sess, ua_event);
638 if (ret < 0) {
fc34caaa
DG
639 /*
640 * If we hit an EPERM, something is wrong with our disable call. If
641 * we get an EEXIST, there is a problem on the tracer side since we
642 * just created it.
643 */
644 switch (ret) {
645 case -EPERM:
646 /* Code flow problem */
647 assert(0);
648 case -EEXIST:
649 /* It's OK for our use case. */
650 ret = 0;
651 break;
652 default:
653 break;
654 }
8535a6d9
DG
655 goto error;
656 }
657 }
658
5b4a0ec0
DG
659error:
660 return ret;
91d76f53 661}
48842b30 662
5b4a0ec0
DG
663/*
664 * Copy data between an UST app event and a LTT event.
665 */
421cb601 666static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
667 struct ltt_ust_event *uevent)
668{
bec39940 669 struct lttng_ht_iter iter;
55cc08a6
DG
670 struct ltt_ust_context *uctx;
671 struct ust_app_ctx *ua_ctx;
672
48842b30
DG
673 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
674 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
675
fc34caaa
DG
676 ua_event->enabled = uevent->enabled;
677
5b4a0ec0
DG
678 /* Copy event attributes */
679 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
680
bec39940 681 cds_lfht_for_each_entry(uevent->ctx->ht, &iter.iter, uctx, node.node) {
55cc08a6
DG
682 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
683 if (ua_ctx == NULL) {
fc34caaa
DG
684 /* malloc() failed. We should simply stop */
685 return;
55cc08a6 686 }
fc34caaa 687
bec39940
DG
688 lttng_ht_node_init_ulong(&ua_ctx->node,
689 (unsigned long) ua_ctx->ctx.ctx);
690 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
55cc08a6 691 }
48842b30
DG
692}
693
5b4a0ec0
DG
694/*
695 * Copy data between an UST app channel and a LTT channel.
696 */
421cb601 697static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
698 struct ltt_ust_channel *uchan)
699{
bec39940
DG
700 struct lttng_ht_iter iter;
701 struct lttng_ht_node_str *ua_event_node;
48842b30 702 struct ltt_ust_event *uevent;
55cc08a6 703 struct ltt_ust_context *uctx;
48842b30 704 struct ust_app_event *ua_event;
55cc08a6 705 struct ust_app_ctx *ua_ctx;
48842b30 706
fc34caaa 707 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
708
709 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
710 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
5b4a0ec0
DG
711 /* Copy event attributes */
712 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
48842b30 713
fc34caaa
DG
714 ua_chan->enabled = uchan->enabled;
715
bec39940 716 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
55cc08a6
DG
717 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
718 if (ua_ctx == NULL) {
719 continue;
720 }
bec39940
DG
721 lttng_ht_node_init_ulong(&ua_ctx->node,
722 (unsigned long) ua_ctx->ctx.ctx);
723 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6 724 }
48842b30 725
421cb601 726 /* Copy all events from ltt ust channel to ust app channel */
bec39940
DG
727 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
728 struct lttng_ht_iter uiter;
ba767faf 729
bec39940
DG
730 lttng_ht_lookup(ua_chan->events, (void *) uevent->attr.name, &uiter);
731 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
48842b30 732 if (ua_event_node == NULL) {
421cb601 733 DBG2("UST event %s not found on shadow copy channel",
48842b30 734 uevent->attr.name);
284d8f55 735 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 736 if (ua_event == NULL) {
5b4a0ec0 737 continue;
48842b30 738 }
421cb601 739 shadow_copy_event(ua_event, uevent);
bec39940 740 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
48842b30 741 }
48842b30
DG
742 }
743
fc34caaa 744 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
745}
746
5b4a0ec0
DG
747/*
748 * Copy data between a UST app session and a regular LTT session.
749 */
421cb601 750static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 751 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 752{
bec39940
DG
753 struct lttng_ht_node_str *ua_chan_node;
754 struct lttng_ht_iter iter;
48842b30
DG
755 struct ltt_ust_channel *uchan;
756 struct ust_app_channel *ua_chan;
477d7741
MD
757 time_t rawtime;
758 struct tm *timeinfo;
759 char datetime[16];
760 int ret;
761
762 /* Get date and time for unique app path */
763 time(&rawtime);
764 timeinfo = localtime(&rawtime);
765 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 766
421cb601 767 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 768
a991f516 769 ua_sess->id = usess->id;
6df2e2c9
MD
770 ua_sess->uid = usess->uid;
771 ua_sess->gid = usess->gid;
48842b30 772
fc34caaa 773 ret = snprintf(ua_sess->path, PATH_MAX, "%s/%s-%d-%s", usess->pathname,
852d0037 774 app->name, app->pid, datetime);
477d7741
MD
775 if (ret < 0) {
776 PERROR("asprintf UST shadow copy session");
777 /* TODO: We cannot return an error from here.. */
778 assert(0);
779 }
780
48842b30
DG
781 /* TODO: support all UST domain */
782
783 /* Iterate over all channels in global domain. */
bec39940
DG
784 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
785 uchan, node.node) {
786 struct lttng_ht_iter uiter;
ba767faf 787
bec39940
DG
788 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
789 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5b4a0ec0 790 if (ua_chan_node != NULL) {
fc34caaa 791 /* Session exist. Contiuing. */
5b4a0ec0
DG
792 continue;
793 }
421cb601 794
5b4a0ec0
DG
795 DBG2("Channel %s not found on shadow session copy, creating it",
796 uchan->name);
797 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
798 if (ua_chan == NULL) {
fc34caaa 799 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
5b4a0ec0 800 continue;
48842b30
DG
801 }
802
5b4a0ec0 803 shadow_copy_channel(ua_chan, uchan);
bec39940 804 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
48842b30
DG
805 }
806}
807
78f0bacd
DG
808/*
809 * Lookup sesison wrapper.
810 */
84cd17c6
MD
811static
812void __lookup_session_by_app(struct ltt_ust_session *usess,
bec39940 813 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
814{
815 /* Get right UST app session from app */
2c348c10 816 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
84cd17c6
MD
817}
818
421cb601
DG
819/*
820 * Return ust app session from the app session hashtable using the UST session
a991f516 821 * id.
421cb601 822 */
48842b30
DG
823static struct ust_app_session *lookup_session_by_app(
824 struct ltt_ust_session *usess, struct ust_app *app)
825{
bec39940
DG
826 struct lttng_ht_iter iter;
827 struct lttng_ht_node_ulong *node;
48842b30 828
84cd17c6 829 __lookup_session_by_app(usess, app, &iter);
bec39940 830 node = lttng_ht_iter_get_node_ulong(&iter);
48842b30
DG
831 if (node == NULL) {
832 goto error;
833 }
834
835 return caa_container_of(node, struct ust_app_session, node);
836
837error:
838 return NULL;
839}
840
421cb601
DG
841/*
842 * Create a UST session onto the tracer of app and add it the session
843 * hashtable.
844 *
845 * Return ust app session or NULL on error.
846 */
847static struct ust_app_session *create_ust_app_session(
848 struct ltt_ust_session *usess, struct ust_app *app)
849{
850 int ret;
851 struct ust_app_session *ua_sess;
852
853 ua_sess = lookup_session_by_app(usess, app);
854 if (ua_sess == NULL) {
a991f516 855 DBG2("UST app pid: %d session id %d not found, creating it",
852d0037 856 app->pid, usess->id);
421cb601
DG
857 ua_sess = alloc_ust_app_session();
858 if (ua_sess == NULL) {
859 /* Only malloc can failed so something is really wrong */
fc34caaa 860 goto end;
421cb601 861 }
477d7741 862 shadow_copy_session(ua_sess, usess, app);
421cb601
DG
863 }
864
865 if (ua_sess->handle == -1) {
852d0037 866 ret = ustctl_create_session(app->sock);
421cb601 867 if (ret < 0) {
852d0037 868 ERR("Creating session for app pid %d", app->pid);
915d047c
DG
869 /* This means that the tracer is gone... */
870 ua_sess = (void*) -1UL;
421cb601
DG
871 goto error;
872 }
873
421cb601
DG
874 ua_sess->handle = ret;
875
876 /* Add ust app session to app's HT */
2c348c10 877 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
bec39940 878 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
421cb601
DG
879
880 DBG2("UST app session created successfully with handle %d", ret);
881 }
882
fc34caaa 883end:
421cb601
DG
884 return ua_sess;
885
886error:
fc34caaa 887 delete_ust_app_session(-1, ua_sess);
421cb601
DG
888 return NULL;
889}
890
55cc08a6
DG
891/*
892 * Create a context for the channel on the tracer.
893 */
894static
895int create_ust_app_channel_context(struct ust_app_session *ua_sess,
896 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
897 struct ust_app *app)
898{
899 int ret = 0;
bec39940
DG
900 struct lttng_ht_iter iter;
901 struct lttng_ht_node_ulong *node;
55cc08a6
DG
902 struct ust_app_ctx *ua_ctx;
903
904 DBG2("UST app adding context to channel %s", ua_chan->name);
905
bec39940
DG
906 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
907 node = lttng_ht_iter_get_node_ulong(&iter);
55cc08a6
DG
908 if (node != NULL) {
909 ret = -EEXIST;
910 goto error;
911 }
912
913 ua_ctx = alloc_ust_app_ctx(uctx);
914 if (ua_ctx == NULL) {
915 /* malloc failed */
916 ret = -1;
917 goto error;
918 }
919
bec39940
DG
920 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
921 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6
DG
922
923 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
924 if (ret < 0) {
925 goto error;
926 }
927
928error:
929 return ret;
930}
931
932/*
933 * Create an UST context and enable it for the event on the tracer.
934 */
935static
936int create_ust_app_event_context(struct ust_app_session *ua_sess,
937 struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
938 struct ust_app *app)
939{
940 int ret = 0;
bec39940
DG
941 struct lttng_ht_iter iter;
942 struct lttng_ht_node_ulong *node;
55cc08a6
DG
943 struct ust_app_ctx *ua_ctx;
944
945 DBG2("UST app adding context to event %s", ua_event->name);
946
bec39940
DG
947 lttng_ht_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx), &iter);
948 node = lttng_ht_iter_get_node_ulong(&iter);
55cc08a6
DG
949 if (node != NULL) {
950 ret = -EEXIST;
951 goto error;
952 }
953
954 ua_ctx = alloc_ust_app_ctx(uctx);
955 if (ua_ctx == NULL) {
956 /* malloc failed */
957 ret = -1;
958 goto error;
959 }
960
bec39940
DG
961 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
962 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
55cc08a6
DG
963
964 ret = create_ust_event_context(ua_event, ua_ctx, app);
965 if (ret < 0) {
966 goto error;
967 }
968
969error:
970 return ret;
971}
972
edb67388
DG
973/*
974 * Enable on the tracer side a ust app event for the session and channel.
975 */
976static
977int enable_ust_app_event(struct ust_app_session *ua_sess,
35a9059d 978 struct ust_app_event *ua_event, struct ust_app *app)
edb67388
DG
979{
980 int ret;
981
982 ret = enable_ust_event(app, ua_sess, ua_event);
983 if (ret < 0) {
984 goto error;
985 }
986
987 ua_event->enabled = 1;
988
989error:
990 return ret;
991}
992
9730260e
DG
993/*
994 * Disable on the tracer side a ust app event for the session and channel.
995 */
996static int disable_ust_app_event(struct ust_app_session *ua_sess,
7f79d3a1 997 struct ust_app_event *ua_event, struct ust_app *app)
9730260e
DG
998{
999 int ret;
1000
1001 ret = disable_ust_event(app, ua_sess, ua_event);
1002 if (ret < 0) {
1003 goto error;
1004 }
1005
1006 ua_event->enabled = 0;
1007
1008error:
1009 return ret;
1010}
1011
78f0bacd
DG
1012/*
1013 * Lookup ust app channel for session and disable it on the tracer side.
1014 */
8535a6d9
DG
1015static
1016int disable_ust_app_channel(struct ust_app_session *ua_sess,
1017 struct ust_app_channel *ua_chan, struct ust_app *app)
78f0bacd 1018{
8535a6d9 1019 int ret;
78f0bacd
DG
1020
1021 ret = disable_ust_channel(app, ua_sess, ua_chan);
1022 if (ret < 0) {
1023 goto error;
1024 }
1025
8535a6d9
DG
1026 ua_chan->enabled = 0;
1027
78f0bacd
DG
1028error:
1029 return ret;
1030}
1031
1032/*
1033 * Lookup ust app channel for session and enable it on the tracer side.
1034 */
1035static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1036 struct ltt_ust_channel *uchan, struct ust_app *app)
1037{
1038 int ret = 0;
bec39940
DG
1039 struct lttng_ht_iter iter;
1040 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1041 struct ust_app_channel *ua_chan;
1042
bec39940
DG
1043 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1044 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
78f0bacd 1045 if (ua_chan_node == NULL) {
a991f516
MD
1046 DBG2("Unable to find channel %s in ust session id %u",
1047 uchan->name, ua_sess->id);
78f0bacd
DG
1048 goto error;
1049 }
1050
1051 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1052
1053 ret = enable_ust_channel(app, ua_sess, ua_chan);
1054 if (ret < 0) {
1055 goto error;
1056 }
1057
1058error:
1059 return ret;
1060}
1061
284d8f55 1062/*
5b4a0ec0 1063 * Create UST app channel and create it on the tracer.
284d8f55 1064 */
5b4a0ec0
DG
1065static struct ust_app_channel *create_ust_app_channel(
1066 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1067 struct ust_app *app)
1068{
1069 int ret = 0;
bec39940
DG
1070 struct lttng_ht_iter iter;
1071 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
1072 struct ust_app_channel *ua_chan;
1073
1074 /* Lookup channel in the ust app session */
bec39940
DG
1075 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1076 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 1077 if (ua_chan_node != NULL) {
5b4a0ec0 1078 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 1079 goto end;
5b4a0ec0
DG
1080 }
1081
fc34caaa
DG
1082 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1083 if (ua_chan == NULL) {
1084 /* Only malloc can fail here */
1085 goto error;
1086 }
1087 shadow_copy_channel(ua_chan, uchan);
1088
5b4a0ec0
DG
1089 ret = create_ust_channel(app, ua_sess, ua_chan);
1090 if (ret < 0) {
fc34caaa
DG
1091 /* Not found previously means that it does not exist on the tracer */
1092 assert(ret != -EEXIST);
5b4a0ec0
DG
1093 goto error;
1094 }
1095
58f3ca76
DG
1096 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1097
fc34caaa 1098 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
852d0037 1099 app->pid);
fc34caaa
DG
1100
1101end:
5b4a0ec0
DG
1102 return ua_chan;
1103
1104error:
fc34caaa 1105 delete_ust_app_channel(-1, ua_chan);
5b4a0ec0
DG
1106 return NULL;
1107}
1108
1109/*
1110 * Create UST app event and create it on the tracer side.
1111 */
edb67388
DG
1112static
1113int create_ust_app_event(struct ust_app_session *ua_sess,
1114 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1115 struct ust_app *app)
284d8f55 1116{
edb67388 1117 int ret = 0;
bec39940
DG
1118 struct lttng_ht_iter iter;
1119 struct lttng_ht_node_str *ua_event_node;
5b4a0ec0 1120 struct ust_app_event *ua_event;
284d8f55 1121
5b4a0ec0 1122 /* Get event node */
bec39940
DG
1123 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
1124 ua_event_node = lttng_ht_iter_get_node_str(&iter);
edb67388 1125 if (ua_event_node != NULL) {
fc34caaa 1126 ret = -EEXIST;
edb67388
DG
1127 goto end;
1128 }
5b4a0ec0 1129
edb67388
DG
1130 /* Does not exist so create one */
1131 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1132 if (ua_event == NULL) {
1133 /* Only malloc can failed so something is really wrong */
1134 ret = -ENOMEM;
fc34caaa 1135 goto end;
5b4a0ec0 1136 }
edb67388 1137 shadow_copy_event(ua_event, uevent);
5b4a0ec0 1138
edb67388 1139 /* Create it on the tracer side */
5b4a0ec0 1140 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 1141 if (ret < 0) {
fc34caaa
DG
1142 /* Not found previously means that it does not exist on the tracer */
1143 assert(ret != -EEXIST);
284d8f55
DG
1144 goto error;
1145 }
1146
bec39940 1147 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
284d8f55 1148
fc34caaa 1149 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 1150 app->pid);
7f79d3a1 1151
edb67388 1152end:
fc34caaa
DG
1153 return ret;
1154
5b4a0ec0 1155error:
fc34caaa
DG
1156 /* Valid. Calling here is already in a read side lock */
1157 delete_ust_app_event(-1, ua_event);
edb67388 1158 return ret;
5b4a0ec0
DG
1159}
1160
1161/*
1162 * Create UST metadata and open it on the tracer side.
1163 */
1164static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1165 char *pathname, struct ust_app *app)
1166{
1167 int ret = 0;
1168
1169 if (ua_sess->metadata == NULL) {
1170 /* Allocate UST metadata */
1171 ua_sess->metadata = trace_ust_create_metadata(pathname);
1172 if (ua_sess->metadata == NULL) {
fc34caaa 1173 /* malloc() failed */
5b4a0ec0
DG
1174 goto error;
1175 }
1176
1177 ret = open_ust_metadata(app, ua_sess);
1178 if (ret < 0) {
7db205b5
DG
1179 DBG3("Opening metadata failed. Cleaning up memory");
1180
fc34caaa
DG
1181 /* Cleanup failed metadata struct */
1182 free(ua_sess->metadata);
7db205b5
DG
1183 /*
1184 * This is very important because delete_ust_app_session check if
1185 * the pointer is null or not in order to delete the metadata.
1186 */
1187 ua_sess->metadata = NULL;
5b4a0ec0
DG
1188 goto error;
1189 }
1190
852d0037 1191 DBG2("UST metadata opened for app pid %d", app->pid);
5b4a0ec0
DG
1192 }
1193
1194 /* Open UST metadata stream */
1195 if (ua_sess->metadata->stream_obj == NULL) {
1196 ret = create_ust_stream(app, ua_sess);
1197 if (ret < 0) {
1198 goto error;
1199 }
1200
e11d277b 1201 ret = run_as_mkdir(ua_sess->path, S_IRWXU | S_IRWXG,
67f747d8 1202 ua_sess->uid, ua_sess->gid);
5b4a0ec0
DG
1203 if (ret < 0) {
1204 PERROR("mkdir UST metadata");
1205 goto error;
1206 }
1207
477d7741
MD
1208 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1209 "%s/metadata", ua_sess->path);
5b4a0ec0
DG
1210 if (ret < 0) {
1211 PERROR("asprintf UST create stream");
1212 goto error;
1213 }
1214
1215 DBG2("UST metadata stream object created for app pid %d",
852d0037 1216 app->pid);
5b4a0ec0
DG
1217 } else {
1218 ERR("Attempting to create stream without metadata opened");
1219 goto error;
1220 }
1221
1222 return 0;
1223
1224error:
1225 return -1;
1226}
1227
1228/*
1229 * Return pointer to traceable apps list.
1230 */
bec39940 1231struct lttng_ht *ust_app_get_ht(void)
5b4a0ec0
DG
1232{
1233 return ust_app_ht;
1234}
1235
1236/*
1237 * Return ust app pointer or NULL if not found.
1238 */
1239struct ust_app *ust_app_find_by_pid(pid_t pid)
1240{
bec39940
DG
1241 struct lttng_ht_node_ulong *node;
1242 struct lttng_ht_iter iter;
5b4a0ec0
DG
1243
1244 rcu_read_lock();
bec39940
DG
1245 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1246 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
1247 if (node == NULL) {
1248 DBG2("UST app no found with pid %d", pid);
1249 goto error;
1250 }
1251 rcu_read_unlock();
1252
1253 DBG2("Found UST app by pid %d", pid);
1254
852d0037 1255 return caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
1256
1257error:
1258 rcu_read_unlock();
1259 return NULL;
1260}
1261
1262/*
1263 * Using pid and uid (of the app), allocate a new ust_app struct and
1264 * add it to the global traceable app list.
1265 *
0df502fd
MD
1266 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1267 * bitness is not supported.
5b4a0ec0
DG
1268 */
1269int ust_app_register(struct ust_register_msg *msg, int sock)
1270{
1271 struct ust_app *lta;
799e2c4f 1272 int ret;
5b4a0ec0 1273
7753dea8
MD
1274 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
1275 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
f943b0fb 1276 ERR("Registration failed: application \"%s\" (pid: %d) has "
7753dea8
MD
1277 "%d-bit long, but no consumerd for this long size is available.\n",
1278 msg->name, msg->pid, msg->bits_per_long);
799e2c4f
MD
1279 ret = close(sock);
1280 if (ret) {
1281 PERROR("close");
1282 }
0df502fd
MD
1283 return -EINVAL;
1284 }
3f2c5fcc
MD
1285 if (msg->major != LTTNG_UST_COMM_MAJOR) {
1286 ERR("Registration failed: application \"%s\" (pid: %d) has "
1287 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1288 msg->name, msg->pid, msg->major, msg->minor);
799e2c4f
MD
1289 ret = close(sock);
1290 if (ret) {
1291 PERROR("close");
1292 }
3f2c5fcc
MD
1293 return -EINVAL;
1294 }
5b4a0ec0
DG
1295 lta = zmalloc(sizeof(struct ust_app));
1296 if (lta == NULL) {
1297 PERROR("malloc");
1298 return -ENOMEM;
1299 }
1300
1301 lta->ppid = msg->ppid;
1302 lta->uid = msg->uid;
1303 lta->gid = msg->gid;
e0c7ec2b 1304 lta->compatible = 0; /* Not compatible until proven */
7753dea8 1305 lta->bits_per_long = msg->bits_per_long;
5b4a0ec0
DG
1306 lta->v_major = msg->major;
1307 lta->v_minor = msg->minor;
1308 strncpy(lta->name, msg->name, sizeof(lta->name));
1309 lta->name[16] = '\0';
bec39940 1310 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
5b4a0ec0 1311
852d0037
DG
1312 lta->pid = msg->pid;
1313 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long)lta->pid);
1314 lta->sock = sock;
1315 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long)lta->sock);
5b4a0ec0
DG
1316
1317 rcu_read_lock();
852d0037
DG
1318
1319 /*
1320 * On a re-registration, we want to kick out the previous registration of
1321 * that pid
1322 */
1323 lttng_ht_add_replace_ulong(ust_app_ht, &lta->pid_n);
1324
1325 /*
1326 * The socket _should_ be unique until _we_ call close. So, a add_unique
1327 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1328 * already in the table.
1329 */
1330 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &lta->sock_n);
1331
5b4a0ec0
DG
1332 rcu_read_unlock();
1333
1334 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
852d0037
DG
1335 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
1336 lta->sock, lta->name, lta->v_major, lta->v_minor);
5b4a0ec0
DG
1337
1338 return 0;
1339}
1340
1341/*
1342 * Unregister app by removing it from the global traceable app list and freeing
1343 * the data struct.
1344 *
1345 * The socket is already closed at this point so no close to sock.
1346 */
1347void ust_app_unregister(int sock)
1348{
1349 struct ust_app *lta;
bec39940
DG
1350 struct lttng_ht_node_ulong *node;
1351 struct lttng_ht_iter iter;
525b0740 1352 int ret;
5b4a0ec0
DG
1353
1354 rcu_read_lock();
886459c6 1355
5b4a0ec0 1356 /* Get the node reference for a call_rcu */
852d0037 1357 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1358 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0 1359 if (node == NULL) {
852d0037 1360 ERR("Unable to find app by sock %d", sock);
5b4a0ec0
DG
1361 goto error;
1362 }
284d8f55 1363
852d0037
DG
1364 lta = caa_container_of(node, struct ust_app, sock_n);
1365
1366 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1367
886459c6 1368 /* Remove application from PID hash table */
852d0037
DG
1369 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1370 assert(!ret);
1371
1372 /* Assign second node for deletion */
1373 iter.iter.node = &lta->pid_n.node;
1374
bec39940 1375 ret = lttng_ht_del(ust_app_ht, &iter);
525b0740 1376 assert(!ret);
852d0037
DG
1377
1378 /* Free memory */
1379 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1380
284d8f55 1381error:
5b4a0ec0
DG
1382 rcu_read_unlock();
1383 return;
284d8f55
DG
1384}
1385
1386/*
5b4a0ec0 1387 * Return traceable_app_count
284d8f55 1388 */
5b4a0ec0 1389unsigned long ust_app_list_count(void)
284d8f55 1390{
5b4a0ec0 1391 unsigned long count;
284d8f55 1392
5b4a0ec0 1393 rcu_read_lock();
bec39940 1394 count = lttng_ht_get_count(ust_app_ht);
5b4a0ec0 1395 rcu_read_unlock();
284d8f55 1396
5b4a0ec0 1397 return count;
284d8f55
DG
1398}
1399
5b4a0ec0
DG
1400/*
1401 * Fill events array with all events name of all registered apps.
1402 */
1403int ust_app_list_events(struct lttng_event **events)
421cb601 1404{
5b4a0ec0
DG
1405 int ret, handle;
1406 size_t nbmem, count = 0;
bec39940 1407 struct lttng_ht_iter iter;
5b4a0ec0
DG
1408 struct ust_app *app;
1409 struct lttng_event *tmp;
421cb601 1410
5b4a0ec0
DG
1411 nbmem = UST_APP_EVENT_LIST_SIZE;
1412 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1413 if (tmp == NULL) {
1414 PERROR("zmalloc ust app events");
1415 ret = -ENOMEM;
421cb601
DG
1416 goto error;
1417 }
1418
5b4a0ec0 1419 rcu_read_lock();
421cb601 1420
852d0037 1421 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
90eaa0d2 1422 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 1423
e0c7ec2b
DG
1424 if (!app->compatible) {
1425 /*
1426 * TODO: In time, we should notice the caller of this error by
1427 * telling him that this is a version error.
1428 */
1429 continue;
1430 }
852d0037 1431 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0
DG
1432 if (handle < 0) {
1433 ERR("UST app list events getting handle failed for app pid %d",
852d0037 1434 app->pid);
5b4a0ec0
DG
1435 continue;
1436 }
421cb601 1437
852d0037 1438 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
90eaa0d2 1439 &uiter)) != -ENOENT) {
815564d8
MD
1440 if (count >= nbmem) {
1441 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1442 2 * nbmem);
1443 nbmem *= 2;
2f221590 1444 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
5b4a0ec0
DG
1445 if (tmp == NULL) {
1446 PERROR("realloc ust app events");
1447 ret = -ENOMEM;
1448 goto rcu_error;
1449 }
1450 }
90eaa0d2 1451 memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
8005f29a 1452 tmp[count].loglevel = uiter.loglevel;
69892bbb 1453 tmp[count].type = LTTNG_UST_TRACEPOINT;
852d0037 1454 tmp[count].pid = app->pid;
5b4a0ec0
DG
1455 tmp[count].enabled = -1;
1456 count++;
421cb601 1457 }
421cb601
DG
1458 }
1459
5b4a0ec0
DG
1460 ret = count;
1461 *events = tmp;
421cb601 1462
5b4a0ec0 1463 DBG2("UST app list events done (%zu events)", count);
421cb601 1464
5b4a0ec0
DG
1465rcu_error:
1466 rcu_read_unlock();
421cb601 1467error:
5b4a0ec0 1468 return ret;
421cb601
DG
1469}
1470
5b4a0ec0
DG
1471/*
1472 * Free and clean all traceable apps of the global list.
1473 */
1474void ust_app_clean_list(void)
421cb601 1475{
5b4a0ec0 1476 int ret;
bec39940
DG
1477 struct lttng_ht_iter iter;
1478 struct lttng_ht_node_ulong *node;
421cb601 1479
5b4a0ec0 1480 DBG2("UST app cleaning registered apps hash table");
421cb601 1481
5b4a0ec0 1482 rcu_read_lock();
421cb601 1483
bec39940
DG
1484 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, node, node) {
1485 ret = lttng_ht_del(ust_app_ht, &iter);
525b0740 1486 assert(!ret);
bec39940 1487 call_rcu(&node->head, delete_ust_app_rcu);
421cb601
DG
1488 }
1489
852d0037
DG
1490 /* Cleanup socket hash table */
1491 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, node, node) {
1492 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
bec39940
DG
1493 assert(!ret);
1494 }
852d0037 1495
bec39940 1496 /* Destroy is done only when the ht is empty */
852d0037
DG
1497 lttng_ht_destroy(ust_app_ht);
1498 lttng_ht_destroy(ust_app_ht_by_sock);
421cb601 1499
5b4a0ec0
DG
1500 rcu_read_unlock();
1501}
1502
1503/*
1504 * Init UST app hash table.
1505 */
1506void ust_app_ht_alloc(void)
1507{
bec39940 1508 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
852d0037 1509 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
421cb601
DG
1510}
1511
78f0bacd
DG
1512/*
1513 * For a specific UST session, disable the channel for all registered apps.
1514 */
35a9059d 1515int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1516 struct ltt_ust_channel *uchan)
1517{
1518 int ret = 0;
bec39940
DG
1519 struct lttng_ht_iter iter;
1520 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1521 struct ust_app *app;
1522 struct ust_app_session *ua_sess;
8535a6d9 1523 struct ust_app_channel *ua_chan;
78f0bacd
DG
1524
1525 if (usess == NULL || uchan == NULL) {
1526 ERR("Disabling UST global channel with NULL values");
1527 ret = -1;
1528 goto error;
1529 }
1530
a991f516
MD
1531 DBG2("UST app disabling channel %s from global domain for session id %d",
1532 uchan->name, usess->id);
78f0bacd
DG
1533
1534 rcu_read_lock();
1535
1536 /* For every registered applications */
852d0037 1537 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 1538 struct lttng_ht_iter uiter;
e0c7ec2b
DG
1539 if (!app->compatible) {
1540 /*
1541 * TODO: In time, we should notice the caller of this error by
1542 * telling him that this is a version error.
1543 */
1544 continue;
1545 }
78f0bacd
DG
1546 ua_sess = lookup_session_by_app(usess, app);
1547 if (ua_sess == NULL) {
1548 continue;
1549 }
1550
8535a6d9 1551 /* Get channel */
bec39940
DG
1552 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1553 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
1554 /* If the session if found for the app, the channel must be there */
1555 assert(ua_chan_node);
1556
1557 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1558 /* The channel must not be already disabled */
1559 assert(ua_chan->enabled == 1);
1560
1561 /* Disable channel onto application */
1562 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
1563 if (ret < 0) {
1564 /* XXX: We might want to report this error at some point... */
1565 continue;
1566 }
1567 }
1568
1569 rcu_read_unlock();
1570
1571error:
1572 return ret;
1573}
1574
1575/*
1576 * For a specific UST session, enable the channel for all registered apps.
1577 */
35a9059d 1578int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1579 struct ltt_ust_channel *uchan)
1580{
1581 int ret = 0;
bec39940 1582 struct lttng_ht_iter iter;
78f0bacd
DG
1583 struct ust_app *app;
1584 struct ust_app_session *ua_sess;
1585
1586 if (usess == NULL || uchan == NULL) {
1587 ERR("Adding UST global channel to NULL values");
1588 ret = -1;
1589 goto error;
1590 }
1591
a991f516
MD
1592 DBG2("UST app enabling channel %s to global domain for session id %d",
1593 uchan->name, usess->id);
78f0bacd
DG
1594
1595 rcu_read_lock();
1596
1597 /* For every registered applications */
852d0037 1598 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1599 if (!app->compatible) {
1600 /*
1601 * TODO: In time, we should notice the caller of this error by
1602 * telling him that this is a version error.
1603 */
1604 continue;
1605 }
78f0bacd
DG
1606 ua_sess = lookup_session_by_app(usess, app);
1607 if (ua_sess == NULL) {
1608 continue;
1609 }
1610
1611 /* Enable channel onto application */
1612 ret = enable_ust_app_channel(ua_sess, uchan, app);
1613 if (ret < 0) {
1614 /* XXX: We might want to report this error at some point... */
1615 continue;
1616 }
1617 }
1618
1619 rcu_read_unlock();
1620
1621error:
1622 return ret;
1623}
1624
b0a40d28
DG
1625/*
1626 * Disable an event in a channel and for a specific session.
1627 */
35a9059d
DG
1628int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1629 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
1630{
1631 int ret = 0;
bec39940
DG
1632 struct lttng_ht_iter iter, uiter;
1633 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
b0a40d28
DG
1634 struct ust_app *app;
1635 struct ust_app_session *ua_sess;
1636 struct ust_app_channel *ua_chan;
1637 struct ust_app_event *ua_event;
1638
1639 DBG("UST app disabling event %s for all apps in channel "
a991f516 1640 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
1641
1642 rcu_read_lock();
1643
1644 /* For all registered applications */
852d0037 1645 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1646 if (!app->compatible) {
1647 /*
1648 * TODO: In time, we should notice the caller of this error by
1649 * telling him that this is a version error.
1650 */
1651 continue;
1652 }
b0a40d28
DG
1653 ua_sess = lookup_session_by_app(usess, app);
1654 if (ua_sess == NULL) {
1655 /* Next app */
1656 continue;
1657 }
1658
1659 /* Lookup channel in the ust app session */
bec39940
DG
1660 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1661 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 1662 if (ua_chan_node == NULL) {
a991f516 1663 DBG2("Channel %s not found in session id %d for app pid %d."
852d0037 1664 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
1665 continue;
1666 }
1667 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1668
bec39940
DG
1669 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
1670 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28
DG
1671 if (ua_event_node == NULL) {
1672 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 1673 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
1674 continue;
1675 }
1676 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1677
7f79d3a1 1678 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
1679 if (ret < 0) {
1680 /* XXX: Report error someday... */
1681 continue;
1682 }
1683 }
1684
1685 rcu_read_unlock();
1686
1687 return ret;
1688}
1689
9730260e 1690/*
edb67388 1691 * For a specific UST session and UST channel, the event for all
9730260e
DG
1692 * registered apps.
1693 */
35a9059d 1694int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
9730260e
DG
1695 struct ltt_ust_channel *uchan)
1696{
1697 int ret = 0;
bec39940
DG
1698 struct lttng_ht_iter iter, uiter;
1699 struct lttng_ht_node_str *ua_chan_node;
9730260e
DG
1700 struct ust_app *app;
1701 struct ust_app_session *ua_sess;
1702 struct ust_app_channel *ua_chan;
1703 struct ust_app_event *ua_event;
1704
1705 DBG("UST app disabling all event for all apps in channel "
a991f516 1706 "%s for session id %d", uchan->name, usess->id);
9730260e
DG
1707
1708 rcu_read_lock();
1709
1710 /* For all registered applications */
852d0037 1711 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1712 if (!app->compatible) {
1713 /*
1714 * TODO: In time, we should notice the caller of this error by
1715 * telling him that this is a version error.
1716 */
1717 continue;
1718 }
9730260e 1719 ua_sess = lookup_session_by_app(usess, app);
edb67388
DG
1720 /* If ua_sess is NULL, there is a code flow error */
1721 assert(ua_sess);
9730260e
DG
1722
1723 /* Lookup channel in the ust app session */
bec39940
DG
1724 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1725 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
1726 /* If the channel is not found, there is a code flow error */
1727 assert(ua_chan_node);
1728
9730260e
DG
1729 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1730
1731 /* Disable each events of channel */
bec39940
DG
1732 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
1733 node.node) {
7f79d3a1 1734 ret = disable_ust_app_event(ua_sess, ua_event, app);
9730260e
DG
1735 if (ret < 0) {
1736 /* XXX: Report error someday... */
1737 continue;
1738 }
1739 }
1740 }
1741
1742 rcu_read_unlock();
1743
1744 return ret;
1745}
1746
421cb601 1747/*
5b4a0ec0 1748 * For a specific UST session, create the channel for all registered apps.
421cb601 1749 */
35a9059d 1750int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
1751 struct ltt_ust_channel *uchan)
1752{
bec39940 1753 struct lttng_ht_iter iter;
48842b30
DG
1754 struct ust_app *app;
1755 struct ust_app_session *ua_sess;
1756 struct ust_app_channel *ua_chan;
1757
fc34caaa
DG
1758 /* Very wrong code flow */
1759 assert(usess);
1760 assert(uchan);
421cb601 1761
a991f516
MD
1762 DBG2("UST app adding channel %s to global domain for session id %d",
1763 uchan->name, usess->id);
48842b30
DG
1764
1765 rcu_read_lock();
421cb601 1766
5b4a0ec0 1767 /* For every registered applications */
852d0037 1768 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1769 if (!app->compatible) {
1770 /*
1771 * TODO: In time, we should notice the caller of this error by
1772 * telling him that this is a version error.
1773 */
1774 continue;
1775 }
edb67388
DG
1776 /*
1777 * Create session on the tracer side and add it to app session HT. Note
1778 * that if session exist, it will simply return a pointer to the ust
1779 * app session.
1780 */
421cb601 1781 ua_sess = create_ust_app_session(usess, app);
509cbaf8 1782 if (ua_sess == NULL) {
915d047c 1783 /* The malloc() failed. */
fc34caaa 1784 goto error;
915d047c
DG
1785 } else if (ua_sess == (void *) -1UL) {
1786 /* The application's socket is not valid. Contiuing */
1787 continue;
48842b30
DG
1788 }
1789
421cb601
DG
1790 /* Create channel onto application */
1791 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1792 if (ua_chan == NULL) {
fc34caaa
DG
1793 /* Major problem here and it's maybe the tracer or malloc() */
1794 goto error;
48842b30 1795 }
48842b30 1796 }
5b4a0ec0 1797
48842b30
DG
1798 rcu_read_unlock();
1799
fc34caaa
DG
1800 return 0;
1801
421cb601 1802error:
fc34caaa 1803 return -1;
48842b30
DG
1804}
1805
5b4a0ec0 1806/*
edb67388 1807 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 1808 */
35a9059d 1809int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
1810 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1811{
1812 int ret = 0;
bec39940
DG
1813 struct lttng_ht_iter iter, uiter;
1814 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
48842b30
DG
1815 struct ust_app *app;
1816 struct ust_app_session *ua_sess;
1817 struct ust_app_channel *ua_chan;
1818 struct ust_app_event *ua_event;
48842b30 1819
a991f516
MD
1820 DBG("UST app enabling event %s for all apps for session id %d",
1821 uevent->attr.name, usess->id);
48842b30 1822
edb67388
DG
1823 /*
1824 * NOTE: At this point, this function is called only if the session and
1825 * channel passed are already created for all apps. and enabled on the
1826 * tracer also.
1827 */
1828
48842b30 1829 rcu_read_lock();
421cb601
DG
1830
1831 /* For all registered applications */
852d0037 1832 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1833 if (!app->compatible) {
1834 /*
1835 * TODO: In time, we should notice the caller of this error by
1836 * telling him that this is a version error.
1837 */
1838 continue;
1839 }
edb67388
DG
1840 ua_sess = lookup_session_by_app(usess, app);
1841 /* If ua_sess is NULL, there is a code flow error */
1842 assert(ua_sess);
ba767faf 1843
edb67388 1844 /* Lookup channel in the ust app session */
bec39940
DG
1845 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1846 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
1847 /* If the channel is not found, there is a code flow error */
1848 assert(ua_chan_node);
1849
1850 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1851
bec39940
DG
1852 lttng_ht_lookup(ua_chan->events, (void*)uevent->attr.name, &uiter);
1853 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
35a9059d 1854 if (ua_event_node == NULL) {
7f79d3a1 1855 DBG3("UST app enable event %s not found for app PID %d."
852d0037 1856 "Skipping app", uevent->attr.name, app->pid);
35a9059d
DG
1857 continue;
1858 }
1859 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1860
1861 ret = enable_ust_app_event(ua_sess, ua_event, app);
1862 if (ret < 0) {
7f79d3a1 1863 goto error;
48842b30 1864 }
edb67388
DG
1865 }
1866
7f79d3a1 1867error:
edb67388 1868 rcu_read_unlock();
edb67388
DG
1869 return ret;
1870}
1871
1872/*
1873 * For a specific existing UST session and UST channel, creates the event for
1874 * all registered apps.
1875 */
35a9059d 1876int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
1877 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1878{
1879 int ret = 0;
bec39940
DG
1880 struct lttng_ht_iter iter, uiter;
1881 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
1882 struct ust_app *app;
1883 struct ust_app_session *ua_sess;
1884 struct ust_app_channel *ua_chan;
1885
a991f516
MD
1886 DBG("UST app creating event %s for all apps for session id %d",
1887 uevent->attr.name, usess->id);
edb67388 1888
edb67388
DG
1889 rcu_read_lock();
1890
1891 /* For all registered applications */
852d0037 1892 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1893 if (!app->compatible) {
1894 /*
1895 * TODO: In time, we should notice the caller of this error by
1896 * telling him that this is a version error.
1897 */
1898 continue;
1899 }
edb67388
DG
1900 ua_sess = lookup_session_by_app(usess, app);
1901 /* If ua_sess is NULL, there is a code flow error */
1902 assert(ua_sess);
48842b30
DG
1903
1904 /* Lookup channel in the ust app session */
bec39940
DG
1905 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1906 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
1907 /* If the channel is not found, there is a code flow error */
1908 assert(ua_chan_node);
1909
48842b30
DG
1910 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1911
edb67388
DG
1912 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1913 if (ret < 0) {
fc34caaa
DG
1914 if (ret != -EEXIST) {
1915 /* Possible value at this point: -ENOMEM. If so, we stop! */
1916 break;
1917 }
1918 DBG2("UST app event %s already exist on app PID %d",
852d0037 1919 uevent->attr.name, app->pid);
5b4a0ec0 1920 continue;
48842b30 1921 }
48842b30 1922 }
5b4a0ec0 1923
48842b30
DG
1924 rcu_read_unlock();
1925
1926 return ret;
1927}
1928
5b4a0ec0
DG
1929/*
1930 * Start tracing for a specific UST session and app.
1931 */
421cb601 1932int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
1933{
1934 int ret = 0;
bec39940 1935 struct lttng_ht_iter iter;
48842b30
DG
1936 struct ust_app_session *ua_sess;
1937 struct ust_app_channel *ua_chan;
5b4a0ec0 1938 struct ltt_ust_stream *ustream;
7753dea8 1939 int consumerd_fd;
48842b30 1940
852d0037 1941 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 1942
509cbaf8
MD
1943 rcu_read_lock();
1944
e0c7ec2b
DG
1945 if (!app->compatible) {
1946 goto end;
1947 }
1948
421cb601
DG
1949 ua_sess = lookup_session_by_app(usess, app);
1950 if (ua_sess == NULL) {
509cbaf8 1951 goto error_rcu_unlock;
421cb601 1952 }
48842b30 1953
aea829b3
DG
1954 /* Upon restart, we skip the setup, already done */
1955 if (ua_sess->started) {
8be98f9a 1956 goto skip_setup;
aea829b3 1957 }
8be98f9a 1958
f848c3eb
DG
1959 /* Indicate that the session has been started once */
1960 ua_sess->started = 1;
1961
421cb601
DG
1962 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1963 if (ret < 0) {
509cbaf8 1964 goto error_rcu_unlock;
421cb601 1965 }
48842b30 1966
421cb601 1967 /* For each channel */
bec39940
DG
1968 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
1969 node.node) {
421cb601
DG
1970 /* Create all streams */
1971 while (1) {
5b4a0ec0 1972 /* Create UST stream */
421cb601
DG
1973 ustream = zmalloc(sizeof(*ustream));
1974 if (ustream == NULL) {
1975 PERROR("zmalloc ust stream");
509cbaf8 1976 goto error_rcu_unlock;
421cb601 1977 }
48842b30 1978
852d0037 1979 ret = ustctl_create_stream(app->sock, ua_chan->obj,
421cb601 1980 &ustream->obj);
48842b30 1981 if (ret < 0) {
421cb601
DG
1982 /* Got all streams */
1983 break;
48842b30 1984 }
421cb601 1985 ustream->handle = ustream->obj->handle;
48842b30 1986
421cb601
DG
1987 /* Order is important */
1988 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
477d7741
MD
1989 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1990 ua_sess->path, ua_chan->name,
1991 ua_chan->streams.count++);
aba8e916
DG
1992 if (ret < 0) {
1993 PERROR("asprintf UST create stream");
421cb601 1994 continue;
aba8e916 1995 }
421cb601
DG
1996 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1997 ustream->pathname);
1998 }
421cb601 1999 }
aba8e916 2000
7753dea8
MD
2001 switch (app->bits_per_long) {
2002 case 64:
2003 consumerd_fd = ust_consumerd64_fd;
2004 break;
2005 case 32:
2006 consumerd_fd = ust_consumerd32_fd;
2007 break;
2008 default:
2009 ret = -EINVAL;
2010 goto error_rcu_unlock;
2011 }
aea829b3 2012
421cb601 2013 /* Setup UST consumer socket and send fds to it */
7753dea8 2014 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
421cb601 2015 if (ret < 0) {
509cbaf8 2016 goto error_rcu_unlock;
421cb601 2017 }
48842b30 2018
8be98f9a 2019skip_setup:
421cb601 2020 /* This start the UST tracing */
852d0037 2021 ret = ustctl_start_session(app->sock, ua_sess->handle);
421cb601 2022 if (ret < 0) {
852d0037 2023 ERR("Error starting tracing for app pid: %d", app->pid);
509cbaf8 2024 goto error_rcu_unlock;
421cb601 2025 }
5b4a0ec0 2026
421cb601 2027 /* Quiescent wait after starting trace */
852d0037 2028 ustctl_wait_quiescent(app->sock);
48842b30 2029
e0c7ec2b
DG
2030end:
2031 rcu_read_unlock();
421cb601 2032 return 0;
48842b30 2033
509cbaf8
MD
2034error_rcu_unlock:
2035 rcu_read_unlock();
421cb601
DG
2036 return -1;
2037}
48842b30 2038
8be98f9a
MD
2039/*
2040 * Stop tracing for a specific UST session and app.
2041 */
2042int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2043{
2044 int ret = 0;
bec39940 2045 struct lttng_ht_iter iter;
8be98f9a 2046 struct ust_app_session *ua_sess;
6d3686da 2047 struct ust_app_channel *ua_chan;
8be98f9a 2048
852d0037 2049 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
2050
2051 rcu_read_lock();
2052
e0c7ec2b
DG
2053 if (!app->compatible) {
2054 goto end;
2055 }
2056
8be98f9a
MD
2057 ua_sess = lookup_session_by_app(usess, app);
2058 if (ua_sess == NULL) {
2059 /* Only malloc can failed so something is really wrong */
2060 goto error_rcu_unlock;
2061 }
2062
9bc07046
DG
2063 /*
2064 * If started = 0, it means that stop trace has been called for a session
2065 * that was never started. This is a code flow error and should never
2066 * happen.
2067 */
2068 assert(ua_sess->started == 1);
7db205b5 2069
9d6c7d3f 2070 /* This inhibits UST tracing */
852d0037 2071 ret = ustctl_stop_session(app->sock, ua_sess->handle);
9d6c7d3f 2072 if (ret < 0) {
852d0037 2073 ERR("Error stopping tracing for app pid: %d", app->pid);
9d6c7d3f
DG
2074 goto error_rcu_unlock;
2075 }
2076
2077 /* Quiescent wait after stopping trace */
852d0037 2078 ustctl_wait_quiescent(app->sock);
9d6c7d3f
DG
2079
2080 /* Flushing buffers */
bec39940
DG
2081 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2082 node.node) {
852d0037 2083 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
8be98f9a 2084 if (ret < 0) {
7db205b5 2085 ERR("UST app PID %d channel %s flush failed with ret %d",
852d0037 2086 app->pid, ua_chan->name, ret);
6d3686da
DG
2087 /* Continuing flushing all buffers */
2088 continue;
8be98f9a
MD
2089 }
2090 }
8be98f9a 2091
90d97d10 2092 /* Flush all buffers before stopping */
852d0037 2093 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
90d97d10 2094 if (ret < 0) {
852d0037 2095 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
7db205b5 2096 ret);
90d97d10
DG
2097 }
2098
7db205b5
DG
2099end:
2100 rcu_read_unlock();
8be98f9a
MD
2101 return 0;
2102
2103error_rcu_unlock:
2104 rcu_read_unlock();
2105 return -1;
2106}
2107
84cd17c6
MD
2108/*
2109 * Destroy a specific UST session in apps.
2110 */
2111int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
2112{
2113 struct ust_app_session *ua_sess;
2114 struct lttng_ust_object_data obj;
bec39940
DG
2115 struct lttng_ht_iter iter;
2116 struct lttng_ht_node_ulong *node;
525b0740 2117 int ret;
84cd17c6 2118
852d0037 2119 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
2120
2121 rcu_read_lock();
2122
e0c7ec2b
DG
2123 if (!app->compatible) {
2124 goto end;
2125 }
2126
84cd17c6 2127 __lookup_session_by_app(usess, app, &iter);
bec39940 2128 node = lttng_ht_iter_get_node_ulong(&iter);
84cd17c6
MD
2129 if (node == NULL) {
2130 /* Only malloc can failed so something is really wrong */
2131 goto error_rcu_unlock;
2132 }
2133 ua_sess = caa_container_of(node, struct ust_app_session, node);
bec39940 2134 ret = lttng_ht_del(app->sessions, &iter);
525b0740 2135 assert(!ret);
84cd17c6
MD
2136 obj.handle = ua_sess->handle;
2137 obj.shm_fd = -1;
2138 obj.wait_fd = -1;
2139 obj.memory_map_size = 0;
852d0037 2140 ustctl_release_object(app->sock, &obj);
84cd17c6 2141
852d0037 2142 delete_ust_app_session(app->sock, ua_sess);
7db205b5 2143
84cd17c6 2144 /* Quiescent wait after stopping trace */
852d0037 2145 ustctl_wait_quiescent(app->sock);
84cd17c6 2146
e0c7ec2b
DG
2147end:
2148 rcu_read_unlock();
84cd17c6
MD
2149 return 0;
2150
2151error_rcu_unlock:
2152 rcu_read_unlock();
2153 return -1;
2154}
2155
5b4a0ec0
DG
2156/*
2157 * Start tracing for the UST session.
2158 */
421cb601
DG
2159int ust_app_start_trace_all(struct ltt_ust_session *usess)
2160{
2161 int ret = 0;
bec39940 2162 struct lttng_ht_iter iter;
421cb601 2163 struct ust_app *app;
48842b30 2164
421cb601
DG
2165 DBG("Starting all UST traces");
2166
2167 rcu_read_lock();
421cb601 2168
852d0037 2169 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
421cb601 2170 ret = ust_app_start_trace(usess, app);
48842b30 2171 if (ret < 0) {
5b4a0ec0
DG
2172 /* Continue to next apps even on error */
2173 continue;
48842b30 2174 }
48842b30 2175 }
5b4a0ec0 2176
48842b30
DG
2177 rcu_read_unlock();
2178
2179 return 0;
2180}
487cf67c 2181
8be98f9a
MD
2182/*
2183 * Start tracing for the UST session.
2184 */
2185int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2186{
2187 int ret = 0;
bec39940 2188 struct lttng_ht_iter iter;
8be98f9a
MD
2189 struct ust_app *app;
2190
2191 DBG("Stopping all UST traces");
2192
2193 rcu_read_lock();
2194
852d0037 2195 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
8be98f9a
MD
2196 ret = ust_app_stop_trace(usess, app);
2197 if (ret < 0) {
2198 /* Continue to next apps even on error */
2199 continue;
2200 }
2201 }
2202
2203 rcu_read_unlock();
2204
2205 return 0;
2206}
2207
84cd17c6
MD
2208/*
2209 * Destroy app UST session.
2210 */
2211int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2212{
2213 int ret = 0;
bec39940 2214 struct lttng_ht_iter iter;
84cd17c6
MD
2215 struct ust_app *app;
2216
2217 DBG("Destroy all UST traces");
2218
2219 rcu_read_lock();
2220
852d0037 2221 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
84cd17c6
MD
2222 ret = ust_app_destroy_trace(usess, app);
2223 if (ret < 0) {
2224 /* Continue to next apps even on error */
2225 continue;
2226 }
2227 }
2228
2229 rcu_read_unlock();
2230
2231 return 0;
2232}
2233
5b4a0ec0
DG
2234/*
2235 * Add channels/events from UST global domain to registered apps at sock.
2236 */
487cf67c
DG
2237void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2238{
55c54cce 2239 int ret = 0;
727d5404 2240 struct lttng_ht_iter iter, uiter, iter_ctx;
487cf67c
DG
2241 struct ust_app *app;
2242 struct ust_app_session *ua_sess;
2243 struct ust_app_channel *ua_chan;
2244 struct ust_app_event *ua_event;
727d5404 2245 struct ust_app_ctx *ua_ctx;
1f3580c7
DG
2246
2247 if (usess == NULL) {
5b4a0ec0 2248 ERR("No UST session on global update. Returning");
1f3580c7
DG
2249 goto error;
2250 }
2251
a991f516
MD
2252 DBG2("UST app global update for app sock %d for session id %d", sock,
2253 usess->id);
487cf67c 2254
284d8f55
DG
2255 rcu_read_lock();
2256
487cf67c
DG
2257 app = find_app_by_sock(sock);
2258 if (app == NULL) {
2259 ERR("Failed to update app sock %d", sock);
2260 goto error;
2261 }
2262
e0c7ec2b
DG
2263 if (!app->compatible) {
2264 goto error;
2265 }
2266
421cb601 2267 ua_sess = create_ust_app_session(usess, app);
487cf67c 2268 if (ua_sess == NULL) {
487cf67c
DG
2269 goto error;
2270 }
2271
284d8f55
DG
2272 /*
2273 * We can iterate safely here over all UST app session sicne the create ust
2274 * app session above made a shadow copy of the UST global domain from the
2275 * ltt ust session.
2276 */
bec39940
DG
2277 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2278 node.node) {
284d8f55
DG
2279 ret = create_ust_channel(app, ua_sess, ua_chan);
2280 if (ret < 0) {
2281 /* FIXME: Should we quit here or continue... */
2282 continue;
487cf67c
DG
2283 }
2284
727d5404
DG
2285 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2286 node.node) {
2287 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2288 if (ret < 0) {
2289 /* FIXME: Should we quit here or continue... */
2290 continue;
2291 }
2292 }
2293
2294
284d8f55 2295 /* For each events */
bec39940
DG
2296 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2297 node.node) {
284d8f55
DG
2298 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2299 if (ret < 0) {
2300 /* FIXME: Should we quit here or continue... */
2301 continue;
487cf67c 2302 }
727d5404
DG
2303
2304 /* Add context on events. */
2305 cds_lfht_for_each_entry(ua_event->ctx->ht, &iter_ctx.iter,
2306 ua_ctx, node.node) {
2307 ret = create_ust_event_context(ua_event, ua_ctx, app);
2308 if (ret < 0) {
2309 /* FIXME: Should we quit here or continue... */
2310 continue;
2311 }
2312 }
36dc12cc 2313 }
487cf67c
DG
2314 }
2315
36dc12cc 2316 if (usess->start_trace) {
421cb601 2317 ret = ust_app_start_trace(usess, app);
36dc12cc 2318 if (ret < 0) {
36dc12cc
DG
2319 goto error;
2320 }
2321
852d0037 2322 DBG2("UST trace started for app pid %d", app->pid);
36dc12cc
DG
2323 }
2324
487cf67c
DG
2325error:
2326 rcu_read_unlock();
2327 return;
2328}
55cc08a6
DG
2329
2330/*
2331 * Add context to a specific channel for global UST domain.
2332 */
2333int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2334 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2335{
2336 int ret = 0;
bec39940
DG
2337 struct lttng_ht_node_str *ua_chan_node;
2338 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
2339 struct ust_app_channel *ua_chan = NULL;
2340 struct ust_app_session *ua_sess;
2341 struct ust_app *app;
2342
2343 rcu_read_lock();
2344
852d0037 2345 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2346 if (!app->compatible) {
2347 /*
2348 * TODO: In time, we should notice the caller of this error by
2349 * telling him that this is a version error.
2350 */
2351 continue;
2352 }
55cc08a6
DG
2353 ua_sess = lookup_session_by_app(usess, app);
2354 if (ua_sess == NULL) {
2355 continue;
2356 }
2357
2358 /* Lookup channel in the ust app session */
bec39940
DG
2359 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2360 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2361 if (ua_chan_node == NULL) {
2362 continue;
2363 }
2364 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2365 node);
2366
2367 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2368 if (ret < 0) {
2369 continue;
2370 }
2371 }
2372
55cc08a6
DG
2373 rcu_read_unlock();
2374 return ret;
2375}
2376
2377/*
2378 * Add context to a specific event in a channel for global UST domain.
2379 */
2380int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2381 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2382 struct ltt_ust_context *uctx)
2383{
2384 int ret = 0;
bec39940
DG
2385 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2386 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
2387 struct ust_app_session *ua_sess;
2388 struct ust_app_event *ua_event;
2389 struct ust_app_channel *ua_chan = NULL;
2390 struct ust_app *app;
2391
2392 rcu_read_lock();
2393
852d0037 2394 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2395 if (!app->compatible) {
2396 /*
2397 * TODO: In time, we should notice the caller of this error by
2398 * telling him that this is a version error.
2399 */
2400 continue;
2401 }
55cc08a6
DG
2402 ua_sess = lookup_session_by_app(usess, app);
2403 if (ua_sess == NULL) {
2404 continue;
2405 }
2406
2407 /* Lookup channel in the ust app session */
bec39940
DG
2408 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2409 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2410 if (ua_chan_node == NULL) {
2411 continue;
2412 }
2413 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2414 node);
2415
bec39940
DG
2416 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2417 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2418 if (ua_event_node == NULL) {
2419 continue;
2420 }
2421 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2422 node);
2423
2424 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2425 if (ret < 0) {
2426 continue;
2427 }
2428 }
2429
55cc08a6
DG
2430 rcu_read_unlock();
2431 return ret;
2432}
76d45b40
DG
2433
2434/*
2435 * Enable event for a channel from a UST session for a specific PID.
2436 */
2437int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2438 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2439{
2440 int ret = 0;
bec39940
DG
2441 struct lttng_ht_iter iter;
2442 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
76d45b40
DG
2443 struct ust_app *app;
2444 struct ust_app_session *ua_sess;
2445 struct ust_app_channel *ua_chan;
2446 struct ust_app_event *ua_event;
2447
2448 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2449
2450 rcu_read_lock();
2451
2452 app = ust_app_find_by_pid(pid);
2453 if (app == NULL) {
2454 ERR("UST app enable event per PID %d not found", pid);
2455 ret = -1;
2456 goto error;
2457 }
2458
e0c7ec2b
DG
2459 if (!app->compatible) {
2460 ret = 0;
2461 goto error;
2462 }
2463
76d45b40
DG
2464 ua_sess = lookup_session_by_app(usess, app);
2465 /* If ua_sess is NULL, there is a code flow error */
2466 assert(ua_sess);
2467
2468 /* Lookup channel in the ust app session */
bec39940
DG
2469 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2470 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
76d45b40
DG
2471 /* If the channel is not found, there is a code flow error */
2472 assert(ua_chan_node);
2473
2474 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2475
bec39940
DG
2476 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2477 ua_event_node = lttng_ht_iter_get_node_str(&iter);
76d45b40
DG
2478 if (ua_event_node == NULL) {
2479 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2480 if (ret < 0) {
2481 goto error;
2482 }
2483 } else {
2484 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2485
2486 ret = enable_ust_app_event(ua_sess, ua_event, app);
2487 if (ret < 0) {
2488 goto error;
2489 }
2490 }
2491
2492error:
2493 rcu_read_unlock();
2494 return ret;
2495}
7f79d3a1
DG
2496
2497/*
2498 * Disable event for a channel from a UST session for a specific PID.
2499 */
2500int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2501 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2502{
2503 int ret = 0;
bec39940
DG
2504 struct lttng_ht_iter iter;
2505 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
7f79d3a1
DG
2506 struct ust_app *app;
2507 struct ust_app_session *ua_sess;
2508 struct ust_app_channel *ua_chan;
2509 struct ust_app_event *ua_event;
2510
2511 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2512
2513 rcu_read_lock();
2514
2515 app = ust_app_find_by_pid(pid);
2516 if (app == NULL) {
2517 ERR("UST app disable event per PID %d not found", pid);
2518 ret = -1;
2519 goto error;
2520 }
2521
e0c7ec2b
DG
2522 if (!app->compatible) {
2523 ret = 0;
2524 goto error;
2525 }
2526
7f79d3a1
DG
2527 ua_sess = lookup_session_by_app(usess, app);
2528 /* If ua_sess is NULL, there is a code flow error */
2529 assert(ua_sess);
2530
2531 /* Lookup channel in the ust app session */
bec39940
DG
2532 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2533 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
7f79d3a1
DG
2534 if (ua_chan_node == NULL) {
2535 /* Channel does not exist, skip disabling */
2536 goto error;
2537 }
2538 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2539
bec39940
DG
2540 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2541 ua_event_node = lttng_ht_iter_get_node_str(&iter);
7f79d3a1
DG
2542 if (ua_event_node == NULL) {
2543 /* Event does not exist, skip disabling */
2544 goto error;
2545 }
2546 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2547
2548 ret = disable_ust_app_event(ua_sess, ua_event, app);
2549 if (ret < 0) {
2550 goto error;
2551 }
2552
2553error:
2554 rcu_read_unlock();
2555 return ret;
2556}
e0c7ec2b
DG
2557
2558/*
2559 * Validate version of UST apps and set the compatible bit.
2560 */
2561int ust_app_validate_version(int sock)
2562{
2563 int ret;
2564 struct ust_app *app;
2565
2566 rcu_read_lock();
2567
2568 app = find_app_by_sock(sock);
2569 assert(app);
2570
2571 ret = ustctl_tracer_version(sock, &app->version);
2572 if (ret < 0) {
2573 goto error;
2574 }
2575
2576 /* Validate version */
2577 if (app->version.major > UST_APP_MAJOR_VERSION) {
2578 goto error;
2579 }
2580
2581 DBG2("UST app PID %d is compatible with major version %d "
852d0037 2582 "(supporting <= %d)", app->pid, app->version.major,
e0c7ec2b
DG
2583 UST_APP_MAJOR_VERSION);
2584 app->compatible = 1;
2585 rcu_read_unlock();
2586 return 0;
2587
2588error:
2589 DBG2("UST app PID %d is not compatible with major version %d "
852d0037 2590 "(supporting <= %d)", app->pid, app->version.major,
e0c7ec2b
DG
2591 UST_APP_MAJOR_VERSION);
2592 app->compatible = 0;
2593 rcu_read_unlock();
2594 return -1;
2595}
4466912f
DG
2596
2597/*
2598 * Calibrate registered applications.
2599 */
2600int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
2601{
2602 int ret = 0;
2603 struct lttng_ht_iter iter;
2604 struct ust_app *app;
2605
2606 rcu_read_lock();
2607
852d0037 2608 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4466912f
DG
2609 if (!app->compatible) {
2610 /*
2611 * TODO: In time, we should notice the caller of this error by
2612 * telling him that this is a version error.
2613 */
2614 continue;
2615 }
2616
852d0037 2617 ret = ustctl_calibrate(app->sock, calibrate);
4466912f
DG
2618 if (ret < 0) {
2619 switch (ret) {
2620 case -ENOSYS:
2621 /* Means that it's not implemented on the tracer side. */
2622 ret = 0;
2623 break;
2624 default:
2625 /* TODO: Report error to user */
2626 DBG2("Calibrate app PID %d returned with error %d",
852d0037 2627 app->pid, ret);
4466912f
DG
2628 break;
2629 }
2630 }
2631 }
2632
2633 DBG("UST app global domain calibration finished");
2634
2635 rcu_read_unlock();
2636
2637 return ret;
2638}
This page took 0.165024 seconds and 4 git commands to generate.