532a924b56a772b3d55d11e611028ebf7267a7fe
[lttng-tools.git] / src / common / tracker.c
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 *
7 */
8
9 #include <lttng/domain.h>
10 #include <lttng/lttng-error.h>
11 #include <lttng/tracker.h>
12
13 #include <common/dynamic-array.h>
14 #include <common/error.h>
15 #include <common/hashtable/hashtable.h>
16 #include <common/hashtable/utils.h>
17 #include <common/tracker.h>
18
19 #include <stdbool.h>
20
21 struct process_attr_tracker_values_comm_header {
22 uint32_t count;
23 };
24
25 struct process_attr_tracker_value_comm {
26 /* enum lttng_process_attr_value_type */
27 int32_t type;
28 union {
29 struct process_attr_integral_value_comm integral;
30 /* Includes the '\0' terminator. */
31 uint32_t name_len;
32 } value;
33 };
34
35 #define GET_INTEGRAL_COMM_VALUE(value_ptr, as_type) \
36 ((as_type)(is_signed(as_type) ? (value_ptr)->u._signed : \
37 (value_ptr)->u._unsigned))
38
39 #define SET_INTEGRAL_COMM_VALUE(comm_value, value) \
40 if (is_signed(typeof(value))) { \
41 (comm_value)->u._signed = \
42 (typeof((comm_value)->u._signed)) value; \
43 } else { \
44 (comm_value)->u._unsigned = \
45 (typeof((comm_value)->u._unsigned)) value; \
46 }
47
48 static inline bool is_virtual_process_attr(enum lttng_process_attr process_attr)
49 {
50 return process_attr == LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID ||
51 process_attr == LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID ||
52 process_attr == LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID;
53 }
54
55 static inline bool is_value_type_name(
56 enum lttng_process_attr_value_type value_type)
57 {
58 return value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ||
59 value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME;
60 }
61
62 LTTNG_HIDDEN
63 enum lttng_error_code process_attr_value_from_comm(
64 enum lttng_domain_type domain,
65 enum lttng_process_attr process_attr,
66 enum lttng_process_attr_value_type value_type,
67 const struct process_attr_integral_value_comm *integral_value,
68 const struct lttng_buffer_view *value_view,
69 struct process_attr_value **_value)
70 {
71 char *name = NULL;
72 enum lttng_error_code ret = LTTNG_OK;
73 struct process_attr_value *value = zmalloc(sizeof(*value));
74
75 if (!value) {
76 ret = LTTNG_ERR_NOMEM;
77 goto error;
78 }
79
80 if (value_view && value_view->size > 0) {
81 if (value_view->data[value_view->size - 1] != '\0') {
82 ret = LTTNG_ERR_INVALID;
83 goto error;
84 }
85 name = strdup(value_view->data);
86 if (!name) {
87 ret = LTTNG_ERR_NOMEM;
88 }
89 }
90
91 if (domain != LTTNG_DOMAIN_UST && domain != LTTNG_DOMAIN_KERNEL) {
92 ERR("Only the user space and kernel space domains may be specified to configure process attribute trackers");
93 ret = LTTNG_ERR_UNSUPPORTED_DOMAIN;
94 goto error;
95 }
96
97 if (!is_virtual_process_attr(process_attr) &&
98 domain != LTTNG_DOMAIN_KERNEL) {
99 ERR("Non-virtual process attributes can only be used in the kernel domain");
100 ret = LTTNG_ERR_UNSUPPORTED_DOMAIN;
101 goto error;
102 }
103
104 /* Only expect a payload for name value types. */
105 if (is_value_type_name(value_type) && value_view->size == 0) {
106 ret = LTTNG_ERR_INVALID_PROTOCOL;
107 goto error;
108 } else if (!is_value_type_name(value_type) && value_view->size != 0) {
109 ret = LTTNG_ERR_INVALID_PROTOCOL;
110 goto error;
111 }
112
113 value->type = value_type;
114 switch (process_attr) {
115 case LTTNG_PROCESS_ATTR_PROCESS_ID:
116 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
117 if (value_type != LTTNG_PROCESS_ATTR_VALUE_TYPE_PID) {
118 ERR("Invalid value type used for process ID process attribute");
119 ret = LTTNG_ERR_INVALID;
120 goto error;
121 }
122 value->value.pid =
123 GET_INTEGRAL_COMM_VALUE(integral_value, pid_t);
124 break;
125 case LTTNG_PROCESS_ATTR_USER_ID:
126 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
127 switch (value_type) {
128 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
129 value->value.uid = GET_INTEGRAL_COMM_VALUE(
130 integral_value, uid_t);
131 break;
132 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
133 if (!name) {
134 ret = LTTNG_ERR_INVALID;
135 goto error;
136 }
137
138 value->value.user_name = name;
139 name = NULL;
140 break;
141 default:
142 ERR("Invalid value type used for user ID process attribute");
143 ret = LTTNG_ERR_INVALID;
144 goto error;
145 }
146 break;
147 case LTTNG_PROCESS_ATTR_GROUP_ID:
148 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
149 switch (value_type) {
150 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
151 value->value.gid = GET_INTEGRAL_COMM_VALUE(
152 integral_value, gid_t);
153 break;
154 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
155 if (!name) {
156 ret = LTTNG_ERR_INVALID;
157 goto error;
158 }
159
160 value->value.group_name = name;
161 name = NULL;
162 break;
163 default:
164 ERR("Invalid value type used for group ID process attribute");
165 ret = LTTNG_ERR_INVALID;
166 goto error;
167 }
168 break;
169 default:
170 ret = LTTNG_ERR_INVALID_PROTOCOL;
171 goto error;
172 }
173
174 *_value = value;
175 value = NULL;
176 return LTTNG_OK;
177 error:
178 free(name);
179 process_attr_value_destroy(value);
180 return ret;
181 }
182
183 LTTNG_HIDDEN
184 const char *lttng_process_attr_to_string(enum lttng_process_attr process_attr)
185 {
186 switch (process_attr) {
187 case LTTNG_PROCESS_ATTR_PROCESS_ID:
188 return "process ID";
189 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
190 return "virtual process ID";
191 case LTTNG_PROCESS_ATTR_USER_ID:
192 return "user ID";
193 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
194 return "virtual user ID";
195 case LTTNG_PROCESS_ATTR_GROUP_ID:
196 return "group ID";
197 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
198 return "virtual group ID";
199 default:
200 return "unknown process attribute";
201 }
202 }
203
204 static void process_attr_tracker_value_destructor(void *ptr)
205 {
206 struct process_attr_value *value = (typeof(value)) ptr;
207
208 process_attr_value_destroy(value);
209 }
210
211 LTTNG_HIDDEN
212 struct lttng_process_attr_values *lttng_process_attr_values_create(void)
213 {
214 struct lttng_process_attr_values *values = zmalloc(sizeof(*values));
215
216 if (!values) {
217 goto end;
218 }
219
220 lttng_dynamic_pointer_array_init(
221 &values->array, process_attr_tracker_value_destructor);
222 end:
223 return values;
224 }
225
226 LTTNG_HIDDEN
227 unsigned int _lttng_process_attr_values_get_count(
228 const struct lttng_process_attr_values *values)
229 {
230 return (unsigned int) lttng_dynamic_pointer_array_get_count(
231 &values->array);
232 }
233
234 LTTNG_HIDDEN
235 const struct process_attr_value *lttng_process_attr_tracker_values_get_at_index(
236 const struct lttng_process_attr_values *values,
237 unsigned int index)
238 {
239 return lttng_dynamic_pointer_array_get_pointer(&values->array, index);
240 }
241
242 static
243 int process_attr_tracker_value_serialize(const struct process_attr_value *value,
244 struct lttng_dynamic_buffer *buffer)
245 {
246 int ret;
247 struct process_attr_tracker_value_comm value_comm = {
248 .type = (int32_t) value->type,
249 };
250 const char *name = NULL;
251
252 switch (value->type) {
253 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
254 SET_INTEGRAL_COMM_VALUE(
255 &value_comm.value.integral, value->value.pid);
256 break;
257 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
258 SET_INTEGRAL_COMM_VALUE(
259 &value_comm.value.integral, value->value.uid);
260 break;
261 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
262 SET_INTEGRAL_COMM_VALUE(
263 &value_comm.value.integral, value->value.gid);
264 break;
265 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
266 name = value->value.user_name;
267 break;
268 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
269 name = value->value.group_name;
270 break;
271 default:
272 abort();
273 }
274
275 if (name) {
276 value_comm.value.name_len = strlen(name) + 1;
277 }
278
279 ret = lttng_dynamic_buffer_append(
280 buffer, &value_comm, sizeof(value_comm));
281 if (ret) {
282 goto end;
283 }
284
285 if (name) {
286 ret = lttng_dynamic_buffer_append(
287 buffer, name, value_comm.value.name_len);
288 }
289 end:
290 return ret;
291 }
292
293 LTTNG_HIDDEN
294 int lttng_process_attr_values_serialize(
295 const struct lttng_process_attr_values *values,
296 struct lttng_dynamic_buffer *buffer)
297 {
298 int ret;
299 unsigned int count, i;
300 struct process_attr_tracker_values_comm_header header = {};
301
302 count = _lttng_process_attr_values_get_count(values);
303 header.count = (uint32_t) count;
304
305 ret = lttng_dynamic_buffer_append(buffer, &header, sizeof(header));
306 if (ret) {
307 goto end;
308 }
309
310 for (i = 0; i < count; i++) {
311 const struct process_attr_value *value =
312 lttng_process_attr_tracker_values_get_at_index(
313 values, i);
314
315 ret = process_attr_tracker_value_serialize(value, buffer);
316 if (ret) {
317 goto end;
318 }
319 }
320 end:
321 return ret;
322 }
323
324 LTTNG_HIDDEN
325 ssize_t lttng_process_attr_values_create_from_buffer(
326 enum lttng_domain_type domain,
327 enum lttng_process_attr process_attr,
328 const struct lttng_buffer_view *buffer_view,
329 struct lttng_process_attr_values **_values)
330 {
331 ssize_t offset;
332 unsigned int i;
333 struct lttng_process_attr_values *values;
334 struct lttng_buffer_view header_view;
335 const struct process_attr_tracker_values_comm_header *header;
336
337 values = lttng_process_attr_values_create();
338 if (!values) {
339 goto error;
340 }
341
342 header_view = lttng_buffer_view_from_view(
343 buffer_view, 0, sizeof(*header));
344 if (!header_view.data) {
345 goto error;
346 }
347 offset = header_view.size;
348 header = (typeof(header)) header_view.data;
349
350 /*
351 * Check that the number of values is not absurdly large with respect to
352 * the received buffer's size.
353 */
354 if (buffer_view->size <
355 header->count * sizeof(struct process_attr_tracker_value_comm)) {
356 goto error;
357 }
358 for (i = 0; i < (unsigned int) header->count; i++) {
359 int ret;
360 enum lttng_error_code ret_code;
361 const struct process_attr_tracker_value_comm *value_comm;
362 struct process_attr_value *value;
363 enum lttng_process_attr_value_type type;
364 struct lttng_buffer_view value_view;
365 struct lttng_buffer_view value_name_view = {};
366
367 value_view = lttng_buffer_view_from_view(
368 buffer_view, offset, sizeof(*value_comm));
369 if (!value_view.data) {
370 goto error;
371 }
372
373 offset += value_view.size;
374 value_comm = (typeof(value_comm)) value_view.data;
375 type = (typeof(type)) value_comm->type;
376
377 if (is_value_type_name(type)) {
378 value_name_view = lttng_buffer_view_from_view(
379 buffer_view, offset,
380 value_comm->value.name_len);
381 offset += value_name_view.size;
382 }
383 ret_code = process_attr_value_from_comm(domain, process_attr,
384 type, &value_comm->value.integral,
385 &value_name_view, &value);
386 if (ret_code != LTTNG_OK) {
387 goto error;
388 }
389
390 ret = lttng_dynamic_pointer_array_add_pointer(
391 &values->array, value);
392 if (ret) {
393 process_attr_value_destroy(value);
394 goto error;
395 }
396 }
397
398 *_values = values;
399 return offset;
400 error:
401 lttng_process_attr_values_destroy(values);
402 return -1;
403 }
404
405 LTTNG_HIDDEN
406 void lttng_process_attr_values_destroy(struct lttng_process_attr_values *values)
407 {
408 if (!values) {
409 return;
410 }
411 lttng_dynamic_pointer_array_reset(&values->array);
412 free(values);
413 }
414
415 LTTNG_HIDDEN
416 struct process_attr_value *process_attr_value_copy(
417 const struct process_attr_value *value)
418 {
419 struct process_attr_value *new_value = NULL;
420
421 if (!value) {
422 goto end;
423 }
424
425 new_value = zmalloc(sizeof(*new_value));
426 if (!new_value) {
427 goto end;
428 }
429 if (is_value_type_name(value->type)) {
430 const char *src =
431 value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ?
432 value->value.user_name :
433 value->value.group_name;
434 char **dst = value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ?
435 &new_value->value.user_name :
436 &new_value->value.group_name;
437
438 new_value->type = value->type;
439 *dst = strdup(src);
440 if (!*dst) {
441 goto error;
442 }
443 } else {
444 *new_value = *value;
445 }
446 end:
447 return new_value;
448 error:
449 free(new_value);
450 return NULL;
451 }
452
453 LTTNG_HIDDEN
454 unsigned long process_attr_value_hash(const struct process_attr_value *a)
455 {
456 unsigned long hash = hash_key_ulong((void *) a->type, lttng_ht_seed);
457
458 switch (a->type) {
459 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
460 hash ^= hash_key_ulong((void *) (unsigned long) a->value.pid,
461 lttng_ht_seed);
462 break;
463 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
464 hash ^= hash_key_ulong((void *) (unsigned long) a->value.uid,
465 lttng_ht_seed);
466 break;
467 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
468 hash ^= hash_key_ulong((void *) (unsigned long) a->value.gid,
469 lttng_ht_seed);
470 break;
471 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
472 hash ^= hash_key_str(a->value.user_name, lttng_ht_seed);
473 break;
474 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
475 hash ^= hash_key_str(a->value.group_name, lttng_ht_seed);
476 break;
477 default:
478 abort();
479 }
480
481 return hash;
482 }
483
484 LTTNG_HIDDEN
485 bool process_attr_tracker_value_equal(const struct process_attr_value *a,
486 const struct process_attr_value *b)
487 {
488 if (a->type != b->type) {
489 return false;
490 }
491 switch (a->type) {
492 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
493 return a->value.pid == b->value.pid;
494 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
495 return a->value.uid == b->value.uid;
496 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
497 return a->value.gid == b->value.gid;
498 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
499 return !strcmp(a->value.user_name, b->value.user_name);
500 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
501 return !strcmp(a->value.group_name, b->value.group_name);
502 default:
503 abort();
504 }
505 }
506
507 LTTNG_HIDDEN
508 void process_attr_value_destroy(struct process_attr_value *value)
509 {
510 if (!value) {
511 return;
512 }
513 if (is_value_type_name(value->type)) {
514 free(value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ?
515 value->value.user_name :
516 value->value.group_name);
517 }
518 free(value);
519 }
This page took 0.065895 seconds and 3 git commands to generate.