|
Packit |
6c4009 |
/* Tests for fork.
|
|
Packit |
6c4009 |
Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <error.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <wait.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static const char testdata[] = "This is a test";
|
|
Packit |
6c4009 |
static const char testdata2[] = "And here we go again";
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
main (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const char *tmpdir = getenv ("TMPDIR");
|
|
Packit |
6c4009 |
char buf[100];
|
|
Packit |
6c4009 |
size_t tmpdirlen;
|
|
Packit |
6c4009 |
char *name;
|
|
Packit |
6c4009 |
int fd;
|
|
Packit |
6c4009 |
pid_t pid;
|
|
Packit |
6c4009 |
pid_t ppid;
|
|
Packit |
6c4009 |
off_t off;
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (tmpdir == NULL || *tmpdir == '\0')
|
|
Packit |
6c4009 |
tmpdir = "/tmp";
|
|
Packit |
6c4009 |
tmpdirlen = strlen (tmpdir);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
name = (char *) malloc (tmpdirlen + strlen ("/forkXXXXXX") + 1);
|
|
Packit |
6c4009 |
if (name == NULL)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot allocate file name");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
mempcpy (mempcpy (name, tmpdir, tmpdirlen),
|
|
Packit |
6c4009 |
"/forkXXXXXX", sizeof ("/forkXXXXXX"));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Open our test file. */
|
|
Packit |
6c4009 |
fd = mkstemp (name);
|
|
Packit |
6c4009 |
if (fd == -1)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Make sure it gets removed. */
|
|
Packit |
6c4009 |
unlink (name);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Write some data. */
|
|
Packit |
6c4009 |
if (write (fd, testdata, strlen (testdata)) != strlen (testdata))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot write test data");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the position in the stream. */
|
|
Packit |
6c4009 |
off = lseek (fd, 0, SEEK_CUR);
|
|
Packit |
6c4009 |
if (off == (off_t) -1 || off != strlen (testdata))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "wrong file position");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the parent PID. */
|
|
Packit |
6c4009 |
ppid = getpid ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now fork of the process. */
|
|
Packit |
6c4009 |
pid = fork ();
|
|
Packit |
6c4009 |
if (pid == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* One little test first: the PID must have changed. */
|
|
Packit |
6c4009 |
if (getpid () == ppid)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, "child and parent have same PID");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Test the `getppid' function. */
|
|
Packit |
6c4009 |
pid = getppid ();
|
|
Packit |
6c4009 |
if (pid == (pid_t) -1 ? errno != ENOSYS : pid != ppid)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0,
|
|
Packit |
6c4009 |
"getppid returned wrong PID (%ld, should be %ld)",
|
|
Packit |
6c4009 |
(long int) pid, (long int) ppid);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This is the child. First get the position of the descriptor. */
|
|
Packit |
6c4009 |
off = lseek (fd, 0, SEEK_CUR);
|
|
Packit |
6c4009 |
if (off == (off_t) -1 || off != strlen (testdata))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "wrong file position in child");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Reset the position. */
|
|
Packit |
6c4009 |
if (lseek (fd, 0, SEEK_SET) != 0)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot reset position in child");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Read the data. */
|
|
Packit |
6c4009 |
if (read (fd, buf, sizeof buf) != strlen (testdata))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot read data in child");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Compare the data. */
|
|
Packit |
6c4009 |
if (memcmp (buf, testdata, strlen (testdata)) != 0)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, "data comparison failed in child");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Reset position again. */
|
|
Packit |
6c4009 |
if (lseek (fd, 0, SEEK_SET) != 0)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot reset position again in child");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Write new data. */
|
|
Packit |
6c4009 |
if (write (fd, testdata2, strlen (testdata2)) != strlen (testdata2))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot write new data in child");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Close the file. This must not remove it. */
|
|
Packit |
6c4009 |
close (fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
_exit (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (pid < 0)
|
|
Packit |
6c4009 |
/* Something went wrong. */
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot fork");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Wait for the child. */
|
|
Packit |
6c4009 |
if (waitpid (pid, &status, 0) != pid)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, "Oops, wrong test program terminated");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (WTERMSIG (status) != 0)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, "Child terminated incorrectly");
|
|
Packit |
6c4009 |
status = WEXITSTATUS (status);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (status == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Test whether the child wrote the right data. First test the
|
|
Packit |
6c4009 |
position. It must be the same as in the child. */
|
|
Packit |
6c4009 |
if (lseek (fd, 0, SEEK_CUR) != strlen (testdata2))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, "file position not changed");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (lseek (fd, 0, SEEK_SET) != 0)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot reset file position");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (read (fd, buf, sizeof buf) != strlen (testdata2))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, "cannot read new data");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (memcmp (buf, testdata2, strlen (testdata2)) != 0)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, "new data not read correctly");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return status;
|
|
Packit |
6c4009 |
}
|