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 <sys/socket.h>
15 #include <common/macros.hpp>
19 # define MSG_NOSIGNAL SO_NOSIGPIPE
23 #if defined(MSG_NOSIGNAL)
25 ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg)
27 return recvmsg(sockfd, msg, MSG_NOSIGNAL);
32 #include <common/compat/errno.hpp>
35 ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg)
39 sigset_t sigpipe_set, pending_set, old_set;
40 int sigpipe_was_pending;
43 * Discard the SIGPIPE from send(), not disturbing any SIGPIPE
44 * that might be already pending. If a bogus SIGPIPE is sent to
45 * the entire process concurrently by a malicious user, it may
46 * be simply discarded.
48 if (sigemptyset(&pending_set)) {
52 * sigpending returns the mask of signals that are _both_
53 * blocked for the thread _and_ pending for either the thread or
56 if (sigpending(&pending_set)) {
59 sigpipe_was_pending = sigismember(&pending_set, SIGPIPE);
61 * If sigpipe was pending, it means it was already blocked, so
62 * no need to block it.
64 if (!sigpipe_was_pending) {
65 if (sigemptyset(&sigpipe_set)) {
68 if (sigaddset(&sigpipe_set, SIGPIPE)) {
71 if (pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set)) {
76 /* Send and save errno. */
77 received = recvmsg(sockfd, msg, 0);
80 if (received == -1 && errno == EPIPE && !sigpipe_was_pending) {
81 struct timespec timeout = { 0, 0 };
85 ret = sigtimedwait(&sigpipe_set, NULL,
87 } while (ret == -1 && errno == EINTR);
89 if (!sigpipe_was_pending) {
90 if (pthread_sigmask(SIG_SETMASK, &old_set, NULL)) {
94 /* Restore send() errno */
104 # ifdef _CMSG_DATA_ALIGN
105 # define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len)
107 /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */
108 # define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & ~(sizeof (long) - 1))
111 # define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + CMSG_ALIGN (len))
114 # define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
121 int getpeereid(int s, uid_t *euid, gid_t *gid)
124 ucred_t *ucred = NULL;
126 ret = getpeerucred(s, &ucred);
131 ret = ucred_geteuid(ucred);
137 ret = ucred_getrgid(ucred);
152 #if defined(__linux__) || defined(__CYGWIN__)
154 #define LTTNG_SOCK_CREDS SCM_CREDENTIALS
156 typedef struct ucred lttng_sock_cred;
158 #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
159 #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
160 #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p
162 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
163 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
164 #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid
166 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
168 struct lttng_sock_cred {
174 typedef struct lttng_sock_cred lttng_sock_cred;
176 #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
177 #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
178 #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p
180 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
181 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
182 #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid
187 int lttng_get_unix_socket_peer_pid(int socket_fd, pid_t *pid)
189 socklen_t pid_len = (socklen_t) sizeof(*pid);
191 /* The getsockopt LOCAL_PEERPID option is available since macOS 10.8. */
192 return getsockopt(socket_fd, SOL_LOCAL, LOCAL_PEERPID, pid, &pid_len);
195 #elif defined(__sun__)
197 /* Use the getpeerucreds interface on Solaris. */
199 int lttng_get_unix_socket_peer_pid(int socket_fd, pid_t *pid)
202 ucred_t *ucred = NULL;
204 ret = getpeerucred(s, &ucred);
209 ret = ucred_getpid(ucred);
222 #elif defined(__FreeBSD__)
224 #include <sys/ucred.h>
227 int lttng_get_unix_socket_peer_pid(int socket_fd, pid_t *pid)
230 struct xucred sock_creds = {};
232 /* Only available in FreeBSD 13.0 and up. */
233 ret = getsockopt(socket_fd, SOL_LOCAL, LOCAL_PEERCRED, &sock_creds,
234 &((socklen_t) {sizeof(sock_creds)}));
239 *pid = sock_creds.cr_pid;
244 #endif /* __APPLE__ */
248 int lttng_get_unix_socket_peer_creds(int socket_fd, struct lttng_sock_cred *creds)
252 /* This is a BSD extension that is supported by Cygwin. */
253 ret = getpeereid(socket_fd, &creds->uid, &creds->gid);
259 * Getting a peer's PID is a bit more troublesome as it is platform
262 ret = lttng_get_unix_socket_peer_pid(socket_fd, &creds->pid);
268 #error "Please add support for your OS."
269 #endif /* __linux__ , __FreeBSD__, __APPLE__ */
271 #endif /* _COMPAT_SOCKET_H */