1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/* Smoke test for SCM_RIGHTS.
Copyright (C) 2021-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* This test passes a file descriptor from a subprocess to the parent
process, using recvmsg/sendmsg or recvmmsg/sendmmsg. */
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <support/check.h>
#include <support/xunistd.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
/* String sent over the socket. */
static char DATA[] = "descriptor";
/* Path that is to be opened and sent over the socket. */
#define PATH "/etc"
/* True if sendmmsg/recvmmsg is to be used. */
static bool use_multi_call;
/* The pair of sockets used for coordination. The subprocess uses
sockets[1]. */
static int sockets[2];
/* Subprocess side of one send/receive test. */
_Noreturn static void
subprocess (void)
{
/* The file descriptor to send. */
int fd = xopen (PATH, O_RDONLY, 0);
struct iovec iov = { .iov_base = DATA, .iov_len = sizeof (DATA) };
union
{
struct cmsghdr header;
char bytes[CMSG_SPACE (sizeof (int))];
} cmsg_storage;
struct mmsghdr mmhdr =
{
.msg_hdr =
{
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsg_storage.bytes,
.msg_controllen = sizeof (cmsg_storage),
},
};
/* Configure the file descriptor for sending. */
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN (sizeof (int));
memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
mmhdr.msg_hdr.msg_controllen = cmsg->cmsg_len;
/* Perform the send operation. */
int ret;
if (use_multi_call)
{
ret = sendmmsg (sockets[1], &mmhdr, 1, 0);
if (ret >= 0)
ret = mmhdr.msg_len;
}
else
ret = sendmsg (sockets[1], &mmhdr.msg_hdr, 0);
TEST_COMPARE (ret, sizeof (DATA));
xclose (fd);
/* Stop the process from exiting. */
while (true)
pause ();
}
/* Performs one send/receive test. */
static void
one_test (void)
{
TEST_COMPARE (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets), 0);
pid_t pid = xfork ();
if (pid == 0)
subprocess ();
char data_storage[sizeof (DATA) + 1];
struct iovec iov =
{
.iov_base = data_storage,
.iov_len = sizeof (data_storage)
};
union
{
struct cmsghdr header;
char bytes[CMSG_SPACE (sizeof (int))];
} cmsg_storage;
struct mmsghdr mmhdr =
{
.msg_hdr =
{
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsg_storage.bytes,
.msg_controllen = sizeof (cmsg_storage),
},
};
/* Set up the space for receiving the file descriptor. */
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mmhdr.msg_hdr);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN (sizeof (int));
mmhdr.msg_hdr.msg_controllen = cmsg->cmsg_len;
/* Perform the receive operation. */
int ret;
if (use_multi_call)
{
ret = recvmmsg (sockets[0], &mmhdr, 1, 0, NULL);
if (ret >= 0)
ret = mmhdr.msg_len;
}
else
ret = recvmsg (sockets[0], &mmhdr.msg_hdr, 0);
TEST_COMPARE (ret, sizeof (DATA));
TEST_COMPARE_BLOB (data_storage, sizeof (DATA), DATA, sizeof (DATA));
/* Extract the file descriptor. */
TEST_VERIFY (CMSG_FIRSTHDR (&mmhdr.msg_hdr) != NULL);
TEST_COMPARE (CMSG_FIRSTHDR (&mmhdr.msg_hdr)->cmsg_len,
CMSG_LEN (sizeof (int)));
TEST_VERIFY (&cmsg_storage.header == CMSG_FIRSTHDR (&mmhdr.msg_hdr));
int fd;
memcpy (&fd, CMSG_DATA (CMSG_FIRSTHDR (&mmhdr.msg_hdr)), sizeof (fd));
/* Verify the received file descriptor. */
TEST_VERIFY (fd > 2);
struct stat64 st_fd;
TEST_COMPARE (fstat64 (fd, &st_fd), 0);
struct stat64 st_path;
TEST_COMPARE (stat64 (PATH, &st_path), 0);
TEST_COMPARE (st_fd.st_ino, st_path.st_ino);
TEST_COMPARE (st_fd.st_dev, st_path.st_dev);
xclose (fd);
/* Terminate the subprocess. */
TEST_COMPARE (kill (pid, SIGUSR1), 0);
int status;
TEST_COMPARE (xwaitpid (pid, &status, 0), pid);
TEST_VERIFY (WIFSIGNALED (status));
TEST_COMPARE (WTERMSIG (status), SIGUSR1);
xclose (sockets[0]);
xclose (sockets[1]);
}
static int
do_test (void)
{
one_test ();
use_multi_call = true;
one_test ();
return 0;
}
#include <support/test-driver.c>
|