| 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 | |
| 12 | int main() { |
| 13 | int msgq = msgget(IPC_PRIVATE, msgflg: 0666); |
| 14 | if (msgq == -1) perror(s: "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(dest: msg.string, CHECK_STRING); |
| 25 | int res = msgsnd(msqid: msgq, msgp: &msg, MSG_BUFLEN, IPC_NOWAIT); |
| 26 | if (res) { |
| 27 | fprintf(stderr, format: "Error sending message! %s\n" , strerror(errno)); |
| 28 | msgctl(msqid: msgq, IPC_RMID, NULL); |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | struct msg_s rcv_msg; |
| 33 | ssize_t len = msgrcv(msqid: msgq, msgp: &rcv_msg, MSG_BUFLEN, msgtyp: 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(msqid: msgq, IPC_RMID, NULL); |
| 38 | return 0; |
| 39 | } |
| 40 | |