Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / compat.h
... / ...
CommitLineData
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#include <lttng/ust-abi.h>
17
18#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
19
20
21#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
22static inline
23int lttng_pthread_setname_np(const char *name)
24{
25 /*
26 * Some implementations don't error out, replicate this behavior for
27 * consistency.
28 */
29 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
30 return ERANGE;
31 }
32
33 return pthread_setname_np(pthread_self(), name);
34}
35
36static inline
37int lttng_pthread_getname_np(char *name, size_t len)
38{
39 return pthread_getname_np(pthread_self(), name, len);
40}
41#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
42static inline
43int lttng_pthread_setname_np(const char *name)
44{
45 return pthread_setname_np(name);
46}
47
48static inline
49int lttng_pthread_getname_np(char *name, size_t len)
50{
51 return pthread_getname_np(name, len);
52}
53#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
54
55#include <pthread_np.h>
56static inline
57int lttng_pthread_setname_np(const char *name)
58{
59 /* Replicate pthread_setname_np's behavior */
60 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
61 return ERANGE;
62 }
63
64 pthread_set_name_np(pthread_self(), name);
65 return 0;
66}
67
68static inline
69int lttng_pthread_getname_np(char *name, size_t len)
70{
71 pthread_get_name_np(pthread_self(), name, len);
72 return 0;
73}
74#elif defined(__linux__)
75
76/* Fallback on prtctl on Linux */
77#include <sys/prctl.h>
78
79static inline
80int lttng_pthread_setname_np(const char *name)
81{
82 /* Replicate pthread_setname_np's behavior */
83 if (strnlen(name, LTTNG_UST_ABI_PROCNAME_LEN) >= LTTNG_UST_ABI_PROCNAME_LEN) {
84 return ERANGE;
85 }
86 return prctl(PR_SET_NAME, name, 0, 0, 0);
87}
88
89static inline
90int lttng_pthread_getname_np(char *name, size_t len)
91{
92 return prctl(PR_GET_NAME, name, 0, 0, 0);
93}
94
95#else
96#error "Please add pthread name support for your OS."
97#endif
98
99/*
100 * If a pthread setname/set_name function is available, declare
101 * the setustprocname() function that will add '-ust' to the end
102 * of the current process name, while truncating it if needed.
103 */
104static inline
105int lttng_ust_setustprocname(void)
106{
107 int ret = 0, len;
108 char name[LTTNG_UST_ABI_PROCNAME_LEN];
109 int limit = LTTNG_UST_ABI_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1;
110
111 /*
112 * Get the current thread name.
113 */
114 ret = lttng_pthread_getname_np(name, LTTNG_UST_ABI_PROCNAME_LEN);
115 if (ret) {
116 goto error;
117 }
118
119 len = strlen(name);
120 if (len > limit) {
121 len = limit;
122 }
123
124 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
125 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
126 goto error;
127 }
128
129 ret = lttng_pthread_setname_np(name);
130
131error:
132 return ret;
133}
134
135#include <errno.h>
136
137#ifndef ENODATA
138#define ENODATA ENOMSG
139#endif
140
141#endif /* _UST_COMPAT_H */
This page took 0.022515 seconds and 4 git commands to generate.