Introduce LTTNG_UST_MAP_POPULATE_POLICY environment variable
[lttng-ust.git] / liblttng-ust / lttng-context-vgid.c
1 /*
2 * lttng-context-vgid.c
3 *
4 * LTTng UST namespaced real group ID context.
5 *
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * 2019 Michael Jeanson <mjeanson@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _LGPL_SOURCE
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <lttng/ust-events.h>
29 #include <lttng/ust-tracer.h>
30 #include <lttng/ringbuffer-config.h>
31 #include "creds.h"
32
33
34 /*
35 * At the kernel level, user IDs and group IDs are a per-thread attribute.
36 * However, POSIX requires that all threads in a process share the same
37 * credentials. The NPTL threading implementation handles the POSIX
38 * requirements by providing wrapper functions for the various system calls
39 * that change process UIDs and GIDs. These wrapper functions (including those
40 * for setreuid() and setregid()) employ a signal-based technique to ensure
41 * that when one thread changes credentials, all of the other threads in the
42 * process also change their credentials.
43 */
44
45 /*
46 * We cache the result to ensure we don't trigger a system call for
47 * each event. User / group IDs are global to the process.
48 */
49 static gid_t cached_vgid = INVALID_GID;
50
51 static
52 gid_t get_vgid(void)
53 {
54 gid_t vgid;
55
56 vgid = CMM_LOAD_SHARED(cached_vgid);
57
58 if (caa_unlikely(cached_vgid == (gid_t) -1)) {
59 vgid = getgid();
60 CMM_STORE_SHARED(cached_vgid, vgid);
61 }
62
63 return vgid;
64 }
65
66 /*
67 * The vgid can change on setuid, setreuid and setresuid.
68 */
69 void lttng_context_vgid_reset(void)
70 {
71 CMM_STORE_SHARED(cached_vgid, INVALID_GID);
72 }
73
74 static
75 size_t vgid_get_size(struct lttng_ctx_field *field, size_t offset)
76 {
77 size_t size = 0;
78
79 size += lib_ring_buffer_align(offset, lttng_alignof(gid_t));
80 size += sizeof(gid_t);
81 return size;
82 }
83
84 static
85 void vgid_record(struct lttng_ctx_field *field,
86 struct lttng_ust_lib_ring_buffer_ctx *ctx,
87 struct lttng_channel *chan)
88 {
89 gid_t vgid;
90
91 vgid = get_vgid();
92 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vgid));
93 chan->ops->event_write(ctx, &vgid, sizeof(vgid));
94 }
95
96 static
97 void vgid_get_value(struct lttng_ctx_field *field,
98 struct lttng_ctx_value *value)
99 {
100 value->u.s64 = get_vgid();
101 }
102
103 int lttng_add_vgid_to_ctx(struct lttng_ctx **ctx)
104 {
105 struct lttng_ctx_field *field;
106
107 field = lttng_append_context(ctx);
108 if (!field)
109 return -ENOMEM;
110 if (lttng_find_context(*ctx, "vgid")) {
111 lttng_remove_context_field(ctx, field);
112 return -EEXIST;
113 }
114 field->event_field.name = "vgid";
115 field->event_field.type.atype = atype_integer;
116 field->event_field.type.u.basic.integer.size = sizeof(gid_t) * CHAR_BIT;
117 field->event_field.type.u.basic.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT;
118 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(gid_t);
119 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
120 field->event_field.type.u.basic.integer.base = 10;
121 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
122 field->get_size = vgid_get_size;
123 field->record = vgid_record;
124 field->get_value = vgid_get_value;
125 lttng_context_update(*ctx);
126 return 0;
127 }
This page took 0.030647 seconds and 4 git commands to generate.