Fawkes API  Fawkes Development Version
proc.h
1 
2 /***************************************************************************
3  * proc.h - Sub-process facilities
4  *
5  * Created: Mon Aug 18 16:54:47 2014
6  * Copyright 2014 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifndef __PLUGINS_OPENPRS_UTILS_PROC_H_
24 #define __PLUGINS_OPENPRS_UTILS_PROC_H_
25 
26 #include <logging/logger.h>
27 
28 #include <boost/asio.hpp>
29 #include <string>
30 #include <thread>
31 
32 namespace fawkes {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 class Logger;
38 
40 {
41  public:
42  SubProcess(const char *progname, const char *file, const char *argv[], const char *envp[]);
43  SubProcess(const char *progname, const char *file, const char *argv[], const char *envp[],
44  fawkes::Logger *logger);
45  ~SubProcess();
46 
47  /** Get PID of sub-process.
48  * @return process ID of sub-process. */
49  pid_t pid() const
50  { return pid_; }
51 
52  /** Get stdin pipe file descriptor.
53  * @return stdin pipe file descriptor, only valid for writing. */
54  int pipe_stdin_w() const
55  { return pipe_stdin_w_; }
56 
57  /** Get stdout pipe file descriptor.
58  * @return stdout pipe file descriptor, only valid for reading. */
59  int pipe_stdout_r() const
60  { return pipe_stdout_r_; }
61 
62  /** Get stderr pipe file descriptor.
63  * @return stderr pipe file descriptor, only valid for reading. */
64  int pipe_stderr_r() const
65  { return pipe_stderr_r_; }
66 
67  /** Get stdin stream descriptor.
68  * @return stdin stream descriptor, only valid for writing. */
69  boost::asio::posix::stream_descriptor & sd_stdin()
70  { return sd_stdin_; }
71 
72  /** Get stdout stream descriptor.
73  * @return stdout stream descriptor, only valid for reading. */
74  boost::asio::posix::stream_descriptor & sd_stdout()
75  { return sd_stdout_; }
76 
77  /** Get stderr stream descriptor.
78  * @return stderr stream descriptor, only valid for reading. */
79  boost::asio::posix::stream_descriptor & sd_stderr()
80  { return sd_stderr_; }
81 
82  void kill(int signum);
83  void check_proc();
84 
85  private:
86  pid_t run_proc(const char *file, const char *argv[], const char *envp[],
87  int & pipe_stdin_w, int & pipe_stdout_r, int & pipe_stderr_r);
88 
89  void run_proc(const char *file, const char *argv[], const char *envp[]);
90 
91  void start_log(const char *logname, fawkes::Logger::LogLevel log_level,
92  boost::asio::posix::stream_descriptor &sd, boost::asio::streambuf &buf);
93  void handle_log_line(const char *logname, fawkes::Logger::LogLevel log_level,
94  boost::asio::posix::stream_descriptor &sd, boost::asio::streambuf &buf,
95  boost::system::error_code ec, size_t bytes_read);
96 
97 
98  private:
99  std::string progname_;
100 
101  pid_t pid_;
102  int pipe_stdin_w_;
103  int pipe_stdout_r_;
104  int pipe_stderr_r_;
105 
106  boost::asio::io_service io_service_;
107  std::thread io_service_thread_;
108  boost::asio::io_service::work io_service_work_;
109 
110  fawkes::Logger *logger_;
111 
112  boost::asio::posix::stream_descriptor sd_stdin_;
113  boost::asio::posix::stream_descriptor sd_stdout_;
114  boost::asio::posix::stream_descriptor sd_stderr_;
115 
116  boost::asio::streambuf buf_stdout_;
117  boost::asio::streambuf buf_stderr_;
118 
119 };
120 
121 } // end namespace fawkes
122 
123 #endif
LogLevel
Log level.
Definition: logger.h:45
SubProcess(const char *progname, const char *file, const char *argv[], const char *envp[])
Constructor.
Definition: proc.cpp:56
Fawkes library namespace.
boost::asio::posix::stream_descriptor & sd_stdout()
Get stdout stream descriptor.
Definition: proc.h:74
int pipe_stdout_r() const
Get stdout pipe file descriptor.
Definition: proc.h:59
void kill(int signum)
Send a signal to the process.
Definition: proc.cpp:100
void check_proc()
Check if the process is still alive.
Definition: proc.cpp:240
int pipe_stderr_r() const
Get stderr pipe file descriptor.
Definition: proc.h:64
int pipe_stdin_w() const
Get stdin pipe file descriptor.
Definition: proc.h:54
~SubProcess()
Destructor.
Definition: proc.cpp:88
boost::asio::posix::stream_descriptor & sd_stderr()
Get stderr stream descriptor.
Definition: proc.h:79
pid_t pid() const
Get PID of sub-process.
Definition: proc.h:49
Sub-process execution with stdin/stdout/stderr redirection.
Definition: proc.h:39
boost::asio::posix::stream_descriptor & sd_stdin()
Get stdin stream descriptor.
Definition: proc.h:69
Interface for logging.
Definition: logger.h:34