fix: pthread_setname_np fails on longer tread names
[deliverable/lttng-ust.git] / liblttng-ust / compat.h
... / ...
CommitLineData
1#ifndef _UST_COMPAT_H
2#define _UST_COMPAT_H
3
4/*
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Copyright (C) 2016 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
7 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <pthread.h>
25#include <errno.h>
26#include <string.h>
27
28#include <lttng/ust-abi.h>
29
30#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
31
32
33#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
34static inline
35int lttng_pthread_setname_np(const char *name)
36{
37 return pthread_setname_np(pthread_self(), name);
38}
39
40static inline
41int lttng_pthread_getname_np(char *name, size_t len)
42{
43 return pthread_getname_np(pthread_self(), name, len);
44}
45#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
46static inline
47int lttng_pthread_setname_np(const char *name)
48{
49 return pthread_setname_np(name);
50}
51
52static inline
53int lttng_pthread_getname_np(char *name, size_t len)
54{
55 return pthread_getname_np(name, len);
56}
57#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
58
59#include <pthread_np.h>
60static inline
61int lttng_pthread_setname_np(const char *name)
62{
63 /* Replicate pthread_setname_np's behavior */
64 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
65 return ERANGE;
66 }
67
68 pthread_set_name_np(pthread_self(), name);
69 return 0;
70}
71
72static inline
73int lttng_pthread_getname_np(char *name, size_t len)
74{
75 pthread_get_name_np(pthread_self(), name, len);
76 return 0;
77}
78#elif defined(__linux__)
79
80/* Fallback on prtctl on Linux */
81#include <sys/prctl.h>
82
83static inline
84int lttng_pthread_setname_np(const char *name)
85{
86 /* Replicate pthread_setname_np's behavior */
87 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
88 return ERANGE;
89 }
90 return prctl(PR_SET_NAME, name, 0, 0, 0);
91}
92
93static inline
94int lttng_pthread_getname_np(char *name, size_t len)
95{
96 return prctl(PR_GET_NAME, name, 0, 0, 0);
97}
98
99#else
100#error "Please add pthread name support for your OS."
101#endif
102
103/*
104 * If a pthread setname/set_name function is available, declare
105 * the setustprocname() function that will add '-ust' to the end
106 * of the current process name, while truncating it if needed.
107 */
108static inline
109int lttng_ust_setustprocname(void)
110{
111 int ret = 0, len;
112 char name[LTTNG_UST_ABI_PROCNAME_LEN];
113 int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
114
115 /*
116 * Get the current thread name.
117 */
118 ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN);
119 if (ret) {
120 goto error;
121 }
122
123 len = strlen(name);
124 if (len > limit) {
125 len = limit;
126 }
127
128 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
129 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
130 goto error;
131 }
132
133 ret = lttng_pthread_setname_np(name);
134
135error:
136 return ret;
137}
138
139#include <errno.h>
140
141#ifndef ENODATA
142#define ENODATA ENOMSG
143#endif
144
145#endif /* _UST_COMPAT_H */
This page took 0.0261 seconds and 5 git commands to generate.