1/* Basic tests for _Fork.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <array_length.h>
20#include <stdlib.h>
21#include <string.h>
22#include <pthread.h>
23#include <stdbool.h>
24#include <support/check.h>
25#include <support/xsignal.h>
26#include <support/temp_file.h>
27#include <support/xunistd.h>
28#include <support/xthread.h>
29
30/* For single-thread, _Fork behaves like fork. */
31static int
32singlethread_test (void)
33{
34 const char testdata1[] = "abcdefghijklmnopqrtuvwxz";
35 enum { testdatalen1 = array_length (testdata1) };
36 const char testdata2[] = "01234567890";
37 enum { testdatalen2 = array_length (testdata2) };
38
39 pid_t ppid = getpid ();
40
41 int tempfd = create_temp_file (base: "tst-_Fork", NULL);
42
43 /* Check if the opened file is shared between process by read and write
44 some data on parent and child processes. */
45 xwrite (tempfd, testdata1, testdatalen1);
46 off_t off = xlseek (fd: tempfd, offset: 0, SEEK_CUR);
47 TEST_COMPARE (off, testdatalen1);
48
49 pid_t pid = _Fork ();
50 TEST_VERIFY_EXIT (pid != -1);
51 if (pid == 0)
52 {
53 TEST_VERIFY_EXIT (getpid () != ppid);
54 TEST_COMPARE (getppid(), ppid);
55
56 TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen1);
57
58 xlseek (fd: tempfd, offset: 0, SEEK_SET);
59 char buf[testdatalen1];
60 TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen1);
61 TEST_COMPARE_BLOB (buf, testdatalen1, testdata1, testdatalen1);
62
63 xlseek (fd: tempfd, offset: 0, SEEK_SET);
64 xwrite (tempfd, testdata2, testdatalen2);
65
66 xclose (tempfd);
67
68 _exit (EXIT_SUCCESS);
69 }
70
71 int status;
72 xwaitpid (pid, status: &status, flags: 0);
73 TEST_VERIFY (WIFEXITED (status));
74 TEST_COMPARE (WEXITSTATUS (status), EXIT_SUCCESS);
75
76 TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen2);
77
78 xlseek (fd: tempfd, offset: 0, SEEK_SET);
79 char buf[testdatalen2];
80 TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen2);
81
82 TEST_COMPARE_BLOB (buf, testdatalen2, testdata2, testdatalen2);
83
84 return 0;
85}
86
87
88static volatile sig_atomic_t sigusr1_handler_ran;
89#define SIG_PID_EXIT_CODE 20
90
91static bool atfork_prepare_var;
92static bool atfork_parent_var;
93static bool atfork_child_var;
94
95static void
96atfork_prepare (void)
97{
98 atfork_prepare_var = true;
99}
100
101static void
102atfork_parent (void)
103{
104 atfork_parent_var = true;
105}
106
107static void
108atfork_child (void)
109{
110 atfork_child_var = true;
111}
112
113/* Different than fork, _Fork does not execute any pthread_atfork
114 handlers. */
115static int
116singlethread_atfork_test (void)
117{
118 pthread_atfork (prepare: atfork_prepare, parent: atfork_parent, child: atfork_child);
119 singlethread_test ();
120 TEST_VERIFY (!atfork_prepare_var);
121 TEST_VERIFY (!atfork_parent_var);
122 TEST_VERIFY (!atfork_child_var);
123
124 return 0;
125}
126
127static void *
128mt_atfork_test (void *args)
129{
130 singlethread_atfork_test ();
131
132 return NULL;
133}
134
135static int
136multithread_atfork_test (void)
137{
138 pthread_t thr = xpthread_create (NULL, thread_func: mt_atfork_test, NULL);
139 xpthread_join (thr);
140
141 return 0;
142}
143
144
145static int
146do_test (void)
147{
148 singlethread_atfork_test ();
149 multithread_atfork_test ();
150
151 return 0;
152}
153
154#include <support/test-driver.c>
155

source code of glibc/posix/tst-_Fork.c