2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
28 static const char testdata
[] = "This is a test";
29 static const char testdata2
[] = "And here we go again";
35 const char *tmpdir
= getenv ("TMPDIR");
45 if (tmpdir
== NULL
|| *tmpdir
== '\0')
47 tmpdirlen
= strlen (tmpdir
);
49 name
= (char *) malloc (tmpdirlen
+ strlen ("/forkXXXXXX") + 1);
51 error (EXIT_FAILURE
, errno
, "cannot allocate file name");
53 mempcpy (mempcpy (name
, tmpdir
, tmpdirlen
),
54 "/forkXXXXXX", sizeof ("/forkXXXXXX"));
56 /* Open our test file. */
59 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
61 /* Make sure it gets removed. */
64 /* Write some data. */
65 if (write (fd
, testdata
, strlen (testdata
)) != strlen (testdata
))
66 error (EXIT_FAILURE
, errno
, "cannot write test data");
68 /* Get the position in the stream. */
69 off
= lseek (fd
, 0, SEEK_CUR
);
70 if (off
== (off_t
) -1 || off
!= strlen (testdata
))
71 error (EXIT_FAILURE
, errno
, "wrong file position");
73 /* Get the parent PID. */
76 /* Now fork of the process. */
80 /* One little test first: the PID must have changed. */
81 if (getpid () == ppid
)
82 error (EXIT_FAILURE
, 0, "child and parent have same PID");
84 /* Test the `getppid' function. */
86 if (pid
== (pid_t
) -1 ? errno
!= ENOSYS
: pid
!= ppid
)
87 error (EXIT_FAILURE
, 0,
88 "getppid returned wrong PID (%ld, should be %ld)",
89 (long int) pid
, (long int) ppid
);
91 /* This is the child. First get the position of the descriptor. */
92 off
= lseek (fd
, 0, SEEK_CUR
);
93 if (off
== (off_t
) -1 || off
!= strlen (testdata
))
94 error (EXIT_FAILURE
, errno
, "wrong file position in child");
96 /* Reset the position. */
97 if (lseek (fd
, 0, SEEK_SET
) != 0)
98 error (EXIT_FAILURE
, errno
, "cannot reset position in child");
101 if (read (fd
, buf
, sizeof buf
) != strlen (testdata
))
102 error (EXIT_FAILURE
, errno
, "cannot read data in child");
104 /* Compare the data. */
105 if (memcmp (buf
, testdata
, strlen (testdata
)) != 0)
106 error (EXIT_FAILURE
, 0, "data comparison failed in child");
108 /* Reset position again. */
109 if (lseek (fd
, 0, SEEK_SET
) != 0)
110 error (EXIT_FAILURE
, errno
, "cannot reset position again in child");
112 /* Write new data. */
113 if (write (fd
, testdata2
, strlen (testdata2
)) != strlen (testdata2
))
114 error (EXIT_FAILURE
, errno
, "cannot write new data in child");
116 /* Close the file. This must not remove it. */
122 /* Something went wrong. */
123 error (EXIT_FAILURE
, errno
, "cannot fork");
125 /* Wait for the child. */
126 if (waitpid (pid
, &status
, 0) != pid
)
127 error (EXIT_FAILURE
, 0, "Oops, wrong test program terminated");
129 if (WTERMSIG (status
) != 0)
130 error (EXIT_FAILURE
, 0, "Child terminated incorrectly");
131 status
= WEXITSTATUS (status
);
135 /* Test whether the child wrote the right data. First test the
136 position. It must be the same as in the child. */
137 if (lseek (fd
, 0, SEEK_CUR
) != strlen (testdata2
))
138 error (EXIT_FAILURE
, 0, "file position not changed");
140 if (lseek (fd
, 0, SEEK_SET
) != 0)
141 error (EXIT_FAILURE
, errno
, "cannot reset file position");
143 if (read (fd
, buf
, sizeof buf
) != strlen (testdata2
))
144 error (EXIT_FAILURE
, errno
, "cannot read new data");
146 if (memcmp (buf
, testdata2
, strlen (testdata2
)) != 0)
147 error (EXIT_FAILURE
, 0, "new data not read correctly");