| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * Copyright (C) 2016 Raphaƫl Beamonte <raphael.beamonte@gmail.com> |
| 6 | * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> |
| 7 | */ |
| 8 | |
| 9 | #ifndef _UST_COMPAT_H |
| 10 | #define _UST_COMPAT_H |
| 11 | |
| 12 | #include <pthread.h> |
| 13 | #include <errno.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #ifdef __FreeBSD__ |
| 17 | #include <pthread_np.h> |
| 18 | #endif |
| 19 | |
| 20 | #include <lttng/ust-abi.h> |
| 21 | |
| 22 | #define LTTNG_UST_PROCNAME_SUFFIX "-ust" |
| 23 | |
| 24 | |
| 25 | #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) |
| 26 | static inline |
| 27 | int lttng_pthread_setname_np(const char *name) |
| 28 | { |
| 29 | /* |
| 30 | * Some implementations don't error out, replicate this behavior for |
| 31 | * consistency. |
| 32 | */ |
| 33 | if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { |
| 34 | return ERANGE; |
| 35 | } |
| 36 | |
| 37 | return pthread_setname_np(pthread_self(), name); |
| 38 | } |
| 39 | #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) |
| 40 | static inline |
| 41 | int lttng_pthread_setname_np(const char *name) |
| 42 | { |
| 43 | return pthread_setname_np(name); |
| 44 | } |
| 45 | #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID) |
| 46 | |
| 47 | static inline |
| 48 | int lttng_pthread_setname_np(const char *name) |
| 49 | { |
| 50 | /* Replicate pthread_setname_np's behavior */ |
| 51 | if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { |
| 52 | return ERANGE; |
| 53 | } |
| 54 | |
| 55 | pthread_set_name_np(pthread_self(), name); |
| 56 | return 0; |
| 57 | } |
| 58 | #elif defined(__linux__) |
| 59 | |
| 60 | /* Fallback on prtctl on Linux */ |
| 61 | #include <sys/prctl.h> |
| 62 | |
| 63 | static inline |
| 64 | int lttng_pthread_setname_np(const char *name) |
| 65 | { |
| 66 | /* Replicate pthread_setname_np's behavior */ |
| 67 | if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) { |
| 68 | return ERANGE; |
| 69 | } |
| 70 | return prctl(PR_SET_NAME, name, 0, 0, 0); |
| 71 | } |
| 72 | #else |
| 73 | #error "Please add pthread set name support for your OS." |
| 74 | #endif |
| 75 | |
| 76 | |
| 77 | #if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID) |
| 78 | static inline |
| 79 | int lttng_pthread_getname_np(char *name, size_t len) |
| 80 | { |
| 81 | return pthread_getname_np(pthread_self(), name, len); |
| 82 | } |
| 83 | #elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID) |
| 84 | static inline |
| 85 | int lttng_pthread_getname_np(char *name, size_t len) |
| 86 | { |
| 87 | return pthread_getname_np(name, len); |
| 88 | } |
| 89 | #elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID) |
| 90 | |
| 91 | static inline |
| 92 | int lttng_pthread_getname_np(char *name, size_t len) |
| 93 | { |
| 94 | pthread_get_name_np(pthread_self(), name, len); |
| 95 | return 0; |
| 96 | } |
| 97 | #elif defined(__linux__) |
| 98 | |
| 99 | /* Fallback on prtctl on Linux */ |
| 100 | #include <sys/prctl.h> |
| 101 | |
| 102 | static inline |
| 103 | int lttng_pthread_getname_np(char *name, size_t len) |
| 104 | { |
| 105 | return prctl(PR_GET_NAME, name, 0, 0, 0); |
| 106 | } |
| 107 | |
| 108 | #else |
| 109 | #error "Please add pthread get name support for your OS." |
| 110 | #endif |
| 111 | |
| 112 | /* |
| 113 | * If a pthread setname/set_name function is available, declare |
| 114 | * the setustprocname() function that will add '-ust' to the end |
| 115 | * of the current process name, while truncating it if needed. |
| 116 | */ |
| 117 | static inline |
| 118 | int lttng_ust_setustprocname(void) |
| 119 | { |
| 120 | int ret = 0, len; |
| 121 | char name[LTTNG_UST_ABI_PROCNAME_LEN]; |
| 122 | int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1; |
| 123 | |
| 124 | /* |
| 125 | * Get the current thread name. |
| 126 | */ |
| 127 | ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN); |
| 128 | if (ret) { |
| 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | len = strlen(name); |
| 133 | if (len > limit) { |
| 134 | len = limit; |
| 135 | } |
| 136 | |
| 137 | ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX); |
| 138 | if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) { |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | ret = lttng_pthread_setname_np(name); |
| 143 | |
| 144 | error: |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | #include <errno.h> |
| 149 | |
| 150 | #ifndef ENODATA |
| 151 | #define ENODATA ENOMSG |
| 152 | #endif |
| 153 | |
| 154 | #endif /* _UST_COMPAT_H */ |