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