Namespace remaining symbols in lttng/ringbuffer-context.h
[lttng-ust.git] / liblttng-ust / lttng-context-pid-ns.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
6 *
7 * LTTng UST pid namespace context.
8 */
9
10 #define _LGPL_SOURCE
11 #include <limits.h>
12 #include <stddef.h>
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>
18 #include <lttng/ringbuffer-context.h>
19
20 #include "context-internal.h"
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 PID namespace is global to the process.
26 */
27 static ino_t cached_pid_ns = NS_INO_UNINITIALIZED;
28
29 static
30 ino_t get_pid_ns(void)
31 {
32 struct stat sb;
33 ino_t pid_ns;
34
35 pid_ns = CMM_LOAD_SHARED(cached_pid_ns);
36
37 /*
38 * If the cache is populated, do nothing and return the
39 * cached inode number.
40 */
41 if (caa_likely(pid_ns != NS_INO_UNINITIALIZED))
42 return pid_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 pid_ns = NS_INO_UNAVAILABLE;
51
52 if (stat("/proc/self/ns/pid", &sb) == 0) {
53 pid_ns = sb.st_ino;
54 }
55
56 /*
57 * And finally, store the inode number in the cache.
58 */
59 CMM_STORE_SHARED(cached_pid_ns, pid_ns);
60
61 return pid_ns;
62 }
63
64 /*
65 * A process's PID namespace membership is determined when the process is
66 * created and cannot be changed thereafter.
67 *
68 * The pid namespace can change only on clone(2) / fork(2) :
69 * - clone(2) with the CLONE_NEWPID flag
70 * - clone(2) / fork(2) after a call to unshare(2) with the CLONE_NEWPID flag
71 * - clone(2) / fork(2) after a call to setns(2) with a PID namespace fd
72 */
73 void lttng_context_pid_ns_reset(void)
74 {
75 CMM_STORE_SHARED(cached_pid_ns, NS_INO_UNINITIALIZED);
76 }
77
78 static
79 size_t pid_ns_get_size(struct lttng_ust_ctx_field *field, size_t offset)
80 {
81 size_t size = 0;
82
83 size += lttng_ust_lib_ring_buffer_align(offset, lttng_alignof(ino_t));
84 size += sizeof(ino_t);
85 return size;
86 }
87
88 static
89 void pid_ns_record(struct lttng_ust_ctx_field *field,
90 struct lttng_ust_lib_ring_buffer_ctx *ctx,
91 struct lttng_ust_channel_buffer *chan)
92 {
93 ino_t pid_ns;
94
95 pid_ns = get_pid_ns();
96 lttng_ust_lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid_ns));
97 chan->ops->event_write(ctx, &pid_ns, sizeof(pid_ns));
98 }
99
100 static
101 void pid_ns_get_value(struct lttng_ust_ctx_field *field,
102 struct lttng_ust_ctx_value *value)
103 {
104 value->u.s64 = get_pid_ns();
105 }
106
107 int lttng_add_pid_ns_to_ctx(struct lttng_ust_ctx **ctx)
108 {
109 struct lttng_ust_ctx_field *field;
110 struct lttng_ust_type_common *type;
111 int ret;
112
113 type = lttng_ust_create_type_integer(sizeof(ino_t) * CHAR_BIT,
114 lttng_alignof(ino_t) * CHAR_BIT,
115 lttng_ust_is_signed_type(ino_t),
116 BYTE_ORDER, 10);
117 if (!type)
118 return -ENOMEM;
119 field = lttng_append_context(ctx);
120 if (!field) {
121 ret = -ENOMEM;
122 goto error_context;
123 }
124 if (lttng_find_context(*ctx, "pid_ns")) {
125 ret = -EEXIST;
126 goto error_find_context;
127 }
128 field->event_field->name = strdup("pid_ns");
129 if (!field->event_field->name) {
130 ret = -ENOMEM;
131 goto error_name;
132 }
133 field->event_field->type = type;
134 field->get_size = pid_ns_get_size;
135 field->record = pid_ns_record;
136 field->get_value = pid_ns_get_value;
137 lttng_context_update(*ctx);
138 return 0;
139
140 error_name:
141 error_find_context:
142 lttng_remove_context_field(ctx, field);
143 error_context:
144 lttng_ust_destroy_type(type);
145 return ret;
146 }
This page took 0.033421 seconds and 5 git commands to generate.