s3:torture: work on LOCAL-MESSAGING-FDPASS2
authorMichael Adam <obnox@samba.org>
Tue, 23 Sep 2014 07:53:15 +0000 (09:53 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 24 Sep 2014 06:44:12 +0000 (08:44 +0200)
- parent: fork
- parent: create up and down pipes,
- parent: pass read end of up pipe and write end of down pipe to child
- parent: write to up pipe
- child:  read from up pipe
- child:  write to down pipe
- parent: read from down pipe

Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source3/torture/proto.h
source3/torture/test_messaging_fd_passing.c
source3/torture/torture.c

index 0402f1a7c9ff6d6afb63bb094b34fb35029843ab..a1d4151a0d929e34d4a76485d6e14e8c67d5c851 100644 (file)
@@ -117,6 +117,7 @@ bool run_messaging_read1(int dummy);
 bool run_messaging_read2(int dummy);
 bool run_messaging_read3(int dummy);
 bool run_messaging_fdpass1(int dummy);
+bool run_messaging_fdpass2(int dummy);
 bool run_oplock_cancel(int dummy);
 
 #endif /* __TORTURE_H__ */
index b860939d46ebc4b566422bd80ab364e3032ad02c..1a296e441ccf6eb00d8548fa1e977ea614df7c74 100644 (file)
@@ -75,3 +75,239 @@ fail:
        TALLOC_FREE(frame);
        return retval;
 }
