| 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 | /* |
| 24 | * lttng_ust_getprocname. |
| 25 | */ |
| 26 | #ifdef __linux__ |
| 27 | |
| 28 | #include <sys/prctl.h> |
| 29 | |
| 30 | #define LTTNG_UST_PROCNAME_LEN 17 |
| 31 | |
| 32 | static inline |
| 33 | void lttng_ust_getprocname(char *name) |
| 34 | { |
| 35 | (void) prctl(PR_GET_NAME, (unsigned long) name, 0, 0, 0); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * If pthread_setname_np is available. |
| 40 | */ |
| 41 | #ifdef HAVE_PTHREAD_SETNAME_NP |
| 42 | static inline |
| 43 | int lttng_pthread_setname_np(pthread_t thread, const char *name) |
| 44 | { |
| 45 | return pthread_setname_np(thread, name); |
| 46 | } |
| 47 | #endif |
| 48 | |
| 49 | #elif defined(__FreeBSD__) |
| 50 | |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | |
| 54 | /* |
| 55 | * Limit imposed by Linux UST-sessiond ABI. |
| 56 | */ |
| 57 | #define LTTNG_UST_PROCNAME_LEN 17 |
| 58 | |
| 59 | /* |
| 60 | * Acts like linux prctl, the string is not necessarily 0-terminated if |
| 61 | * 16-byte long. |
| 62 | */ |
| 63 | static inline |
| 64 | void lttng_ust_getprocname(char *name) |
| 65 | { |
| 66 | const char *bsd_name; |
| 67 | |
| 68 | bsd_name = getprogname(); |
| 69 | if (!bsd_name) |
| 70 | name[0] = '\0'; |
| 71 | else |
| 72 | strncpy(name, bsd_name, LTTNG_UST_PROCNAME_LEN - 1); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * If pthread_set_name_np is available. |
| 77 | */ |
| 78 | #ifdef HAVE_PTHREAD_SET_NAME_NP |
| 79 | static inline |
| 80 | int lttng_pthread_setname_np(pthread_t thread, const char *name) |
| 81 | { |
| 82 | return pthread_set_name_np(thread, name); |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | /* |
| 89 | * If a pthread setname/set_name function is available, declare |
| 90 | * the setustprocname() function that will add '-ust' to the end |
| 91 | * of the current process name, while truncating it if needed. |
| 92 | */ |
| 93 | #if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SETNAME_NP) |
| 94 | #define LTTNG_UST_PROCNAME_SUFFIX "-ust" |
| 95 | |
| 96 | #include <pthread.h> |
| 97 | |
| 98 | static inline |
| 99 | int lttng_ust_setustprocname(void) |
| 100 | { |
| 101 | int ret = 0, len; |
| 102 | char name[LTTNG_UST_PROCNAME_LEN]; |
| 103 | int limit = LTTNG_UST_PROCNAME_LEN - strlen(LTTNG_UST_PROCNAME_SUFFIX) - 1; |
| 104 | |
| 105 | lttng_ust_getprocname(name); |
| 106 | |
| 107 | len = strlen(name); |
| 108 | if (len > limit) { |
| 109 | len = limit; |
| 110 | } |
| 111 | |
| 112 | ret = sprintf(name + len, LTTNG_UST_PROCNAME_SUFFIX); |
| 113 | if (ret != strlen(LTTNG_UST_PROCNAME_SUFFIX)) { |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | ret = lttng_pthread_setname_np(pthread_self(), name); |
| 118 | |
| 119 | error: |
| 120 | return ret; |
| 121 | } |
| 122 | #else |
| 123 | static inline |
| 124 | int lttng_ust_setustprocname(void) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #include <errno.h> |
| 131 | |
| 132 | #ifndef ENODATA |
| 133 | #define ENODATA ENOMSG |
| 134 | #endif |
| 135 | |
| 136 | #endif /* _UST_COMPAT_H */ |