在Android中以编程方式更改tty端口的权限和所有权 [英] Change the permissions and ownership of tty port in android programmatically

查看:266
本文介绍了在Android中以编程方式更改tty端口的权限和所有权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在该端口上写入数据时,我需要更改/ dev / tty *文件的权限。我可以通过以root用户身份进入adb shell来从Windows命令提示符处更改权限。

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.

我尝试通过使用 Runtime.exec()方法从应用程序运行adb命令,但这不会像应用程序那样更改任何内容没有权限来更改文件所有权权限。

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();

当执行 su命令时,错误为 su: uid 10044不允许su

因此,在执行chown或chmod时出现错误无法进行chmod操作不允许

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 !

谢谢!!

推荐答案

默认值(来自 Google AOSP源 su 与它一起运行的二进制检查 UID 。只有 root shell 用户被允许运行 su 。除非您安装修补后的 su 二进制文件,该二进制文件不会检查应用程序的运行状态,否则您将无法从应用程序运行 su UID

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.

adb root 命令的全部作用是在设备上执行以下命令:

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

setprop service.adb.root 1
stop adbd
start adbd

但是我很肯定你在这里问错了一个问题。 su 命令没有理由不能满足您的需求。

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.

您应执行的更改命令tty设备权限为:

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

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

也在android中,用作在 chown 命令中而不是中用户和组字段之间的分隔符。正确的命令应该是:

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

另一个常见的错误是尝试使用标准的Linux su 选项,例如 -c su 的AOSP版本要简单得多:

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.

这篇关于在Android中以编程方式更改tty端口的权限和所有权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