[英]write: Invalid argument
I am writing a native executable binary in C, which will write the android device node: /sys/class/leds/lcd-brightness/brightness
.我正在用 C 编写本机可执行二进制文件,它将编写 android 设备节点: /sys/class/leds/lcd-brightness/brightness
。 push it to /system/bin
and run as root.将其推送到/system/bin
并以 root 身份运行。
However, system call write
will return error message: "write: Invalid argument"但是,系统调用write
会返回错误信息:“write: Invalid argument”
The code is as below:代码如下:
// set background brightness
int bfd = open("/sys/class/leds/lcd-backlight/brightness", O_WRONLY);
if(bfd < 0 ) {
perror("open");
exit(EXIT_FAILURE);
}
printf("bfd: %d\n", bfd);
printf("*p: %d\n", (int)*p);
if(write(bfd, p, 1) < 0) {
free(p);
perror("write");
exit(EXIT_FAILURE);
}
close(bfd);
close(fd);
p is a pointer to the data, I print *p before write, and it's all right. p是指向数据的指针,我在写之前打印*p,就可以了。
/sys/class/leds/lcd-brightness/brightness
has the following file permission: /sys/class/leds/lcd-brightness/brightness
具有以下文件权限:
-rw-rw-rw 1 system system 4096 2018-04-18 18:46 brightness
And I can override this file successfully with following command in adb shell as root我可以在 adb shell 中以 root 身份使用以下命令成功覆盖此文件
echo 100 > /sys/class/leds/lcd-backlight/brightness
What's wrong with my code ?我的代码有什么问题? I have struggled with this issue for hours.我在这个问题上挣扎了几个小时。 Any suggestion is appreciated.任何建议表示赞赏。
Thank you谢谢
Your problem seems to be permission that's why You need to set flag for writing file您的问题似乎是权限,这就是为什么您需要设置写入文件的标志
Below are the diffrent flags you have to set.以下是您必须设置的不同标志。
Try this Code试试这个代码
int bfd = open("/sys/class/leds/lcd-backlight/brightness", O_WRONLY | O_APPEND | S_IWUSR |S_IXUSR |S_IXOTH);
S_IRUSR
Set read rights for the owner to true. S_IRUSR
将所有者的读取权限设置为 true。S_IWUSR
Set write rights for the owner to true. S_IWUSR
将所有者的写权限设置为 true。S_IXUSR
Set execution rights for the owner to true. S_IXUSR
将所有者的执行权限设置为 true。S_IRGRP
Set read rights for the group to true. S_IRGRP
将组的读取权限设置为 true。S_IWGRP
Set write rights for the group to true. S_IWGRP
将组的写权限设置为 true。S_IXGRP
Set execution rights for the group to true. S_IXGRP
将组的执行权限设置为 true。S_IROTH
Set read rights for other users to true. S_IROTH
将其他用户的读取权限设置为 true。S_IWOTH
Set write rights for other users to true. S_IWOTH
将其他用户的写入权限设置为 true。S_IXOTH
Set execution rights for other users to true. S_IXOTH
将其他用户的执行权限设置为 true。Hope This helps希望这有帮助
Sysfs attributes expect data in a string format, ie, an array of char null-terminated. Sysfs 属性需要字符串格式的数据,即以空字符结尾的字符数组。 From your code, it seems that you are passing to write a pointer to an integer:从您的代码中,您似乎正在传递一个指向整数的指针:
printf("*p: %d\\n", (int)*p);
Try something like:尝试类似:
int ret;
int bfd;
char p_str[24];
int bfd = open("/sys/class/leds/lcd-backlight/brightness", O_WRONLY);
if (bfd < 0) {
/* handle the error */
}
snprintf(p_str, sizeof p_str, "%i", p);
ret = write(bfd, p_str, sizeof(p_str));
if (ret < 0) {
/* handle the error */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.