Rename struct lttng_event_notifier to struct lttng_ust_event_notifier
[lttng-ust.git] / liblttng-ust / lttng-context-vgid.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 group ID context.
fca2f191
MJ
8 */
9
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 gid_t cached_vgid = INVALID_GID;
39
40static
41gid_t get_vgid(void)
42{
43 gid_t vgid;
44
45 vgid = CMM_LOAD_SHARED(cached_vgid);
46
47 if (caa_unlikely(cached_vgid == (gid_t) -1)) {
48 vgid = getgid();
49 CMM_STORE_SHARED(cached_vgid, vgid);
50 }
51
52 return vgid;
53}
54
55/*
56 * The vgid can change on setuid, setreuid and setresuid.
57 */
58void lttng_context_vgid_reset(void)
59{
60 CMM_STORE_SHARED(cached_vgid, INVALID_GID);
61}
62
63static
64size_t vgid_get_size(struct lttng_ctx_field *field, size_t offset)
65{
66 size_t size = 0;
67
68 size += lib_ring_buffer_align(offset, lttng_alignof(gid_t));
69 size += sizeof(gid_t);
70 return size;
71}
72
73static
74void vgid_record(struct lttng_ctx_field *field,
75 struct lttng_ust_lib_ring_buffer_ctx *ctx,
76 struct lttng_channel *chan)
77{
78 gid_t vgid;
79
80 vgid = get_vgid();
81 lib_ring_buffer_align_ctx(ctx, lttng_alignof(vgid));
82 chan->ops->event_write(ctx, &vgid, sizeof(vgid));
83}
84
85static
86void vgid_get_value(struct lttng_ctx_field *field,
87 struct lttng_ctx_value *value)
88{
89 value->u.s64 = get_vgid();
90}
91
92int lttng_add_vgid_to_ctx(struct lttng_ctx **ctx)
93{
94 struct lttng_ctx_field *field;
95
96 field = lttng_append_context(ctx);
97 if (!field)
98 return -ENOMEM;
99 if (lttng_find_context(*ctx, "vgid")) {
100 lttng_remove_context_field(ctx, field);
101 return -EEXIST;
102 }
103 field->event_field.name = "vgid";
104 field->event_field.type.atype = atype_integer;
218deb69
MD
105 field->event_field.type.u.integer.size = sizeof(gid_t) * CHAR_BIT;
106 field->event_field.type.u.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT;
107 field->event_field.type.u.integer.signedness = lttng_is_signed_type(gid_t);
108 field->event_field.type.u.integer.reverse_byte_order = 0;
109 field->event_field.type.u.integer.base = 10;
110 field->event_field.type.u.integer.encoding = lttng_encode_none;
fca2f191
MJ
111 field->get_size = vgid_get_size;
112 field->record = vgid_record;
113 field->get_value = vgid_get_value;
114 lttng_context_update(*ctx);
115 return 0;
116}
This page took 0.028421 seconds and 4 git commands to generate.