android i2c权限,linux - Android i2c permission denied - Stack Overflow

博主在Android设备上开发了一个使用NDK访问I2C设备(MPU6050)的应用,通过i2c-dev接口进行交互。在设置设备DT绑定并启用i2c-1总线后,尝试打开/dev/i2c-1文件时遇到权限被拒绝的问题。尝试修改文件权限和ueventd.rc文件,甚至将i2ctools集成到系统映像中,但应用程序仍然返回权限被拒绝错误。尽管i2ctools可以访问该设备,但NDK应用无法正常工作,即使以root权限运行i2ctools也仅在shell下可行。目前尚未找到阻止‘权限被拒绝’错误的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I’ve developed an NDK application to access an i2c device via the i2c-dev interface. This application is running on an Android development kit, on which we have connected an MPU6050 device via i2c.

I’ve setup the DT binding for the device, as shown below

i2c@78b5000 {

MPU6050@68 {

compatible = "qcom, i2c-msm-v2";

reg = <0x68>;

};

};

and enabled the i2c-1 bus – which is the i2c bus the device is connected to – by changing the following line from “disabled” to “okay”

&i2c_1 {

status = "okay";

};

The issue is that when I try to open the i2c-1 file in my application via open()

char *filename = "dev/i2c-1";

const int file = open(filename, O_RDWR);

I get the permission denied error code returned.

I have only once successfully read from the WHO-AM-I register, by the following steps:

Adjust the permissions of the /dev/i2c-1 file to 666 using

$ chmod 666 i2c-1

Modify the ueventd.rc file to include this line

/dev/i2c-1 0666 root root

Kill the ueventd process to allow it to reload.

$ pgrep ueventd

$ kill xxx

Running ls -la in the /dev folder showed the permissions for i2c-1 as

crw-rw-rw- root root

The issue with applying this process is that each time the device reboots the ueventd file will be restored back to the original state and the permissions reverted.

I have since tried repeating this exact process, rebuilding the boot image with the altered files/permissions and flashing the new boot image to the device. This doesn't seem to work as I still receive the same "permission denied" error.

In an attempt to get around this I included i2ctools into the system image (did not come stock). When trying to run i2cdetect I initially got permission denied, so I followed the same procedure outlined above. This process successfully allowed i2ctools to access /dev/i2c-1 and detect my device.

The problem is that the NDK application - which uses /dev/i2c-1 - is still returning permission denied even after applying the process.

I could potentially modify my application to use i2ctools but my thinking is that if i2ctools can access i2c-1 after modifying the permissions, then my application should be able to - please correct me if i’m wrong.

UPDATE:

It seems that I can only use i2ctool if I am running a shell as root; this is with the permissions of i2c-1 set to global read/write. I've also tried setting the permissions of the parent folders in which i2c-1 resides in to 777 - still the same issue.

Can someone please tell me what i'm doing wrong or how I might stop the permission denied error?

