manual: Clarify undefined behavior of feenableexcept (BZ 31019)
[glibc.git] / posix / tst-fork.c
blob3e1abf17572966e90bf6599873de3a0384f199ad
1 /* Tests for fork.
2 Copyright (C) 2000-2023 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 <errno.h>
20 #include <error.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <wait.h>
27 static const char testdata[] = "This is a test";
28 static const char testdata2[] = "And here we go again";
31 int
32 main (void)
34 const char *tmpdir = getenv ("TMPDIR");
35 char buf[100];
36 size_t tmpdirlen;
37 char *name;
38 int fd;
39 pid_t pid;
40 pid_t ppid;
41 off_t off;
42 int status;
44 if (tmpdir == NULL || *tmpdir == '\0')
45 tmpdir = "/tmp";
46 tmpdirlen = strlen (tmpdir);
48 name = (char *) malloc (tmpdirlen + strlen ("/forkXXXXXX") + 1);
49 if (name == NULL)
50 error (EXIT_FAILURE, errno, "cannot allocate file name");
52 mempcpy (mempcpy (name, tmpdir, tmpdirlen),
53 "/forkXXXXXX", sizeof ("/forkXXXXXX"));
55 /* Open our test file. */
56 fd = mkstemp (name);
57 if (fd == -1)
58 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
60 /* Make sure it gets removed. */
61 unlink (name);
63 /* Write some data. */
64 if (write (fd, testdata, strlen (testdata)) != strlen (testdata))
65 error (EXIT_FAILURE, errno, "cannot write test data");
67 /* Get the position in the stream. */
68 off = lseek (fd, 0, SEEK_CUR);
69 if (off == (off_t) -1 || off != strlen (testdata))
70 error (EXIT_FAILURE, errno, "wrong file position");
72 /* Get the parent PID. */
73 ppid = getpid ();
75 /* Now fork of the process. */
76 pid = fork ();
77 if (pid == 0)
79 /* One little test first: the PID must have changed. */
80 if (getpid () == ppid)
81 error (EXIT_FAILURE, 0, "child and parent have same PID");
83 /* Test the `getppid' function. */
84 pid = getppid ();
85 if (pid == (pid_t) -1 ? errno != ENOSYS : pid != ppid)
86 error (EXIT_FAILURE, 0,
87 "getppid returned wrong PID (%ld, should be %ld)",
88 (long int) pid, (long int) ppid);
90 /* This is the child. First get the position of the descriptor. */
91 off = lseek (fd, 0, SEEK_CUR);
92 if (off == (off_t) -1 || off != strlen (testdata))
93 error (EXIT_FAILURE, errno, "wrong file position in child");
95 /* Reset the position. */
96 if (lseek (fd, 0, SEEK_SET) != 0)
97 error (EXIT_FAILURE, errno, "cannot reset position in child");
99 /* Read the data. */
100 if (read (fd, buf, sizeof buf) != strlen (testdata))
101 error (EXIT_FAILURE, errno, "cannot read data in child");
103 /* Compare the data. */
104 if (memcmp (buf, testdata, strlen (testdata)) != 0)
105 error (EXIT_FAILURE, 0, "data comparison failed in child");
107 /* Reset position again. */
108 if (lseek (fd, 0, SEEK_SET) != 0)
109 error (EXIT_FAILURE, errno, "cannot reset position again in child");
111 /* Write new data. */
112 if (write (fd, testdata2, strlen (testdata2)) != strlen (testdata2))
113 error (EXIT_FAILURE, errno, "cannot write new data in child");
115 /* Close the file. This must not remove it. */
116 close (fd);
118 _exit (0);
120 else if (pid < 0)
121 /* Something went wrong. */
122 error (EXIT_FAILURE, errno, "cannot fork");
124 /* Wait for the child. */
125 if (waitpid (pid, &status, 0) != pid)
126 error (EXIT_FAILURE, 0, "Oops, wrong test program terminated");
128 if (WTERMSIG (status) != 0)
129 error (EXIT_FAILURE, 0, "Child terminated incorrectly");
130 status = WEXITSTATUS (status);
132 if (status == 0)
134 /* Test whether the child wrote the right data. First test the
135 position. It must be the same as in the child. */
136 if (lseek (fd, 0, SEEK_CUR) != strlen (testdata2))
137 error (EXIT_FAILURE, 0, "file position not changed");
139 if (lseek (fd, 0, SEEK_SET) != 0)
140 error (EXIT_FAILURE, errno, "cannot reset file position");
142 if (read (fd, buf, sizeof buf) != strlen (testdata2))
143 error (EXIT_FAILURE, errno, "cannot read new data");
145 if (memcmp (buf, testdata2, strlen (testdata2)) != 0)
146 error (EXIT_FAILURE, 0, "new data not read correctly");
149 return status;