Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust / lttng-context-user-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 user 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 user namespace is global to the process.
26 */
27static ino_t cached_user_ns = NS_INO_UNINITIALIZED;
28
29static
30ino_t get_user_ns(void)
31{
32 struct stat sb;
33 ino_t user_ns;
34
35 user_ns = CMM_LOAD_SHARED(cached_user_ns);
36
37 /*
38 * If the cache is populated, do nothing and return the
39 * cached inode number.
40 */
41 if (caa_likely(user_ns != NS_INO_UNINITIALIZED))
42 return user_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 user_ns = NS_INO_UNAVAILABLE;
51
52 if (stat("/proc/self/ns/user", &sb) == 0) {
53 user_ns = sb.st_ino;
54 }
55
56 /*
57 * And finally, store the inode number in the cache.
58 */
59 CMM_STORE_SHARED(cached_user_ns, user_ns);
60
61 return user_ns;
62}
63
64/*
65 * The user namespace can change for 3 reasons
66 * * clone(2) called with CLONE_NEWUSER
67 * * setns(2) called with the fd of a different user ns
68 * * unshare(2) called with CLONE_NEWUSER
69 */
70void lttng_context_user_ns_reset(void)
71{
72 CMM_STORE_SHARED(cached_user_ns, NS_INO_UNINITIALIZED);
73}
74
75static
4e48b5d2 76size_t user_ns_get_size(void *priv __attribute__((unused)),
2208d8b5 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
4e48b5d2 87void user_ns_record(void *priv __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 user_ns;
92
93 user_ns = get_user_ns();
8936b6c0 94 chan->ops->event_write(ctx, &user_ns, sizeof(user_ns), lttng_ust_rb_alignof(user_ns));
735bef47
MJ
95}
96
97static
4e48b5d2 98void user_ns_get_value(void *priv __attribute__((unused)),
daacdbfc 99 struct lttng_ust_ctx_value *value)
735bef47
MJ
100{
101 value->u.s64 = get_user_ns();
102}
103
4e48b5d2
MD
104static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
105 lttng_ust_static_event_field("user_ns",
106 lttng_ust_static_type_integer(sizeof(ino_t) * CHAR_BIT,
107 lttng_ust_rb_alignof(ino_t) * CHAR_BIT,
108 lttng_ust_is_signed_type(ino_t),
109 BYTE_ORDER, 10),
110 false, false),
111 user_ns_get_size,
112 user_ns_record,
113 user_ns_get_value,
114 NULL, NULL);
115
daacdbfc 116int lttng_add_user_ns_to_ctx(struct lttng_ust_ctx **ctx)
735bef47 117{
a084756d
MD
118 int ret;
119
4e48b5d2 120 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
a084756d
MD
121 ret = -EEXIST;
122 goto error_find_context;
123 }
4e48b5d2
MD
124 ret = lttng_ust_context_append(ctx, ctx_field);
125 if (ret)
126 return ret;
735bef47 127 return 0;
a084756d 128
a084756d 129error_find_context:
a084756d 130 return ret;
735bef47 131}
This page took 0.031728 seconds and 4 git commands to generate.