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.h>
14 #include <common/error.h>
15 #include <common/hashtable/hashtable.h>
16 #include <common/hashtable/utils.h>
17 #include <common/tracker.h>
21 struct process_attr_tracker_values_comm_header
{
25 struct process_attr_tracker_value_comm
{
26 /* enum lttng_process_attr_value_type */
29 struct process_attr_integral_value_comm integral
;
30 /* Includes the '\0' terminator. */
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))
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; \
44 (comm_value)->u._unsigned = \
45 (typeof((comm_value)->u._unsigned)) value; \
48 static inline bool is_virtual_process_attr(enum lttng_process_attr process_attr
)
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
;
55 static inline bool is_value_type_name(
56 enum lttng_process_attr_value_type value_type
)
58 return value_type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
||
59 value_type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
;
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
)
72 enum lttng_error_code ret
= LTTNG_OK
;
73 struct process_attr_value
*value
= zmalloc(sizeof(*value
));
76 ret
= LTTNG_ERR_NOMEM
;
80 if (value_view
&& value_view
->size
> 0) {
81 if (value_view
->data
[value_view
->size
- 1] != '\0') {
82 ret
= LTTNG_ERR_INVALID
;
85 name
= strdup(value_view
->data
);
87 ret
= LTTNG_ERR_NOMEM
;
92 if (domain
!= LTTNG_DOMAIN_UST
&& domain
!= LTTNG_DOMAIN_KERNEL
) {
93 ERR("Only the user space and kernel space domains may be specified to configure process attribute trackers");
94 ret
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
98 if (!is_virtual_process_attr(process_attr
) &&
99 domain
!= LTTNG_DOMAIN_KERNEL
) {
100 ERR("Non-virtual process attributes can only be used in the kernel domain");
101 ret
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
105 /* Only expect a payload for name value types. */
106 if (is_value_type_name(value_type
) && value_view
->size
== 0) {
107 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
109 } else if (!is_value_type_name(value_type
) && value_view
&&
110 value_view
->size
!= 0) {
111 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
115 value
->type
= value_type
;
116 switch (process_attr
) {
117 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
118 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
119 if (value_type
!= LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
) {
120 ERR("Invalid value type used for process ID process attribute");
121 ret
= LTTNG_ERR_INVALID
;
125 GET_INTEGRAL_COMM_VALUE(integral_value
, pid_t
);
127 case LTTNG_PROCESS_ATTR_USER_ID
:
128 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
129 switch (value_type
) {
130 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
131 value
->value
.uid
= GET_INTEGRAL_COMM_VALUE(
132 integral_value
, uid_t
);
134 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
136 ret
= LTTNG_ERR_INVALID
;
140 value
->value
.user_name
= name
;
144 ERR("Invalid value type used for user ID process attribute");
145 ret
= LTTNG_ERR_INVALID
;
149 case LTTNG_PROCESS_ATTR_GROUP_ID
:
150 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
151 switch (value_type
) {
152 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
153 value
->value
.gid
= GET_INTEGRAL_COMM_VALUE(
154 integral_value
, gid_t
);
156 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
158 ret
= LTTNG_ERR_INVALID
;
162 value
->value
.group_name
= name
;
166 ERR("Invalid value type used for group ID process attribute");
167 ret
= LTTNG_ERR_INVALID
;
172 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
182 process_attr_value_destroy(value
);
187 const char *lttng_process_attr_to_string(enum lttng_process_attr process_attr
)
189 switch (process_attr
) {
190 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
192 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
193 return "virtual process ID";
194 case LTTNG_PROCESS_ATTR_USER_ID
:
196 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
197 return "virtual user ID";
198 case LTTNG_PROCESS_ATTR_GROUP_ID
:
200 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
201 return "virtual group ID";
203 return "unknown process attribute";
207 static void process_attr_tracker_value_destructor(void *ptr
)
209 struct process_attr_value
*value
= (typeof(value
)) ptr
;
211 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(sizeof(*values
));
223 lttng_dynamic_pointer_array_init(
224 &values
->array
, process_attr_tracker_value_destructor
);
230 unsigned int _lttng_process_attr_values_get_count(
231 const struct lttng_process_attr_values
*values
)
233 return (unsigned int) lttng_dynamic_pointer_array_get_count(
238 const struct process_attr_value
*lttng_process_attr_tracker_values_get_at_index(
239 const struct lttng_process_attr_values
*values
,
242 return lttng_dynamic_pointer_array_get_pointer(&values
->array
, index
);
246 int process_attr_tracker_value_serialize(const struct process_attr_value
*value
,
247 struct lttng_dynamic_buffer
*buffer
)
250 struct process_attr_tracker_value_comm value_comm
= {
251 .type
= (int32_t) value
->type
,
253 const char *name
= NULL
;
255 switch (value
->type
) {
256 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
257 SET_INTEGRAL_COMM_VALUE(
258 &value_comm
.value
.integral
, value
->value
.pid
);
260 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
261 SET_INTEGRAL_COMM_VALUE(
262 &value_comm
.value
.integral
, value
->value
.uid
);
264 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
265 SET_INTEGRAL_COMM_VALUE(
266 &value_comm
.value
.integral
, value
->value
.gid
);
268 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
269 name
= value
->value
.user_name
;
271 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
272 name
= value
->value
.group_name
;
279 value_comm
.value
.name_len
= strlen(name
) + 1;
282 ret
= lttng_dynamic_buffer_append(
283 buffer
, &value_comm
, sizeof(value_comm
));
289 ret
= lttng_dynamic_buffer_append(
290 buffer
, name
, value_comm
.value
.name_len
);
297 int lttng_process_attr_values_serialize(
298 const struct lttng_process_attr_values
*values
,
299 struct lttng_dynamic_buffer
*buffer
)
302 unsigned int count
, i
;
303 struct process_attr_tracker_values_comm_header header
= {};
305 count
= _lttng_process_attr_values_get_count(values
);
306 header
.count
= (uint32_t) count
;
308 ret
= lttng_dynamic_buffer_append(buffer
, &header
, sizeof(header
));
313 for (i
= 0; i
< count
; i
++) {
314 const struct process_attr_value
*value
=
315 lttng_process_attr_tracker_values_get_at_index(
318 ret
= process_attr_tracker_value_serialize(value
, buffer
);
328 ssize_t
lttng_process_attr_values_create_from_buffer(
329 enum lttng_domain_type domain
,
330 enum lttng_process_attr process_attr
,
331 const struct lttng_buffer_view
*buffer_view
,
332 struct lttng_process_attr_values
**_values
)
336 struct lttng_process_attr_values
*values
;
337 struct lttng_buffer_view header_view
;
338 const struct process_attr_tracker_values_comm_header
*header
;
340 values
= lttng_process_attr_values_create();
345 header_view
= lttng_buffer_view_from_view(
346 buffer_view
, 0, sizeof(*header
));
347 if (!header_view
.data
) {
350 offset
= header_view
.size
;
351 header
= (typeof(header
)) header_view
.data
;
354 * Check that the number of values is not absurdly large with respect to
355 * the received buffer's size.
357 if (buffer_view
->size
<
358 header
->count
* sizeof(struct process_attr_tracker_value_comm
)) {
361 for (i
= 0; i
< (unsigned int) header
->count
; i
++) {
363 enum lttng_error_code ret_code
;
364 const struct process_attr_tracker_value_comm
*value_comm
;
365 struct process_attr_value
*value
;
366 enum lttng_process_attr_value_type type
;
367 struct lttng_buffer_view value_view
;
368 struct lttng_buffer_view value_name_view
= {};
370 value_view
= lttng_buffer_view_from_view(
371 buffer_view
, offset
, sizeof(*value_comm
));
372 if (!value_view
.data
) {
376 offset
+= value_view
.size
;
377 value_comm
= (typeof(value_comm
)) value_view
.data
;
378 type
= (typeof(type
)) value_comm
->type
;
380 if (is_value_type_name(type
)) {
381 value_name_view
= lttng_buffer_view_from_view(
383 value_comm
->value
.name_len
);
384 offset
+= value_name_view
.size
;
386 ret_code
= process_attr_value_from_comm(domain
, process_attr
,
387 type
, &value_comm
->value
.integral
,
388 &value_name_view
, &value
);
389 if (ret_code
!= LTTNG_OK
) {
393 ret
= lttng_dynamic_pointer_array_add_pointer(
394 &values
->array
, value
);
396 process_attr_value_destroy(value
);
404 lttng_process_attr_values_destroy(values
);
409 void lttng_process_attr_values_destroy(struct lttng_process_attr_values
*values
)
414 lttng_dynamic_pointer_array_reset(&values
->array
);
419 struct process_attr_value
*process_attr_value_copy(
420 const struct process_attr_value
*value
)
422 struct process_attr_value
*new_value
= NULL
;
428 new_value
= zmalloc(sizeof(*new_value
));
432 if (is_value_type_name(value
->type
)) {
434 value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
?
435 value
->value
.user_name
:
436 value
->value
.group_name
;
437 char **dst
= value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
?
438 &new_value
->value
.user_name
:
439 &new_value
->value
.group_name
;
441 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
);
488 bool process_attr_tracker_value_equal(const struct process_attr_value
*a
,
489 const struct process_attr_value
*b
)
491 if (a
->type
!= b
->type
) {
495 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID
:
496 return a
->value
.pid
== b
->value
.pid
;
497 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID
:
498 return a
->value
.uid
== b
->value
.uid
;
499 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID
:
500 return a
->value
.gid
== b
->value
.gid
;
501 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
:
502 return !strcmp(a
->value
.user_name
, b
->value
.user_name
);
503 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
:
504 return !strcmp(a
->value
.group_name
, b
->value
.group_name
);
511 void process_attr_value_destroy(struct process_attr_value
*value
)
516 if (is_value_type_name(value
->type
)) {
517 free(value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
?
518 value
->value
.user_name
:
519 value
->value
.group_name
);