Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the permissions and ownership of tty port in android programmatically

Tags:

android

adb

I need to change the permission of /dev/tty* file as I am writing data on that port. I am able to change the permission from the windows command prompt by entering the adb shell as root.

But when I reboot the device, the permissions are not persisted. Instead, they are set to default values.

So, every time I need to run my app after reboot, I need to change the permissions.

I tried to run the adb commands from the app by using Runtime.exec() method but that does not change anything as the app does not have permission to change the file ownership permissions.

This is how I executed the command from app ( tested it in emulator in this case) :

Process process =   Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("chown root:root /dev/ttyS0 \n");
os.writeBytes("chmod 777 /dev/ttyS0 \n");
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();

When the "su" command gets executed, it gives the error su: uid 10044 not allowed to su Because of that, on executing chown or chmod gives error Unable to chmod Operation Not permitted

So, how do I change permissions and ownership of that file ?

Any help is highly appreciated !

Thanks !!

like image 240
Siva Kumar Avatar asked Nov 13 '22 12:11

Siva Kumar


1 Answers

The default (from the Google AOSP source) su binary checks UID it is being run with. Only root and shell users are allowed to run su. You won't be able to run su from your application unless you install the patched su binary which does not check the UID.

All what adb root command does is executing the following commands on the device:

setprop service.adb.root 1
stop adbd
start adbd

But I am positive that you are asking a wrong question here. There is no reason why su command would not work for your purpose.

The command you should be executing to change the tty device permissions is:

Runtime.getRuntime().exec("su 0 chmod 777 /dev/ttyS0");

Also in android the . is used as a delimiter between user and group fields in the chown command instead of the :. The proper command would be:

su 0 chown root.root /dev/ttyS0

Another common mistake is trying to use standard Linux su options like -c. AOSP version of su is much simpler:

usage: su [UID[,GID[,GID2]...]] [COMMAND [ARG...]]

        Switch to WHO (default 'root') and run the given command (default sh).
        where WHO is a comma-separated list of user, group,
        and supplementary groups in that order.
like image 188
Alex P. Avatar answered Nov 15 '22 05:11

Alex P.