httpd-apreq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "William A. Rowe, Jr." <>
Subject Re: HANDLE from FILE on windows
Date Tue, 31 Dec 2002 17:23:50 GMT
They are just stating the obvious (true on Unix, too) that buffering and
other behaviors may exist in the FILE * (or the fd) that writes to the raw
handle may overwrite or be overwritten by buffered data, and buffered
data won't show up on a read against the handle.  Nothing you didn't
already expect.

Here's the code from httpd-2.0/server/mpm/winnt/nt_eventlog.c (rev 1.3)
that does exactly what MS says not to...

/* Flush stderr and unset its buffer, then commit and replace stderr.
 * This is typically a noop for Win2K/XP since services with NULL std
 * handles [but valid FILE *'s, oddly enough], but is required
 * for NT 4.0 and to use this code outside of services.
setvbuf(stderr, NULL, _IONBF, 0); 
_commit(2 /* stderr */); 
fd = _open_osfhandle((long) hPipeWrite, _O_WRONLY | _O_BINARY); 
_dup2(fd, 2); 
_setmode(2, _O_BINARY); 
/* hPipeWrite was _close()'ed above, and _dup2()'ed
 * to fd 2 creating a new, inherited Win32 handle.
 * Recover that real handle from fd 2.
hPipeWrite = (HANDLE)_get_osfhandle(2); SetStdHandle(STD_ERROR_HANDLE, hPipeWrite); 

Notice; we flush and remove the buffering from stderr altogether, and then
commit the fd before we even think of touching the handle.  So should you.

One more thing the above code hints at ... you will be looking at a raw
file (all win32 HANDLEs are raw) so you will have to deal with \r\n line
endings yourself, as applicable.


At 04:47 PM 12/30/2002, you wrote:
>"William A. Rowe, Jr." <> writes:
>> First off... with Apache 2.0 all of these problems go away (we told
>> you there was a reason to upgrade :-) The apr_os_file_get() fn will
>> turn any apr_file_t into a win32 handle, or a Unix fd, or whatnot on
>> another platform.
>We'll get to it when we get to it.  Meanwhile... I want to try and
>stabilize things on windows as much as possible.  If you want to help
>(more than you have already by providing lots of useful info), more
>committers are welcome;-)
>We're also waiting on apreq to be completely ok for 2.0.
>> IIRC, you are doing the 1.3 thing.  That reference is correct for
>> turning the fd into a HANDLE, but that isn't what you wanted.  The
>> full syntax, as demonstrated in
>> apache-1.3/src/main/http_main.c(6719) is;
>>   FILE *file;
>>   HANDLE hFile;
>>   hFile = (HANDLE)_get_osfhandle(fileno(file));
>> Yes, the cast is needed because _get_osfhandle is defined to return
>> a long, iirc.  Best of luck.
>Sure, but it says to not actually use it for anything...  Should we
>disregard that?
>David N. Welton
>   Consulting:
>     Personal:
>Free Software:
>   Apache Tcl:

View raw message