Blame rt/tst-shm.c

Packit 6c4009
/* Test program for POSIX shm_* functions.
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 <fcntl.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We want to see output immediately.  */
Packit 6c4009
#define STDOUT_UNBUFFERED
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
worker (int write_now)
Packit 6c4009
{
Packit 6c4009
  struct timespec ts;
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  int i;
Packit 6c4009
  int fd = shm_open ("/glibc-shm-test", O_RDWR, 0600);
Packit 6c4009
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    error (EXIT_FAILURE, 0, "failed to open shared memory object: shm_open");
Packit 6c4009
Packit 6c4009
  char *mem;
Packit 6c4009
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    exit (fd);
Packit 6c4009
Packit 6c4009
  if (fstat64 (fd, &st) == -1)
Packit 6c4009
    error (EXIT_FAILURE, 0, "stat failed");
Packit 6c4009
  if (st.st_size != 4000)
Packit 6c4009
    error (EXIT_FAILURE, 0, "size incorrect");
Packit 6c4009
Packit 6c4009
  mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Packit 6c4009
  if (mem == MAP_FAILED)
Packit 6c4009
    error (EXIT_FAILURE, 0, "mmap failed");
Packit 6c4009
Packit 6c4009
  ts.tv_sec = 0;
Packit 6c4009
  ts.tv_nsec = 500000000;
Packit 6c4009
Packit 6c4009
  if (write_now)
Packit 6c4009
    for (i = 0; i <= 4; ++i)
Packit 6c4009
      mem[i] = i;
Packit 6c4009
  else
Packit 6c4009
    /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4.  */
Packit 6c4009
    while (1)
Packit 6c4009
      {
Packit 6c4009
	for (i = 0; i <= 4; ++i)
Packit 6c4009
	  if (mem[i] != i)
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	if (i > 4)
Packit 6c4009
	  /* OK, that's done.  */
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	nanosleep (&ts, NULL);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (!write_now)
Packit 6c4009
    for (i = 0; i <= 4; ++i)
Packit 6c4009
      mem[i] = 4 + i;
Packit 6c4009
  else
Packit 6c4009
    /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8.  */
Packit 6c4009
    while (1)
Packit 6c4009
      {
Packit 6c4009
	for (i = 0; i <= 4; ++i)
Packit 6c4009
	  if (mem[i] != 4 + i)
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	if (i > 4)
Packit 6c4009
	  /* OK, that's done.  */
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	nanosleep (&ts, NULL);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (munmap (mem, 4000) == -1)
Packit 6c4009
    error (EXIT_FAILURE, errno, "munmap");
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  exit (0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int fd;
Packit 6c4009
  pid_t pid1;
Packit 6c4009
  pid_t pid2;
Packit 6c4009
  int status1;
Packit 6c4009
  int status2;
Packit 6c4009
  struct stat64 st;
Packit 6c4009
Packit 6c4009
  fd = shm_open ("/../escaped", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      perror ("read file outside of SHMDIR directory");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
Packit 6c4009
  /* Create the shared memory object.  */
Packit 6c4009
  fd = shm_open ("/glibc-shm-test", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      /* If shm_open is unimplemented we skip the test.  */
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
        {
Packit 6c4009
	  perror ("shm_open unimplemented.  Test skipped.");
Packit 6c4009
          return 0;
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        error (EXIT_FAILURE, 0, "failed to create shared memory object: shm_open");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Size the object.  We make it 4000 bytes long.  */
Packit 6c4009
  if (ftruncate (fd, 4000) == -1)
Packit 6c4009
    {
Packit 6c4009
      /* This failed.  Must be a bug in the implementation of the
Packit 6c4009
         shared memory itself.  */
Packit 6c4009
      perror ("failed to size of shared memory object: ftruncate");
Packit 6c4009
      close (fd);
Packit 6c4009
      shm_unlink ("/glibc-shm-test");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fstat64 (fd, &st) == -1)
Packit 6c4009
    {
Packit 6c4009
      shm_unlink ("/glibc-shm-test");
Packit 6c4009
      error (EXIT_FAILURE, 0, "initial stat failed");
Packit 6c4009
    }
Packit 6c4009
  if (st.st_size != 4000)
Packit 6c4009
    {
Packit 6c4009
      shm_unlink ("/glibc-shm-test");
Packit 6c4009
      error (EXIT_FAILURE, 0, "initial size not correct");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Spawn to processes which will do the work.  */
Packit 6c4009
  pid1 = fork ();
Packit 6c4009
  if (pid1 == 0)
Packit 6c4009
    worker (0);
Packit 6c4009
  else if (pid1 == -1)
Packit 6c4009
    {
Packit 6c4009
      /* Couldn't create a second process.  */
Packit 6c4009
      perror ("fork");
Packit 6c4009
      close (fd);
Packit 6c4009
      shm_unlink ("/glibc-shm-test");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pid2 = fork ();
Packit 6c4009
  if (pid2 == 0)
Packit 6c4009
    worker (1);
Packit 6c4009
  else if (pid2 == -1)
Packit 6c4009
    {
Packit 6c4009
      /* Couldn't create a second process.  */
Packit 6c4009
      int ignore;
Packit 6c4009
      perror ("fork");
Packit 6c4009
      kill (pid1, SIGTERM);
Packit 6c4009
      waitpid (pid1, &ignore, 0);
Packit 6c4009
      close (fd);
Packit 6c4009
      shm_unlink ("/glibc-shm-test");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Wait until the two processes are finished.  */
Packit 6c4009
  waitpid (pid1, &status1, 0);
Packit 6c4009
  waitpid (pid2, &status2, 0);
Packit 6c4009
Packit 6c4009
  /* Now we can unlink the shared object.  */
Packit 6c4009
  shm_unlink ("/glibc-shm-test");
Packit 6c4009
Packit 6c4009
  return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
Packit 6c4009
	  || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
cleanup_handler (void)
Packit 6c4009
{
Packit 6c4009
  shm_unlink ("/glibc-shm-test");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define CLEANUP_HANDLER cleanup_handler
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>