Commit | Line | Data |
---|---|---|
b728d87e | 1 | /* |
c0c0989a MJ |
2 | * SPDX-License-Identifier: LGPL-2.1-only |
3 | * | |
e92f3e28 | 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
01f0e40c | 5 | * Copyright (C) 2016 Raphaël Beamonte <raphael.beamonte@gmail.com> |
0db3d6ee | 6 | * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> |
b728d87e MD |
7 | */ |
8 | ||
c0c0989a MJ |
9 | #ifndef _UST_COMPAT_H |
10 | #define _UST_COMPAT_H | |
11 | ||
d9c56a93 | 12 | #include <pthread.h> |
0db3d6ee | 13 | #include <errno.h> |
f2db7517 | 14 | #include <string.h> |
d9c56a93 | 15 | |
653f7d34 MJ |
16 | #ifdef __FreeBSD__ |
17 | #include <pthread_np.h> | |
18 | #endif | |
19 | ||
0db3d6ee | 20 | #include <lttng/ust-abi.h> |
08114193 | 21 | |
d9c56a93 | 22 | #define LTTNG_UST_PROCNAME_SUFFIX "-ust" |
08114193 | 23 | |
08114193 | 24 | |
d9c56a93 | 25 | #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID) |
08114193 | 26 | static inline |
d9c56a93 | 27 | int lttng_pthread_setname_np(const char *name) |
08114193 | 28 | { |
bebc0c82 MJ |
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 | ||
0db3d6ee | 37 | return pthread_setname_np(pthread_self(), name); |
08114193 | 38 | } |
d9c56a93 | 39 | #elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID) |
e4486ebc | 40 | static inline |
d9c56a93 | 41 | int lttng_pthread_setname_np(const char *name) |
e4486ebc | 42 | { |
0db3d6ee | 43 | return pthread_setname_np(name); |
e4486ebc | 44 | } |
0db3d6ee MJ |
45 | #elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID) |
46 | ||
01f0e40c | 47 | static inline |
d9c56a93 | 48 | int lttng_pthread_setname_np(const char *name) |
01f0e40c | 49 | { |
f2db7517 MJ |
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 | ||
0db3d6ee MJ |
55 | pthread_set_name_np(pthread_self(), name); |
56 | return 0; | |
01f0e40c | 57 | } |
0db3d6ee MJ |
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 | { | |
f2db7517 MJ |
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 | } | |
0db3d6ee | 70 | return prctl(PR_SET_NAME, name, 0, 0, 0); |
d9c56a93 | 71 | } |
653f7d34 MJ |
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> | |
01f0e40c | 101 | |
d9c56a93 | 102 | static inline |
0db3d6ee | 103 | int lttng_pthread_getname_np(char *name, size_t len) |
d9c56a93 | 104 | { |
0db3d6ee | 105 | return prctl(PR_GET_NAME, name, 0, 0, 0); |
d9c56a93 | 106 | } |
01f0e40c | 107 | |
0db3d6ee | 108 | #else |
653f7d34 | 109 | #error "Please add pthread get name support for your OS." |
0db3d6ee MJ |
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 | */ | |
01f0e40c RB |
117 | static inline |
118 | int lttng_ust_setustprocname(void) | |
119 | { | |
120 | int ret = 0, len; | |
0db3d6ee MJ |
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 | } | |
01f0e40c RB |
131 | |
132 | len = strlen(name); | |
133 | if (len > limit) { | |
134 | len = limit; | |
135 | } | |
136 | ||
137 | ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX); | |
8db078dc | 138 | if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) { |
01f0e40c RB |
139 | goto error; |
140 | } | |
141 | ||
d9c56a93 | 142 | ret = lttng_pthread_setname_np(name); |
01f0e40c RB |
143 | |
144 | error: | |
145 | return ret; | |
146 | } | |
d9c56a93 | 147 | |
bdcf8d82 MD |
148 | #include <errno.h> |
149 | ||
150 | #ifndef ENODATA | |
151 | #define ENODATA ENOMSG | |
152 | #endif | |
153 | ||
b728d87e | 154 | #endif /* _UST_COMPAT_H */ |