2012-06-05 20:18:59 +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;
|
2022-03-16 22:38:08 +00:00
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2013-05-03 06:02:03 +00:00
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2012-06-05 20:18:59 +00:00
|
|
|
* @file piper_proc.cc
|
|
|
|
*/
|
2009-09-14 01:07:32 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "piper_proc.hh"
|
2010-01-29 23:17:08 +00:00
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
#include <errno.h>
|
2013-05-01 04:48:16 +00:00
|
|
|
#include <fcntl.h>
|
2022-03-16 22:38:08 +00:00
|
|
|
#include <poll.h>
|
2009-09-14 01:07:32 +00:00
|
|
|
#include <signal.h>
|
2022-03-16 22:38:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
2009-09-14 01:07:32 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-05-08 12:30:59 +00:00
|
|
|
#include "base/lnav_log.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "config.h"
|
2013-05-01 04:48:16 +00:00
|
|
|
#include "line_buffer.hh"
|
2009-09-14 01:07:32 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static const char* STDIN_EOF_MSG = "---- END-OF-STDIN ----";
|
2013-05-03 06:02:03 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static ssize_t
|
|
|
|
write_timestamp(int fd, off_t woff)
|
2013-05-03 06:02:03 +00:00
|
|
|
{
|
2022-03-16 22:38:08 +00:00
|
|
|
char time_str[64];
|
2013-05-03 06:02:03 +00:00
|
|
|
struct timeval tv;
|
2022-03-16 22:38:08 +00:00
|
|
|
char ms_str[10];
|
2013-05-03 06:02:03 +00:00
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
gettimeofday(&tv, nullptr);
|
2013-06-28 00:23:28 +00:00
|
|
|
strftime(time_str, sizeof(time_str), "%FT%T", localtime(&tv.tv_sec));
|
2022-03-16 22:38:08 +00:00
|
|
|
snprintf(ms_str, sizeof(ms_str), ".%03d", (int) (tv.tv_usec / 1000));
|
2013-05-03 06:02:03 +00:00
|
|
|
strcat(time_str, ms_str);
|
2013-06-28 00:23:28 +00:00
|
|
|
strcat(time_str, " ");
|
2013-05-03 06:02:03 +00:00
|
|
|
return pwrite(fd, time_str, strlen(time_str), woff);
|
|
|
|
}
|
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
piper_proc::piper_proc(int pipefd, bool timestamp, int filefd)
|
|
|
|
: pp_fd(filefd), pp_child(-1)
|
2009-09-14 01:07:32 +00:00
|
|
|
{
|
2014-03-06 14:58:49 +00:00
|
|
|
require(pipefd >= 0);
|
2010-02-24 04:35:52 +00:00
|
|
|
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(fcntl(this->pp_fd.get(), F_SETFD, FD_CLOEXEC));
|
2013-10-11 13:22:29 +00:00
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
this->pp_child = fork();
|
|
|
|
switch (this->pp_child) {
|
2022-03-16 22:38:08 +00:00
|
|
|
case -1:
|
|
|
|
throw error(errno);
|
|
|
|
|
|
|
|
case 0: {
|
|
|
|
auto_fd infd(pipefd);
|
|
|
|
line_buffer lb;
|
|
|
|
off_t woff = 0, last_woff = 0;
|
|
|
|
file_range last_range;
|
|
|
|
int nullfd;
|
|
|
|
|
|
|
|
nullfd = open("/dev/null", O_RDWR);
|
|
|
|
if (pipefd != STDIN_FILENO) {
|
|
|
|
dup2(nullfd, STDIN_FILENO);
|
2015-03-16 16:16:49 +00:00
|
|
|
}
|
2022-03-16 22:38:08 +00:00
|
|
|
dup2(nullfd, STDOUT_FILENO);
|
|
|
|
for (int fd_to_close = 0; fd_to_close < 1024; fd_to_close++) {
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if (fd_to_close == this->pp_fd.get()) {
|
|
|
|
continue;
|
2019-06-15 13:32:02 +00:00
|
|
|
}
|
2022-03-16 22:38:08 +00:00
|
|
|
if ((flags = fcntl(fd_to_close, F_GETFD)) == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (flags & FD_CLOEXEC) {
|
|
|
|
close(fd_to_close);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_perror(fcntl(infd.get(), F_SETFL, O_NONBLOCK));
|
|
|
|
lb.set_fd(infd);
|
|
|
|
do {
|
|
|
|
struct pollfd pfd = {lb.get_fd(), POLLIN, 0};
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
poll(&pfd, 1, -1);
|
|
|
|
while (true) {
|
|
|
|
auto load_result = lb.load_next_line(last_range);
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
if (load_result.isErr()) {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
auto li = load_result.unwrap();
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
if (li.li_partial && !lb.is_pipe_closed()) {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
if (li.li_file_range.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
auto read_result = lb.read_range(li.li_file_range);
|
2019-06-15 13:32:02 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
if (read_result.isErr()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sbr = read_result.unwrap();
|
|
|
|
|
|
|
|
ssize_t wrc;
|
|
|
|
|
|
|
|
last_woff = woff;
|
|
|
|
if (timestamp) {
|
|
|
|
wrc = write_timestamp(this->pp_fd, woff);
|
|
|
|
if (wrc == -1) {
|
|
|
|
perror("Unable to write to output file for stdin");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
woff += wrc;
|
|
|
|
}
|
2013-07-05 16:14:39 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
/* Need to do pwrite here since the fd is used by the main
|
|
|
|
* lnav process as well.
|
|
|
|
*/
|
|
|
|
wrc = pwrite(
|
|
|
|
this->pp_fd, sbr.get_data(), sbr.length(), woff);
|
2013-07-05 16:14:39 +00:00
|
|
|
if (wrc == -1) {
|
|
|
|
perror("Unable to write to output file for stdin");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
woff += wrc;
|
2022-03-16 22:38:08 +00:00
|
|
|
|
|
|
|
last_range = li.li_file_range;
|
|
|
|
if (sbr.get_data()[sbr.length() - 1] != '\n'
|
|
|
|
&& (last_range.next_offset() != lb.get_file_size()))
|
|
|
|
{
|
|
|
|
woff = last_woff;
|
|
|
|
}
|
2013-07-05 16:14:39 +00:00
|
|
|
}
|
2022-03-16 22:38:08 +00:00
|
|
|
} while (lb.is_pipe() && !lb.is_pipe_closed());
|
2013-07-05 16:14:39 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
if (timestamp) {
|
|
|
|
ssize_t wrc;
|
|
|
|
|
|
|
|
wrc = write_timestamp(this->pp_fd, woff);
|
2013-05-03 06:02:03 +00:00
|
|
|
if (wrc == -1) {
|
|
|
|
perror("Unable to write to output file for stdin");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
woff += wrc;
|
2022-03-16 22:38:08 +00:00
|
|
|
wrc = pwrite(
|
|
|
|
this->pp_fd, STDIN_EOF_MSG, strlen(STDIN_EOF_MSG), woff);
|
|
|
|
if (wrc == -1) {
|
|
|
|
perror("Unable to write to output file for stdin");
|
|
|
|
break;
|
2014-02-19 13:58:31 +00:00
|
|
|
}
|
2013-05-28 04:35:00 +00:00
|
|
|
}
|
2013-05-03 06:02:03 +00:00
|
|
|
}
|
2022-03-16 22:38:08 +00:00
|
|
|
_exit(0);
|
|
|
|
break;
|
2013-05-28 04:35:00 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-09-14 01:07:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool
|
|
|
|
piper_proc::has_exited()
|
2013-10-11 13:22:29 +00:00
|
|
|
{
|
|
|
|
if (this->pp_child > 0) {
|
|
|
|
int rc, status;
|
|
|
|
|
|
|
|
rc = waitpid(this->pp_child, &status, WNOHANG);
|
|
|
|
if (rc == -1 || rc == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->pp_child = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
piper_proc::~piper_proc()
|
|
|
|
{
|
|
|
|
if (this->pp_child > 0) {
|
2013-05-28 04:35:00 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
kill(this->pp_child, SIGTERM);
|
|
|
|
while (waitpid(this->pp_child, &status, 0) < 0 && (errno == EINTR)) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->pp_child = -1;
|
2009-09-14 01:07:32 +00:00
|
|
|
}
|
|
|
|
}
|