Skip to contentSkip to conversation

Remove setvbuf(<handle>, NULL, _IOLBF, 0) calls for Win32

PATCH In review Filip Navara 2 participants 6 messages Jul 27, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 3/5 Remove setvbuf(<handle>, NULL, _IOLBF, 0) calls for Win32

On Win32 the setvbuf function requires the last parameter to be size between 2 and INT_MAX bytes, so the calls always failed. Since the whole point of the calls is to set line-buffered mode for the file handle and that's not supported on Win32 anyway, conditionally remove them.

---
 exec.c |    3 ++-
 vl.c   |    3 +++
 2 files changed, 5 insertions(+), 1 deletions(-)
exec.c +2 -1
index 7d3eb1a..ef79d6d 100644
@@ -1491,7 +1491,8 @@ void cpu_set_log(int log_flags)
             static char logfile_buf[4096];
             setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
         }
-#else
+#elif !defined(_WIN32)
+        /* Win32 doesn't support line-buffering and requires size >= 2 */
         setvbuf(logfile, NULL, _IOLBF, 0);
 #endif
         log_append = 1;
vl.c +3 -0
index ce213c2..d4e49e4 100644
@@ -5735,7 +5735,10 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+#ifndef _WIN32
+    /* Win32 doesn't support line-buffering and requires size >= 2 */
     setvbuf(stdout, NULL, _IOLBF, 0);
+#endif
 
     init_timers();
     if (init_timer_alarm() < 0) {
Signature · 1 line
1.6.3.2.1299.gee46c
#1 lore raw
Quoted 1 line · Filip Navara

On Win32 the setvbuf function requires the last parameter to be size between 2 and INT_MAX bytes, so the calls always failed. Since the whole point of the calls is to set line-buffered mode for the file handle and that's not supported on Win32 anyway, conditionally remove them.

Should they be unbuffered where _IOLBF is not supported, then? I assume the line buffering was to make them more useful in event of a crash.

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery
#2 lore raw
Quoted 6 lines · Daniel Jacobowitz
Quoted 1 line · Filip Navara

On Win32 the setvbuf function requires the last parameter to be size between 2 and INT_MAX bytes, so the calls always failed. Since the whole point of the calls is to set line-buffered mode for the file handle and that's not supported on Win32 anyway, conditionally remove them.

Should they be unbuffered where _IOLBF is not supported, then?  I assume the line buffering was to make them more useful in event of a crash.

I guess your assumption is correct. I'd be fine with using _IONBF for the exec.c part, but setting it for stdout doesn't sound too useful to me.

Best regards,
Filip Navara

#3 lore raw
Quoted 11 lines · Filip Navara
Quoted 6 lines · Daniel Jacobowitz
Quoted 1 line · Filip Navara

On Win32 the setvbuf function requires the last parameter to be size between 2 and INT_MAX bytes, so the calls always failed. Since the whole point of the calls is to set line-buffered mode for the file handle and that's not supported on Win32 anyway, conditionally remove them.

Should they be unbuffered where _IOLBF is not supported, then?  I assume the line buffering was to make them more useful in event of a crash.

I guess your assumption is correct. I'd be fine with using _IONBF for the exec.c part, but setting it for stdout doesn't sound too useful to me.

I've found that leaving stdout buffered on Windows is a pain; on Unix "isatty()" is reliable, but on Windows it breaks down at the Cygwin/Windows interaction border. So your simulated programs end up using block output all the time if you have a mingw32 qemu in a Cygwin terminal, for instance.

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery
#4 lore raw
Quoted 17 lines · Daniel Jacobowitz
Quoted 1 line · Filip Navara

On Mon, Jul 27, 2009 at 3:21 PM, Daniel Jacobowitz<[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines · Filip Navara
Quoted 1 line

On Win32 the setvbuf function requires the last parameter to be size

between 2 and INT_MAX bytes, so the calls always failed. Since the whole point of the calls is to set line-buffered mode for the file handle and that's not supported on Win32 anyway, conditionally remove them.

Quoted 8 lines
Quoted 4 lines

Should they be unbuffered where _IOLBF is not supported, then? I assume the line buffering was to make them more useful in event of a crash.

I guess your assumption is correct. I'd be fine with using _IONBF for the exec.c part, but setting it for stdout doesn't sound too useful to me.

Quoted 5 lines

I've found that leaving stdout buffered on Windows is a pain; on Unix "isatty()" is reliable, but on Windows it breaks down at the Cygwin/Windows interaction border. So your simulated programs end up using block output all the time if you have a mingw32 qemu in a Cygwin terminal, for instance.

I use MinGW-built QEMU in regular command line and MSYS sh and I never had a single problem with the buffering. I can't speak for Cygwin, but if they can't get it right I don't think it's up to QEMU to fix it (heck, checking for a console handle is as easy as "(handle & 3) != 0").

The log files are different story, since for certain types of crashes the last block is lost. Since log files are actually used for debugging problems I acknowledge that keeping the content is important.

Best regards,
Filip Navara

#5 lore raw
Quoted 4 lines · Filip Navara

I use MinGW-built QEMU in regular command line and MSYS sh and I never had a single problem with the buffering. I can't speak for Cygwin, but if they can't get it right I don't think it's up to QEMU to fix it (heck, checking for a console handle is as easy as "(handle & 3) != 0").

It is not a Cygwin problem. It's a MSVCRT runtime problem. The runtime checks isatty to determine the buffering of stdout (this is a standard C thing to do), and Cygwin terminals do not show up as consoles.

If the underlying device is really a Windows console, of course, there's no problem. But Cygwin SSH ptys, for example, are not Windows consoles.

Signature · 2 lines
Daniel Jacobowitz
CodeSourcery