1 /* 2 * Copyright (c) International Business Machines Corp., 2001 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12 * the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. 16 */ 17 18 /* 19 * DESCRIPTION 20 * 1) msgget(2) fails if a message queue exists for key and msgflg 21 * specified both IPC_CREAT and IPC_EXCL. 22 * 2) msgget(2) fails if no message queue exists for key and msgflg 23 * did not specify IPC_CREAT. 24 * 3) msgget(2) fails if a message queue exists for key, but the 25 * calling process does not have permission to access the queue, 26 * and does not have the CAP_IPC_OWNER capability. 27 * 28 */ 29 #include <errno.h> 30 #include <stdlib.h> 31 #include <sys/types.h> 32 #include <sys/ipc.h> 33 #include <sys/msg.h> 34 #include <pwd.h> 35 36 #include "tst_test.h" 37 #include "tst_safe_sysv_ipc.h" 38 #include "libnewipc.h" 39 40 static key_t msgkey, msgkey1; 41 static int queue_id = -1; 42 static struct passwd *pw; 43 44 static struct tcase { 45 int *key; 46 int flags; 47 int exp_err; 48 /*1: nobody expected 0: root expected */ 49 int exp_user; 50 } tcases[] = { 51 {&msgkey, IPC_CREAT | IPC_EXCL, EEXIST, 0}, 52 {&msgkey1, IPC_PRIVATE, ENOENT, 0}, 53 {&msgkey1, IPC_EXCL, ENOENT, 0}, 54 {&msgkey, MSG_RD, EACCES, 1}, 55 {&msgkey, MSG_WR, EACCES, 1}, 56 {&msgkey, MSG_RW, EACCES, 1} 57 }; 58 59 static void verify_msgget(struct tcase *tc) 60 { 61 TEST(msgget(*tc->key, tc->flags)); 62 63 if (TEST_RETURN != -1) { 64 tst_res(TFAIL, "msgget() succeeded unexpectedly"); 65 return; 66 } 67 68 if (TEST_ERRNO == tc->exp_err) { 69 tst_res(TPASS | TTERRNO, "msgget() failed as expected"); 70 } else { 71 tst_res(TFAIL | TTERRNO, "msgget() failed unexpectedly," 72 " expected %s", tst_strerrno(tc->exp_err)); 73 } 74 } 75 76 static void do_test(unsigned int n) 77 { 78 pid_t pid; 79 struct tcase *tc = &tcases[n]; 80 81 if (tc->exp_user == 0) { 82 verify_msgget(tc); 83 } else { 84 pid = SAFE_FORK(); 85 if (pid) { 86 tst_reap_children(); 87 } else { 88 SAFE_SETUID(pw->pw_uid); 89 verify_msgget(tc); 90 exit(0); 91 } 92 } 93 } 94 95 static void setup(void) 96 { 97 msgkey = GETIPCKEY(); 98 msgkey1 = GETIPCKEY(); 99 100 queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL); 101 102 pw = SAFE_GETPWNAM("nobody"); 103 } 104 105 static void cleanup(void) 106 { 107 if (queue_id != -1) 108 SAFE_MSGCTL(queue_id, IPC_RMID, NULL); 109 } 110 111 static struct tst_test test = { 112 .tid = "msgget02", 113 .needs_tmpdir = 1, 114 .needs_root = 1, 115 .forks_child = 1, 116 .tcnt = ARRAY_SIZE(tcases), 117 .setup = setup, 118 .cleanup = cleanup, 119 .test = do_test 120 }; 121