标签:tty return name 串口 telnet ioctl linux include TIOCCONS
如果要实时显示printk 信息 可以参考 https://www.cnblogs.com/ChenChangXiong/p/11357416.html
有时候调试需要 但是没有串口 使用telnet ssh远程登录的时候 不能显示启动时候运行的程序的打印 这个时候需要重定向
源码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <fcntl.h> 5 #include <sys/ioctl.h> 6 #include <unistd.h> 7 8 int main(int argc, char *argv[]) 9 { 10 int tty = -1; 11 char *tty_name = NULL; 12 13 if(argc < 2) 14 { 15 printf("miss argument\n"); 16 return 0; 17 } 18 19 /* 获取当前tty名称 */ 20 tty_name = ttyname(STDOUT_FILENO); 21 printf("tty_name: %s\n", tty_name); 22 23 if(!strcmp(argv[1], "on")) 24 { 25 /* 重定向console到当前tty */ 26 tty = open(tty_name, O_RDONLY | O_WRONLY); 27 ioctl(tty, TIOCCONS); 28 perror("ioctl TIOCCONS"); 29 } 30 else if(!strcmp(argv[1], "off")) 31 { 32 /* 恢复console */ 33 tty = open("/dev/console", O_RDONLY | O_WRONLY); 34 ioctl(tty, TIOCCONS); 35 perror("ioctl TIOCCONS"); 36 } 37 else 38 { 39 printf("error argument\n"); 40 return 0; 41 } 42 43 close(tty); 44 return 0; 45 }
参考以下: https://blog.csdn.net/lqxandroid2012/article/details/79165141
标签:tty,return,name,串口,telnet,ioctl,linux,include,TIOCCONS 来源: https://www.cnblogs.com/ChenChangXiong/p/11357458.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。