1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Basic error test for msgrcv(2). 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * 1)msgrcv(2) fails and sets errno to E2BIG if the message text length is 8f08c3bdfSopenharmony_ci * greater than msgsz and MSG_NOERROR isn't specified in msgflg. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * 2)The calling process does not have read permission on the message 11f08c3bdfSopenharmony_ci * queue, so msgrcv(2) fails and sets errno to EACCES. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * 3)msgrcv(2) fails and sets errno to EFAULT if the message buffer address 14f08c3bdfSopenharmony_ci * isn't accessible. 15f08c3bdfSopenharmony_ci * 16f08c3bdfSopenharmony_ci * 4)msgrcv(2) fails and sets errno to EINVAL if msqid was invalid(<0). 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci * 5)msgrcv(2) fails and sets errno to EINVAL if msgsize is less than 0. 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * 6)msgrcv(2) fails and sets errno to ENOMSG if IPC_NOWAIT was specified in 21f08c3bdfSopenharmony_ci * msgflg and no message of the requested type existed on the message queue. 22f08c3bdfSopenharmony_ci */ 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#define _GNU_SOURCE 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#include <string.h> 27f08c3bdfSopenharmony_ci#include <sys/wait.h> 28f08c3bdfSopenharmony_ci#include <sys/msg.h> 29f08c3bdfSopenharmony_ci#include <stdlib.h> 30f08c3bdfSopenharmony_ci#include <pwd.h> 31f08c3bdfSopenharmony_ci#include "tst_test.h" 32f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h" 33f08c3bdfSopenharmony_ci#include "libnewipc.h" 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic key_t msgkey; 36f08c3bdfSopenharmony_cistatic int queue_id = -1; 37f08c3bdfSopenharmony_cistatic int bad_id = -1; 38f08c3bdfSopenharmony_cistruct passwd *pw; 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_cistatic struct buf { 41f08c3bdfSopenharmony_ci long type; 42f08c3bdfSopenharmony_ci char mtext[MSGSIZE]; 43f08c3bdfSopenharmony_ci} rcv_buf, snd_buf = {2, "hello"}; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic struct tcase { 46f08c3bdfSopenharmony_ci int *id; 47f08c3bdfSopenharmony_ci struct buf *buffer; 48f08c3bdfSopenharmony_ci int msgsz; 49f08c3bdfSopenharmony_ci long msgtyp; 50f08c3bdfSopenharmony_ci int msgflag; 51f08c3bdfSopenharmony_ci int exp_user; 52f08c3bdfSopenharmony_ci int exp_err; 53f08c3bdfSopenharmony_ci} tcases[] = { 54f08c3bdfSopenharmony_ci {&queue_id, &rcv_buf, MSGSIZE - 1, 2, 0, 0, E2BIG}, 55f08c3bdfSopenharmony_ci {&queue_id, &rcv_buf, MSGSIZE, 2, 0, 1, EACCES}, 56f08c3bdfSopenharmony_ci {&queue_id, NULL, MSGSIZE, 2, 0, 0, EFAULT}, 57f08c3bdfSopenharmony_ci {&bad_id, &rcv_buf, MSGSIZE, 2, 0, 0, EINVAL}, 58f08c3bdfSopenharmony_ci {&queue_id, &rcv_buf, -1, 2, 0, 0, EINVAL}, 59f08c3bdfSopenharmony_ci {&queue_id, &rcv_buf, MSGSIZE, 3, IPC_NOWAIT, 0, ENOMSG}, 60f08c3bdfSopenharmony_ci {&queue_id, &rcv_buf, MSGSIZE, -1, IPC_NOWAIT, 0, ENOMSG}, 61f08c3bdfSopenharmony_ci {&queue_id, &rcv_buf, MSGSIZE, -1, IPC_NOWAIT | MSG_EXCEPT, 0, ENOMSG}, 62f08c3bdfSopenharmony_ci}; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cistatic void verify_msgrcv(struct tcase *tc) 65f08c3bdfSopenharmony_ci{ 66f08c3bdfSopenharmony_ci TST_EXP_FAIL2(msgrcv(*tc->id, tc->buffer, tc->msgsz, tc->msgtyp, tc->msgflag), tc->exp_err, 67f08c3bdfSopenharmony_ci "msgrcv(%i, %p, %i, %ld, %i)", *tc->id, tc->buffer, tc->msgsz, tc->msgtyp, tc->msgflag); 68f08c3bdfSopenharmony_ci} 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cistatic void do_test(unsigned int n) 71f08c3bdfSopenharmony_ci{ 72f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 73f08c3bdfSopenharmony_ci pid_t pid; 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0); 78f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 79f08c3bdfSopenharmony_ci if (pid == 0) { 80f08c3bdfSopenharmony_ci if (tc->exp_user) 81f08c3bdfSopenharmony_ci SAFE_SETUID(pw->pw_uid); 82f08c3bdfSopenharmony_ci verify_msgrcv(tc); 83f08c3bdfSopenharmony_ci exit(0); 84f08c3bdfSopenharmony_ci } 85f08c3bdfSopenharmony_ci tst_reap_children(); 86f08c3bdfSopenharmony_ci SAFE_MSGCTL(queue_id, IPC_RMID, NULL); 87f08c3bdfSopenharmony_ci} 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_cistatic void setup(void) 90f08c3bdfSopenharmony_ci{ 91f08c3bdfSopenharmony_ci msgkey = GETIPCKEY(); 92f08c3bdfSopenharmony_ci pw = SAFE_GETPWNAM("nobody"); 93f08c3bdfSopenharmony_ci} 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_cistatic void cleanup(void) 96f08c3bdfSopenharmony_ci{ 97f08c3bdfSopenharmony_ci if (queue_id != -1) 98f08c3bdfSopenharmony_ci SAFE_MSGCTL(queue_id, IPC_RMID, NULL); 99f08c3bdfSopenharmony_ci} 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_cistatic struct tst_test test = { 102f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 103f08c3bdfSopenharmony_ci .needs_root = 1, 104f08c3bdfSopenharmony_ci .forks_child = 1, 105f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 106f08c3bdfSopenharmony_ci .setup = setup, 107f08c3bdfSopenharmony_ci .cleanup = cleanup, 108f08c3bdfSopenharmony_ci .test = do_test 109f08c3bdfSopenharmony_ci}; 110