Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-context-vgid.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
6 *
7 * LTTng UST namespaced real group ID context.
8 */
9
10 #define _LGPL_SOURCE
11 #include <stddef.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <lttng/ust-events.h>
16 #include <lttng/ust-tracer.h>
17 #include <lttng/ringbuffer-config.h>
18 #include "creds.h"
19
20
21 /*
22 * At the kernel level, user IDs and group IDs are a per-thread attribute.
23 * However, POSIX requires that all threads in a process share the same
24 * credentials. The NPTL threading implementation handles the POSIX
25 * requirements by providing wrapper functions for the various system calls
26 * that change process UIDs and GIDs. These wrapper functions (including those
27 * for setreuid() and setregid()) employ a signal-based technique to ensure
28 * that when one thread changes credentials, all of the other threads in the
29 * process also change their credentials.
30 */
31
32 /*
33 * We cache the result to ensure we don't trigger a system call for
34 * each event. User / group IDs are global to the process.
35 */
36 static gid_t cached_vgid = INVALID_GID;
37
38 static
39 gid_t get_vgid(void)
40 {
41 gid_t vgid;
42
43 vgid = CMM_LOAD_SHARED(cached_vgid);
44
45 if (caa_unlikely(cached_vgid == (gid_t) -1)) {
46 vgid = getgid();
47 CMM_STORE_SHARED(cached_vgid, vgid);
48 }
49
50 return vgid;
51 }
52
53 /*
54 * The vgid can change on setuid, setreuid and setresuid.
55 */
56 void lttng_context_vgid_reset(void)
57 {
58 CMM_STORE_SHARED(cached_vgid, INVALID_GID);
59 }
60
61 static
62 size_t vgid_get_size(struct lttng_ctx_field *field, size_t offset)
63 {
64 size_t size = 0;
65
66 size += lib_ring_buffer_align(offset, lttng_alignof(gid_t));
67 size += sizeof(gid_t);
68 return size;
69 }
70
71 static
72 void vgid_record(struct lttng_ctx_field *field,
73 struct lttng_ust_lib_ring_buffer_ctx *ctx,
74 struct lttng_channel *chan)
75 {
76 gid_t vgid;
77
78 vgid = get_vgid();
79 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vgid));
80 chan->ops->event_write(ctx, &vgid, sizeof(vgid));
81 }
82
83 static
84 void vgid_get_value(struct lttng_ctx_field *field,
85 struct lttng_ctx_value *value)
86 {
87 value->u.s64 = get_vgid();
88 }
89
90 int lttng_add_vgid_to_ctx(struct lttng_ctx **ctx)
91 {
92 struct lttng_ctx_field *field;
93
94 field = lttng_append_context(ctx);
95 if (!field)
96 return -ENOMEM;
97 if (lttng_find_context(*ctx, "vgid")) {
98 lttng_remove_context_field(ctx, field);
99 return -EEXIST;
100 }
101 field->event_field.name = "vgid";
102 field->event_field.type.atype = atype_integer;
103 field->event_field.type.u.integer.size = sizeof(gid_t) * CHAR_BIT;
104 field->event_field.type.u.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT;
105 field->event_field.type.u.integer.signedness = lttng_is_signed_type(gid_t);
106 field->event_field.type.u.integer.reverse_byte_order = 0;
107 field->event_field.type.u.integer.base = 10;
108 field->event_field.type.u.integer.encoding = lttng_encode_none;
109 field->get_size = vgid_get_size;
110 field->record = vgid_record;
111 field->get_value = vgid_get_value;
112 lttng_context_update(*ctx);
113 return 0;
114 }
This page took 0.031302 seconds and 4 git commands to generate.