+
+/**
+ * test fdpass2:
+ *
+ * - parent: create a child
+ * - parent: create a two pipes in the parent: up and down
+ * - parent: pass the up pipe's reading end and the down pipe's writing
+ *   end to the child and close them
+ * - parent: write a number into the up pipe's writing end
+ * - child: read number from the passed reading fd (up)
+ * - child: write the read number to the passed writing fd (down)
+ * - parent: read number from the down pipe's reading end and compare with
+ *   original number
+ */
+
+#define MSG_TORTURE_FDPASS2 0xF002
+
+static bool fdpass2_filter(struct messaging_rec *rec, void *private_data)
+{
+       if (rec->msg_type != MSG_TORTURE_FDPASS2) {
+               return false;
+       }
+
+       if (rec->num_fds != 2) {
+               return false;
+       }
+
+       return true;
+}
+
+static bool fdpass2_child(int ready_fd)
+{
+       struct tevent_context *ev = NULL;
+       struct messaging_context *msg_ctx = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+       bool retval = false;
+       uint8_t c = 1;
+       struct tevent_req *subreq;
+       int ret;
+       ssize_t bytes;
+       int up_fd, down_fd;
+       struct messaging_rec *rec;
+       bool ok;
+
+       ev = samba_tevent_context_init(frame);
+       if (ev == NULL) {
+               fprintf(stderr, "child: tevent_context_init failed\n");
+               goto done;
+       }
+
+       msg_ctx = messaging_init(ev, ev);
+       if (msg_ctx == NULL) {
+               fprintf(stderr, "child: messaging_init failed\n");
+               goto done;
+       }
+
+       /* Tell the parent we are ready to receive mesages. */
+       bytes = write(ready_fd, &c, 1);
+       if (bytes != 1) {
+               perror("child: failed to write to error_pipe");
+               goto done;
+       }
+
+       subreq = messaging_filtered_read_send(frame, /* TALLOC_CTX */
+                                             ev, msg_ctx,
+                                             fdpass2_filter, NULL);
+       if (subreq == NULL) {
+               fprintf(stderr, "child: messaging_filtered_read_send failed\n");
+               goto done;
+       }
+
+       ok = tevent_req_poll(subreq, ev);
+       if (!ok) {
+               fprintf(stderr, "child: tevent_req_poll failed\n");
+               goto done;
+       }
+
+       ret = messaging_filtered_read_recv(subreq, frame, &rec);
+       TALLOC_FREE(subreq);
+       if (ret != 0) {
+               fprintf(stderr, "child: messaging_filtered_read_recv failed\n");
+               goto done;
+       }
+
+       SMB_ASSERT(rec->num_fds == 2);
+
+       up_fd = rec->fds[0];
+       down_fd = rec->fds[1];
+
+       bytes = read(up_fd, &c, 1);
+       if (bytes != 1) {
+               perror("child: read from up_fd failed");
+               goto done;
+       }
+
+       bytes = write(down_fd, &c, 1);
+       if (bytes != 1) {
+               perror("child: write to down_fd failed");
+       }
+
+       printf("child: done\n");
+
+       retval = true;
+
+done:
+       TALLOC_FREE(frame);
+       return retval;
+}
+
+static bool fdpass2_parent(pid_t child_pid, int ready_fd)
+{
+       struct tevent_context *ev = NULL;
+       struct messaging_context *msg_ctx = NULL;
+       bool retval = false;
+       int up_pipe[2];
+       int down_pipe[2];
+       int pass_fds[2] = { 0 };
+       int ret;
+       NTSTATUS status;
+       struct server_id dst;
+       TALLOC_CTX *frame = talloc_stackframe();
+       uint8_t c1 = 1, c2, c;
+       ssize_t bytes;
+       struct iovec iov;
+       DATA_BLOB blob;
+
+       ev = samba_tevent_context_init(frame);
+       if (ev == NULL) {
+               fprintf(stderr, "parent: tevent_context_init failed\n");
+               goto done;
+       }
+
+       msg_ctx = messaging_init(ev, ev);
+       if (msg_ctx == NULL) {
+               fprintf(stderr, "parent: messaging_init failed\n");
+               goto done;
+       }
+
+       /* wait util the child is ready to receive messages */
+       bytes = read(ready_fd, &c, 1);
+       if (bytes != 1) {
+               perror("parent: read from ready_fd failed");
+               goto done;
+       }
+
+       ret = pipe(up_pipe);
+       if (ret != 0) {
+               perror("parent: pipe failed for up_pipe");
+               goto done;
+       }
+
+       ret = pipe(down_pipe);
+       if (ret != 0) {
+               perror("parent: pipe failed for down_pipe");
+               goto done;
+       }
+
+       pass_fds[0] = up_pipe[0];
+       pass_fds[1] = down_pipe[1];
+
+       dst = messaging_server_id(msg_ctx);
+       dst.pid = child_pid;
+
+       /*
+        * Send a certain payload with the fds, to test to test
+        * that fd-passing works when we have fragmentation and
+        * re-assembly of the datagrams.
+        */
+       blob = data_blob_talloc_zero(frame, 2*1000);
+       iov.iov_base = blob.data;
+       iov.iov_len  = blob.length;
+
+       status = messaging_send_iov(msg_ctx, dst, MSG_TORTURE_FDPASS2, &iov, 1,
+                                   pass_fds, 2);
+       if (!NT_STATUS_IS_OK(status)) {
+               fprintf(stderr, "parent: messaging_send_iov failed: %s\n",
+                       nt_errstr(status));
+               goto done;
+       }
+
+       bytes = write(up_pipe[1], &c1, 1);
+       if (bytes != 1) {
+               perror("parent: write to up pipe failed");
+               goto done;
+       }
+
+       bytes = read(down_pipe[0], &c2, 1);
+       if (bytes != 1) {
+               perror("parent: read from down pipe failed");
+               goto done;
+       }
+
+       if (c1 != c2) {
+               fprintf(stderr, "parent: c1[%d] != c2[%d]\n", c1, c2);
+               goto done;
+       }
+
+       ret = waitpid(child_pid, NULL, 0);
+       if (ret == -1) {
+               perror("parent: waitpid failed");
+               goto done;
+       }
+
+       retval = true;
+
+done:
+       TALLOC_FREE(frame);
+       return retval;
+}
+
+bool run_messaging_fdpass2(int dummy)
+{
+       bool retval = false;
+       pid_t child_pid;
+       int ready_pipe[2];
+       int ret;
+
+       ret = pipe(ready_pipe);
+       if (ret != 0) {
+               perror("parent: pipe failed for ready_pipe");
+               return retval;
+       }
+
+       child_pid = fork();
+       if (child_pid == -1) {
+               perror("fork failed");
+       } else if (child_pid == 0) {
+               close(ready_pipe[0]);
+               retval = fdpass2_child(ready_pipe[1]);
+       } else {
+               close(ready_pipe[1]);
+               retval = fdpass2_parent(child_pid, ready_pipe[0]);
+       }
+
+       return retval;
+}
index b3d9237ba3a35f9e39a024b9e45b802b69559ac1..ebc81db9d5f6b5d701035015c127c05831bf5f48 100644 (file)
@@ -9607,6 +9607,7 @@ static struct {
        { "LOCAL-MESSAGING-READ2", run_messaging_read2, 0 },
        { "LOCAL-MESSAGING-READ3", run_messaging_read3, 0 },
        { "LOCAL-MESSAGING-FDPASS1", run_messaging_fdpass1, 0 },
+       { "LOCAL-MESSAGING-FDPASS2", run_messaging_fdpass2, 0 },
        { "LOCAL-BASE64", run_local_base64, 0},
        { "LOCAL-RBTREE", run_local_rbtree, 0},
        { "LOCAL-MEMCACHE", run_local_memcache, 0},