轉自 http://blog.csdn.net/cinmyheart/article/details/38960967
關於 "setconsole.c" Alesssandro Rubini 的郵件回覆
抱着試一試的心態給偶像寫了一封求助e-mail,回覆了!!!我的小心臟啊~
呵呵~Rubini是誰不解釋了...
我的HELP原文主要的關於setconsole.c的問題,這個是LDD3的一個小程序
setconsole.c
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#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};
-
-
if (argc==2) bytes[1] = atoi(argv[1]);
-
else {
-
fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
-
}
-
if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) {
-
fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",
-
argv[0], strerror(errno));
-
exit(1);
-
}
-
exit(0);
-
}
但是在虛擬控制檯之間測試一直不成功,很糾結~
我用*某*搜索引擎檢索了很多blog,都是水...都是直接copy原書的內容,僅發現一個有對程序進行測試的,但是測試沒有成功,google不能用,不知道情況如果,用TTT檢索出來的一樣,沒有實質性的對setconsole.c的討論和測試!
下面是Rubini 對我的回覆:
首先指出了我的誤區
1. ioctl(TIOCLINUX) 影響的並不是printf,而是printk!
2.tty 或者 terminal 不是console! 通過ctrl + alt + F* (1~6 )達到的都是控制檯,F7是圖形環境,不是console!
Allan Cruse 在2007年對setconsole做了一點改進
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#include <fcntl.h> // for open() <--- added
-
#include <stdio.h> // for fprintf()
-
#include <errno.h> // for errno
-
#include <stdlib.h> // for exit()
-
#include <unistd.h> // for STDIN_FILENO
-
#include <string.h> // for strerror()
-
#include <sys/ioctl.h> // for ioctl()
-
#include <asm/ioctls.h> // for TIOCLINUX
-
-
int main( int argc, char **argv )
-
{
-
char bytes[ 2 ] = { 11, 0 };
-
-
if ( argc == 2 ) bytes[1] = atoi( argv[1] );
-
else {
-
fprintf( stderr, "%s: need a single argument\n", argv[0] );
-
exit(1);
-
}
-
-
int fd = open( "/dev/console", O_RDWR );
-
if ( fd < 0 ) { perror( "/dev/console" ); exit(1); }
-
-
if ( ioctl( fd, TIOCLINUX, bytes ) < 0 )
-
{
-
fprintf( stderr, "%s: ioctl( fd, TIOCLINUX ): %s\n",
-
argv[0], strerror( errno ) );
-
exit(1);
-
}
-
-
exit(0);
-
}
是可以在console之間重定向IO的!!
這幅圖看到的是tty4,我把控制檯的IO重定向到了console 3,也就是/dev/tty3
切換到tty2,我們插入hello.ko 模塊,調用printk,打印hello world
我故意執行了tty這個shell程序,提示讀者當前我們在tty2,進行insmod操作
明明是有hello world的爲什麼不打印呢?去哪兒了?console 3!
我們ctrl + alt + F3 切換到console 3看看
終於搞定了!利用ioctl實現了對於不同console之間的IO重定向!
最後謝謝偶像~ Alesssandro Rubini & Allan Cruse