| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/pid_namespace.h> |
| 16 | #include <lttng/events.h> |
| 17 | #include <lttng/events-internal.h> |
| 18 | #include <ringbuffer/frontend_types.h> |
| 19 | #include <wrapper/vmalloc.h> |
| 20 | #include <wrapper/namespace.h> |
| 21 | #include <lttng/tracer.h> |
| 22 | |
| 23 | #if defined(CONFIG_PID_NS) && \ |
| 24 | (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,8,0)) |
| 25 | |
| 26 | static |
| 27 | size_t pid_ns_get_size(size_t offset) |
| 28 | { |
| 29 | size_t size = 0; |
| 30 | |
| 31 | size += lib_ring_buffer_align(offset, lttng_alignof(unsigned int)); |
| 32 | size += sizeof(unsigned int); |
| 33 | return size; |
| 34 | } |
| 35 | |
| 36 | static |
| 37 | void pid_ns_record(struct lttng_kernel_ctx_field *field, |
| 38 | struct lib_ring_buffer_ctx *ctx, |
| 39 | struct lttng_channel *chan) |
| 40 | { |
| 41 | struct pid_namespace *ns; |
| 42 | unsigned int pid_ns_inum = 0; |
| 43 | |
| 44 | /* |
| 45 | * The pid namespace is an exception -- it's accessed using |
| 46 | * task_active_pid_ns. The pid namespace in nsproxy is the |
| 47 | * namespace that children will use. |
| 48 | */ |
| 49 | ns = task_active_pid_ns(current); |
| 50 | |
| 51 | if (ns) |
| 52 | pid_ns_inum = ns->lttng_ns_inum; |
| 53 | |
| 54 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid_ns_inum)); |
| 55 | chan->ops->event_write(ctx, &pid_ns_inum, sizeof(pid_ns_inum)); |
| 56 | } |
| 57 | |
| 58 | static |
| 59 | void pid_ns_get_value(struct lttng_kernel_ctx_field *field, |
| 60 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 61 | union lttng_ctx_value *value) |
| 62 | { |
| 63 | struct pid_namespace *ns; |
| 64 | unsigned int pid_ns_inum = 0; |
| 65 | |
| 66 | /* |
| 67 | * The pid namespace is an exception -- it's accessed using |
| 68 | * task_active_pid_ns. The pid namespace in nsproxy is the |
| 69 | * namespace that children will use. |
| 70 | */ |
| 71 | ns = task_active_pid_ns(current); |
| 72 | |
| 73 | if (ns) |
| 74 | pid_ns_inum = ns->lttng_ns_inum; |
| 75 | |
| 76 | value->s64 = pid_ns_inum; |
| 77 | } |
| 78 | |
| 79 | static const struct lttng_kernel_ctx_field *ctx_field = lttng_kernel_static_ctx_field( |
| 80 | lttng_kernel_static_event_field("pid_ns", |
| 81 | lttng_kernel_static_type_integer_from_type(unsigned int, __BYTE_ORDER, 10), |
| 82 | false, false, false), |
| 83 | pid_ns_get_size, |
| 84 | NULL, |
| 85 | pid_ns_record, |
| 86 | pid_ns_get_value, |
| 87 | NULL, NULL); |
| 88 | |
| 89 | int lttng_add_pid_ns_to_ctx(struct lttng_kernel_ctx **ctx) |
| 90 | { |
| 91 | int ret; |
| 92 | |
| 93 | if (lttng_kernel_find_context(*ctx, ctx_field->event_field->name)) |
| 94 | return -EEXIST; |
| 95 | ret = lttng_kernel_context_append(ctx, ctx_field); |
| 96 | wrapper_vmalloc_sync_mappings(); |
| 97 | return ret; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(lttng_add_pid_ns_to_ctx); |
| 100 | |
| 101 | #endif |