Blame src/functional/ipc_msg.c

nsz f7a881
#ifndef _XOPEN_SOURCE
nsz f7a881
#define _XOPEN_SOURCE 700
nsz f7a881
#endif
nsz f7a881
#include <errno.h>
nsz f7a881
#include <stdlib.h>
nsz f7a881
#include <string.h>
nsz f7a881
#include <time.h>
nsz f7a881
#include <sys/types.h>
nsz f7a881
#include <sys/msg.h>
nsz f7a881
#include <sys/wait.h>
nsz f7a881
#include <unistd.h>
nsz f7a881
#include "test.h"
nsz f7a881
nsz f7a881
static const char path[] = ".";
Szabolcs Nagy a5138a
static const int id = 'm';
nsz f7a881
nsz f7a881
#define T(f) do{ \
nsz f7a881
	if ((f)+1 == 0) \
Szabolcs Nagy cfa23c
		t_error("%s failed: %s\n", #f, strerror(errno)); \
nsz f7a881
}while(0)
nsz f7a881
nsz f7a881
#define EQ(a,b,fmt) do{ \
nsz f7a881
	if ((a) != (b)) \
Szabolcs Nagy cfa23c
		t_error("%s == %s failed: " fmt "\n", #a, #b, a, b); \
nsz f7a881
}while(0)
nsz f7a881
nsz f7a881
static void snd()
nsz f7a881
{
nsz f7a881
	time_t t;
nsz f7a881
	key_t k;
nsz f7a881
	int qid;
nsz f7a881
	struct msqid_ds qid_ds;
nsz f7a881
	struct {
nsz f7a881
		long type;
nsz f7a881
		char data[20];
nsz f7a881
	} msg = {1, "test message"};
nsz f7a881
nsz f7a881
	T(t = time(0));
nsz f7a881
	T(k = ftok(path, id));
nsz f7a881
nsz f7a881
	/* make sure we get a clean message queue id */
nsz f7a881
	T(qid = msgget(k, IPC_CREAT|0666));
nsz f7a881
	T(msgctl(qid, IPC_RMID, 0));
nsz f7a881
	T(qid = msgget(k, IPC_CREAT|IPC_EXCL|0666));
nsz f7a881
Szabolcs Nagy cfa23c
	if (t_status)
Szabolcs Nagy cfa23c
		exit(t_status);
nsz f7a881
nsz f7a881
	/* check IPC_EXCL */
nsz f7a881
	errno = 0;
nsz f7a881
	if (msgget(k, IPC_CREAT|IPC_EXCL|0666) != -1 || errno != EEXIST)
Szabolcs Nagy cfa23c
		t_error("msgget(IPC_CREAT|IPC_EXCL) should have failed with EEXIST, got %s\n", strerror(errno));
nsz f7a881
nsz f7a881
	/* check if msgget initilaized the msqid_ds structure correctly */
nsz f7a881
	T(msgctl(qid, IPC_STAT, &qid_ds));
nsz f7a881
	EQ(qid_ds.msg_perm.cuid, geteuid(), "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_perm.uid, geteuid(), "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_perm.cgid, getegid(), "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_perm.gid, getegid(), "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_perm.mode & 0x1ff, 0666, "got %o, want %o");
nsz f7a881
	EQ(qid_ds.msg_qnum, 0, "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_lspid, 0, "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_lrpid, 0, "got %d, want %d");
Szabolcs Nagy d8e961
	EQ((long long)qid_ds.msg_stime, 0, "got %lld, want %d");
Szabolcs Nagy d8e961
	EQ((long long)qid_ds.msg_rtime, 0, "got %lld, want %d");
nsz f7a881
	if (qid_ds.msg_ctime < t)
Szabolcs Nagy d8e961
		t_error("qid_ds.msg_ctime >= t failed: got %lld, want >= %lld\n", (long long)qid_ds.msg_ctime, (long long)t);
Szabolcs Nagy d5fe2f
	if (qid_ds.msg_ctime > t+5)
Szabolcs Nagy d8e961
		t_error("qid_ds.msg_ctime <= t+5 failed: got %lld, want <= %lld\n", (long long)qid_ds.msg_ctime, (long long)t+5);
nsz f7a881
	if (qid_ds.msg_qbytes <= 0)
Szabolcs Nagy d5fe2f
		t_error("qid_ds.msg_qbytes > 0 failed: got %d, want > 0\n", qid_ds.msg_qbytes, t);
nsz f7a881
nsz f7a881
	/* test send */
nsz f7a881
	T(msgsnd(qid, &msg, sizeof msg.data, IPC_NOWAIT));
nsz f7a881
	T(msgctl(qid, IPC_STAT, &qid_ds));
nsz f7a881
	EQ(qid_ds.msg_qnum, 1, "got %d, want %d");
nsz f7a881
	EQ(qid_ds.msg_lspid, getpid(), "got %d, want %d");
nsz f7a881
	if (qid_ds.msg_stime < t)
Szabolcs Nagy d8e961
		t_error("msg_stime is %lld want >= %lld\n", (long long)qid_ds.msg_stime, (long long)t);
Szabolcs Nagy d5fe2f
	if (qid_ds.msg_stime > t+5)
Szabolcs Nagy d8e961
		t_error("msg_stime is %lld want <= %lld\n", (long long)qid_ds.msg_stime, (long long)t+5);
nsz f7a881
}
nsz f7a881
nsz f7a881
static void rcv()
nsz f7a881
{
nsz f7a881
	key_t k;
nsz f7a881
	int qid;
nsz f7a881
	struct {
nsz f7a881
		long type;
nsz f7a881
		char data[20];
nsz f7a881
	} msg;
nsz f7a881
	long msgtyp = 0;
nsz f7a881
nsz f7a881
	T(k = ftok(path, id));
nsz f7a881
	T(qid = msgget(k, 0));
nsz f7a881
nsz f7a881
	errno = 0;
nsz f7a881
	if (msgrcv(qid, &msg, 0, msgtyp, 0) != -1 || errno != E2BIG)
Szabolcs Nagy cfa23c
		t_error("msgrcv should have failed when msgsize==0 with E2BIG, got %s\n", strerror(errno));
nsz f7a881
nsz f7a881
	/* test receive */
nsz f7a881
	T(msgrcv(qid, &msg, sizeof msg.data, msgtyp, IPC_NOWAIT));
nsz f7a881
	if (strcmp(msg.data,"test message") != 0)
Szabolcs Nagy cfa23c
		t_error("received \"%s\" instead of \"%s\"\n", msg.data, "test message");
nsz f7a881
nsz f7a881
	errno = 0;
nsz f7a881
	if (msgrcv(qid, &msg, sizeof msg.data, msgtyp, MSG_NOERROR|IPC_NOWAIT) != -1 || errno != ENOMSG)
Szabolcs Nagy cfa23c
		t_error("msgrcv should have failed when ther is no msg with ENOMSG, got %s\n", strerror(errno));
nsz 90956e
nsz 90956e
	/* cleanup */
nsz 90956e
	T(msgctl(qid, IPC_RMID, 0));
nsz f7a881
}
nsz f7a881
nsz f7a881
int main(void)
nsz f7a881
{
nsz f7a881
	int p;
nsz f7a881
	int status;
nsz f7a881
nsz f7a881
	snd();
nsz f7a881
	p = fork();
nsz f7a881
	if (p == -1)
Szabolcs Nagy cfa23c
		t_error("fork failed: %s\n", strerror(errno));
nsz f7a881
	else if (p == 0)
nsz f7a881
		rcv();
nsz f7a881
	else {
nsz f7a881
		T(waitpid(p, &status, 0));
nsz f7a881
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
Szabolcs Nagy cfa23c
			t_error("child exit status: %d\n", status);
nsz f7a881
	}
Szabolcs Nagy cfa23c
	return t_status;
nsz f7a881
}