summaryrefslogtreecommitdiff
path: root/mysys/my_fopen.c
diff options
context:
space:
mode:
authorMichael Widenius <monty@askmonty.org>2011-02-20 18:51:43 +0200
committerMichael Widenius <monty@askmonty.org>2011-02-20 18:51:43 +0200
commit58bb0769bdf13a9747e900aa2f0955137738ce9d (patch)
tree1b46ea0f72dce27532f0c87c60ba8a1baa453fa0 /mysys/my_fopen.c
parent7e497abcfb3e761ba5a368316192ae930fb58f6b (diff)
parentde3c4428b8c759e85631d8d70b5845c872de5400 (diff)
downloadmariadb-git-58bb0769bdf13a9747e900aa2f0955137738ce9d.tar.gz
Merge with MySQL 5.1.55
- Fixed some issues with partitions and connection_string, which also fixed lp:716890 "Pre- and post-recovery crash in Aria" - Fixed wrong assert in Aria Now need to merge with latest xtradb before pushing sql/ha_partition.cc: Ensure that m_ordered_rec_buffer is not freed before close. sql/mysqld.cc: Changed to use opt_stack_trace instead of opt_pstack. Removed references to pstack sql/partition_element.h: Ensure that connect_string is initialized storage/maria/ma_key_recover.c: Fixed wrong assert
Diffstat (limited to 'mysys/my_fopen.c')
-rw-r--r--mysys/my_fopen.c138
1 files changed, 136 insertions, 2 deletions
diff --git a/mysys/my_fopen.c b/mysys/my_fopen.c
index 351851cca76..a4b0c9f895d 100644
--- a/mysys/my_fopen.c
+++ b/mysys/my_fopen.c
@@ -1,4 +1,5 @@
-/* Copyright (C) 2000 MySQL AB
+/* Copyright (c) 2000, 2011, Oracle and/or its affiliates.
+ Copyright (c) 1985-2011 Monty Program Ab
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -18,6 +19,10 @@
#include <errno.h>
#include "mysys_err.h"
+#if defined(__FreeBSD__)
+extern int getosreldate(void);
+#endif
+
static void make_ftype(char * to,int flag);
/*
@@ -97,8 +102,137 @@ FILE *my_fopen(const char *filename, int flags, myf MyFlags)
} /* my_fopen */
- /* Close a stream */
+#if defined(_WIN32)
+
+static FILE *my_win_freopen(const char *path, const char *mode, FILE *stream)
+{
+ int handle_fd, fd= _fileno(stream);
+ HANDLE osfh;
+
+ DBUG_ASSERT(path && stream);
+
+ /* Services don't have stdout/stderr on Windows, so _fileno returns -1. */
+ if (fd < 0)
+ {
+ if (!freopen(path, mode, stream))
+ return NULL;
+
+ fd= _fileno(stream);
+ }
+
+ if ((osfh= CreateFile(path, GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE |
+ FILE_SHARE_DELETE, NULL,
+ OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
+ NULL)) == INVALID_HANDLE_VALUE)
+ return NULL;
+
+ if ((handle_fd= _open_osfhandle((intptr_t)osfh,
+ _O_APPEND | _O_TEXT)) == -1)
+ {
+ CloseHandle(osfh);
+ return NULL;
+ }
+
+ if (_dup2(handle_fd, fd) < 0)
+ {
+ CloseHandle(osfh);
+ return NULL;
+ }
+
+ _close(handle_fd);
+
+ return stream;
+}
+
+#elif defined(__FreeBSD__)
+
+/* No close operation hook. */
+
+static int no_close(void *cookie __attribute__((unused)))
+{
+ return 0;
+}
+
+/*
+ A hack around a race condition in the implementation of freopen.
+
+ The race condition steams from the fact that the current fd of
+ the stream is closed before its number is used to duplicate the
+ new file descriptor. This defeats the desired atomicity of the
+ close and duplicate of dup2().
+
+ See PR number 79887 for reference:
+ http://www.freebsd.org/cgi/query-pr.cgi?pr=79887
+*/
+
+static FILE *my_freebsd_freopen(const char *path, const char *mode, FILE *stream)
+{
+ int old_fd;
+ FILE *result;
+
+ flockfile(stream);
+
+ old_fd= fileno(stream);
+
+ /* Use a no operation close hook to avoid having the fd closed. */
+ stream->_close= no_close;
+
+ /* Relies on the implicit dup2 to close old_fd. */
+ result= freopen(path, mode, stream);
+
+ /* If successful, the _close hook was replaced. */
+
+ if (result == NULL)
+ close(old_fd);
+ else
+ funlockfile(result);
+
+ return result;
+}
+
+#endif
+
+
+/**
+ Change the file associated with a file stream.
+
+ @param path Path to file.
+ @param mode Mode of the stream.
+ @param stream File stream.
+
+ @note
+ This function is used to redirect stdout and stderr to a file and
+ subsequently to close and reopen that file for log rotation.
+
+ @retval A FILE pointer on success. Otherwise, NULL.
+*/
+
+FILE *my_freopen(const char *path, const char *mode, FILE *stream)
+{
+ FILE *result;
+
+#if defined(_WIN32)
+ result= my_win_freopen(path, mode, stream);
+#elif defined(__FreeBSD__)
+ /*
+ XXX: Once the fix is ported to the stable releases, this should
+ be dependent upon the specific FreeBSD versions. Check at:
+ http://www.freebsd.org/cgi/query-pr.cgi?pr=79887
+ */
+ if (getosreldate() > 900027)
+ result= freopen(path, mode, stream);
+ else
+ result= my_freebsd_freopen(path, mode, stream);
+#else
+ result= freopen(path, mode, stream);
+#endif
+
+ return result;
+}
+
+/* Close a stream */
int my_fclose(FILE *fd, myf MyFlags)
{
int err,file;