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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值