Update ChangeLog.old/ChangeLog.23.
[glibc.git] / posix / tst-_Fork.c
blob43a65c0fea676d093e367a29d3ef9f4a7944ac18
1 /* Basic tests for _Fork.
2 Copyright (C) 2021 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>
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>
30 /* For single-thread, _Fork behaves like fork. */
31 static int
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);
49 pid_t pid = _Fork ();
50 TEST_VERIFY_EXIT (pid != -1);
51 if (pid == 0)
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);
66 xclose (tempfd);
68 _exit (EXIT_SUCCESS);
71 int status;
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);
84 return 0;
88 static volatile sig_atomic_t sigusr1_handler_ran;
89 #define SIG_PID_EXIT_CODE 20
91 static bool atfork_prepare_var;
92 static bool atfork_parent_var;
93 static bool atfork_child_var;
95 static void
96 atfork_prepare (void)
98 atfork_prepare_var = true;
101 static void
102 atfork_parent (void)
104 atfork_parent_var = true;
107 static void
108 atfork_child (void)
110 atfork_child_var = true;
113 /* Different than fork, _Fork does not execute any pthread_atfork
114 handlers. */
115 static int
116 singlethread_atfork_test (void)
118 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
119 singlethread_test ();
120 TEST_VERIFY (!atfork_prepare_var);
121 TEST_VERIFY (!atfork_parent_var);
122 TEST_VERIFY (!atfork_child_var);
124 return 0;
127 static void *
128 mt_atfork_test (void *args)
130 singlethread_atfork_test ();
132 return NULL;
135 static int
136 multithread_atfork_test (void)
138 pthread_t thr = xpthread_create (NULL, mt_atfork_test, NULL);
139 xpthread_join (thr);
141 return 0;
145 static int
146 do_test (void)
148 singlethread_atfork_test ();
149 multithread_atfork_test ();
151 return 0;
154 #include <support/test-driver.c>