How to invoke ioctl in shell script?

Hung-Te Lin picture Hung-Te Lin · Nov 10, 2011 · Viewed 24.7k times · Source

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

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);".

Answer

Jérôme Pouiller picture Jérôme Pouiller · Dec 19, 2015

I wrote ioctl tool exactly for this purpose: 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?

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

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. ioctl will read/write buffer content from/to standard input/output. ioctl try to guess buffer size and direction from ioctl number.

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

ioctl /dev/video0 VIDIOC_QUERYCAP > video_caps