2 Copyright (C) 2000 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 static const char testdata
[] = "This is a test";
30 static const char testdata2
[] = "And here we go again";
36 const char *tmpdir
= getenv ("TMPDIR");
46 if (tmpdir
== NULL
|| *tmpdir
== '\0')
48 tmpdirlen
= strlen (tmpdir
);
50 name
= (char *) malloc (tmpdirlen
+ strlen ("/forkXXXXXX") + 1);
52 error (EXIT_FAILURE
, errno
, "cannot allocate file name");
54 mempcpy (mempcpy (name
, tmpdir
, tmpdirlen
),
55 "/forkXXXXXX", sizeof ("/forkXXXXXX"));
57 /* Open our test file. */
60 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
62 /* Make sure it gets removed. */
65 /* Write some data. */
66 if (write (fd
, testdata
, strlen (testdata
)) != strlen (testdata
))
67 error (EXIT_FAILURE
, errno
, "cannot write test data");
69 /* Get the position in the stream. */
70 off
= lseek (fd
, 0, SEEK_CUR
);
71 if (off
== (off_t
) -1 || off
!= strlen (testdata
))
72 error (EXIT_FAILURE
, errno
, "wrong file position");
74 /* Get the parent PID. */
77 /* Now fork of the process. */
81 /* One little test first: the PID must have changed. */
82 if (getpid () == ppid
)
83 error (EXIT_FAILURE
, 0, "child and parent have same PID");
85 /* Test the `getppid' function. */
87 if (pid
== (pid_t
) -1 ? errno
!= ENOSYS
: pid
!= ppid
)
88 error (EXIT_FAILURE
, 0,
89 "getppid returned wrong PID (%ld, should be %ld)",
90 (long int) pid
, (long int) ppid
);
92 /* This is the child. First get the position of the descriptor. */
93 off
= lseek (fd
, 0, SEEK_CUR
);
94 if (off
== (off_t
) -1 || off
!= strlen (testdata
))
95 error (EXIT_FAILURE
, errno
, "wrong file position in child");
97 /* Reset the position. */
98 if (lseek (fd
, 0, SEEK_SET
) != 0)
99 error (EXIT_FAILURE
, errno
, "cannot reset position in child");
102 if (read (fd
, buf
, sizeof buf
) != strlen (testdata
))
103 error (EXIT_FAILURE
, errno
, "cannot read data in child");
105 /* Compare the data. */
106 if (memcmp (buf
, testdata
, strlen (testdata
)) != 0)
107 error (EXIT_FAILURE
, 0, "data comparison failed in child");
109 /* Reset position again. */
110 if (lseek (fd
, 0, SEEK_SET
) != 0)
111 error (EXIT_FAILURE
, errno
, "cannot reset position again in child");
113 /* Write new data. */
114 if (write (fd
, testdata2
, strlen (testdata2
)) != strlen (testdata2
))
115 error (EXIT_FAILURE
, errno
, "cannot write new data in child");
117 /* Close the file. This must not remove it. */
123 /* Something went wrong. */
124 error (EXIT_FAILURE
, errno
, "cannot fork");
126 /* Wait for the child. */
127 if (waitpid (pid
, &status
, 0) != pid
)
128 error (EXIT_FAILURE
, 0, "Oops, wrong test program terminated");
130 if (WTERMSIG (status
) != 0)
131 error (EXIT_FAILURE
, 0, "Child terminated incorrectly");
132 status
= WEXITSTATUS (status
);
136 /* Test whether the child wrote the right data. First test the
137 position. It must be the same as in the child. */
138 if (lseek (fd
, 0, SEEK_CUR
) != strlen (testdata2
))
139 error (EXIT_FAILURE
, 0, "file position not changed");
141 if (lseek (fd
, 0, SEEK_SET
) != 0)
142 error (EXIT_FAILURE
, errno
, "cannot reset file position");
144 if (read (fd
, buf
, sizeof buf
) != strlen (testdata2
))
145 error (EXIT_FAILURE
, errno
, "cannot read new data");
147 if (memcmp (buf
, testdata2
, strlen (testdata2
)) != 0)
148 error (EXIT_FAILURE
, 0, "new data not read correctly");