File: msgque.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (136 lines) | stat: -rw-r--r-- 2,985 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <sched.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <errno.h>

#include "zdtmtst.h"

const char *test_doc = "Tests sysv5 msg queues supporting by checkpointing";
const char *test_author = "Stanislav Kinsbursky <skinsbursky@openvz.org>";

struct msg1 {
	long mtype;
	char mtext[30];
};
#define TEST_STRING "Test sysv5 msg"
#define MSG_TYPE    1

#define ANOTHER_TEST_STRING "Yet another test sysv5 msg"
#define ANOTHER_MSG_TYPE    26538

int main(int argc, char **argv)
{
	key_t key;
	int msg, pid;
	struct msg1 msgbuf;
	int chret;

	test_init(argc, argv);

	key = ftok(argv[0], 822155650);
	if (key == -1) {
		pr_perror("Can't make key");
		exit(1);
	}

	pid = test_fork();
	if (pid < 0) {
		pr_perror("Can't fork");
		exit(1);
	}

	msg = msgget(key, IPC_CREAT | IPC_EXCL | 0666);
	if (msg == -1) {
		msg = msgget(key, 0666);
		if (msg == -1) {
			pr_perror("Can't get queue");
			goto err_kill;
		}
	}

	if (pid == 0) {
		test_waitsig();

		if (msgrcv(msg, &msgbuf, sizeof(TEST_STRING), MSG_TYPE, IPC_NOWAIT) == -1) {
			fail("Child: msgrcv failed");
			return -errno;
		}

		if (strncmp(TEST_STRING, msgbuf.mtext, sizeof(TEST_STRING))) {
			fail("Child: the source and received strings aren't equal");
			return -errno;
		}
		test_msg("Child: received %s\n", msgbuf.mtext);

		msgbuf.mtype = ANOTHER_MSG_TYPE;
		memcpy(msgbuf.mtext, ANOTHER_TEST_STRING, sizeof(ANOTHER_TEST_STRING));
		if (msgsnd(msg, &msgbuf, sizeof(ANOTHER_TEST_STRING), IPC_NOWAIT) != 0) {
			fail("Child: msgsnd failed");
			return -errno;
		};
		pass();
		return 0;
	} else {
		msgbuf.mtype = MSG_TYPE;
		memcpy(msgbuf.mtext, TEST_STRING, sizeof(TEST_STRING));
		if (msgsnd(msg, &msgbuf, sizeof(TEST_STRING), IPC_NOWAIT) != 0) {
			fail("Parent: msgsnd failed");
			goto err_kill;
		};

		msgbuf.mtype = ANOTHER_MSG_TYPE;
		memcpy(msgbuf.mtext, ANOTHER_TEST_STRING, sizeof(ANOTHER_TEST_STRING));
		if (msgsnd(msg, &msgbuf, sizeof(ANOTHER_TEST_STRING), IPC_NOWAIT) != 0) {
			fail("child: msgsnd (2) failed");
			return -errno;
		};

		test_daemon();
		test_waitsig();

		kill(pid, SIGTERM);

		wait(&chret);
		chret = WEXITSTATUS(chret);
		if (chret) {
			fail("Parent: child exited with non-zero code %d (%s)", chret, strerror(chret));
			goto out;
		}

		if (msgrcv(msg, &msgbuf, sizeof(ANOTHER_TEST_STRING), ANOTHER_MSG_TYPE, IPC_NOWAIT) == -1) {
			fail("Parent: msgrcv failed");
			goto err;
		}

		if (strncmp(ANOTHER_TEST_STRING, msgbuf.mtext, sizeof(ANOTHER_TEST_STRING))) {
			fail("Parent: the source and received strings aren't equal");
			goto err;
		}
		test_msg("Parent: received %s\n", msgbuf.mtext);

		pass();
	}

out:
	if (msgctl(msg, IPC_RMID, 0)) {
		fail("Failed to destroy message queue");
		return -errno;
	}
	return chret;

err_kill:
	kill(pid, SIGKILL);
	wait(NULL);
err:
	chret = -errno;
	goto out;
}