Refactoring: type description structures
[lttng-modules.git] / src / lttng-context-hostname.c
CommitLineData
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
22static
23size_t hostname_get_size(size_t offset)
24{
25 size_t size = 0;
26
27 size += LTTNG_HOSTNAME_CTX_LEN;
28 return size;
29}
30
31static
437d5aa5 32void hostname_record(struct lttng_kernel_ctx_field *field,
975da2c0
JD
33 struct lib_ring_buffer_ctx *ctx,
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 56static
437d5aa5 57void hostname_get_value(struct lttng_kernel_ctx_field *field,
79150a49 58 struct lttng_probe_ctx *lttng_probe_ctx,
f127e61e
MD
59 union lttng_ctx_value *value)
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 }
76 value->str = hostname;
77}
78
437d5aa5
MD
79static 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,
84 NULL,
85 hostname_record,
86 hostname_get_value,
87 NULL, NULL);
ceabb767 88
437d5aa5 89int lttng_add_hostname_to_ctx(struct lttng_kernel_ctx **ctx)
975da2c0 90{
437d5aa5 91 int ret;
975da2c0 92
437d5aa5 93 if (lttng_kernel_find_context(*ctx, ctx_field->event_field->name))
975da2c0 94 return -EEXIST;
437d5aa5 95 ret = lttng_kernel_context_append(ctx, ctx_field);
263b6c88 96 wrapper_vmalloc_sync_mappings();
437d5aa5 97 return ret;
975da2c0
JD
98}
99EXPORT_SYMBOL_GPL(lttng_add_hostname_to_ctx);
This page took 0.044137 seconds and 4 git commands to generate.