Introduce LTTNG_UST_MAP_POPULATE_POLICY environment variable
[lttng-ust.git] / liblttng-ust / lttng-context-user-ns.c
1 /*
2 * lttng-context-user-ns.c
3 *
4 * LTTng UST user namespace 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
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <lttng/ust-events.h>
29 #include <lttng/ust-tracer.h>
30 #include <lttng/ringbuffer-config.h>
31
32 #include "ns.h"
33
34 /*
35 * We cache the result to ensure we don't stat(2) the proc filesystem on
36 * each event. The user namespace is global to the process.
37 */
38 static ino_t cached_user_ns = NS_INO_UNINITIALIZED;
39
40 static
41 ino_t get_user_ns(void)
42 {
43 struct stat sb;
44 ino_t user_ns;
45
46 user_ns = CMM_LOAD_SHARED(cached_user_ns);
47
48 /*
49 * If the cache is populated, do nothing and return the
50 * cached inode number.
51 */
52 if (caa_likely(user_ns != NS_INO_UNINITIALIZED))
53 return user_ns;
54
55 /*
56 * At this point we have to populate the cache, set the initial
57 * value to NS_INO_UNAVAILABLE (0), if we fail to get the inode
58 * number from the proc filesystem, this is the value we will
59 * cache.
60 */
61 user_ns = NS_INO_UNAVAILABLE;
62
63 if (stat("/proc/self/ns/user", &sb) == 0) {
64 user_ns = sb.st_ino;
65 }
66
67 /*
68 * And finally, store the inode number in the cache.
69 */
70 CMM_STORE_SHARED(cached_user_ns, user_ns);
71
72 return user_ns;
73 }
74
75 /*
76 * The user namespace can change for 3 reasons
77 * * clone(2) called with CLONE_NEWUSER
78 * * setns(2) called with the fd of a different user ns
79 * * unshare(2) called with CLONE_NEWUSER
80 */
81 void lttng_context_user_ns_reset(void)
82 {
83 CMM_STORE_SHARED(cached_user_ns, NS_INO_UNINITIALIZED);
84 }
85
86 static
87 size_t user_ns_get_size(struct lttng_ctx_field *field, size_t offset)
88 {
89 size_t size = 0;
90
91 size += lib_ring_buffer_align(offset, lttng_alignof(ino_t));
92 size += sizeof(ino_t);
93 return size;
94 }
95
96 static
97 void user_ns_record(struct lttng_ctx_field *field,
98 struct lttng_ust_lib_ring_buffer_ctx *ctx,
99 struct lttng_channel *chan)
100 {
101 ino_t user_ns;
102
103 user_ns = get_user_ns();
104 lib_ring_buffer_align_ctx(ctx, lttng_alignof(user_ns));
105 chan->ops->event_write(ctx, &user_ns, sizeof(user_ns));
106 }
107
108 static
109 void user_ns_get_value(struct lttng_ctx_field *field,
110 struct lttng_ctx_value *value)
111 {
112 value->u.s64 = get_user_ns();
113 }
114
115 int lttng_add_user_ns_to_ctx(struct lttng_ctx **ctx)
116 {
117 struct lttng_ctx_field *field;
118
119 field = lttng_append_context(ctx);
120 if (!field)
121 return -ENOMEM;
122 if (lttng_find_context(*ctx, "user_ns")) {
123 lttng_remove_context_field(ctx, field);
124 return -EEXIST;
125 }
126 field->event_field.name = "user_ns";
127 field->event_field.type.atype = atype_integer;
128 field->event_field.type.u.basic.integer.size = sizeof(ino_t) * CHAR_BIT;
129 field->event_field.type.u.basic.integer.alignment = lttng_alignof(ino_t) * CHAR_BIT;
130 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(ino_t);
131 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
132 field->event_field.type.u.basic.integer.base = 10;
133 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
134 field->get_size = user_ns_get_size;
135 field->record = user_ns_record;
136 field->get_value = user_ns_get_value;
137 lttng_context_update(*ctx);
138 return 0;
139 }
This page took 0.031327 seconds and 4 git commands to generate.