News for hackers. Stuff that matters.
Re:Redirecting Stdio Is Hard by kyberias on Monday December 22, 2014 @12:48PM (#8784102)
This reminds me of a bug I was hunting years ago in an obscure web server software written in C that was ported from Unix (Solaris perhaps) to Windows. It implemented connections to backend-systems and databases using sockets. Sometimes we got random file errors out of nowhere on Windows but not on Unix.

Turns out BSD sockets are closed with close() just like file descriptors but winsock requires applications to use closesocket(). The guy that ported this to Windows didn't know this and used close() on sockets leaving them dangling and sometimes closing random files on specific circumstances.

Re:Redirecting Stdio Is Hard by cesarb on Monday December 22, 2014 @01:35PM (#8784341)
Dealing with stdin/stdout/stderr is much harder on Windows than on Unix.

On Unix, you have the stdio layer (FILE *) and the file descriptor layer. On Windows, you have multiple stdio layers (one for each C runtime you have loaded in your process), multiple file descriptor layers (again, one for each C runtime), and the native "standard handles" layer (GetStdHandle/SetStdHandle).

On Unix, to redirect stdout is just a dup2() call, perhaps preceded by a fflush. But on Windows, there's no handle equivalent of dup2() (DuplicateHandle can duplicate a handle, but it will give you a new handle; you cannot use it to replace an existing handle).

So you have to either use SetStdHandle, which won't work well on multithreaded programs (see the comments for the blog post at http://blogs.msdn.com/b/oldnewthing/archive/2013/03/07/10399...), or call a dup2() equivalent on each C runtime the process is using, which won't work if (as in the second bug report in this article) another component is directly using the underlying handle.

Re:Redirecting Stdio Is Hard by SCHiM on Monday December 22, 2014 @01:40PM (#8784369)
On windows you use DuplicateHandle() on pipes made by CreatePipe(). You can then pass these handles to, for example, CreateProcess() (which does what the name implies). Now when the child process writes to a handle (stdout), the parent process can then read from this handle as if it were stdin.
Re:Redirecting Stdio Is Hard by dnr on Monday December 22, 2014 @02:41PM (#8784714)
These problems would basically all go away if you used a process monitor to handle setting up the environment for the server process, including fds 1 and 2, and then just let it log to stdout or stderr. Any halfway-decent process monitor can do this, and most of them can handle log rotation as well.
Re:Redirecting Stdio Is Hard by mapcars on Monday December 22, 2014 @03:53PM (#8785155)
So, what about modern OSes, like plan9?
Re:Redirecting Stdio Is Hard by notacoward on Monday December 22, 2014 @12:23PM (#8783976)
tl;dr Windows and Java both have crappy file-descriptor handling.
Re:Redirecting Stdio Is Hard by comex on Monday December 22, 2014 @02:00PM (#8784482)
And Unix, when it comes to O_CLOEXEC stuff. Everybody loses!
Re:Redirecting Stdio Is Hard by rollypollybear on Monday December 22, 2014 @07:38PM (#8786181)
Interesting article...I wonder if the Java problem could have been solved more succinctly?