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