Commit | Line | Data |
---|---|---|
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> | |
0466ac28 | 17 | #include <lttng/ringbuffer-context.h> |
735bef47 | 18 | |
fc80554e | 19 | #include "context-internal.h" |
735bef47 MJ |
20 | #include "ns.h" |
21 | ||
22 | /* | |
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. | |
25 | */ | |
26 | static ino_t cached_pid_ns = NS_INO_UNINITIALIZED; | |
27 | ||
28 | static | |
29 | ino_t get_pid_ns(void) | |
30 | { | |
31 | struct stat sb; | |
32 | ino_t pid_ns; | |
33 | ||
34 | pid_ns = CMM_LOAD_SHARED(cached_pid_ns); | |
35 | ||
36 | /* | |
37 | * If the cache is populated, do nothing and return the | |
38 | * cached inode number. | |
39 | */ | |
40 | if (caa_likely(pid_ns != NS_INO_UNINITIALIZED)) | |
41 | return pid_ns; | |
42 | ||
43 | /* | |
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 | |
47 | * cache. | |
48 | */ | |
49 | pid_ns = NS_INO_UNAVAILABLE; | |
50 | ||
51 | if (stat("/proc/self/ns/pid", &sb) == 0) { | |
52 | pid_ns = sb.st_ino; | |
53 | } | |
54 | ||
55 | /* | |
56 | * And finally, store the inode number in the cache. | |
57 | */ | |
58 | CMM_STORE_SHARED(cached_pid_ns, pid_ns); | |
59 | ||
60 | return pid_ns; | |
61 | } | |
62 | ||
63 | /* | |
64 | * A process's PID namespace membership is determined when the process is | |
65 | * created and cannot be changed thereafter. | |
66 | * | |
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 | |
71 | */ | |
72 | void lttng_context_pid_ns_reset(void) | |
73 | { | |
74 | CMM_STORE_SHARED(cached_pid_ns, NS_INO_UNINITIALIZED); | |
75 | } | |
76 | ||
77 | static | |
daacdbfc | 78 | size_t pid_ns_get_size(struct lttng_ust_ctx_field *field, size_t offset) |
735bef47 MJ |
79 | { |
80 | size_t size = 0; | |
81 | ||
82 | size += lib_ring_buffer_align(offset, lttng_alignof(ino_t)); | |
83 | size += sizeof(ino_t); | |
84 | return size; | |
85 | } | |
86 | ||
87 | static | |
daacdbfc | 88 | void pid_ns_record(struct lttng_ust_ctx_field *field, |
735bef47 | 89 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 90 | struct lttng_ust_channel_buffer *chan) |
735bef47 MJ |
91 | { |
92 | ino_t pid_ns; | |
93 | ||
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)); | |
97 | } | |
98 | ||
99 | static | |
daacdbfc MD |
100 | void pid_ns_get_value(struct lttng_ust_ctx_field *field, |
101 | struct lttng_ust_ctx_value *value) | |
735bef47 MJ |
102 | { |
103 | value->u.s64 = get_pid_ns(); | |
104 | } | |
105 | ||
daacdbfc | 106 | int lttng_add_pid_ns_to_ctx(struct lttng_ust_ctx **ctx) |
735bef47 | 107 | { |
daacdbfc | 108 | struct lttng_ust_ctx_field *field; |
a084756d MD |
109 | struct lttng_ust_type_common *type; |
110 | int ret; | |
111 | ||
112 | type = lttng_ust_create_type_integer(sizeof(ino_t) * CHAR_BIT, | |
113 | lttng_alignof(ino_t) * CHAR_BIT, | |
eae3c729 | 114 | lttng_ust_is_signed_type(ino_t), |
a084756d MD |
115 | BYTE_ORDER, 10); |
116 | if (!type) | |
735bef47 | 117 | return -ENOMEM; |
a084756d MD |
118 | field = lttng_append_context(ctx); |
119 | if (!field) { | |
120 | ret = -ENOMEM; | |
121 | goto error_context; | |
122 | } | |
735bef47 | 123 | if (lttng_find_context(*ctx, "pid_ns")) { |
a084756d MD |
124 | ret = -EEXIST; |
125 | goto error_find_context; | |
126 | } | |
127 | field->event_field->name = strdup("pid_ns"); | |
128 | if (!field->event_field->name) { | |
129 | ret = -ENOMEM; | |
130 | goto error_name; | |
735bef47 | 131 | } |
a084756d | 132 | field->event_field->type = type; |
735bef47 MJ |
133 | field->get_size = pid_ns_get_size; |
134 | field->record = pid_ns_record; | |
135 | field->get_value = pid_ns_get_value; | |
136 | lttng_context_update(*ctx); | |
137 | return 0; | |
a084756d MD |
138 | |
139 | error_name: | |
140 | error_find_context: | |
141 | lttng_remove_context_field(ctx, field); | |
142 | error_context: | |
143 | lttng_ust_destroy_type(type); | |
144 | return ret; | |
735bef47 | 145 | } |