2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
7 * LTTng UST mnt namespace context.
13 #include <sys/types.h>
16 #include <lttng/ust-events.h>
17 #include <lttng/ust-tracer.h>
18 #include <lttng/ringbuffer-context.h>
20 #include "context-internal.h"
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.
27 static ino_t cached_mnt_ns
= NS_INO_UNINITIALIZED
;
30 ino_t
get_mnt_ns(void)
35 mnt_ns
= CMM_LOAD_SHARED(cached_mnt_ns
);
38 * If the cache is populated, do nothing and return the
39 * cached inode number.
41 if (caa_likely(mnt_ns
!= NS_INO_UNINITIALIZED
))
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
50 mnt_ns
= NS_INO_UNAVAILABLE
;
52 if (stat("/proc/self/ns/mnt", &sb
) == 0) {
57 * And finally, store the inode number in the cache.
59 CMM_STORE_SHARED(cached_mnt_ns
, mnt_ns
);
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
70 void lttng_context_mnt_ns_reset(void)
72 CMM_STORE_SHARED(cached_mnt_ns
, NS_INO_UNINITIALIZED
);
76 size_t mnt_ns_get_size(struct lttng_ust_ctx_field
*field
, size_t offset
)
80 size
+= lttng_ust_lib_ring_buffer_align(offset
, lttng_ust_rb_alignof(ino_t
));
81 size
+= sizeof(ino_t
);
86 void mnt_ns_record(struct lttng_ust_ctx_field
*field
,
87 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
88 struct lttng_ust_channel_buffer
*chan
)
92 mnt_ns
= get_mnt_ns();
93 lttng_ust_lib_ring_buffer_align_ctx(ctx
, lttng_ust_rb_alignof(mnt_ns
));
94 chan
->ops
->event_write(ctx
, &mnt_ns
, sizeof(mnt_ns
));
98 void mnt_ns_get_value(struct lttng_ust_ctx_field
*field
,
99 struct lttng_ust_ctx_value
*value
)
101 value
->u
.s64
= get_mnt_ns();
104 int lttng_add_mnt_ns_to_ctx(struct lttng_ust_ctx
**ctx
)
106 struct lttng_ust_ctx_field
*field
;
107 struct lttng_ust_type_common
*type
;
110 type
= lttng_ust_create_type_integer(sizeof(ino_t
) * CHAR_BIT
,
111 lttng_ust_rb_alignof(ino_t
) * CHAR_BIT
,
112 lttng_ust_is_signed_type(ino_t
),
116 field
= lttng_append_context(ctx
);
121 if (lttng_find_context(*ctx
, "mnt_ns")) {
123 goto error_find_context
;
125 field
->event_field
->name
= strdup("mnt_ns");
126 if (!field
->event_field
->name
) {
130 field
->event_field
->type
= type
;
131 field
->get_size
= mnt_ns_get_size
;
132 field
->record
= mnt_ns_record
;
133 field
->get_value
= mnt_ns_get_value
;
134 lttng_context_update(*ctx
);
139 lttng_remove_context_field(ctx
, field
);
141 lttng_ust_destroy_type(type
);