1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <errno.h>
17 #ifdef HAS_KCMP
18 #include <linux/kcmp.h>
19 #include <sys/syscall.h>
20 #endif
21 #include <sys/ipc.h>
22 #include <sys/msg.h>
23 #include <sys/sem.h>
24 #include <sys/shm.h>
25 #include <unistd.h>
26
27 #include <gtest/gtest.h>
28
29 #ifdef HAS_KCMP
kcmp(pid_t pid1,pid_t pid2,int type,unsigned long idx1,unsigned long idx2)30 int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
31 return syscall(SYS_kcmp, pid1, pid2, type, 0, idx1, idx2);
32 }
33 #endif
34
msgctl(int id,int cmd,msqid_ds * buf)35 int msgctl(int id, int cmd, msqid_ds* buf) {
36 #if !defined(__LP64__) || defined(__mips__)
37 // Annoyingly, the kernel requires this for 32-bit but rejects it for 64-bit.
38 // Mips64 is an exception to this, it requires the flag.
39 cmd |= IPC_64;
40 #endif
41 #if defined(SYS_msgctl)
42 return syscall(SYS_msgctl, id, cmd, buf);
43 #else
44 return syscall(SYS_ipc, MSGCTL, id, cmd, 0, buf, 0);
45 #endif
46 }
47
semctl(int id,int num,int cmd,semid_ds * buf)48 int semctl(int id, int num, int cmd, semid_ds* buf) {
49 #if !defined(__LP64__) || defined(__mips__)
50 // Annoyingly, the kernel requires this for 32-bit but rejects it for 64-bit.
51 // Mips64 is an exception to this, it requires the flag.
52 cmd |= IPC_64;
53 #endif
54 #if defined(SYS_msgctl)
55 return syscall(SYS_semctl, id, num, cmd, buf);
56 #else
57 return syscall(SYS_ipc, SEMCTL, id, num, cmd, buf, 0);
58 #endif
59 }
60
shmctl(int id,int cmd,shmid_ds * buf)61 int shmctl(int id, int cmd, shmid_ds* buf) {
62 #if !defined(__LP64__) || defined(__mips__)
63 // Annoyingly, the kernel requires this for 32-bit but rejects it for 64-bit.
64 // Mips64 is an exception to this, it requires the flag.
65 cmd |= IPC_64;
66 #endif
67 #if defined(SYS_shmctl)
68 return syscall(SYS_shmctl, id, cmd, buf);
69 #else
70 return syscall(SYS_ipc, SHMCTL, id, cmd, 0, buf, 0);
71 #endif
72 }
73
TEST(kernel_config,NOT_CONFIG_SYSVIPC)74 TEST(kernel_config, NOT_CONFIG_SYSVIPC) {
75 #ifdef HAS_KCMP
76 pid_t pid = getpid();
77 int ret = kcmp(pid, pid, KCMP_SYSVSEM, 0, 0);
78 int error = (ret == -1) ? (errno == ENOSYS) ? EOPNOTSUPP : errno : 0;
79 EXPECT_EQ(-1, kcmp(pid, pid, KCMP_SYSVSEM, 0, 0));
80 EXPECT_EQ(EOPNOTSUPP, error);
81 #endif
82
83 EXPECT_EQ(-1, access("/proc/sysvipc", R_OK));
84
85 EXPECT_EQ(-1, access("/proc/sysvipc/msg", F_OK));
86 EXPECT_EQ(-1, msgctl(-1, IPC_STAT, nullptr));
87 EXPECT_EQ(ENOSYS, errno);
88
89 EXPECT_EQ(-1, access("/proc/sysvipc/sem", F_OK));
90 EXPECT_EQ(-1, semctl(-1, 0, IPC_STAT, nullptr));
91 EXPECT_EQ(ENOSYS, errno);
92
93 EXPECT_EQ(-1, access("/proc/sysvipc/shm", F_OK));
94 EXPECT_EQ(-1, shmctl(-1, IPC_STAT, nullptr));
95 EXPECT_EQ(ENOSYS, errno);
96 }
97
98