源码:
log.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int tty = -1;
char *tty_name = NULL;
if(argc < 2)
{
printf("miss argument\n");
return 0;
}
tty_name = ttyname(STDOUT_FILENO);
printf("tty_name: %s\n", tty_name);
if(!strcmp(argv[1], "on"))
{
tty = open(tty_name, O_RDONLY | O_WRONLY);
ioctl(tty, TIOCCONS);
perror("ioctl TIOCCONS");
}
else if(!strcmp(argv[1], "off"))
{
tty = open("/dev/console", O_RDONLY | O_WRONLY);
ioctl(tty, TIOCCONS);
perror("ioctl TIOCCONS");
}
else
{
printf("error argument\n");
return 0;
}
close(tty);
return 0;
}
编译:gcc log.c -o log
应用:telnet 运行 ./log on即可看到实时打印
telnet后,执行命令:setconsole /dev/pts/0(tty显示终端)也有同样的功能
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)