Fix: invalid discarded events on start/stop without event production
[lttng-tools.git] / src / common / compat / socket.h
CommitLineData
b17231c6 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
b17231c6 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
b17231c6 5 *
b17231c6
DG
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
fbb1fd3a
MJ
16#ifndef MSG_NOSIGNAL
17# ifdef SO_NOSIGPIPE
18# define MSG_NOSIGNAL SO_NOSIGPIPE
19# endif
20#endif
21
22#if defined(MSG_NOSIGNAL)
23static inline
24ssize_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
33static inline
34ssize_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
b17231c6
DG
101#ifdef __linux__
102
103#define LTTNG_SOCK_CREDS SCM_CREDENTIALS
b17231c6
DG
104
105typedef struct ucred lttng_sock_cred;
106
1268b9d6
DG
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
b17231c6 114
d05d6fe1 115#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
b17231c6 116
d27c42b8
MD
117struct lttng_sock_cred {
118 uid_t uid;
119 gid_t gid;
120};
b17231c6 121
d27c42b8 122typedef struct lttng_sock_cred lttng_sock_cred;
b17231c6 123
d27c42b8
MD
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
b17231c6
DG
127
128#else
7657ae75 129#error "Please add support for your OS."
b17231c6
DG
130#endif /* __linux__ , __FreeBSD__ */
131
28105b32
MJ
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>
11a2fd46
MJ
152
153static inline
154int getpeereid(int s, uid_t *euid, gid_t *gid)
28105b32
MJ
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;
176free:
177 ucred_free(ucred);
178end:
179 return ret;
180}
181
182#endif /* __sun__ */
183
b17231c6 184#endif /* _COMPAT_SOCKET_H */
This page took 0.054338 seconds and 4 git commands to generate.