1 /* Basic tests for SYSV message queue functions.
2 Copyright (C) 2016-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
27 #include <test-sysvipc.h>
29 #include <support/support.h>
30 #include <support/check.h>
31 #include <support/temp_file.h>
37 __syscall_slong_t type
;
44 #define MSGTYPE 0x01020304
45 #define MSGDATA "0123456789"
47 /* These are for the temporary file we generate. */
54 /* Enforce message queue removal in case of early test failure.
55 Ignore error since the msgq may already have being removed. */
56 msgctl (msqid
, IPC_RMID
, NULL
);
60 do_prepare (int argc
, char *argv
[])
62 int fd
= create_temp_file ("tst-sysvmsg.", &name
);
64 FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno
);
67 #define PREPARE do_prepare
69 /* It is not an extensive test, but rather a functional one aimed to check
70 correct parameter passing on kernel. */
72 #define MSGQ_MODE 0644
79 key_t key
= ftok (name
, 'G');
81 FAIL_EXIT1 ("ftok failed");
83 msqid
= msgget (key
, MSGQ_MODE
| IPC_CREAT
);
87 FAIL_UNSUPPORTED ("msgget not supported");
88 FAIL_EXIT1 ("msgget failed (errno=%d)", errno
);
91 TEST_COMPARE (msgctl (msqid
, first_msg_invalid_cmd (), NULL
), -1);
92 TEST_COMPARE (errno
, EINVAL
);
94 /* Get message queue kernel information and do some sanity checks. */
95 struct msqid_ds msginfo
;
96 if (msgctl (msqid
, IPC_STAT
, &msginfo
) == -1)
97 FAIL_EXIT1 ("msgctl with IPC_STAT failed (errno=%d)", errno
);
99 if (msginfo
.msg_perm
.__key
!= key
)
100 FAIL_EXIT1 ("msgid_ds::msg_perm::key (%d) != %d",
101 (int) msginfo
.msg_perm
.__key
, (int) key
);
102 if (msginfo
.msg_perm
.mode
!= MSGQ_MODE
)
103 FAIL_EXIT1 ("msgid_ds::msg_perm::mode (%o) != %o",
104 msginfo
.msg_perm
.mode
, MSGQ_MODE
);
105 if (msginfo
.msg_qnum
!= 0)
106 FAIL_EXIT1 ("msgid_ds::msg_qnum (%lu) != 0",
107 (long unsigned) msginfo
.msg_qnum
);
109 /* Check if last argument (IPC_NOWAIT) is correctly handled. */
110 struct msgbuf_t msg2rcv
= { 0 };
111 if (msgrcv (msqid
, &msg2rcv
, sizeof (msg2rcv
.buf
), MSGTYPE
,
114 FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno
);
116 struct msgbuf_t msg2snd
= { MSGTYPE
, MSGDATA
};
117 if (msgsnd (msqid
, &msg2snd
, sizeof (msg2snd
.buf
), 0) == -1)
118 FAIL_EXIT1 ("msgsnd failed (errno=%d)", errno
);
120 if (msgrcv (msqid
, &msg2rcv
, sizeof (msg2rcv
.buf
), MSGTYPE
, 0) == -1)
121 FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno
);
124 if (strncmp (msg2snd
.buf
, msg2rcv
.buf
, TEXTSIZE
) != 0)
127 if (msgctl (msqid
, IPC_RMID
, NULL
) == -1)
128 FAIL_EXIT1 ("msgctl failed");
133 #include <support/test-driver.c>