port: fix pthread autoconf detection to support FreeBSD
[lttng-ust.git] / liblttng-ust / compat.h
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 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <pthread.h>
24
25 /*
26 * Limit imposed by Linux UST-sessiond ABI.
27 */
28 #define LTTNG_UST_PROCNAME_LEN 17
29
30 #define LTTNG_UST_PROCNAME_SUFFIX "-ust"
31
32
33 #if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
34 static inline
35 int lttng_pthread_setname_np(const char *name)
36 {
37 return pthread_setname_np(pthread_self(), name);
38 }
39
40 static inline
41 int 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)
46 static inline
47 int lttng_pthread_setname_np(const char *name)
48 {
49 return pthread_setname_np(name);
50 }
51
52 static inline
53 int lttng_pthread_getname_np(char *name, size_t len)
54 {
55 return pthread_getname_np(name, len);
56 }
57 #else
58 /*
59 * For platforms without thread name support, do nothing.
60 */
61 static inline
62 int lttng_pthread_setname_np(const char *name)
63 {
64 return -ENOSYS;
65 }
66
67 static inline
68 int lttng_pthread_getname_np(char *name, size_t len)
69 {
70 return -ENOSYS;
71 }
72 #endif
73
74 static inline
75 void lttng_ust_getprocname(char *name)
76 {
77 lttng_pthread_getname_np(name, LTTNG_UST_PROCNAME_LEN);
78 }
79
80 static inline
81 int lttng_ust_setustprocname(void)
82 {
83 int ret = 0, len;
84 char name[LTTNG_UST_PROCNAME_LEN];
85 int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
86
87 lttng_pthread_getname_np(name, LTTNG_UST_PROCNAME_LEN);
88
89 len = strlen(name);
90 if (len > limit) {
91 len = limit;
92 }
93
94 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
95 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
96 goto error;
97 }
98
99 ret = lttng_pthread_setname_np(name);
100
101 error:
102 return ret;
103 }
104
105
106 #include <errno.h>
107
108 #ifndef ENODATA
109 #define ENODATA ENOMSG
110 #endif
111
112 #endif /* _UST_COMPAT_H */
This page took 0.032317 seconds and 5 git commands to generate.