Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
917216f6 DG |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
fac6795d | 4 | * |
917216f6 DG |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; only version 2 of the License. | |
fac6795d | 8 | * |
917216f6 DG |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
fac6795d | 13 | * |
917216f6 DG |
14 | * You should have received a copy of the GNU General Public License along with |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
16 | * Place - Suite 330, Boston, MA 02111-1307, USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
1e307fab | 20 | #include <assert.h> |
fac6795d DG |
21 | #include <limits.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
fac6795d DG |
25 | #include <sys/stat.h> |
26 | #include <sys/types.h> | |
27 | #include <sys/un.h> | |
28 | #include <unistd.h> | |
3bd1e081 | 29 | #include <errno.h> |
fac6795d | 30 | |
990570ed DG |
31 | #include <common/defaults.h> |
32 | ||
10a8a223 | 33 | #include "sessiond-comm.h" |
fac6795d DG |
34 | |
35 | /* | |
36 | * Human readable error message. | |
37 | */ | |
38 | static const char *lttcomm_readable_code[] = { | |
39 | [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success", | |
40 | [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error", | |
41 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command", | |
7d29a247 DG |
42 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented", |
43 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain", | |
fac6795d DG |
44 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found", |
45 | [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps", | |
e065084a | 46 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found", |
f3ed775e | 47 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found", |
ce3d728c | 48 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found", |
ca95a216 | 49 | [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon", |
df0da139 | 50 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed", |
379473d2 | 51 | [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed", |
520ff687 | 52 | [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed", |
379473d2 DG |
53 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable", |
54 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected", | |
27673bb6 | 55 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist", |
d9800920 | 56 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket", |
2b0bf864 | 57 | [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found", |
8e0af1b4 | 58 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied", |
20fe2104 | 59 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available", |
394016d1 | 60 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_VERSION) ] = "Kernel tracer version is not compatible", |
7d29a247 | 61 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists", |
20fe2104 DG |
62 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed", |
63 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed", | |
f3ed775e | 64 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found", |
26cc6b4e | 65 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed", |
d36b8583 | 66 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed", |
d65106b1 | 67 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed", |
f34daff7 DG |
68 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed", |
69 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed", | |
aaf26714 | 70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", |
8c0faa1d DG |
71 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed", |
72 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed", | |
73 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed", | |
74 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed", | |
75 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed", | |
76 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist", | |
84291629 | 77 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found", |
2ef84c95 | 78 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed", |
e0c7ec2b | 79 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION) ] = "UST tracer version is not compatible", |
0177d773 | 80 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed", |
2b0bf864 | 81 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed", |
f6a9efaa | 82 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist", |
2bdd86d4 MD |
83 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found", |
84 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed", | |
85 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed", | |
86 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed", | |
87 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed", | |
88 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed", | |
89 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed", | |
90 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed", | |
91 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed", | |
7753dea8 MD |
92 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed", |
93 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed", | |
2bdd86d4 MD |
94 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed", |
95 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL) ] = "UST trace directory creation failed", | |
96 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST) ] = "UST trace directory already exist", | |
97 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION) ] = "No UST session found", | |
98 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL) ] = "Listing UST events failed", | |
3b25d04d | 99 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist", |
be4f0921 | 100 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found", |
ae357384 | 101 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist", |
3bd1e081 MD |
102 | [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", |
103 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", | |
104 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", | |
f2fc6720 | 105 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", |
3bd1e081 MD |
106 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", |
107 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", | |
108 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", | |
109 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", | |
110 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", | |
111 | [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", | |
112 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", | |
113 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", | |
114 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", | |
115 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", | |
d65106b1 | 116 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
fac6795d DG |
117 | }; |
118 | ||
119 | /* | |
917216f6 DG |
120 | * Return ptr to string representing a human readable error code from the |
121 | * lttcomm_return_code enum. | |
fac6795d | 122 | * |
917216f6 | 123 | * These code MUST be negative in other to treat that as an error value. |
fac6795d DG |
124 | */ |
125 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) | |
126 | { | |
127 | int tmp_code = -code; | |
128 | ||
129 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { | |
130 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; | |
131 | } | |
132 | ||
133 | return "Unknown error code"; | |
134 | } | |
135 | ||
136 | /* | |
917216f6 | 137 | * Connect to unix socket using the path name. |
fac6795d DG |
138 | */ |
139 | int lttcomm_connect_unix_sock(const char *pathname) | |
140 | { | |
686204ab MD |
141 | struct sockaddr_un sun; |
142 | int fd; | |
a8afeb4a | 143 | int ret; |
fac6795d | 144 | |
686204ab | 145 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
fac6795d DG |
146 | if (fd < 0) { |
147 | perror("socket"); | |
a8afeb4a | 148 | ret = fd; |
fac6795d DG |
149 | goto error; |
150 | } | |
151 | ||
686204ab MD |
152 | memset(&sun, 0, sizeof(sun)); |
153 | sun.sun_family = AF_UNIX; | |
154 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
99497cd0 | 155 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
fac6795d | 156 | |
686204ab MD |
157 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
158 | if (ret < 0) { | |
a8afeb4a | 159 | /* |
917216f6 DG |
160 | * Don't print message on connect error, because connect is used in |
161 | * normal execution to detect if sessiond is alive. | |
a8afeb4a MD |
162 | */ |
163 | goto error_connect; | |
686204ab | 164 | } |
fac6795d | 165 | |
686204ab | 166 | return fd; |
fac6795d | 167 | |
a8afeb4a MD |
168 | error_connect: |
169 | close(fd); | |
fac6795d | 170 | error: |
a8afeb4a | 171 | return ret; |
fac6795d DG |
172 | } |
173 | ||
174 | /* | |
917216f6 DG |
175 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
176 | * MUST be bind(2) before. | |
fac6795d DG |
177 | */ |
178 | int lttcomm_accept_unix_sock(int sock) | |
179 | { | |
180 | int new_fd; | |
181 | struct sockaddr_un sun; | |
182 | socklen_t len = 0; | |
183 | ||
184 | /* Blocking call */ | |
185 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
186 | if (new_fd < 0) { | |
187 | perror("accept"); | |
fac6795d DG |
188 | } |
189 | ||
190 | return new_fd; | |
fac6795d DG |
191 | } |
192 | ||
193 | /* | |
917216f6 DG |
194 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
195 | * and return the fd. | |
fac6795d DG |
196 | */ |
197 | int lttcomm_create_unix_sock(const char *pathname) | |
198 | { | |
199 | struct sockaddr_un sun; | |
200 | int fd; | |
201 | int ret = -1; | |
202 | ||
203 | /* Create server socket */ | |
204 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
205 | perror("socket"); | |
206 | goto error; | |
207 | } | |
208 | ||
209 | memset(&sun, 0, sizeof(sun)); | |
210 | sun.sun_family = AF_UNIX; | |
99497cd0 MD |
211 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
212 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
fac6795d | 213 | |
7d8234d9 MD |
214 | /* Unlink the old file if present */ |
215 | (void) unlink(pathname); | |
fac6795d DG |
216 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
217 | if (ret < 0) { | |
218 | perror("bind"); | |
219 | goto error; | |
220 | } | |
221 | ||
222 | return fd; | |
223 | ||
224 | error: | |
225 | return ret; | |
226 | } | |
227 | ||
228 | /* | |
f158a754 | 229 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
fac6795d DG |
230 | */ |
231 | int lttcomm_listen_unix_sock(int sock) | |
232 | { | |
233 | int ret; | |
234 | ||
f158a754 | 235 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
fac6795d DG |
236 | if (ret < 0) { |
237 | perror("listen"); | |
238 | } | |
239 | ||
240 | return ret; | |
241 | } | |
242 | ||
243 | /* | |
917216f6 DG |
244 | * Receive data of size len in put that data into the buf param. Using recvmsg |
245 | * API. | |
fac6795d | 246 | * |
917216f6 | 247 | * Return the size of received data. |
fac6795d DG |
248 | */ |
249 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) | |
250 | { | |
159c7ff4 | 251 | struct msghdr msg = { 0 }; |
fac6795d DG |
252 | struct iovec iov[1]; |
253 | ssize_t ret = -1; | |
254 | ||
fac6795d DG |
255 | iov[0].iov_base = buf; |
256 | iov[0].iov_len = len; | |
257 | msg.msg_iov = iov; | |
258 | msg.msg_iovlen = 1; | |
259 | ||
260 | ret = recvmsg(sock, &msg, 0); | |
261 | if (ret < 0) { | |
262 | perror("recvmsg"); | |
263 | } | |
264 | ||
265 | return ret; | |
266 | } | |
267 | ||
268 | /* | |
917216f6 | 269 | * Send buf data of size len. Using sendmsg API. |
fac6795d | 270 | * |
917216f6 | 271 | * Return the size of sent data. |
fac6795d DG |
272 | */ |
273 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) | |
274 | { | |
159c7ff4 | 275 | struct msghdr msg = { 0 }; |
fac6795d DG |
276 | struct iovec iov[1]; |
277 | ssize_t ret = -1; | |
278 | ||
fac6795d DG |
279 | iov[0].iov_base = buf; |
280 | iov[0].iov_len = len; | |
281 | msg.msg_iov = iov; | |
282 | msg.msg_iovlen = 1; | |
283 | ||
284 | ret = sendmsg(sock, &msg, 0); | |
285 | if (ret < 0) { | |
286 | perror("sendmsg"); | |
287 | } | |
288 | ||
289 | return ret; | |
290 | } | |
87378cf5 DG |
291 | |
292 | /* | |
917216f6 | 293 | * Shutdown cleanly a unix socket. |
87378cf5 DG |
294 | */ |
295 | int lttcomm_close_unix_sock(int sock) | |
296 | { | |
297 | int ret; | |
298 | ||
299 | /* Shutdown receptions and transmissions */ | |
300 | ret = shutdown(sock, SHUT_RDWR); | |
301 | if (ret < 0) { | |
302 | perror("shutdown"); | |
303 | } | |
304 | ||
305 | return ret; | |
306 | } | |
8c0faa1d DG |
307 | |
308 | /* | |
3bd1e081 MD |
309 | * Send a message accompanied by fd(s) over a unix socket. |
310 | * | |
311 | * Returns the size of data sent, or negative error value. | |
8c0faa1d | 312 | */ |
3bd1e081 | 313 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
8c0faa1d | 314 | { |
159c7ff4 | 315 | struct msghdr msg = { 0 }; |
8c0faa1d DG |
316 | struct cmsghdr *cmptr; |
317 | struct iovec iov[1]; | |
318 | ssize_t ret = -1; | |
319 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
320 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
3bd1e081 | 321 | char dummy = 0; |
8c0faa1d | 322 | |
3bd1e081 MD |
323 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
324 | return -EINVAL; | |
8c0faa1d DG |
325 | |
326 | msg.msg_control = (caddr_t)tmp; | |
327 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
328 | ||
329 | cmptr = CMSG_FIRSTHDR(&msg); | |
8c0faa1d DG |
330 | cmptr->cmsg_level = SOL_SOCKET; |
331 | cmptr->cmsg_type = SCM_RIGHTS; | |
159c7ff4 | 332 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
8c0faa1d | 333 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
159c7ff4 MD |
334 | /* Sum of the length of all control messages in the buffer: */ |
335 | msg.msg_controllen = cmptr->cmsg_len; | |
8c0faa1d | 336 | |
3bd1e081 MD |
337 | iov[0].iov_base = &dummy; |
338 | iov[0].iov_len = 1; | |
8c0faa1d DG |
339 | msg.msg_iov = iov; |
340 | msg.msg_iovlen = 1; | |
341 | ||
342 | ret = sendmsg(sock, &msg, 0); | |
343 | if (ret < 0) { | |
344 | perror("sendmsg"); | |
345 | } | |
8c0faa1d DG |
346 | return ret; |
347 | } | |
09de5c68 DG |
348 | |
349 | /* | |
3bd1e081 | 350 | * Recv a message accompanied by fd(s) from a unix socket. |
09de5c68 | 351 | * |
3bd1e081 MD |
352 | * Returns the size of received data, or negative error value. |
353 | * | |
354 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
355 | * actually received in nb_fd. | |
09de5c68 | 356 | */ |
3bd1e081 | 357 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
09de5c68 DG |
358 | { |
359 | struct iovec iov[1]; | |
3bd1e081 | 360 | ssize_t ret = 0; |
09de5c68 | 361 | struct cmsghdr *cmsg; |
3bd1e081 MD |
362 | size_t sizeof_fds = nb_fd * sizeof(int); |
363 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
09de5c68 | 364 | struct msghdr msg = { 0 }; |
3bd1e081 | 365 | char dummy; |
09de5c68 DG |
366 | |
367 | /* Prepare to receive the structures */ | |
3bd1e081 MD |
368 | iov[0].iov_base = &dummy; |
369 | iov[0].iov_len = 1; | |
09de5c68 DG |
370 | msg.msg_iov = iov; |
371 | msg.msg_iovlen = 1; | |
372 | msg.msg_control = recv_fd; | |
373 | msg.msg_controllen = sizeof(recv_fd); | |
374 | ||
375 | ret = recvmsg(sock, &msg, 0); | |
376 | if (ret < 0) { | |
377 | perror("recvmsg fds"); | |
378 | goto end; | |
379 | } | |
3bd1e081 MD |
380 | if (ret != 1) { |
381 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
382 | ret, 1); | |
383 | goto end; | |
384 | } | |
385 | if (msg.msg_flags & MSG_CTRUNC) { | |
386 | fprintf(stderr, "Error: Control message truncated.\n"); | |
387 | ret = -1; | |
09de5c68 DG |
388 | goto end; |
389 | } | |
09de5c68 DG |
390 | cmsg = CMSG_FIRSTHDR(&msg); |
391 | if (!cmsg) { | |
3bd1e081 | 392 | fprintf(stderr, "Error: Invalid control message header\n"); |
09de5c68 DG |
393 | ret = -1; |
394 | goto end; | |
395 | } | |
09de5c68 | 396 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
3bd1e081 | 397 | fprintf(stderr, "Didn't received any fd\n"); |
09de5c68 DG |
398 | ret = -1; |
399 | goto end; | |
400 | } | |
3bd1e081 MD |
401 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
402 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
403 | cmsg->cmsg_len, CMSG_LEN(sizeof_fds)); | |
404 | ret = -1; | |
405 | goto end; | |
09de5c68 | 406 | } |
3bd1e081 MD |
407 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
408 | ret = sizeof_fds; | |
09de5c68 DG |
409 | end: |
410 | return ret; | |
411 | } | |
d19d9ada DG |
412 | |
413 | /* | |
414 | * Send a message with credentials over a unix socket. | |
415 | * | |
416 | * Returns the size of data sent, or negative error value. | |
417 | */ | |
418 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) | |
419 | { | |
420 | struct msghdr msg = { 0 }; | |
421 | struct cmsghdr *cmptr; | |
422 | struct iovec iov[1]; | |
423 | ssize_t ret = -1; | |
424 | struct ucred *creds; | |
425 | size_t sizeof_cred = sizeof(struct ucred); | |
426 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
427 | ||
428 | iov[0].iov_base = buf; | |
429 | iov[0].iov_len = len; | |
430 | msg.msg_iov = iov; | |
431 | msg.msg_iovlen = 1; | |
432 | ||
433 | msg.msg_control = (caddr_t) anc_buf; | |
434 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
435 | ||
436 | cmptr = CMSG_FIRSTHDR(&msg); | |
437 | cmptr->cmsg_level = SOL_SOCKET; | |
438 | cmptr->cmsg_type = SCM_CREDENTIALS; | |
439 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); | |
440 | ||
441 | creds = (struct ucred *) CMSG_DATA(cmptr); | |
442 | ||
443 | creds->uid = geteuid(); | |
444 | creds->gid = getegid(); | |
445 | creds->pid = getpid(); | |
446 | ||
447 | ret = sendmsg(sock, &msg, 0); | |
448 | if (ret < 0) { | |
449 | perror("sendmsg"); | |
450 | } | |
451 | ||
452 | return ret; | |
453 | } | |
454 | ||
455 | /* | |
456 | * Recv a message accompanied with credentials from a unix socket. | |
457 | * | |
458 | * Returns the size of received data, or negative error value. | |
459 | */ | |
460 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, | |
461 | struct ucred *creds) | |
462 | { | |
463 | struct msghdr msg = { 0 }; | |
464 | struct cmsghdr *cmptr; | |
465 | struct iovec iov[1]; | |
466 | ssize_t ret; | |
467 | size_t sizeof_cred = sizeof(struct ucred); | |
468 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
469 | ||
470 | /* Not allowed */ | |
471 | if (creds == NULL) { | |
472 | ret = -1; | |
473 | goto end; | |
474 | } | |
475 | ||
476 | /* Prepare to receive the structures */ | |
477 | iov[0].iov_base = buf; | |
478 | iov[0].iov_len = len; | |
479 | msg.msg_iov = iov; | |
480 | msg.msg_iovlen = 1; | |
481 | ||
482 | msg.msg_control = anc_buf; | |
483 | msg.msg_controllen = sizeof(anc_buf); | |
484 | ||
485 | ret = recvmsg(sock, &msg, 0); | |
486 | if (ret < 0) { | |
487 | perror("recvmsg fds"); | |
488 | goto end; | |
489 | } | |
490 | ||
491 | if (msg.msg_flags & MSG_CTRUNC) { | |
492 | fprintf(stderr, "Error: Control message truncated.\n"); | |
493 | ret = -1; | |
494 | goto end; | |
495 | } | |
496 | ||
497 | cmptr = CMSG_FIRSTHDR(&msg); | |
498 | if (cmptr == NULL) { | |
499 | fprintf(stderr, "Error: Invalid control message header\n"); | |
500 | ret = -1; | |
501 | goto end; | |
502 | } | |
503 | ||
504 | if (cmptr->cmsg_level != SOL_SOCKET || | |
505 | cmptr->cmsg_type != SCM_CREDENTIALS) { | |
506 | fprintf(stderr, "Didn't received any credentials\n"); | |
507 | ret = -1; | |
508 | goto end; | |
509 | } | |
510 | ||
511 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
512 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
513 | cmptr->cmsg_len, CMSG_LEN(sizeof_cred)); | |
514 | ret = -1; | |
515 | goto end; | |
516 | } | |
517 | ||
518 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
519 | ||
520 | end: | |
521 | return ret; | |
522 | } | |
523 | ||
524 | /* | |
525 | * Set socket option to use credentials passing. | |
526 | */ | |
527 | int lttcomm_setsockopt_creds_unix_sock(int sock) | |
528 | { | |
529 | int ret, on = 1; | |
530 | ||
531 | /* Set socket for credentials retrieval */ | |
532 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
533 | if (ret < 0) { | |
534 | perror("setsockopt creds unix sock"); | |
535 | } | |
536 | ||
537 | return ret; | |
538 | } |