lnav/src/grep_proc.cc

389 lines
12 KiB
C++
Raw Permalink Normal View History

2009-09-14 01:07:32 +00:00
/**
2013-05-03 06:02:03 +00:00
* Copyright (c) 2007-2012, Timothy Stack
*
* All rights reserved.
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
2009-09-14 01:07:32 +00:00
* @file grep_proc.cc
*/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "lnav_log.hh"
2009-09-14 01:07:32 +00:00
#include "grep_proc.hh"
#include "time_T.hh"
using namespace std;
grep_proc::grep_proc(pcre *code,
2013-05-28 04:35:00 +00:00
grep_proc_source &gps,
int &maxfd,
fd_set &readfds)
2009-09-14 01:07:32 +00:00
: gp_pcre(code),
2011-06-06 02:05:46 +00:00
gp_code(code),
2009-09-14 01:07:32 +00:00
gp_source(gps),
gp_pipe_offset(0),
gp_child(-1),
gp_child_started(false),
gp_maxfd(maxfd),
gp_readfds(readfds),
gp_last_line(0),
gp_sink(NULL),
gp_control(NULL)
{
require(this->invariant());
2009-09-14 01:07:32 +00:00
}
grep_proc::~grep_proc()
{
this->gp_queue.clear();
2009-09-14 01:07:32 +00:00
this->cleanup();
}
void grep_proc::handle_match(int line,
2013-05-28 04:35:00 +00:00
string &line_value,
int off,
int *matches,
int count)
2009-09-14 01:07:32 +00:00
{
int lpc;
2013-05-28 04:35:00 +00:00
2009-09-14 01:07:32 +00:00
if (off == 0) {
2013-05-28 04:35:00 +00:00
fprintf(stdout, "%d\n", line);
2009-09-14 01:07:32 +00:00
}
fprintf(stdout, "[%d:%d]\n", matches[0], matches[1]);
for (lpc = 1; lpc < count; lpc++) {
2013-05-28 04:35:00 +00:00
fprintf(stdout,
"(%d:%d)",
matches[lpc * 2],
matches[lpc * 2 + 1]);
fwrite(&(line_value.c_str()[matches[lpc * 2]]),
1,
matches[lpc * 2 + 1] -
matches[lpc * 2],
stdout);
fputc('\n', stdout);
2009-09-14 01:07:32 +00:00
}
}
void grep_proc::start(void)
{
require(this->invariant());
2009-09-14 01:07:32 +00:00
if (this->gp_child_started || this->gp_queue.empty()) {
2013-05-28 04:35:00 +00:00
return;
2009-09-14 01:07:32 +00:00
}
auto_pipe in_pipe(STDIN_FILENO);
auto_pipe out_pipe(STDOUT_FILENO);
auto_pipe err_pipe(STDERR_FILENO);
2009-09-14 01:07:32 +00:00
/* Get ahold of some pipes for stdout and stderr. */
if (out_pipe.open() < 0) {
2013-05-28 04:35:00 +00:00
throw error(errno);
2009-09-14 01:07:32 +00:00
}
if (err_pipe.open() < 0) {
2013-05-28 04:35:00 +00:00
throw error(errno);
2009-09-14 01:07:32 +00:00
}
if ((this->gp_child = fork()) < 0) {
2013-05-28 04:35:00 +00:00
throw error(errno);
2009-09-14 01:07:32 +00:00
}
in_pipe.after_fork(this->gp_child);
out_pipe.after_fork(this->gp_child);
err_pipe.after_fork(this->gp_child);
2009-09-14 01:07:32 +00:00
if (this->gp_child != 0) {
fcntl(out_pipe.read_end(), F_SETFL, O_NONBLOCK);
fcntl(out_pipe.read_end(), F_SETFD, 1);
this->gp_line_buffer.set_fd(out_pipe.read_end());
2013-05-28 04:35:00 +00:00
fcntl(err_pipe.read_end(), F_SETFL, O_NONBLOCK);
fcntl(err_pipe.read_end(), F_SETFD, 1);
require(this->gp_err_pipe.get() == -1);
this->gp_err_pipe = err_pipe.read_end();
2013-05-28 04:35:00 +00:00
this->gp_child_started = true;
FD_SET(this->gp_line_buffer.get_fd(), &this->gp_readfds);
FD_SET(this->gp_err_pipe, &this->gp_readfds);
this->gp_maxfd = std::max(this->gp_maxfd,
std::max(this->gp_line_buffer.get_fd(),
this->gp_err_pipe.get()));
this->gp_queue.clear();
return;
2009-09-14 01:07:32 +00:00
}
/* In the child... */
2013-05-28 04:35:00 +00:00
2009-09-14 01:07:32 +00:00
/*
* Restore the default signal handlers so we don't hang around
2009-09-14 01:07:32 +00:00
* forever if there is a problem.
*/
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
this->child_init();
2013-05-28 04:35:00 +00:00
2012-04-24 21:25:38 +00:00
this->child_loop();
_exit(0);
2012-04-24 21:25:38 +00:00
}
void grep_proc::child_loop(void)
{
2009-09-14 01:07:32 +00:00
char outbuf[BUFSIZ * 2];
string line_value;
stdout = fdopen(STDOUT_FILENO, "w");
/* Make sure buffering is on, not sure of the state in the parent. */
if (setvbuf(stdout, outbuf, _IOFBF, BUFSIZ * 2) < 0) {
2013-05-28 04:35:00 +00:00
perror("setvbuf");
2009-09-14 01:07:32 +00:00
}
line_value.reserve(BUFSIZ * 2);
while (!this->gp_queue.empty()) {
2013-05-28 04:35:00 +00:00
grep_line_t start_line = this->gp_queue.front().first;
grep_line_t stop_line = this->gp_queue.front().second;
bool done = false;
int line;
this->gp_queue.pop_front();
if (start_line == -1) {
start_line = this->gp_highest_line;
}
for (line = start_line;
(stop_line == -1 || line < stop_line) && !done;
line++) {
line_value.clear();
done = !this->gp_source.grep_value_for_line(line, line_value);
if (!done) {
pcre_context_static<128> pc;
2013-05-28 04:35:00 +00:00
pcre_input pi(line_value);
while (this->gp_pcre.match(pc, pi)) {
pcre_context::iterator pc_iter;
pcre_context::capture_t *m;
if (pi.pi_offset == 0) {
fprintf(stdout, "%d\n", line);
}
m = pc.all();
fprintf(stdout, "[%d:%d]\n", m->c_begin, m->c_end);
for (pc_iter = pc.begin(); pc_iter != pc.end();
pc_iter++) {
2014-06-18 04:29:42 +00:00
if (!pc_iter->is_valid()) {
continue;
}
2013-05-28 04:35:00 +00:00
fprintf(stdout,
"(%d:%d)",
pc_iter->c_begin,
pc_iter->c_end);
/* If the capture was conditional, pcre will return a -1
* here.
*/
if (pc_iter->c_begin >= 0) {
fwrite(pi.get_substr_start(pc_iter),
1,
pc_iter->length(),
stdout);
}
fputc('\n', stdout);
}
fprintf(stdout, "/\n");
}
}
if (((line + 1) % 10000) == 0) {
/* Periodically flush the buffer so the parent sees progress */
this->child_batch();
}
}
if (stop_line == -1) {
// When scanning to the end of the source, we need to return the
// highest line that was seen so that the next request that
// continues from the end works properly.
fprintf(stdout, "h%d\n", line - 1);
}
2013-05-28 04:35:00 +00:00
this->child_term();
2009-09-14 01:07:32 +00:00
}
}
void grep_proc::cleanup(void)
{
if (this->gp_child != -1 && this->gp_child != 0) {
2013-05-28 04:35:00 +00:00
int status;
kill(this->gp_child, SIGTERM);
while (waitpid(this->gp_child, &status, 0) < 0 && (errno == EINTR)) {
;
}
require(!WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT);
2013-05-28 04:35:00 +00:00
this->gp_child = -1;
this->gp_child_started = false;
if (this->gp_sink) {
this->gp_sink->grep_end(*this);
}
2009-09-14 01:07:32 +00:00
}
if (this->gp_err_pipe != -1) {
2013-05-28 04:35:00 +00:00
FD_CLR(this->gp_err_pipe, &this->gp_readfds);
this->gp_err_pipe.reset();
2009-09-14 01:07:32 +00:00
}
if (this->gp_line_buffer.get_fd() != -1) {
2013-05-28 04:35:00 +00:00
FD_CLR(this->gp_line_buffer.get_fd(), &this->gp_readfds);
2009-09-14 01:07:32 +00:00
}
this->gp_pipe_offset = 0;
this->gp_line_buffer.reset();
ensure(this->invariant());
2009-09-14 01:07:32 +00:00
if (!this->gp_queue.empty()) {
2013-05-28 04:35:00 +00:00
this->start();
2009-09-14 01:07:32 +00:00
}
}
void grep_proc::dispatch_line(char *line)
{
int start, end, capture_start;
require(line != NULL);
2009-09-14 01:07:32 +00:00
if (sscanf(line, "h%d", this->gp_highest_line.out()) == 1) {
2009-09-14 01:07:32 +00:00
} else if (sscanf(line, "%d", this->gp_last_line.out()) == 1) {
/* Starting a new line with matches. */
ensure(this->gp_last_line >= 0);
2009-09-14 01:07:32 +00:00
}
else if (sscanf(line, "[%d:%d]", &start, &end) == 2) {
require(start >= 0);
require(end >= 0);
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
/* Pass the match offsets to the sink delegate. */
if (this->gp_sink != NULL) {
this->gp_sink->grep_match(*this, this->gp_last_line, start, end);
}
2009-09-14 01:07:32 +00:00
}
else if (sscanf(line, "(%d:%d)%n", &start, &end, &capture_start) == 2) {
require(start == -1 || start >= 0);
require(end >= 0);
2013-05-28 04:35:00 +00:00
/* Pass the captured strings to the sink delegate. */
if (this->gp_sink != NULL) {
this->gp_sink->grep_capture(*this,
this->gp_last_line,
start,
end,
start < 0 ?
NULL : &line[capture_start]);
}
2011-06-06 02:05:46 +00:00
}
else if (line[0] == '/') {
2013-05-28 04:35:00 +00:00
if (this->gp_sink != NULL) {
this->gp_sink->grep_match_end(*this, this->gp_last_line);
}
2009-09-14 01:07:32 +00:00
}
else {
log_error("bad line from child -- %s", line);
2009-09-14 01:07:32 +00:00
}
}
void grep_proc::check_fd_set(fd_set &ready_fds)
{
require(this->invariant());
2009-09-14 01:07:32 +00:00
if (this->gp_err_pipe != -1 && FD_ISSET(this->gp_err_pipe, &ready_fds)) {
2013-05-28 04:35:00 +00:00
char buffer[1024 + 1];
ssize_t rc;
2013-05-28 04:35:00 +00:00
rc = read(this->gp_err_pipe, buffer, sizeof(buffer) - 1);
if (rc > 0) {
static const char *PREFIX = ": ";
buffer[rc] = '\0';
if (strncmp(buffer, PREFIX, strlen(PREFIX)) == 0) {
char *lf;
if ((lf = strchr(buffer, '\n')) != NULL) {
*lf = '\0';
}
if (this->gp_control != NULL) {
this->gp_control->grep_error(&buffer[strlen(PREFIX)]);
}
}
}
else if (rc == 0) {
FD_CLR(this->gp_err_pipe, &this->gp_readfds);
this->gp_err_pipe.reset();
}
2009-09-14 01:07:32 +00:00
}
if (this->gp_line_buffer.get_fd() != -1 &&
2013-05-28 04:35:00 +00:00
FD_ISSET(this->gp_line_buffer.get_fd(), &ready_fds)) {
try {
static const int MAX_LOOPS = 100;
int loop_count = 0;
line_value lv;
2013-05-28 04:35:00 +00:00
while ((loop_count < MAX_LOOPS) &&
(this->gp_line_buffer.read_line(this->gp_pipe_offset, lv))) {
2014-06-18 04:29:42 +00:00
lv.terminate();
this->dispatch_line(lv.lv_start);
2013-05-28 04:35:00 +00:00
loop_count += 1;
}
if (this->gp_sink != NULL) {
this->gp_sink->grep_end_batch(*this);
}
if ((off_t) this->gp_line_buffer.get_file_size() ==
this->gp_pipe_offset) {
this->cleanup();
}
}
catch (line_buffer::error & e) {
this->cleanup();
}
2009-09-14 01:07:32 +00:00
}
ensure(this->invariant());
2009-09-14 01:07:32 +00:00
}