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> | |
2df37e95 | 14 | #include <lttng/events.h> |
437d5aa5 | 15 | #include <lttng/events-internal.h> |
24591303 | 16 | #include <ringbuffer/frontend_types.h> |
241ae9a8 | 17 | #include <wrapper/vmalloc.h> |
2df37e95 | 18 | #include <lttng/tracer.h> |
975da2c0 JD |
19 | |
20 | #define LTTNG_HOSTNAME_CTX_LEN (__NEW_UTS_LEN + 1) | |
21 | ||
22 | static | |
a92e844e | 23 | size_t hostname_get_size(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, size_t offset) |
975da2c0 JD |
24 | { |
25 | size_t size = 0; | |
26 | ||
27 | size += LTTNG_HOSTNAME_CTX_LEN; | |
28 | return size; | |
29 | } | |
30 | ||
31 | static | |
a92e844e | 32 | void hostname_record(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, |
8a57ec02 | 33 | struct lttng_kernel_ring_buffer_ctx *ctx, |
975da2c0 JD |
34 | struct lttng_channel *chan) |
35 | { | |
36 | struct nsproxy *nsproxy; | |
37 | struct uts_namespace *ns; | |
38 | char *hostname; | |
39 | ||
3d0d43db MD |
40 | /* |
41 | * No need to take the RCU read-side lock to read current | |
42 | * nsproxy. (documented in nsproxy.h) | |
43 | */ | |
44 | nsproxy = current->nsproxy; | |
975da2c0 JD |
45 | if (nsproxy) { |
46 | ns = nsproxy->uts_ns; | |
47 | hostname = ns->name.nodename; | |
48 | chan->ops->event_write(ctx, hostname, | |
49 | LTTNG_HOSTNAME_CTX_LEN); | |
50 | } else { | |
51 | chan->ops->event_memset(ctx, 0, | |
52 | LTTNG_HOSTNAME_CTX_LEN); | |
53 | } | |
975da2c0 JD |
54 | } |
55 | ||
f127e61e | 56 | static |
2dc781e0 | 57 | void hostname_get_value(void *priv, |
a92e844e | 58 | struct lttng_kernel_probe_ctx *lttng_probe_ctx, |
2dc781e0 | 59 | struct lttng_ctx_value *value) |
f127e61e MD |
60 | { |
61 | struct nsproxy *nsproxy; | |
62 | struct uts_namespace *ns; | |
63 | char *hostname; | |
64 | ||
65 | /* | |
66 | * No need to take the RCU read-side lock to read current | |
67 | * nsproxy. (documented in nsproxy.h) | |
68 | */ | |
69 | nsproxy = current->nsproxy; | |
70 | if (nsproxy) { | |
71 | ns = nsproxy->uts_ns; | |
72 | hostname = ns->name.nodename; | |
73 | } else { | |
74 | hostname = ""; | |
75 | } | |
2dc781e0 | 76 | value->u.str = hostname; |
f127e61e MD |
77 | } |
78 | ||
437d5aa5 MD |
79 | static const struct lttng_kernel_ctx_field *ctx_field = lttng_kernel_static_ctx_field( |
80 | lttng_kernel_static_event_field("hostname", | |
81 | lttng_kernel_static_type_array_text(LTTNG_HOSTNAME_CTX_LEN), | |
82 | false, false, false), | |
83 | hostname_get_size, | |
437d5aa5 MD |
84 | hostname_record, |
85 | hostname_get_value, | |
86 | NULL, NULL); | |
ceabb767 | 87 | |
437d5aa5 | 88 | int lttng_add_hostname_to_ctx(struct lttng_kernel_ctx **ctx) |
975da2c0 | 89 | { |
437d5aa5 | 90 | int ret; |
975da2c0 | 91 | |
437d5aa5 | 92 | if (lttng_kernel_find_context(*ctx, ctx_field->event_field->name)) |
975da2c0 | 93 | return -EEXIST; |
437d5aa5 | 94 | ret = lttng_kernel_context_append(ctx, ctx_field); |
263b6c88 | 95 | wrapper_vmalloc_sync_mappings(); |
437d5aa5 | 96 | return ret; |
975da2c0 JD |
97 | } |
98 | EXPORT_SYMBOL_GPL(lttng_add_hostname_to_ctx); |