fix: redundant decl of channel_destroy (-Wredundant-decls)
[lttng-ust.git] / liblttng-ust / lttng-context-vuid.c
CommitLineData
fca2f191 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
fca2f191
MJ
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
c0c0989a 5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
fca2f191 6 *
c0c0989a 7 * LTTng UST namespaced real user ID context.
fca2f191
MJ
8 */
9
10#define _LGPL_SOURCE
9af5d97a 11#include <limits.h>
b4051ad8 12#include <stddef.h>
fca2f191
MJ
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>
0466ac28 18#include <lttng/ringbuffer-context.h>
fc80554e
MJ
19
20#include "context-internal.h"
fca2f191
MJ
21#include "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 */
39static uid_t cached_vuid = INVALID_UID;
40
41static
42uid_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 */
59void lttng_context_vuid_reset(void)
60{
61 CMM_STORE_SHARED(cached_vuid, INVALID_UID);
62}
63
64static
daacdbfc 65size_t vuid_get_size(struct lttng_ust_ctx_field *field, size_t offset)
fca2f191
MJ
66{
67 size_t size = 0;
68
dc325c1d 69 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uid_t));
fca2f191
MJ
70 size += sizeof(uid_t);
71 return size;
72}
73
74static
daacdbfc 75void vuid_record(struct lttng_ust_ctx_field *field,
fca2f191 76 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 77 struct lttng_ust_channel_buffer *chan)
fca2f191
MJ
78{
79 uid_t vuid;
80
81 vuid = get_vuid();
8936b6c0 82 chan->ops->event_write(ctx, &vuid, sizeof(vuid), lttng_ust_rb_alignof(vuid));
fca2f191
MJ
83}
84
85static
daacdbfc
MD
86void vuid_get_value(struct lttng_ust_ctx_field *field,
87 struct lttng_ust_ctx_value *value)
fca2f191
MJ
88{
89 value->u.s64 = get_vuid();
90}
91
daacdbfc 92int lttng_add_vuid_to_ctx(struct lttng_ust_ctx **ctx)
fca2f191 93{
daacdbfc 94 struct lttng_ust_ctx_field *field;
a084756d
MD
95 struct lttng_ust_type_common *type;
96 int ret;
97
98 type = lttng_ust_create_type_integer(sizeof(uid_t) * CHAR_BIT,
dc325c1d 99 lttng_ust_rb_alignof(uid_t) * CHAR_BIT,
eae3c729 100 lttng_ust_is_signed_type(uid_t),
a084756d
MD
101 BYTE_ORDER, 10);
102 if (!type)
fca2f191 103 return -ENOMEM;
a084756d
MD
104 field = lttng_append_context(ctx);
105 if (!field) {
106 ret = -ENOMEM;
107 goto error_context;
108 }
fca2f191 109 if (lttng_find_context(*ctx, "vuid")) {
a084756d
MD
110 ret = -EEXIST;
111 goto error_find_context;
112 }
113 field->event_field->name = strdup("vuid");
114 if (!field->event_field->name) {
115 ret = -ENOMEM;
116 goto error_name;
fca2f191 117 }
a084756d 118 field->event_field->type = type;
fca2f191
MJ
119 field->get_size = vuid_get_size;
120 field->record = vuid_record;
121 field->get_value = vuid_get_value;
122 lttng_context_update(*ctx);
123 return 0;
a084756d
MD
124
125error_name:
126error_find_context:
127 lttng_remove_context_field(ctx, field);
128error_context:
129 lttng_ust_destroy_type(type);
130 return ret;
fca2f191 131}
This page took 0.030313 seconds and 4 git commands to generate.