| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-context-hostname.c |
| 4 | * |
| 5 | * LTTng hostname context. |
| 6 | * |
| 7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/utsname.h> |
| 14 | #include <lttng/events.h> |
| 15 | #include <lttng/events-internal.h> |
| 16 | #include <ringbuffer/frontend_types.h> |
| 17 | #include <wrapper/vmalloc.h> |
| 18 | #include <lttng/tracer.h> |
| 19 | |
| 20 | #define LTTNG_HOSTNAME_CTX_LEN (__NEW_UTS_LEN + 1) |
| 21 | |
| 22 | static |
| 23 | size_t hostname_get_size(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, size_t offset) |
| 24 | { |
| 25 | size_t size = 0; |
| 26 | |
| 27 | size += LTTNG_HOSTNAME_CTX_LEN; |
| 28 | return size; |
| 29 | } |
| 30 | |
| 31 | static |
| 32 | void hostname_record(void *priv, struct lttng_kernel_probe_ctx *probe_ctx, |
| 33 | struct lttng_kernel_ring_buffer_ctx *ctx, |
| 34 | struct lttng_kernel_channel_buffer *chan) |
| 35 | { |
| 36 | struct nsproxy *nsproxy; |
| 37 | struct uts_namespace *ns; |
| 38 | char *hostname; |
| 39 | |
| 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; |
| 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, 1); |
| 50 | } else { |
| 51 | chan->ops->event_memset(ctx, 0, |
| 52 | LTTNG_HOSTNAME_CTX_LEN); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | void hostname_get_value(void *priv, |
| 58 | struct lttng_kernel_probe_ctx *lttng_probe_ctx, |
| 59 | struct 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->u.str = hostname; |
| 77 | } |
| 78 | |
| 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), |
| 83 | hostname_get_size, |
| 84 | hostname_record, |
| 85 | hostname_get_value, |
| 86 | NULL, NULL); |
| 87 | |
| 88 | int lttng_add_hostname_to_ctx(struct lttng_kernel_ctx **ctx) |
| 89 | { |
| 90 | int ret; |
| 91 | |
| 92 | if (lttng_kernel_find_context(*ctx, ctx_field->event_field->name)) |
| 93 | return -EEXIST; |
| 94 | ret = lttng_kernel_context_append(ctx, ctx_field); |
| 95 | wrapper_vmalloc_sync_mappings(); |
| 96 | return ret; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(lttng_add_hostname_to_ctx); |