简体   繁体   English

关闭telnet会话会将标准输出重定向到http套接字

[英]Closing telnet session will redirect the stdout to a http socket

I have a program that display the log output to the stdout. 我有一个程序,将日志输出显示到标准输出。

So if I open a telnet session to my target linux and then launch on this telnet session my program then I will get the log messages displayed on my telnet session. 因此,如果我打开到目标linux的telnet会话,然后在该telnet会话中启动我的程序,那么我将获得显示在telnet会话中的日志消息。

In my program I have a little http server running. 在我的程序中,我正在运行一个小的http服务器。 Now if I change the IP address of my target linux and then I restart the interface (the http server will restart automatically because I detect the change of ip address with netlink) And then I will get the telnet session closed and the stdout messages are redirected to the socket opened by my http server and I will get the printf of the log message locked. 现在,如果我更改目标linux的IP地址,然后重新启动接口(http服务器将自动重新启动,因为我通过netlink检测到ip地址的更改),然后将关闭telnet会话,并将stdout消息重定向到我的http服务器打开的套接字上,我将锁定日志消息的printf。

I tried with select to detect this lock but without success: How to use select with stdout? 我尝试使用select来检测到此锁定,但未成功: 如何在stdout中使用select?

The select return success before going to the prinf (which locks) 选择成功后,将转到主对象(已锁定)

Any suggestion to avoid this problem ? 有什么建议可以避免这个问题?

If I understand correctly, the telnet session (why aren't you using SSH??) under which the HTTP server is running becomes broken due to the change of IP address. 如果我理解正确,则由于IP地址的更改,正在运行HTTP服务器的telnet会话(为什么不使用SSH ??)会中断。

What will happen after that if the program continues to write data to this session (which is its stdout) is that, at first the writes will succeed as the system buffers up data, then eventually the writes will block (not "lock"). 之后,如果程序继续向该会话写入数据(即其stdout),则将首先在系统缓冲数据时写入成功,然后最终写入将阻塞(而不是“锁定”)。 Finally, the TCP connection will time out and the writes will return an error. 最后,TCP连接将超时并且写入将返回错误。 It may or may not take a long time for the TCP session to time out, but it eventually will. TCP会话超时可能会花费很长时间,也可能不会很长,但最终会。

You can make your log output code use non-blocking writes to stdout if you want to avoid blocking (eg if your application is event-driven and must not block). 如果要避免阻塞,可以使日志输出代码对stdout使用非阻塞写入(例如,如果您的应用程序是事件驱动的并且不能阻塞)。 You will need to use fcntl to change stdout to non-blocking and you will probably need to avoid stdio altogether because stdio is not designed to work with non-blocking output. 您将需要使用fcntl将stdout更改为非阻塞,并且您可能需要完全避免使用stdio,因为stdio并非旨在与非阻塞输出一起使用。 You must implement your own buffering and write directly to file descriptor 1. 您必须实现自己的缓冲并直接写入文件描述符1。

You also mentioned that you want to log to an HTTP connection after the stdout log becomes broken. 您还提到了在标准输出日志损坏后要登录到HTTP连接。 You could do that too (triggered once you get an error writing to stdout) but it will be a lot more work. 您也可以这样做(一旦在写入stdout时出错,就会触发该操作),但这将需要更多工作。 You will have to manage your log buffer internally in your application until an HTTP client connects and requests it. 您将必须在应用程序内部管理日志缓冲区,直到HTTP客户端连接并请求它为止。 You will also want to add a provision for discarding the log if it gets too big, in case no HTTP client connects. 如果没有太大的日志(如果没有HTTP客户端连接),您还希望添加一条丢弃日志的规定。 All of that is betyond the scope of a SO question... 所有这些都超出了SO问题的范围...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM