• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2009
4  *				Veerendra C <vechandr@in.ibm.com>
5  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Test SysV IPC message passing through different namespaces.
12  *
13  * [Algorithm]
14  *
15  * In parent process create a new mesgq with a specific key.
16  * In cloned process try to access the created mesgq.
17  *
18  * Test will PASS if the mesgq is readable when flag is None.
19  * Test will FAIL if the mesgq is readable when flag is Unshare or Clone or
20  * the message received is wrong.
21  */
22 
23 #define _GNU_SOURCE
24 
25 #include <sys/wait.h>
26 #include <sys/msg.h>
27 #include <sys/types.h>
28 #include "tst_safe_sysv_ipc.h"
29 #include "tst_test.h"
30 #include "common.h"
31 
32 #define KEY_VAL 154326L
33 #define MSG_TYPE 5
34 #define MSG_TEXT "My message!"
35 
36 static char *str_op;
37 static int use_clone;
38 static int ipc_id = -1;
39 
40 struct msg_buf {
41 	long mtype;
42 	char mtext[80];
43 };
44 
check_mesgq(LTP_ATTRIBUTE_UNUSED void * vtest)45 static int check_mesgq(LTP_ATTRIBUTE_UNUSED void *vtest)
46 {
47 	int id, n;
48 	struct msg_buf msg = {};
49 
50 	id = msgget(KEY_VAL, 0);
51 
52 	if (id < 0) {
53 		if (use_clone == T_NONE)
54 			tst_res(TFAIL, "Plain cloned process didn't find mesgq");
55 		else
56 			tst_res(TPASS, "%s: container didn't find mesgq", str_op);
57 
58 		return 0;
59 	}
60 
61 	if (use_clone == T_NONE) {
62 		tst_res(TPASS, "Plain cloned process found mesgq inside container");
63 
64 		n = SAFE_MSGRCV(id, &msg, sizeof(msg.mtext), MSG_TYPE, 0);
65 
66 		tst_res(TINFO, "Mesg read of %d bytes, Type %ld, Msg: %s", n, msg.mtype, msg.mtext);
67 
68 		if (strcmp(msg.mtext, MSG_TEXT))
69 			tst_res(TFAIL, "Received the wrong text message");
70 
71 		return 0;
72 	}
73 
74 	tst_res(TFAIL, "%s: container init process found mesgq", str_op);
75 	return 0;
76 }
77 
run(void)78 static void run(void)
79 {
80 	struct msg_buf msg = {
81 		.mtype = MSG_TYPE,
82 		.mtext = MSG_TEXT,
83 	};
84 
85 	if (use_clone == T_NONE)
86 		SAFE_MSGSND(ipc_id, &msg, strlen(msg.mtext), 0);
87 
88 	tst_res(TINFO, "mesgq namespaces test: %s", str_op);
89 
90 	clone_unshare_test(use_clone, CLONE_NEWIPC, check_mesgq, NULL);
91 }
92 
setup(void)93 static void setup(void)
94 {
95 	use_clone = get_clone_unshare_enum(str_op);
96 
97 	if (use_clone != T_NONE)
98 		check_newipc();
99 
100 	ipc_id = SAFE_MSGGET(KEY_VAL, IPC_CREAT | IPC_EXCL | 0600);
101 }
102 
cleanup(void)103 static void cleanup(void)
104 {
105 	if (ipc_id != -1) {
106 		tst_res(TINFO, "Destroying message queue");
107 		SAFE_MSGCTL(ipc_id, IPC_RMID, NULL);
108 	}
109 }
110 
111 static struct tst_test test = {
112 	.test_all = run,
113 	.setup = setup,
114 	.cleanup = cleanup,
115 	.needs_root = 1,
116 	.forks_child = 1,
117 	.options = (struct tst_option[]) {
118 		{ "m:", &str_op, "Test execution mode <clone|unshare|none>" },
119 		{},
120 	},
121 };
122