1 /** 2 * Copyright (c) 2007-2012, Timothy Stack 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * * Neither the name of Timothy Stack nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * @file piper_proc.hh 30 */ 31 32 #ifndef piper_proc_hh 33 #define piper_proc_hh 34 35 #include <string> 36 #include <sys/types.h> 37 #include "auto_fd.hh" 38 39 /** 40 * Creates a subprocess that reads data from a pipe and writes it to a file so 41 * lnav can treat it like any other file and do preads. 42 * 43 * TODO: Add support for gzipped files. 44 */ 45 class piper_proc { 46 public: 47 class error 48 : public std::exception { 49 public: error(int err)50 error(int err) 51 : e_err(err) { }; 52 53 int e_err; 54 }; 55 56 /** 57 * Forks a subprocess that will read data from the given file descriptor 58 * and write it to a temporary file. 59 * 60 * @param pipefd The file descriptor to read the file contents from. 61 * @param timestamp True if an ISO 8601 timestamp should be prepended onto 62 * the lines read from pipefd. 63 * @param filefd The descriptor for the backing file. 64 */ 65 piper_proc(int pipefd, bool timestamp, int filefd); 66 67 bool has_exited(); 68 69 /** 70 * Terminates the child process. 71 */ 72 virtual ~piper_proc(); 73 74 /** @return The file descriptor for the temporary file. */ get_fd()75 auto_fd get_fd() { return std::move(this->pp_fd); }; 76 get_child_pid() const77 pid_t get_child_pid() const { return this->pp_child; }; 78 79 private: 80 /** A file descriptor that refers to the temporary file. */ 81 auto_fd pp_fd; 82 83 /** The child process' pid. */ 84 pid_t pp_child; 85 }; 86 #endif 87