Bug #23735 mysqlbinlog client fails when reading binlog from stdin
Submitted: 27 Oct 2006 19:14 Modified: 3 Feb 2007 2:37
Reporter: Chuck Bell Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S3 (Non-critical)
Version:5.1.13 OS:Windows (Windows)
Assigned to: Chuck Bell CPU Architecture:Any

[27 Oct 2006 19:14] Chuck Bell
Description:
The mysqlbinlog client cannot read a binlog from the command-line using redirection. When the mysqlbinlog client is run on Windows using the - switch for console input and the < redirection, the code fails to read the file correctly. The problem is the file contains data that is being read as an EOF marker (CTRL-Z). This is because Windows reads stdin as text rather than binary.

Related bug report: #7853.

Errors may resemble the following:

ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 98, event_t
ype: 15
ERROR: Could not read a Format_description_log_event event at offset 4 ; this co
uld be a log format error or read error
mysqltest: At line 266: command "$MYSQL_BINLOG --short-form --position=412 - < $
MYSQLTEST_VARDIR/log/master-bin.000001" failed

How to repeat:
There are two ways to repeat the bug (may be more):

1. Run the rpl_row_mysqlbinlog test on Windows as shown (from cygwin):
./mysql-test-run.pl --mysqld=--binlog-format=row rpl_row_mysqlbinlog

Note: you must have Cygwin and ActivePerl installed to run the test suite on Windows.

2. Run the mysqlbinlog client from the command line passing in the binlog using redirection as shown:

mysqlbinlog --short-form - < master-bin.000001

Suggested fix:
Windows clients cannot read a binlog from the command-line using redirection because on Windows reading from stdin is set to text mode by default. To fix this, the code must be changed to open stdin in binary mode.
[27 Oct 2006 19:44] MySQL Verification Team
Thank you for the bug report.

c:\mysql\bin>mysqlbinlog --short-form - < c:\mysql\data\light-bin.000002
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 98, event_type: 15
Could not read entry at offset 4:Error in log format or read error
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

c:\mysql\bin>mysqld-nt -V
mysqld-nt  Ver 5.1.13-beta-nt for Win32 on ia32 (Source distribution)
[5 Nov 2006 9:18] Magnus BlÄudd
Please attach the patch or a link to the patch to the bug report
[7 Nov 2006 18:08] Chuck Bell
I pushed this to the mysql-5.1-new-rpl tree.
[18 Nov 2006 17:35] Igor Babaev
Chuck, where's commit for this patch?
[20 Nov 2006 15:36] Lars Thalmann
Pushed into 5.1.14.

This is the patch:

--- 1.134/client/mysqlbinlog.cc 2006-11-01 10:56:52 -05:00
+++ 1.135/client/mysqlbinlog.cc 2006-11-01 10:56:52 -05:00
@@ -1360,6 +1360,24 @@
   }
   else // reading from stdin;
   {
+    /*
+      Bug fix: #23735
+      Author: Chuck Bell
+      Description:
+        Windows opens stdin in text mode by default. Certain characters
+        such as CTRL-Z are interpeted as events and the read() method
+        will stop. CTRL-Z is the EOF marker in Windows. to get past this
+        you have to open stdin in binary mode. Setmode() is used to set
+        stdin in binary mode. Errors on setting this mode result in
+        halting the function and printing an error message to stderr.
+    */
+#if defined (__WIN__) || (_WIN64)
+    if (_setmode(fileno(stdin), O_BINARY) == -1)
+    {
+       fprintf(stderr, "Could not set binary mode on stdin.\n");
+       return 1;
+    }
+#endif
     if (init_io_cache(file, fileno(stdin), 0, READ_CACHE, (my_off_t) 0,
                      0, MYF(MY_WME | MY_NABP | MY_DONT_CHECK_FILESIZE)))
       return 1;
[28 Nov 2006 20:45] Paul DuBois
Noted in 5.1.14 changelog.

When reading from the standard input on Windows, mysqlbinlog opened
the input in text mode rather than binary mode and consequently
misinterpreted some characters such as Control-Z. 

Resetting bug report to NDI pending decision about whether the
fix should go in 5.0.  (If not, just close the report.)
[11 Dec 2006 10:25] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/16760

ChangeSet@1.2348, 2006-12-11 11:25:45+01:00, msvensson@neptunus.(none) +1 -0
  Bug#23735 mysqlbinlog client fails when reading binlog from stdin
   - Windows opens stdin in text mode by default. Certain characters
     such as CTRL-Z are interpeted as events and the read() method
     will stop. CTRL-Z is the EOF marker in Windows. to get past this
     you have to open stdin in binary mode. Setmode() is used to set
     stdin in binary mode. Errors on setting this mode result in
     halting the function and printing an error message to stderr.
[31 Jan 2007 19:16] Chad MILLER
Available in 5.0.36, 5.1.15-beta.
[3 Feb 2007 2:37] Paul DuBois
Noted in 5.0.36 changelog, and I moved the 5.1 entry
from 5.1.14 to 5.1.15.