Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
975da2c0 JD |
3 | * lttng-context-hostname.c |
4 | * | |
5 | * LTTng hostname context. | |
6 | * | |
7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
975da2c0 JD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/sched.h> | |
13 | #include <linux/utsname.h> | |
241ae9a8 | 14 | #include <lttng-events.h> |
5760f3f4 | 15 | #include <include/ringbuffer/frontend_types.h> |
241ae9a8 | 16 | #include <lttng-tracer.h> |
975da2c0 JD |
17 | |
18 | #define LTTNG_HOSTNAME_CTX_LEN (__NEW_UTS_LEN + 1) | |
19 | ||
20 | static | |
21 | size_t hostname_get_size(size_t offset) | |
22 | { | |
23 | size_t size = 0; | |
24 | ||
25 | size += LTTNG_HOSTNAME_CTX_LEN; | |
26 | return size; | |
27 | } | |
28 | ||
29 | static | |
30 | void hostname_record(struct lttng_ctx_field *field, | |
31 | struct lib_ring_buffer_ctx *ctx, | |
32 | struct lttng_channel *chan) | |
33 | { | |
34 | struct nsproxy *nsproxy; | |
35 | struct uts_namespace *ns; | |
36 | char *hostname; | |
37 | ||
3d0d43db MD |
38 | /* |
39 | * No need to take the RCU read-side lock to read current | |
40 | * nsproxy. (documented in nsproxy.h) | |
41 | */ | |
42 | nsproxy = current->nsproxy; | |
975da2c0 JD |
43 | if (nsproxy) { |
44 | ns = nsproxy->uts_ns; | |
45 | hostname = ns->name.nodename; | |
46 | chan->ops->event_write(ctx, hostname, | |
47 | LTTNG_HOSTNAME_CTX_LEN); | |
48 | } else { | |
49 | chan->ops->event_memset(ctx, 0, | |
50 | LTTNG_HOSTNAME_CTX_LEN); | |
51 | } | |
975da2c0 JD |
52 | } |
53 | ||
f127e61e MD |
54 | static |
55 | void hostname_get_value(struct lttng_ctx_field *field, | |
79150a49 | 56 | struct lttng_probe_ctx *lttng_probe_ctx, |
f127e61e MD |
57 | union lttng_ctx_value *value) |
58 | { | |
59 | struct nsproxy *nsproxy; | |
60 | struct uts_namespace *ns; | |
61 | char *hostname; | |
62 | ||
63 | /* | |
64 | * No need to take the RCU read-side lock to read current | |
65 | * nsproxy. (documented in nsproxy.h) | |
66 | */ | |
67 | nsproxy = current->nsproxy; | |
68 | if (nsproxy) { | |
69 | ns = nsproxy->uts_ns; | |
70 | hostname = ns->name.nodename; | |
71 | } else { | |
72 | hostname = ""; | |
73 | } | |
74 | value->str = hostname; | |
75 | } | |
76 | ||
ceabb767 MD |
77 | static const struct lttng_type hostname_array_elem_type = |
78 | __type_integer(char, 0, 0, -1, __BYTE_ORDER, 10, UTF8); | |
79 | ||
975da2c0 JD |
80 | int lttng_add_hostname_to_ctx(struct lttng_ctx **ctx) |
81 | { | |
82 | struct lttng_ctx_field *field; | |
83 | ||
84 | field = lttng_append_context(ctx); | |
85 | if (!field) | |
86 | return -ENOMEM; | |
87 | if (lttng_find_context(*ctx, "hostname")) { | |
88 | lttng_remove_context_field(ctx, field); | |
89 | return -EEXIST; | |
90 | } | |
91 | field->event_field.name = "hostname"; | |
ceabb767 MD |
92 | field->event_field.type.atype = atype_array_nestable; |
93 | field->event_field.type.u.array_nestable.elem_type = | |
94 | &hostname_array_elem_type; | |
95 | field->event_field.type.u.array_nestable.length = LTTNG_HOSTNAME_CTX_LEN; | |
96 | field->event_field.type.u.array_nestable.alignment = 0; | |
975da2c0 JD |
97 | |
98 | field->get_size = hostname_get_size; | |
99 | field->record = hostname_record; | |
f127e61e | 100 | field->get_value = hostname_get_value; |
a9dd15da | 101 | lttng_context_update(*ctx); |
975da2c0 JD |
102 | return 0; |
103 | } | |
104 | EXPORT_SYMBOL_GPL(lttng_add_hostname_to_ctx); |