liblttng-ctl: add facilities for lttng_snapshot_output object
[lttng-tools.git] / src / common / snapshot.c
1 /*
2 * Copyright (C) 2020 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <common/buffer-view.h>
9 #include <common/dynamic-buffer.h>
10 #include <common/snapshot.h>
11 #include <lttng/snapshot-internal.h>
12 #include <lttng/snapshot.h>
13
14 #include <assert.h>
15 #include <stdlib.h>
16
17 LTTNG_HIDDEN
18 bool lttng_snapshot_output_validate(const struct lttng_snapshot_output *output)
19 {
20 bool valid = false;
21 size_t len;
22
23 /*
24 * It is mandatory to have a ctrl_url. If there is only one output
25 * URL (in the net://, net6:// or file:// form), it will be in this
26 * field.
27 */
28 len = lttng_strnlen(output->ctrl_url, sizeof(output->ctrl_url));
29 if (len == 0 || len >= sizeof(output->ctrl_url)) {
30 goto end;
31 }
32
33 len = lttng_strnlen(output->data_url, sizeof(output->data_url));
34 if (len >= sizeof(output->data_url)) {
35 goto end;
36 }
37
38 len = lttng_strnlen(output->name, sizeof(output->name));
39 if (len >= sizeof(output->name)) {
40 goto end;
41 }
42
43 valid = true;
44
45 end:
46 return valid;
47 }
48
49 LTTNG_HIDDEN
50 bool lttng_snapshot_output_is_equal(
51 const struct lttng_snapshot_output *a,
52 const struct lttng_snapshot_output *b)
53 {
54 bool equal = false;
55
56 assert(a);
57 assert(b);
58
59 if (a->max_size != b->max_size) {
60 goto end;
61 }
62
63 if (strcmp(a->name, b->name) != 0) {
64 goto end;
65 }
66
67 if (strcmp(a->ctrl_url, b->ctrl_url) != 0) {
68 goto end;
69 }
70
71 if (strcmp(a->data_url, b->data_url) != 0) {
72 goto end;
73 }
74
75 equal = true;
76
77 end:
78 return equal;
79 }
80
81 /*
82 * This is essentially the same as `struct lttng_snapshot_output`, but packed.
83 */
84 struct lttng_snapshot_output_comm {
85 uint32_t id;
86 uint64_t max_size;
87 char name[LTTNG_NAME_MAX];
88 char ctrl_url[PATH_MAX];
89 char data_url[PATH_MAX];
90 } LTTNG_PACKED;
91
92 LTTNG_HIDDEN
93 int lttng_snapshot_output_serialize(
94 const struct lttng_snapshot_output *output,
95 struct lttng_dynamic_buffer *buf)
96 {
97 struct lttng_snapshot_output_comm comm;
98 int ret;
99
100 comm.id = output->id;
101 comm.max_size = output->max_size;
102
103 ret = lttng_strncpy(comm.name, output->name, sizeof(comm.name));
104 if (ret) {
105 goto end;
106 }
107
108 ret = lttng_strncpy(comm.ctrl_url, output->ctrl_url, sizeof(comm.ctrl_url));
109 if (ret) {
110 goto end;
111 }
112
113 ret = lttng_strncpy(comm.data_url, output->data_url, sizeof(comm.data_url));
114 if (ret) {
115 goto end;
116 }
117
118 ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
119 if (ret) {
120 goto end;
121 }
122
123 end:
124 return ret;
125 }
126
127 LTTNG_HIDDEN
128 ssize_t lttng_snapshot_output_create_from_buffer(
129 const struct lttng_buffer_view *view,
130 struct lttng_snapshot_output **output_p)
131 {
132 const struct lttng_snapshot_output_comm *comm;
133 struct lttng_snapshot_output *output = NULL;
134 int ret;
135
136 if (view->size != sizeof(*comm)) {
137 ret = -1;
138 goto end;
139 }
140
141 output = lttng_snapshot_output_create();
142 if (!output) {
143 ret = -1;
144 goto end;
145 }
146
147 comm = (const struct lttng_snapshot_output_comm *) view->data;
148
149 output->id = comm->id;
150 output->max_size = comm->max_size;
151
152 ret = lttng_strncpy(output->name, comm->name, sizeof(output->name));
153 if (ret) {
154 goto end;
155 }
156
157 ret = lttng_strncpy(output->ctrl_url, comm->ctrl_url, sizeof(output->ctrl_url));
158 if (ret) {
159 goto end;
160 }
161
162 ret = lttng_strncpy(output->data_url, comm->data_url, sizeof(output->data_url));
163 if (ret) {
164 goto end;
165 }
166
167 *output_p = output;
168 output = NULL;
169 ret = sizeof(*comm);
170
171 end:
172 lttng_snapshot_output_destroy(output);
173 return ret;
174 }
This page took 0.031688 seconds and 4 git commands to generate.