Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
a6cf40a4 MJ |
2 | * |
3 | * lttng-context-pid-ns.c | |
4 | * | |
5 | * LTTng pid namespace context. | |
6 | * | |
7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * 2019 Michael Jeanson <mjeanson@efficios.com> | |
9 | * | |
10 | */ | |
11 | ||
455d6bcc MJ |
12 | #ifdef CONFIG_PID_NS |
13 | ||
a6cf40a4 MJ |
14 | #include <linux/module.h> |
15 | #include <linux/slab.h> | |
16 | #include <linux/sched.h> | |
17 | #include <linux/pid_namespace.h> | |
2df37e95 | 18 | #include <lttng/events.h> |
437d5aa5 | 19 | #include <lttng/events-internal.h> |
24591303 | 20 | #include <ringbuffer/frontend_types.h> |
a6cf40a4 | 21 | #include <wrapper/vmalloc.h> |
2df37e95 | 22 | #include <lttng/tracer.h> |
a6cf40a4 | 23 | |
a6cf40a4 | 24 | static |
a92e844e | 25 | size_t pid_ns_get_size(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, size_t offset) |
a6cf40a4 MJ |
26 | { |
27 | size_t size = 0; | |
28 | ||
29 | size += lib_ring_buffer_align(offset, lttng_alignof(unsigned int)); | |
30 | size += sizeof(unsigned int); | |
31 | return size; | |
32 | } | |
33 | ||
34 | static | |
a92e844e | 35 | void pid_ns_record(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, |
8a57ec02 | 36 | struct lttng_kernel_ring_buffer_ctx *ctx, |
f7d06400 | 37 | struct lttng_kernel_channel_buffer *chan) |
a6cf40a4 MJ |
38 | { |
39 | struct pid_namespace *ns; | |
40 | unsigned int pid_ns_inum = 0; | |
41 | ||
42 | /* | |
43 | * The pid namespace is an exception -- it's accessed using | |
44 | * task_active_pid_ns. The pid namespace in nsproxy is the | |
45 | * namespace that children will use. | |
46 | */ | |
47 | ns = task_active_pid_ns(current); | |
48 | ||
49 | if (ns) | |
93e5bcec | 50 | pid_ns_inum = ns->ns.inum; |
a6cf40a4 | 51 | |
f5ffbd77 | 52 | chan->ops->event_write(ctx, &pid_ns_inum, sizeof(pid_ns_inum), lttng_alignof(pid_ns_inum)); |
a6cf40a4 MJ |
53 | } |
54 | ||
55 | static | |
2dc781e0 | 56 | void pid_ns_get_value(void *priv, |
a92e844e | 57 | struct lttng_kernel_probe_ctx *lttng_probe_ctx, |
2dc781e0 | 58 | struct lttng_ctx_value *value) |
a6cf40a4 MJ |
59 | { |
60 | struct pid_namespace *ns; | |
61 | unsigned int pid_ns_inum = 0; | |
62 | ||
63 | /* | |
64 | * The pid namespace is an exception -- it's accessed using | |
65 | * task_active_pid_ns. The pid namespace in nsproxy is the | |
66 | * namespace that children will use. | |
67 | */ | |
68 | ns = task_active_pid_ns(current); | |
69 | ||
70 | if (ns) | |
93e5bcec | 71 | pid_ns_inum = ns->ns.inum; |
a6cf40a4 | 72 | |
2dc781e0 | 73 | value->u.s64 = pid_ns_inum; |
a6cf40a4 MJ |
74 | } |
75 | ||
437d5aa5 MD |
76 | static const struct lttng_kernel_ctx_field *ctx_field = lttng_kernel_static_ctx_field( |
77 | lttng_kernel_static_event_field("pid_ns", | |
78 | lttng_kernel_static_type_integer_from_type(unsigned int, __BYTE_ORDER, 10), | |
4697aac7 | 79 | false, false), |
437d5aa5 | 80 | pid_ns_get_size, |
437d5aa5 MD |
81 | pid_ns_record, |
82 | pid_ns_get_value, | |
83 | NULL, NULL); | |
84 | ||
85 | int lttng_add_pid_ns_to_ctx(struct lttng_kernel_ctx **ctx) | |
a6cf40a4 | 86 | { |
437d5aa5 | 87 | int ret; |
a6cf40a4 | 88 | |
437d5aa5 | 89 | if (lttng_kernel_find_context(*ctx, ctx_field->event_field->name)) |
a6cf40a4 | 90 | return -EEXIST; |
437d5aa5 | 91 | ret = lttng_kernel_context_append(ctx, ctx_field); |
263b6c88 | 92 | wrapper_vmalloc_sync_mappings(); |
437d5aa5 | 93 | return ret; |
a6cf40a4 MJ |
94 | } |
95 | EXPORT_SYMBOL_GPL(lttng_add_pid_ns_to_ctx); | |
96 | ||
97 | #endif |