简体   繁体   English

Android NDK open() 设备权限被拒绝

[英]Android NDK open() device permission denied

In a native call, I'm trying to open("/dev/video4", O_RDWR) but I get errno EACCES 13 "permission denied".在本地调用中,我试图open("/dev/video4", O_RDWR)但我得到 errno EACCES 13 "permission denied"。

If I run the same code* in an executable, on the same Android host, as the same UID of the installed app I'm running above, it works fine.如果我在同一 Android 主机上的可执行文件中运行相同的代码*,与我在上面运行的已安装应用程序的 UID 相同,则它可以正常工作。 (* minor differences like main() instead of Java_com_test_testOpen() ) (* 细微差别,如main()而不是Java_com_test_testOpen()

I've tried chmod 666 /dev/video4 and still get EACCES, which is especially strange.我试过chmod 666 /dev/video4仍然得到 EACCES,这特别奇怪。

Why does the same code , on the same host , as the same user , give EACCESS when called via JNI, and success when called as standalone executable?为什么相同的代码,在相同的主机上,作为相同的用户,在通过 JNI 调用时给出 EACCESS,而在作为独立可执行文件调用时成功?

The test device is rooted and running Cyanogenmod 12.1 (API 22) and I'm targeting >= API 21 (5.0 Lollipop) on rooted devices.测试设备已植根并运行 Cyanogenmod 12.1 (API 22),我的目标是 >= API 21 (5.0 Lollipop) 在植根设备上。 Thanks for your help.谢谢你的帮助。

Since you get an error when running your code from an Android Java application, I would guess that you are missing a permission.由于您在从 Android Java 应用程序运行代码时遇到错误,我猜您是缺少权限。 It's the camera that you are trying to access, if I am not mistaken, so if you add:如果我没记错的话,这是您要访问的相机,所以如果您添加:

 <uses-permission android:name="android.permission.CAMERA" />

to your AndroidManifest.xml, your application should run fine.到您的 AndroidManifest.xml,您的应用程序应该可以正常运行。

Since I'm building Cyanogenmod 12.1 (API 22) with other minor hacks I was able to get permissions for /dev/video* in my app by using the following hacks:由于我正在使用其他小技巧构建 Cyanogenmod 12.1 (API 22),因此我能够通过使用以下技巧在我的应用中获得/dev/video*权限:

  1. For standard Linux permissions, android.permission.CAMERA no longer seems to allow access to /dev/video* even though they're owned by system:camera .对于标准的 Linux 权限, android.permission.CAMERA似乎不再允许访问/dev/video*即使它们归system:camera Instead, I edited device/samsung/klte-common/rootdir/etc/ueventd.qcom.rc and changed the /dev/video* line to 0666 .相反,我编辑了device/samsung/klte-common/rootdir/etc/ueventd.qcom.rc并将/dev/video*行更改为0666
  2. For SE Linux permissions, I added the line allow untrusted_app video_device:chr_file rw_file_perms;对于 SE Linux 权限,我添加了行allow untrusted_app video_device:chr_file rw_file_perms; to external/sepolicy/untrusted_app.te .external/sepolicy/untrusted_app.te

After rebuilding and installing the image, my JNI lib is able to access /dev/video* and my client is happy!重建和安装映像后,我的 JNI 库可以访问/dev/video* ,我的客户很高兴!

The answers was not enough for me, so I left one more anwser.答案对我来说还不够,所以我又留下了一个答案。 open API has more parameter 'mode'.开放 API 有更多参数“模式”。

int open(const char *pathname, int flags, mode_t mode); int open(const char *pathname, int flags, mode_t mode);

check out the link http://man7.org/linux/man-pages/man2/open.2.html查看链接http://man7.org/linux/man-pages/man2/open.2.html

you might need to use 'open' with S_IRWXU option.您可能需要使用带有 S_IRWXU 选项的“打开”。 like喜欢

open("/dev/video4", O_RDWR | O_CREAT, S_IRWXU )打开(“/dev/video4”,O_RDWR | O_CREAT,S_IRWXU)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM