4a690b7d24e84e3b3361eb57679a4cd7000cb8c4
[lttng-ust.git] / liblttng-ust / lttng-context-pid-ns.c
1 /*
2 * lttng-context-pid-ns.c
3 *
4 * LTTng UST pid 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
25 #include <stddef.h>
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 PID namespace is global to the process.
38 */
39 static ino_t cached_pid_ns = NS_INO_UNINITIALIZED;
40
41 static
42 ino_t get_pid_ns(void)
43 {
44 struct stat sb;
45 ino_t pid_ns;
46
47 pid_ns = CMM_LOAD_SHARED(cached_pid_ns);
48
49 /*
50 * If the cache is populated, do nothing and return the
51 * cached inode number.
52 */
53 if (caa_likely(pid_ns != NS_INO_UNINITIALIZED))
54 return pid_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 pid_ns = NS_INO_UNAVAILABLE;
63
64 if (stat("/proc/self/ns/pid", &sb) == 0) {
65 pid_ns = sb.st_ino;
66 }
67
68 /*
69 * And finally, store the inode number in the cache.
70 */
71 CMM_STORE_SHARED(cached_pid_ns, pid_ns);
72
73 return pid_ns;
74 }
75
76 /*
77 * A process's PID namespace membership is determined when the process is
78 * created and cannot be changed thereafter.
79 *
80 * The pid namespace can change only on clone(2) / fork(2) :
81 * - clone(2) with the CLONE_NEWPID flag
82 * - clone(2) / fork(2) after a call to unshare(2) with the CLONE_NEWPID flag
83 * - clone(2) / fork(2) after a call to setns(2) with a PID namespace fd
84 */
85 void lttng_context_pid_ns_reset(void)
86 {
87 CMM_STORE_SHARED(cached_pid_ns, NS_INO_UNINITIALIZED);
88 }
89
90 static
91 size_t pid_ns_get_size(struct lttng_ctx_field *field, size_t offset)
92 {
93 size_t size = 0;
94
95 size += lib_ring_buffer_align(offset, lttng_alignof(ino_t));
96 size += sizeof(ino_t);
97 return size;
98 }
99
100 static
101 void pid_ns_record(struct lttng_ctx_field *field,
102 struct lttng_ust_lib_ring_buffer_ctx *ctx,
103 struct lttng_channel *chan)
104 {
105 ino_t pid_ns;
106
107 pid_ns = get_pid_ns();
108 lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid_ns));
109 chan->ops->event_write(ctx, &pid_ns, sizeof(pid_ns));
110 }
111
112 static
113 void pid_ns_get_value(struct lttng_ctx_field *field,
114 struct lttng_ctx_value *value)
115 {
116 value->u.s64 = get_pid_ns();
117 }
118
119 int lttng_add_pid_ns_to_ctx(struct lttng_ctx **ctx)
120 {
121 struct lttng_ctx_field *field;
122
123 field = lttng_append_context(ctx);
124 if (!field)
125 return -ENOMEM;
126 if (lttng_find_context(*ctx, "pid_ns")) {
127 lttng_remove_context_field(ctx, field);
128 return -EEXIST;
129 }
130 field->event_field.name = "pid_ns";
131 field->event_field.type.atype = atype_integer;
132 field->event_field.type.u.integer.size = sizeof(ino_t) * CHAR_BIT;
133 field->event_field.type.u.integer.alignment = lttng_alignof(ino_t) * CHAR_BIT;
134 field->event_field.type.u.integer.signedness = lttng_is_signed_type(ino_t);
135 field->event_field.type.u.integer.reverse_byte_order = 0;
136 field->event_field.type.u.integer.base = 10;
137 field->event_field.type.u.integer.encoding = lttng_encode_none;
138 field->get_size = pid_ns_get_size;
139 field->record = pid_ns_record;
140 field->get_value = pid_ns_get_value;
141 lttng_context_update(*ctx);
142 return 0;
143 }
This page took 0.03085 seconds and 3 git commands to generate.