Support new liblttng-ust-ctl error code
[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 if (ret == -EEXIST || ret == -EPERM) {
716 ret = 0;
717 goto error;
718 }
719 ERR("Error ustctl create event %s for app pid: %d with ret %d",
720 ua_event->attr.name, app->pid, ret);
721 goto error;
722 }
723
724 ua_event->handle = ua_event->obj->handle;
725
726 DBG2("UST app event %s created successfully for pid:%d",
727 ua_event->attr.name, app->pid);
728
729 health_code_update(&health_thread_cmd);
730
731 /* If event not enabled, disable it on the tracer */
732 if (ua_event->enabled == 0) {
733 ret = disable_ust_event(app, ua_sess, ua_event);
734 if (ret < 0) {
735 /*
736 * If we hit an EPERM, something is wrong with our disable call. If
737 * we get an EEXIST, there is a problem on the tracer side since we
738 * just created it.
739 */
740 switch (ret) {
741 case -EPERM:
742 /* Code flow problem */
743 assert(0);
744 case -EEXIST:
745 /* It's OK for our use case. */
746 ret = 0;
747 break;
748 default:
749 break;
750 }
751 goto error;
752 }
753 }
754
755 error:
756 health_code_update(&health_thread_cmd);
757 return ret;
758 }
759
760 /*
761 * Copy data between an UST app event and a LTT event.
762 */
763 static void shadow_copy_event(struct ust_app_event *ua_event,
764 struct ltt_ust_event *uevent)
765 {
766 struct lttng_ht_iter iter;
767 struct ltt_ust_context *uctx;
768 struct ust_app_ctx *ua_ctx;
769
770 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
771 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
772
773 ua_event->enabled = uevent->enabled;
774
775 /* Copy event attributes */
776 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
777
778 /* Copy filter bytecode */
779 if (uevent->filter) {
780 ua_event->filter = zmalloc(sizeof(*ua_event->filter) +
781 uevent->filter->len);
782 if (!ua_event->filter) {
783 return;
784 }
785 memcpy(ua_event->filter, uevent->filter,
786 sizeof(*ua_event->filter) + uevent->filter->len);
787 }
788 cds_lfht_for_each_entry(uevent->ctx->ht, &iter.iter, uctx, node.node) {
789 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
790 if (ua_ctx == NULL) {
791 /* malloc() failed. We should simply stop */
792 return;
793 }
794
795 lttng_ht_node_init_ulong(&ua_ctx->node,
796 (unsigned long) ua_ctx->ctx.ctx);
797 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
798 }
799 }
800
801 /*
802 * Copy data between an UST app channel and a LTT channel.
803 */
804 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
805 struct ltt_ust_channel *uchan)
806 {
807 struct lttng_ht_iter iter;
808 struct lttng_ht_node_str *ua_event_node;
809 struct ltt_ust_event *uevent;
810 struct ltt_ust_context *uctx;
811 struct ust_app_event *ua_event;
812 struct ust_app_ctx *ua_ctx;
813
814 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
815
816 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
817 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
818 /* Copy event attributes */
819 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
820
821 ua_chan->enabled = uchan->enabled;
822
823 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
824 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
825 if (ua_ctx == NULL) {
826 continue;
827 }
828 lttng_ht_node_init_ulong(&ua_ctx->node,
829 (unsigned long) ua_ctx->ctx.ctx);
830 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
831 }
832
833 /* Copy all events from ltt ust channel to ust app channel */
834 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
835 struct lttng_ht_iter uiter;
836
837 lttng_ht_lookup(ua_chan->events, (void *) uevent->attr.name, &uiter);
838 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
839 if (ua_event_node == NULL) {
840 DBG2("UST event %s not found on shadow copy channel",
841 uevent->attr.name);
842 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
843 if (ua_event == NULL) {
844 continue;
845 }
846 shadow_copy_event(ua_event, uevent);
847 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
848 }
849 }
850
851 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
852 }
853
854 /*
855 * Copy data between a UST app session and a regular LTT session.
856 */
857 static void shadow_copy_session(struct ust_app_session *ua_sess,
858 struct ltt_ust_session *usess, struct ust_app *app)
859 {
860 struct lttng_ht_node_str *ua_chan_node;
861 struct lttng_ht_iter iter;
862 struct ltt_ust_channel *uchan;
863 struct ust_app_channel *ua_chan;
864 time_t rawtime;
865 struct tm *timeinfo;
866 char datetime[16];
867 int ret;
868
869 /* Get date and time for unique app path */
870 time(&rawtime);
871 timeinfo = localtime(&rawtime);
872 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
873
874 DBG2("Shadow copy of session handle %d", ua_sess->handle);
875
876 ua_sess->id = usess->id;
877 ua_sess->uid = usess->uid;
878 ua_sess->gid = usess->gid;
879
880 ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
881 datetime);
882 if (ret < 0) {
883 PERROR("asprintf UST shadow copy session");
884 /* TODO: We cannot return an error from here.. */
885 assert(0);
886 }
887
888 /* TODO: support all UST domain */
889
890 /* Iterate over all channels in global domain. */
891 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
892 uchan, node.node) {
893 struct lttng_ht_iter uiter;
894
895 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
896 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
897 if (ua_chan_node != NULL) {
898 /* Session exist. Contiuing. */
899 continue;
900 }
901
902 DBG2("Channel %s not found on shadow session copy, creating it",
903 uchan->name);
904 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
905 if (ua_chan == NULL) {
906 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
907 continue;
908 }
909
910 shadow_copy_channel(ua_chan, uchan);
911 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
912 }
913 }
914
915 /*
916 * Lookup sesison wrapper.
917 */
918 static
919 void __lookup_session_by_app(struct ltt_ust_session *usess,
920 struct ust_app *app, struct lttng_ht_iter *iter)
921 {
922 /* Get right UST app session from app */
923 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
924 }
925
926 /*
927 * Return ust app session from the app session hashtable using the UST session
928 * id.
929 */
930 static struct ust_app_session *lookup_session_by_app(
931 struct ltt_ust_session *usess, struct ust_app *app)
932 {
933 struct lttng_ht_iter iter;
934 struct lttng_ht_node_ulong *node;
935
936 __lookup_session_by_app(usess, app, &iter);
937 node = lttng_ht_iter_get_node_ulong(&iter);
938 if (node == NULL) {
939 goto error;
940 }
941
942 return caa_container_of(node, struct ust_app_session, node);
943
944 error:
945 return NULL;
946 }
947
948 /*
949 * Create a UST session onto the tracer of app and add it the session
950 * hashtable.
951 *
952 * Return ust app session or NULL on error.
953 */
954 static struct ust_app_session *create_ust_app_session(
955 struct ltt_ust_session *usess, struct ust_app *app)
956 {
957 int ret;
958 struct ust_app_session *ua_sess;
959
960 health_code_update(&health_thread_cmd);
961
962 ua_sess = lookup_session_by_app(usess, app);
963 if (ua_sess == NULL) {
964 DBG2("UST app pid: %d session id %d not found, creating it",
965 app->pid, usess->id);
966 ua_sess = alloc_ust_app_session();
967 if (ua_sess == NULL) {
968 /* Only malloc can failed so something is really wrong */
969 goto end;
970 }
971 shadow_copy_session(ua_sess, usess, app);
972 }
973
974 health_code_update(&health_thread_cmd);
975
976 if (ua_sess->handle == -1) {
977 ret = ustctl_create_session(app->sock);
978 if (ret < 0) {
979 ERR("Creating session for app pid %d", app->pid);
980 delete_ust_app_session(-1, ua_sess);
981 /* This means that the tracer is gone... */
982 ua_sess = (void*) -1UL;
983 goto end;
984 }
985
986 ua_sess->handle = ret;
987
988 /* Add ust app session to app's HT */
989 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
990 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
991
992 DBG2("UST app session created successfully with handle %d", ret);
993 }
994
995 end:
996 health_code_update(&health_thread_cmd);
997 return ua_sess;
998 }
999
1000 /*
1001 * Create a context for the channel on the tracer.
1002 */
1003 static
1004 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
1005 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
1006 struct ust_app *app)
1007 {
1008 int ret = 0;
1009 struct lttng_ht_iter iter;
1010 struct lttng_ht_node_ulong *node;
1011 struct ust_app_ctx *ua_ctx;
1012
1013 DBG2("UST app adding context to channel %s", ua_chan->name);
1014
1015 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1016 node = lttng_ht_iter_get_node_ulong(&iter);
1017 if (node != NULL) {
1018 ret = -EEXIST;
1019 goto error;
1020 }
1021
1022 ua_ctx = alloc_ust_app_ctx(uctx);
1023 if (ua_ctx == NULL) {
1024 /* malloc failed */
1025 ret = -1;
1026 goto error;
1027 }
1028
1029 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1030 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
1031
1032 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
1033 if (ret < 0) {
1034 goto error;
1035 }
1036
1037 error:
1038 return ret;
1039 }
1040
1041 /*
1042 * Create an UST context and enable it for the event on the tracer.
1043 */
1044 static
1045 int create_ust_app_event_context(struct ust_app_session *ua_sess,
1046 struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
1047 struct ust_app *app)
1048 {
1049 int ret = 0;
1050 struct lttng_ht_iter iter;
1051 struct lttng_ht_node_ulong *node;
1052 struct ust_app_ctx *ua_ctx;
1053
1054 DBG2("UST app adding context to event %s", ua_event->name);
1055
1056 lttng_ht_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1057 node = lttng_ht_iter_get_node_ulong(&iter);
1058 if (node != NULL) {
1059 ret = -EEXIST;
1060 goto error;
1061 }
1062
1063 ua_ctx = alloc_ust_app_ctx(uctx);
1064 if (ua_ctx == NULL) {
1065 /* malloc failed */
1066 ret = -1;
1067 goto error;
1068 }
1069
1070 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1071 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
1072
1073 ret = create_ust_event_context(ua_event, ua_ctx, app);
1074 if (ret < 0) {
1075 goto error;
1076 }
1077
1078 error:
1079 return ret;
1080 }
1081
1082 /*
1083 * Set UST filter for the event on the tracer.
1084 */
1085 static
1086 int set_ust_app_event_filter(struct ust_app_session *ua_sess,
1087 struct ust_app_event *ua_event,
1088 struct lttng_filter_bytecode *bytecode,
1089 struct ust_app *app)
1090 {
1091 int ret = 0;
1092
1093 DBG2("UST app adding context to event %s", ua_event->name);
1094
1095 /* Copy filter bytecode */
1096 ua_event->filter = zmalloc(sizeof(*ua_event->filter) + bytecode->len);
1097 if (!ua_event->filter) {
1098 return -ENOMEM;
1099 }
1100 memcpy(ua_event->filter, bytecode,
1101 sizeof(*ua_event->filter) + bytecode->len);
1102 ret = set_ust_event_filter(ua_event, app);
1103 if (ret < 0) {
1104 goto error;
1105 }
1106
1107 error:
1108 return ret;
1109 }
1110
1111 /*
1112 * Enable on the tracer side a ust app event for the session and channel.
1113 */
1114 static
1115 int enable_ust_app_event(struct ust_app_session *ua_sess,
1116 struct ust_app_event *ua_event, struct ust_app *app)
1117 {
1118 int ret;
1119
1120 ret = enable_ust_event(app, ua_sess, ua_event);
1121 if (ret < 0) {
1122 goto error;
1123 }
1124
1125 ua_event->enabled = 1;
1126
1127 error:
1128 return ret;
1129 }
1130
1131 /*
1132 * Disable on the tracer side a ust app event for the session and channel.
1133 */
1134 static int disable_ust_app_event(struct ust_app_session *ua_sess,
1135 struct ust_app_event *ua_event, struct ust_app *app)
1136 {
1137 int ret;
1138
1139 ret = disable_ust_event(app, ua_sess, ua_event);
1140 if (ret < 0) {
1141 goto error;
1142 }
1143
1144 ua_event->enabled = 0;
1145
1146 error:
1147 return ret;
1148 }
1149
1150 /*
1151 * Lookup ust app channel for session and disable it on the tracer side.
1152 */
1153 static
1154 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1155 struct ust_app_channel *ua_chan, struct ust_app *app)
1156 {
1157 int ret;
1158
1159 ret = disable_ust_channel(app, ua_sess, ua_chan);
1160 if (ret < 0) {
1161 goto error;
1162 }
1163
1164 ua_chan->enabled = 0;
1165
1166 error:
1167 return ret;
1168 }
1169
1170 /*
1171 * Lookup ust app channel for session and enable it on the tracer side.
1172 */
1173 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1174 struct ltt_ust_channel *uchan, struct ust_app *app)
1175 {
1176 int ret = 0;
1177 struct lttng_ht_iter iter;
1178 struct lttng_ht_node_str *ua_chan_node;
1179 struct ust_app_channel *ua_chan;
1180
1181 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1182 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1183 if (ua_chan_node == NULL) {
1184 DBG2("Unable to find channel %s in ust session id %u",
1185 uchan->name, ua_sess->id);
1186 goto error;
1187 }
1188
1189 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1190
1191 ret = enable_ust_channel(app, ua_sess, ua_chan);
1192 if (ret < 0) {
1193 goto error;
1194 }
1195
1196 error:
1197 return ret;
1198 }
1199
1200 /*
1201 * Create UST app channel and create it on the tracer.
1202 */
1203 static struct ust_app_channel *create_ust_app_channel(
1204 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1205 struct ust_app *app)
1206 {
1207 int ret = 0;
1208 struct lttng_ht_iter iter;
1209 struct lttng_ht_node_str *ua_chan_node;
1210 struct ust_app_channel *ua_chan;
1211
1212 /* Lookup channel in the ust app session */
1213 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1214 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1215 if (ua_chan_node != NULL) {
1216 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1217 goto end;
1218 }
1219
1220 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1221 if (ua_chan == NULL) {
1222 /* Only malloc can fail here */
1223 goto error;
1224 }
1225 shadow_copy_channel(ua_chan, uchan);
1226
1227 ret = create_ust_channel(app, ua_sess, ua_chan);
1228 if (ret < 0) {
1229 /* Not found previously means that it does not exist on the tracer */
1230 assert(ret != -EEXIST);
1231 goto error;
1232 }
1233
1234 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1235
1236 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
1237 app->pid);
1238
1239 end:
1240 return ua_chan;
1241
1242 error:
1243 delete_ust_app_channel(-1, ua_chan);
1244 return NULL;
1245 }
1246
1247 /*
1248 * Create UST app event and create it on the tracer side.
1249 */
1250 static
1251 int create_ust_app_event(struct ust_app_session *ua_sess,
1252 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1253 struct ust_app *app)
1254 {
1255 int ret = 0;
1256 struct lttng_ht_iter iter;
1257 struct lttng_ht_node_str *ua_event_node;
1258 struct ust_app_event *ua_event;
1259
1260 /* Get event node */
1261 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
1262 ua_event_node = lttng_ht_iter_get_node_str(&iter);
1263 if (ua_event_node != NULL) {
1264 ret = -EEXIST;
1265 goto end;
1266 }
1267
1268 /* Does not exist so create one */
1269 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1270 if (ua_event == NULL) {
1271 /* Only malloc can failed so something is really wrong */
1272 ret = -ENOMEM;
1273 goto end;
1274 }
1275 shadow_copy_event(ua_event, uevent);
1276
1277 /* Create it on the tracer side */
1278 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1279 if (ret < 0) {
1280 /* Not found previously means that it does not exist on the tracer */
1281 assert(ret != -EEXIST);
1282 goto error;
1283 }
1284
1285 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
1286
1287 DBG2("UST app create event %s for PID %d completed", ua_event->name,
1288 app->pid);
1289
1290 end:
1291 return ret;
1292
1293 error:
1294 /* Valid. Calling here is already in a read side lock */
1295 delete_ust_app_event(-1, ua_event);
1296 return ret;
1297 }
1298
1299 /*
1300 * Create UST metadata and open it on the tracer side.
1301 */
1302 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1303 char *pathname, struct ust_app *app)
1304 {
1305 int ret = 0;
1306
1307 if (ua_sess->metadata == NULL) {
1308 /* Allocate UST metadata */
1309 ua_sess->metadata = trace_ust_create_metadata(pathname);
1310 if (ua_sess->metadata == NULL) {
1311 /* malloc() failed */
1312 goto error;
1313 }
1314
1315 ret = open_ust_metadata(app, ua_sess);
1316 if (ret < 0) {
1317 DBG3("Opening metadata failed. Cleaning up memory");
1318
1319 /* Cleanup failed metadata struct */
1320 free(ua_sess->metadata);
1321 /*
1322 * This is very important because delete_ust_app_session check if
1323 * the pointer is null or not in order to delete the metadata.
1324 */
1325 ua_sess->metadata = NULL;
1326 goto error;
1327 }
1328
1329 DBG2("UST metadata opened for app pid %d", app->pid);
1330 }
1331
1332 /* Open UST metadata stream */
1333 if (ua_sess->metadata->stream_obj == NULL) {
1334 ret = create_ust_stream(app, ua_sess);
1335 if (ret < 0) {
1336 goto error;
1337 }
1338
1339 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1340 "%s/metadata", ua_sess->path);
1341 if (ret < 0) {
1342 PERROR("asprintf UST create stream");
1343 goto error;
1344 }
1345
1346 DBG2("UST metadata stream object created for app pid %d",
1347 app->pid);
1348 } else {
1349 ERR("Attempting to create stream without metadata opened");
1350 goto error;
1351 }
1352
1353 return 0;
1354
1355 error:
1356 return -1;
1357 }
1358
1359 /*
1360 * Return pointer to traceable apps list.
1361 */
1362 struct lttng_ht *ust_app_get_ht(void)
1363 {
1364 return ust_app_ht;
1365 }
1366
1367 /*
1368 * Return ust app pointer or NULL if not found.
1369 */
1370 struct ust_app *ust_app_find_by_pid(pid_t pid)
1371 {
1372 struct lttng_ht_node_ulong *node;
1373 struct lttng_ht_iter iter;
1374
1375 rcu_read_lock();
1376 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1377 node = lttng_ht_iter_get_node_ulong(&iter);
1378 if (node == NULL) {
1379 DBG2("UST app no found with pid %d", pid);
1380 goto error;
1381 }
1382 rcu_read_unlock();
1383
1384 DBG2("Found UST app by pid %d", pid);
1385
1386 return caa_container_of(node, struct ust_app, pid_n);
1387
1388 error:
1389 rcu_read_unlock();
1390 return NULL;
1391 }
1392
1393 /*
1394 * Using pid and uid (of the app), allocate a new ust_app struct and
1395 * add it to the global traceable app list.
1396 *
1397 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1398 * bitness is not supported.
1399 */
1400 int ust_app_register(struct ust_register_msg *msg, int sock)
1401 {
1402 struct ust_app *lta;
1403 int ret;
1404
1405 if ((msg->bits_per_long == 64 &&
1406 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
1407 || (msg->bits_per_long == 32 &&
1408 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
1409 ERR("Registration failed: application \"%s\" (pid: %d) has "
1410 "%d-bit long, but no consumerd for this long size is available.\n",
1411 msg->name, msg->pid, msg->bits_per_long);
1412 ret = close(sock);
1413 if (ret) {
1414 PERROR("close");
1415 }
1416 lttng_fd_put(LTTNG_FD_APPS, 1);
1417 return -EINVAL;
1418 }
1419 if (msg->major != LTTNG_UST_COMM_MAJOR) {
1420 ERR("Registration failed: application \"%s\" (pid: %d) has "
1421 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1422 msg->name, msg->pid, msg->major, msg->minor);
1423 ret = close(sock);
1424 if (ret) {
1425 PERROR("close");
1426 }
1427 lttng_fd_put(LTTNG_FD_APPS, 1);
1428 return -EINVAL;
1429 }
1430 lta = zmalloc(sizeof(struct ust_app));
1431 if (lta == NULL) {
1432 PERROR("malloc");
1433 return -ENOMEM;
1434 }
1435
1436 lta->ppid = msg->ppid;
1437 lta->uid = msg->uid;
1438 lta->gid = msg->gid;
1439 lta->compatible = 0; /* Not compatible until proven */
1440 lta->bits_per_long = msg->bits_per_long;
1441 lta->v_major = msg->major;
1442 lta->v_minor = msg->minor;
1443 strncpy(lta->name, msg->name, sizeof(lta->name));
1444 lta->name[16] = '\0';
1445 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1446
1447 lta->pid = msg->pid;
1448 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long)lta->pid);
1449 lta->sock = sock;
1450 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long)lta->sock);
1451
1452 rcu_read_lock();
1453
1454 /*
1455 * On a re-registration, we want to kick out the previous registration of
1456 * that pid
1457 */
1458 lttng_ht_add_replace_ulong(ust_app_ht, &lta->pid_n);
1459
1460 /*
1461 * The socket _should_ be unique until _we_ call close. So, a add_unique
1462 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1463 * already in the table.
1464 */
1465 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &lta->sock_n);
1466
1467 rcu_read_unlock();
1468
1469 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1470 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
1471 lta->sock, lta->name, lta->v_major, lta->v_minor);
1472
1473 return 0;
1474 }
1475
1476 /*
1477 * Unregister app by removing it from the global traceable app list and freeing
1478 * the data struct.
1479 *
1480 * The socket is already closed at this point so no close to sock.
1481 */
1482 void ust_app_unregister(int sock)
1483 {
1484 struct ust_app *lta;
1485 struct lttng_ht_node_ulong *node;
1486 struct lttng_ht_iter iter;
1487 int ret;
1488
1489 rcu_read_lock();
1490
1491 /* Get the node reference for a call_rcu */
1492 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1493 node = lttng_ht_iter_get_node_ulong(&iter);
1494 if (node == NULL) {
1495 ERR("Unable to find app by sock %d", sock);
1496 goto error;
1497 }
1498
1499 lta = caa_container_of(node, struct ust_app, sock_n);
1500
1501 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1502
1503 /* Remove application from PID hash table */
1504 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1505 assert(!ret);
1506
1507 /* Assign second node for deletion */
1508 iter.iter.node = &lta->pid_n.node;
1509
1510 /*
1511 * Ignore return value since the node might have been removed before by an
1512 * add replace during app registration because the PID can be reassigned by
1513 * the OS.
1514 */
1515 ret = lttng_ht_del(ust_app_ht, &iter);
1516 if (ret) {
1517 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1518 lta->pid);
1519 }
1520
1521 /* Free memory */
1522 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1523
1524 error:
1525 rcu_read_unlock();
1526 return;
1527 }
1528
1529 /*
1530 * Return traceable_app_count
1531 */
1532 unsigned long ust_app_list_count(void)
1533 {
1534 unsigned long count;
1535
1536 rcu_read_lock();
1537 count = lttng_ht_get_count(ust_app_ht);
1538 rcu_read_unlock();
1539
1540 return count;
1541 }
1542
1543 /*
1544 * Fill events array with all events name of all registered apps.
1545 */
1546 int ust_app_list_events(struct lttng_event **events)
1547 {
1548 int ret, handle;
1549 size_t nbmem, count = 0;
1550 struct lttng_ht_iter iter;
1551 struct ust_app *app;
1552 struct lttng_event *tmp;
1553
1554 nbmem = UST_APP_EVENT_LIST_SIZE;
1555 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1556 if (tmp == NULL) {
1557 PERROR("zmalloc ust app events");
1558 ret = -ENOMEM;
1559 goto error;
1560 }
1561
1562 rcu_read_lock();
1563
1564 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1565 struct lttng_ust_tracepoint_iter uiter;
1566
1567 health_code_update(&health_thread_cmd);
1568
1569 if (!app->compatible) {
1570 /*
1571 * TODO: In time, we should notice the caller of this error by
1572 * telling him that this is a version error.
1573 */
1574 continue;
1575 }
1576 handle = ustctl_tracepoint_list(app->sock);
1577 if (handle < 0) {
1578 ERR("UST app list events getting handle failed for app pid %d",
1579 app->pid);
1580 continue;
1581 }
1582
1583 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
1584 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1585 health_code_update(&health_thread_cmd);
1586 if (count >= nbmem) {
1587 /* In case the realloc fails, we free the memory */
1588 void *tmp_ptr = (void *) tmp;
1589 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1590 2 * nbmem);
1591 nbmem *= 2;
1592 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
1593 if (tmp == NULL) {
1594 PERROR("realloc ust app events");
1595 free(tmp_ptr);
1596 ret = -ENOMEM;
1597 goto rcu_error;
1598 }
1599 }
1600 memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1601 tmp[count].loglevel = uiter.loglevel;
1602 tmp[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
1603 tmp[count].pid = app->pid;
1604 tmp[count].enabled = -1;
1605 count++;
1606 }
1607 }
1608
1609 ret = count;
1610 *events = tmp;
1611
1612 DBG2("UST app list events done (%zu events)", count);
1613
1614 rcu_error:
1615 rcu_read_unlock();
1616 error:
1617 health_code_update(&health_thread_cmd);
1618 return ret;
1619 }
1620
1621 /*
1622 * Fill events array with all events name of all registered apps.
1623 */
1624 int ust_app_list_event_fields(struct lttng_event_field **fields)
1625 {
1626 int ret, handle;
1627 size_t nbmem, count = 0;
1628 struct lttng_ht_iter iter;
1629 struct ust_app *app;
1630 struct lttng_event_field *tmp;
1631
1632 nbmem = UST_APP_EVENT_LIST_SIZE;
1633 tmp = zmalloc(nbmem * sizeof(struct lttng_event_field));
1634 if (tmp == NULL) {
1635 PERROR("zmalloc ust app event fields");
1636 ret = -ENOMEM;
1637 goto error;
1638 }
1639
1640 rcu_read_lock();
1641
1642 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1643 struct lttng_ust_field_iter uiter;
1644
1645 health_code_update(&health_thread_cmd);
1646
1647 if (!app->compatible) {
1648 /*
1649 * TODO: In time, we should notice the caller of this error by
1650 * telling him that this is a version error.
1651 */
1652 continue;
1653 }
1654 handle = ustctl_tracepoint_field_list(app->sock);
1655 if (handle < 0) {
1656 ERR("UST app list event fields getting handle failed for app pid %d",
1657 app->pid);
1658 continue;
1659 }
1660
1661 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
1662 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1663 health_code_update(&health_thread_cmd);
1664 if (count >= nbmem) {
1665 /* In case the realloc fails, we free the memory */
1666 void *tmp_ptr = (void *) tmp;
1667 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
1668 2 * nbmem);
1669 nbmem *= 2;
1670 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event_field));
1671 if (tmp == NULL) {
1672 PERROR("realloc ust app event fields");
1673 free(tmp_ptr);
1674 ret = -ENOMEM;
1675 goto rcu_error;
1676 }
1677 }
1678
1679 memcpy(tmp[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
1680 tmp[count].type = uiter.type;
1681 tmp[count].nowrite = uiter.nowrite;
1682
1683 memcpy(tmp[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
1684 tmp[count].event.loglevel = uiter.loglevel;
1685 tmp[count].event.type = LTTNG_UST_TRACEPOINT;
1686 tmp[count].event.pid = app->pid;
1687 tmp[count].event.enabled = -1;
1688 count++;
1689 }
1690 }
1691
1692 ret = count;
1693 *fields = tmp;
1694
1695 DBG2("UST app list event fields done (%zu events)", count);
1696
1697 rcu_error:
1698 rcu_read_unlock();
1699 error:
1700 health_code_update(&health_thread_cmd);
1701 return ret;
1702 }
1703
1704 /*
1705 * Free and clean all traceable apps of the global list.
1706 */
1707 void ust_app_clean_list(void)
1708 {
1709 int ret;
1710 struct ust_app *app;
1711 struct lttng_ht_iter iter;
1712
1713 DBG2("UST app cleaning registered apps hash table");
1714
1715 rcu_read_lock();
1716
1717 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1718 ret = lttng_ht_del(ust_app_ht, &iter);
1719 assert(!ret);
1720 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
1721 }
1722
1723 /* Cleanup socket hash table */
1724 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
1725 sock_n.node) {
1726 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1727 assert(!ret);
1728 }
1729
1730 /* Destroy is done only when the ht is empty */
1731 lttng_ht_destroy(ust_app_ht);
1732 lttng_ht_destroy(ust_app_ht_by_sock);
1733
1734 rcu_read_unlock();
1735 }
1736
1737 /*
1738 * Init UST app hash table.
1739 */
1740 void ust_app_ht_alloc(void)
1741 {
1742 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1743 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1744 }
1745
1746 /*
1747 * For a specific UST session, disable the channel for all registered apps.
1748 */
1749 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
1750 struct ltt_ust_channel *uchan)
1751 {
1752 int ret = 0;
1753 struct lttng_ht_iter iter;
1754 struct lttng_ht_node_str *ua_chan_node;
1755 struct ust_app *app;
1756 struct ust_app_session *ua_sess;
1757 struct ust_app_channel *ua_chan;
1758
1759 if (usess == NULL || uchan == NULL) {
1760 ERR("Disabling UST global channel with NULL values");
1761 ret = -1;
1762 goto error;
1763 }
1764
1765 DBG2("UST app disabling channel %s from global domain for session id %d",
1766 uchan->name, usess->id);
1767
1768 rcu_read_lock();
1769
1770 /* For every registered applications */
1771 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1772 struct lttng_ht_iter uiter;
1773 if (!app->compatible) {
1774 /*
1775 * TODO: In time, we should notice the caller of this error by
1776 * telling him that this is a version error.
1777 */
1778 continue;
1779 }
1780 ua_sess = lookup_session_by_app(usess, app);
1781 if (ua_sess == NULL) {
1782 continue;
1783 }
1784
1785 /* Get channel */
1786 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1787 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1788 /* If the session if found for the app, the channel must be there */
1789 assert(ua_chan_node);
1790
1791 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1792 /* The channel must not be already disabled */
1793 assert(ua_chan->enabled == 1);
1794
1795 /* Disable channel onto application */
1796 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
1797 if (ret < 0) {
1798 /* XXX: We might want to report this error at some point... */
1799 continue;
1800 }
1801 }
1802
1803 rcu_read_unlock();
1804
1805 error:
1806 return ret;
1807 }
1808
1809 /*
1810 * For a specific UST session, enable the channel for all registered apps.
1811 */
1812 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
1813 struct ltt_ust_channel *uchan)
1814 {
1815 int ret = 0;
1816 struct lttng_ht_iter iter;
1817 struct ust_app *app;
1818 struct ust_app_session *ua_sess;
1819
1820 if (usess == NULL || uchan == NULL) {
1821 ERR("Adding UST global channel to NULL values");
1822 ret = -1;
1823 goto error;
1824 }
1825
1826 DBG2("UST app enabling channel %s to global domain for session id %d",
1827 uchan->name, usess->id);
1828
1829 rcu_read_lock();
1830
1831 /* For every registered applications */
1832 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
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 }
1840 ua_sess = lookup_session_by_app(usess, app);
1841 if (ua_sess == NULL) {
1842 continue;
1843 }
1844
1845 /* Enable channel onto application */
1846 ret = enable_ust_app_channel(ua_sess, uchan, app);
1847 if (ret < 0) {
1848 /* XXX: We might want to report this error at some point... */
1849 continue;
1850 }
1851 }
1852
1853 rcu_read_unlock();
1854
1855 error:
1856 return ret;
1857 }
1858
1859 /*
1860 * Disable an event in a channel and for a specific session.
1861 */
1862 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1863 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1864 {
1865 int ret = 0;
1866 struct lttng_ht_iter iter, uiter;
1867 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
1868 struct ust_app *app;
1869 struct ust_app_session *ua_sess;
1870 struct ust_app_channel *ua_chan;
1871 struct ust_app_event *ua_event;
1872
1873 DBG("UST app disabling event %s for all apps in channel "
1874 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
1875
1876 rcu_read_lock();
1877
1878 /* For all registered applications */
1879 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1880 if (!app->compatible) {
1881 /*
1882 * TODO: In time, we should notice the caller of this error by
1883 * telling him that this is a version error.
1884 */
1885 continue;
1886 }
1887 ua_sess = lookup_session_by_app(usess, app);
1888 if (ua_sess == NULL) {
1889 /* Next app */
1890 continue;
1891 }
1892
1893 /* Lookup channel in the ust app session */
1894 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1895 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1896 if (ua_chan_node == NULL) {
1897 DBG2("Channel %s not found in session id %d for app pid %d."
1898 "Skipping", uchan->name, usess->id, app->pid);
1899 continue;
1900 }
1901 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1902
1903 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
1904 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
1905 if (ua_event_node == NULL) {
1906 DBG2("Event %s not found in channel %s for app pid %d."
1907 "Skipping", uevent->attr.name, uchan->name, app->pid);
1908 continue;
1909 }
1910 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1911
1912 ret = disable_ust_app_event(ua_sess, ua_event, app);
1913 if (ret < 0) {
1914 /* XXX: Report error someday... */
1915 continue;
1916 }
1917 }
1918
1919 rcu_read_unlock();
1920
1921 return ret;
1922 }
1923
1924 /*
1925 * For a specific UST session and UST channel, the event for all
1926 * registered apps.
1927 */
1928 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
1929 struct ltt_ust_channel *uchan)
1930 {
1931 int ret = 0;
1932 struct lttng_ht_iter iter, uiter;
1933 struct lttng_ht_node_str *ua_chan_node;
1934 struct ust_app *app;
1935 struct ust_app_session *ua_sess;
1936 struct ust_app_channel *ua_chan;
1937 struct ust_app_event *ua_event;
1938
1939 DBG("UST app disabling all event for all apps in channel "
1940 "%s for session id %d", uchan->name, usess->id);
1941
1942 rcu_read_lock();
1943
1944 /* For all registered applications */
1945 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1946 if (!app->compatible) {
1947 /*
1948 * TODO: In time, we should notice the caller of this error by
1949 * telling him that this is a version error.
1950 */
1951 continue;
1952 }
1953 ua_sess = lookup_session_by_app(usess, app);
1954 /* If ua_sess is NULL, there is a code flow error */
1955 assert(ua_sess);
1956
1957 /* Lookup channel in the ust app session */
1958 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1959 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1960 /* If the channel is not found, there is a code flow error */
1961 assert(ua_chan_node);
1962
1963 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1964
1965 /* Disable each events of channel */
1966 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
1967 node.node) {
1968 ret = disable_ust_app_event(ua_sess, ua_event, app);
1969 if (ret < 0) {
1970 /* XXX: Report error someday... */
1971 continue;
1972 }
1973 }
1974 }
1975
1976 rcu_read_unlock();
1977
1978 return ret;
1979 }
1980
1981 /*
1982 * For a specific UST session, create the channel for all registered apps.
1983 */
1984 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
1985 struct ltt_ust_channel *uchan)
1986 {
1987 int ret = 0;
1988 struct lttng_ht_iter iter;
1989 struct ust_app *app;
1990 struct ust_app_session *ua_sess;
1991 struct ust_app_channel *ua_chan;
1992
1993 /* Very wrong code flow */
1994 assert(usess);
1995 assert(uchan);
1996
1997 DBG2("UST app adding channel %s to global domain for session id %d",
1998 uchan->name, usess->id);
1999
2000 rcu_read_lock();
2001
2002 /* For every registered applications */
2003 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2004 if (!app->compatible) {
2005 /*
2006 * TODO: In time, we should notice the caller of this error by
2007 * telling him that this is a version error.
2008 */
2009 continue;
2010 }
2011 /*
2012 * Create session on the tracer side and add it to app session HT. Note
2013 * that if session exist, it will simply return a pointer to the ust
2014 * app session.
2015 */
2016 ua_sess = create_ust_app_session(usess, app);
2017 if (ua_sess == NULL) {
2018 /* The malloc() failed. */
2019 ret = -1;
2020 goto error;
2021 } else if (ua_sess == (void *) -1UL) {
2022 /* The application's socket is not valid. Contiuing */
2023 ret = -1;
2024 continue;
2025 }
2026
2027 /* Create channel onto application */
2028 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
2029 if (ua_chan == NULL) {
2030 /* Major problem here and it's maybe the tracer or malloc() */
2031 ret = -1;
2032 goto error;
2033 }
2034 }
2035
2036 rcu_read_unlock();
2037
2038 error:
2039 return ret;
2040 }
2041
2042 /*
2043 * Enable event for a specific session and channel on the tracer.
2044 */
2045 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
2046 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2047 {
2048 int ret = 0;
2049 struct lttng_ht_iter iter, uiter;
2050 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2051 struct ust_app *app;
2052 struct ust_app_session *ua_sess;
2053 struct ust_app_channel *ua_chan;
2054 struct ust_app_event *ua_event;
2055
2056 DBG("UST app enabling event %s for all apps for session id %d",
2057 uevent->attr.name, usess->id);
2058
2059 /*
2060 * NOTE: At this point, this function is called only if the session and
2061 * channel passed are already created for all apps. and enabled on the
2062 * tracer also.
2063 */
2064
2065 rcu_read_lock();
2066
2067 /* For all registered applications */
2068 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2069 if (!app->compatible) {
2070 /*
2071 * TODO: In time, we should notice the caller of this error by
2072 * telling him that this is a version error.
2073 */
2074 continue;
2075 }
2076 ua_sess = lookup_session_by_app(usess, app);
2077 /* If ua_sess is NULL, there is a code flow error */
2078 assert(ua_sess);
2079
2080 /* Lookup channel in the ust app session */
2081 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2082 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2083 /* If the channel is not found, there is a code flow error */
2084 assert(ua_chan_node);
2085
2086 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2087
2088 lttng_ht_lookup(ua_chan->events, (void*)uevent->attr.name, &uiter);
2089 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
2090 if (ua_event_node == NULL) {
2091 DBG3("UST app enable event %s not found for app PID %d."
2092 "Skipping app", uevent->attr.name, app->pid);
2093 continue;
2094 }
2095 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2096
2097 ret = enable_ust_app_event(ua_sess, ua_event, app);
2098 if (ret < 0) {
2099 goto error;
2100 }
2101 }
2102
2103 error:
2104 rcu_read_unlock();
2105 return ret;
2106 }
2107
2108 /*
2109 * For a specific existing UST session and UST channel, creates the event for
2110 * all registered apps.
2111 */
2112 int ust_app_create_event_glb(struct ltt_ust_session *usess,
2113 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2114 {
2115 int ret = 0;
2116 struct lttng_ht_iter iter, uiter;
2117 struct lttng_ht_node_str *ua_chan_node;
2118 struct ust_app *app;
2119 struct ust_app_session *ua_sess;
2120 struct ust_app_channel *ua_chan;
2121
2122 DBG("UST app creating event %s for all apps for session id %d",
2123 uevent->attr.name, usess->id);
2124
2125 rcu_read_lock();
2126
2127 /* For all registered applications */
2128 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2129 if (!app->compatible) {
2130 /*
2131 * TODO: In time, we should notice the caller of this error by
2132 * telling him that this is a version error.
2133 */
2134 continue;
2135 }
2136 ua_sess = lookup_session_by_app(usess, app);
2137 /* If ua_sess is NULL, there is a code flow error */
2138 assert(ua_sess);
2139
2140 /* Lookup channel in the ust app session */
2141 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2142 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2143 /* If the channel is not found, there is a code flow error */
2144 assert(ua_chan_node);
2145
2146 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2147
2148 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2149 if (ret < 0) {
2150 if (ret != -EEXIST) {
2151 /* Possible value at this point: -ENOMEM. If so, we stop! */
2152 break;
2153 }
2154 DBG2("UST app event %s already exist on app PID %d",
2155 uevent->attr.name, app->pid);
2156 continue;
2157 }
2158 }
2159
2160 rcu_read_unlock();
2161
2162 return ret;
2163 }
2164
2165 /*
2166 * Start tracing for a specific UST session and app.
2167 */
2168 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
2169 {
2170 int ret = 0;
2171 struct lttng_ht_iter iter;
2172 struct ust_app_session *ua_sess;
2173 struct ust_app_channel *ua_chan;
2174 struct ltt_ust_stream *ustream;
2175 struct consumer_socket *socket;
2176
2177 DBG("Starting tracing for ust app pid %d", app->pid);
2178
2179 rcu_read_lock();
2180
2181 if (!app->compatible) {
2182 goto end;
2183 }
2184
2185 ua_sess = lookup_session_by_app(usess, app);
2186 if (ua_sess == NULL) {
2187 goto error_rcu_unlock;
2188 }
2189
2190 /* Upon restart, we skip the setup, already done */
2191 if (ua_sess->started) {
2192 goto skip_setup;
2193 }
2194
2195 /* Create directories if consumer is LOCAL and has a path defined. */
2196 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
2197 strlen(usess->consumer->dst.trace_path) > 0) {
2198 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
2199 S_IRWXU | S_IRWXG, usess->uid, usess->gid);
2200 if (ret < 0) {
2201 if (ret != -EEXIST) {
2202 ERR("Trace directory creation error");
2203 ret = -1;
2204 goto error_rcu_unlock;
2205 }
2206 }
2207 }
2208
2209 /* Indicate that the session has been started once */
2210 ua_sess->started = 1;
2211
2212 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
2213 if (ret < 0) {
2214 ret = LTTNG_ERR_UST_META_FAIL;
2215 goto error_rcu_unlock;
2216 }
2217
2218 /* For each channel */
2219 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2220 node.node) {
2221 /* Create all streams */
2222 while (1) {
2223 /* Create UST stream */
2224 ustream = zmalloc(sizeof(*ustream));
2225 if (ustream == NULL) {
2226 PERROR("zmalloc ust stream");
2227 goto error_rcu_unlock;
2228 }
2229
2230 /* We are going to receive 2 fds, we need to reserve them. */
2231 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2232 if (ret < 0) {
2233 ERR("Exhausted number of available FD upon stream create");
2234 free(ustream);
2235 goto error_rcu_unlock;
2236 }
2237
2238 health_code_update(&health_thread_cmd);
2239
2240 ret = ustctl_create_stream(app->sock, ua_chan->obj,
2241 &ustream->obj);
2242 if (ret < 0) {
2243 /* Got all streams */
2244 lttng_fd_put(LTTNG_FD_APPS, 2);
2245 free(ustream);
2246 ret = LTTNG_ERR_UST_STREAM_FAIL;
2247 break;
2248 }
2249 ustream->handle = ustream->obj->handle;
2250
2251 health_code_update(&health_thread_cmd);
2252
2253 /* Order is important */
2254 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
2255 ret = snprintf(ustream->name, sizeof(ustream->name), "%s_%u",
2256 ua_chan->name, ua_chan->streams.count);
2257 ua_chan->streams.count++;
2258 if (ret < 0) {
2259 PERROR("asprintf UST create stream");
2260 /*
2261 * XXX what should we do here with the
2262 * stream ?
2263 */
2264 continue;
2265 }
2266 DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count,
2267 ustream->handle);
2268 }
2269
2270 health_code_update(&health_thread_cmd);
2271 }
2272
2273 switch (app->bits_per_long) {
2274 case 64:
2275 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
2276 usess->consumer);
2277 if (socket == NULL) {
2278 goto skip_setup;
2279 }
2280 break;
2281 case 32:
2282 socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd),
2283 usess->consumer);
2284 if (socket == NULL) {
2285 goto skip_setup;
2286 }
2287 break;
2288 default:
2289 ret = -EINVAL;
2290 goto error_rcu_unlock;
2291 }
2292
2293 /* Setup UST consumer socket and send fds to it */
2294 ret = ust_consumer_send_session(ua_sess, usess->consumer, socket);
2295 if (ret < 0) {
2296 goto error_rcu_unlock;
2297 }
2298
2299 health_code_update(&health_thread_cmd);
2300
2301 skip_setup:
2302 /* This start the UST tracing */
2303 ret = ustctl_start_session(app->sock, ua_sess->handle);
2304 if (ret < 0) {
2305 ERR("Error starting tracing for app pid: %d", app->pid);
2306 goto error_rcu_unlock;
2307 }
2308
2309 health_code_update(&health_thread_cmd);
2310
2311 /* Quiescent wait after starting trace */
2312 ustctl_wait_quiescent(app->sock);
2313
2314 end:
2315 rcu_read_unlock();
2316 health_code_update(&health_thread_cmd);
2317 return 0;
2318
2319 error_rcu_unlock:
2320 rcu_read_unlock();
2321 health_code_update(&health_thread_cmd);
2322 return -1;
2323 }
2324
2325 /*
2326 * Stop tracing for a specific UST session and app.
2327 */
2328 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2329 {
2330 int ret = 0;
2331 struct lttng_ht_iter iter;
2332 struct ust_app_session *ua_sess;
2333 struct ust_app_channel *ua_chan;
2334
2335 DBG("Stopping tracing for ust app pid %d", app->pid);
2336
2337 rcu_read_lock();
2338
2339 if (!app->compatible) {
2340 goto end;
2341 }
2342
2343 ua_sess = lookup_session_by_app(usess, app);
2344 if (ua_sess == NULL) {
2345 /* Only malloc can failed so something is really wrong */
2346 goto error_rcu_unlock;
2347 }
2348
2349 /*
2350 * If started = 0, it means that stop trace has been called for a session
2351 * that was never started. This is a code flow error and should never
2352 * happen.
2353 */
2354 assert(ua_sess->started == 1);
2355
2356 health_code_update(&health_thread_cmd);
2357
2358 /* This inhibits UST tracing */
2359 ret = ustctl_stop_session(app->sock, ua_sess->handle);
2360 if (ret < 0) {
2361 ERR("Error stopping tracing for app pid: %d", app->pid);
2362 goto error_rcu_unlock;
2363 }
2364
2365 health_code_update(&health_thread_cmd);
2366
2367 /* Quiescent wait after stopping trace */
2368 ustctl_wait_quiescent(app->sock);
2369
2370 health_code_update(&health_thread_cmd);
2371
2372 /* Flushing buffers */
2373 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2374 node.node) {
2375 health_code_update(&health_thread_cmd);
2376 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
2377 if (ret < 0) {
2378 ERR("UST app PID %d channel %s flush failed with ret %d",
2379 app->pid, ua_chan->name, ret);
2380 /* Continuing flushing all buffers */
2381 continue;
2382 }
2383 }
2384
2385 health_code_update(&health_thread_cmd);
2386
2387 /* Flush all buffers before stopping */
2388 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
2389 if (ret < 0) {
2390 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
2391 ret);
2392 }
2393
2394 end:
2395 rcu_read_unlock();
2396 health_code_update(&health_thread_cmd);
2397 return 0;
2398
2399 error_rcu_unlock:
2400 rcu_read_unlock();
2401 health_code_update(&health_thread_cmd);
2402 return -1;
2403 }
2404
2405 /*
2406 * Destroy a specific UST session in apps.
2407 */
2408 int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
2409 {
2410 struct ust_app_session *ua_sess;
2411 struct lttng_ust_object_data obj;
2412 struct lttng_ht_iter iter;
2413 struct lttng_ht_node_ulong *node;
2414 int ret;
2415
2416 DBG("Destroy tracing for ust app pid %d", app->pid);
2417
2418 rcu_read_lock();
2419
2420 if (!app->compatible) {
2421 goto end;
2422 }
2423
2424 __lookup_session_by_app(usess, app, &iter);
2425 node = lttng_ht_iter_get_node_ulong(&iter);
2426 if (node == NULL) {
2427 /* Only malloc can failed so something is really wrong */
2428 goto error_rcu_unlock;
2429 }
2430 ua_sess = caa_container_of(node, struct ust_app_session, node);
2431 ret = lttng_ht_del(app->sessions, &iter);
2432 assert(!ret);
2433 obj.handle = ua_sess->handle;
2434 obj.shm_fd = -1;
2435 obj.wait_fd = -1;
2436 obj.memory_map_size = 0;
2437 health_code_update(&health_thread_cmd);
2438 ustctl_release_object(app->sock, &obj);
2439
2440 health_code_update(&health_thread_cmd);
2441 delete_ust_app_session(app->sock, ua_sess);
2442
2443 /* Quiescent wait after stopping trace */
2444 ustctl_wait_quiescent(app->sock);
2445
2446 end:
2447 rcu_read_unlock();
2448 health_code_update(&health_thread_cmd);
2449 return 0;
2450
2451 error_rcu_unlock:
2452 rcu_read_unlock();
2453 health_code_update(&health_thread_cmd);
2454 return -1;
2455 }
2456
2457 /*
2458 * Start tracing for the UST session.
2459 */
2460 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2461 {
2462 int ret = 0;
2463 struct lttng_ht_iter iter;
2464 struct ust_app *app;
2465
2466 DBG("Starting all UST traces");
2467
2468 rcu_read_lock();
2469
2470 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2471 ret = ust_app_start_trace(usess, app);
2472 if (ret < 0) {
2473 /* Continue to next apps even on error */
2474 continue;
2475 }
2476 }
2477
2478 rcu_read_unlock();
2479
2480 return 0;
2481 }
2482
2483 /*
2484 * Start tracing for the UST session.
2485 */
2486 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2487 {
2488 int ret = 0;
2489 struct lttng_ht_iter iter;
2490 struct ust_app *app;
2491
2492 DBG("Stopping all UST traces");
2493
2494 rcu_read_lock();
2495
2496 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2497 ret = ust_app_stop_trace(usess, app);
2498 if (ret < 0) {
2499 /* Continue to next apps even on error */
2500 continue;
2501 }
2502 }
2503
2504 rcu_read_unlock();
2505
2506 return 0;
2507 }
2508
2509 /*
2510 * Destroy app UST session.
2511 */
2512 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2513 {
2514 int ret = 0;
2515 struct lttng_ht_iter iter;
2516 struct ust_app *app;
2517
2518 DBG("Destroy all UST traces");
2519
2520 rcu_read_lock();
2521
2522 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2523 ret = ust_app_destroy_trace(usess, app);
2524 if (ret < 0) {
2525 /* Continue to next apps even on error */
2526 continue;
2527 }
2528 }
2529
2530 rcu_read_unlock();
2531
2532 return 0;
2533 }
2534
2535 /*
2536 * Add channels/events from UST global domain to registered apps at sock.
2537 */
2538 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2539 {
2540 int ret = 0;
2541 struct lttng_ht_iter iter, uiter, iter_ctx;
2542 struct ust_app *app;
2543 struct ust_app_session *ua_sess;
2544 struct ust_app_channel *ua_chan;
2545 struct ust_app_event *ua_event;
2546 struct ust_app_ctx *ua_ctx;
2547
2548 if (usess == NULL) {
2549 ERR("No UST session on global update. Returning");
2550 goto error;
2551 }
2552
2553 DBG2("UST app global update for app sock %d for session id %d", sock,
2554 usess->id);
2555
2556 rcu_read_lock();
2557
2558 app = find_app_by_sock(sock);
2559 if (app == NULL) {
2560 ERR("Failed to update app sock %d", sock);
2561 goto error;
2562 }
2563
2564 if (!app->compatible) {
2565 goto error;
2566 }
2567
2568 ua_sess = create_ust_app_session(usess, app);
2569 if (ua_sess == NULL || ua_sess == (void *) -1UL) {
2570 /* Tracer is gone for this session and has been freed */
2571 goto error;
2572 }
2573
2574 /*
2575 * We can iterate safely here over all UST app session sicne the create ust
2576 * app session above made a shadow copy of the UST global domain from the
2577 * ltt ust session.
2578 */
2579 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2580 node.node) {
2581 ret = create_ust_channel(app, ua_sess, ua_chan);
2582 if (ret < 0) {
2583 /* FIXME: Should we quit here or continue... */
2584 continue;
2585 }
2586
2587 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2588 node.node) {
2589 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2590 if (ret < 0) {
2591 /* FIXME: Should we quit here or continue... */
2592 continue;
2593 }
2594 }
2595
2596
2597 /* For each events */
2598 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2599 node.node) {
2600 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2601 if (ret < 0) {
2602 /* FIXME: Should we quit here or continue... */
2603 continue;
2604 }
2605
2606 /* Add context on events. */
2607 cds_lfht_for_each_entry(ua_event->ctx->ht, &iter_ctx.iter,
2608 ua_ctx, node.node) {
2609 ret = create_ust_event_context(ua_event, ua_ctx, app);
2610 if (ret < 0) {
2611 /* FIXME: Should we quit here or continue... */
2612 continue;
2613 }
2614 }
2615 ret = set_ust_event_filter(ua_event, app);
2616 if (ret < 0) {
2617 /* FIXME: Should we quit here or continue... */
2618 continue;
2619 }
2620 }
2621 }
2622
2623 if (usess->start_trace) {
2624 ret = ust_app_start_trace(usess, app);
2625 if (ret < 0) {
2626 goto error;
2627 }
2628
2629 DBG2("UST trace started for app pid %d", app->pid);
2630 }
2631
2632 error:
2633 rcu_read_unlock();
2634 return;
2635 }
2636
2637 /*
2638 * Add context to a specific channel for global UST domain.
2639 */
2640 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2641 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2642 {
2643 int ret = 0;
2644 struct lttng_ht_node_str *ua_chan_node;
2645 struct lttng_ht_iter iter, uiter;
2646 struct ust_app_channel *ua_chan = NULL;
2647 struct ust_app_session *ua_sess;
2648 struct ust_app *app;
2649
2650 rcu_read_lock();
2651
2652 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2653 if (!app->compatible) {
2654 /*
2655 * TODO: In time, we should notice the caller of this error by
2656 * telling him that this is a version error.
2657 */
2658 continue;
2659 }
2660 ua_sess = lookup_session_by_app(usess, app);
2661 if (ua_sess == NULL) {
2662 continue;
2663 }
2664
2665 /* Lookup channel in the ust app session */
2666 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2667 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2668 if (ua_chan_node == NULL) {
2669 continue;
2670 }
2671 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2672 node);
2673
2674 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2675 if (ret < 0) {
2676 continue;
2677 }
2678 }
2679
2680 rcu_read_unlock();
2681 return ret;
2682 }
2683
2684 /*
2685 * Add context to a specific event in a channel for global UST domain.
2686 */
2687 int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2688 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2689 struct ltt_ust_context *uctx)
2690 {
2691 int ret = 0;
2692 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2693 struct lttng_ht_iter iter, uiter;
2694 struct ust_app_session *ua_sess;
2695 struct ust_app_event *ua_event;
2696 struct ust_app_channel *ua_chan = NULL;
2697 struct ust_app *app;
2698
2699 rcu_read_lock();
2700
2701 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2702 if (!app->compatible) {
2703 /*
2704 * TODO: In time, we should notice the caller of this error by
2705 * telling him that this is a version error.
2706 */
2707 continue;
2708 }
2709 ua_sess = lookup_session_by_app(usess, app);
2710 if (ua_sess == NULL) {
2711 continue;
2712 }
2713
2714 /* Lookup channel in the ust app session */
2715 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2716 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2717 if (ua_chan_node == NULL) {
2718 continue;
2719 }
2720 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2721 node);
2722
2723 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2724 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
2725 if (ua_event_node == NULL) {
2726 continue;
2727 }
2728 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2729 node);
2730
2731 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2732 if (ret < 0) {
2733 continue;
2734 }
2735 }
2736
2737 rcu_read_unlock();
2738 return ret;
2739 }
2740
2741 /*
2742 * Add context to a specific event in a channel for global UST domain.
2743 */
2744 int ust_app_set_filter_event_glb(struct ltt_ust_session *usess,
2745 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2746 struct lttng_filter_bytecode *bytecode)
2747 {
2748 int ret = 0;
2749 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2750 struct lttng_ht_iter iter, uiter;
2751 struct ust_app_session *ua_sess;
2752 struct ust_app_event *ua_event;
2753 struct ust_app_channel *ua_chan = NULL;
2754 struct ust_app *app;
2755
2756 rcu_read_lock();
2757
2758 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2759 if (!app->compatible) {
2760 /*
2761 * TODO: In time, we should notice the caller of this error by
2762 * telling him that this is a version error.
2763 */
2764 continue;
2765 }
2766 ua_sess = lookup_session_by_app(usess, app);
2767 if (ua_sess == NULL) {
2768 continue;
2769 }
2770
2771 /* Lookup channel in the ust app session */
2772 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2773 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2774 if (ua_chan_node == NULL) {
2775 continue;
2776 }
2777 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2778 node);
2779
2780 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2781 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
2782 if (ua_event_node == NULL) {
2783 continue;
2784 }
2785 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2786 node);
2787
2788 ret = set_ust_app_event_filter(ua_sess, ua_event, bytecode, app);
2789 if (ret < 0) {
2790 continue;
2791 }
2792 }
2793
2794 rcu_read_unlock();
2795 return ret;
2796 }
2797
2798 /*
2799 * Enable event for a channel from a UST session for a specific PID.
2800 */
2801 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2802 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2803 {
2804 int ret = 0;
2805 struct lttng_ht_iter iter;
2806 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2807 struct ust_app *app;
2808 struct ust_app_session *ua_sess;
2809 struct ust_app_channel *ua_chan;
2810 struct ust_app_event *ua_event;
2811
2812 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2813
2814 rcu_read_lock();
2815
2816 app = ust_app_find_by_pid(pid);
2817 if (app == NULL) {
2818 ERR("UST app enable event per PID %d not found", pid);
2819 ret = -1;
2820 goto error;
2821 }
2822
2823 if (!app->compatible) {
2824 ret = 0;
2825 goto error;
2826 }
2827
2828 ua_sess = lookup_session_by_app(usess, app);
2829 /* If ua_sess is NULL, there is a code flow error */
2830 assert(ua_sess);
2831
2832 /* Lookup channel in the ust app session */
2833 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2834 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2835 /* If the channel is not found, there is a code flow error */
2836 assert(ua_chan_node);
2837
2838 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2839
2840 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2841 ua_event_node = lttng_ht_iter_get_node_str(&iter);
2842 if (ua_event_node == NULL) {
2843 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2844 if (ret < 0) {
2845 goto error;
2846 }
2847 } else {
2848 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2849
2850 ret = enable_ust_app_event(ua_sess, ua_event, app);
2851 if (ret < 0) {
2852 goto error;
2853 }
2854 }
2855
2856 error:
2857 rcu_read_unlock();
2858 return ret;
2859 }
2860
2861 /*
2862 * Disable event for a channel from a UST session for a specific PID.
2863 */
2864 int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2865 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2866 {
2867 int ret = 0;
2868 struct lttng_ht_iter iter;
2869 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2870 struct ust_app *app;
2871 struct ust_app_session *ua_sess;
2872 struct ust_app_channel *ua_chan;
2873 struct ust_app_event *ua_event;
2874
2875 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2876
2877 rcu_read_lock();
2878
2879 app = ust_app_find_by_pid(pid);
2880 if (app == NULL) {
2881 ERR("UST app disable event per PID %d not found", pid);
2882 ret = -1;
2883 goto error;
2884 }
2885
2886 if (!app->compatible) {
2887 ret = 0;
2888 goto error;
2889 }
2890
2891 ua_sess = lookup_session_by_app(usess, app);
2892 /* If ua_sess is NULL, there is a code flow error */
2893 assert(ua_sess);
2894
2895 /* Lookup channel in the ust app session */
2896 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2897 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2898 if (ua_chan_node == NULL) {
2899 /* Channel does not exist, skip disabling */
2900 goto error;
2901 }
2902 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2903
2904 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2905 ua_event_node = lttng_ht_iter_get_node_str(&iter);
2906 if (ua_event_node == NULL) {
2907 /* Event does not exist, skip disabling */
2908 goto error;
2909 }
2910 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2911
2912 ret = disable_ust_app_event(ua_sess, ua_event, app);
2913 if (ret < 0) {
2914 goto error;
2915 }
2916
2917 error:
2918 rcu_read_unlock();
2919 return ret;
2920 }
2921
2922 /*
2923 * Validate version of UST apps and set the compatible bit.
2924 */
2925 int ust_app_validate_version(int sock)
2926 {
2927 int ret;
2928 struct ust_app *app;
2929
2930 rcu_read_lock();
2931
2932 app = find_app_by_sock(sock);
2933 assert(app);
2934
2935 health_code_update(&health_thread_cmd);
2936
2937 ret = ustctl_tracer_version(sock, &app->version);
2938 if (ret < 0) {
2939 goto error;
2940 }
2941
2942 /* Validate version */
2943 if (app->version.major != UST_APP_MAJOR_VERSION) {
2944 goto error;
2945 }
2946
2947 DBG2("UST app PID %d is compatible with internal major version %d "
2948 "(supporting == %d)", app->pid, app->version.major,
2949 UST_APP_MAJOR_VERSION);
2950 app->compatible = 1;
2951 rcu_read_unlock();
2952 health_code_update(&health_thread_cmd);
2953 return 0;
2954
2955 error:
2956 DBG2("UST app PID %d is not compatible with internal major version %d "
2957 "(supporting == %d)", app->pid, app->version.major,
2958 UST_APP_MAJOR_VERSION);
2959 app->compatible = 0;
2960 rcu_read_unlock();
2961 health_code_update(&health_thread_cmd);
2962 return -1;
2963 }
2964
2965 /*
2966 * Calibrate registered applications.
2967 */
2968 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
2969 {
2970 int ret = 0;
2971 struct lttng_ht_iter iter;
2972 struct ust_app *app;
2973
2974 rcu_read_lock();
2975
2976 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2977 if (!app->compatible) {
2978 /*
2979 * TODO: In time, we should notice the caller of this error by
2980 * telling him that this is a version error.
2981 */
2982 continue;
2983 }
2984
2985 health_code_update(&health_thread_cmd);
2986
2987 ret = ustctl_calibrate(app->sock, calibrate);
2988 if (ret < 0) {
2989 switch (ret) {
2990 case -ENOSYS:
2991 /* Means that it's not implemented on the tracer side. */
2992 ret = 0;
2993 break;
2994 default:
2995 /* TODO: Report error to user */
2996 DBG2("Calibrate app PID %d returned with error %d",
2997 app->pid, ret);
2998 break;
2999 }
3000 }
3001 }
3002
3003 DBG("UST app global domain calibration finished");
3004
3005 rcu_read_unlock();
3006
3007 health_code_update(&health_thread_cmd);
3008
3009 return ret;
3010 }
This page took 0.164588 seconds and 4 git commands to generate.