2000-04-12 14:48:39 +08:00
|
|
|
/* Test program for POSIX shm_* functions.
|
2022-01-02 02:54:23 +08:00
|
|
|
Copyright (C) 2000-2022 Free Software Foundation, Inc.
|
2001-07-06 12:58:11 +08:00
|
|
|
This file is part of the GNU C Library.
|
2000-04-12 14:48:39 +08:00
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2000-04-12 14:48:39 +08:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
2000-04-12 14:48:39 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-10 07:18:22 +08:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2000-04-12 14:48:39 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <error.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* We want to see output immediately. */
|
|
|
|
#define STDOUT_UNBUFFERED
|
|
|
|
|
2020-10-16 01:59:04 +08:00
|
|
|
static char shm_test_name[sizeof "/glibc-shm-test-" + sizeof (pid_t) * 3];
|
|
|
|
static char shm_escape_name[sizeof "/../escaped-" + sizeof (pid_t) * 3];
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_shm_test_names (void)
|
|
|
|
{
|
|
|
|
snprintf (shm_test_name, sizeof (shm_test_name), "/glibc-shm-test-%u",
|
|
|
|
getpid ());
|
|
|
|
snprintf (shm_escape_name, sizeof (shm_escape_name), "/../escaped-%u",
|
|
|
|
getpid ());
|
|
|
|
}
|
|
|
|
|
2000-04-12 14:48:39 +08:00
|
|
|
static void
|
|
|
|
worker (int write_now)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
2007-05-19 11:30:44 +08:00
|
|
|
struct stat64 st;
|
2000-04-12 14:48:39 +08:00
|
|
|
int i;
|
2020-10-16 01:59:04 +08:00
|
|
|
|
|
|
|
int fd = shm_open (shm_test_name, O_RDWR, 0600);
|
2013-12-04 03:16:01 +08:00
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
error (EXIT_FAILURE, 0, "failed to open shared memory object: shm_open");
|
|
|
|
|
2000-04-12 14:48:39 +08:00
|
|
|
char *mem;
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
exit (fd);
|
|
|
|
|
2007-05-19 11:30:44 +08:00
|
|
|
if (fstat64 (fd, &st) == -1)
|
2000-04-12 14:48:39 +08:00
|
|
|
error (EXIT_FAILURE, 0, "stat failed");
|
2007-05-19 11:30:44 +08:00
|
|
|
if (st.st_size != 4000)
|
|
|
|
error (EXIT_FAILURE, 0, "size incorrect");
|
2000-04-12 14:48:39 +08:00
|
|
|
|
|
|
|
mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2012-11-01 07:23:09 +08:00
|
|
|
if (mem == MAP_FAILED)
|
2000-04-12 14:48:39 +08:00
|
|
|
error (EXIT_FAILURE, 0, "mmap failed");
|
|
|
|
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 500000000;
|
|
|
|
|
|
|
|
if (write_now)
|
|
|
|
for (i = 0; i <= 4; ++i)
|
|
|
|
mem[i] = i;
|
|
|
|
else
|
|
|
|
/* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4. */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
for (i = 0; i <= 4; ++i)
|
|
|
|
if (mem[i] != i)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i > 4)
|
|
|
|
/* OK, that's done. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
nanosleep (&ts, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!write_now)
|
|
|
|
for (i = 0; i <= 4; ++i)
|
|
|
|
mem[i] = 4 + i;
|
|
|
|
else
|
|
|
|
/* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8. */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
for (i = 0; i <= 4; ++i)
|
|
|
|
if (mem[i] != 4 + i)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i > 4)
|
|
|
|
/* OK, that's done. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
nanosleep (&ts, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (munmap (mem, 4000) == -1)
|
|
|
|
error (EXIT_FAILURE, errno, "munmap");
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-28 04:26:07 +08:00
|
|
|
static int
|
2000-04-12 14:48:39 +08:00
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
pid_t pid1;
|
|
|
|
pid_t pid2;
|
|
|
|
int status1;
|
|
|
|
int status2;
|
2007-05-19 11:30:44 +08:00
|
|
|
struct stat64 st;
|
2000-04-12 14:48:39 +08:00
|
|
|
|
2020-10-16 01:59:04 +08:00
|
|
|
init_shm_test_names ();
|
|
|
|
|
|
|
|
fd = shm_open (shm_escape_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
|
2013-10-31 20:58:01 +08:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
perror ("read file outside of SHMDIR directory");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-12 14:48:39 +08:00
|
|
|
/* Create the shared memory object. */
|
2020-10-16 01:59:04 +08:00
|
|
|
fd = shm_open (shm_test_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
|
2000-04-12 14:48:39 +08:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
2013-12-04 03:16:01 +08:00
|
|
|
/* If shm_open is unimplemented we skip the test. */
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
{
|
|
|
|
perror ("shm_open unimplemented. Test skipped.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error (EXIT_FAILURE, 0, "failed to create shared memory object: shm_open");
|
2000-04-12 14:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Size the object. We make it 4000 bytes long. */
|
|
|
|
if (ftruncate (fd, 4000) == -1)
|
|
|
|
{
|
|
|
|
/* This failed. Must be a bug in the implementation of the
|
|
|
|
shared memory itself. */
|
|
|
|
perror ("failed to size of shared memory object: ftruncate");
|
|
|
|
close (fd);
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2000-04-12 14:48:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-19 11:30:44 +08:00
|
|
|
if (fstat64 (fd, &st) == -1)
|
2002-04-03 08:36:12 +08:00
|
|
|
{
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2002-04-03 08:36:12 +08:00
|
|
|
error (EXIT_FAILURE, 0, "initial stat failed");
|
|
|
|
}
|
2007-05-19 11:30:44 +08:00
|
|
|
if (st.st_size != 4000)
|
|
|
|
{
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2007-05-19 11:30:44 +08:00
|
|
|
error (EXIT_FAILURE, 0, "initial size not correct");
|
|
|
|
}
|
2002-04-03 08:36:12 +08:00
|
|
|
|
2000-04-12 14:48:39 +08:00
|
|
|
/* Spawn to processes which will do the work. */
|
|
|
|
pid1 = fork ();
|
|
|
|
if (pid1 == 0)
|
|
|
|
worker (0);
|
|
|
|
else if (pid1 == -1)
|
|
|
|
{
|
|
|
|
/* Couldn't create a second process. */
|
|
|
|
perror ("fork");
|
|
|
|
close (fd);
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2000-04-12 14:48:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid2 = fork ();
|
|
|
|
if (pid2 == 0)
|
|
|
|
worker (1);
|
|
|
|
else if (pid2 == -1)
|
|
|
|
{
|
|
|
|
/* Couldn't create a second process. */
|
|
|
|
int ignore;
|
|
|
|
perror ("fork");
|
|
|
|
kill (pid1, SIGTERM);
|
|
|
|
waitpid (pid1, &ignore, 0);
|
|
|
|
close (fd);
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2000-04-12 14:48:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until the two processes are finished. */
|
|
|
|
waitpid (pid1, &status1, 0);
|
|
|
|
waitpid (pid2, &status2, 0);
|
|
|
|
|
|
|
|
/* Now we can unlink the shared object. */
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2000-04-12 14:48:39 +08:00
|
|
|
|
|
|
|
return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
|
|
|
|
|| !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:18:27 +08:00
|
|
|
static void
|
|
|
|
cleanup_handler (void)
|
|
|
|
{
|
2020-10-16 01:59:04 +08:00
|
|
|
shm_unlink (shm_test_name);
|
2016-12-09 15:18:27 +08:00
|
|
|
}
|
2000-04-12 14:48:39 +08:00
|
|
|
|
2016-12-09 15:18:27 +08:00
|
|
|
#define CLEANUP_HANDLER cleanup_handler
|
2000-04-12 14:48:39 +08:00
|
|
|
|
2016-12-09 15:18:27 +08:00
|
|
|
#include <support/test-driver.c>
|