1 /*
2 * librdkafka - Apache Kafka C library
3 *
4 * Copyright (c) 2012-2015, Magnus Edenhill
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "test.h"
30 #include "rdkafka.h"
31
32 #ifndef _WIN32
33 #include <unistd.h>
34 #include <sys/wait.h>
35 #endif
36
37 /**
38 * @brief Forking a threaded process will not transfer threads (such as
39 * librdkafka's background threads) to the child process.
40 * There is no way such a forked client instance will work
41 * in the child process, but it should not crash on destruction: #1674
42 */
43
main_0079_fork(int argc,char ** argv)44 int main_0079_fork (int argc, char **argv) {
45
46 #if __SANITIZE_ADDRESS__
47 TEST_SKIP("AddressSanitizer is enabled: this test leaks memory (due to fork())\n");
48 return 0;
49 #endif
50 #ifdef _WIN32
51 TEST_SKIP("No fork() support on Windows");
52 return 0;
53 #else
54 pid_t pid;
55 rd_kafka_t *rk;
56 int status;
57
58 rk = test_create_producer();
59
60 rd_kafka_producev(rk,
61 RD_KAFKA_V_TOPIC("atopic"),
62 RD_KAFKA_V_VALUE("hi", 2),
63 RD_KAFKA_V_END);
64
65 pid = fork();
66 TEST_ASSERT(pid != 1, "fork() failed: %s", strerror(errno));
67
68 if (pid == 0) {
69 /* Child process */
70
71 /* This call will enqueue the message on a queue
72 * which is not served by any thread, but it should not crash */
73 rd_kafka_producev(rk,
74 RD_KAFKA_V_TOPIC("atopic"),
75 RD_KAFKA_V_VALUE("hello", 5),
76 RD_KAFKA_V_END);
77
78 /* Don't crash on us */
79 rd_kafka_destroy(rk);
80
81 exit(0);
82 }
83
84 /* Parent process, wait for child to exit cleanly. */
85 if (waitpid(pid, &status, 0) == -1)
86 TEST_FAIL("waitpid(%d) failed: %s", (int)pid, strerror(errno));
87
88 if (!WIFEXITED(status) ||
89 WEXITSTATUS(status) != 0)
90 TEST_FAIL("child exited with status %d", WEXITSTATUS(status));
91
92 rd_kafka_destroy(rk);
93
94 return 0;
95 #endif
96 }
97