2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef _COMPAT_SOCKET_H
9 #define _COMPAT_SOCKET_H
11 #include <sys/socket.h>
14 #include <common/macros.h>
18 # define MSG_NOSIGNAL SO_NOSIGPIPE
22 #if defined(MSG_NOSIGNAL)
24 ssize_t
lttng_recvmsg_nosigpipe(int sockfd
, struct msghdr
*msg
)
26 return recvmsg(sockfd
, msg
, MSG_NOSIGNAL
);
31 #include <common/compat/errno.h>
34 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
,
86 } while (ret
== -1 && errno
== EINTR
);
88 if (!sigpipe_was_pending
) {
89 if (pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
)) {
93 /* Restore send() errno */
103 #define LTTNG_SOCK_CREDS SCM_CREDENTIALS
105 typedef struct ucred lttng_sock_cred
;
107 #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
108 #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
109 #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p
111 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
112 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
113 #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid
115 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
117 struct lttng_sock_cred
{
122 typedef struct lttng_sock_cred lttng_sock_cred
;
124 #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
125 #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
126 #define LTTNG_SOCK_SET_PID_CRED(c, p)
128 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
129 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
130 #define LTTNG_SOCK_GET_PID_CRED(c) -1
133 #error "Please add support for your OS."
134 #endif /* __linux__ , __FreeBSD__ */
140 # ifdef _CMSG_DATA_ALIGN
141 # define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len)
143 /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */
144 # define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & ~(sizeof (long) - 1))
147 # define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + CMSG_ALIGN (len))
150 # define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
158 int getpeereid(int s
, uid_t
*euid
, gid_t
*gid
)
161 ucred_t
*ucred
= NULL
;
163 ret
= getpeerucred(s
, &ucred
);
168 ret
= ucred_geteuid(ucred
);
174 ret
= ucred_getrgid(ucred
);
188 #endif /* _COMPAT_SOCKET_H */