2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
7 * LTTng UST namespaced real group ID context.
13 #include <sys/types.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ust-tracer.h>
18 #include <lttng/ringbuffer-context.h>
20 #include "context-internal.h"
25 * At the kernel level, user IDs and group IDs are a per-thread attribute.
26 * However, POSIX requires that all threads in a process share the same
27 * credentials. The NPTL threading implementation handles the POSIX
28 * requirements by providing wrapper functions for the various system calls
29 * that change process UIDs and GIDs. These wrapper functions (including those
30 * for setreuid() and setregid()) employ a signal-based technique to ensure
31 * that when one thread changes credentials, all of the other threads in the
32 * process also change their credentials.
36 * We cache the result to ensure we don't trigger a system call for
37 * each event. User / group IDs are global to the process.
39 static gid_t cached_vgid
= INVALID_GID
;
46 vgid
= CMM_LOAD_SHARED(cached_vgid
);
48 if (caa_unlikely(cached_vgid
== (gid_t
) -1)) {
50 CMM_STORE_SHARED(cached_vgid
, vgid
);
57 * The vgid can change on setuid, setreuid and setresuid.
59 void lttng_context_vgid_reset(void)
61 CMM_STORE_SHARED(cached_vgid
, INVALID_GID
);
65 size_t vgid_get_size(struct lttng_ust_ctx_field
*field
, size_t offset
)
69 size
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_alignof(gid_t
));
70 size
+= sizeof(gid_t
);
75 void vgid_record(struct lttng_ust_ctx_field
*field
,
76 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
77 struct lttng_ust_channel_buffer
*chan
)
82 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_alignof(vgid
));
83 chan
->ops
->event_write(ctx
, &vgid
, sizeof(vgid
));
87 void vgid_get_value(struct lttng_ust_ctx_field
*field
,
88 struct lttng_ust_ctx_value
*value
)
90 value
->u
.s64
= get_vgid();
93 int lttng_add_vgid_to_ctx(struct lttng_ust_ctx
**ctx
)
95 struct lttng_ust_ctx_field
*field
;
96 struct lttng_ust_type_common
*type
;
99 type
= lttng_ust_create_type_integer(sizeof(gid_t
) * CHAR_BIT
,
100 lttng_alignof(gid_t
) * CHAR_BIT
,
101 lttng_ust_is_signed_type(gid_t
),
105 field
= lttng_append_context(ctx
);
110 if (lttng_find_context(*ctx
, "vgid")) {
112 goto error_find_context
;
114 field
->event_field
->name
= strdup("vgid");
115 if (!field
->event_field
->name
) {
119 field
->event_field
->type
= type
;
120 field
->get_size
= vgid_get_size
;
121 field
->record
= vgid_record
;
122 field
->get_value
= vgid_get_value
;
123 lttng_context_update(*ctx
);
128 lttng_remove_context_field(ctx
, field
);
130 lttng_ust_destroy_type(type
);