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