Fix: add missing errno.h in pthread compat
[lttng-tools.git] / src / common / compat / socket.h
1 /*
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _COMPAT_SOCKET_H
9 #define _COMPAT_SOCKET_H
10
11 #include <sys/socket.h>
12 #include <sys/un.h>
13
14 #include <common/macros.h>
15
16 #ifndef MSG_NOSIGNAL
17 # ifdef SO_NOSIGPIPE
18 # define MSG_NOSIGNAL SO_NOSIGPIPE
19 # endif
20 #endif
21
22 #if defined(MSG_NOSIGNAL)
23 static inline
24 ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg)
25 {
26 return recvmsg(sockfd, msg, MSG_NOSIGNAL);
27 }
28 #else
29
30 #include <signal.h>
31 #include <errno.h>
32
33 static inline
34 ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg)
35 {
36 ssize_t received;
37 int saved_err;
38 sigset_t sigpipe_set, pending_set, old_set;
39 int sigpipe_was_pending;
40
41 /*
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.
46 */
47 if (sigemptyset(&pending_set)) {
48 return -1;
49 }
50 /*
51 * sigpending returns the mask of signals that are _both_
52 * blocked for the thread _and_ pending for either the thread or
53 * the entire process.
54 */
55 if (sigpending(&pending_set)) {
56 return -1;
57 }
58 sigpipe_was_pending = sigismember(&pending_set, SIGPIPE);
59 /*
60 * If sigpipe was pending, it means it was already blocked, so
61 * no need to block it.
62 */
63 if (!sigpipe_was_pending) {
64 if (sigemptyset(&sigpipe_set)) {
65 return -1;
66 }
67 if (sigaddset(&sigpipe_set, SIGPIPE)) {
68 return -1;
69 }
70 if (pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set)) {
71 return -1;
72 }
73 }
74
75 /* Send and save errno. */
76 received = recvmsg(sockfd, msg, 0);
77 saved_err = errno;
78
79 if (received == -1 && errno == EPIPE && !sigpipe_was_pending) {
80 struct timespec timeout = { 0, 0 };
81 int ret;
82
83 do {
84 ret = sigtimedwait(&sigpipe_set, NULL,
85 &timeout);
86 } while (ret == -1 && errno == EINTR);
87 }
88 if (!sigpipe_was_pending) {
89 if (pthread_sigmask(SIG_SETMASK, &old_set, NULL)) {
90 return -1;
91 }
92 }
93 /* Restore send() errno */
94 errno = saved_err;
95
96 return received;
97 }
98 #endif
99
100
101 #ifdef __linux__
102
103 #define LTTNG_SOCK_CREDS SCM_CREDENTIALS
104
105 typedef struct ucred lttng_sock_cred;
106
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
110
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
114
115 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
116
117 struct lttng_sock_cred {
118 uid_t uid;
119 gid_t gid;
120 };
121
122 typedef struct lttng_sock_cred lttng_sock_cred;
123
124 #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
125 #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
126 #define LTTNG_SOCK_GET_PID_CRED(c) -1
127
128 #else
129 #error "Please add support for your OS."
130 #endif /* __linux__ , __FreeBSD__ */
131
132
133 #ifdef __sun__
134
135 # ifndef CMSG_ALIGN
136 # ifdef _CMSG_DATA_ALIGN
137 # define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len)
138 # else
139 /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */
140 # define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & ~(sizeof (long) - 1))
141 # endif
142 # ifndef CMSG_SPACE
143 # define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + CMSG_ALIGN (len))
144 # endif
145 # ifndef CMSG_LEN
146 # define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
147 # endif
148 # endif
149
150
151 #include <ucred.h>
152
153 static inline
154 int getpeereid(int s, uid_t *euid, gid_t *gid)
155 {
156 int ret = 0;
157 ucred_t *ucred = NULL;
158
159 ret = getpeerucred(s, &ucred);
160 if (ret == -1) {
161 goto end;
162 }
163
164 ret = ucred_geteuid(ucred);
165 if (ret == -1) {
166 goto free;
167 }
168 *euid = ret;
169
170 ret = ucred_getrgid(ucred);
171 if (ret == -1) {
172 goto free;
173 }
174 *gid = ret;
175 ret = 0;
176 free:
177 ucred_free(ucred);
178 end:
179 return ret;
180 }
181
182 #endif /* __sun__ */
183
184 #endif /* _COMPAT_SOCKET_H */
This page took 0.043991 seconds and 4 git commands to generate.