Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
a6cf40a4 MJ |
2 | * |
3 | * lttng-context-uts-ns.c | |
4 | * | |
5 | * LTTng uts 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/utsname.h> | |
16 | #include <lttng-events.h> | |
17 | #include <wrapper/ringbuffer/frontend_types.h> | |
a6cf40a4 MJ |
18 | #include <lttng-tracer.h> |
19 | ||
9f920259 | 20 | #if defined(CONFIG_UTS_NS) |
a6cf40a4 MJ |
21 | |
22 | static | |
23 | size_t uts_ns_get_size(size_t offset) | |
24 | { | |
25 | size_t size = 0; | |
26 | ||
27 | size += lib_ring_buffer_align(offset, lttng_alignof(unsigned int)); | |
28 | size += sizeof(unsigned int); | |
29 | return size; | |
30 | } | |
31 | ||
32 | static | |
33 | void uts_ns_record(struct lttng_ctx_field *field, | |
34 | struct lib_ring_buffer_ctx *ctx, | |
35 | struct lttng_channel *chan) | |
36 | { | |
37 | unsigned int uts_ns_inum = 0; | |
38 | ||
39 | /* | |
40 | * nsproxy can be NULL when scheduled out of exit. | |
41 | * | |
42 | * As documented in 'linux/nsproxy.h' namespaces access rules, no | |
43 | * precautions should be taken when accessing the current task's | |
44 | * namespaces, just dereference the pointers. | |
45 | */ | |
46 | if (current->nsproxy) | |
6388029c | 47 | uts_ns_inum = current->nsproxy->uts_ns->ns.inum; |
a6cf40a4 MJ |
48 | |
49 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uts_ns_inum)); | |
50 | chan->ops->event_write(ctx, &uts_ns_inum, sizeof(uts_ns_inum)); | |
51 | } | |
52 | ||
53 | static | |
54 | void uts_ns_get_value(struct lttng_ctx_field *field, | |
55 | struct lttng_probe_ctx *lttng_probe_ctx, | |
56 | union lttng_ctx_value *value) | |
57 | { | |
58 | unsigned int uts_ns_inum = 0; | |
59 | ||
60 | /* | |
61 | * nsproxy can be NULL when scheduled out of exit. | |
62 | * | |
63 | * As documented in 'linux/nsproxy.h' namespaces access rules, no | |
64 | * precautions should be taken when accessing the current task's | |
65 | * namespaces, just dereference the pointers. | |
66 | */ | |
67 | if (current->nsproxy) | |
6388029c | 68 | uts_ns_inum = current->nsproxy->uts_ns->ns.inum; |
a6cf40a4 MJ |
69 | |
70 | value->s64 = uts_ns_inum; | |
71 | } | |
72 | ||
73 | int lttng_add_uts_ns_to_ctx(struct lttng_ctx **ctx) | |
74 | { | |
75 | struct lttng_ctx_field *field; | |
76 | ||
77 | field = lttng_append_context(ctx); | |
78 | if (!field) | |
79 | return -ENOMEM; | |
80 | if (lttng_find_context(*ctx, "uts_ns")) { | |
81 | lttng_remove_context_field(ctx, field); | |
82 | return -EEXIST; | |
83 | } | |
84 | field->event_field.name = "uts_ns"; | |
85 | field->event_field.type.atype = atype_integer; | |
ceabb767 MD |
86 | field->event_field.type.u.integer.size = sizeof(unsigned int) * CHAR_BIT; |
87 | field->event_field.type.u.integer.alignment = lttng_alignof(unsigned int) * CHAR_BIT; | |
88 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(unsigned int); | |
89 | field->event_field.type.u.integer.reverse_byte_order = 0; | |
90 | field->event_field.type.u.integer.base = 10; | |
91 | field->event_field.type.u.integer.encoding = lttng_encode_none; | |
a6cf40a4 MJ |
92 | field->get_size = uts_ns_get_size; |
93 | field->record = uts_ns_record; | |
94 | field->get_value = uts_ns_get_value; | |
95 | lttng_context_update(*ctx); | |
a6cf40a4 MJ |
96 | return 0; |
97 | } | |
98 | EXPORT_SYMBOL_GPL(lttng_add_uts_ns_to_ctx); | |
99 | ||
100 | #endif |