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