Refactoring: Channel structures
[lttng-ust.git] / liblttng-ust / lttng-context-vsgid.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-group 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>
0466ac28 17#include <lttng/ringbuffer-context.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 gid_t cached_vsgid = INVALID_GID;
39
40static
41gid_t get_vsgid(void)
42{
43 gid_t vsgid;
44
45 vsgid = CMM_LOAD_SHARED(cached_vsgid);
46
47 if (caa_unlikely(vsgid == INVALID_GID)) {
48 gid_t gid, egid, sgid;
49
50 if (getresgid(&gid, &egid, &sgid) == 0) {
51 vsgid = sgid;
52 CMM_STORE_SHARED(cached_vsgid, vsgid);
53 }
54 }
55
56 return vsgid;
57}
58
59/*
60 * The vsgid can change on setuid, setreuid and setresuid.
61 */
62void lttng_context_vsgid_reset(void)
63{
64 CMM_STORE_SHARED(cached_vsgid, INVALID_GID);
65}
66
67static
daacdbfc 68size_t vsgid_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(gid_t));
73 size += sizeof(gid_t);
74 return size;
75}
76
77static
daacdbfc 78void vsgid_record(struct lttng_ust_ctx_field *field,
fca2f191 79 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 80 struct lttng_ust_channel_buffer *chan)
fca2f191
MJ
81{
82 gid_t vsgid;
83
84 vsgid = get_vsgid();
85 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vsgid));
86 chan->ops->event_write(ctx, &vsgid, sizeof(vsgid));
87}
88
89static
daacdbfc
MD
90void vsgid_get_value(struct lttng_ust_ctx_field *field,
91 struct lttng_ust_ctx_value *value)
fca2f191
MJ
92{
93 value->u.s64 = get_vsgid();
94}
95
daacdbfc 96int lttng_add_vsgid_to_ctx(struct lttng_ust_ctx **ctx)
fca2f191 97{
daacdbfc 98 struct lttng_ust_ctx_field *field;
a084756d
MD
99 struct lttng_ust_type_common *type;
100 int ret;
101
102 type = lttng_ust_create_type_integer(sizeof(gid_t) * CHAR_BIT,
103 lttng_alignof(gid_t) * CHAR_BIT,
104 lttng_is_signed_type(gid_t),
105 BYTE_ORDER, 10);
106 if (!type)
fca2f191 107 return -ENOMEM;
a084756d
MD
108 field = lttng_append_context(ctx);
109 if (!field) {
110 ret = -ENOMEM;
111 goto error_context;
112 }
fca2f191 113 if (lttng_find_context(*ctx, "vsgid")) {
a084756d
MD
114 ret = -EEXIST;
115 goto error_find_context;
116 }
117 field->event_field->name = strdup("vsgid");
118 if (!field->event_field->name) {
119 ret = -ENOMEM;
120 goto error_name;
fca2f191 121 }
a084756d 122 field->event_field->type = type;
fca2f191
MJ
123 field->get_size = vsgid_get_size;
124 field->record = vsgid_record;
125 field->get_value = vsgid_get_value;
126 lttng_context_update(*ctx);
127 return 0;
a084756d
MD
128
129error_name:
130error_find_context:
131 lttng_remove_context_field(ctx, field);
132error_context:
133 lttng_ust_destroy_type(type);
134 return ret;
fca2f191 135}
This page took 0.029047 seconds and 4 git commands to generate.