1 /* $NetBSD: t_msgget.c,v 1.2 2014/02/27 00:59:50 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jukka Ruohonen. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_msgget.c,v 1.2 2014/02/27 00:59:50 joerg Exp $"); 33 34 #include <sys/msg.h> 35 #include <sys/stat.h> 36 #include <sys/sysctl.h> 37 #include <sys/wait.h> 38 39 #include <atf-c.h> 40 #include <errno.h> 41 #include <pwd.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <sysexits.h> 46 #include <time.h> 47 #include <unistd.h> 48 49 #define MSG_KEY 12345689 50 51 static void clean(void); 52 53 static void 54 clean(void) 55 { 56 int id; 57 58 if ((id = msgget(MSG_KEY, 0)) != -1) 59 (void)msgctl(id, IPC_RMID, 0); 60 } 61 62 ATF_TC_WITH_CLEANUP(msgget_excl); 63 ATF_TC_HEAD(msgget_excl, tc) 64 { 65 atf_tc_set_md_var(tc, "descr", "Test msgget(2) with IPC_EXCL"); 66 } 67 68 ATF_TC_BODY(msgget_excl, tc) 69 { 70 int id; 71 72 /* 73 * Create a message queue and re-open it with 74 * O_CREAT and IPC_EXCL set. This should fail. 75 */ 76 id = msgget(MSG_KEY, IPC_CREAT | 0600); 77 78 if (id < 0) 79 atf_tc_fail("failed to create message queue"); 80 81 errno = 0; 82 83 if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) != -1) 84 atf_tc_fail("msgget(2) failed for IPC_EXCL"); 85 86 ATF_REQUIRE(errno == EEXIST); 87 88 /* 89 * However, the same call should succeed 90 * when IPC_EXCL is not set in the flags. 91 */ 92 id = msgget(MSG_KEY, IPC_CREAT | 0600); 93 94 if (id < 0) 95 atf_tc_fail("msgget(2) failed to re-open"); 96 97 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0); 98 } 99 100 ATF_TC_CLEANUP(msgget_excl, tc) 101 { 102 clean(); 103 } 104 105 ATF_TC_WITH_CLEANUP(msgget_exit); 106 ATF_TC_HEAD(msgget_exit, tc) 107 { 108 atf_tc_set_md_var(tc, "descr", 109 "Test that XSI message queues are " 110 "not removed when the process exits"); 111 } 112 113 ATF_TC_BODY(msgget_exit, tc) 114 { 115 int id, sta; 116 pid_t pid; 117 118 pid = fork(); 119 ATF_REQUIRE(pid >= 0); 120 121 if (pid == 0) { 122 123 if (msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | 0600) == -1) 124 _exit(EXIT_FAILURE); 125 126 _exit(EXIT_SUCCESS); 127 } 128 129 (void)wait(&sta); 130 131 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 132 atf_tc_fail("failed to create message queue"); 133 134 id = msgget(MSG_KEY, 0); 135 136 if (id == -1) 137 atf_tc_fail("message queue was removed on process exit"); 138 139 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0); 140 } 141 142 ATF_TC_CLEANUP(msgget_exit, tc) 143 { 144 clean(); 145 } 146 147 ATF_TC_WITH_CLEANUP(msgget_init); 148 ATF_TC_HEAD(msgget_init, tc) 149 { 150 atf_tc_set_md_var(tc, "descr", 151 "Test that msgget(2) initializes data structures properly"); 152 } 153 154 ATF_TC_BODY(msgget_init, tc) 155 { 156 const uid_t uid = geteuid(); 157 const gid_t gid = getegid(); 158 struct msqid_ds msgds; 159 time_t t; 160 int id; 161 162 (void)memset(&msgds, 0x9, sizeof(struct msqid_ds)); 163 164 t = time(NULL); 165 id = msgget(MSG_KEY, IPC_CREAT | 0600); 166 167 ATF_REQUIRE(id !=-1); 168 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0); 169 170 ATF_CHECK(msgds.msg_qnum == 0); 171 ATF_CHECK(msgds.msg_lspid == 0); 172 ATF_CHECK(msgds.msg_lrpid == 0); 173 ATF_CHECK(msgds.msg_rtime == 0); 174 ATF_CHECK(msgds.msg_stime == 0); 175 ATF_CHECK(msgds.msg_perm.uid == uid); 176 ATF_CHECK(msgds.msg_perm.gid == gid); 177 ATF_CHECK(msgds.msg_perm.cuid == uid); 178 ATF_CHECK(msgds.msg_perm.cgid == gid); 179 ATF_CHECK(msgds.msg_perm.mode == 0600); 180 181 if (llabs(t - msgds.msg_ctime) > 5) 182 atf_tc_fail("msgget(2) initialized current time incorrectly"); 183 184 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0); 185 } 186 187 ATF_TC_CLEANUP(msgget_init, tc) 188 { 189 clean(); 190 } 191 192 ATF_TC(msgget_limit); 193 ATF_TC_HEAD(msgget_limit, tc) 194 { 195 atf_tc_set_md_var(tc, "descr", "Test msgget(2) against system limits"); 196 } 197 198 ATF_TC_BODY(msgget_limit, tc) 199 { 200 size_t len = sizeof(int); 201 bool fail = false; 202 int i, lim = 0; 203 int *buf; 204 205 if (sysctlbyname("kern.ipc.msgmni", &lim, &len, NULL, 0) != 0) 206 atf_tc_skip("failed to read kern.ipc.msgmni sysctl"); 207 208 buf = calloc(lim + 1, sizeof(*buf)); 209 ATF_REQUIRE(buf != NULL); 210 211 for (i = 0; i < lim; i++) { 212 213 buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600); 214 215 (void)fprintf(stderr, "key[%d] = %d\n", i, buf[i]); 216 217 /* 218 * This test only works when there are zero existing 219 * message queues. Thus, bypass the unit test when 220 * this precondition is not met, for reason or another. 221 */ 222 if (buf[i] == -1) 223 goto out; 224 } 225 226 errno = 0; 227 228 buf[i] = msgget(MSG_KEY + i, IPC_CREAT | IPC_EXCL | 0600); 229 230 if (buf[i] != -1 || errno != ENOSPC) 231 fail = true; 232 233 out: /* Remember to clean-up. */ 234 for (i = 0; i < lim; i++) 235 (void)msgctl(buf[i], IPC_RMID, 0); 236 237 free(buf); 238 239 if (fail != false) 240 atf_tc_fail("msgget(2) opened more than %d queues", lim); 241 } 242 243 ATF_TC_WITH_CLEANUP(msgget_mode); 244 ATF_TC_HEAD(msgget_mode, tc) 245 { 246 atf_tc_set_md_var(tc, "descr", "Test different modes with msgget(2)"); 247 atf_tc_set_md_var(tc, "require.user", "root"); 248 } 249 250 ATF_TC_BODY(msgget_mode, tc) 251 { 252 static const mode_t mode[] = { 253 S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP, 254 S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH 255 }; 256 257 struct msqid_ds msgds; 258 size_t i; 259 int id; 260 261 for (i = 0; i < __arraycount(mode); i++) { 262 263 (void)fprintf(stderr, "testing mode %d\n", mode[i]); 264 (void)memset(&msgds, 0, sizeof(struct msqid_ds)); 265 266 id = msgget(MSG_KEY, IPC_CREAT | IPC_EXCL | (int)mode[i]); 267 268 ATF_REQUIRE(id != -1); 269 ATF_REQUIRE(msgctl(id, IPC_STAT, &msgds) == 0); 270 ATF_REQUIRE(msgds.msg_perm.mode == mode[i]); 271 ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0); 272 } 273 } 274 275 ATF_TC_CLEANUP(msgget_mode, tc) 276 { 277 clean(); 278 } 279 280 281 ATF_TP_ADD_TCS(tp) 282 { 283 284 ATF_TP_ADD_TC(tp, msgget_excl); 285 ATF_TP_ADD_TC(tp, msgget_exit); 286 ATF_TP_ADD_TC(tp, msgget_init); 287 ATF_TP_ADD_TC(tp, msgget_limit); 288 ATF_TP_ADD_TC(tp, msgget_mode); 289 290 return atf_no_error(); 291 } 292