Вопрос

I have an application which consists of the native part and higher layer.

Therefore, it needs to be granted the root privilege to run some native functions.

As the article I post before, I added a piece of code taken from Bluez Utils(hidd.c).

if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    close(sk);
    return -1;
   }

The code has been built to be a .so by NDK.

Unfortunately, It keeps returning -1 and errno is 13 which means Permission Denied.

Some experienced developers(Thank you here) mentioned about that the error happens because this application doesnt have enough permission.

It "SHOULD NOT" be something like

Process process = Runtime.getRuntime().exec("su");

I think this means you possess a system permission but your application doesnt. (Let me know if I am wrong)

Is there any way to grant the app for having the root privilege?

Any reply/clue/hint will be highly appreciated.

Regards-

Sam

Это было полезно?

Решение

I think i can help you with this.

Here we gooo...

void gainRoot()
{
    Process chperm;
    try {
        chperm=Runtime.getRuntime().exec("su");
          DataOutputStream os = 
              new DataOutputStream(chperm.getOutputStream());
            os.writeBytes("chmod 777 /dev/video0\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();
    }
}

This is a function i normally use to change the permissions of root files.

When you do su, a new process will be created with root privileges. So using this Process you can do any root operations. There is some specific syntax that needs to be followed. Which is shown in the above example.

so one thing what you can do is build the bind part of the code as a separate executable and place it inside system/bin or at that place where normally executables are kept. Suppose the name of the executable is bind123 then instead of

            os.writeBytes("chmod 777 /dev/video0\n");
            os.flush();

replace it with

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

in the above code.

This should work. Probably once this is working we can try integrating it with the application without any dependency on the executable. I hope i am not confusing you. If atall some clarity is neede, let me knowww.... ALL the best...

Другие советы

You don't need root to bind to port >=1024. You only need INTERNET permission in your ApplicationManifest.xml file.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top