Commit | Line | Data |
---|---|---|
fca2f191 MJ |
1 | /* |
2 | * lttng-context-vgid.c | |
3 | * | |
4 | * LTTng UST namespaced real group ID context. | |
5 | * | |
6 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * 2019 Michael Jeanson <mjeanson@efficios.com> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; only | |
12 | * version 2.1 of the License. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | */ | |
23 | ||
24 | #define _LGPL_SOURCE | |
b4051ad8 | 25 | #include <stddef.h> |
fca2f191 MJ |
26 | #include <sys/types.h> |
27 | #include <sys/stat.h> | |
28 | #include <unistd.h> | |
29 | #include <lttng/ust-events.h> | |
30 | #include <lttng/ust-tracer.h> | |
31 | #include <lttng/ringbuffer-config.h> | |
32 | #include "creds.h" | |
33 | ||
34 | ||
35 | /* | |
36 | * At the kernel level, user IDs and group IDs are a per-thread attribute. | |
37 | * However, POSIX requires that all threads in a process share the same | |
38 | * credentials. The NPTL threading implementation handles the POSIX | |
39 | * requirements by providing wrapper functions for the various system calls | |
40 | * that change process UIDs and GIDs. These wrapper functions (including those | |
41 | * for setreuid() and setregid()) employ a signal-based technique to ensure | |
42 | * that when one thread changes credentials, all of the other threads in the | |
43 | * process also change their credentials. | |
44 | */ | |
45 | ||
46 | /* | |
47 | * We cache the result to ensure we don't trigger a system call for | |
48 | * each event. User / group IDs are global to the process. | |
49 | */ | |
50 | static gid_t cached_vgid = INVALID_GID; | |
51 | ||
52 | static | |
53 | gid_t get_vgid(void) | |
54 | { | |
55 | gid_t vgid; | |
56 | ||
57 | vgid = CMM_LOAD_SHARED(cached_vgid); | |
58 | ||
59 | if (caa_unlikely(cached_vgid == (gid_t) -1)) { | |
60 | vgid = getgid(); | |
61 | CMM_STORE_SHARED(cached_vgid, vgid); | |
62 | } | |
63 | ||
64 | return vgid; | |
65 | } | |
66 | ||
67 | /* | |
68 | * The vgid can change on setuid, setreuid and setresuid. | |
69 | */ | |
70 | void lttng_context_vgid_reset(void) | |
71 | { | |
72 | CMM_STORE_SHARED(cached_vgid, INVALID_GID); | |
73 | } | |
74 | ||
75 | static | |
76 | size_t vgid_get_size(struct lttng_ctx_field *field, size_t offset) | |
77 | { | |
78 | size_t size = 0; | |
79 | ||
80 | size += lib_ring_buffer_align(offset, lttng_alignof(gid_t)); | |
81 | size += sizeof(gid_t); | |
82 | return size; | |
83 | } | |
84 | ||
85 | static | |
86 | void vgid_record(struct lttng_ctx_field *field, | |
87 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
88 | struct lttng_channel *chan) | |
89 | { | |
90 | gid_t vgid; | |
91 | ||
92 | vgid = get_vgid(); | |
93 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(vgid)); | |
94 | chan->ops->event_write(ctx, &vgid, sizeof(vgid)); | |
95 | } | |
96 | ||
97 | static | |
98 | void vgid_get_value(struct lttng_ctx_field *field, | |
99 | struct lttng_ctx_value *value) | |
100 | { | |
101 | value->u.s64 = get_vgid(); | |
102 | } | |
103 | ||
104 | int lttng_add_vgid_to_ctx(struct lttng_ctx **ctx) | |
105 | { | |
106 | struct lttng_ctx_field *field; | |
107 | ||
108 | field = lttng_append_context(ctx); | |
109 | if (!field) | |
110 | return -ENOMEM; | |
111 | if (lttng_find_context(*ctx, "vgid")) { | |
112 | lttng_remove_context_field(ctx, field); | |
113 | return -EEXIST; | |
114 | } | |
115 | field->event_field.name = "vgid"; | |
116 | field->event_field.type.atype = atype_integer; | |
218deb69 MD |
117 | field->event_field.type.u.integer.size = sizeof(gid_t) * CHAR_BIT; |
118 | field->event_field.type.u.integer.alignment = lttng_alignof(gid_t) * CHAR_BIT; | |
119 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(gid_t); | |
120 | field->event_field.type.u.integer.reverse_byte_order = 0; | |
121 | field->event_field.type.u.integer.base = 10; | |
122 | field->event_field.type.u.integer.encoding = lttng_encode_none; | |
fca2f191 MJ |
123 | field->get_size = vgid_get_size; |
124 | field->record = vgid_record; | |
125 | field->get_value = vgid_get_value; | |
126 | lttng_context_update(*ctx); | |
127 | return 0; | |
128 | } |