API refactoring: introduce probe context
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-vuid.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 user 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 uid_t cached_vuid = INVALID_UID;
40
41 static
42 uid_t get_vuid(void)
43 {
44 uid_t vuid;
45
46 vuid = CMM_LOAD_SHARED(cached_vuid);
47
48 if (caa_unlikely(vuid == INVALID_UID)) {
49 vuid = getuid();
50 CMM_STORE_SHARED(cached_vuid, vuid);
51 }
52
53 return vuid;
54 }
55
56 /*
57 * The vuid can change on setuid, setreuid and setresuid.
58 */
59 void lttng_context_vuid_reset(void)
60 {
61 CMM_STORE_SHARED(cached_vuid, INVALID_UID);
62 }
63
64 static
65 size_t vuid_get_size(void *priv __attribute__((unused)),
66 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
67 size_t offset)
68 {
69 size_t size = 0;
70
71 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uid_t));
72 size += sizeof(uid_t);
73 return size;
74 }
75
76 static
77 void vuid_record(void *priv __attribute__((unused)),
78 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
79 struct lttng_ust_ring_buffer_ctx *ctx,
80 struct lttng_ust_channel_buffer *chan)
81 {
82 uid_t vuid;
83
84 vuid = get_vuid();
85 chan->ops->event_write(ctx, &vuid, sizeof(vuid), lttng_ust_rb_alignof(vuid));
86 }
87
88 static
89 void vuid_get_value(void *priv __attribute__((unused)),
90 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
91 struct lttng_ust_ctx_value *value)
92 {
93 value->u.u64 = get_vuid();
94 }
95
96 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
97 lttng_ust_static_event_field("vuid",
98 lttng_ust_static_type_integer(sizeof(uid_t) * CHAR_BIT,
99 lttng_ust_rb_alignof(uid_t) * CHAR_BIT,
100 lttng_ust_is_signed_type(uid_t),
101 LTTNG_UST_BYTE_ORDER, 10),
102 false, false),
103 vuid_get_size,
104 vuid_record,
105 vuid_get_value,
106 NULL, NULL);
107
108 int lttng_add_vuid_to_ctx(struct lttng_ust_ctx **ctx)
109 {
110 int ret;
111
112 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
113 ret = -EEXIST;
114 goto error_find_context;
115 }
116 ret = lttng_ust_context_append(ctx, ctx_field);
117 if (ret)
118 return ret;
119 return 0;
120
121 error_find_context:
122 return ret;
123 }
This page took 0.030968 seconds and 4 git commands to generate.