unix: receive pid on non-linux platforms
[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>
edf4b93e 31#include <common/compat/errno.h>
fbb1fd3a
MJ
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;
ba66a850 120 pid_t pid;
d27c42b8 121};
b17231c6 122
d27c42b8 123typedef struct lttng_sock_cred lttng_sock_cred;
b17231c6 124
de559cf1
MJ
125#define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u
126#define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g
127#define LTTNG_SOCK_SET_PID_CRED(c, p)
128
d27c42b8
MD
129#define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid
130#define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid
131#define LTTNG_SOCK_GET_PID_CRED(c) -1
b17231c6
DG
132
133#else
7657ae75 134#error "Please add support for your OS."
b17231c6
DG
135#endif /* __linux__ , __FreeBSD__ */
136
28105b32
MJ
137
138#ifdef __sun__
139
140# ifndef CMSG_ALIGN
141# ifdef _CMSG_DATA_ALIGN
142# define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len)
143# else
144 /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */
145# define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & ~(sizeof (long) - 1))
146# endif
147# ifndef CMSG_SPACE
148# define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + CMSG_ALIGN (len))
149# endif
150# ifndef CMSG_LEN
151# define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
152# endif
153# endif
154
155
156#include <ucred.h>
11a2fd46
MJ
157
158static inline
ba66a850 159int getpeereid(int s, uid_t *euid, gid_t *gid, pid_t *pid)
28105b32
MJ
160{
161 int ret = 0;
162 ucred_t *ucred = NULL;
163
164 ret = getpeerucred(s, &ucred);
165 if (ret == -1) {
166 goto end;
167 }
168
169 ret = ucred_geteuid(ucred);
170 if (ret == -1) {
171 goto free;
172 }
173 *euid = ret;
174
175 ret = ucred_getrgid(ucred);
176 if (ret == -1) {
177 goto free;
178 }
179 *gid = ret;
ba66a850
JG
180
181 ret = ucred_getpid(ucred);
182 if (ret == -1) {
183 goto free;
184 }
185 *pid = ret;
186
28105b32
MJ
187 ret = 0;
188free:
189 ucred_free(ucred);
190end:
191 return ret;
192}
193
194#endif /* __sun__ */
195
b17231c6 196#endif /* _COMPAT_SOCKET_H */
This page took 0.058228 seconds and 4 git commands to generate.