Update copyright dates with scripts/update-copyrights
[platform/upstream/glibc.git] / sysvipc / test-sysvmsg.c
1 /* Basic tests for SYSV message queue functions.
2    Copyright (C) 2016-2023 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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/>.  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/ipc.h>
25 #include <sys/msg.h>
26
27 #include <test-sysvipc.h>
28
29 #include <support/support.h>
30 #include <support/check.h>
31 #include <support/temp_file.h>
32
33 #define TEXTSIZE 32
34 struct msgbuf_t
35 {
36 #ifdef _GNU_SOURCE
37   __syscall_slong_t type;
38 #else
39   long type;
40 #endif
41   char buf[TEXTSIZE];
42 };
43
44 #define MSGTYPE 0x01020304
45 #define MSGDATA "0123456789"
46
47 /* These are for the temporary file we generate.  */
48 static char *name;
49 static int msqid;
50
51 static void
52 remove_msq (void)
53 {
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);
57 }
58
59 static void
60 do_prepare (int argc, char *argv[])
61 {
62   int fd = create_temp_file ("tst-sysvmsg.", &name);
63   if (fd == -1)
64     FAIL_EXIT1 ("cannot create temporary file (errno=%d)", errno);
65 }
66
67 #define PREPARE do_prepare
68
69 /* It is not an extensive test, but rather a functional one aimed to check
70    correct parameter passing on kernel.  */
71
72 #define MSGQ_MODE 0644
73
74 static int
75 do_test (void)
76 {
77   atexit (remove_msq);
78
79   key_t key = ftok (name, 'G');
80   if (key == -1)
81     FAIL_EXIT1 ("ftok failed");
82
83   msqid = msgget (key, MSGQ_MODE | IPC_CREAT);
84   if (msqid == -1)
85     {
86       if (errno == ENOSYS)
87         FAIL_UNSUPPORTED ("msgget not supported");
88       FAIL_EXIT1 ("msgget failed (errno=%d)", errno);
89     }
90
91   TEST_COMPARE (msgctl (msqid, first_msg_invalid_cmd (), NULL), -1);
92   TEST_COMPARE (errno, EINVAL);
93
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);
98
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);
108
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,
112               IPC_NOWAIT) == -1
113       && errno != ENOMSG)
114     FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
115
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);
119
120   if (msgrcv (msqid, &msg2rcv, sizeof (msg2rcv.buf), MSGTYPE, 0) == -1)
121     FAIL_EXIT1 ("msgrcv failed (errno=%d)", errno);
122
123   int ret = 0;
124   if (strncmp (msg2snd.buf, msg2rcv.buf, TEXTSIZE) != 0)
125     ret = 1;
126
127   if (msgctl (msqid, IPC_RMID, NULL) == -1)
128     FAIL_EXIT1 ("msgctl failed");
129
130   return ret;
131 }
132
133 #include <support/test-driver.c>