s3:torture: in LOCAL-MESSAGING-FDPASS2, close fds after passing them
[Samba.git] / source3 / torture / test_messaging_fd_passing.c
blob5b3099e7639f276f41d153e970d1c3a02ebf61fb
1 /*
2 Unix SMB/CIFS implementation.
3 Test for fd passing with messaging
5 Copyright (C) Michael Adam 2014
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.
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.
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/>.
21 #include "includes.h"
22 #include "torture/proto.h"
23 #include "lib/util/tevent_unix.h"
24 #include "messages.h"
26 /**
27 * test fdpass1:
29 * Try to pass an fd to the sending process - fails.
31 bool run_messaging_fdpass1(int dummy)
33 struct tevent_context *ev = NULL;
34 struct messaging_context *msg_ctx = NULL;
35 bool retval = false;
36 int pipe_fds[2];
37 int pass_fds[1] = { 0 };
38 int ret;
39 NTSTATUS status;
40 struct server_id dst;
41 TALLOC_CTX *frame = talloc_stackframe();
43 ev = samba_tevent_context_init(frame);
44 if (ev == NULL) {
45 fprintf(stderr, "tevent_context_init failed\n");
46 goto fail;
48 msg_ctx = messaging_init(ev, ev);
49 if (msg_ctx == NULL) {
50 fprintf(stderr, "messaging_init failed\n");
51 goto fail;
54 dst = messaging_server_id(msg_ctx);
56 ret = pipe(pipe_fds);
57 if (ret != 0) {
58 perror("pipe failed");
59 goto fail;
62 pass_fds[0] = pipe_fds[0];
64 status = messaging_send_iov(msg_ctx, dst, MSG_PING, NULL, 0,
65 pass_fds, 1);
66 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
67 fprintf(stderr,
68 "messaging_send_iov gave: %s\n", nt_errstr(status));
69 goto fail;
72 retval = true;
74 fail:
75 TALLOC_FREE(frame);
76 return retval;
79 /**
80 * test fdpass2:
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
93 #define MSG_TORTURE_FDPASS2 0xF002
95 static bool fdpass2_filter(struct messaging_rec *rec, void *private_data)
97 if (rec->msg_type != MSG_TORTURE_FDPASS2) {
98 return false;
101 if (rec->num_fds != 2) {
102 return false;
105 return true;
108 static bool fdpass2_child(int ready_fd)
110 struct tevent_context *ev = NULL;
111 struct messaging_context *msg_ctx = NULL;
112 TALLOC_CTX *frame = talloc_stackframe();
113 bool retval = false;
114 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;
122 ev = samba_tevent_context_init(frame);
123 if (ev == NULL) {
124 fprintf(stderr, "child: tevent_context_init failed\n");
125 goto done;
128 msg_ctx = messaging_init(ev, ev);
129 if (msg_ctx == NULL) {
130 fprintf(stderr, "child: messaging_init failed\n");
131 goto done;
134 /* Tell the parent we are ready to receive mesages. */
135 bytes = write(ready_fd, &c, 1);
136 if (bytes != 1) {
137 perror("child: failed to write to error_pipe");
138 goto done;
141 subreq = messaging_filtered_read_send(frame, /* TALLOC_CTX */
142 ev, msg_ctx,
143 fdpass2_filter, NULL);
144 if (subreq == NULL) {
145 fprintf(stderr, "child: messaging_filtered_read_send failed\n");
146 goto done;
149 ok = tevent_req_poll(subreq, ev);
150 if (!ok) {
151 fprintf(stderr, "child: tevent_req_poll failed\n");
152 goto done;
155 ret = messaging_filtered_read_recv(subreq, frame, &rec);
156 TALLOC_FREE(subreq);
157 if (ret != 0) {
158 fprintf(stderr, "child: messaging_filtered_read_recv failed\n");
159 goto done;
162 SMB_ASSERT(rec->num_fds == 2);
164 up_fd = rec->fds[0];
165 down_fd = rec->fds[1];
167 bytes = read(up_fd, &c, 1);
168 if (bytes != 1) {
169 perror("child: read from up_fd failed");
170 goto done;
173 bytes = write(down_fd, &c, 1);
174 if (bytes != 1) {
175 perror("child: write to down_fd failed");
178 printf("child: done\n");
180 retval = true;
182 done:
183 TALLOC_FREE(frame);
184 return retval;
187 static bool fdpass2_parent(pid_t child_pid, int ready_fd)
189 struct tevent_context *ev = NULL;
190 struct messaging_context *msg_ctx = NULL;
191 bool retval = false;
192 int up_pipe[2];
193 int down_pipe[2];
194 int pass_fds[2] = { 0 };
195 int ret;
196 NTSTATUS status;
197 struct server_id dst;
198 TALLOC_CTX *frame = talloc_stackframe();
199 uint8_t c1 = 1, c2, c;
200 ssize_t bytes;
201 struct iovec iov;
202 DATA_BLOB blob;
204 ev = samba_tevent_context_init(frame);
205 if (ev == NULL) {
206 fprintf(stderr, "parent: tevent_context_init failed\n");
207 goto done;
210 msg_ctx = messaging_init(ev, ev);
211 if (msg_ctx == NULL) {
212 fprintf(stderr, "parent: messaging_init failed\n");
213 goto done;
216 /* wait util the child is ready to receive messages */
217 bytes = read(ready_fd, &c, 1);
218 if (bytes != 1) {
219 perror("parent: read from ready_fd failed");
220 goto done;
223 ret = pipe(up_pipe);
224 if (ret != 0) {
225 perror("parent: pipe failed for up_pipe");
226 goto done;
229 ret = pipe(down_pipe);
230 if (ret != 0) {
231 perror("parent: pipe failed for down_pipe");
232 goto done;
235 pass_fds[0] = up_pipe[0];
236 pass_fds[1] = down_pipe[1];
238 dst = messaging_server_id(msg_ctx);
239 dst.pid = child_pid;
242 * Send a certain payload with the fds, to test to test
243 * that fd-passing works when we have fragmentation and
244 * re-assembly of the datagrams.
246 blob = data_blob_talloc_zero(frame, 2*1000);
247 iov.iov_base = blob.data;
248 iov.iov_len = blob.length;
250 status = messaging_send_iov(msg_ctx, dst, MSG_TORTURE_FDPASS2, &iov, 1,
251 pass_fds, 2);
252 if (!NT_STATUS_IS_OK(status)) {
253 fprintf(stderr, "parent: messaging_send_iov failed: %s\n",
254 nt_errstr(status));
255 goto done;
258 close(up_pipe[0]);
259 close(down_pipe[1]);
261 bytes = write(up_pipe[1], &c1, 1);
262 if (bytes != 1) {
263 perror("parent: write to up pipe failed");
264 goto done;
267 bytes = read(down_pipe[0], &c2, 1);
268 if (bytes != 1) {
269 perror("parent: read from down pipe failed");
270 goto done;
273 if (c1 != c2) {
274 fprintf(stderr, "parent: c1[%d] != c2[%d]\n", c1, c2);
275 goto done;
278 ret = waitpid(child_pid, NULL, 0);
279 if (ret == -1) {
280 perror("parent: waitpid failed");
281 goto done;
284 retval = true;
286 done:
287 TALLOC_FREE(frame);
288 return retval;
291 bool run_messaging_fdpass2(int dummy)
293 bool retval = false;
294 pid_t child_pid;
295 int ready_pipe[2];
296 int ret;
298 ret = pipe(ready_pipe);
299 if (ret != 0) {
300 perror("parent: pipe failed for ready_pipe");
301 return retval;
304 child_pid = fork();
305 if (child_pid == -1) {
306 perror("fork failed");
307 } else if (child_pid == 0) {
308 close(ready_pipe[0]);
309 retval = fdpass2_child(ready_pipe[1]);
310 } else {
311 close(ready_pipe[1]);
312 retval = fdpass2_parent(child_pid, ready_pipe[0]);
315 return retval;