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

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

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

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

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


setconsole.c
[cpp] view plaincopy print?
  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. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <sys/ioctl.h>
  26. int main(int argc, char **argv)
  27. {
  28. char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */
  29. if (argc==2) bytes[1] = atoi(argv[1]); /* the chosen console */
  30. else {
  31. fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
  32. }
  33. if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) {    /* use stdin */
  34. fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",
  35. argv[0], strerror(errno));
  36. exit(1);
  37. }
  38. exit(0);
  39. }

但是在虚拟控制台之间测试一直不成功,很纠结~

我用*某*搜索引擎检索了很多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做了一点改进

[cpp] view plaincopy print?
  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. #include <fcntl.h>        // for open()       <--- added
  22. #include <stdio.h>        // for fprintf()
  23. #include <errno.h>        // for errno
  24. #include <stdlib.h>       // for exit()
  25. #include <unistd.h>       // for STDIN_FILENO
  26. #include <string.h>       // for strerror()
  27. #include <sys/ioctl.h>        // for ioctl()
  28. #include <asm/ioctls.h>       // for TIOCLINUX
  29. int main( int argc, char **argv )
  30. {
  31. char    bytes[ 2 ] = { 11, 0 }; // 11 is the TIOCLINUX command-number
  32. if ( argc == 2 ) bytes[1] = atoi( argv[1] );    // console id-number
  33. else    {
  34. fprintf( stderr, "%s: need a single argument\n", argv[0] );
  35. exit(1);
  36. }
  37. int fd = open( "/dev/console", O_RDWR );        // <--- added
  38. if ( fd < 0 ) { perror( "/dev/console" ); exit(1); } // <--- added
  39. if ( ioctl( fd, TIOCLINUX, bytes ) < 0 )     // <--- changed
  40. {
  41. fprintf( stderr, "%s: ioctl( fd, TIOCLINUX ): %s\n", // <---
  42. argv[0], strerror( errno ) );
  43. exit(1);
  44. }
  45. exit(0);
  46. }

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

关于LDD3 setconsole.c Alesssandro Rubini 的邮件回复相关推荐

  1. 解密:Gmail移动客户端自动邮件回复技术

    雷锋网(公众号:雷锋网)按:本文作者NLP日月星辰,北京航空航天大学在读博士生,研究方向为 自然语言处理,深度学习. 深度学习已经在许多工业产品中取得应用.例如Jeff Dean(谷歌大脑负责人)在去 ...

  2. 工作中邮件回复10大注意事项

    背景: 工作中经常需要回复邮件,但邮件回复的一些细节却没人注意,尤其初入职场的小白,简单整理了一下,感觉不管是什么岗位都能用得着: 1.邮件内容最好不要超出一屏: (这样是为了容易看,一眼就能看完所有 ...

  3. Valine - 自定义邮件回复提示

    前言 前面一篇文章主要写了: Valine - 一款快速.简洁且高效的无后端评论系统 在Hexo(sakura)主题中引入valine评论系统以及魔改diy样式! 插曲:根据cndrew的提醒,这里修 ...

  4. WordPress知更鸟(Begin)主题评论邮件回复失效的解决办法

    小任还不容易搞定了腾讯云这个坑人的安全组,实现了smtp的方式发邮件,具体文章:WordPress腾讯云服务器SMTP不能发邮件的解决办法,但是经过了测试发现,这个对于评论邮件回复居然没有用,但是有新 ...

  5. 外企邮件回复模板_电子邮件回复模板

    外企邮件回复模板 If you frequently type the same responses to email questions, you can use an email response ...

  6. 关于《强化狼群等级制度的灰狼优化算法》的问题邮件回复

    声明: 自己当初在看到该文章的时候就抱着试试的态度,给作者发了 邮件,不过 会回复的那么快,而且每个问题都做说明,很是惊喜,还开心,之前就一直收藏在邮箱里,这两天重新翻阅,越发觉得应该贴出来.再次谢谢 ...

  7. 关于MetaAPI问题CSDN给的邮件回复

    在设置office发送博文功能时,发现csdn的MetaAPI功能不能用了. 随手给csdn的服务邮箱发一封邮件,没想到csdn给回复了态度还很诚恳,赞一个! -------------------- ...

  8. 微软 Exchange 服务器被滥用于内部邮件回复链攻击

     聚焦源代码安全,网罗国内外最新资讯! 编译:代码卫士 摘要 威胁行动者正在利用 ProxyShell 和 ProxyLogon exploit 分发恶意软件并利用被盗的内部回复链邮件绕过检测. 分发 ...

  9. php邮件回复功能,用PHP回复邮件(Laravel Mailgun)

    我有一个使用电子邮件创建记录的应用程序 . 用户向某个地址发送电子邮件(例如:create@example.com),Mailgun获取邮件,解析邮件并向我的应用程序中的某个脚本发送POST请求(PO ...

最新文章

  1. React 万能的函数表达式
  2. Express中间件工作原理介绍
  3. [silverlight]WCF和Web Service(ASMX)的soap header传输方法
  4. 支持向量机器—SMO算法
  5. windows2003 apache php mysql_Windows 2003搭建Apache PHP MySQL环境经验分享
  6. 阿里P8成长路线!我的头条面试经历分享,吊打面试官系列!
  7. (JAVA)Map集合
  8. html5硬件接口,HTML5通用接口详解
  9. python获取某天的属于第几周等时间问题
  10. 惊 腾讯云、阿里云服务器无需备案配置域名访问方法
  11. Spring复习——B站
  12. python 操作word教程_Python 使用 win32com 模块对word文件进行操作
  13. JavaSE——IO流
  14. 传统民俗闹新春 浙江千年古村飘“非遗年味”
  15. 如何找大量微信群?有没有找微信群的小程序?
  16. 量子逻辑电路的初步探索
  17. 计算机怎么让两个用户独立,两个显示屏如何用同一个电脑_一台主机两个显示器独立工作-win7之家...
  18. Ubuntu 20.04修改ip地址(Netplan)
  19. emd_visu函数_visu
  20. 社区项目分享 | 用 Jina 搭建一个电影推荐系统

热门文章

  1. 皮一皮:这才是书法的最高境界...
  2. 皮一皮:这是。。。养了个白眼狼???
  3. 每日一皮:这就是成都马拉松???...
  4. Mybatis 强大的结果映射器ResultMap
  5. 找条朋友圈手都刷断了,原来是没有掌握正确的使用姿势...
  6. 死磕Java并发:J.U.C之重入锁:ReentrantLock
  7. 一文让你秒懂AQS,附带源码剖析!
  8. 注册界面翻译_B站UP主自制的开源OCR翻译器走红Github,用一次就粉了
  9. 【SpringCloud】Feigin-实例
  10. module ‘open3d‘ has no attribute ‘PointCloud‘