d62bc860d99ca6f65decea985a0cfab913787037
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-mnt-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 mnt 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/ust-ringbuffer-context.h>
19
20 #include "context-internal.h"
21 #include "common/ns.h"
22
23 /*
24 * We cache the result to ensure we don't stat(2) the proc filesystem on
25 * each event. The mount namespace is global to the process.
26 */
27 static ino_t cached_mnt_ns = NS_INO_UNINITIALIZED;
28
29 static
30 ino_t get_mnt_ns(void)
31 {
32 struct stat sb;
33 ino_t mnt_ns;
34
35 mnt_ns = CMM_LOAD_SHARED(cached_mnt_ns);
36
37 /*
38 * If the cache is populated, do nothing and return the
39 * cached inode number.
40 */
41 if (caa_likely(mnt_ns != NS_INO_UNINITIALIZED))
42 return mnt_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 mnt_ns = NS_INO_UNAVAILABLE;
51
52 if (stat("/proc/self/ns/mnt", &sb) == 0) {
53 mnt_ns = sb.st_ino;
54 }
55
56 /*
57 * And finally, store the inode number in the cache.
58 */
59 CMM_STORE_SHARED(cached_mnt_ns, mnt_ns);
60
61 return mnt_ns;
62 }
63
64 /*
65 * The mnt namespace can change for 3 reasons
66 * * clone(2) called with CLONE_NEWNS
67 * * setns(2) called with the fd of a different mnt ns
68 * * unshare(2) called with CLONE_NEWNS
69 */
70 void lttng_context_mnt_ns_reset(void)
71 {
72 CMM_STORE_SHARED(cached_mnt_ns, NS_INO_UNINITIALIZED);
73 }
74
75 static
76 size_t mnt_ns_get_size(void *priv __attribute__((unused)),
77 size_t offset)
78 {
79 size_t size = 0;
80
81 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(ino_t));
82 size += sizeof(ino_t);
83 return size;
84 }
85
86 static
87 void mnt_ns_record(void *priv __attribute__((unused)),
88 struct lttng_ust_ring_buffer_ctx *ctx,
89 struct lttng_ust_channel_buffer *chan)
90 {
91 ino_t mnt_ns;
92
93 mnt_ns = get_mnt_ns();
94 chan->ops->event_write(ctx, &mnt_ns, sizeof(mnt_ns), lttng_ust_rb_alignof(mnt_ns));
95 }
96
97 static
98 void mnt_ns_get_value(void *priv __attribute__((unused)),
99 struct lttng_ust_ctx_value *value)
100 {
101 value->u.s64 = get_mnt_ns();
102 }
103
104 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
105 lttng_ust_static_event_field("mnt_ns",
106 lttng_ust_static_type_integer(sizeof(ino_t) * CHAR_BIT,
107 lttng_ust_rb_alignof(ino_t) * CHAR_BIT,
108 lttng_ust_is_signed_type(ino_t),
109 LTTNG_UST_BYTE_ORDER, 10),
110 false, false),
111 mnt_ns_get_size,
112 mnt_ns_record,
113 mnt_ns_get_value,
114 NULL, NULL);
115
116 int lttng_add_mnt_ns_to_ctx(struct lttng_ust_ctx **ctx)
117 {
118 int ret;
119
120 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
121 ret = -EEXIST;
122 goto error_find_context;
123 }
124 ret = lttng_ust_context_append(ctx, ctx_field);
125 if (ret)
126 return ret;
127 return 0;
128
129 error_find_context:
130 return ret;
131 }
This page took 0.030674 seconds and 3 git commands to generate.