设为首页 加入收藏

TOP

关于(LDD3) "setconsole.c" Alesssandro Rubini 的邮件回复
2015-07-20 17:46:44 来源: 作者: 【 】 浏览:1
Tags:关于 LDD3 " setconsole.c" Alesssandro Rubini 邮件 回复

关于 "setconsole.c" Alesssandro Rubini 的邮件回复


抱着试一试的心态给偶像写了一封求助e-mail,回复了!!!我的小心脏啊~


呵呵~Rubini是谁不解释了...


我的HELP原文主要的关于setconsole.c的问题,这个是LDD3的一个小程序


setconsole.c
/*
 * setconsole.c -- choose a console to receive kernel messages
 *
 * Copyright (C) 1998,2000,2001 Alessandro Rubini
 * 
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         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); }
       
      
     
    
   
  

但是在虚拟控制台之间测试一直不成功,很纠结~ 我用*某*搜索引擎检索了很多blog,都是水...都是直接copy原书的内容,仅发现一个有对程序进行测试的,但是测试没有成功,google不能用,不知道情况如果,用TTT检索出来的一样,没有实质性的对setconsole.c的讨论和测试!

下面是Rubini 对我的回复:

\

\

<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cD48YnI+CjwvcD4KPHA+ytfPyNa4s/bBy87StcTO88f4PC9wPgo8cD4xLiBpb2N0bChUSU9DTElOVVgpINOwz+y1xLKisrvKx3ByaW50ZqOstvjKx3ByaW50a6OhPC9wPgo8cD4yLnR0eSC78tXfIHRlcm1pbmFsILK7ysdjb25zb2xlISAgIM2ouf1jdHJsICYjNDM7IGFsdCAmIzQzOyBGKiAoMX42ICm077W9tcS2vMrHv9jWxsyoo6xGN8rHzbzQzru3vrOjrLK7ysdjb25zb2xlo6E8L3A+CjxwPjxicj4KPC9wPgo8cD48L3A+CjxwPkFsbGFuIENydXNlINTaMjAwN8TqttRzZXRjb25zb2xl1/bBy9K7teO4xL34PC9wPgo8YnI+CjxwPjwvcD4KPHByZSBjbGFzcz0="brush:java;">//---------------------------------------------------------------- // setconsole.cpp // // This utility allows a user possessing root privilege // to redirect 'printk' output to a designated console. // // compile using: // root# gcc -o setconsole setconsole.cpp // root# chmod a+s setconsole // // execute using: // user$ setconsole 4 // // Code used here is from an example by Alesandro Rubini, // "Linux Device Drivers (2nd Edition)," pages 99-100. // // programmer: ALLAN CRUSE // written on: 24 NOV 2002 // revised on: 24 JUN 2007 -- to use "/dev/console" device //---------------------------------------------------------------- #include // for open() <--- added #include // for fprintf() #include // for errno #include // for exit() #include // for STDIN_FILENO #include // for strerror() #include // for ioctl() #include // for TIOCLINUX int main( int argc, char **argv ) { char bytes[ 2 ] = { 11, 0 }; // 11 is the TIOCLINUX command-number if ( argc == 2 ) bytes[1] = atoi( argv[1] ); // console id-number else { fprintf( stderr, "%s: need a single argument\n", argv[0] ); exit(1); } int fd = open( "/dev/console", O_RDWR ); // <--- added if ( fd < 0 ) { perror( "/dev/console" ); exit(1); } // <--- added if ( ioctl( fd, TIOCLINUX, bytes ) < 0 ) // <--- changed { 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






】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇NYOJ-比大小 下一篇C++设计模式之外观模式(一)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)