2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
7 * LTTng UST pid namespace context.
12 #include <sys/types.h>
15 #include <lttng/ust-events.h>
16 #include <lttng/ust-tracer.h>
17 #include <lttng/ringbuffer-config.h>
19 #include "context-internal.h"
23 * We cache the result to ensure we don't stat(2) the proc filesystem on
24 * each event. The PID namespace is global to the process.
26 static ino_t cached_pid_ns
= NS_INO_UNINITIALIZED
;
29 ino_t
get_pid_ns(void)
34 pid_ns
= CMM_LOAD_SHARED(cached_pid_ns
);
37 * If the cache is populated, do nothing and return the
38 * cached inode number.
40 if (caa_likely(pid_ns
!= NS_INO_UNINITIALIZED
))
44 * At this point we have to populate the cache, set the initial
45 * value to NS_INO_UNAVAILABLE (0), if we fail to get the inode
46 * number from the proc filesystem, this is the value we will
49 pid_ns
= NS_INO_UNAVAILABLE
;
51 if (stat("/proc/self/ns/pid", &sb
) == 0) {
56 * And finally, store the inode number in the cache.
58 CMM_STORE_SHARED(cached_pid_ns
, pid_ns
);
64 * A process's PID namespace membership is determined when the process is
65 * created and cannot be changed thereafter.
67 * The pid namespace can change only on clone(2) / fork(2) :
68 * - clone(2) with the CLONE_NEWPID flag
69 * - clone(2) / fork(2) after a call to unshare(2) with the CLONE_NEWPID flag
70 * - clone(2) / fork(2) after a call to setns(2) with a PID namespace fd
72 void lttng_context_pid_ns_reset(void)
74 CMM_STORE_SHARED(cached_pid_ns
, NS_INO_UNINITIALIZED
);
78 size_t pid_ns_get_size(struct lttng_ust_ctx_field
*field
, size_t offset
)
82 size
+= lib_ring_buffer_align(offset
, lttng_alignof(ino_t
));
83 size
+= sizeof(ino_t
);
88 void pid_ns_record(struct lttng_ust_ctx_field
*field
,
89 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
90 struct lttng_channel
*chan
)
94 pid_ns
= get_pid_ns();
95 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(pid_ns
));
96 chan
->ops
->event_write(ctx
, &pid_ns
, sizeof(pid_ns
));
100 void pid_ns_get_value(struct lttng_ust_ctx_field
*field
,
101 struct lttng_ust_ctx_value
*value
)
103 value
->u
.s64
= get_pid_ns();
106 int lttng_add_pid_ns_to_ctx(struct lttng_ust_ctx
**ctx
)
108 struct lttng_ust_ctx_field
*field
;
110 field
= lttng_append_context(ctx
);
113 if (lttng_find_context(*ctx
, "pid_ns")) {
114 lttng_remove_context_field(ctx
, field
);
117 field
->event_field
->name
= "pid_ns";
118 field
->event_field
->type
.atype
= atype_integer
;
119 field
->event_field
->type
.u
.integer
.size
= sizeof(ino_t
) * CHAR_BIT
;
120 field
->event_field
->type
.u
.integer
.alignment
= lttng_alignof(ino_t
) * CHAR_BIT
;
121 field
->event_field
->type
.u
.integer
.signedness
= lttng_is_signed_type(ino_t
);
122 field
->event_field
->type
.u
.integer
.reverse_byte_order
= 0;
123 field
->event_field
->type
.u
.integer
.base
= 10;
124 field
->event_field
->type
.u
.integer
.encoding
= lttng_encode_none
;
125 field
->get_size
= pid_ns_get_size
;
126 field
->record
= pid_ns_record
;
127 field
->get_value
= pid_ns_get_value
;
128 lttng_context_update(*ctx
);