Update comments regarding the power of 2 constraint on sub-buffer sizes
[lttng-tools.git] / src / bin / lttng-sessiond / save.c
1 /*
2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include <urcu/uatomic.h>
24 #include <unistd.h>
25
26 #include <common/defaults.h>
27 #include <common/error.h>
28 #include <common/config/config.h>
29 #include <common/utils.h>
30 #include <common/runas.h>
31 #include <lttng/save-internal.h>
32
33 #include "save.h"
34 #include "session.h"
35 #include "syscall.h"
36 #include "trace-ust.h"
37
38 static
39 int save_kernel_channel_attributes(struct config_writer *writer,
40 struct lttng_channel_attr *attr)
41 {
42 int ret;
43
44 ret = config_writer_write_element_string(writer,
45 config_element_overwrite_mode,
46 attr->overwrite ? config_overwrite_mode_overwrite :
47 config_overwrite_mode_discard);
48 if (ret) {
49 goto end;
50 }
51
52 ret = config_writer_write_element_unsigned_int(writer,
53 config_element_subbuf_size, attr->subbuf_size);
54 if (ret) {
55 goto end;
56 }
57
58 ret = config_writer_write_element_unsigned_int(writer,
59 config_element_num_subbuf,
60 attr->num_subbuf);
61 if (ret) {
62 goto end;
63 }
64
65 ret = config_writer_write_element_unsigned_int(writer,
66 config_element_switch_timer_interval,
67 attr->switch_timer_interval);
68 if (ret) {
69 goto end;
70 }
71
72 ret = config_writer_write_element_unsigned_int(writer,
73 config_element_read_timer_interval,
74 attr->read_timer_interval);
75 if (ret) {
76 goto end;
77 }
78
79 ret = config_writer_write_element_string(writer,
80 config_element_output_type,
81 attr->output == LTTNG_EVENT_SPLICE ?
82 config_output_type_splice : config_output_type_mmap);
83 if (ret) {
84 goto end;
85 }
86
87 ret = config_writer_write_element_unsigned_int(writer,
88 config_element_tracefile_size, attr->tracefile_size);
89 if (ret) {
90 goto end;
91 }
92
93 ret = config_writer_write_element_unsigned_int(writer,
94 config_element_tracefile_count,
95 attr->tracefile_count);
96 if (ret) {
97 goto end;
98 }
99
100 ret = config_writer_write_element_unsigned_int(writer,
101 config_element_live_timer_interval,
102 attr->live_timer_interval);
103 if (ret) {
104 goto end;
105 }
106 end:
107 return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0;
108 }
109
110 static
111 int save_ust_channel_attributes(struct config_writer *writer,
112 struct lttng_ust_channel_attr *attr)
113 {
114 int ret;
115
116 ret = config_writer_write_element_string(writer,
117 config_element_overwrite_mode,
118 attr->overwrite ? config_overwrite_mode_overwrite :
119 config_overwrite_mode_discard);
120 if (ret) {
121 goto end;
122 }
123
124 ret = config_writer_write_element_unsigned_int(writer,
125 config_element_subbuf_size, attr->subbuf_size);
126 if (ret) {
127 goto end;
128 }
129
130 ret = config_writer_write_element_unsigned_int(writer,
131 config_element_num_subbuf,
132 attr->num_subbuf);
133 if (ret) {
134 goto end;
135 }
136
137 ret = config_writer_write_element_unsigned_int(writer,
138 config_element_switch_timer_interval,
139 attr->switch_timer_interval);
140 if (ret) {
141 goto end;
142 }
143
144 ret = config_writer_write_element_unsigned_int(writer,
145 config_element_read_timer_interval,
146 attr->read_timer_interval);
147 if (ret) {
148 goto end;
149 }
150
151 ret = config_writer_write_element_string(writer,
152 config_element_output_type,
153 attr->output == LTTNG_UST_MMAP ?
154 config_output_type_mmap : config_output_type_splice);
155 if (ret) {
156 goto end;
157 }
158 end:
159 return ret ? LTTNG_ERR_SAVE_IO_FAIL : 0;
160 }
161
162 static
163 const char *get_kernel_instrumentation_string(
164 enum lttng_kernel_instrumentation instrumentation)
165 {
166 const char *instrumentation_string;
167
168 switch (instrumentation) {
169 case LTTNG_KERNEL_ALL:
170 instrumentation_string = config_event_type_all;
171 break;
172 case LTTNG_KERNEL_TRACEPOINT:
173 instrumentation_string = config_event_type_tracepoint;
174 break;
175 case LTTNG_KERNEL_KPROBE:
176 instrumentation_string = config_event_type_kprobe;
177 break;
178 case LTTNG_KERNEL_FUNCTION:
179 instrumentation_string = config_event_type_function;
180 break;
181 case LTTNG_KERNEL_KRETPROBE:
182 instrumentation_string = config_event_type_kretprobe;
183 break;
184 case LTTNG_KERNEL_NOOP:
185 instrumentation_string = config_event_type_noop;
186 break;
187 case LTTNG_KERNEL_SYSCALL:
188 instrumentation_string = config_event_type_syscall;
189 break;
190 default:
191 instrumentation_string = NULL;
192 }
193
194 return instrumentation_string;
195 }
196
197 static
198 const char *get_kernel_context_type_string(
199 enum lttng_kernel_context_type context_type)
200 {
201 const char *context_type_string;
202
203 switch (context_type) {
204 case LTTNG_KERNEL_CONTEXT_PID:
205 context_type_string = config_event_context_pid;
206 break;
207 case LTTNG_KERNEL_CONTEXT_PROCNAME:
208 context_type_string = config_event_context_procname;
209 break;
210 case LTTNG_KERNEL_CONTEXT_PRIO:
211 context_type_string = config_event_context_prio;
212 break;
213 case LTTNG_KERNEL_CONTEXT_NICE:
214 context_type_string = config_event_context_nice;
215 break;
216 case LTTNG_KERNEL_CONTEXT_VPID:
217 context_type_string = config_event_context_vpid;
218 break;
219 case LTTNG_KERNEL_CONTEXT_TID:
220 context_type_string = config_event_context_tid;
221 break;
222 case LTTNG_KERNEL_CONTEXT_VTID:
223 context_type_string = config_event_context_vtid;
224 break;
225 case LTTNG_KERNEL_CONTEXT_PPID:
226 context_type_string = config_event_context_ppid;
227 break;
228 case LTTNG_KERNEL_CONTEXT_VPPID:
229 context_type_string = config_event_context_vppid;
230 break;
231 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
232 context_type_string = config_event_context_hostname;
233 break;
234 default:
235 context_type_string = NULL;
236 }
237
238 return context_type_string;
239 }
240
241 static
242 const char *get_ust_context_type_string(
243 enum lttng_ust_context_type context_type)
244 {
245 const char *context_type_string;
246
247 switch (context_type) {
248 case LTTNG_UST_CONTEXT_PROCNAME:
249 context_type_string = config_event_context_procname;
250 break;
251 case LTTNG_UST_CONTEXT_VPID:
252 context_type_string = config_event_context_vpid;
253 break;
254 case LTTNG_UST_CONTEXT_VTID:
255 context_type_string = config_event_context_vtid;
256 break;
257 case LTTNG_UST_CONTEXT_IP:
258 context_type_string = config_event_context_ip;
259 break;
260 case LTTNG_UST_CONTEXT_PTHREAD_ID:
261 context_type_string = config_event_context_pthread_id;
262 break;
263 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
264 /*
265 * Error, should not be stored in the XML, perf contexts
266 * are stored as a node of type event_perf_context_type.
267 */
268 default:
269 context_type_string = NULL;
270 break;
271 }
272
273 return context_type_string;
274 }
275
276 static
277 const char *get_buffer_type_string(
278 enum lttng_buffer_type buffer_type)
279 {
280 const char *buffer_type_string;
281
282 switch (buffer_type) {
283 case LTTNG_BUFFER_PER_PID:
284 buffer_type_string = config_buffer_type_per_pid;
285 break;
286 case LTTNG_BUFFER_PER_UID:
287 buffer_type_string = config_buffer_type_per_uid;
288 break;
289 case LTTNG_BUFFER_GLOBAL:
290 buffer_type_string = config_buffer_type_global;
291 break;
292 default:
293 buffer_type_string = NULL;
294 }
295
296 return buffer_type_string;
297 }
298
299 static
300 const char *get_loglevel_type_string(
301 enum lttng_ust_loglevel_type loglevel_type)
302 {
303 const char *loglevel_type_string;
304
305 switch (loglevel_type) {
306 case LTTNG_UST_LOGLEVEL_ALL:
307 loglevel_type_string = config_loglevel_type_all;
308 break;
309 case LTTNG_UST_LOGLEVEL_RANGE:
310 loglevel_type_string = config_loglevel_type_range;
311 break;
312 case LTTNG_UST_LOGLEVEL_SINGLE:
313 loglevel_type_string = config_loglevel_type_single;
314 break;
315 default:
316 loglevel_type_string = NULL;
317 }
318
319 return loglevel_type_string;
320 }
321
322 static
323 int save_kernel_event(struct config_writer *writer,
324 struct ltt_kernel_event *event)
325 {
326 int ret;
327 const char *instrumentation_type;
328
329 ret = config_writer_open_element(writer, config_element_event);
330 if (ret) {
331 ret = LTTNG_ERR_SAVE_IO_FAIL;
332 goto end;
333 }
334
335 if (event->event->name[0]) {
336 ret = config_writer_write_element_string(writer,
337 config_element_name, event->event->name);
338 if (ret) {
339 ret = LTTNG_ERR_SAVE_IO_FAIL;
340 goto end;
341 }
342 }
343
344 ret = config_writer_write_element_bool(writer, config_element_enabled,
345 event->enabled);
346 if (ret) {
347 ret = LTTNG_ERR_SAVE_IO_FAIL;
348 goto end;
349 }
350
351 instrumentation_type = get_kernel_instrumentation_string(
352 event->event->instrumentation);
353 if (!instrumentation_type) {
354 ret = LTTNG_ERR_INVALID;
355 goto end;
356 }
357
358 ret = config_writer_write_element_string(writer, config_element_type,
359 instrumentation_type);
360 if (ret) {
361 ret = LTTNG_ERR_SAVE_IO_FAIL;
362 goto end;
363 }
364
365 if (event->event->instrumentation == LTTNG_KERNEL_FUNCTION ||
366 event->event->instrumentation == LTTNG_KERNEL_KPROBE ||
367 event->event->instrumentation == LTTNG_KERNEL_KRETPROBE) {
368
369 ret = config_writer_open_element(writer,
370 config_element_attributes);
371 if (ret) {
372 ret = LTTNG_ERR_SAVE_IO_FAIL;
373 goto end;
374 }
375
376 switch (event->event->instrumentation) {
377 case LTTNG_KERNEL_SYSCALL:
378 case LTTNG_KERNEL_FUNCTION:
379 ret = config_writer_open_element(writer,
380 config_element_function_attributes);
381 if (ret) {
382 ret = LTTNG_ERR_SAVE_IO_FAIL;
383 goto end;
384 }
385
386 ret = config_writer_write_element_string(writer,
387 config_element_name,
388 event->event->u.ftrace.symbol_name);
389 if (ret) {
390 ret = LTTNG_ERR_SAVE_IO_FAIL;
391 goto end;
392 }
393
394 /* /function attributes */
395 ret = config_writer_close_element(writer);
396 if (ret) {
397 ret = LTTNG_ERR_SAVE_IO_FAIL;
398 goto end;
399 }
400 break;
401 case LTTNG_KERNEL_KPROBE:
402 case LTTNG_KERNEL_KRETPROBE:
403 {
404 const char *symbol_name;
405 uint64_t addr;
406 uint64_t offset;
407
408 if (event->event->instrumentation ==
409 LTTNG_KERNEL_KPROBE) {
410 /*
411 * Comments in lttng-kernel.h mention that
412 * either addr or symbol_name are set, not both.
413 */
414 addr = event->event->u.kprobe.addr;
415 offset = event->event->u.kprobe.offset;
416 symbol_name = addr ? NULL :
417 event->event->u.kprobe.symbol_name;
418 } else {
419 symbol_name =
420 event->event->u.kretprobe.symbol_name;
421 addr = event->event->u.kretprobe.addr;
422 offset = event->event->u.kretprobe.offset;
423 }
424
425 ret = config_writer_open_element(writer,
426 config_element_probe_attributes);
427 if (ret) {
428 ret = LTTNG_ERR_SAVE_IO_FAIL;
429 goto end;
430 }
431
432 if (symbol_name) {
433 ret = config_writer_write_element_string(writer,
434 config_element_symbol_name,
435 symbol_name);
436 if (ret) {
437 ret = LTTNG_ERR_SAVE_IO_FAIL;
438 goto end;
439 }
440 }
441
442 if (addr) {
443 ret = config_writer_write_element_unsigned_int(
444 writer, config_element_address, addr);
445 if (ret) {
446 ret = LTTNG_ERR_SAVE_IO_FAIL;
447 goto end;
448 }
449 }
450
451 if (offset) {
452 ret = config_writer_write_element_unsigned_int(
453 writer, config_element_offset, offset);
454 if (ret) {
455 ret = LTTNG_ERR_SAVE_IO_FAIL;
456 goto end;
457 }
458 }
459
460 ret = config_writer_close_element(writer);
461 if (ret) {
462 ret = LTTNG_ERR_SAVE_IO_FAIL;
463 goto end;
464 }
465 break;
466 }
467 default:
468 ERR("Unsupported kernel instrumentation type.");
469 ret = LTTNG_ERR_INVALID;
470 goto end;
471 }
472
473 /* /attributes */
474 ret = config_writer_close_element(writer);
475 if (ret) {
476 ret = LTTNG_ERR_SAVE_IO_FAIL;
477 goto end;
478 }
479 }
480
481 /* /event */
482 ret = config_writer_close_element(writer);
483 if (ret) {
484 ret = LTTNG_ERR_SAVE_IO_FAIL;
485 goto end;
486 }
487 end:
488 return ret;
489 }
490
491 static
492 int save_kernel_syscall(struct config_writer *writer,
493 struct ltt_kernel_channel *kchan)
494 {
495 int ret, i;
496 ssize_t count;
497 struct lttng_event *events = NULL;
498
499 assert(writer);
500 assert(kchan);
501
502 count = syscall_list_channel(kchan, &events, 0);
503 if (!count) {
504 /* No syscalls, just gracefully return. */
505 ret = 0;
506 goto end;
507 }
508
509 for (i = 0; i < count; i++) {
510 struct ltt_kernel_event *kevent;
511
512 /* Create a temporary kevent in order to save it. */
513 /*
514 * TODO: struct lttng_event does not really work for a filter,
515 * but unfortunately, it is exposed as external API (and used as
516 * internal representation. Using NULL meanwhile.
517 */
518 kevent = trace_kernel_create_event(&events[i],
519 NULL, NULL);
520 if (!kevent) {
521 ret = -ENOMEM;
522 goto end;
523 }
524 /* Init list in order so the destroy call can del the node. */
525 CDS_INIT_LIST_HEAD(&kevent->list);
526
527 ret = save_kernel_event(writer, kevent);
528 trace_kernel_destroy_event(kevent);
529 if (ret) {
530 goto end;
531 }
532 }
533
534 /* Everything went well */
535 ret = 0;
536
537 end:
538 free(events);
539 return ret;
540 }
541
542 static
543 int save_kernel_events(struct config_writer *writer,
544 struct ltt_kernel_channel *kchan)
545 {
546 int ret;
547 struct ltt_kernel_event *event;
548
549 ret = config_writer_open_element(writer, config_element_events);
550 if (ret) {
551 ret = LTTNG_ERR_SAVE_IO_FAIL;
552 goto end;
553 }
554
555 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
556 ret = save_kernel_event(writer, event);
557 if (ret) {
558 goto end;
559 }
560 }
561
562 /* Save syscalls if any. */
563 ret = save_kernel_syscall(writer, kchan);
564 if (ret) {
565 goto end;
566 }
567
568 /* /events */
569 ret = config_writer_close_element(writer);
570 if (ret) {
571 ret = LTTNG_ERR_SAVE_IO_FAIL;
572 goto end;
573 }
574 end:
575 return ret;
576 }
577
578 static
579 int save_ust_event(struct config_writer *writer,
580 struct ltt_ust_event *event)
581 {
582 int ret;
583 const char *loglevel_type_string;
584
585 ret = config_writer_open_element(writer, config_element_event);
586 if (ret) {
587 ret = LTTNG_ERR_SAVE_IO_FAIL;
588 goto end;
589 }
590
591 if (event->attr.name[0]) {
592 ret = config_writer_write_element_string(writer,
593 config_element_name, event->attr.name);
594 if (ret) {
595 ret = LTTNG_ERR_SAVE_IO_FAIL;
596 goto end;
597 }
598 }
599
600 ret = config_writer_write_element_bool(writer, config_element_enabled,
601 event->enabled);
602 if (ret) {
603 ret = LTTNG_ERR_SAVE_IO_FAIL;
604 goto end;
605 }
606
607 if (event->attr.instrumentation != LTTNG_UST_TRACEPOINT) {
608 ERR("Unsupported UST instrumentation type.");
609 ret = LTTNG_ERR_INVALID;
610 goto end;
611 }
612 ret = config_writer_write_element_string(writer, config_element_type,
613 config_event_type_tracepoint);
614 if (ret) {
615 ret = LTTNG_ERR_SAVE_IO_FAIL;
616 goto end;
617 }
618
619 loglevel_type_string = get_loglevel_type_string(
620 event->attr.loglevel_type);
621 if (!loglevel_type_string) {
622 ERR("Unsupported UST loglevel type.");
623 ret = LTTNG_ERR_INVALID;
624 goto end;
625 }
626
627 ret = config_writer_write_element_string(writer,
628 config_element_loglevel_type, loglevel_type_string);
629 if (ret) {
630 ret = LTTNG_ERR_SAVE_IO_FAIL;
631 goto end;
632 }
633
634 ret = config_writer_write_element_signed_int(writer,
635 config_element_loglevel, event->attr.loglevel);
636 if (ret) {
637 ret = LTTNG_ERR_SAVE_IO_FAIL;
638 goto end;
639 }
640
641 if (event->filter_expression) {
642 ret = config_writer_write_element_string(writer,
643 config_element_filter, event->filter_expression);
644 if (ret) {
645 ret = LTTNG_ERR_SAVE_IO_FAIL;
646 goto end;
647 }
648 }
649
650 if (event->exclusion && event->exclusion->count) {
651 uint32_t i;
652
653 ret = config_writer_open_element(writer,
654 config_element_exclusions);
655 if (ret) {
656 ret = LTTNG_ERR_SAVE_IO_FAIL;
657 goto end;
658 }
659
660 for (i = 0; i < event->exclusion->count; i++) {
661 ret = config_writer_write_element_string(writer,
662 config_element_exclusion,
663 &event->exclusion->names[0][i]);
664 if (ret) {
665 ret = LTTNG_ERR_SAVE_IO_FAIL;
666 goto end;
667 }
668 }
669
670 /* /exclusions */
671 ret = config_writer_close_element(writer);
672 if (ret) {
673 ret = LTTNG_ERR_SAVE_IO_FAIL;
674 goto end;
675 }
676 }
677
678 /* /event */
679 ret = config_writer_close_element(writer);
680 if (ret) {
681 ret = LTTNG_ERR_SAVE_IO_FAIL;
682 goto end;
683 }
684 end:
685 return ret;
686 }
687
688 static
689 int save_ust_events(struct config_writer *writer,
690 struct lttng_ht *events)
691 {
692 int ret;
693 struct ltt_ust_event *event;
694 struct lttng_ht_node_str *node;
695 struct lttng_ht_iter iter;
696
697 ret = config_writer_open_element(writer, config_element_events);
698 if (ret) {
699 ret = LTTNG_ERR_SAVE_IO_FAIL;
700 goto end;
701 }
702
703 rcu_read_lock();
704 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
705 event = caa_container_of(node, struct ltt_ust_event, node);
706
707 ret = save_ust_event(writer, event);
708 if (ret) {
709 rcu_read_unlock();
710 goto end;
711 }
712 }
713 rcu_read_unlock();
714
715 /* /events */
716 ret = config_writer_close_element(writer);
717 if (ret) {
718 ret = LTTNG_ERR_SAVE_IO_FAIL;
719 goto end;
720 }
721 end:
722 return ret;
723 }
724
725 /* TODO: save/restore tracker pid */
726
727 static
728 int save_kernel_context(struct config_writer *writer,
729 struct lttng_kernel_context *ctx)
730 {
731 int ret = 0;
732
733 if (!ctx) {
734 goto end;
735 }
736
737 ret = config_writer_open_element(writer, config_element_context);
738 if (ret) {
739 ret = LTTNG_ERR_SAVE_IO_FAIL;
740 goto end;
741 }
742
743 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
744 ret = config_writer_open_element(writer, config_element_perf);
745 if (ret) {
746 ret = LTTNG_ERR_SAVE_IO_FAIL;
747 goto end;
748 }
749
750 ret = config_writer_write_element_unsigned_int(writer,
751 config_element_type, ctx->u.perf_counter.type);
752 if (ret) {
753 ret = LTTNG_ERR_SAVE_IO_FAIL;
754 goto end;
755 }
756
757 ret = config_writer_write_element_unsigned_int(writer,
758 config_element_config, ctx->u.perf_counter.config);
759 if (ret) {
760 ret = LTTNG_ERR_SAVE_IO_FAIL;
761 goto end;
762 }
763
764 ret = config_writer_write_element_string(writer,
765 config_element_name, ctx->u.perf_counter.name);
766 if (ret) {
767 ret = LTTNG_ERR_SAVE_IO_FAIL;
768 goto end;
769 }
770
771 /* /perf */
772 ret = config_writer_close_element(writer);
773 if (ret) {
774 ret = LTTNG_ERR_SAVE_IO_FAIL;
775 goto end;
776 }
777 } else {
778 const char *context_type_string =
779 get_kernel_context_type_string(ctx->ctx);
780
781 if (!context_type_string) {
782 ERR("Unsupported kernel context type.");
783 ret = LTTNG_ERR_INVALID;
784 goto end;
785 }
786
787 ret = config_writer_write_element_string(writer,
788 config_element_type, context_type_string);
789 if (ret) {
790 ret = LTTNG_ERR_SAVE_IO_FAIL;
791 goto end;
792 }
793 }
794
795 /* /context */
796 ret = config_writer_close_element(writer);
797 if (ret) {
798 ret = LTTNG_ERR_SAVE_IO_FAIL;
799 goto end;
800 }
801
802 end:
803 return ret;
804 }
805
806 static
807 int save_kernel_contexts(struct config_writer *writer,
808 struct ltt_kernel_channel *kchan)
809 {
810 int ret;
811 struct ltt_kernel_context *ctx;
812
813 if (cds_list_empty(&kchan->ctx_list)) {
814 ret = 0;
815 goto end;
816 }
817
818 ret = config_writer_open_element(writer, config_element_contexts);
819 if (ret) {
820 ret = LTTNG_ERR_SAVE_IO_FAIL;
821 goto end;
822 }
823
824 cds_list_for_each_entry(ctx, &kchan->ctx_list, list) {
825 ret = save_kernel_context(writer, &ctx->ctx);
826 if (ret) {
827 goto end;
828 }
829 }
830
831 /* /contexts */
832 ret = config_writer_close_element(writer);
833 if (ret) {
834 ret = LTTNG_ERR_SAVE_IO_FAIL;
835 goto end;
836 }
837 end:
838 return ret;
839 }
840
841 static
842 int save_ust_context(struct config_writer *writer,
843 struct cds_list_head *ctx_list)
844 {
845 int ret;
846 struct ltt_ust_context *ctx;
847
848 assert(writer);
849 assert(ctx_list);
850
851 ret = config_writer_open_element(writer, config_element_contexts);
852 if (ret) {
853 ret = LTTNG_ERR_SAVE_IO_FAIL;
854 goto end;
855 }
856
857 cds_list_for_each_entry(ctx, ctx_list, list) {
858 const char *context_type_string;
859
860
861 ret = config_writer_open_element(writer,
862 config_element_context);
863 if (ret) {
864 ret = LTTNG_ERR_SAVE_IO_FAIL;
865 goto end;
866 }
867
868 if (ctx->ctx.ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) {
869 /* Perf contexts are saved as event_perf_context_type */
870 ret = config_writer_open_element(writer,
871 config_element_perf);
872 if (ret) {
873 ret = LTTNG_ERR_SAVE_IO_FAIL;
874 goto end;
875 }
876
877 ret = config_writer_write_element_unsigned_int(writer,
878 config_element_type,
879 ctx->ctx.u.perf_counter.type);
880 if (ret) {
881 ret = LTTNG_ERR_SAVE_IO_FAIL;
882 goto end;
883 }
884
885 ret = config_writer_write_element_unsigned_int(writer,
886 config_element_config,
887 ctx->ctx.u.perf_counter.config);
888 if (ret) {
889 ret = LTTNG_ERR_SAVE_IO_FAIL;
890 goto end;
891 }
892
893 ret = config_writer_write_element_string(writer,
894 config_element_name,
895 ctx->ctx.u.perf_counter.name);
896 if (ret) {
897 ret = LTTNG_ERR_SAVE_IO_FAIL;
898 goto end;
899 }
900
901 /* /perf */
902 ret = config_writer_close_element(writer);
903 if (ret) {
904 ret = LTTNG_ERR_SAVE_IO_FAIL;
905 goto end;
906 }
907 } else {
908 /* Save context as event_context_type_type */
909 context_type_string = get_ust_context_type_string(
910 ctx->ctx.ctx);
911 if (!context_type_string) {
912 ERR("Unsupported UST context type.")
913 ret = LTTNG_ERR_INVALID;
914 goto end;
915 }
916
917 ret = config_writer_write_element_string(writer,
918 config_element_type, context_type_string);
919 if (ret) {
920 ret = LTTNG_ERR_SAVE_IO_FAIL;
921 goto end;
922 }
923 }
924
925 /* /context */
926 ret = config_writer_close_element(writer);
927 if (ret) {
928 ret = LTTNG_ERR_SAVE_IO_FAIL;
929 goto end;
930 }
931 }
932
933 /* /contexts */
934 ret = config_writer_close_element(writer);
935 if (ret) {
936 ret = LTTNG_ERR_SAVE_IO_FAIL;
937 goto end;
938 }
939 end:
940 return ret;
941 }
942
943 static
944 int save_kernel_channel(struct config_writer *writer,
945 struct ltt_kernel_channel *kchan)
946 {
947 int ret;
948
949 assert(writer);
950 assert(kchan);
951
952 ret = config_writer_open_element(writer, config_element_channel);
953 if (ret) {
954 ret = LTTNG_ERR_SAVE_IO_FAIL;
955 goto end;
956 }
957
958 ret = config_writer_write_element_string(writer, config_element_name,
959 kchan->channel->name);
960 if (ret) {
961 ret = LTTNG_ERR_SAVE_IO_FAIL;
962 goto end;
963 }
964
965 ret = config_writer_write_element_bool(writer, config_element_enabled,
966 kchan->channel->enabled);
967 if (ret) {
968 ret = LTTNG_ERR_SAVE_IO_FAIL;
969 goto end;
970 }
971
972 ret = save_kernel_channel_attributes(writer, &kchan->channel->attr);
973 if (ret) {
974 goto end;
975 }
976
977 ret = save_kernel_events(writer, kchan);
978 if (ret) {
979 goto end;
980 }
981
982 ret = save_kernel_contexts(writer, kchan);
983 if (ret) {
984 goto end;
985 }
986
987 /* /channel */
988 ret = config_writer_close_element(writer);
989 if (ret) {
990 ret = LTTNG_ERR_SAVE_IO_FAIL;
991 goto end;
992 }
993 end:
994 return ret;
995 }
996
997 static
998 int save_ust_channel(struct config_writer *writer,
999 struct ltt_ust_channel *ust_chan,
1000 struct ltt_ust_session *session)
1001 {
1002 int ret;
1003
1004 assert(writer);
1005 assert(ust_chan);
1006 assert(session);
1007
1008 ret = config_writer_open_element(writer, config_element_channel);
1009 if (ret) {
1010 ret = LTTNG_ERR_SAVE_IO_FAIL;
1011 goto end;
1012 }
1013
1014 ret = config_writer_write_element_string(writer, config_element_name,
1015 ust_chan->name);
1016 if (ret) {
1017 ret = LTTNG_ERR_SAVE_IO_FAIL;
1018 goto end;
1019 }
1020
1021 ret = config_writer_write_element_bool(writer, config_element_enabled,
1022 ust_chan->enabled);
1023 if (ret) {
1024 ret = LTTNG_ERR_SAVE_IO_FAIL;
1025 goto end;
1026 }
1027
1028 ret = save_ust_channel_attributes(writer, &ust_chan->attr);
1029 if (ret) {
1030 goto end;
1031 }
1032
1033 ret = config_writer_write_element_unsigned_int(writer,
1034 config_element_tracefile_size, ust_chan->tracefile_size);
1035 if (ret) {
1036 ret = LTTNG_ERR_SAVE_IO_FAIL;
1037 goto end;
1038 }
1039
1040 ret = config_writer_write_element_unsigned_int(writer,
1041 config_element_tracefile_count, ust_chan->tracefile_count);
1042 if (ret) {
1043 ret = LTTNG_ERR_SAVE_IO_FAIL;
1044 goto end;
1045 }
1046
1047 ret = config_writer_write_element_unsigned_int(writer,
1048 config_element_live_timer_interval,
1049 session->live_timer_interval);
1050 if (ret) {
1051 ret = LTTNG_ERR_SAVE_IO_FAIL;
1052 goto end;
1053 }
1054
1055 ret = save_ust_events(writer, ust_chan->events);
1056 if (ret) {
1057 ret = LTTNG_ERR_SAVE_IO_FAIL;
1058 goto end;
1059 }
1060
1061 ret = save_ust_context(writer, &ust_chan->ctx_list);
1062 if (ret) {
1063 goto end;
1064 }
1065
1066 /* /channel */
1067 ret = config_writer_close_element(writer);
1068 if (ret) {
1069 ret = LTTNG_ERR_SAVE_IO_FAIL;
1070 goto end;
1071 }
1072 end:
1073 return ret;
1074 }
1075
1076 static
1077 int save_kernel_session(struct config_writer *writer,
1078 struct ltt_session *session)
1079 {
1080 int ret;
1081 struct ltt_kernel_channel *kchan;
1082
1083 assert(writer);
1084 assert(session);
1085
1086 ret = config_writer_write_element_string(writer, config_element_type,
1087 config_domain_type_kernel);
1088 if (ret) {
1089 ret = LTTNG_ERR_SAVE_IO_FAIL;
1090 goto end;
1091 }
1092
1093 ret = config_writer_write_element_string(writer,
1094 config_element_buffer_type, config_buffer_type_global);
1095 if (ret) {
1096 ret = LTTNG_ERR_SAVE_IO_FAIL;
1097 goto end;
1098 }
1099
1100 ret = config_writer_open_element(writer,
1101 config_element_channels);
1102 if (ret) {
1103 ret = LTTNG_ERR_SAVE_IO_FAIL;
1104 goto end;
1105 }
1106
1107 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head,
1108 list) {
1109 ret = save_kernel_channel(writer, kchan);
1110 if (ret) {
1111 goto end;
1112 }
1113 }
1114
1115 /* /channels */
1116 ret = config_writer_close_element(writer);
1117 if (ret) {
1118 ret = LTTNG_ERR_SAVE_IO_FAIL;
1119 goto end;
1120 }
1121 end:
1122 return ret;
1123 }
1124
1125 static
1126 int save_ust_session(struct config_writer *writer,
1127 struct ltt_session *session, int save_agent)
1128 {
1129 int ret;
1130 struct ltt_ust_channel *ust_chan;
1131 const char *buffer_type_string;
1132 struct lttng_ht_node_str *node;
1133 struct lttng_ht_iter iter;
1134
1135 assert(writer);
1136 assert(session);
1137
1138 ret = config_writer_write_element_string(writer, config_element_type,
1139 save_agent ? config_domain_type_jul : config_domain_type_ust);
1140 if (ret) {
1141 ret = LTTNG_ERR_SAVE_IO_FAIL;
1142 goto end;
1143 }
1144
1145 buffer_type_string = get_buffer_type_string(
1146 session->ust_session->buffer_type);
1147 if (!buffer_type_string) {
1148 ERR("Unsupported buffer type.");
1149 ret = LTTNG_ERR_INVALID;
1150 goto end;
1151 }
1152
1153 ret = config_writer_write_element_string(writer,
1154 config_element_buffer_type, buffer_type_string);
1155 if (ret) {
1156 ret = LTTNG_ERR_SAVE_IO_FAIL;
1157 goto end;
1158 }
1159
1160 ret = config_writer_open_element(writer, config_element_channels);
1161 if (ret) {
1162 ret = LTTNG_ERR_SAVE_IO_FAIL;
1163 goto end;
1164 }
1165
1166 rcu_read_lock();
1167 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
1168 &iter.iter, node, node) {
1169 int agent_channel;
1170
1171 ust_chan = caa_container_of(node, struct ltt_ust_channel, node);
1172 agent_channel = !strcmp(DEFAULT_JUL_CHANNEL_NAME, ust_chan->name) ||
1173 !strcmp(DEFAULT_LOG4J_CHANNEL_NAME, ust_chan->name) ||
1174 !strcmp(DEFAULT_PYTHON_CHANNEL_NAME, ust_chan->name);
1175 if (!(save_agent ^ agent_channel)) {
1176 ret = save_ust_channel(writer, ust_chan, session->ust_session);
1177 if (ret) {
1178 rcu_read_unlock();
1179 goto end;
1180 }
1181 }
1182 }
1183 rcu_read_unlock();
1184
1185 /* /channels */
1186 ret = config_writer_close_element(writer);
1187 if (ret) {
1188 ret = LTTNG_ERR_SAVE_IO_FAIL;
1189 goto end;
1190 }
1191 end:
1192 return ret;
1193 }
1194
1195 static
1196 int save_domains(struct config_writer *writer, struct ltt_session *session)
1197 {
1198 int ret = 0;
1199
1200 assert(writer);
1201 assert(session);
1202
1203 if (!session->kernel_session && !session->ust_session) {
1204 goto end;
1205 }
1206
1207 ret = config_writer_open_element(writer, config_element_domains);
1208 if (ret) {
1209 ret = LTTNG_ERR_SAVE_IO_FAIL;
1210 goto end;
1211 }
1212
1213
1214 if (session->kernel_session) {
1215 ret = config_writer_open_element(writer,
1216 config_element_domain);
1217 if (ret) {
1218 ret = LTTNG_ERR_SAVE_IO_FAIL;
1219 goto end;
1220 }
1221
1222 ret = save_kernel_session(writer, session);
1223 if (ret) {
1224 goto end;
1225 }
1226
1227 /* /domain */
1228 ret = config_writer_close_element(writer);
1229 if (ret) {
1230 ret = LTTNG_ERR_SAVE_IO_FAIL;
1231 goto end;
1232 }
1233 }
1234
1235 if (session->ust_session) {
1236 unsigned long agent_count;
1237
1238 ret = config_writer_open_element(writer,
1239 config_element_domain);
1240 if (ret) {
1241 ret = LTTNG_ERR_SAVE_IO_FAIL;
1242 goto end;
1243 }
1244
1245 ret = save_ust_session(writer, session, 0);
1246 if (ret) {
1247 goto end;
1248 }
1249
1250 /* /domain */
1251 ret = config_writer_close_element(writer);
1252 if (ret) {
1253 ret = LTTNG_ERR_SAVE_IO_FAIL;
1254 goto end;
1255 }
1256
1257 rcu_read_lock();
1258 agent_count =
1259 lttng_ht_get_count(session->ust_session->agents);
1260 rcu_read_unlock();
1261
1262 if (agent_count > 0) {
1263 ret = config_writer_open_element(writer,
1264 config_element_domain);
1265 if (ret) {
1266 ret = LTTNG_ERR_SAVE_IO_FAIL;
1267 goto end;
1268 }
1269
1270 ret = save_ust_session(writer, session, 1);
1271 if (ret) {
1272 goto end;
1273 }
1274
1275 /* /domain */
1276 ret = config_writer_close_element(writer);
1277 if (ret) {
1278 ret = LTTNG_ERR_SAVE_IO_FAIL;
1279 goto end;
1280 }
1281 }
1282 }
1283
1284 if (session->ust_session) {
1285 }
1286
1287 /* /domains */
1288 ret = config_writer_close_element(writer);
1289 if (ret) {
1290 ret = LTTNG_ERR_SAVE_IO_FAIL;
1291 goto end;
1292 }
1293 end:
1294 return ret;
1295 }
1296
1297 static
1298 int save_consumer_output(struct config_writer *writer,
1299 struct consumer_output *output)
1300 {
1301 int ret;
1302
1303 assert(writer);
1304 assert(output);
1305
1306 ret = config_writer_open_element(writer, config_element_consumer_output);
1307 if (ret) {
1308 ret = LTTNG_ERR_SAVE_IO_FAIL;
1309 goto end;
1310 }
1311
1312 ret = config_writer_write_element_bool(writer, config_element_enabled,
1313 output->enabled);
1314 if (ret) {
1315 ret = LTTNG_ERR_SAVE_IO_FAIL;
1316 goto end;
1317 }
1318
1319 ret = config_writer_open_element(writer, config_element_destination);
1320 if (ret) {
1321 ret = LTTNG_ERR_SAVE_IO_FAIL;
1322 goto end;
1323 }
1324
1325 switch (output->type) {
1326 case CONSUMER_DST_LOCAL:
1327 ret = config_writer_write_element_string(writer,
1328 config_element_path, output->dst.trace_path);
1329 if (ret) {
1330 ret = LTTNG_ERR_SAVE_IO_FAIL;
1331 goto end;
1332 }
1333 break;
1334 case CONSUMER_DST_NET:
1335 {
1336 char *uri;
1337
1338 uri = zmalloc(PATH_MAX);
1339 if (!uri) {
1340 ret = LTTNG_ERR_NOMEM;
1341 goto end;
1342 }
1343
1344 ret = config_writer_open_element(writer, config_element_net_output);
1345 if (ret) {
1346 ret = LTTNG_ERR_SAVE_IO_FAIL;
1347 goto end_net_output;
1348 }
1349
1350 if (output->dst.net.control_isset &&
1351 output->dst.net.data_isset) {
1352 ret = uri_to_str_url(&output->dst.net.control, uri, PATH_MAX);
1353 if (ret < 0) {
1354 ret = LTTNG_ERR_INVALID;
1355 goto end_net_output;
1356 }
1357
1358 ret = config_writer_write_element_string(writer,
1359 config_element_control_uri, uri);
1360 if (ret) {
1361 ret = LTTNG_ERR_SAVE_IO_FAIL;
1362 goto end_net_output;
1363 }
1364
1365 ret = uri_to_str_url(&output->dst.net.data, uri, PATH_MAX);
1366 if (ret < 0) {
1367 ret = LTTNG_ERR_INVALID;
1368 goto end_net_output;
1369 }
1370
1371 ret = config_writer_write_element_string(writer,
1372 config_element_data_uri, uri);
1373 if (ret) {
1374 ret = LTTNG_ERR_SAVE_IO_FAIL;
1375 goto end_net_output;
1376 }
1377
1378 end_net_output:
1379 free(uri);
1380 if (ret) {
1381 goto end;
1382 }
1383 } else {
1384 ret = !output->dst.net.control_isset ?
1385 LTTNG_ERR_URL_CTRL_MISS :
1386 LTTNG_ERR_URL_DATA_MISS;
1387 free(uri);
1388 goto end;
1389 }
1390
1391 ret = config_writer_close_element(writer);
1392 if (ret) {
1393 ret = LTTNG_ERR_SAVE_IO_FAIL;
1394 goto end;
1395 }
1396 break;
1397 }
1398 default:
1399 ERR("Unsupported consumer output type.");
1400 ret = LTTNG_ERR_INVALID;
1401 goto end;
1402 }
1403
1404 /* /destination */
1405 ret = config_writer_close_element(writer);
1406 if (ret) {
1407 ret = LTTNG_ERR_SAVE_IO_FAIL;
1408 goto end;
1409 }
1410
1411 /* /consumer_output */
1412 ret = config_writer_close_element(writer);
1413 if (ret) {
1414 ret = LTTNG_ERR_SAVE_IO_FAIL;
1415 goto end;
1416 }
1417 end:
1418 return ret;
1419 }
1420
1421 static
1422 int save_snapshot_outputs(struct config_writer *writer,
1423 struct snapshot *snapshot)
1424 {
1425 int ret;
1426 struct lttng_ht_iter iter;
1427 struct snapshot_output *output;
1428
1429 assert(writer);
1430 assert(snapshot);
1431
1432 ret = config_writer_open_element(writer, config_element_snapshot_outputs);
1433 if (ret) {
1434 ret = LTTNG_ERR_SAVE_IO_FAIL;
1435 goto end;
1436 }
1437
1438 rcu_read_lock();
1439 cds_lfht_for_each_entry(snapshot->output_ht->ht, &iter.iter, output,
1440 node.node) {
1441 ret = config_writer_open_element(writer,
1442 config_element_output);
1443 if (ret) {
1444 ret = LTTNG_ERR_SAVE_IO_FAIL;
1445 goto end_unlock;
1446 }
1447
1448 ret = config_writer_write_element_string(writer,
1449 config_element_name, output->name);
1450 if (ret) {
1451 ret = LTTNG_ERR_SAVE_IO_FAIL;
1452 goto end_unlock;
1453 }
1454
1455 ret = config_writer_write_element_unsigned_int(writer,
1456 config_element_max_size, output->max_size);
1457 if (ret) {
1458 ret = LTTNG_ERR_SAVE_IO_FAIL;
1459 goto end_unlock;
1460 }
1461
1462 ret = save_consumer_output(writer, output->consumer);
1463 if (ret) {
1464 goto end_unlock;
1465 }
1466
1467 /* /output */
1468 ret = config_writer_close_element(writer);
1469 if (ret) {
1470 ret = LTTNG_ERR_SAVE_IO_FAIL;
1471 goto end_unlock;
1472 }
1473 }
1474 rcu_read_unlock();
1475
1476 /* /snapshot_outputs */
1477 ret = config_writer_close_element(writer);
1478 if (ret) {
1479 ret = LTTNG_ERR_SAVE_IO_FAIL;
1480 goto end;
1481 }
1482
1483 end:
1484 return ret;
1485 end_unlock:
1486 rcu_read_unlock();
1487 return ret;
1488 }
1489
1490 static
1491 int save_session_output(struct config_writer *writer,
1492 struct ltt_session *session)
1493 {
1494 int ret;
1495
1496 assert(writer);
1497 assert(session);
1498
1499 if ((session->snapshot_mode && session->snapshot.nb_output == 0) ||
1500 (!session->snapshot_mode && !session->consumer)) {
1501 /* Session is in no output mode */
1502 ret = 0;
1503 goto end;
1504 }
1505
1506 ret = config_writer_open_element(writer, config_element_output);
1507 if (ret) {
1508 ret = LTTNG_ERR_SAVE_IO_FAIL;
1509 goto end;
1510 }
1511
1512 if (session->snapshot_mode) {
1513 ret = save_snapshot_outputs(writer, &session->snapshot);
1514 if (ret) {
1515 goto end;
1516 }
1517 } else {
1518 if (session->consumer) {
1519 ret = save_consumer_output(writer, session->consumer);
1520 if (ret) {
1521 goto end;
1522 }
1523 }
1524 }
1525
1526 /* /output */
1527 ret = config_writer_close_element(writer);
1528 if (ret) {
1529 ret = LTTNG_ERR_SAVE_IO_FAIL;
1530 goto end;
1531 }
1532 end:
1533 return ret;
1534 }
1535
1536 /*
1537 * Save the given session.
1538 *
1539 * Return 0 on success else a LTTNG_ERR* code.
1540 */
1541 static
1542 int save_session(struct ltt_session *session,
1543 struct lttng_save_session_attr *attr, lttng_sock_cred *creds)
1544 {
1545 int ret, fd;
1546 unsigned int file_opened = 0; /* Indicate if the file has been opened */
1547 char config_file_path[PATH_MAX];
1548 size_t len;
1549 struct config_writer *writer = NULL;
1550 size_t session_name_len;
1551 const char *provided_path;
1552
1553 assert(session);
1554 assert(attr);
1555 assert(creds);
1556
1557 session_name_len = strlen(session->name);
1558 memset(config_file_path, 0, sizeof(config_file_path));
1559
1560 if (!session_access_ok(session,
1561 LTTNG_SOCK_GET_UID_CRED(creds),
1562 LTTNG_SOCK_GET_GID_CRED(creds))) {
1563 ret = LTTNG_ERR_EPERM;
1564 goto end;
1565 }
1566
1567 provided_path = lttng_save_session_attr_get_output_url(attr);
1568 if (provided_path) {
1569 DBG3("Save session in provided path %s", provided_path);
1570 len = strlen(provided_path);
1571 if (len >= sizeof(config_file_path)) {
1572 ret = LTTNG_ERR_SET_URL;
1573 goto end;
1574 }
1575 strncpy(config_file_path, provided_path, len);
1576 } else {
1577 ssize_t ret_len;
1578 char *home_dir = utils_get_user_home_dir(
1579 LTTNG_SOCK_GET_UID_CRED(creds));
1580 if (!home_dir) {
1581 ret = LTTNG_ERR_SET_URL;
1582 goto end;
1583 }
1584
1585 ret_len = snprintf(config_file_path, sizeof(config_file_path),
1586 DEFAULT_SESSION_HOME_CONFIGPATH, home_dir);
1587 free(home_dir);
1588 if (ret_len < 0) {
1589 PERROR("snprintf save session");
1590 ret = LTTNG_ERR_SET_URL;
1591 goto end;
1592 }
1593 len = ret_len;
1594 }
1595
1596 /*
1597 * Check the path fits in the config file path dst including the '/'
1598 * followed by trailing .lttng extension and the NULL terminated string.
1599 */
1600 if ((len + session_name_len + 2 +
1601 sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION))
1602 > sizeof(config_file_path)) {
1603 ret = LTTNG_ERR_SET_URL;
1604 goto end;
1605 }
1606
1607 ret = run_as_mkdir_recursive(config_file_path, S_IRWXU | S_IRWXG,
1608 LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds));
1609 if (ret) {
1610 ret = LTTNG_ERR_SET_URL;
1611 goto end;
1612 }
1613
1614 /*
1615 * At this point, we know that everything fits in the buffer. Validation
1616 * was done just above.
1617 */
1618 config_file_path[len++] = '/';
1619 strncpy(config_file_path + len, session->name, session_name_len);
1620 len += session_name_len;
1621 strcpy(config_file_path + len, DEFAULT_SESSION_CONFIG_FILE_EXTENSION);
1622 len += sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION);
1623 config_file_path[len] = '\0';
1624
1625 if (!access(config_file_path, F_OK) && !attr->overwrite) {
1626 /* File exists, notify the user since the overwrite flag is off. */
1627 ret = LTTNG_ERR_SAVE_FILE_EXIST;
1628 goto end;
1629 }
1630
1631 fd = run_as_open(config_file_path, O_CREAT | O_WRONLY | O_TRUNC,
1632 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
1633 LTTNG_SOCK_GET_UID_CRED(creds), LTTNG_SOCK_GET_GID_CRED(creds));
1634 if (fd < 0) {
1635 PERROR("Could not create configuration file");
1636 ret = LTTNG_ERR_SAVE_IO_FAIL;
1637 goto end;
1638 }
1639 file_opened = 1;
1640
1641 writer = config_writer_create(fd, 1);
1642 if (!writer) {
1643 ret = LTTNG_ERR_NOMEM;
1644 goto end;
1645 }
1646
1647 ret = config_writer_open_element(writer, config_element_sessions);
1648 if (ret) {
1649 ret = LTTNG_ERR_SAVE_IO_FAIL;
1650 goto end;
1651 }
1652
1653 ret = config_writer_open_element(writer, config_element_session);
1654 if (ret) {
1655 ret = LTTNG_ERR_SAVE_IO_FAIL;
1656 goto end;
1657 }
1658
1659 ret = config_writer_write_element_string(writer, config_element_name,
1660 session->name);
1661 if (ret) {
1662 ret = LTTNG_ERR_SAVE_IO_FAIL;
1663 goto end;
1664 }
1665
1666 if(session->shm_path[0] != '\0') {
1667 ret = config_writer_write_element_string(writer,
1668 config_element_shared_memory_path,
1669 session->shm_path);
1670 if (ret) {
1671 ret = LTTNG_ERR_SAVE_IO_FAIL;
1672 goto end;
1673 }
1674 }
1675
1676 ret = save_domains(writer, session);
1677 if (ret) {
1678 goto end;
1679 }
1680
1681 ret = config_writer_write_element_bool(writer, config_element_started,
1682 session->active);
1683 if (ret) {
1684 ret = LTTNG_ERR_SAVE_IO_FAIL;
1685 goto end;
1686 }
1687
1688 if (session->snapshot_mode || session->live_timer) {
1689 ret = config_writer_open_element(writer, config_element_attributes);
1690 if (ret) {
1691 ret = LTTNG_ERR_SAVE_IO_FAIL;
1692 goto end;
1693 }
1694
1695 if (session->snapshot_mode) {
1696 ret = config_writer_write_element_bool(writer,
1697 config_element_snapshot_mode, 1);
1698 if (ret) {
1699 ret = LTTNG_ERR_SAVE_IO_FAIL;
1700 goto end;
1701 }
1702 } else {
1703 ret = config_writer_write_element_unsigned_int(writer,
1704 config_element_live_timer_interval, session->live_timer);
1705 if (ret) {
1706 ret = LTTNG_ERR_SAVE_IO_FAIL;
1707 goto end;
1708 }
1709 }
1710
1711 /* /attributes */
1712 ret = config_writer_close_element(writer);
1713 if (ret) {
1714 ret = LTTNG_ERR_SAVE_IO_FAIL;
1715 goto end;
1716 }
1717 }
1718
1719 ret = save_session_output(writer, session);
1720 if (ret) {
1721 goto end;
1722 }
1723
1724 /* /session */
1725 ret = config_writer_close_element(writer);
1726 if (ret) {
1727 ret = LTTNG_ERR_SAVE_IO_FAIL;
1728 goto end;
1729 }
1730
1731 /* /sessions */
1732 ret = config_writer_close_element(writer);
1733 if (ret) {
1734 ret = LTTNG_ERR_SAVE_IO_FAIL;
1735 goto end;
1736 }
1737 end:
1738 if (writer && config_writer_destroy(writer)) {
1739 /* Preserve the original error code */
1740 ret = ret ? ret : LTTNG_ERR_SAVE_IO_FAIL;
1741 }
1742 if (ret) {
1743 /* Delete file in case of error */
1744 if (file_opened && unlink(config_file_path)) {
1745 PERROR("Unlinking XML session configuration.");
1746 }
1747 }
1748
1749 return ret;
1750 }
1751
1752 int cmd_save_sessions(struct lttng_save_session_attr *attr,
1753 lttng_sock_cred *creds)
1754 {
1755 int ret;
1756 const char *session_name;
1757 struct ltt_session *session;
1758
1759 session_lock_list();
1760
1761 session_name = lttng_save_session_attr_get_session_name(attr);
1762 if (session_name) {
1763 session = session_find_by_name(session_name);
1764 if (!session) {
1765 ret = LTTNG_ERR_SESS_NOT_FOUND;
1766 goto end;
1767 }
1768
1769 session_lock(session);
1770 ret = save_session(session, attr, creds);
1771 session_unlock(session);
1772 if (ret) {
1773 goto end;
1774 }
1775 } else {
1776 struct ltt_session_list *list = session_get_list();
1777
1778 cds_list_for_each_entry(session, &list->head, list) {
1779 session_lock(session);
1780 ret = save_session(session, attr, creds);
1781 session_unlock(session);
1782
1783 /* Don't abort if we don't have the required permissions. */
1784 if (ret && ret != LTTNG_ERR_EPERM) {
1785 goto end;
1786 }
1787 }
1788 }
1789 ret = LTTNG_OK;
1790
1791 end:
1792 session_unlock_list();
1793 return ret;
1794 }
This page took 0.066572 seconds and 4 git commands to generate.