(MindSpore) [ma-user SigmoidCustom]$bash build.sh using ASCEND_HOME_PATH: /home/ma-user/Ascend/ascend-toolkit/latest Preset CMake variables: ASCEND_COMPUTE_UNIT:STRING="ascend310b;ascend910b" ASCEND_PYTHON_EXECUTABLE:STRING="python3" CMAKE_BUILD_TYPE:STRING="Release" CMAKE_CROSS_PLATFORM_COMPILER:PATH="/usr/bin/aarch64-linux-gnu-g++" CMAKE_INSTALL_PREFIX:PATH="/home/ma-user/work/SigmoidCustom/SigmoidCustom/build_out" ENABLE_BINARY_PACKAGE:BOOL="True" ENABLE_CROSS_COMPILE:BOOL="False" ENABLE_SOURCE_PACKAGE:BOOL="True" ENABLE_TEST:BOOL="True" vendor_name:STRING="customize" -- The C compiler identification is GNU 7.3.0 CMake Warning (dev) at /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeFindBinUtils.cmake:224 (find_program): Policy CMP0109 is not set: find_program() requires permission to execute but not to read. Run "cmake --help-policy CMP0109" for policy details. Use the cmake_policy command to set the policy and suppress this warning. The file /usr/bin/ld is executable but not readable. CMake is ignoring it for compatibility. Call Stack (most recent call first): /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeDetermineCCompiler.cmake:196 (include) CMakeLists.txt:2 (project) This warning is for project developers. Use -Wno-dev to suppress it. CMake Warning (dev) at /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeFindBinUtils.cmake:224 (find_program): Policy CMP0109 is not set: find_program() requires permission to execute but not to read. Run "cmake --help-policy CMP0109" for policy details. Use the cmake_policy command to set the policy and suppress this warning. The file /bin/ld is executable but not readable. CMake is ignoring it for compatibility. Call Stack (most recent call first): /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeDetermineCCompiler.cmake:196 (include) CMakeLists.txt:2 (project) This warning is for project developers. Use -Wno-dev to suppress it. -- The CXX compiler identification is GNU 7.3.0 CMake Warning (dev) at /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeFindBinUtils.cmake:224 (find_program): Policy CMP0109 is not set: find_program() requires permission to execute but not to read. Run "cmake --help-policy CMP0109" for policy details. Use the cmake_policy command to set the policy and suppress this warning. The file /usr/bin/ld is executable but not readable. CMake is ignoring it for compatibility. Call Stack (most recent call first): /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeDetermineCXXCompiler.cmake:202 (include) CMakeLists.txt:2 (project) This warning is for project developers. Use -Wno-dev to suppress it. CMake Warning (dev) at /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeFindBinUtils.cmake:224 (find_program): Policy CMP0109 is not set: find_program() requires permission to execute but not to read. Run "cmake --help-policy CMP0109" for policy details. Use the cmake_policy command to set the policy and suppress this warning. The file /bin/ld is executable but not readable. CMake is ignoring it for compatibility. Call Stack (most recent call first): /home/ma-user/work/cmake-3.28.3-linux-aarch64/share/cmake-3.28/Modules/CMakeDetermineCXXCompiler.cmake:202 (include) CMakeLists.txt:2 (project) This warning is for project developers. Use -Wno-dev to suppress it. -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Opbuild generating sources -- Opbuild generating sources - done -- Configuring done (3.2s) -- Generating done (0.0s) CMake Warning: Manually-specified variables were not used by the project: CMAKE_CROSS_PLATFORM_COMPILER -- Build files have been written to: /home/ma-user/work/SigmoidCustom/SigmoidCustom/build_out [ 7%] Generating scripts/install.sh, scripts/upgrade.sh [ 14%] Building CXX object op_host/CMakeFiles/cust_optiling.dir/sigmoid_custom.cpp.o [ 28%] Generating tbe/.impl_timestamp [ 28%] Generating tbe/op_info_cfg/ai_core/ascend910b/aic-ascend910b-ops-info.json [ 35%] Generating tbe/op_info_cfg/ai_core/ascend310b/aic-ascend310b-ops-info.json [ 42%] Generating tbe/op_info_cfg/ai_core/npu_supported_ops.json [ 71%] Building CXX object op_host/CMakeFiles/cust_op_proto.dir/sigmoid_custom.cpp.o [ 71%] Building CXX object framework/tf_plugin/CMakeFiles/cust_tf_parsers.dir/tensorflow_sigmoid_custom_plugin.cc.o [ 71%] Building CXX object op_host/CMakeFiles/cust_opapi.dir/__/autogen/aclnn_sigmoid_custom.cpp.o [ 71%] Building CXX object op_host/CMakeFiles/cust_op_proto.dir/__/autogen/op_proto.cc.o [ 71%] Built target gen_version_info /bin/sh: /home/ma-user/work/SigmoidCustom/SigmoidCustom/cmake/util/gen_ops_filter.sh: Permission denied gmake[2]: *** [op_kernel/CMakeFiles/npu_supported_ops.dir/build.make:74: op_kernel/tbe/op_info_cfg/ai_core/npu_supported_ops.json] Error 126 gmake[1]: *** [CMakeFiles/Makefile2:671: op_kernel/CMakeFiles/npu_supported_ops.dir/all] Error 2 gmake[1]: *** Waiting for unfinished jobs.... [ 71%] Built target modify_vendor ==============check valid for ops info start============== ==============check valid for ops info end================ Compile op info cfg successfully. ==============check valid for ops info start============== ==============check valid for ops info end================ Compile op info cfg successfully. [ 71%] Built target ops_info_gen_ascend310b [ 71%] Built target ops_info_gen_ascend910b [ 71%] Built target ascendc_impl_gen [ 78%] Linking CXX shared library libcust_opapi.so [ 78%] Built target cust_opapi [ 85%] Linking CXX shared library libcust_tf_parsers.so [ 85%] Built target cust_tf_parsers [ 92%] Linking CXX shared library libcust_opsproto_rt2.0.so [100%] Linking CXX shared library libcust_opmaster_rt2.0.so [100%] Built target cust_op_proto [100%] Built target cust_optiling gmake: *** [Makefile:156: all] Error 2
最新发布
06-24
<think>我们正在处理CMake构建过程中的两个主要问题:1.权限被拒绝错误(Permissiondeniederror)2.CMP0109策略警告(CMP0109policywarnings)参考提供的引用,我们可以找到一些线索:-引用[2]和[3]提到了权限问题,通常发生在尝试向系统目录(如/usr/local)写入文件时,而当前用户没有足够的权限-引用[1]和[5]提供了一些关于策略设置和权限设置的解决方案。此外,用户提到了MindSpore在ascend310b和ascend910b平台上的构建。因此,我们需要考虑这些特定环境。解决方案思路:一、权限问题(Permissiondenied-原因:通常是因为CMake尝试将文件安装到需要更高权限的目录(如/usr/local)。-解决方案:a.使用有权限的目录:更改安装路径到用户有写入权限的目录(例如用户主目录下的某个目录)。b.使用sudo:但根据引用[3],用户可能没有sudo权限(公用服务器),所以可能需要其他方法。c.在构建时指定安装前缀(prefix)到有权限的路径。在CMake中,我们可以通过设置`CMAKE_INSTALL_PREFIX`变量来指定安装路径。例如:cmake-DCMAKE_INSTALL_PREFIX=$HOME/local..这样,安装过程就会将文件安装到用户主目录下的local目录,而不会需要root权限。二、关于CMP0109策略警告-引用[1]提到了使用`cmake_policy`命令来设置策略的行为(OLD或NEW)。-策略CMP0109是关于`find_program`忽略`PATH`环境变量中的空条目。从CMake3.9开始引入,默认行为是NEW(忽略空条目)。如果项目是为旧版CMake编写的,可能会触发警告。解决方案:在CMakeLists.txt中显式设置策略行为,以消除警告。例如:cmake_policy(SETCMP0109NEW)#使用新行为或者,如果项目需要旧行为(尽管不推荐,因为旧行为可能会在未来版本中被移除):cmake_policy(SETCMP0109OLD)但通常建议更新项目以适应新行为。结合用户的环境(MindSpore,ascend310b/ascend910b),我们可能需要考虑这些平台特定的构建要求。具体步骤建议:1.对于权限问题:在配置步骤时,指定一个用户有写入权限的安装目录。例如:mkdirbuildcdbuildcmake-DCMAKE_INSTALL_PREFIX=~/mindspore_install..#或其他有权限的路径makemakeinstall这样,安装过程就不会需要root权限。2.对于CMP0109策略警告:在CMakeLists.txt中找到合适的位置(通常在顶部)添加:if(POLICYCMP0109)cmake_policy(SETCMP0109NEW)endif()或者,如果不想修改CMakeLists.txt,也可以在CMake命令行中设置:cmake-DCMAKE_POLICY_DEFAULT_CMP0109=NEW...但注意,命令行设置策略默认值可能会影响其他策略,所以更推荐在CMakeLists.txt中显式设置。3.其他注意事项:根据引用[4],在Android6.0及以上,需要动态申请权限,但这里我们处理的是构建时的权限,所以不适用。引用[5]提到了在shell中修改文件权限(chmod),这也可以用于解决构建后脚本的权限问题,但主要问题还是安装目录的写入权限。由于用户的环境是Ascend平台,可能还需要注意一些环境变量的设置,比如`ASCEND_HOME`等,但这不属于当前问题的范畴。因此,总结解决方案:步骤一:解决权限问题通过设置安装前缀到用户目录。步骤二:解决CMP0109警告在CMakeLists.txt中设置策略CMP0109为NEW。如果用户无法修改CMakeLists.txt(比如是第三方项目),可以尝试在命令行传递策略设置(但并非所有CMake版本都支持):cmake--help-policyCMP0109#查看是否支持cmake-DCMAKE_POLICY_DEFAULT_CMP0109=NEW...然而,更通用的方法还是修改CMakeLists.txt。最后,如果构建过程中还有其他权限问题(比如在安装步骤之前就有权限问题),可能需要检查整个构建过程中是否有写操作需要权限,并相应调整路径。相关问题:1.如何设置CMake的安装路径以避免权限问题?2.如何在CMake中处理策略警告(如CMP0109)?3.在Ascend平台上构建MindSpore时,还有哪些常见问题需要注意?4.如何为没有root权限的用户安装软件?5.CMake策略(Policy)是什么?如何管理不同CMake版本的行为?</think>### CMake构建权限问题与CMP0109策略警告解决方案 #### 一、权限问题解决方案(Permission denied) 1. **修改安装路径** 在CMake配置时指定用户有写入权限的安装目录: ```bash cmake -DCMAKE_INSTALL_PREFIX=$HOME/local .. make install ``` 这将安装到`~/local`目录,避免系统目录权限问题[^3]。 2. **修复目录所有权** 若必须安装到系统目录(如`/usr/local`),使用以下命令修复所有权(需sudo权限): ```bash sudo chown -R $USER /usr/local/.catkin # 修改报错提示的目录 ``` 3. **处理运行时权限** 对于构建后运行的二进制文件(如Ascend平台程序),添加可执行权限: ```bash chmod u+x your_binary_file # 给所有者添加执行权限[^5] ``` #### 二、CMP0109策略警告解决方案 1. **显式设置策略行为** 在CMakeLists.txt顶部添加(需CMake ≥ 3.9): ```cmake cmake_policy(SET CMP0109 NEW) # 启用新行为 ``` **作用**:确保`find_program()`忽略PATH环境变量中的空条目[^1]。 2. **版本兼容处理** 添加版本检查避免兼容性问题: ```cmake if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.9) cmake_policy(SET CMP0109 NEW) endif() ``` 3. **检查策略依赖** 通过命令查看策略详情: ```bash cmake --help-policy CMP0109 ``` #### 三、Ascend平台特别注意事项 1. **环境变量权限** Ascend的Device侧程序可能需要访问设备: ```bash sudo chmod 777 /dev/davinci* # 临时方案,生产环境需严格授权 ``` 2. **安装脚本调整** 修改MindSpore安装脚本(`build.sh`): ```bash # 在cmake命令前添加策略设置 cmake .. -DCMAKE_POLICY_DEFAULT_CMP0109=NEW \ -DCMAKE_INSTALL_PREFIX=$HOME/mindspore ``` 3. **构建目录权限** 确保整个构建目录所有权正确: ```bash mkdir -p build && chown -R $USER:$USER build cd build && cmake .. ``` #### 四、完整解决流程 ```mermaid graph TD A[权限问题] --> B{是否必须系统目录?} B -->|是| C[chown修复目录权限] B -->|否| D[设置CMAKE_INSTALL_PREFIX] E[CMP0109警告] --> F[cmake_policy SET NEW] F --> G[添加版本兼容检查] H[Ascend平台] --> I[设备文件授权] I --> J[修改安装脚本] D --> K[构建成功] G --> K J --> K ``` ### 相关问题 1. 如何在非root环境下安全安装CMake及其依赖项? 2. CMake策略机制的工作原理是什么?如何管理跨版本策略兼容性? 3. Ascend平台下还有哪些常见构建权限问题需要处理? 4. 如何永久解决`/dev/davinci*`设备文件的访问权限问题? 5. CMake的`find_program`函数在路径搜索时有哪些注意事项? [^1]: CMake策略设置需匹配版本,OLD行为可能在未来移除 [^2]: 系统目录写入需root权限或目录所有权调整 [^3]: 用户级安装目录可彻底避免权限问题 [^4]: 运行时权限需代码动态申请或文件系统授权 [^5]: `chmod u+x`是最简执行权限解决方案
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值