Move to kernel style SPDX license identifiers
[deliverable/lttng-ust.git] / liblttng-ust / lttng-context-pid-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 pid 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 PID namespace is global to the process.
24 */
25static ino_t cached_pid_ns = NS_INO_UNINITIALIZED;
26
27static
28ino_t get_pid_ns(void)
29{
30 struct stat sb;
31 ino_t pid_ns;
32
33 pid_ns = CMM_LOAD_SHARED(cached_pid_ns);
34
35 /*
36 * If the cache is populated, do nothing and return the
37 * cached inode number.
38 */
39 if (caa_likely(pid_ns != NS_INO_UNINITIALIZED))
40 return pid_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 pid_ns = NS_INO_UNAVAILABLE;
49
50 if (stat("/proc/self/ns/pid", &sb) == 0) {
51 pid_ns = sb.st_ino;
52 }
53
54 /*
55 * And finally, store the inode number in the cache.
56 */
57 CMM_STORE_SHARED(cached_pid_ns, pid_ns);
58
59 return pid_ns;
60}
61
62/*
63 * A process's PID namespace membership is determined when the process is
64 * created and cannot be changed thereafter.
65 *
66 * The pid namespace can change only on clone(2) / fork(2) :
67 * - clone(2) with the CLONE_NEWPID flag
68 * - clone(2) / fork(2) after a call to unshare(2) with the CLONE_NEWPID flag
69 * - clone(2) / fork(2) after a call to setns(2) with a PID namespace fd
70 */
71void lttng_context_pid_ns_reset(void)
72{
73 CMM_STORE_SHARED(cached_pid_ns, NS_INO_UNINITIALIZED);
74}
75
76static
77size_t pid_ns_get_size(struct lttng_ctx_field *field, size_t offset)
78{
79 size_t size = 0;
80
81 size += lib_ring_buffer_align(offset, lttng_alignof(ino_t));
82 size += sizeof(ino_t);
83 return size;
84}
85
86static
87void pid_ns_record(struct lttng_ctx_field *field,
88 struct lttng_ust_lib_ring_buffer_ctx *ctx,
89 struct lttng_channel *chan)
90{
91 ino_t pid_ns;
92
93 pid_ns = get_pid_ns();
94 lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid_ns));
95 chan->ops->event_write(ctx, &pid_ns, sizeof(pid_ns));
96}
97
98static
99void pid_ns_get_value(struct lttng_ctx_field *field,
100 struct lttng_ctx_value *value)
101{
102 value->u.s64 = get_pid_ns();
103}
104
105int lttng_add_pid_ns_to_ctx(struct lttng_ctx **ctx)
106{
107 struct lttng_ctx_field *field;
108
109 field = lttng_append_context(ctx);
110 if (!field)
111 return -ENOMEM;
112 if (lttng_find_context(*ctx, "pid_ns")) {
113 lttng_remove_context_field(ctx, field);
114 return -EEXIST;
115 }
116 field->event_field.name = "pid_ns";
117 field->event_field.type.atype = atype_integer;
218deb69
MD
118 field->event_field.type.u.integer.size = sizeof(ino_t) * CHAR_BIT;
119 field->event_field.type.u.integer.alignment = lttng_alignof(ino_t) * CHAR_BIT;
120 field->event_field.type.u.integer.signedness = lttng_is_signed_type(ino_t);
121 field->event_field.type.u.integer.reverse_byte_order = 0;
122 field->event_field.type.u.integer.base = 10;
123 field->event_field.type.u.integer.encoding = lttng_encode_none;
735bef47
MJ
124 field->get_size = pid_ns_get_size;
125 field->record = pid_ns_record;
126 field->get_value = pid_ns_get_value;
127 lttng_context_update(*ctx);
128 return 0;
129}
This page took 0.031831 seconds and 5 git commands to generate.