android开启Sys V IPC,并使用共享内存编程
参考:安卓开启Sys V IPC,并使用共享内存编程 | 久奈浜的CS部
删除config中-# CONFIG_SYSVIPC is not set
在rk3576.config中增加CONFIG_SYSVIPC=y
CONFIG_SYSVIPC=y
CONFIG_SYSVIPC_SYSCTL=y
CONFIG_SYSVIPC_COMPAT=y
CONFIG_IPC_NS=y
system/sepolicy/prebuilts/api/34.0/public/domain.te
system/sepolicy/public/domain.te
system/libvintf/check_vintf.cpp
去掉shm限制
+++ b/system/libvintf/check_vintf.cpp
@@ -652,7 +652,8 @@ int main(int argc, char** argv) {
if (compat.error().code() == 0) {
LOG(ERROR) << "ERROR: files are incompatible: " << compat.error();
std::cout << "INCOMPATIBLE" << std::endl;
- return EX_DATAERR;
+ //return EX_DATAERR;
+ return EX_OK;
}
LOG(ERROR) << "ERROR: " << strerror(compat.error().code()) << ": " << compat.error();
return EX_SOFTWARE;
diff --git a/system/sepolicy/prebuilts/api/34.0/public/domain.te b/system/sepolicy/prebuilts/api/34.0/public/domain.te
index 1da3f51a96a..2bdec93bcf4 100644
--- a/system/sepolicy/prebuilts/api/34.0/public/domain.te
+++ b/system/sepolicy/prebuilts/api/34.0/public/domain.te
@@ -1020,7 +1020,7 @@ neverallow { domain -init -system_server } heapdump_data_file:file read;
# that, even assuming only non-buggy and non-malicious code, it is very likely
# that over time, the kernel global tables used to implement SysV IPCs will fill
# up.
-neverallow * *:{ shm sem msg msgq } *;
+neverallow * *:{ sem msg msgq } *;
# Do not mount on top of symlinks, fifos, or sockets.
# Feature parity with Chromium LSM.
diff --git a/system/sepolicy/public/domain.te b/system/sepolicy/public/domain.te
index 1da3f51a96a..2bdec93bcf4 100644
--- a/system/sepolicy/public/domain.te
+++ b/system/sepolicy/public/domain.te
@@ -1020,7 +1020,7 @@ neverallow { domain -init -system_server } heapdump_data_file:file read;
# that, even assuming only non-buggy and non-malicious code, it is very likely
# that over time, the kernel global tables used to implement SysV IPCs will fill
# up.
-neverallow * *:{ shm sem msg msgq } *;
+neverallow * *:{ sem msg msgq } *;
# Do not mount on top of symlinks, fifos, or sockets.
# Feature parity with Chromium LSM.
修改check_vintf.cpp
安卓编译的时候还会有一个检查,以确保CONFIG_SYS_V_IPC设置为n,为了规避这项检查,我们需要修改./system/libvintf/check_vintf.cpp
中的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main()
{
int shmid;
char *shmaddr;
char message[] = "hello world";
key_t key1 = ftok("/data/local/tmp/key/test_key", 1);
// 创建共享内存段
shmid = shmget(key1, sizeof(message), IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget");
exit(1);
}
// 将共享内存连接到当前进程的地址空间
shmaddr = (char *)shmat(shmid, NULL, 0);
if (shmaddr == (char *)(-1)) {
perror("shmat");
exit(1);
}
// 将数据写入共享内存
strcpy(shmaddr, message);
printf("Message '%s' written to shared memory\n", message);
// 分离共享内存
shmdt(shmaddr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
int shmid;
char *shmaddr;
key_t key1 = ftok("/data/local/tmp/key/test_key", 1);
// 获取共享内存段 12 is sizeof("hello world")
shmid = shmget(key1, 12, 0666);
if (shmid == -1) {
perror("shmget");
exit(1);
}
// 连接到共享内存段
shmaddr = (char *)shmat(shmid, NULL, 0);
if (shmaddr == (char *)(-1)) {
perror("shmat");
exit(1);
}
// 从共享内存读取数据并打印
printf("Read from shared memory: %s\n", shmaddr);
// 分离共享内存
shmdt(shmaddr);
// 删除共享内存段(在实际应用中,可能需要谨慎处理删除操作)
shmctl(shmid, IPC_RMID, NULL);
return 0;
}