cleanup: explicitly mark unused parameters (-Wunused-parameter)
[lttng-ust.git] / liblttng-ust / lttng-context-mnt-ns.c
CommitLineData
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
9af5d97a 11#include <limits.h>
b4051ad8 12#include <stddef.h>
735bef47
MJ
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>
0466ac28 18#include <lttng/ringbuffer-context.h>
735bef47 19
fc80554e 20#include "context-internal.h"
735bef47
MJ
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 mount namespace is global to the process.
26 */
27static ino_t cached_mnt_ns = NS_INO_UNINITIALIZED;
28
29static
30ino_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 */
70void lttng_context_mnt_ns_reset(void)
71{
72 CMM_STORE_SHARED(cached_mnt_ns, NS_INO_UNINITIALIZED);
73}
74
75static
2208d8b5
MJ
76size_t mnt_ns_get_size(struct lttng_ust_ctx_field *field __attribute__((unused)),
77 size_t offset)
735bef47
MJ
78{
79 size_t size = 0;
80
dc325c1d 81 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(ino_t));
735bef47
MJ
82 size += sizeof(ino_t);
83 return size;
84}
85
86static
2208d8b5 87void mnt_ns_record(struct lttng_ust_ctx_field *field __attribute__((unused)),
735bef47 88 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 89 struct lttng_ust_channel_buffer *chan)
735bef47
MJ
90{
91 ino_t mnt_ns;
92
93 mnt_ns = get_mnt_ns();
8936b6c0 94 chan->ops->event_write(ctx, &mnt_ns, sizeof(mnt_ns), lttng_ust_rb_alignof(mnt_ns));
735bef47
MJ
95}
96
97static
2208d8b5 98void mnt_ns_get_value(struct lttng_ust_ctx_field *field __attribute__((unused)),
daacdbfc 99 struct lttng_ust_ctx_value *value)
735bef47
MJ
100{
101 value->u.s64 = get_mnt_ns();
102}
103
daacdbfc 104int lttng_add_mnt_ns_to_ctx(struct lttng_ust_ctx **ctx)
735bef47 105{
daacdbfc 106 struct lttng_ust_ctx_field *field;
a084756d
MD
107 struct lttng_ust_type_common *type;
108 int ret;
109
110 type = lttng_ust_create_type_integer(sizeof(ino_t) * CHAR_BIT,
dc325c1d 111 lttng_ust_rb_alignof(ino_t) * CHAR_BIT,
eae3c729 112 lttng_ust_is_signed_type(ino_t),
a084756d
MD
113 BYTE_ORDER, 10);
114 if (!type)
735bef47 115 return -ENOMEM;
a084756d
MD
116 field = lttng_append_context(ctx);
117 if (!field) {
118 ret = -ENOMEM;
119 goto error_context;
120 }
735bef47 121 if (lttng_find_context(*ctx, "mnt_ns")) {
a084756d
MD
122 ret = -EEXIST;
123 goto error_find_context;
124 }
125 field->event_field->name = strdup("mnt_ns");
126 if (!field->event_field->name) {
127 ret = -ENOMEM;
128 goto error_name;
735bef47 129 }
a084756d 130 field->event_field->type = type;
735bef47
MJ
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);
135 return 0;
a084756d
MD
136
137error_name:
138error_find_context:
139 lttng_remove_context_field(ctx, field);
140error_context:
141 lttng_ust_destroy_type(type);
142 return ret;
735bef47 143}
This page took 0.032873 seconds and 4 git commands to generate.