參考:安卓開啟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;
}