Blame sysvipc/test-sysvshm.c

Packit 6c4009
/* Basic tests for SYSV shared memory functions.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/ipc.h>
Packit 6c4009
#include <sys/shm.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
Packit 6c4009
/* These are for the temporary file we generate.  */
Packit 6c4009
static char *name;
Packit 6c4009
static int shmid;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
remove_shm (void)
Packit 6c4009
{
Packit 6c4009
  /* Enforce message queue removal in case of early test failure.
Packit 6c4009
     Ignore error since the shm may already have being removed.  */
Packit 6c4009
  shmctl (shmid, IPC_RMID, 0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  int fd = create_temp_file ("tst-sysvshm.", &name);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
/* It is not an extensive test, but rather a functional one aimed to check
Packit 6c4009
   correct parameter passing on kernel.  */
Packit 6c4009
Packit 6c4009
#define CHECK_EQ(v, k) \
Packit 6c4009
  if ((v) != (k)) \
Packit 6c4009
    FAIL_EXIT1("%d != %d", v, k)
Packit 6c4009
Packit 6c4009
#define SHM_MODE 0666
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  atexit (remove_shm);
Packit 6c4009
Packit 6c4009
  key_t key = ftok (name, 'G');
Packit 6c4009
  if (key == -1)
Packit 6c4009
    FAIL_EXIT1 ("ftok failed");
Packit 6c4009
Packit 6c4009
  long int pgsz = sysconf (_SC_PAGESIZE);
Packit 6c4009
  if (pgsz == -1)
Packit 6c4009
    FAIL_EXIT1 ("sysconf (_SC_PAGESIZE) failed (errno = %d)", errno);
Packit 6c4009
Packit 6c4009
  shmid = shmget(key, pgsz, IPC_CREAT | IPC_EXCL | SHM_MODE);
Packit 6c4009
  if (shmid == -1)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	FAIL_UNSUPPORTED ("shmget not supported");
Packit 6c4009
      FAIL_EXIT1 ("shmget failed (errno=%d)", errno);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Get shared memory kernel information and do some sanity checks.  */
Packit 6c4009
  struct shmid_ds shminfo;
Packit 6c4009
  if (shmctl (shmid, IPC_STAT, &shminfo) == -1)
Packit 6c4009
    FAIL_EXIT1 ("shmctl with IPC_STAT failed (errno=%d)", errno);
Packit 6c4009
Packit 6c4009
  if (shminfo.shm_perm.__key != key)
Packit 6c4009
    FAIL_EXIT1 ("shmid_ds::shm_perm::key (%d) != %d",
Packit 6c4009
		(int) shminfo.shm_perm.__key, (int) key);
Packit 6c4009
  if (shminfo.shm_perm.mode != SHM_MODE)
Packit 6c4009
    FAIL_EXIT1 ("shmid_ds::shm_perm::mode (%o) != %o",
Packit 6c4009
		shminfo.shm_perm.mode, SHM_MODE);
Packit 6c4009
  if (shminfo.shm_segsz != pgsz)
Packit 6c4009
    FAIL_EXIT1 ("shmid_ds::shm_segsz (%lu) != %lu",
Packit 6c4009
		(long unsigned) shminfo.shm_segsz, pgsz);
Packit 6c4009
Packit 6c4009
  /* Attach on shared memory and realize some operations.  */
Packit 6c4009
  int *shmem = shmat (shmid, NULL, 0);
Packit 6c4009
  if (shmem == (void*) -1)
Packit 6c4009
    FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
Packit 6c4009
Packit 6c4009
  shmem[0]   = 0x55555555;
Packit 6c4009
  shmem[32]  = 0x44444444;
Packit 6c4009
  shmem[64]  = 0x33333333;
Packit 6c4009
  shmem[128] = 0x22222222;
Packit 6c4009
Packit 6c4009
  if (shmdt (shmem) == -1)
Packit 6c4009
    FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
Packit 6c4009
Packit 6c4009
  shmem = shmat (shmid, NULL, SHM_RDONLY);
Packit 6c4009
  if (shmem == (void*) -1)
Packit 6c4009
    FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
Packit 6c4009
Packit 6c4009
  CHECK_EQ (shmem[0],   0x55555555);
Packit 6c4009
  CHECK_EQ (shmem[32],  0x44444444);
Packit 6c4009
  CHECK_EQ (shmem[64],  0x33333333);
Packit 6c4009
  CHECK_EQ (shmem[128], 0x22222222);
Packit 6c4009
Packit 6c4009
  if (shmdt (shmem) == -1)
Packit 6c4009
    FAIL_EXIT1 ("shmem failed (errno=%d)", errno);
Packit 6c4009
Packit 6c4009
  /* Finally free up the semnaphore resource.  */
Packit 6c4009
  if (shmctl (shmid, IPC_RMID, 0) == -1)
Packit 6c4009
    FAIL_EXIT1 ("semctl failed (errno=%d)", errno);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>