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