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 | |
9af5d97a | 11 | #include <limits.h> |
b4051ad8 | 12 | #include <stddef.h> |
735bef47 MJ |
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> | |
0b4b8811 | 18 | #include <lttng/ust-ringbuffer-context.h> |
735bef47 | 19 | |
fc80554e | 20 | #include "context-internal.h" |
c5e8ef85 | 21 | #include "common/ns.h" |
735bef47 MJ |
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 | |
4e48b5d2 | 79 | size_t pid_ns_get_size(void *priv __attribute__((unused)), |
b2e37d27 | 80 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
2208d8b5 | 81 | size_t offset) |
735bef47 MJ |
82 | { |
83 | size_t size = 0; | |
84 | ||
b5457df5 | 85 | size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(ino_t)); |
735bef47 MJ |
86 | size += sizeof(ino_t); |
87 | return size; | |
88 | } | |
89 | ||
90 | static | |
4e48b5d2 | 91 | void pid_ns_record(void *priv __attribute__((unused)), |
b2e37d27 MD |
92 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
93 | struct lttng_ust_ring_buffer_ctx *ctx, | |
94 | struct lttng_ust_channel_buffer *chan) | |
735bef47 MJ |
95 | { |
96 | ino_t pid_ns; | |
97 | ||
98 | pid_ns = get_pid_ns(); | |
8936b6c0 | 99 | chan->ops->event_write(ctx, &pid_ns, sizeof(pid_ns), lttng_ust_rb_alignof(pid_ns)); |
735bef47 MJ |
100 | } |
101 | ||
102 | static | |
4e48b5d2 | 103 | void pid_ns_get_value(void *priv __attribute__((unused)), |
b2e37d27 | 104 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
daacdbfc | 105 | struct lttng_ust_ctx_value *value) |
735bef47 | 106 | { |
b2e37d27 | 107 | value->u.u64 = get_pid_ns(); |
735bef47 MJ |
108 | } |
109 | ||
4e48b5d2 MD |
110 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
111 | lttng_ust_static_event_field("pid_ns", | |
112 | lttng_ust_static_type_integer(sizeof(ino_t) * CHAR_BIT, | |
113 | lttng_ust_rb_alignof(ino_t) * CHAR_BIT, | |
114 | lttng_ust_is_signed_type(ino_t), | |
baa8acf3 | 115 | LTTNG_UST_BYTE_ORDER, 10), |
4e48b5d2 MD |
116 | false, false), |
117 | pid_ns_get_size, | |
118 | pid_ns_record, | |
119 | pid_ns_get_value, | |
120 | NULL, NULL); | |
121 | ||
daacdbfc | 122 | int lttng_add_pid_ns_to_ctx(struct lttng_ust_ctx **ctx) |
735bef47 | 123 | { |
a084756d MD |
124 | int ret; |
125 | ||
4e48b5d2 | 126 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
a084756d MD |
127 | ret = -EEXIST; |
128 | goto error_find_context; | |
129 | } | |
4e48b5d2 MD |
130 | ret = lttng_ust_context_append(ctx, ctx_field); |
131 | if (ret) | |
132 | return ret; | |
735bef47 | 133 | return 0; |
a084756d | 134 | |
a084756d | 135 | error_find_context: |
a084756d | 136 | return ret; |
735bef47 | 137 | } |