MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:vasil.dimov Date:December 8 2010 12:29pm
Subject:bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666) Bug#51023
View as plain text  
#At file:///bzrroot/server/mysql-5.1-innodb/ based on revid:vasil.dimov@stripped

 3666 Vasil Dimov	2010-12-08
      Fix Bug#51023 Mysql server crashes on SIGHUP and destroys InnoDB files
      
      Emulate freopen() on FreeBSD. This patch assumes we can assign to stdout
      like "FILE* f; ...; stdout = f;"

    modified:
      sql/log.cc
      sql/mysqld.cc
=== modified file 'sql/log.cc'
--- a/sql/log.cc	revid:vasil.dimov@stripped
+++ b/sql/log.cc	revid:vasil.dimov@stripped
@@ -5066,36 +5066,81 @@ void sql_perror(const char *message)
 #endif
 }
 
+/**
+  Opens filename and reassociates f with it.
+
+  @param filename		Path to file
+  @param f			Stream to reassociate
+
+  @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
+    true	Success
+  @retval
+    false	Error
+*/
+static bool my_freopen(const char *filename, FILE** f)
+{
+#ifdef __FreeBSD__
+  int fd_new;
+  int fd_orig;
+  FILE* f_new;
+
+  fd_new = open(filename, O_WRONLY | O_APPEND);
+  if (fd_new == -1) {
+    fclose(*f);
+    return false;
+  }
+  fflush(*f);
+  fd_orig = fileno(*f);
+  if (dup2(fd_new, fd_orig) == -1) {
+    close(fd_new);
+    return false;
+  }
+  close(fd_new);
+  f_new = fdopen(fd_orig, "a");
+  if (f_new == NULL) {
+    close(fd_orig);
+    return false;
+  }
+  *f = f_new;
+  return true;
+#else
+  return freopen(filename, "a", *f) != NULL;
+#endif
+}
 
 #ifdef __WIN__
 extern "C" my_bool reopen_fstreams(const char *filename,
-                                   FILE *outstream, FILE *errstream)
+                                   FILE **outstream, FILE **errstream)
 {
   int handle_fd;
   int err_fd, out_fd;
   HANDLE osfh;
 
-  DBUG_ASSERT(filename && errstream);
+  DBUG_ASSERT(filename && *errstream);
 
   // Services don't have stdout/stderr on Windows, so _fileno returns -1.
-  err_fd= _fileno(errstream);
+  err_fd= _fileno(*errstream);
   if (err_fd < 0)
   {
-    if (!freopen(filename, "a+", errstream))
+    if (!my_freopen(filename, errstream))
       return TRUE;
 
-    setbuf(errstream, NULL);
-    err_fd= _fileno(errstream);
+    setbuf(*errstream, NULL);
+    err_fd= _fileno(*errstream);
   }
 
-  if (outstream)
+  if (outstream != NULL)
   {
-    out_fd= _fileno(outstream);
+    out_fd= _fileno(*outstream);
     if (out_fd < 0)
     {
-      if (!freopen(filename, "a+", outstream))
+      if (!my_freopen(filename, outstream))
         return TRUE;
-      out_fd= _fileno(outstream);
+      out_fd= _fileno(*outstream);
     }
   }
 
@@ -5119,7 +5164,7 @@ extern "C" my_bool reopen_fstreams(const
     return TRUE;
   }
 
-  if (outstream && _dup2(handle_fd, out_fd) < 0)
+  if (*outstream && _dup2(handle_fd, out_fd) < 0)
   {
     CloseHandle(osfh);
     return TRUE;
@@ -5130,12 +5175,12 @@ extern "C" my_bool reopen_fstreams(const
 }
 #else
 extern "C" my_bool reopen_fstreams(const char *filename,
-                                   FILE *outstream, FILE *errstream)
+                                   FILE **outstream, FILE **errstream)
 {
-  if (outstream && !freopen(filename, "a+", outstream))
+  if (outstream != NULL && !my_freopen(filename, outstream))
     return TRUE;
 
-  if (errstream && !freopen(filename, "a+", errstream))
+  if (errstream != NULL && !my_freopen(filename, errstream))
     return TRUE;
 
   return FALSE;
@@ -5149,7 +5194,7 @@ extern "C" my_bool reopen_fstreams(const
 */
 static bool redirect_std_streams(const char *file)
 {
-  if (reopen_fstreams(file, stdout, stderr))
+  if (reopen_fstreams(file, &stdout, &stderr))
     return TRUE;
 
   setbuf(stderr, NULL);

=== modified file 'sql/mysqld.cc'
--- a/sql/mysqld.cc	revid:vasil.dimov@stripped
+++ b/sql/mysqld.cc	revid:vasil.dimov@stripped
@@ -198,7 +198,7 @@ typedef fp_except fp_except_t;
 #endif
 
 extern "C" my_bool reopen_fstreams(const char *filename,
-                                   FILE *outstream, FILE *errstream);
+                                   FILE **outstream, FILE **errstream);
 
 inline void setup_fpu()
 {
@@ -3818,9 +3818,9 @@ static int init_server_components()
     {
       my_bool res;
 #ifndef EMBEDDED_LIBRARY
-      res= reopen_fstreams(log_error_file, stdout, stderr);
+      res= reopen_fstreams(log_error_file, &stdout, &stderr);
 #else
-      res= reopen_fstreams(log_error_file, NULL, stderr);
+      res= reopen_fstreams(log_error_file, NULL, &stderr);
 #endif
 
       if (!res)
@@ -4472,7 +4472,7 @@ we force server id to 2, but this MySQL 
 #ifdef __WIN__
   if (!opt_console)
   {
-    if (reopen_fstreams(log_error_file, stdout, stderr))
+    if (reopen_fstreams(log_error_file, &stdout, &stderr))
       unireg_abort(1);
     setbuf(stderr, NULL);
     FreeConsole();				// Remove window


Attachment: [text/bzr-bundle] bzr/vasil.dimov@oracle.com-20101208122745-nncdyc2t63kii3d1.bundle
Thread
bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666) Bug#51023vasil.dimov8 Dec
  • Re: bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666) Bug#51023Davi Arnaut13 Dec
    • Re: bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666)Bug#51023Vasil Dimov14 Dec
      • Re: bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666) Bug#51023Davi Arnaut14 Dec
        • Re: bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666)Bug#51023Vasil Dimov15 Dec
  • Re: bzr commit into mysql-5.1-innodb branch (vasil.dimov:3666) Bug#51023Davi Arnaut13 Dec