f8a4fba0f3944b4112b2680934a7f91de5bc2a8f
[lttng-tools.git] / src / common / tracker.c
1 /*
2 * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library 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 Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <assert.h>
19 #include <common/defaults.h>
20 #include <common/error.h>
21 #include <common/macros.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/uri.h>
24 #include <lttng/tracker-internal.h>
25 #include <stdio.h>
26 #include <time.h>
27
28 struct lttng_tracker_id *lttng_tracker_id_create(void)
29 {
30 struct lttng_tracker_id *id;
31
32 id = zmalloc(sizeof(*id));
33 if (!id) {
34 goto error;
35 }
36
37 id->type = LTTNG_ID_UNKNOWN;
38 id->string = NULL;
39 id->value = -1;
40 return id;
41 error:
42 lttng_tracker_id_destroy(id);
43 return NULL;
44 }
45
46 enum lttng_tracker_id_status lttng_tracker_id_set_value(
47 struct lttng_tracker_id *id, int value)
48 {
49 assert(id);
50
51 if (value < 0) {
52 return LTTNG_TRACKER_ID_STATUS_INVALID;
53 }
54
55 id->type = LTTNG_ID_VALUE;
56 id->value = value;
57 return LTTNG_TRACKER_ID_STATUS_OK;
58 }
59
60 enum lttng_tracker_id_status lttng_tracker_id_set_string(
61 struct lttng_tracker_id *id, const char *value)
62 {
63 assert(id);
64 assert(value);
65
66 id->type = LTTNG_ID_STRING;
67 id->string = strdup(value);
68 if (id->string == NULL) {
69 /* No memory left */
70 goto error;
71 }
72
73 return LTTNG_TRACKER_ID_STATUS_OK;
74 error:
75 return LTTNG_TRACKER_ID_STATUS_INVALID;
76 }
77
78 enum lttng_tracker_id_status lttng_tracker_id_set_all(
79 struct lttng_tracker_id *id)
80 {
81 assert(id);
82
83 id->type = LTTNG_ID_ALL;
84
85 return LTTNG_TRACKER_ID_STATUS_OK;
86 }
87
88 void lttng_tracker_id_destroy(struct lttng_tracker_id *id)
89 {
90 if (id == NULL) {
91 return;
92 }
93
94 if (id->string != NULL) {
95 free(id->string);
96 }
97
98 free(id);
99 }
100
101 void lttng_tracker_ids_destroy(struct lttng_tracker_id **ids, size_t nr_ids)
102 {
103 if (ids == NULL) {
104 return;
105 }
106
107 for (int i = 0; i < nr_ids; i++) {
108 lttng_tracker_id_destroy(ids[i]);
109 }
110 }
111
112 enum lttng_tracker_id_type lttng_tracker_id_get_type(
113 const struct lttng_tracker_id *id)
114 {
115 assert(id);
116 return id->type;
117 }
118
119 enum lttng_tracker_id_status lttng_tracker_id_get_value(
120 const struct lttng_tracker_id *id, int *value)
121 {
122 assert(id);
123 if (id->type == LTTNG_ID_UNKNOWN) {
124 return LTTNG_TRACKER_ID_STATUS_UNSET;
125 }
126
127 if (id->type != LTTNG_ID_VALUE) {
128 return LTTNG_TRACKER_ID_STATUS_INVALID;
129 }
130
131 *value = id->value;
132 return LTTNG_TRACKER_ID_STATUS_OK;
133 }
134
135 bool lttng_tracker_id_is_equal(const struct lttng_tracker_id *left,
136 const struct lttng_tracker_id *right)
137 {
138 if (left->type != right->type) {
139 return 0;
140 }
141
142 switch (left->type) {
143 case LTTNG_ID_ALL:
144 return 1;
145 case LTTNG_ID_VALUE:
146 if (left->value != right->value) {
147 return 0;
148 }
149 break;
150 case LTTNG_ID_STRING:
151 if (strcmp(left->string, right->string) != 0) {
152 return 0;
153 }
154 break;
155 default:
156 /*
157 * Normally this should return true, but comparing unset tracker
158 * id is "invalid".
159 */
160 return 0;
161 }
162 return 1;
163 }
164
165 struct lttng_tracker_id *lttng_tracker_id_copy(
166 const struct lttng_tracker_id *orig)
167 {
168 struct lttng_tracker_id *copy = NULL;
169 enum lttng_tracker_id_status status;
170
171 copy = lttng_tracker_id_create();
172 if (copy == NULL) {
173 goto error;
174 }
175
176 switch (orig->type) {
177 case LTTNG_ID_ALL:
178 status = lttng_tracker_id_set_all(copy);
179 break;
180 case LTTNG_ID_VALUE:
181 status = lttng_tracker_id_set_value(copy, orig->value);
182 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
183 goto error;
184 }
185 break;
186 case LTTNG_ID_STRING:
187 status = lttng_tracker_id_set_string(copy, orig->string);
188 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
189 goto error;
190 }
191 break;
192 default:
193 status = LTTNG_TRACKER_ID_STATUS_OK;
194 break;
195 }
196
197 if (status != LTTNG_TRACKER_ID_STATUS_OK) {
198 goto error;
199 }
200
201 return copy;
202 error:
203 lttng_tracker_id_destroy(copy);
204 return NULL;
205 }
206
207 enum lttng_tracker_id_status lttng_tracker_id_get_string(
208 const struct lttng_tracker_id *id, const char **value)
209 {
210 assert(id);
211 if (id->type == LTTNG_ID_UNKNOWN) {
212 *value = NULL;
213 return LTTNG_TRACKER_ID_STATUS_UNSET;
214 }
215
216 if (id->type != LTTNG_ID_STRING) {
217 *value = NULL;
218 return LTTNG_TRACKER_ID_STATUS_INVALID;
219 }
220
221 *value = id->string;
222 return LTTNG_TRACKER_ID_STATUS_OK;
223 }
This page took 0.0461 seconds and 3 git commands to generate.