cleanup: explicitly mark unused parameters (-Wunused-parameter)
[lttng-ust.git] / liblttng-ust / lttng-context-veuid.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 effective 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_veuid = INVALID_UID;
40
41static
42uid_t get_veuid(void)
43{
44 uid_t veuid;
45
46 veuid = CMM_LOAD_SHARED(cached_veuid);
47
48 if (caa_unlikely(veuid == INVALID_UID)) {
49 veuid = geteuid();
50 CMM_STORE_SHARED(cached_veuid, veuid);
51 }
52
53 return veuid;
54}
55
56/*
57 * The veuid can change on setuid, setreuid, setresuid and seteuid.
58 */
59void lttng_context_veuid_reset(void)
60{
61 CMM_STORE_SHARED(cached_veuid, INVALID_UID);
62}
63
64static
2208d8b5
MJ
65size_t veuid_get_size(struct lttng_ust_ctx_field *field __attribute__((unused)),
66 size_t offset)
fca2f191
MJ
67{
68 size_t size = 0;
69
dc325c1d 70 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uid_t));
fca2f191
MJ
71 size += sizeof(uid_t);
72 return size;
73}
74
75static
2208d8b5 76void veuid_record(struct lttng_ust_ctx_field *field __attribute__((unused)),
fca2f191 77 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 78 struct lttng_ust_channel_buffer *chan)
fca2f191
MJ
79{
80 uid_t veuid;
81
82 veuid = get_veuid();
8936b6c0 83 chan->ops->event_write(ctx, &veuid, sizeof(veuid), lttng_ust_rb_alignof(veuid));
fca2f191
MJ
84}
85
86static
2208d8b5 87void veuid_get_value(struct lttng_ust_ctx_field *field __attribute__((unused)),
daacdbfc 88 struct lttng_ust_ctx_value *value)
fca2f191
MJ
89{
90 value->u.s64 = get_veuid();
91}
92
daacdbfc 93int lttng_add_veuid_to_ctx(struct lttng_ust_ctx **ctx)
fca2f191 94{
daacdbfc 95 struct lttng_ust_ctx_field *field;
a084756d
MD
96 struct lttng_ust_type_common *type;
97 int ret;
98
99 type = lttng_ust_create_type_integer(sizeof(uid_t) * CHAR_BIT,
dc325c1d 100 lttng_ust_rb_alignof(uid_t) * CHAR_BIT,
eae3c729 101 lttng_ust_is_signed_type(uid_t),
a084756d
MD
102 BYTE_ORDER, 10);
103 if (!type)
fca2f191 104 return -ENOMEM;
a084756d
MD
105 field = lttng_append_context(ctx);
106 if (!field) {
107 ret = -ENOMEM;
108 goto error_context;
109 }
fca2f191 110 if (lttng_find_context(*ctx, "veuid")) {
a084756d
MD
111 ret = -EEXIST;
112 goto error_find_context;
113 }
114 field->event_field->name = strdup("veuid");
115 if (!field->event_field->name) {
116 ret = -ENOMEM;
117 goto error_name;
fca2f191 118 }
a084756d 119 field->event_field->type = type;
fca2f191
MJ
120 field->get_size = veuid_get_size;
121 field->record = veuid_record;
122 field->get_value = veuid_get_value;
123 lttng_context_update(*ctx);
124 return 0;
a084756d
MD
125
126error_name:
127error_find_context:
128 lttng_remove_context_field(ctx, field);
129error_context:
130 lttng_ust_destroy_type(type);
131 return ret;
fca2f191 132}
This page took 0.030101 seconds and 4 git commands to generate.