2c23b669c2d800bf35e7b528b0e346f3bc0ef1a5
[lttng-tools.git] / tests / unit / test_unix_socket.cpp
1 /*
2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/compat/fcntl.hpp>
9 #include <common/defaults.hpp>
10 #include <common/error.hpp>
11 #include <common/payload-view.hpp>
12 #include <common/payload.hpp>
13 #include <common/sessiond-comm/sessiond-comm.hpp>
14 #include <common/unix.hpp>
15 #include <common/utils.hpp>
16
17 #include <lttng/constant.h>
18
19 #include <algorithm>
20 #include <pthread.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/wait.h>
25 #include <tap/tap.h>
26 #include <unistd.h>
27
28 #define HIGH_FD_COUNT LTTCOMM_MAX_SEND_FDS
29 #define MESSAGE_COUNT 4
30 #define LARGE_PAYLOAD_SIZE 4 * 1024
31 #define LARGE_PAYLOAD_RECV_SIZE 100
32
33 static const int TEST_COUNT = 37;
34
35 /* For error.h */
36 int lttng_opt_quiet;
37 int lttng_opt_verbose;
38 int lttng_opt_mi;
39
40 /*
41 * Validate that a large number of file descriptors can be received in one shot.
42 */
43 static void test_high_fd_count(unsigned int fd_count)
44 {
45 int sockets[2] = { -1, -1 };
46 int ret;
47 unsigned int i;
48 const unsigned int payload_content = 42;
49 struct lttng_payload sent_payload;
50 struct lttng_payload received_payload;
51
52 diag("Send and receive high FD count atomically (%u FDs)", fd_count);
53 lttng_payload_init(&sent_payload);
54 lttng_payload_init(&received_payload);
55
56 ret = lttcomm_create_anon_unix_socketpair(sockets);
57 ok(ret == 0, "Created anonymous unix socket pair");
58 if (ret < 0) {
59 PERROR("Failed to create an anonymous pair of unix sockets");
60 goto error;
61 }
62
63 /* Add dummy content to payload. */
64 ret = lttng_dynamic_buffer_append(
65 &sent_payload.buffer, &payload_content, sizeof(payload_content));
66 if (ret) {
67 PERROR("Failed to initialize test payload");
68 goto error;
69 }
70
71 for (i = 0; i < fd_count; i++) {
72 struct fd_handle *handle;
73 int fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
74
75 if (fd < 0) {
76 PERROR("Failed to create fd while creating test payload");
77 goto error;
78 }
79
80 handle = fd_handle_create(fd);
81 if (!handle) {
82 if (close(fd)) {
83 PERROR("Failed to close fd while preparing test payload");
84 goto error;
85 }
86 }
87
88 ret = lttng_payload_push_fd_handle(&sent_payload, handle);
89 fd_handle_put(handle);
90 if (ret) {
91 PERROR("Failed to add fd handle to test payload");
92 goto error;
93 }
94 }
95
96 /* Send payload. */
97 {
98 ssize_t sock_ret;
99 struct lttng_payload_view pv =
100 lttng_payload_view_from_payload(&sent_payload, 0, -1);
101
102 /* Not expected to block considering the size of the payload. */
103 sock_ret = lttcomm_send_unix_sock(sockets[0], pv.buffer.data, pv.buffer.size);
104 ok(sock_ret == pv.buffer.size, "Sent complete test payload");
105 if (sock_ret != pv.buffer.size) {
106 ERR("Failed to send test payload bytes: ret = %zd, expected = %zu",
107 sock_ret,
108 pv.buffer.size);
109 goto error;
110 }
111
112 sock_ret = lttcomm_send_payload_view_fds_unix_sock(sockets[0], &pv);
113 ok(sock_ret == 1, "Sent test payload file descriptors");
114 if (sock_ret != 1) {
115 if (sock_ret < 0) {
116 PERROR("Failed to send test payload file descriptors: ret = %zd, expected = %d",
117 sock_ret,
118 1);
119 } else {
120 diag("Failed to send test payload file descriptors: ret = %zd, expected = %d",
121 sock_ret,
122 1);
123 }
124
125 goto error;
126 }
127 }
128
129 /* Receive payload */
130 {
131 ssize_t sock_ret;
132
133 ret = lttng_dynamic_buffer_set_size(&received_payload.buffer,
134 sent_payload.buffer.size);
135 if (ret) {
136 PERROR("Failed to pre-allocate reception buffer");
137 goto error;
138 }
139
140 sock_ret = lttcomm_recv_unix_sock(
141 sockets[1], received_payload.buffer.data, received_payload.buffer.size);
142 ok(sock_ret == received_payload.buffer.size, "Received payload bytes");
143 if (sock_ret != received_payload.buffer.size) {
144 ERR("Failed to receive payload bytes: ret = %zd, expected = %zu",
145 sock_ret,
146 received_payload.buffer.size);
147 goto error;
148 }
149
150 sock_ret =
151 lttcomm_recv_payload_fds_unix_sock(sockets[1], fd_count, &received_payload);
152 ok(sock_ret == (int) (sizeof(int) * fd_count),
153 "FD reception return value is number of fd * sizeof(int)");
154 if (sock_ret != (int) (sizeof(int) * fd_count)) {
155 ERR("Failed to receive test payload file descriptors: ret = %zd, expected = %d",
156 sock_ret,
157 (int) (fd_count * sizeof(int)));
158 goto error;
159 }
160
161 {
162 const struct lttng_payload_view pv =
163 lttng_payload_view_from_payload(&received_payload, 0, -1);
164 const int fd_handle_count = lttng_payload_view_get_fd_handle_count(&pv);
165
166 ok(fd_handle_count == fd_count,
167 "Received all test payload file descriptors in one invocation");
168 }
169 }
170
171 error:
172 for (i = 0; i < 2; i++) {
173 if (sockets[i] < 0) {
174 continue;
175 }
176
177 if (close(sockets[i])) {
178 PERROR("Failed to close unix socket");
179 }
180 }
181
182 lttng_payload_reset(&sent_payload);
183 lttng_payload_reset(&received_payload);
184 }
185
186 /*
187 * Validate that if the sender sent multiple messages, each containing 1 fd,
188 * the receiver can receive one message at a time (the binary payload and its
189 * fd) and is not forced to receive all file descriptors at once.
190 */
191 static void test_one_fd_per_message(unsigned int message_count)
192 {
193 const unsigned int payload_content = 42;
194 int sockets[2] = { -1, -1 };
195 int ret;
196 unsigned int i;
197 struct lttng_payload sent_payload;
198 struct lttng_payload received_payload;
199
200 diag("Send and receive small messages with one FD each (%u messages)", message_count);
201 lttng_payload_init(&sent_payload);
202 lttng_payload_init(&received_payload);
203
204 ret = lttcomm_create_anon_unix_socketpair(sockets);
205 ok(ret == 0, "Created anonymous unix socket pair");
206 if (ret < 0) {
207 PERROR("Failed to create an anonymous pair of unix sockets");
208 goto error;
209 }
210
211 /* Send messages with one fd each. */
212 for (i = 0; i < message_count; i++) {
213 struct fd_handle *handle;
214 int fd;
215
216 /* Add dummy content to payload. */
217 ret = lttng_dynamic_buffer_append(
218 &sent_payload.buffer, &payload_content, sizeof(payload_content));
219 if (ret) {
220 PERROR("Failed to initialize test payload");
221 goto error;
222 }
223
224 fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
225 if (fd < 0) {
226 PERROR("Failed to create fd while creating test payload");
227 goto error;
228 }
229
230 handle = fd_handle_create(fd);
231 if (!handle) {
232 if (close(fd)) {
233 PERROR("Failed to close fd while preparing test payload");
234 goto error;
235 }
236 }
237
238 ret = lttng_payload_push_fd_handle(&sent_payload, handle);
239 fd_handle_put(handle);
240 if (ret) {
241 PERROR("Failed to add fd handle to test payload");
242 goto error;
243 }
244
245 /* Send payload. */
246 {
247 ssize_t sock_ret;
248 struct lttng_payload_view pv =
249 lttng_payload_view_from_payload(&sent_payload, 0, -1);
250
251 /* Not expected to block considering the size of the
252 * payload. */
253 sock_ret =
254 lttcomm_send_unix_sock(sockets[0], pv.buffer.data, pv.buffer.size);
255 ok(sock_ret == pv.buffer.size, "Sent binary payload for message %u", i);
256 if (sock_ret != pv.buffer.size) {
257 ERR("Failed to send test payload bytes: ret = %zd, expected = %zu",
258 sock_ret,
259 pv.buffer.size);
260 goto error;
261 }
262
263 sock_ret = lttcomm_send_payload_view_fds_unix_sock(sockets[0], &pv);
264 ok(sock_ret == 1, "Sent file descriptors payload for message %u", i);
265 if (sock_ret != 1) {
266 if (sock_ret < 0) {
267 PERROR("Failed to send test payload file descriptors: ret = %zd, expected = %d",
268 sock_ret,
269 1);
270 } else {
271 diag("Failed to send test payload file descriptors: ret = %zd, expected = %d",
272 sock_ret,
273 1);
274 }
275
276 goto error;
277 }
278 }
279
280 lttng_payload_clear(&sent_payload);
281 }
282
283 /* Receive messages one at a time. */
284 for (i = 0; i < message_count; i++) {
285 ssize_t sock_ret;
286
287 ret = lttng_dynamic_buffer_set_size(&received_payload.buffer,
288 sizeof(payload_content));
289 if (ret) {
290 PERROR("Failed to pre-allocate reception buffer");
291 goto error;
292 }
293
294 sock_ret = lttcomm_recv_unix_sock(
295 sockets[1], received_payload.buffer.data, received_payload.buffer.size);
296 ok(sock_ret == received_payload.buffer.size,
297 "Received payload bytes for message %u",
298 i);
299 if (sock_ret != received_payload.buffer.size) {
300 ERR("Failed to receive payload bytes: ret = %zd, expected = %zu",
301 sock_ret,
302 received_payload.buffer.size);
303 goto error;
304 }
305
306 sock_ret = lttcomm_recv_payload_fds_unix_sock(sockets[1], 1, &received_payload);
307 ok(sock_ret == (int) sizeof(int), "Received fd for message %u", i);
308 if (sock_ret != (int) sizeof(int)) {
309 ERR("Failed to receive test payload file descriptors: ret = %zd, expected = %u",
310 sock_ret,
311 (int) sizeof(int));
312 goto error;
313 }
314
315 {
316 const struct lttng_payload_view pv =
317 lttng_payload_view_from_payload(&received_payload, 0, -1);
318 const int fd_handle_count = lttng_payload_view_get_fd_handle_count(&pv);
319
320 ok(fd_handle_count == 1, "Payload contains 1 fd for message %u", i);
321 }
322
323 lttng_payload_clear(&received_payload);
324 }
325
326 error:
327 for (i = 0; i < 2; i++) {
328 if (sockets[i] < 0) {
329 continue;
330 }
331
332 if (close(sockets[i])) {
333 PERROR("Failed to close unix socket");
334 }
335 }
336
337 lttng_payload_reset(&sent_payload);
338 lttng_payload_reset(&received_payload);
339 }
340
341 /*
342 * Validate that a large message can be received in multiple chunks.
343 */
344 static void test_receive_in_chunks(unsigned int payload_size, unsigned int max_recv_size)
345 {
346 int sockets[2] = { -1, -1 };
347 int ret;
348 unsigned int i;
349 struct lttng_payload sent_payload;
350 struct lttng_payload received_payload;
351 struct fd_handle *handle;
352 int fd;
353 ssize_t sock_ret, received = 0;
354
355 diag("Receive a message in multiple chunks");
356 lttng_payload_init(&sent_payload);
357 lttng_payload_init(&received_payload);
358
359 ret = lttcomm_create_anon_unix_socketpair(sockets);
360 ok(ret == 0, "Created anonymous unix socket pair");
361 if (ret < 0) {
362 PERROR("Failed to create an anonymous pair of unix sockets");
363 goto error;
364 }
365
366 /* Add dummy content to payload. */
367 ret = lttng_dynamic_buffer_set_size(&sent_payload.buffer, payload_size);
368 if (ret) {
369 PERROR("Failed to initialize test payload");
370 goto error;
371 }
372
373 fd = fcntl(STDOUT_FILENO, F_DUPFD, 0);
374 if (fd < 0) {
375 PERROR("Failed to create fd while creating test payload");
376 goto error;
377 }
378
379 handle = fd_handle_create(fd);
380 if (!handle) {
381 if (close(fd)) {
382 PERROR("Failed to close fd while preparing test payload");
383 goto error;
384 }
385 }
386
387 ret = lttng_payload_push_fd_handle(&sent_payload, handle);
388 fd_handle_put(handle);
389 if (ret) {
390 PERROR("Failed to add fd handle to test payload");
391 goto error;
392 }
393
394 /* Send payload. */
395 {
396 struct lttng_payload_view pv =
397 lttng_payload_view_from_payload(&sent_payload, 0, -1);
398
399 /* Not expected to block considering the size of the payload. */
400 sock_ret = lttcomm_send_unix_sock(sockets[0], pv.buffer.data, pv.buffer.size);
401 ok(sock_ret == pv.buffer.size, "Sent complete test payload");
402 if (sock_ret != pv.buffer.size) {
403 ERR("Failed to send test payload bytes: ret = %zd, expected = %zu",
404 sock_ret,
405 pv.buffer.size);
406 goto error;
407 }
408
409 sock_ret = lttcomm_send_payload_view_fds_unix_sock(sockets[0], &pv);
410 ok(sock_ret == 1, "Sent test payload file descriptors");
411 if (sock_ret != 1) {
412 if (sock_ret < 0) {
413 PERROR("Failed to send test payload file descriptors: ret = %zd, expected = %d",
414 sock_ret,
415 1);
416 } else {
417 diag("Failed to send test payload file descriptors: ret = %zd, expected = %d",
418 sock_ret,
419 1);
420 }
421
422 goto error;
423 }
424 }
425
426 /* Receive payload */
427 ret = lttng_dynamic_buffer_set_size(&received_payload.buffer, sent_payload.buffer.size);
428 if (ret) {
429 PERROR("Failed to pre-allocate reception buffer");
430 goto error;
431 }
432
433 do {
434 const ssize_t to_receive_this_pass =
435 std::min<ssize_t>(max_recv_size, sent_payload.buffer.size - received);
436
437 sock_ret = lttcomm_recv_unix_sock(
438 sockets[1], received_payload.buffer.data + received, to_receive_this_pass);
439 if (sock_ret != to_receive_this_pass) {
440 ERR("Failed to receive payload bytes: ret = %zd, expected = %zu",
441 sock_ret,
442 to_receive_this_pass);
443 break;
444 }
445
446 received += sock_ret;
447 } while (received < sent_payload.buffer.size);
448
449 ok(received == sent_payload.buffer.size,
450 "Received complete payload in chunks of %u bytes",
451 max_recv_size);
452 if (received != sent_payload.buffer.size) {
453 goto error;
454 }
455
456 sock_ret = lttcomm_recv_payload_fds_unix_sock(sockets[1], 1, &received_payload);
457 ok(sock_ret == (int) sizeof(int),
458 "Received file descriptor after receiving payload in chunks");
459 if (sock_ret != (int) sizeof(int)) {
460 ERR("Failed to receive test payload file descriptors: ret = %zd, expected = %d",
461 sock_ret,
462 (int) sizeof(int));
463 goto error;
464 }
465
466 {
467 const struct lttng_payload_view pv =
468 lttng_payload_view_from_payload(&received_payload, 0, -1);
469 const int fd_handle_count = lttng_payload_view_get_fd_handle_count(&pv);
470
471 ok(fd_handle_count == 1, "Payload contains 1 fd after receiving payload in chunks");
472 }
473
474 error:
475 for (i = 0; i < 2; i++) {
476 if (sockets[i] < 0) {
477 continue;
478 }
479
480 if (close(sockets[i])) {
481 PERROR("Failed to close unix socket");
482 }
483 }
484
485 lttng_payload_reset(&sent_payload);
486 lttng_payload_reset(&received_payload);
487 }
488
489 static void test_creds_passing(void)
490 {
491 pid_t fork_ret = -1;
492 int ret, parent_socket = -1, child_connection_socket = -1;
493 ssize_t sock_ret;
494 char socket_dir_path[] = "/tmp/test.unix.socket.creds.passing.XXXXXX";
495 char socket_path[PATH_MAX] = {};
496 struct expected_creds {
497 uid_t euid;
498 gid_t egid;
499 pid_t pid;
500 } expected_creds;
501
502 diag("Receive peer's effective uid, effective gid, and pid from a unix socket");
503
504 if (!mkdtemp(socket_dir_path)) {
505 PERROR("Failed to generate temporary socket location");
506 goto error;
507 }
508
509 strncat(socket_path, socket_dir_path, sizeof(socket_path) - strlen(socket_path) - 1);
510 strncat(socket_path, "/test_unix_socket", sizeof(socket_path) - strlen(socket_path) - 1);
511
512 parent_socket = lttcomm_create_unix_sock(socket_path);
513 ok(parent_socket >= 0, "Created unix socket at path `%s`", socket_path);
514 if (parent_socket < 0) {
515 PERROR("Failed to create unix socket at path `%s`", socket_path);
516 goto error;
517 }
518
519 ret = lttcomm_listen_unix_sock(parent_socket);
520 if (ret < 0) {
521 PERROR("Failed to mark parent socket as a passive socket");
522 goto error;
523 }
524
525 ret = lttcomm_setsockopt_creds_unix_sock(parent_socket);
526 if (ret) {
527 PERROR("Failed to set SO_PASSCRED on parent socket");
528 goto error;
529 }
530
531 fork_ret = fork();
532 if (fork_ret < 0) {
533 PERROR("Failed to fork");
534 goto error;
535 }
536
537 if (fork_ret == 0) {
538 /* Child. */
539 int child_socket;
540
541 expected_creds = (struct expected_creds){
542 .euid = geteuid(),
543 .egid = getegid(),
544 .pid = getpid(),
545 };
546
547 child_socket = lttcomm_connect_unix_sock(socket_path);
548 if (child_socket < 0) {
549 PERROR("Failed to connect to parent socket");
550 goto error;
551 }
552
553 ret = lttcomm_setsockopt_creds_unix_sock(child_socket);
554 if (ret) {
555 PERROR("Failed to set SO_PASSCRED on child socket");
556 }
557
558 sock_ret = lttcomm_send_creds_unix_sock(
559 child_socket, &expected_creds, sizeof(expected_creds));
560 if (sock_ret < 0) {
561 PERROR("Failed to send expected credentials");
562 }
563
564 ret = close(child_socket);
565 if (ret) {
566 PERROR("Failed to close child socket");
567 }
568 } else {
569 /* Parent. */
570 int child_status;
571 pid_t wait_pid_ret;
572 lttng_sock_cred received_creds = {};
573
574 child_connection_socket = lttcomm_accept_unix_sock(parent_socket);
575 if (child_connection_socket < 0) {
576 PERROR();
577 goto error;
578 }
579
580 ret = lttcomm_setsockopt_creds_unix_sock(child_connection_socket);
581 if (ret) {
582 PERROR("Failed to set SO_PASSCRED on child connection socket");
583 goto error;
584 }
585
586 sock_ret = lttcomm_recv_creds_unix_sock(child_connection_socket,
587 &expected_creds,
588 sizeof(expected_creds),
589 &received_creds);
590 if (sock_ret < 0) {
591 PERROR("Failed to receive credentials");
592 goto error;
593 }
594
595 wait_pid_ret = waitpid(fork_ret, &child_status, 0);
596 if (wait_pid_ret == -1) {
597 PERROR("Failed to wait for termination of child process");
598 goto error;
599 }
600 if (!WIFEXITED(child_status) || WEXITSTATUS(child_status)) {
601 diag("Child process reported an error, test failed");
602 goto error;
603 }
604
605 ok(expected_creds.euid == received_creds.uid,
606 "Received the expected effective uid (%d == %d)",
607 expected_creds.euid,
608 received_creds.uid);
609 ok(expected_creds.egid == received_creds.gid,
610 "Received the expected effective gid (%d == %d)",
611 expected_creds.egid,
612 received_creds.gid);
613 ok(expected_creds.pid == received_creds.pid,
614 "Received the expected pid (%d == %d)",
615 expected_creds.pid,
616 received_creds.pid);
617 }
618
619 error:
620 if (parent_socket >= 0) {
621 ret = close(parent_socket);
622 if (ret) {
623 PERROR("Failed to close parent socket");
624 }
625 }
626
627 if (fork_ret == 0) {
628 /* Prevent libtap from printing a result for the child. */
629 fclose(stdout);
630 fclose(stderr);
631
632 /* Child exits at the end of this test. */
633 exit(0);
634 } else if (parent_socket >= 0) {
635 if (child_connection_socket >= 0) {
636 ret = close(child_connection_socket);
637 if (ret) {
638 PERROR("Failed to close child connection socket");
639 }
640 }
641
642 ret = unlink(socket_path);
643 if (ret) {
644 PERROR("Failed to unlink socket at path `%s`", socket_path);
645 }
646
647 ret = rmdir(socket_dir_path);
648 if (ret) {
649 PERROR("Failed to remove test directory at `%s`", socket_dir_path);
650 }
651 }
652 }
653
654 int main(void)
655 {
656 plan_tests(TEST_COUNT);
657
658 test_high_fd_count(HIGH_FD_COUNT);
659 test_one_fd_per_message(MESSAGE_COUNT);
660 test_receive_in_chunks(LARGE_PAYLOAD_SIZE, LARGE_PAYLOAD_RECV_SIZE);
661 test_creds_passing();
662
663 return exit_status();
664 }
This page took 0.049959 seconds and 4 git commands to generate.