1 /* Tests for fork.
2    Copyright (C) 2000-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <errno.h>
20 #include <error.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <wait.h>
25 
26 
27 static const char testdata[] = "This is a test";
28 static const char testdata2[] = "And here we go again";
29 
30 
31 int
main(void)32 main (void)
33 {
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;
43 
44   if (tmpdir == NULL || *tmpdir == '\0')
45     tmpdir = "/tmp";
46   tmpdirlen = strlen (tmpdir);
47 
48   name = (char *) malloc (tmpdirlen + strlen ("/forkXXXXXX") + 1);
49   if (name == NULL)
50     error (EXIT_FAILURE, errno, "cannot allocate file name");
51 
52   mempcpy (mempcpy (name, tmpdir, tmpdirlen),
53 	   "/forkXXXXXX", sizeof ("/forkXXXXXX"));
54 
55   /* Open our test file.   */
56   fd = mkstemp (name);
57   if (fd == -1)
58      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
59 
60   /* Make sure it gets removed.  */
61   unlink (name);
62 
63   /* Write some data.  */
64   if (write (fd, testdata, strlen (testdata)) != strlen (testdata))
65     error (EXIT_FAILURE, errno, "cannot write test data");
66 
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");
71 
72   /* Get the parent PID.  */
73   ppid = getpid ();
74 
75   /* Now fork of the process.  */
76   pid = fork ();
77   if (pid == 0)
78     {
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");
82 
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);
89 
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");
94 
95       /* Reset the position.  */
96       if (lseek (fd, 0, SEEK_SET) != 0)
97 	error (EXIT_FAILURE, errno, "cannot reset position in child");
98 
99       /* Read the data.  */
100       if (read (fd, buf, sizeof buf) != strlen (testdata))
101 	error (EXIT_FAILURE, errno, "cannot read data in child");
102 
103       /* Compare the data.  */
104       if (memcmp (buf, testdata, strlen (testdata)) != 0)
105 	error (EXIT_FAILURE, 0, "data comparison failed in child");
106 
107       /* Reset position again.  */
108       if (lseek (fd, 0, SEEK_SET) != 0)
109 	error (EXIT_FAILURE, errno, "cannot reset position again in child");
110 
111       /* Write new data.  */
112       if (write (fd, testdata2, strlen (testdata2)) != strlen (testdata2))
113 	error (EXIT_FAILURE, errno, "cannot write new data in child");
114 
115       /* Close the file.  This must not remove it.  */
116       close (fd);
117 
118       _exit (0);
119     }
120   else if (pid < 0)
121     /* Something went wrong.  */
122     error (EXIT_FAILURE, errno, "cannot fork");
123 
124   /* Wait for the child.  */
125   if (waitpid (pid, &status, 0) != pid)
126     error (EXIT_FAILURE, 0, "Oops, wrong test program terminated");
127 
128   if (WTERMSIG (status) != 0)
129     error (EXIT_FAILURE, 0, "Child terminated incorrectly");
130   status = WEXITSTATUS (status);
131 
132   if (status == 0)
133     {
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");
138 
139       if (lseek (fd, 0, SEEK_SET) != 0)
140 	error (EXIT_FAILURE, errno, "cannot reset file position");
141 
142       if (read (fd, buf, sizeof buf) != strlen (testdata2))
143 	error (EXIT_FAILURE, errno, "cannot read new data");
144 
145       if (memcmp (buf, testdata2, strlen (testdata2)) != 0)
146 	error (EXIT_FAILURE, 0, "new data not read correctly");
147     }
148 
149   return status;
150 }
151