Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / compat.h
CommitLineData
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
0db3d6ee 16#include <lttng/ust-abi.h>
08114193 17
d9c56a93 18#define LTTNG_UST_PROCNAME_SUFFIX "-ust"
08114193 19
08114193 20
d9c56a93 21#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
08114193 22static inline
d9c56a93 23int lttng_pthread_setname_np(const char *name)
08114193 24{
bebc0c82
MJ
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
0db3d6ee 33 return pthread_setname_np(pthread_self(), name);
08114193
MD
34}
35
01f0e40c 36static inline
d9c56a93 37int lttng_pthread_getname_np(char *name, size_t len)
01f0e40c 38{
0db3d6ee 39 return pthread_getname_np(pthread_self(), name, len);
01f0e40c 40}
d9c56a93 41#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
e4486ebc 42static inline
d9c56a93 43int lttng_pthread_setname_np(const char *name)
e4486ebc 44{
0db3d6ee 45 return pthread_setname_np(name);
e4486ebc 46}
08114193 47
d9c56a93
MJ
48static inline
49int lttng_pthread_getname_np(char *name, size_t len)
50{
0db3d6ee 51 return pthread_getname_np(name, len);
d9c56a93 52}
0db3d6ee
MJ
53#elif defined(HAVE_PTHREAD_SET_NAME_NP_WITH_TID)
54
55#include <pthread_np.h>
01f0e40c 56static inline
d9c56a93 57int lttng_pthread_setname_np(const char *name)
01f0e40c 58{
f2db7517
MJ
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
0db3d6ee
MJ
64 pthread_set_name_np(pthread_self(), name);
65 return 0;
01f0e40c 66}
01f0e40c 67
d9c56a93
MJ
68static inline
69int lttng_pthread_getname_np(char *name, size_t len)
70{
0db3d6ee
MJ
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{
f2db7517
MJ
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 }
0db3d6ee 86 return prctl(PR_SET_NAME, name, 0, 0, 0);
d9c56a93 87}
01f0e40c 88
d9c56a93 89static inline
0db3d6ee 90int lttng_pthread_getname_np(char *name, size_t len)
d9c56a93 91{
0db3d6ee 92 return prctl(PR_GET_NAME, name, 0, 0, 0);
d9c56a93 93}
01f0e40c 94
0db3d6ee
MJ
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 */
01f0e40c
RB
104static inline
105int lttng_ust_setustprocname(void)
106{
107 int ret = 0, len;
0db3d6ee
MJ
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 }
01f0e40c
RB
118
119 len = strlen(name);
120 if (len > limit) {
121 len = limit;
122 }
123
124 ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX);
8db078dc 125 if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) {
01f0e40c
RB
126 goto error;
127 }
128
d9c56a93 129 ret = lttng_pthread_setname_np(name);
01f0e40c
RB
130
131error:
132 return ret;
133}
d9c56a93 134
bdcf8d82
MD
135#include <errno.h>
136
137#ifndef ENODATA
138#define ENODATA ENOMSG
139#endif
140
b728d87e 141#endif /* _UST_COMPAT_H */
This page took 0.036567 seconds and 4 git commands to generate.