1 /* Basic tests for _Fork.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <array_length.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>
30 /* For single-thread, _Fork behaves like fork. */
32 singlethread_test (void)
34 const char testdata1
[] = "abcdefghijklmnopqrtuvwxz";
35 enum { testdatalen1
= array_length (testdata1
) };
36 const char testdata2
[] = "01234567890";
37 enum { testdatalen2
= array_length (testdata2
) };
39 pid_t ppid
= getpid ();
41 int tempfd
= create_temp_file ("tst-_Fork", NULL
);
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 (tempfd
, 0, SEEK_CUR
);
47 TEST_COMPARE (off
, testdatalen1
);
50 TEST_VERIFY_EXIT (pid
!= -1);
53 TEST_VERIFY_EXIT (getpid () != ppid
);
54 TEST_COMPARE (getppid(), ppid
);
56 TEST_COMPARE (xlseek (tempfd
, 0, SEEK_CUR
), testdatalen1
);
58 xlseek (tempfd
, 0, SEEK_SET
);
59 char buf
[testdatalen1
];
60 TEST_COMPARE (read (tempfd
, buf
, sizeof (buf
)), testdatalen1
);
61 TEST_COMPARE_BLOB (buf
, testdatalen1
, testdata1
, testdatalen1
);
63 xlseek (tempfd
, 0, SEEK_SET
);
64 xwrite (tempfd
, testdata2
, testdatalen2
);
72 xwaitpid (pid
, &status
, 0);
73 TEST_VERIFY (WIFEXITED (status
));
74 TEST_COMPARE (WEXITSTATUS (status
), EXIT_SUCCESS
);
76 TEST_COMPARE (xlseek (tempfd
, 0, SEEK_CUR
), testdatalen2
);
78 xlseek (tempfd
, 0, SEEK_SET
);
79 char buf
[testdatalen2
];
80 TEST_COMPARE (read (tempfd
, buf
, sizeof (buf
)), testdatalen2
);
82 TEST_COMPARE_BLOB (buf
, testdatalen2
, testdata2
, testdatalen2
);
88 #define SIG_PID_EXIT_CODE 20
90 static bool atfork_prepare_var
;
91 static bool atfork_parent_var
;
92 static bool atfork_child_var
;
97 atfork_prepare_var
= true;
103 atfork_parent_var
= true;
109 atfork_child_var
= true;
112 /* Different than fork, _Fork does not execute any pthread_atfork
115 singlethread_atfork_test (void)
117 pthread_atfork (atfork_prepare
, atfork_parent
, atfork_child
);
118 singlethread_test ();
119 TEST_VERIFY (!atfork_prepare_var
);
120 TEST_VERIFY (!atfork_parent_var
);
121 TEST_VERIFY (!atfork_child_var
);
127 mt_atfork_test (void *args
)
129 singlethread_atfork_test ();
135 multithread_atfork_test (void)
137 pthread_t thr
= xpthread_create (NULL
, mt_atfork_test
, NULL
);
147 singlethread_atfork_test ();
148 multithread_atfork_test ();
153 #include <support/test-driver.c>