linux重定向串口打印到telnet ssh远程中断

如果要实时显示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

猜你喜欢

转载自www.cnblogs.com/ChenChangXiong/p/11357458.html