Namespace remaining symbols in lttng/ringbuffer-context.h
[lttng-ust.git] / liblttng-ust / lttng-context-user-ns.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
6 *
7 * LTTng UST user namespace context.
8 */
9
10 #define _LGPL_SOURCE
11 #include <limits.h>
12 #include <stddef.h>
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>
18 #include <lttng/ringbuffer-context.h>
19
20 #include "context-internal.h"
21 #include "ns.h"
22
23 /*
24 * We cache the result to ensure we don't stat(2) the proc filesystem on
25 * each event. The user namespace is global to the process.
26 */
27 static ino_t cached_user_ns = NS_INO_UNINITIALIZED;
28
29 static
30 ino_t get_user_ns(void)
31 {
32 struct stat sb;
33 ino_t user_ns;
34
35 user_ns = CMM_LOAD_SHARED(cached_user_ns);
36
37 /*
38 * If the cache is populated, do nothing and return the
39 * cached inode number.
40 */
41 if (caa_likely(user_ns != NS_INO_UNINITIALIZED))
42 return user_ns;
43
44 /*
45 * At this point we have to populate the cache, set the initial
46 * value to NS_INO_UNAVAILABLE (0), if we fail to get the inode
47 * number from the proc filesystem, this is the value we will
48 * cache.
49 */
50 user_ns = NS_INO_UNAVAILABLE;
51
52 if (stat("/proc/self/ns/user", &sb) == 0) {
53 user_ns = sb.st_ino;
54 }
55
56 /*
57 * And finally, store the inode number in the cache.
58 */
59 CMM_STORE_SHARED(cached_user_ns, user_ns);
60
61 return user_ns;
62 }
63
64 /*
65 * The user namespace can change for 3 reasons
66 * * clone(2) called with CLONE_NEWUSER
67 * * setns(2) called with the fd of a different user ns
68 * * unshare(2) called with CLONE_NEWUSER
69 */
70 void lttng_context_user_ns_reset(void)
71 {
72 CMM_STORE_SHARED(cached_user_ns, NS_INO_UNINITIALIZED);
73 }
74
75 static
76 size_t user_ns_get_size(struct lttng_ust_ctx_field *field, size_t offset)
77 {
78 size_t size = 0;
79
80 size += lttng_ust_lib_ring_buffer_align(offset, lttng_alignof(ino_t));
81 size += sizeof(ino_t);
82 return size;
83 }
84
85 static
86 void user_ns_record(struct lttng_ust_ctx_field *field,
87 struct lttng_ust_lib_ring_buffer_ctx *ctx,
88 struct lttng_ust_channel_buffer *chan)
89 {
90 ino_t user_ns;
91
92 user_ns = get_user_ns();
93 lttng_ust_lib_ring_buffer_align_ctx(ctx, lttng_alignof(user_ns));
94 chan->ops->event_write(ctx, &user_ns, sizeof(user_ns));
95 }
96
97 static
98 void user_ns_get_value(struct lttng_ust_ctx_field *field,
99 struct lttng_ust_ctx_value *value)
100 {
101 value->u.s64 = get_user_ns();
102 }
103
104 int lttng_add_user_ns_to_ctx(struct lttng_ust_ctx **ctx)
105 {
106 struct lttng_ust_ctx_field *field;
107 struct lttng_ust_type_common *type;
108 int ret;
109
110 type = lttng_ust_create_type_integer(sizeof(ino_t) * CHAR_BIT,
111 lttng_alignof(ino_t) * CHAR_BIT,
112 lttng_ust_is_signed_type(ino_t),
113 BYTE_ORDER, 10);
114 if (!type)
115 return -ENOMEM;
116 field = lttng_append_context(ctx);
117 if (!field) {
118 ret = -ENOMEM;
119 goto error_context;
120 }
121 if (lttng_find_context(*ctx, "user_ns")) {
122 ret = -EEXIST;
123 goto error_find_context;
124 }
125 field->event_field->name = strdup("user_ns");
126 if (!field->event_field->name) {
127 ret = -ENOMEM;
128 goto error_name;
129 }
130 field->event_field->type = type;
131 field->get_size = user_ns_get_size;
132 field->record = user_ns_record;
133 field->get_value = user_ns_get_value;
134 lttng_context_update(*ctx);
135 return 0;
136
137 error_name:
138 error_find_context:
139 lttng_remove_context_field(ctx, field);
140 error_context:
141 lttng_ust_destroy_type(type);
142 return ret;
143 }
This page took 0.051158 seconds and 5 git commands to generate.