Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Test for fd passing with messaging
4 :
5 : Copyright (C) Michael Adam 2014
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "torture/proto.h"
23 : #include "lib/util/tevent_unix.h"
24 : #include "messages.h"
25 :
26 : /**
27 : * test fdpass1:
28 : *
29 : * Try to pass an fd to the sending process - fails.
30 : */
31 0 : bool run_messaging_fdpass1(int dummy)
32 : {
33 0 : struct tevent_context *ev = NULL;
34 0 : struct messaging_context *msg_ctx = NULL;
35 0 : bool retval = false;
36 : int pipe_fds[2];
37 0 : int pass_fds[1] = { 0 };
38 : int ret;
39 : NTSTATUS status;
40 : struct server_id dst;
41 0 : TALLOC_CTX *frame = talloc_stackframe();
42 :
43 0 : ev = samba_tevent_context_init(frame);
44 0 : if (ev == NULL) {
45 0 : fprintf(stderr, "tevent_context_init failed\n");
46 0 : goto fail;
47 : }
48 0 : msg_ctx = messaging_init(ev, ev);
49 0 : if (msg_ctx == NULL) {
50 0 : fprintf(stderr, "messaging_init failed\n");
51 0 : goto fail;
52 : }
53 :
54 0 : dst = messaging_server_id(msg_ctx);
55 :
56 0 : ret = pipe(pipe_fds);
57 0 : if (ret != 0) {
58 0 : perror("pipe failed");
59 0 : goto fail;
60 : }
61 :
62 0 : pass_fds[0] = pipe_fds[0];
63 :
64 0 : status = messaging_send_iov(msg_ctx, dst, MSG_PING, NULL, 0,
65 : pass_fds, 1);
66 0 : if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
67 0 : fprintf(stderr,
68 : "messaging_send_iov gave: %s\n", nt_errstr(status));
69 0 : goto fail;
70 : }
71 :
72 0 : retval = true;
73 :
74 0 : fail:
75 0 : TALLOC_FREE(frame);
76 0 : return retval;
77 : }
78 :
79 : /**
80 : * test fdpass2:
81 : *
82 : * - parent: create a child
83 : * - parent: create a two pipes in the parent: up and down
84 : * - parent: pass the up pipe's reading end and the down pipe's writing
85 : * end to the child and close them
86 : * - parent: write a number into the up pipe's writing end
87 : * - child: read number from the passed reading fd (up)
88 : * - child: write the read number to the passed writing fd (down)
89 : * - parent: read number from the down pipe's reading end and compare with
90 : * original number
91 : */
92 :
93 : #define MSG_TORTURE_FDPASS2 0xF002
94 :
95 0 : static bool fdpass2_filter(struct messaging_rec *rec, void *private_data)
96 : {
97 0 : if (rec->msg_type != MSG_TORTURE_FDPASS2) {
98 0 : return false;
99 : }
100 :
101 0 : if (rec->num_fds != 2) {
102 0 : return false;
103 : }
104 :
105 0 : return true;
106 : }
107 :
108 0 : static bool fdpass2_child(int ready_fd)
109 : {
110 0 : struct tevent_context *ev = NULL;
111 0 : struct messaging_context *msg_ctx = NULL;
112 0 : TALLOC_CTX *frame = talloc_stackframe();
113 0 : bool retval = false;
114 0 : uint8_t c = 1;
115 : struct tevent_req *subreq;
116 : int ret;
117 : ssize_t bytes;
118 : int up_fd, down_fd;
119 : struct messaging_rec *rec;
120 : bool ok;
121 :
122 0 : ev = samba_tevent_context_init(frame);
123 0 : if (ev == NULL) {
124 0 : fprintf(stderr, "child: tevent_context_init failed\n");
125 0 : goto done;
126 : }
127 :
128 0 : msg_ctx = messaging_init(ev, ev);
129 0 : if (msg_ctx == NULL) {
130 0 : fprintf(stderr, "child: messaging_init failed\n");
131 0 : goto done;
132 : }
133 :
134 : /* Tell the parent we are ready to receive mesages. */
135 0 : bytes = write(ready_fd, &c, 1);
136 0 : if (bytes != 1) {
137 0 : perror("child: failed to write to ready_fd");
138 0 : goto done;
139 : }
140 :
141 0 : subreq = messaging_filtered_read_send(frame, /* TALLOC_CTX */
142 : ev, msg_ctx,
143 : fdpass2_filter, NULL);
144 0 : if (subreq == NULL) {
145 0 : fprintf(stderr, "child: messaging_filtered_read_send failed\n");
146 0 : goto done;
147 : }
148 :
149 0 : ok = tevent_req_poll(subreq, ev);
150 0 : if (!ok) {
151 0 : fprintf(stderr, "child: tevent_req_poll failed\n");
152 0 : goto done;
153 : }
154 :
155 0 : ret = messaging_filtered_read_recv(subreq, frame, &rec);
156 0 : TALLOC_FREE(subreq);
157 0 : if (ret != 0) {
158 0 : fprintf(stderr, "child: messaging_filtered_read_recv failed\n");
159 0 : goto done;
160 : }
161 :
162 0 : SMB_ASSERT(rec->num_fds == 2);
163 :
164 : /* Tell the parent we are done. */
165 0 : bytes = write(ready_fd, &c, 1);
166 0 : if (bytes != 1) {
167 0 : perror("child: failed to write to ready_fd");
168 0 : goto done;
169 : }
170 :
171 0 : up_fd = rec->fds[0];
172 0 : down_fd = rec->fds[1];
173 :
174 0 : bytes = read(up_fd, &c, 1);
175 0 : if (bytes != 1) {
176 0 : perror("child: read from up_fd failed");
177 0 : goto done;
178 : }
179 :
180 0 : bytes = write(down_fd, &c, 1);
181 0 : if (bytes != 1) {
182 0 : perror("child: write to down_fd failed");
183 : }
184 :
185 0 : printf("child: done\n");
186 :
187 0 : retval = true;
188 :
189 0 : done:
190 0 : TALLOC_FREE(frame);
191 0 : return retval;
192 : }
193 :
194 : struct child_done_state {
195 : int fd;
196 : bool done;
197 : };
198 :
199 0 : static void child_done_cb(struct tevent_context *ev,
200 : struct tevent_fd *fde,
201 : uint16_t flags,
202 : void *private_data)
203 : {
204 0 : struct child_done_state *state =
205 : (struct child_done_state *)private_data;
206 0 : char c = 0;
207 : ssize_t bytes;
208 :
209 0 : bytes = read(state->fd, &c, 1);
210 0 : if (bytes != 1) {
211 0 : perror("parent: read from ready_fd failed");
212 : }
213 :
214 0 : state->done = true;
215 0 : }
216 :
217 0 : static bool fdpass2_parent(pid_t child_pid, int ready_fd, size_t payload_size)
218 : {
219 0 : struct tevent_context *ev = NULL;
220 0 : struct messaging_context *msg_ctx = NULL;
221 0 : bool retval = false;
222 : int up_pipe[2];
223 : int down_pipe[2];
224 0 : int pass_fds[2] = { 0 };
225 : int ret;
226 : NTSTATUS status;
227 : struct server_id dst;
228 0 : TALLOC_CTX *frame = talloc_stackframe();
229 0 : uint8_t c1 = 1, c2, c;
230 : ssize_t bytes;
231 : struct iovec iov;
232 : DATA_BLOB blob;
233 : struct tevent_fd *child_done_fde;
234 : struct child_done_state child_state;
235 :
236 0 : ev = samba_tevent_context_init(frame);
237 0 : if (ev == NULL) {
238 0 : fprintf(stderr, "parent: tevent_context_init failed\n");
239 0 : goto done;
240 : }
241 :
242 0 : msg_ctx = messaging_init(ev, ev);
243 0 : if (msg_ctx == NULL) {
244 0 : fprintf(stderr, "parent: messaging_init failed\n");
245 0 : goto done;
246 : }
247 :
248 : /* wait util the child is ready to receive messages */
249 0 : bytes = read(ready_fd, &c, 1);
250 0 : if (bytes != 1) {
251 0 : perror("parent: read from ready_fd failed");
252 0 : goto done;
253 : }
254 :
255 0 : ret = pipe(up_pipe);
256 0 : if (ret != 0) {
257 0 : perror("parent: pipe failed for up_pipe");
258 0 : goto done;
259 : }
260 :
261 0 : ret = pipe(down_pipe);
262 0 : if (ret != 0) {
263 0 : perror("parent: pipe failed for down_pipe");
264 0 : goto done;
265 : }
266 :
267 0 : child_state.fd = ready_fd;
268 0 : child_state.done = false;
269 :
270 0 : child_done_fde = tevent_add_fd(ev, ev, ready_fd, TEVENT_FD_READ,
271 : child_done_cb, &child_state);
272 0 : if (child_done_fde == NULL) {
273 0 : fprintf(stderr,
274 : "parent: failed tevent_add_fd for child done\n");
275 0 : goto done;
276 : }
277 :
278 0 : pass_fds[0] = up_pipe[0];
279 0 : pass_fds[1] = down_pipe[1];
280 :
281 0 : dst = messaging_server_id(msg_ctx);
282 0 : dst.pid = child_pid;
283 :
284 : /*
285 : * Send a certain payload with the fds, to test to test
286 : * that fd-passing works when we have fragmentation and
287 : * re-assembly of the datagrams.
288 : *
289 : * Fragmentation/queuing is triggered by a certain payload
290 : * size. Payloads below that size use the fast path.
291 : */
292 0 : blob = data_blob_talloc_zero(frame, payload_size);
293 0 : iov.iov_base = blob.data;
294 0 : iov.iov_len = blob.length;
295 :
296 0 : status = messaging_send_iov(msg_ctx, dst, MSG_TORTURE_FDPASS2, &iov, 1,
297 : pass_fds, 2);
298 0 : if (!NT_STATUS_IS_OK(status)) {
299 0 : fprintf(stderr, "parent: messaging_send_iov failed: %s\n",
300 : nt_errstr(status));
301 0 : goto done;
302 : }
303 :
304 0 : printf("parent: waiting for child to confirm\n");
305 :
306 0 : while (!child_state.done) {
307 0 : ret = tevent_loop_once(ev);
308 0 : if (ret != 0) {
309 0 : fprintf(stderr, "parent: tevent_loop_once failed\n");
310 0 : goto done;
311 : }
312 : }
313 :
314 0 : printf("parent: child confirmed\n");
315 :
316 0 : close(up_pipe[0]);
317 0 : close(down_pipe[1]);
318 :
319 0 : bytes = write(up_pipe[1], &c1, 1);
320 0 : if (bytes != 1) {
321 0 : perror("parent: write to up pipe failed");
322 0 : goto done;
323 : }
324 :
325 0 : bytes = read(down_pipe[0], &c2, 1);
326 0 : if (bytes != 1) {
327 0 : perror("parent: read from down pipe failed");
328 0 : goto done;
329 : }
330 :
331 0 : if (c1 != c2) {
332 0 : fprintf(stderr, "parent: c1[%d] != c2[%d]\n", c1, c2);
333 0 : goto done;
334 : }
335 :
336 0 : ret = waitpid(child_pid, NULL, 0);
337 0 : if (ret == -1) {
338 0 : perror("parent: waitpid failed");
339 0 : goto done;
340 : }
341 :
342 0 : retval = true;
343 :
344 0 : done:
345 0 : TALLOC_FREE(frame);
346 0 : return retval;
347 : }
348 :
349 0 : static bool run_messaging_fdpass2_int(int dummy, size_t payload_size)
350 : {
351 0 : bool retval = false;
352 : pid_t child_pid;
353 : int ready_pipe[2];
354 : int ret;
355 :
356 0 : ret = pipe(ready_pipe);
357 0 : if (ret != 0) {
358 0 : perror("parent: pipe failed for ready_pipe");
359 0 : return retval;
360 : }
361 :
362 0 : child_pid = fork();
363 0 : if (child_pid == -1) {
364 0 : perror("fork failed");
365 0 : } else if (child_pid == 0) {
366 0 : close(ready_pipe[0]);
367 0 : retval = fdpass2_child(ready_pipe[1]);
368 : } else {
369 0 : close(ready_pipe[1]);
370 0 : retval = fdpass2_parent(child_pid, ready_pipe[0], payload_size);
371 : }
372 :
373 0 : return retval;
374 : }
375 :
376 0 : bool run_messaging_fdpass2(int dummy)
377 : {
378 0 : return run_messaging_fdpass2_int(dummy, 1000*1000);
379 : }
380 :
381 : /**
382 : * Variant of the FDPASS2 test that tests the non-queuing fast path
383 : * with a small payload.
384 : */
385 0 : bool run_messaging_fdpass2a(int dummy)
386 : {
387 0 : return run_messaging_fdpass2_int(dummy, 1);
388 : }
389 :
390 : /**
391 : * Variant of the FDPASS2 test that tests the non-queuing fast path
392 : * without a payload.
393 : */
394 0 : bool run_messaging_fdpass2b(int dummy)
395 : {
396 0 : return run_messaging_fdpass2_int(dummy, 0);
397 : }
|