glibc/rt/tst-mqueue2.c
Paul Eggert 5a82c74822 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 02:43:31 -07:00

476 lines
12 KiB
C

/* Test message queue passing.
Copyright (C) 2004-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
The GNU C Library is free software; you can redistribute it and/or
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.
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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <fcntl.h>
#include <mqueue.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "tst-mqueue.h"
static void
alrm_handler (int sig)
{
}
#define TEST_FUNCTION do_test ()
static int
do_test (void)
{
int result = 0;
char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
if (q == (mqd_t) -1)
{
printf ("mq_open failed with: %m\n");
return result;
}
else
add_temp_mq (name);
mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
if (q2 != (mqd_t) -1)
{
puts ("mq_open with O_EXCL unexpectedly succeeded");
result = 1;
}
else if (errno != EEXIST)
{
printf ("mq_open did not fail with EEXIST: %m\n");
result = 1;
}
char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
attr.mq_maxmsg = -2;
q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
if (q2 != (mqd_t) -1)
{
puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
add_temp_mq (name2);
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_open with invalid mq_maxmsg did not fail with "
"EINVAL: %m\n");
result = 1;
}
attr.mq_maxmsg = 2;
attr.mq_msgsize = -56;
q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
if (q2 != (mqd_t) -1)
{
puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
add_temp_mq (name2);
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_open with invalid mq_msgsize did not fail with "
"EINVAL: %m\n");
result = 1;
}
char buf[3];
struct timespec ts;
if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
ts.tv_sec += 10;
else
{
ts.tv_sec = time (NULL) + 10;
ts.tv_nsec = 0;
}
if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
{
puts ("mq_timedreceive with too small msg_len did not fail");
result = 1;
}
else if (errno != EMSGSIZE)
{
printf ("mq_timedreceive with too small msg_len did not fail with "
"EMSGSIZE: %m\n");
result = 1;
}
ts.tv_nsec = -1;
if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
{
puts ("mq_timedreceive with negative tv_nsec did not fail");
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_timedreceive with negative tv_nsec did not fail with "
"EINVAL: %m\n");
result = 1;
}
ts.tv_nsec = 1000000000;
if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
{
puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
"EINVAL: %m\n");
result = 1;
}
struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
sigemptyset (&sa.sa_mask);
sigaction (SIGALRM, &sa, NULL);
struct itimerval it = { .it_value = { .tv_sec = 1 } };
setitimer (ITIMER_REAL, &it, NULL);
if (mq_receive (q, buf, 2, NULL) == 0)
{
puts ("mq_receive on empty queue did not block");
result = 1;
}
else if (errno != EINTR)
{
printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
result = 1;
}
setitimer (ITIMER_REAL, &it, NULL);
ts.tv_nsec = 0;
if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
{
puts ("mq_timedreceive on empty queue did not block");
result = 1;
}
else if (errno != EINTR)
{
printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
result = 1;
}
buf[0] = '6';
buf[1] = '7';
if (mq_send (q, buf, 2, 3) != 0
|| (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
{
printf ("mq_send failed: %m\n");
result = 1;
}
memset (buf, ' ', sizeof (buf));
unsigned int prio;
ssize_t rets = mq_receive (q, buf, 3, &prio);
if (rets != 1)
{
if (rets == -1)
printf ("mq_receive failed: %m\n");
else
printf ("mq_receive returned %zd != 1\n", rets);
result = 1;
}
else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
{
printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
prio, buf[0], buf[1], buf[2]);
result = 1;
}
rets = mq_receive (q, buf, 2, NULL);
if (rets != 2)
{
if (rets == -1)
printf ("mq_receive failed: %m\n");
else
printf ("mq_receive returned %zd != 2\n", rets);
result = 1;
}
else if (memcmp (buf, "67 ", 3) != 0)
{
printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
buf[0], buf[1], buf[2]);
result = 1;
}
buf[0] = '2';
buf[1] = '1';
if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
ts.tv_sec = time (NULL);
ts.tv_nsec = -1000000001;
if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
&& (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
|| (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
(mq_timedsend (q, buf, 1, 4, &ts) != 0
&& (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
{
printf ("mq_timedsend failed: %m\n");
result = 1;
}
buf[0] = '-';
ts.tv_nsec = 1000000001;
if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
{
puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
"EINVAL: %m\n");
result = 1;
}
ts.tv_nsec = -2;
if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
{
puts ("mq_timedsend with negative tv_nsec did not fail");
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_timedsend with megatove tv_nsec did not fail with "
"EINVAL: %m\n");
result = 1;
}
setitimer (ITIMER_REAL, &it, NULL);
if (mq_send (q, buf, 2, 8) == 0)
{
puts ("mq_send on full queue did not block");
result = 1;
}
else if (errno != EINTR)
{
printf ("mq_send on full queue did not fail with EINTR: %m\n");
result = 1;
}
setitimer (ITIMER_REAL, &it, NULL);
ts.tv_sec += 10;
ts.tv_nsec = 0;
if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
{
puts ("mq_timedsend on full queue did not block");
result = 1;
}
else if (errno != EINTR)
{
printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
result = 1;
}
memset (buf, ' ', sizeof (buf));
if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
ts.tv_sec = time (NULL);
ts.tv_nsec = -1000000001;
rets = mq_timedreceive (q, buf, 2, &prio, &ts);
if (rets == -1 && errno == EINVAL)
rets = mq_receive (q, buf, 2, &prio);
if (rets != 2)
{
if (rets == -1)
printf ("mq_timedreceive failed: %m\n");
else
printf ("mq_timedreceive returned %zd != 2\n", rets);
result = 1;
}
else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
{
printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
prio, buf[0], buf[1], buf[2]);
result = 1;
}
if (mq_receive (q, buf, 1, NULL) == 0)
{
puts ("mq_receive with too small msg_len did not fail");
result = 1;
}
else if (errno != EMSGSIZE)
{
printf ("mq_receive with too small msg_len did not fail with "
"EMSGSIZE: %m\n");
result = 1;
}
ts.tv_nsec = -ts.tv_nsec;
rets = mq_timedreceive (q, buf, 2, NULL, &ts);
if (rets == -1 && errno == EINVAL)
rets = mq_receive (q, buf, 2, NULL);
if (rets != 1)
{
if (rets == -1)
printf ("mq_timedreceive failed: %m\n");
else
printf ("mq_timedreceive returned %zd != 1\n", rets);
result = 1;
}
else if (memcmp (buf, "31 ", 3) != 0)
{
printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
buf[0], buf[1], buf[2]);
result = 1;
}
if (mq_send (q, "", 0, 2) != 0)
{
printf ("mq_send with msg_len 0 failed: %m\n");
result = 1;
}
rets = mq_receive (q, buf, 2, &prio);
if (rets)
{
if (rets == -1)
printf ("mq_receive failed: %m\n");
else
printf ("mq_receive returned %zd != 0\n", rets);
result = 1;
}
long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
{
if (mq_send (q, buf, 1, mq_prio_max) == 0)
{
puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
result = 1;
}
else if (errno != EINVAL)
{
printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
"EINVAL: %m\n");
result = 1;
}
if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
{
printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
result = 1;
}
}
if (mq_unlink (name) != 0)
{
printf ("mq_unlink failed: %m\n");
result = 1;
}
q2 = mq_open (name, O_RDWR);
if (q2 != (mqd_t) -1)
{
printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
"succeeded\n", name);
result = 1;
}
else if (errno != ENOENT)
{
printf ("mq_open of unlinked %s without O_CREAT did not fail with "
"ENOENT: %m\n", name);
result = 1;
}
if (mq_close (q) != 0)
{
printf ("mq_close in parent failed: %m\n");
result = 1;
}
if (mq_receive (q, buf, 2, NULL) == 0)
{
puts ("mq_receive on invalid mqd_t did not fail");
result = 1;
}
else if (errno != EBADF)
{
printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
result = 1;
}
if (mq_send (q, buf, 1, 2) == 0)
{
puts ("mq_send on invalid mqd_t did not fail");
result = 1;
}
else if (errno != EBADF)
{
printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
result = 1;
}
if (mq_getattr (q, &attr) == 0)
{
puts ("mq_getattr on invalid mqd_t did not fail");
result = 1;
}
else if (errno != EBADF)
{
printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
result = 1;
}
memset (&attr, 0, sizeof (attr));
if (mq_setattr (q, &attr, NULL) == 0)
{
puts ("mq_setattr on invalid mqd_t did not fail");
result = 1;
}
else if (errno != EBADF)
{
printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
result = 1;
}
if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
{
puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
result = 1;
}
else if (errno != ENOENT)
{
printf ("mq_unlink of non-existant message queue did not fail with "
"ENOENT: %m\n");
result = 1;
}
return result;
}
#include "../test-skeleton.c"