Make strerror_r portable.

This uses the same code that broker already uses to determine if we use
the XSI or gnu version of strerror_r. Patch by Thomas Petersen.
This commit is contained in:
Johanna Amann 2017-09-18 14:43:42 -07:00
parent 2a873f5aed
commit fc33bf2014
13 changed files with 63 additions and 18 deletions

18
CHANGES
View file

@ -1,4 +1,22 @@
2.5-306 | 2017-09-18 14:43:42 -0700
* Make strerror_r portable, supporting XSI/gnu versions. (Thomas Petersen)
* Prevent crash when calling bro -U. (Thomas Petersen)
* Remove annoying error message from connsize bifs. (Johanna Amann)
* Add test to verify that log rotation works with gzipped logs (Daniel Thayer)
* Fix ascii writer to not discard a ".gz" file extension. (Daniel Thayer)
When Bro writes a compressed log, it uses a file extension of ".gz".
However, upon log rotation the ascii writer script function
"default_rotation_postprocessor_func" was discarding the ".gz"
file extension. Fixed so that the correct file extension is
preserved after rotation. (Daniel Thayer)
2.5-297 | 2017-09-11 09:26:33 -0700 2.5-297 | 2017-09-11 09:26:33 -0700
* Fix small OCSP parser bug; serial numbers were not passed to events * Fix small OCSP parser bug; serial numbers were not passed to events

View file

@ -1 +1 @@
2.5-297 2.5-306

View file

@ -302,7 +302,7 @@ FILE* BroFile::BringIntoCache()
if ( ! f ) if ( ! f )
{ {
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("can't open %s: %s", name, buf); reporter->Error("can't open %s: %s", name, buf);
f = fopen("/dev/null", "w"); f = fopen("/dev/null", "w");
@ -313,7 +313,7 @@ FILE* BroFile::BringIntoCache()
return f; return f;
} }
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("can't open /dev/null: %s", buf); reporter->Error("can't open /dev/null: %s", buf);
return 0; return 0;
} }
@ -323,7 +323,7 @@ FILE* BroFile::BringIntoCache()
if ( fseek(f, position, SEEK_SET) < 0 ) if ( fseek(f, position, SEEK_SET) < 0 )
{ {
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("reopen seek failed: %s", buf); reporter->Error("reopen seek failed: %s", buf);
} }
@ -413,7 +413,7 @@ void BroFile::Suspend()
if ( (position = ftell(f)) < 0 ) if ( (position = ftell(f)) < 0 )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("ftell failed: %s", buf); reporter->Error("ftell failed: %s", buf);
position = 0; position = 0;
} }

View file

@ -16,7 +16,7 @@ Flare::Flare()
static void bad_pipe_op(const char* which) static void bad_pipe_op(const char* which)
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->FatalErrorWithCore("unexpected pipe %s failure: %s", which, buf); reporter->FatalErrorWithCore("unexpected pipe %s failure: %s", which, buf);
} }

View file

@ -12,7 +12,7 @@ using namespace bro;
static void pipe_fail(int eno) static void pipe_fail(int eno)
{ {
char tmp[256]; char tmp[256];
strerror_r(eno, tmp, sizeof(tmp)); bro_strerror_r(eno, tmp, sizeof(tmp));
reporter->FatalError("Pipe failure: %s", tmp); reporter->FatalError("Pipe failure: %s", tmp);
} }

View file

@ -84,7 +84,7 @@ bool LoadPolicyFileText(const char* policy_filename)
if ( fstat(fileno(f), &st) != 0 ) if ( fstat(fileno(f), &st) != 0 )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("fstat failed on %s: %s", policy_filename, buf); reporter->Error("fstat failed on %s: %s", policy_filename, buf);
fclose(f); fclose(f);
return false; return false;

View file

@ -229,7 +229,7 @@ int TCP_Endpoint::DataSent(double t, uint64 seq, int len, int caplen,
if ( fwrite(data, 1, len, f) < unsigned(len) ) if ( fwrite(data, 1, len, f) < unsigned(len) )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("TCP contents write failed: %s", buf); reporter->Error("TCP contents write failed: %s", buf);
if ( contents_file_write_failure ) if ( contents_file_write_failure )

View file

@ -20,7 +20,7 @@ Extract::Extract(RecordVal* args, File* file, const string& arg_filename,
{ {
fd = 0; fd = 0;
char buf[128]; char buf[128];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("cannot open %s: %s", filename.c_str(), buf); reporter->Error("cannot open %s: %s", filename.c_str(), buf);
} }
} }

