Fix: lttng: add-trigger: invalid access past end of exclusions buffer
[lttng-tools.git] / src / common / compat / pthread.h
CommitLineData
cb8d0d24
MJ
1/*
2 * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#ifndef _COMPAT_PTHREAD_H
9#define _COMPAT_PTHREAD_H
10
11#include <pthread.h>
edf4b93e 12#include <common/compat/errno.h>
014d7d3b
MJ
13#include <string.h>
14
3f3e7eb7
MJ
15#ifdef __FreeBSD__
16#include <pthread_np.h>
17#endif
18
014d7d3b 19#define LTTNG_PTHREAD_NAMELEN 16
cb8d0d24
MJ
20
21#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
22static inline
23int lttng_pthread_setname_np(const char *name)
24{
014d7d3b
MJ
25 /*
26 * Some implementations don't error out, replicate this behavior for
27 * consistency.
28 */
29 if (strnlen(name, LTTNG_PTHREAD_NAMELEN) >= LTTNG_PTHREAD_NAMELEN) {
30 return ERANGE;
31 }
32
cb8d0d24
MJ
33 return pthread_setname_np(pthread_self(), name);
34}
35#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
36static inline
37int lttng_pthread_setname_np(const char *name)
38{
39 return pthread_setname_np(name);
40}
014d7d3b
MJ
41#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
42
014d7d3b
MJ
43static inline
44int lttng_pthread_setname_np(const char *name)
45{
46 /* Replicate pthread_setname_np's behavior. */
47 if (strnlen(name, LTTNG_PTHREAD_NAMELEN) >= LTTNG_PTHREAD_NAMELEN) {
48 return ERANGE;
49 }
50
51 pthread_set_name_np(pthread_self(), name);
52 return 0;
53}
014d7d3b
MJ
54#elif defined(__linux__)
55
56/* Fallback on prtctl on Linux */
57#include <sys/prctl.h>
58
59static inline
60int lttng_pthread_setname_np(const char *name)
61{
62 /* Replicate pthread_setname_np's behavior. */
63 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
64 return ERANGE;
65 }
66 return prctl(PR_SET_NAME, name, 0, 0, 0);
67}
cb8d0d24
MJ
68#else
69/*
70 * For platforms without thread name support, do nothing.
71 */
72static inline
73int lttng_pthread_setname_np(const char *name)
74{
75 return -ENOSYS;
76}
3f3e7eb7
MJ
77#endif
78
014d7d3b 79
3f3e7eb7
MJ
80#if defined(HAVE_PTHREAD_GETNAME_NP_WITH_TID)
81static inline
82int lttng_pthread_getname_np(char *name, size_t len)
83{
84 return pthread_getname_np(pthread_self(), name, len);
85}
86#elif defined(HAVE_PTHREAD_GETNAME_NP_WITHOUT_TID)
87static inline
88int lttng_pthread_getname_np(char *name, size_t len)
89{
90 return pthread_getname_np(name, len);
91}
92#elif defined(HAVE_PTHREAD_GET_NAME_NP_WITH_TID)
93static inline
94int lttng_pthread_getname_np(char *name, size_t len)
95{
96 pthread_get_name_np(pthread_self(), name, len);
97 return 0;
98}
99#elif defined(__linux__)
100
101/* Fallback on prtctl on Linux */
102#include <sys/prctl.h>
103
104static inline
105int lttng_pthread_getname_np(char *name, size_t len)
106{
107 return prctl(PR_GET_NAME, name, 0, 0, 0);
108}
109#else
110/*
111 * For platforms without thread name support, do nothing.
112 */
014d7d3b
MJ
113static inline
114int lttng_pthread_getname_np(char *name, size_t len)
115{
116 return -ENOSYS;
117}
cb8d0d24
MJ
118#endif
119
120#endif /* _COMPAT_PTHREAD_H */
This page took 0.030156 seconds and 4 git commands to generate.