当前位置: 首页 > news >正文

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

相关文章:

  • 流影---开源网络流量分析平台(二)(功能部署--流量探针)
  • C++ 中遍历 std::map
  • 网络基础概念
  • vue在template块里使用v-for循环对象、数组及嵌套结构数据
  • Redis-01.Redis课程内容介绍
  • YO-CSA-T:基于上下文与空间注意力的实时羽毛球轨迹追踪系统解析
  • 为什么package.json里的npm和npm -v版本不一致?
  • Flutter项目之构建打包分析
  • OpenCV 图形API(4)内核 API
  • 某合约任意提取BNB漏洞
  • centos7修复漏洞CVE-2023-38408
  • 群晖(Synology)存储目录挂载到Ubuntu 22.04.3 LTS系统的详细教程
  • 左右图文布局-语雀笔记
  • 力扣DAY34 | 热100 | 合并K个升序链表
  • ant-design-vue中英文切换
  • 【Easylive】SpringBoot启动类——EasyLiveWebRunApplication
  • MySQL索引优化全攻略:从原理到实战
  • OpenAI发布的《Addendum to GPT-4o System Card: Native image generation》文件的详尽笔记
  • MySQL执行原理
  • 测试用例篇
  • 亚马逊网站建设进度计划/买卖平台
  • 天津建设工程评标专家网站/今天新闻头条新闻
  • 免费的网站域名和空间/域名查询ip爱站网
  • 做网站需准备些什么软件/百度在线客服中心
  • 永久免费网站空间/电商关键词工具
  • 房地产行业市场分析/网站产品怎么优化