關於 "setconsole.c" Alesssandro Rubini 的郵件回覆

轉自 http://blog.csdn.net/cinmyheart/article/details/38960967

關於 "setconsole.c"  Alesssandro Rubini 的郵件回覆


                   抱着試一試的心態給偶像寫了一封求助e-mail,回覆了!!!我的小心臟啊~


呵呵~Rubini是誰不解釋了...


我的HELP原文主要的關於setconsole.c的問題,這個是LDD3的一個小程序


setconsole.c
  1. /* 
  2.  * setconsole.c -- choose a console to receive kernel messages 
  3.  * 
  4.  * Copyright (C) 1998,2000,2001 Alessandro Rubini 
  5.  *  
  6.  *   This program is free software; you can redistribute it and/or modify 
  7.  *   it under the terms of the GNU General Public License as published by 
  8.  *   the Free Software Foundation; either version 2 of the License, or 
  9.  *   (at your option) any later version. 
  10.  * 
  11.  *   This program is distributed in the hope that it will be useful, 
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14.  *   GNU General Public License for more details. 
  15.  * 
  16.  *   You should have received a copy of the GNU General Public License 
  17.  *   along with this program; if not, write to the Free Software 
  18.  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
  19.  */  
  20.   
  21. #include <stdio.h>  
  22. #include <stdlib.h>  
  23. #include <string.h>  
  24. #include <errno.h>  
  25. #include <unistd.h>  
  26. #include <sys/ioctl.h>  
  27.   
  28. int main(int argc, char **argv)  
  29. {  
  30.     char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */  
  31.   
  32.     if (argc==2) bytes[1] = atoi(argv[1]); /* the chosen console */                                                                                                 
  33.     else {  
  34.         fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);  
  35.     }  
  36.     if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) {    /* use stdin */  
  37.         fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",  
  38.                 argv[0], strerror(errno));  
  39.         exit(1);  
  40.     }  
  41.     exit(0);  
  42. }  

但是在虛擬控制檯之間測試一直不成功,很糾結~
我用*某*搜索引擎檢索了很多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做了一點改進


  1. //----------------------------------------------------------------  
  2. //  setconsole.cpp  
  3. //  
  4. //  This utility allows a user possessing root privilege   
  5. //  to redirect 'printk' output to a designated console.   
  6. //  
  7. //  compile using:  
  8. //      root# gcc -o setconsole setconsole.cpp  
  9. //      root# chmod a+s setconsole  
  10. //  
  11. //  execute using:  
  12. //      user$ setconsole 4    
  13. //        
  14. //  Code used here is from an example by Alesandro Rubini,    
  15. //  "Linux Device Drivers (2nd Edition)," pages 99-100.  
  16. //              
  17. //  programmer: ALLAN CRUSE  
  18. //  written on: 24 NOV 2002  
  19. //  revised on: 24 JUN 2007 -- to use "/dev/console" device  
  20. //----------------------------------------------------------------  
  21.   
  22. #include <fcntl.h>        // for open()       <--- added   
  23. #include <stdio.h>        // for fprintf()  
  24. #include <errno.h>        // for errno  
  25. #include <stdlib.h>       // for exit()  
  26. #include <unistd.h>       // for STDIN_FILENO  
  27. #include <string.h>       // for strerror()  
  28. #include <sys/ioctl.h>        // for ioctl()  
  29. #include <asm/ioctls.h>       // for TIOCLINUX  
  30.   
  31. int main( int argc, char **argv )  
  32. {     
  33.     char    bytes[ 2 ] = { 11, 0 }; // 11 is the TIOCLINUX command-number  
  34.   
  35.     if ( argc == 2 ) bytes[1] = atoi( argv[1] );    // console id-number  
  36.     else    {  
  37.         fprintf( stderr, "%s: need a single argument\n", argv[0] );  
  38.         exit(1);  
  39.         }  
  40.   
  41.     int fd = open( "/dev/console", O_RDWR );        // <--- added  
  42.     if ( fd < 0 ) { perror( "/dev/console" ); exit(1); } // <--- added  
  43.       
  44.     if ( ioctl( fd, TIOCLINUX, bytes ) < 0 )     // <--- changed  
  45.         {     
  46.         fprintf( stderr, "%s: ioctl( fd, TIOCLINUX ): %s\n"// <---   
  47.                         argv[0], strerror( errno ) );  
  48.         exit(1);  
  49.         }  
  50.   
  51.     exit(0);  
  52. }  


是可以在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






發佈了97 篇原創文章 · 獲贊 90 · 訪問量 129萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章