2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <lttng/domain.h>
10 #include <lttng/lttng-error.h>
11 #include <lttng/tracker.h>
13 #include <common/dynamic-array.hpp>
14 #include <common/error.hpp>
15 #include <common/hashtable/hashtable.hpp>
16 #include <common/hashtable/utils.hpp>
17 #include <common/tracker.hpp>
21 #include <type_traits>
23 struct process_attr_tracker_values_comm_header
{
27 struct process_attr_tracker_value_comm
{
28 /* enum lttng_process_attr_value_type */
31 struct process_attr_integral_value_comm integral
;
32 /* Includes the '\0' terminator. */
37 #define GET_INTEGRAL_COMM_VALUE(value_ptr, as_type) \
38 ((as_type)(std::is_signed<as_type>::value ? (value_ptr)->u._signed : \
39 (value_ptr)->u._unsigned))
41 #define SET_INTEGRAL_COMM_VALUE(comm_value, val) \
42 if (std::is_signed<typeof(val)>::value) { \
43 (comm_value)->u._signed = \
44 (typeof((comm_value)->u._signed)) val; \
46 (comm_value)->u._unsigned = \
47 (typeof((comm_value)->u._unsigned)) val; \
50 static inline bool is_virtual_process_attr(enum lttng_process_attr process_attr
)
52 return process_attr
== LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
||
53 process_attr
== LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
||
54 process_attr
== LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
;
57 static inline bool is_value_type_name(
58 enum lttng_process_attr_value_type value_type
)
60 return value_type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
||
61 value_type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
;
64 enum lttng_error_code
process_attr_value_from_comm(
65 enum lttng_domain_type domain
,
66 enum lttng_process_attr process_attr
,
67 enum lttng_process_attr_value_type value_type
,
68 const struct process_attr_integral_value_comm
*integral_value
,
69 const struct lttng_buffer_view
*value_view
,
70 struct process_attr_value
**_value
)
73 enum lttng_error_code ret
= LTTNG_OK
;
74 struct process_attr_value
*value
= zmalloc
<process_attr_value
>();
77 ret
= LTTNG_ERR_NOMEM
;
81 if (value_view
&& value_view
->size
> 0) {
82 if (value_view
->data
[value_view
->size
- 1] != '\0') {
83 ret
= LTTNG_ERR_INVALID
;
86 name
= strdup(value_view
->data
);
88 ret
= LTTNG_ERR_NOMEM
;
93 if (domain
!= LTTNG_DOMAIN_UST
&& domain
!= LTTNG_DOMAIN_KERNEL
) {
94 ERR("Only the user space and kernel space domains may be specified to configure process attribute trackers");
95 ret
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
99 if (!is_virtual_process_attr(process_attr
) &&
100 domain
!= LTTNG_DOMAIN_KERNEL
) {
101 ERR("Non-virtual process attributes can only be used in the kernel domain");
102 ret
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
106 /* Only expect a payload for name value types. */
107 if (is_value_type_name(value_type
) &&
108 (!value_view
|| value_view
->size
== 0)) {
109 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
111 } else if (!is_value_type_name(value_type
) && value_view
&&
112 value_view
->size
!= 0) {
113 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
117 value
->type
= value_type
;
118 switch (process_attr
) {
119 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
120 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
121 if (value_type
!= LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
) {
122 ERR("Invalid value type used for process ID process attribute");
123 ret
= LTTNG_ERR_INVALID
;
127 GET_INTEGRAL_COMM_VALUE(integral_value
, pid_t
);
129 case LTTNG_PROCESS_ATTR_USER_ID
:
130 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
131 switch (value_type
) {
132 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
133 value
->value
.uid
= GET_INTEGRAL_COMM_VALUE(
134 integral_value
, uid_t
);
136 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
138 ret
= LTTNG_ERR_INVALID
;
142 value
->value
.user_name
= name
;
146 ERR("Invalid value type used for user ID process attribute");
147 ret
= LTTNG_ERR_INVALID
;
151 case LTTNG_PROCESS_ATTR_GROUP_ID
:
152 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
153 switch (value_type
) {
154 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
155 value
->value
.gid
= GET_INTEGRAL_COMM_VALUE(
156 integral_value
, gid_t
);
158 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
160 ret
= LTTNG_ERR_INVALID
;
164 value
->value
.group_name
= name
;
168 ERR("Invalid value type used for group ID process attribute");
169 ret
= LTTNG_ERR_INVALID
;
174 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
184 process_attr_value_destroy(value
);
188 const char *lttng_process_attr_to_string(enum lttng_process_attr process_attr
)
190 switch (process_attr
) {
191 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
193 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
194 return "virtual process ID";
195 case LTTNG_PROCESS_ATTR_USER_ID
:
197 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
198 return "virtual user ID";
199 case LTTNG_PROCESS_ATTR_GROUP_ID
:
201 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
202 return "virtual group ID";
204 return "unknown process attribute";
208 static void process_attr_tracker_value_destructor(void *ptr
)
210 struct process_attr_value
*value
= (typeof(value
)) ptr
;
212 process_attr_value_destroy(value
);
215 struct lttng_process_attr_values
*lttng_process_attr_values_create(void)
217 struct lttng_process_attr_values
*values
= zmalloc
<lttng_process_attr_values
>();
223 lttng_dynamic_pointer_array_init(
224 &values
->array
, process_attr_tracker_value_destructor
);
229 unsigned int _lttng_process_attr_values_get_count(
230 const struct lttng_process_attr_values
*values
)
232 return (unsigned int) lttng_dynamic_pointer_array_get_count(
236 const struct process_attr_value
*lttng_process_attr_tracker_values_get_at_index(
237 const struct lttng_process_attr_values
*values
,
240 return (process_attr_value
*) lttng_dynamic_pointer_array_get_pointer(&values
->array
, index
);
244 int process_attr_tracker_value_serialize(const struct process_attr_value
*value
,
245 struct lttng_dynamic_buffer
*buffer
)
248 struct process_attr_tracker_value_comm value_comm
= {
249 .type
= (int32_t) value
->type
,
252 const char *name
= NULL
;
254 switch (value
->type
) {
255 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
256 SET_INTEGRAL_COMM_VALUE(
257 &value_comm
.value
.integral
, value
->value
.pid
);
259 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
260 SET_INTEGRAL_COMM_VALUE(
261 &value_comm
.value
.integral
, value
->value
.uid
);
263 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
264 SET_INTEGRAL_COMM_VALUE(
265 &value_comm
.value
.integral
, value
->value
.gid
);
267 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
268 name
= value
->value
.user_name
;
270 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
271 name
= value
->value
.group_name
;
278 value_comm
.value
.name_len
= strlen(name
) + 1;
281 ret
= lttng_dynamic_buffer_append(
282 buffer
, &value_comm
, sizeof(value_comm
));
288 ret
= lttng_dynamic_buffer_append(
289 buffer
, name
, value_comm
.value
.name_len
);
295 int lttng_process_attr_values_serialize(
296 const struct lttng_process_attr_values
*values
,
297 struct lttng_dynamic_buffer
*buffer
)
300 unsigned int count
, i
;
301 struct process_attr_tracker_values_comm_header header
= {};
303 count
= _lttng_process_attr_values_get_count(values
);
304 header
.count
= (uint32_t) count
;
306 ret
= lttng_dynamic_buffer_append(buffer
, &header
, sizeof(header
));
311 for (i
= 0; i
< count
; i
++) {
312 const struct process_attr_value
*value
=
313 lttng_process_attr_tracker_values_get_at_index(
316 ret
= process_attr_tracker_value_serialize(value
, buffer
);
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
)
333 struct lttng_process_attr_values
*values
;
334 struct lttng_buffer_view header_view
;
335 const struct process_attr_tracker_values_comm_header
*header
;
337 values
= lttng_process_attr_values_create();
342 header_view
= lttng_buffer_view_from_view(
343 buffer_view
, 0, sizeof(*header
));
344 if (!lttng_buffer_view_is_valid(&header_view
)) {
348 offset
= header_view
.size
;
349 header
= (typeof(header
)) header_view
.data
;
352 * Check that the number of values is not absurdly large with respect to
353 * the received buffer's size.
355 if (buffer_view
->size
<
356 header
->count
* sizeof(struct process_attr_tracker_value_comm
)) {
359 for (i
= 0; i
< (unsigned int) header
->count
; i
++) {
361 enum lttng_error_code ret_code
;
362 const struct process_attr_tracker_value_comm
*value_comm
;
363 struct process_attr_value
*value
;
364 enum lttng_process_attr_value_type type
;
365 struct lttng_buffer_view value_view
;
366 struct lttng_buffer_view value_name_view
= {};
368 value_view
= lttng_buffer_view_from_view(
369 buffer_view
, offset
, sizeof(*value_comm
));
370 if (!lttng_buffer_view_is_valid(&value_view
)) {
374 offset
+= value_view
.size
;
375 value_comm
= (typeof(value_comm
)) value_view
.data
;
376 type
= (typeof(type
)) value_comm
->type
;
378 if (is_value_type_name(type
)) {
379 value_name_view
= lttng_buffer_view_from_view(
381 value_comm
->value
.name_len
);
382 if (!lttng_buffer_view_is_valid(&value_name_view
)) {
386 offset
+= value_name_view
.size
;
389 ret_code
= process_attr_value_from_comm(domain
, process_attr
,
390 type
, &value_comm
->value
.integral
,
391 &value_name_view
, &value
);
392 if (ret_code
!= LTTNG_OK
) {
396 ret
= lttng_dynamic_pointer_array_add_pointer(
397 &values
->array
, value
);
399 process_attr_value_destroy(value
);
407 lttng_process_attr_values_destroy(values
);
411 void lttng_process_attr_values_destroy(struct lttng_process_attr_values
*values
)
416 lttng_dynamic_pointer_array_reset(&values
->array
);
420 struct process_attr_value
*process_attr_value_copy(
421 const struct process_attr_value
*value
)
423 struct process_attr_value
*new_value
= NULL
;
429 new_value
= zmalloc
<process_attr_value
>();
433 if (is_value_type_name(value
->type
)) {
435 value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
?
436 value
->value
.user_name
:
437 value
->value
.group_name
;
438 char **dst
= value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
?
439 &new_value
->value
.user_name
:
440 &new_value
->value
.group_name
;
442 new_value
->type
= value
->type
;
457 unsigned long process_attr_value_hash(const struct process_attr_value
*a
)
459 unsigned long hash
= hash_key_ulong((void *) a
->type
, lttng_ht_seed
);
462 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
463 hash
^= hash_key_ulong((void *) (unsigned long) a
->value
.pid
,
466 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
467 hash
^= hash_key_ulong((void *) (unsigned long) a
->value
.uid
,
470 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
471 hash
^= hash_key_ulong((void *) (unsigned long) a
->value
.gid
,
474 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
475 hash
^= hash_key_str(a
->value
.user_name
, lttng_ht_seed
);
477 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
478 hash
^= hash_key_str(a
->value
.group_name
, lttng_ht_seed
);
487 bool process_attr_tracker_value_equal(const struct process_attr_value
*a
,
488 const struct process_attr_value
*b
)
490 if (a
->type
!= b
->type
) {
494 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
495 return a
->value
.pid
== b
->value
.pid
;
496 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
497 return a
->value
.uid
== b
->value
.uid
;
498 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
499 return a
->value
.gid
== b
->value
.gid
;
500 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
501 return !strcmp(a
->value
.user_name
, b
->value
.user_name
);
502 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
503 return !strcmp(a
->value
.group_name
, b
->value
.group_name
);
509 void process_attr_value_destroy(struct process_attr_value
*value
)
514 if (is_value_type_name(value
->type
)) {
515 free(value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
?
516 value
->value
.user_name
:
517 value
->value
.group_name
);