2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #ifndef _COMPAT_SOCKET_H
9 #define _COMPAT_SOCKET_H
11 #include <common/macros.hpp>
13 #include <sys/socket.h>
19 #define MSG_NOSIGNAL SO_NOSIGPIPE
23 #if defined(MSG_NOSIGNAL)
24 static inline ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg)
26 return recvmsg(sockfd, msg, MSG_NOSIGNAL);
30 #include <common/compat/errno.hpp>
34 static inline ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg)
38 sigset_t sigpipe_set, pending_set, old_set;
39 int sigpipe_was_pending;
42 * Discard the SIGPIPE from send(), not disturbing any SIGPIPE
43 * that might be already pending. If a bogus SIGPIPE is sent to
44 * the entire process concurrently by a malicious user, it may
45 * be simply discarded.
47 if (sigemptyset(&pending_set)) {
51 * sigpending returns the mask of signals that are _both_
52 * blocked for the thread _and_ pending for either the thread or
55 if (sigpending(&pending_set)) {
58 sigpipe_was_pending = sigismember(&pending_set, SIGPIPE);
60 * If sigpipe was pending, it means it was already blocked, so
61 * no need to block it.
63 if (!sigpipe_was_pending) {
64 if (sigemptyset(&sigpipe_set)) {
67 if (sigaddset(&sigpipe_set, SIGPIPE)) {
70 if (pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set)) {
75 /* Send and save errno. */
76 received = recvmsg(sockfd, msg, 0);
79 if (received == -1 && errno == EPIPE && !sigpipe_was_pending) {
80 struct timespec timeout = { 0, 0 };
84 ret = sigtimedwait(&sigpipe_set, NULL, &timeout);
85 } while (ret == -1 && errno == EINTR);
87 if (!sigpipe_was_pending) {
88 if (pthread_sigmask(SIG_SETMASK, &old_set, NULL)) {
92 /* Restore send() errno */
102 #ifdef _CMSG_DATA_ALIGN
103 #define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len)
105 /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */
106 #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
109 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
112 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
118 static inline int getpeereid(int s, uid_t *euid, gid_t *gid)
121 ucred_t *ucred = NULL;
123 ret = getpeerucred(s, &ucred);
128 ret = ucred_geteuid(ucred);
134 ret = ucred_getrgid(ucred);
148 #if defined(__linux__) || defined(__CYGWIN__)
150 #define LTTNG_SOCK_CREDS SCM_CREDENTIALS
152 using lttng_sock_cred = struct ucred;
154 #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
155 #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
156 #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p
158 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
159 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
160 #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid
162 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
164 struct lttng_sock_cred {
170 typedef struct lttng_sock_cred lttng_sock_cred;
172 #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
173 #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
174 #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p
176 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
177 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
178 #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid
182 static inline int lttng_get_unix_socket_peer_pid(int socket_fd, pid_t *pid)
184 socklen_t pid_len = (socklen_t) sizeof(*pid);
186 /* The getsockopt LOCAL_PEERPID option is available since macOS 10.8. */
187 return getsockopt(socket_fd, SOL_LOCAL, LOCAL_PEERPID, pid, &pid_len);
190 #elif defined(__sun__)
192 /* Use the getpeerucreds interface on Solaris. */
193 static inline int lttng_get_unix_socket_peer_pid(int socket_fd, pid_t *pid)
196 ucred_t *ucred = NULL;
198 ret = getpeerucred(s, &ucred);
203 ret = ucred_getpid(ucred);
216 #elif defined(__FreeBSD__)
218 #include <sys/ucred.h>
220 static inline int lttng_get_unix_socket_peer_pid(int socket_fd, pid_t *pid)
223 struct xucred sock_creds = {};
224 socklen_t sock_creds_len = (socklen_t) sizeof(sock_creds);
226 /* Only available in FreeBSD 13.0 and up. */
227 ret = getsockopt(socket_fd, SOL_LOCAL, LOCAL_PEERCRED, &sock_creds, &sock_creds_len);
232 *pid = sock_creds.cr_pid;
237 #endif /* __APPLE__ */
239 static inline int lttng_get_unix_socket_peer_creds(int socket_fd, struct lttng_sock_cred *creds)
243 /* This is a BSD extension that is supported by Cygwin. */
244 ret = getpeereid(socket_fd, &creds->uid, &creds->gid);
250 * Getting a peer's PID is a bit more troublesome as it is platform
253 ret = lttng_get_unix_socket_peer_pid(socket_fd, &creds->pid);
259 #error "Please add support for your OS."
260 #endif /* __linux__ , __FreeBSD__, __APPLE__ */
262 #endif /* _COMPAT_SOCKET_H */