Refactoring: context structures
[lttng-ust.git] / liblttng-ust / lttng-context-vsuid.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 saved set-user ID context.
fca2f191
MJ
8 */
9
fca2f191 10#define _LGPL_SOURCE
b4051ad8 11#include <stddef.h>
fca2f191
MJ
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>
fc80554e
MJ
18
19#include "context-internal.h"
fca2f191
MJ
20#include "creds.h"
21
22
23/*
24 * At the kernel level, user IDs and group IDs are a per-thread attribute.
25 * However, POSIX requires that all threads in a process share the same
26 * credentials. The NPTL threading implementation handles the POSIX
27 * requirements by providing wrapper functions for the various system calls
28 * that change process UIDs and GIDs. These wrapper functions (including those
29 * for setreuid() and setregid()) employ a signal-based technique to ensure
30 * that when one thread changes credentials, all of the other threads in the
31 * process also change their credentials.
32 */
33
34/*
35 * We cache the result to ensure we don't trigger a system call for
36 * each event. User / group IDs are global to the process.
37 */
38static uid_t cached_vsuid = INVALID_UID;
39
40static
41uid_t get_vsuid(void)
42{
43 uid_t vsuid;
44
45 vsuid = CMM_LOAD_SHARED(cached_vsuid);
46
47 if (caa_unlikely(vsuid == INVALID_UID)) {
48 uid_t uid, euid, suid;
49
50 if (getresuid(&uid, &euid, &suid) == 0) {
51 vsuid = suid;
52 CMM_STORE_SHARED(cached_vsuid, vsuid);
53 }
54 }
55
56 return vsuid;
57}
58
59/*
60 * The vsuid can change on setuid, setreuid and setresuid.
61 */
62void lttng_context_vsuid_reset(void)
63{
64 CMM_STORE_SHARED(cached_vsuid, INVALID_UID);
65}
66
67static
daacdbfc 68size_t vsuid_get_size(struct lttng_ust_ctx_field *field, size_t offset)
fca2f191
MJ
69{
70 size_t size = 0;
71
72 size += lib_ring_buffer_align(offset, lttng_alignof(uid_t));
73 size += sizeof(uid_t);
74 return size;
75}
76
77static
daacdbfc 78void vsuid_record(struct lttng_ust_ctx_field *field,
fca2f191
MJ
79 struct lttng_ust_lib_ring_buffer_ctx *ctx,
80 struct lttng_channel *chan)
81{
82 uid_t vsuid;
83
84 vsuid = get_vsuid();
85 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vsuid));
86 chan->ops->event_write(ctx, &vsuid, sizeof(vsuid));
87}
88
89static
daacdbfc
MD
90void vsuid_get_value(struct lttng_ust_ctx_field *field,
91 struct lttng_ust_ctx_value *value)
fca2f191
MJ
92{
93 value->u.s64 = get_vsuid();
94}
95
daacdbfc 96int lttng_add_vsuid_to_ctx(struct lttng_ust_ctx **ctx)
fca2f191 97{
daacdbfc 98 struct lttng_ust_ctx_field *field;
fca2f191
MJ
99
100 field = lttng_append_context(ctx);
101 if (!field)
102 return -ENOMEM;
103 if (lttng_find_context(*ctx, "vsuid")) {
104 lttng_remove_context_field(ctx, field);
105 return -EEXIST;
106 }
daacdbfc
MD
107 field->event_field->name = "vsuid";
108 field->event_field->type.atype = atype_integer;
109 field->event_field->type.u.integer.size = sizeof(uid_t) * CHAR_BIT;
110 field->event_field->type.u.integer.alignment = lttng_alignof(uid_t) * CHAR_BIT;
111 field->event_field->type.u.integer.signedness = lttng_is_signed_type(uid_t);
112 field->event_field->type.u.integer.reverse_byte_order = 0;
113 field->event_field->type.u.integer.base = 10;
114 field->event_field->type.u.integer.encoding = lttng_encode_none;
fca2f191
MJ
115 field->get_size = vsuid_get_size;
116 field->record = vsuid_record;
117 field->get_value = vsuid_get_value;
118 lttng_context_update(*ctx);
119 return 0;
120}
This page took 0.028904 seconds and 4 git commands to generate.