|
Packit |
6c4009 |
/* Basic tests for SYSV message queue 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 <sys/types.h>
|
|
Packit |
6c4009 |
#include <sys/ipc.h>
|
|
Packit |
6c4009 |
#include <sys/msg.h>
|
|
Packit |
6c4009 |
|
|
Packit Service |
7e67f2 |
#include <test-sysvipc.h>
|
|
Packit Service |
7e67f2 |
|
|
Packit |
6c4009 |
#include <support/support.h>
|
|
Packit |
6c4009 |
#include <support/check.h>
|
|
Packit |
6c4009 |
#include <support/temp_file.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TEXTSIZE 32
|
|
Packit |
6c4009 |
struct msgbuf_t
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
#ifdef _GNU_SOURCE
|
|
Packit |
6c4009 |
__syscall_slong_t type;
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
long type;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
char buf[TEXTSIZE];
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define MSGTYPE 0x01020304
|
|
Packit |
6c4009 |
#define MSGDATA "0123456789"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* These are for the temporary file we generate. */
|
|
Packit |
6c4009 |
static char *name;
|
|
Packit |
6c4009 |
static int msqid;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
remove_msq (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Enforce message queue removal in case of early test failure.
|
|
Packit |
6c4009 |
Ignore error since the msgq may already have being removed. */
|
|
Packit |
6c4009 |
msgctl (msqid, IPC_RMID, NULL);
|
|
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-sysvmsg.", &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 MSGQ_MODE 0644
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
atexit (remove_msq);
|
|
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 |
msqid = msgget (key, MSGQ_MODE | IPC_CREAT);
|
|
Packit |
6c4009 |
if (msqid == -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == ENOSYS)
|
|
Packit |
6c4009 |
FAIL_UNSUPPORTED ("msgget not supported");
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgget failed (errno=%d)", errno);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit Service |
7e67f2 |
TEST_COMPARE (msgctl (msqid, first_msg_invalid_cmd (), NULL), -1);
|
|
Packit Service |
7e67f2 |
TEST_COMPARE (errno, EINVAL);
|
|
Packit Service |
7e67f2 |
|
|
Packit |
6c4009 |
/* Get message queue kernel information and do some sanity checks. */
|
|
Packit |
6c4009 |
struct msqid_ds msginfo;
|
|
Packit |
6c4009 |
if (msgctl (msqid, IPC_STAT, &msginfo) == -1)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgctl with IPC_STAT failed (errno=%d)", errno);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (msginfo.msg_perm.__key != key)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgid_ds::msg_perm::key (%d) != %d",
|
|
Packit |
6c4009 |
(int) msginfo.msg_perm.__key, (int) key);
|
|
Packit |
6c4009 |
if (msginfo.msg_perm.mode != MSGQ_MODE)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgid_ds::msg_perm::mode (%o) != %o",
|
|
Packit |
6c4009 |
msginfo.msg_perm.mode, MSGQ_MODE);
|
|
Packit |
6c4009 |
if (msginfo.msg_qnum != 0)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgid_ds::msg_qnum (%lu) != 0",
|
|
Packit |
6c4009 |
(long unsigned) msginfo.msg_qnum);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check if last argument (IPC_NOWAIT) is correctly handled. */
|
|
Packit |
6c4009 |
struct msgbuf_t msg2rcv = { 0 };
|
|
Packit |
6c4009 |
if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE,
|
|
Packit |
6c4009 |
IPC_NOWAIT) == -1
|
|
Packit |
6c4009 |
&& errno != ENOMSG)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct msgbuf_t msg2snd = { MSGTYPE, MSGDATA };
|
|
Packit |
6c4009 |
if (msgsnd (msqid, &msg2snd, sizeof (msg2snd.buf), 0) == -1)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgsnd failed (errno=%d)", errno);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE, 0) == -1)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int ret = 0;
|
|
Packit |
6c4009 |
if (strncmp (msg2snd.buf, msg2rcv.buf, TEXTSIZE) != 0)
|
|
Packit |
6c4009 |
ret = 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (msgctl (msqid, IPC_RMID, NULL) == -1)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("msgctl failed");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return ret;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <support/test-driver.c>
|