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

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


http://www.kler.cn/a/613252.html

相关文章:

  • 基于音频驱动的CATIA动态曲面生成技术解析
  • MFC TRACE 宏的使用说明
  • MATLAB中getfield函数用法
  • 2025 年吉林省燃气企业从业人员考试:实用备考攻略与考试提分要点​
  • package.json版本前缀
  • 71. 我的第一个Linux驱动实验
  • ai画图comfyUI lora风格特化。
  • 项目管理证书 PMP 的含金量高吗?
  • A SAM-guided Two-stream Lightweight Model for AnomalyDetection
  • Pytorch中Tensorboard的学习
  • Web3.0合约安全:重入攻击防御方案
  • 李想官宣开源整车操作系统
  • Transformers重大更新!v4.50.1修复多Bug,模型训练更稳了
  • 【SPP】蓝牙串口协议(SPP)深度解析:从 RS232 仿真到设备互联的技术实现
  • 028-ImageCompress
  • Problem D: 类的继承
  • 【零基础入门unity游戏开发——2D篇】2D物理关节 —— Joint2D相关组件
  • 06-ADC
  • Linux内核调试 - Hung_task机制分析下
  • LLM 优化技术(2)——paged_attention 原理