Files
ftmemsim-valgrind/none/tests/mq.c
Mark Wielaard 4015813336 Remove some warnings from tests
Various tests do things which we want to detect at runtime, like
ignoring the result of malloc or doing a deliberate impossibly large
allocation or operations that would result in overflowing or
truncated strings, that generate a warning from gcc.

In once case, mq_setattr called with new and old attrs overlapping,
this was explicitly fixed, in others -Wno-foobar was added to silence
the warning. This is safe even for older gcc, since a compiler will
ignore any -Wno-foobar they don't know about - since they do know they
won't warn for foobar.
2021-10-10 15:56:50 +02:00

125 lines
2.1 KiB
C

#include <sys/types.h>
#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#if defined(HAVE_MQUEUE_H)
#include <mqueue.h>
#define MSGMAX 10
#define MSGSIZEMAX 1024
int main(int argc, char **argv)
{
struct mq_attr mqa, mqa2;
mqd_t mqdw;
mqd_t mqdr;
char buffer[MSGSIZEMAX];
unsigned int priority;
int len;
mqa.mq_maxmsg = MSGMAX;
mqa.mq_msgsize = MSGSIZEMAX;
if ((mqdw = mq_open("/valgrind-mqueue", O_CREAT|O_EXCL|O_WRONLY, 0600, &mqa)) < 0)
{
if (errno == ENOSYS)
exit(0);
perror("mq_open");
exit(1);
}
if ((mqdr = mq_open("/valgrind-mqueue", O_RDONLY)) < 0)
{
perror("mq_open");
mq_unlink("/valgrind-mqueue");
mq_close(mqdw);
exit(1);
}
if (mq_unlink("/valgrind-mqueue") < 0)
{
perror("mq_unlink");
mq_close(mqdw);
mq_close(mqdr);
exit(1);
}
if (mq_send(mqdw, "PING", 4, 0) < 0)
{
perror("mq_send");
mq_close(mqdr);
mq_close(mqdw);
exit(1);
}
if ((len = mq_receive(mqdr, buffer, sizeof(buffer), &priority)) < 0)
{
perror("mq_receive");
mq_close(mqdr);
mq_close(mqdw);
exit(1);
}
#if !defined(VGO_solaris)
/* On Solaris, there is no existing notification registration. */
if (mq_notify(mqdr, NULL) < 0)
{
perror("mq_notify");
mq_close(mqdr);
mq_close(mqdw);
exit(1);
}
#endif /* !VGO_solaris */
if (len != 4 || memcmp(buffer, "PING", 4) != 0)
{
fprintf(stderr, "Message corrupt!");
}
if (mq_getattr(mqdr, &mqa) < 0)
{
perror("mq_getattr");
mq_close(mqdr);
mq_close(mqdw);
exit(1);
}
mqa2 = mqa;
if (mq_setattr(mqdw, &mqa, &mqa2) < 0)
{
perror("mq_setattr");
mq_close(mqdr);
mq_close(mqdw);
exit(1);
}
if (mq_close(mqdr) < 0)
{
perror("mq_close");
mq_close(mqdw);
exit(1);
}
if (mq_close(mqdw) < 0)
{
perror("mq_close");
exit(1);
}
exit(0);
}
#else
int main(int argc, char **argv)
{
exit(0);
}
#endif