From 6c8d7ea8949eb938b1428ab85d199c6dfcbc2576 Mon Sep 17 00:00:00 2001
From: "Kristofer.Pettersson@naruto." <>
Date: Fri, 1 Dec 2006 09:49:19 +0100
Subject: [PATCH] Bug#17733 Flushing logs causes daily server crash

Server crashes if a flush commmand is issued and binlog is closed.
- added check to prevent binlog access when binlog file isn't opened.
---
 mysql-test/r/flush2.result     | 24 ++++++++++++++++++++++++
 mysql-test/t/flush2-master.opt |  1 +
 mysql-test/t/flush2.test       |  9 +++++++++
 sql/sql_parse.cc               | 11 ++++++++---
 4 files changed, 42 insertions(+), 3 deletions(-)
 create mode 100644 mysql-test/r/flush2.result
 create mode 100644 mysql-test/t/flush2-master.opt
 create mode 100644 mysql-test/t/flush2.test

diff --git a/mysql-test/r/flush2.result b/mysql-test/r/flush2.result
new file mode 100644
index 00000000000..7c94219fd71
--- /dev/null
+++ b/mysql-test/r/flush2.result
@@ -0,0 +1,24 @@
+flush logs;
+set global expire_logs_days = 3;
+show variables like 'log%';
+Variable_name	Value
+log	ON
+log_bin	OFF
+log_bin_trust_function_creators	ON
+log_error	
+log_queries_not_using_indexes	OFF
+log_slave_updates	OFF
+log_slow_queries	OFF
+log_warnings	1
+flush logs;
+show variables like 'log%';
+Variable_name	Value
+log	ON
+log_bin	OFF
+log_bin_trust_function_creators	ON
+log_error	
+log_queries_not_using_indexes	OFF
+log_slave_updates	OFF
+log_slow_queries	OFF
+log_warnings	1
+set global expire_logs_days = 0;
diff --git a/mysql-test/t/flush2-master.opt b/mysql-test/t/flush2-master.opt
new file mode 100644
index 00000000000..ccbd01c91d3
--- /dev/null
+++ b/mysql-test/t/flush2-master.opt
@@ -0,0 +1 @@
+--disable-log-bin
diff --git a/mysql-test/t/flush2.test b/mysql-test/t/flush2.test
new file mode 100644
index 00000000000..fc9e88e3141
--- /dev/null
+++ b/mysql-test/t/flush2.test
@@ -0,0 +1,9 @@
+#
+# Bug#17733 Flushing logs causes daily server crash
+#
+flush logs;
+set global expire_logs_days = 3;
+show variables like 'log%';
+flush logs;
+show variables like 'log%';
+set global expire_logs_days = 0;
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 4992d2514c9..acc4b48e5a6 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -4008,8 +4008,9 @@ mysql_execute_command(THD *thd)
   case SQLCOM_FLUSH:
   {
     bool write_to_binlog;
-    if (check_global_access(thd,RELOAD_ACL) || check_db_used(thd, all_tables))
+    if (check_global_access(thd,RELOAD_ACL))
       goto error;
+
     /*
       reload_acl_and_cache() will tell us if we are allowed to write to the
       binlog or not.
@@ -4030,7 +4031,8 @@ mysql_execute_command(THD *thd)
         }
       }
       send_ok(thd);
-    }
+    } 
+    
     break;
   }
   case SQLCOM_KILL:
@@ -6696,7 +6698,10 @@ bool reload_acl_and_cache(THD *thd, ulong options, TABLE_LIST *tables,
     tmp_write_to_binlog= 0;
     mysql_log.new_file(1);
     mysql_slow_log.new_file(1);
-    mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
+    if( mysql_bin_log.is_open() )
+    {
+      mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
+    }
 #ifdef HAVE_REPLICATION
     pthread_mutex_lock(&LOCK_active_mi);
     rotate_relay_log(active_mi);
-- 
2.30.9