root_android_open_dev

http://stackoverflow.com/questions/10480594/android-unable-to-open-device-file-in-jni-implementaion



I have taken Code Aurora's FM Radio code and merged with my Android Gingerbread codebase.

The FM app framework tries to access the fm radio device ( /dev/radio ) using JNI which is implemented in a file by name android_hardware_fm.cpp . There is a function in this file which tries to acquire a file descriptor to the device node using open() in the read/write mode. However, the call fails with error code -13 : Permission denied.

I also made a small C executable which tries to open the /dev/radio file ( in RDWR mode), prints its fd and closes it. It runs from /system/bin in the target system and displays a valid fd.

Btw, the JNI implementation is part of the android core library. It is located in frameworks/base/core/jni and is compiled as part of libandroid_runtime.so

Any ideas/solutions? Thanks in advance.

Clearly you donot have permissions to open the device from user space. In the second case when you are running the executable from terminal, you are having permissions probably because you have donesu before running the executable.

For your problem here, two things can be done.

1) Change the permissions of the node from terimnal.

Steps involved:

  • Open the terminal (adb shell)

  • Do su(In order to do this your device must be rooted)

  • Do chmod 777 /dev/radio in the terminal

Once this is done, your radio node is having proper permissions for the user to read and write. So you can now do open() call and it will work.

2) Programmatically you can achieve this (assuming your device is rooted and su is running on your device) by calling the below function - changePerm(). This is a small function I have written which will change the permissions of the device nodes or rather any system file that does not have user access. Once you have permissions, you can open it from user space. open() call will work properly after this.

void changePerm()
{
    Process chperm;
    try {
        chperm=Runtime.getRuntime().exec("su");


   DataOutputStream os = 
          new DataOutputStream(chperm.getOutputStream());
        os.writeBytes("chmod 777 /dev/radio\n");
        os.flush();

        os.writeBytes("exit\n");
        os.flush();

          chperm.waitFor();

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

I have tested this for other nodes. So it should also work for radio aswell. Let me know in case yo are facing any difficulty. Thanks

link | improve this answer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值