2 Copyright (C) 2000-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/>. */
27 static const char testdata[] = "This is a test";
28 static const char testdata2[] = "And here we go again";
34 const char *tmpdir = getenv ("TMPDIR");
44 if (tmpdir == NULL || *tmpdir == '\0')
46 tmpdirlen = strlen (tmpdir);
48 name = (char *) malloc (tmpdirlen + strlen ("/forkXXXXXX") + 1);
50 error (EXIT_FAILURE, errno, "cannot allocate file name");
52 mempcpy (mempcpy (name, tmpdir, tmpdirlen),
53 "/forkXXXXXX", sizeof ("/forkXXXXXX"));
55 /* Open our test file. */
58 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
60 /* Make sure it gets removed. */
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. */
75 /* Now fork of the process. */
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. */
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");
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. */
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);
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");