106
static FILE *my_win_freopen(const char *path, const char *mode, FILE *stream)
108
int handle_fd, fd= _fileno(stream);
111
DBUG_ASSERT(path && stream);
113
/* Services don't have stdout/stderr on Windows, so _fileno returns -1. */
116
if (!freopen(path, mode, stream))
122
if ((osfh= CreateFile(path, GENERIC_READ | GENERIC_WRITE,
123
FILE_SHARE_READ | FILE_SHARE_WRITE |
124
FILE_SHARE_DELETE, NULL,
125
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
126
NULL)) == INVALID_HANDLE_VALUE)
129
if ((handle_fd= _open_osfhandle((intptr_t)osfh,
130
_O_APPEND | _O_TEXT)) == -1)
136
if (_dup2(handle_fd, fd) < 0)
147
#elif defined(__FreeBSD__)
149
/* No close operation hook. */
151
static int no_close(void *cookie __attribute__((unused)))
157
A hack around a race condition in the implementation of freopen.
159
The race condition steams from the fact that the current fd of
160
the stream is closed before its number is used to duplicate the
161
new file descriptor. This defeats the desired atomicity of the
162
close and duplicate of dup2().
164
See PR number 79887 for reference:
165
http://www.freebsd.org/cgi/query-pr.cgi?pr=79887
168
static FILE *my_freebsd_freopen(const char *path, const char *mode, FILE *stream)
175
old_fd= fileno(stream);
177
/* Use a no operation close hook to avoid having the fd closed. */
178
stream->_close= no_close;
180
/* Relies on the implicit dup2 to close old_fd. */
181
result= freopen(path, mode, stream);
183
/* If successful, the _close hook was replaced. */
197
Change the file associated with a file stream.
199
@param path Path to file.
200
@param mode Mode of the stream.
201
@param stream File stream.
204
This function is used to redirect stdout and stderr to a file and
205
subsequently to close and reopen that file for log rotation.
207
@retval A FILE pointer on success. Otherwise, NULL.
210
FILE *my_freopen(const char *path, const char *mode, FILE *stream)
215
result= my_win_freopen(path, mode, stream);
216
#elif defined(__FreeBSD__)
218
XXX: Once the fix is ported to the stable releases, this should
219
be dependent upon the specific FreeBSD versions. Check at:
220
http://www.freebsd.org/cgi/query-pr.cgi?pr=79887
222
if (getosreldate() > 900027)
223
result= freopen(path, mode, stream);
225
result= my_freebsd_freopen(path, mode, stream);
227
result= freopen(path, mode, stream);
102
235
int my_fclose(FILE *fd, myf MyFlags)