From 08656c976b7f0f5194c0bcbf6abba2eba2dbb6f2 Mon Sep 17 00:00:00 2001 From: Bernhard Amann Date: Mon, 27 May 2013 22:59:27 -0700 Subject: [PATCH] small fixes. --- src/input/readers/Raw.cc | 18 +++++---- src/input/readers/Raw.h | 2 +- .../out | 2 + .../base/frameworks/input/raw/long.bro | 37 +++++++++++++++++++ 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 testing/btest/Baseline/scripts.base.frameworks.input.raw.long/out create mode 100644 testing/btest/scripts/base/frameworks/input/raw/long.bro diff --git a/src/input/readers/Raw.cc b/src/input/readers/Raw.cc index 3b79ca4bf2..435876ece1 100644 --- a/src/input/readers/Raw.cc +++ b/src/input/readers/Raw.cc @@ -18,7 +18,7 @@ using namespace input::reader; using threading::Value; using threading::Field; -const int Raw::block_size = 512; // how big do we expect our chunks of data to be... +const int Raw::block_size = 4096; // how big do we expect our chunks of data to be... Raw::Raw(ReaderFrontend *frontend) : ReaderBackend(frontend) @@ -102,7 +102,6 @@ bool Raw::Execute() dup2(pipes[stderr_out], stderr_fileno); } - //execv("/usr/bin/uname",test); execl("/bin/sh", "sh", "-c", fname.c_str(), NULL); fprintf(stderr, "Exec failed :(......\n"); exit(255); @@ -294,7 +293,8 @@ bool Raw::DoInit(const ReaderInfo& info, int num_fields, const Field* const* fie int64_t Raw::GetLine(FILE* arg_file) { errno = 0; - int pos = 0; // strstr_n only works on ints - so no use to use something bigger here + int pos = 0; // strstr_n only works on ints - so no use to use something different here + int offset = 0; if ( buf == 0 ) buf = new char[block_size]; @@ -303,9 +303,10 @@ int64_t Raw::GetLine(FILE* arg_file) for (;;) { - size_t readbytes = fread(buf+bufpos, 1, block_size-bufpos, arg_file); + size_t readbytes = fread(buf+bufpos+offset, 1, block_size-bufpos, arg_file); pos += bufpos + readbytes; - bufpos = 0; // read full block size in next read... + //printf("Pos: %d\n", pos); + bufpos = offset = 0; // read full block size in next read... if ( pos == 0 && errno != 0 ) break; @@ -336,6 +337,7 @@ int64_t Raw::GetLine(FILE* arg_file) memcpy(newbuf, buf, block_size*(repeats-1)); delete buf; buf = newbuf; + offset = block_size*(repeats-1); } else { @@ -348,11 +350,11 @@ int64_t Raw::GetLine(FILE* arg_file) { // we have leftovers. copy them into the buffer for the next line buf = new char[block_size]; - memcpy(buf, buf + found + sep_length, pos - found - sep_length); + memcpy(buf, outbuf + found + sep_length, pos - found - sep_length); bufpos = pos - found - sep_length; } - return found; + return found; } } @@ -511,7 +513,7 @@ bool Raw::DoUpdate() // and let's check if the child process is still alive int return_code; - if ( waitpid(childpid, &return_code, WNOHANG) != 0 ) { + if ( childpid != -1 && waitpid(childpid, &return_code, WNOHANG) != 0 ) { // child died :( bool signal = false; int code = 0; diff --git a/src/input/readers/Raw.h b/src/input/readers/Raw.h index 8ea03a70b4..6dbae21002 100644 --- a/src/input/readers/Raw.h +++ b/src/input/readers/Raw.h @@ -45,7 +45,7 @@ private: unsigned int sep_length; // length of the separator static const int block_size; - uint64_t bufpos; + int bufpos; char* buf; char* outbuf; diff --git a/testing/btest/Baseline/scripts.base.frameworks.input.raw.long/out b/testing/btest/Baseline/scripts.base.frameworks.input.raw.long/out new file mode 100644 index 0000000000..fac8e79c0b --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.input.raw.long/out @@ -0,0 +1,2 @@ +Input::EVENT_NEW +8193 diff --git a/testing/btest/scripts/base/frameworks/input/raw/long.bro b/testing/btest/scripts/base/frameworks/input/raw/long.bro new file mode 100644 index 0000000000..ac07639f77 --- /dev/null +++ b/testing/btest/scripts/base/frameworks/input/raw/long.bro @@ -0,0 +1,37 @@ +# @TEST-EXEC: dd if=/dev/zero of=input.log bs=8193 count=1 +# @TEST-EXEC: btest-bg-run bro bro -b %INPUT +# @TEST-EXEC: btest-bg-wait -k 5 +# @TEST-EXEC: btest-diff out +# +# this test should be longer than one block-size. to test behavior of input-reader if it has to re-allocate stuff. + +redef exit_only_after_terminate = T; + +global outfile: file; +global try: count; + +module A; + +type Val: record { + s: string; +}; + +event line(description: Input::EventDescription, tpe: Input::Event, s: string) + { + print outfile, tpe; + print outfile, |s|; + try = try + 1; + if ( try == 1 ) + { + close(outfile); + terminate(); + } + } + +event bro_init() + { + try = 0; + outfile = open("../out"); + Input::add_event([$source="../input.log", $reader=Input::READER_RAW, $mode=Input::STREAM, $name="input", $fields=Val, $ev=line, $want_record=F]); + Input::remove("input"); + }