Jump to content

usage of ioctl command TIOCLINUX

gururaj.g's Photo
Posted Dec 12 2012 11:30 PM
1482 Views

hi I have been reading the Linux Device Drivers 3 book.

In Chapter 4 on debugging an example program for changing console for printk was given.
but it is not executing in my terminal what could be the reason? I have given the source code also.
Also when I googled for the TIOCLINUX command I was able find only 10 subcommands for it. what is the subcommand 11 doing in the code? Please direct me to any article or links or guide me.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */

if (argc==2) bytes[1] = atoi(argv[1]); /* the chosen console */
else {
fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
}
if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) { /* use stdin */
fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",
argv[0], strerror(errno));
exit(1);
}
exit(0);
}

Thank you

1 Reply

0
  ChrisOlson's Photo
Posted Dec 13 2012 04:47 PM

Hi,

I passed your message on to the authors, and one of them replied with the following message.

I hope that helps,
Chris Olson
O'Reilly Book Support

------
> In Chapter 4 on debugging an example program for changing console
> for printk was given. but it is not executing in my terminal what
> could be the reason?

It must be executed in a linux terminal (text login on vga, or
equivalent on non-pc platforms). I'm now running the "setconsole"
program from our misc-progs directory.

From a graphical terminal, it doesn't work:

ostro% env | grep TERM=
TERM=xterm
ostro% ./setconsole 2
./setconsole: ioctl(stdin, TIOCLINUX): Invalid argument

And this is from a text login (control-alt-F2) in the same host:

ostro% env | grep TERM=
TERM=linux
ostro% ./setconsole 2
./setconsole: ioctl(stdin, TIOCLINUX): Operation not permitted
ostro% sudo ./setconsole 2
ostro%

> Also when I googled for the TIOCLINUX command I was able find only
> 10 subcommands for it. what is the subcommand 11 doing in the code?
> Please direct me to any article or links or guide me.

You shouldn't google when you have the source code. In recent kernels
the implementation is the function tioclinux() in
./drivers/tty/vt/vt.c (drivers/tty/vt/vt_ioctl.c implements ioctl and
calls it). The implementation now uses symbolic names rather than
numbers, and 11 is TIOCL_SETKMSGREDIRECT (defined in include/linux/tiocl.h):

#define TIOCL_SETKMSGREDIRECT 11 /* restrict kernel messages to a vt */

And it works as advertised.

Hope this helps
/alessandro