View file

@ -90,7 +90,7 @@ bool Raw::SetFDFlags(int fd, int cmd, int flags)
return true; return true;
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
Error(Fmt("failed to set fd flags: %s", buf)); Error(Fmt("failed to set fd flags: %s", buf));
return false; return false;
} }
@ -197,7 +197,7 @@ bool Raw::Execute()
else else
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
Warning(Fmt("Could not set child process group: %s", buf)); Warning(Fmt("Could not set child process group: %s", buf));
} }
} }
@ -293,7 +293,7 @@ bool Raw::OpenInput()
if ( fseek(file.get(), pos, whence) < 0 ) if ( fseek(file.get(), pos, whence) < 0 )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
Error(Fmt("Seek failed in init: %s", buf)); Error(Fmt("Seek failed in init: %s", buf));
} }
} }

View file

@ -414,7 +414,7 @@ bool Ascii::DoRotate(const char* rotated_path, double open, double close, bool t
if ( rename(fname.c_str(), nname.c_str()) != 0 ) if ( rename(fname.c_str(), nname.c_str()) != 0 )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
Error(Fmt("failed to rename %s to %s: %s", fname.c_str(), Error(Fmt("failed to rename %s to %s: %s", fname.c_str(),
nname.c_str(), buf)); nname.c_str(), buf));
FinishedRotation(); FinishedRotation();

View file

@ -98,7 +98,7 @@ const char* BasicThread::Strerror(int err)
if ( ! strerr_buffer ) if ( ! strerr_buffer )
strerr_buffer = new char[256]; strerr_buffer = new char[256];
strerror_r(err, strerr_buffer, 256); bro_strerror_r(err, strerr_buffer, 256);
return strerr_buffer; return strerr_buffer;
} }

View file

@ -1012,7 +1012,7 @@ FILE* open_file(const string& path, const string& mode)
if ( ! rval ) if ( ! rval )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
reporter->Error("Failed to open file %s: %s", filename, buf); reporter->Error("Failed to open file %s: %s", filename, buf);
} }
@ -1396,7 +1396,7 @@ void _set_processing_status(const char* status)
if ( fd < 0 ) if ( fd < 0 )
{ {
char buf[256]; char buf[256];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
if ( reporter ) if ( reporter )
reporter->Error("Failed to open process status file '%s': %s", reporter->Error("Failed to open process status file '%s': %s",
proc_status_file, buf); proc_status_file, buf);
@ -1616,7 +1616,7 @@ void safe_close(int fd)
if ( close(fd) < 0 && errno != EINTR ) if ( close(fd) < 0 && errno != EINTR )
{ {
char buf[128]; char buf[128];
strerror_r(errno, buf, sizeof(buf)); bro_strerror_r(errno, buf, sizeof(buf));
fprintf(stderr, "safe_close error %d: %s\n", errno, buf); fprintf(stderr, "safe_close error %d: %s\n", errno, buf);
abort(); abort();
} }
@ -1749,3 +1749,24 @@ std::string canonify_name(const std::string& name)
return nname; return nname;
} }
static void strerror_r_helper(char* result, char* buf, size_t buflen)
{
// Seems the GNU flavor of strerror_r may return a pointer to a static
// string. So try to copy as much as possible into desired buffer.
auto len = strlen(result);
strncpy(buf, result, buflen);
if ( len >= buflen )
buf[buflen - 1] = 0;
}
static void strerror_r_helper(int result, char* buf, size_t buflen)
{ /* XSI flavor of strerror_r, no-op. */ }
void bro_strerror_r(int bro_errno, char* buf, size_t buflen)
{
auto res = strerror_r(bro_errno, buf, buflen);
// GNU vs. XSI flavors make it harder to use strerror_r.
strerror_r_helper(res, buf, buflen);
}

View file

@ -516,4 +516,10 @@ struct CompareString
*/ */
std::string canonify_name(const std::string& name); std::string canonify_name(const std::string& name);
/**
* Reentrant version of strerror(). Takes care of the difference between the
* XSI-compliant and the GNU-specific version of strerror_r().
*/
void bro_strerror_r(int bro_errno, char* buf, size_t buflen);
#endif #endif