API refactoring: introduce probe context
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-vsgid.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 saved set-group ID context.
8 */
9
10 #define _LGPL_SOURCE
11 #include <limits.h>
12 #include <stddef.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ust-tracer.h>
18 #include <lttng/ust-ringbuffer-context.h>
19
20 #include "context-internal.h"
21 #include "common/creds.h"
22
23
24 /*
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.
33 */
34
35 /*
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.
38 */
39 static gid_t cached_vsgid = INVALID_GID;
40
41 static
42 gid_t get_vsgid(void)
43 {
44 gid_t vsgid;
45
46 vsgid = CMM_LOAD_SHARED(cached_vsgid);
47
48 if (caa_unlikely(vsgid == INVALID_GID)) {
49 gid_t gid, egid, sgid;
50
51 if (getresgid(&gid, &egid, &sgid) == 0) {
52 vsgid = sgid;
53 CMM_STORE_SHARED(cached_vsgid, vsgid);
54 }
55 }
56
57 return vsgid;
58 }
59
60 /*
61 * The vsgid can change on setuid, setreuid and setresuid.
62 */
63 void lttng_context_vsgid_reset(void)
64 {
65 CMM_STORE_SHARED(cached_vsgid, INVALID_GID);
66 }
67
68 static
69 size_t vsgid_get_size(void *priv __attribute__((unused)),
70 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
71 size_t offset)
72 {
73 size_t size = 0;
74
75 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(gid_t));
76 size += sizeof(gid_t);
77 return size;
78 }
79
80 static
81 void vsgid_record(void *priv __attribute__((unused)),
82 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
83 struct lttng_ust_ring_buffer_ctx *ctx,
84 struct lttng_ust_channel_buffer *chan)
85 {
86 gid_t vsgid;
87
88 vsgid = get_vsgid();
89 chan->ops->event_write(ctx, &vsgid, sizeof(vsgid), lttng_ust_rb_alignof(vsgid));
90 }
91
92 static
93 void vsgid_get_value(void *priv __attribute__((unused)),
94 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
95 struct lttng_ust_ctx_value *value)
96 {
97 value->u.u64 = get_vsgid();
98 }
99
100 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
101 lttng_ust_static_event_field("vsgid",
102 lttng_ust_static_type_integer(sizeof(gid_t) * CHAR_BIT,
103 lttng_ust_rb_alignof(gid_t) * CHAR_BIT,
104 lttng_ust_is_signed_type(gid_t),
105 LTTNG_UST_BYTE_ORDER, 10),
106 false, false),
107 vsgid_get_size,
108 vsgid_record,
109 vsgid_get_value,
110 NULL, NULL);
111
112 int lttng_add_vsgid_to_ctx(struct lttng_ust_ctx **ctx)
113 {
114 int ret;
115
116 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
117 ret = -EEXIST;
118 goto error_find_context;
119 }
120 ret = lttng_ust_context_append(ctx, ctx_field);
121 if (ret)
122 return ret;
123 return 0;
124
125 error_find_context:
126 return ret;
127 }
This page took 0.03209 seconds and 4 git commands to generate.