xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/sysmsg.c (revision cabe31f415054b45b4fa6c17e4ddf09cc39bf4e8)
1 // RUN: %clang -O1 %s -o %t && %run %t
2 // UNSUPPORTED: android
3 #include <assert.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/msg.h>
8 
9 #define CHECK_STRING "hello, world!"
10 #define MSG_BUFLEN 0x100
11 
main()12 int main() {
13   int msgq = msgget(IPC_PRIVATE, 0666);
14   if (msgq == -1) perror("msgget:");
15   assert(msgq != -1);
16 
17   struct msg_s {
18     long mtype;
19     char string[MSG_BUFLEN];
20   };
21 
22   struct msg_s msg = {
23       .mtype = 1};
24   strcpy(msg.string, CHECK_STRING);
25   int res = msgsnd(msgq, &msg, MSG_BUFLEN, IPC_NOWAIT);
26   if (res) {
27     fprintf(stderr, "Error sending message! %s\n", strerror(errno));
28     msgctl(msgq, IPC_RMID, NULL);
29     return -1;
30   }
31 
32   struct msg_s rcv_msg;
33   ssize_t len = msgrcv(msgq, &rcv_msg, MSG_BUFLEN, msg.mtype, IPC_NOWAIT);
34   assert(len == MSG_BUFLEN);
35   assert(msg.mtype == rcv_msg.mtype);
36   assert(!memcmp(msg.string, rcv_msg.string, MSG_BUFLEN));
37   msgctl(msgq, IPC_RMID, NULL);
38   return 0;
39 }
40