| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
| 6 | * |
| 7 | * LTTng UST user namespace context. |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <stddef.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <unistd.h> |
| 15 | #include <lttng/ust-events.h> |
| 16 | #include <lttng/ust-tracer.h> |
| 17 | #include <lttng/ringbuffer-config.h> |
| 18 | |
| 19 | #include "context-internal.h" |
| 20 | #include "ns.h" |
| 21 | |
| 22 | /* |
| 23 | * We cache the result to ensure we don't stat(2) the proc filesystem on |
| 24 | * each event. The user namespace is global to the process. |
| 25 | */ |
| 26 | static ino_t cached_user_ns = NS_INO_UNINITIALIZED; |
| 27 | |
| 28 | static |
| 29 | ino_t get_user_ns(void) |
| 30 | { |
| 31 | struct stat sb; |
| 32 | ino_t user_ns; |
| 33 | |
| 34 | user_ns = CMM_LOAD_SHARED(cached_user_ns); |
| 35 | |
| 36 | /* |
| 37 | * If the cache is populated, do nothing and return the |
| 38 | * cached inode number. |
| 39 | */ |
| 40 | if (caa_likely(user_ns != NS_INO_UNINITIALIZED)) |
| 41 | return user_ns; |
| 42 | |
| 43 | /* |
| 44 | * At this point we have to populate the cache, set the initial |
| 45 | * value to NS_INO_UNAVAILABLE (0), if we fail to get the inode |
| 46 | * number from the proc filesystem, this is the value we will |
| 47 | * cache. |
| 48 | */ |
| 49 | user_ns = NS_INO_UNAVAILABLE; |
| 50 | |
| 51 | if (stat("/proc/self/ns/user", &sb) == 0) { |
| 52 | user_ns = sb.st_ino; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * And finally, store the inode number in the cache. |
| 57 | */ |
| 58 | CMM_STORE_SHARED(cached_user_ns, user_ns); |
| 59 | |
| 60 | return user_ns; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * The user namespace can change for 3 reasons |
| 65 | * * clone(2) called with CLONE_NEWUSER |
| 66 | * * setns(2) called with the fd of a different user ns |
| 67 | * * unshare(2) called with CLONE_NEWUSER |
| 68 | */ |
| 69 | void lttng_context_user_ns_reset(void) |
| 70 | { |
| 71 | CMM_STORE_SHARED(cached_user_ns, NS_INO_UNINITIALIZED); |
| 72 | } |
| 73 | |
| 74 | static |
| 75 | size_t user_ns_get_size(struct lttng_ctx_field *field, size_t offset) |
| 76 | { |
| 77 | size_t size = 0; |
| 78 | |
| 79 | size += lib_ring_buffer_align(offset, lttng_alignof(ino_t)); |
| 80 | size += sizeof(ino_t); |
| 81 | return size; |
| 82 | } |
| 83 | |
| 84 | static |
| 85 | void user_ns_record(struct lttng_ctx_field *field, |
| 86 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 87 | struct lttng_channel *chan) |
| 88 | { |
| 89 | ino_t user_ns; |
| 90 | |
| 91 | user_ns = get_user_ns(); |
| 92 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(user_ns)); |
| 93 | chan->ops->event_write(ctx, &user_ns, sizeof(user_ns)); |
| 94 | } |
| 95 | |
| 96 | static |
| 97 | void user_ns_get_value(struct lttng_ctx_field *field, |
| 98 | struct lttng_ctx_value *value) |
| 99 | { |
| 100 | value->u.s64 = get_user_ns(); |
| 101 | } |
| 102 | |
| 103 | int lttng_add_user_ns_to_ctx(struct lttng_ctx **ctx) |
| 104 | { |
| 105 | struct lttng_ctx_field *field; |
| 106 | |
| 107 | field = lttng_append_context(ctx); |
| 108 | if (!field) |
| 109 | return -ENOMEM; |
| 110 | if (lttng_find_context(*ctx, "user_ns")) { |
| 111 | lttng_remove_context_field(ctx, field); |
| 112 | return -EEXIST; |
| 113 | } |
| 114 | field->event_field.name = "user_ns"; |
| 115 | field->event_field.type.atype = atype_integer; |
| 116 | field->event_field.type.u.integer.size = sizeof(ino_t) * CHAR_BIT; |
| 117 | field->event_field.type.u.integer.alignment = lttng_alignof(ino_t) * CHAR_BIT; |
| 118 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(ino_t); |
| 119 | field->event_field.type.u.integer.reverse_byte_order = 0; |
| 120 | field->event_field.type.u.integer.base = 10; |
| 121 | field->event_field.type.u.integer.encoding = lttng_encode_none; |
| 122 | field->get_size = user_ns_get_size; |
| 123 | field->record = user_ns_record; |
| 124 | field->get_value = user_ns_get_value; |
| 125 | lttng_context_update(*ctx); |
| 126 | return 0; |
| 127 | } |