bash 如何在shell脚本中调用ioctl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8082302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:11:00  来源:igfitidea点击:

How to invoke ioctl in shell script?

bashshellioctl

提问by Hung-Te Lin

I'm trying to execute an ioctl call on a system with only bash and primitive base utilities.

我试图在只有 bash 和原始基本实用程序的系统上执行 ioctl 调用。

Is there any way to execute arbitrary ioctl command (if the params are simply integers) to a specific device file in /dev in shell script, without writing C / perl / python programs? Something like "magic_ioctl /dev/console 30 1 2" which would calls "ioctl(open("/dev/console"), 30, 1, 2);".

有没有办法在shell脚本中对/dev中的特定设备文件执行任意ioctl命令(如果参数只是整数),而无需编写C/perl/python程序?类似于“magic_ioctl /dev/console 30 1 2”,它会调用“ioctl(open("/dev/console"), 30, 1, 2);”。

回答by Jér?me Pouiller

I wrote ioctltool exactly for this purpose: https://github.com/jerome-pouiller/ioctl.

ioctl正是为此目的编写了工具:https: //github.com/jerome-pouiller/ioctl

Currently, it is not possible to pass multiple argument to ioctl call. Have you an example where it would be usefull?

目前,无法将多个参数传递给 ioctl 调用。你有一个有用的例子吗?

If you want to call ioctl(open("/dev/console"), 30, 1);, you can run:

如果你想调用ioctl(open("/dev/console"), 30, 1);,你可以运行:

ioctl /dev/console 30 -v 1

However, for most ioctl, you want to allocate a buffer and pass a pointer to this buffer in argument to ioctl call. In this case, just forget -v. ioctlwill read/write buffer content from/to standard input/output. ioctltry to guess buffer size and direction from ioctl number.

但是,对于大多数 ioctl,您希望分配一个缓冲区并将指向该缓冲区的指针作为参数传递给 ioctl 调用。在这种情况下,只需忘记-vioctl将从/向标准输入/输出读取/写入缓冲区内容。ioctl尝试从 ioctl 数字猜测缓冲区大小和方向。

The best is: ioctlunderstand many (around 2200) ioctl symbolic names. Thus you can call:

最好的是:ioctl了解许多(大约 2200 个)ioctl 符号名称。因此你可以调用:

ioctl /dev/video0 VIDIOC_QUERYCAP > video_caps

回答by Sergey Beduev

Why you reject perl/c/python solutions ? You can made this by perl one-liner like this: perl -e require "sys/ioctl.ph"; ioctl(...);

为什么你拒绝 perl/c/python 解决方案?你可以像这样通过 perl one-liner 做到这一点: perl -e require "sys/ioctl.ph"; ioctl(...);