1/*
2 Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "testutil.hpp"
31#include "testutil_unity.hpp"
32
33#include <assert.h>
34
35#include <stdlib.h>
36#include <unistd.h>
37#include <sys/wait.h>
38
39SETUP_TEARDOWN_TESTCONTEXT
40
41char connect_address[MAX_SOCKET_STRING];
42
43#define NUM_MESSAGES 5
44
45void test_fork ()
46{
47#if !defined(ZMQ_HAVE_WINDOWS)
48 // Create and bind pull socket to receive messages
49 void *pull = test_context_socket (ZMQ_PULL);
50 bind_loopback_ipv4 (pull, connect_address, sizeof connect_address);
51
52 int pid = fork ();
53 if (pid == 0) {
54 // use regular assertions in the child process
55
56 // Child process
57 // Immediately close parent sockets and context
58 zmq_close (pull);
59 zmq_ctx_term (get_test_context ());
60
61 // Create new context, socket, connect and send some messages
62 void *child_ctx = zmq_ctx_new ();
63 assert (child_ctx);
64 void *push = zmq_socket (child_ctx, ZMQ_PUSH);
65 assert (push);
66 int rc = zmq_connect (push, connect_address);
67 assert (rc == 0);
68 int count;
69 for (count = 0; count < NUM_MESSAGES; count++)
70 zmq_send (push, "Hello", 5, 0);
71
72 zmq_close (push);
73 zmq_ctx_destroy (child_ctx);
74 exit (0);
75 } else {
76 // Parent process
77 int count;
78 for (count = 0; count < NUM_MESSAGES; count++) {
79 recv_string_expect_success (pull, "Hello", 0);
80 }
81 int child_status;
82 while (true) {
83 int rc = waitpid (pid, &child_status, 0);
84 if (rc == -1 && errno == EINTR)
85 continue;
86 TEST_ASSERT_GREATER_THAN (0, rc);
87 // Verify the status code of the child was zero
88 TEST_ASSERT_EQUAL (0, WEXITSTATUS (child_status));
89 break;
90 }
91 test_context_socket_close (pull);
92 }
93#endif
94}
95
96int main (void)
97{
98 setup_test_environment ();
99
100 UNITY_BEGIN ();
101 RUN_TEST (test_fork);
102 return UNITY_END ();
103}
104