Consolidate conditionals in mp sin/cos functions
[glibc.git] / posix / tst-fork.c
blob8384c1ea1cbab6cd4a0337029f174765856601cc
1 /* Tests for fork.
2 Copyright (C) 2000-2013 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 <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <error.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <wait.h>
28 static const char testdata[] = "This is a test";
29 static const char testdata2[] = "And here we go again";
32 int
33 main (void)
35 const char *tmpdir = getenv ("TMPDIR");
36 char buf[100];
37 size_t tmpdirlen;
38 char *name;
39 int fd;
40 pid_t pid;
41 pid_t ppid;
42 off_t off;
43 int status;
45 if (tmpdir == NULL || *tmpdir == '\0')
46 tmpdir = "/tmp";
47 tmpdirlen = strlen (tmpdir);
49 name = (char *) malloc (tmpdirlen + strlen ("/forkXXXXXX") + 1);
50 if (name == NULL)
51 error (EXIT_FAILURE, errno, "cannot allocate file name");
53 mempcpy (mempcpy (name, tmpdir, tmpdirlen),
54 "/forkXXXXXX", sizeof ("/forkXXXXXX"));
56 /* Open our test file. */
57 fd = mkstemp (name);
58 if (fd == -1)
59 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
61 /* Make sure it gets removed. */
62 unlink (name);
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. */
74 ppid = getpid ();
76 /* Now fork of the process. */
77 pid = fork ();
78 if (pid == 0)
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. */
85 pid = getppid ();
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");
100 /* Read the data. */
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. */
117 close (fd);
119 _exit (0);
121 else if (pid < 0)
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);
133 if (status == 0)
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");
150 return status;