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 
34 class Logger;
35 
37 {
38 public:
39  SubProcess(const char *progname, const char *file, const char *argv[], const char *envp[]);
40  SubProcess(const char * progname,
41  const char * file,
42  const char * argv[],
43  const char * envp[],
44  fawkes::Logger *logger);
45  SubProcess(const std::string & progname,
46  const std::string & file,
47  const std::vector<std::string> &argv,
48  const std::vector<std::string> &envp);
49  SubProcess(const std::string & progname,
50  const std::string & file,
51  const std::vector<std::string> &argv,
52  const std::vector<std::string> &envp,
53  fawkes::Logger * logger);
54  ~SubProcess();
55 
56  /** Get PID of sub-process.
57  * @return process ID of sub-process. */
58  pid_t
59  pid() const
60  {
61  return pid_;
62  }
63 
64  /** Get stdin pipe file descriptor.
65  * @return stdin pipe file descriptor, only valid for writing. */
66  int
67  pipe_stdin_w() const
68  {
69  return pipe_stdin_w_;
70  }
71 
72  /** Get stdout pipe file descriptor.
73  * @return stdout pipe file descriptor, only valid for reading. */
74  int
75  pipe_stdout_r() const
76  {
77  return pipe_stdout_r_;
78  }
79 
80  /** Get stderr pipe file descriptor.
81  * @return stderr pipe file descriptor, only valid for reading. */
82  int
83  pipe_stderr_r() const
84  {
85  return pipe_stderr_r_;
86  }
87 
88  /** Get stdin stream descriptor.
89  * @return stdin stream descriptor, only valid for writing. */
90  boost::asio::posix::stream_descriptor &
92  {
93  return sd_stdin_;
94  }
95 
96  /** Get stdout stream descriptor.
97  * @return stdout stream descriptor, only valid for reading. */
98  boost::asio::posix::stream_descriptor &
100  {
101  return sd_stdout_;
102  }
103 
104  /** Get stderr stream descriptor.
105  * @return stderr stream descriptor, only valid for reading. */
106  boost::asio::posix::stream_descriptor &
108  {
109  return sd_stderr_;
110  }
111 
112  void kill(int signum);
113  void check_proc();
114  bool alive();
115 
116  int exit_status();
117 
118 private:
119  pid_t run_proc(const char *file,
120  const char *argv[],
121  const char *envp[],
122  int & pipe_stdin_w,
123  int & pipe_stdout_r,
124  int & pipe_stderr_r);
125 
126  void run_proc(const char *file, const char *argv[], const char *envp[]);
127 
128  void start_log(const char * logname,
129  fawkes::Logger::LogLevel log_level,
130  boost::asio::posix::stream_descriptor &sd,
131  boost::asio::streambuf & buf);
132  void handle_log_line(const char * logname,
133  fawkes::Logger::LogLevel log_level,
134  boost::asio::posix::stream_descriptor &sd,
135  boost::asio::streambuf & buf,
136  boost::system::error_code ec,
137  size_t bytes_read);
138 
139 private:
140  std::string progname_;
141 
142  pid_t pid_;
143  int pipe_stdin_w_;
144  int pipe_stdout_r_;
145  int pipe_stderr_r_;
146 
147  boost::asio::io_service io_service_;
148  std::thread io_service_thread_;
149  boost::asio::io_service::work io_service_work_;
150 
151  fawkes::Logger *logger_;
152 
153  boost::asio::posix::stream_descriptor sd_stdin_;
154  boost::asio::posix::stream_descriptor sd_stdout_;
155  boost::asio::posix::stream_descriptor sd_stderr_;
156 
157  boost::asio::streambuf buf_stdout_;
158  boost::asio::streambuf buf_stderr_;
159 
160  int exit_status_;
161 };
162 
163 } // end namespace fawkes
164 
165 #endif
LogLevel
Log level.
Definition: logger.h:51
SubProcess(const char *progname, const char *file, const char *argv[], const char *envp[])
Constructor.
Definition: proc.cpp:55
Fawkes library namespace.
boost::asio::posix::stream_descriptor & sd_stdout()
Get stdout stream descriptor.
Definition: proc.h:99
int pipe_stdout_r() const
Get stdout pipe file descriptor.
Definition: proc.h:75
void kill(int signum)
Send a signal to the process.
Definition: proc.cpp:188
void check_proc()
Check if the process is still alive.
Definition: proc.cpp:375
int pipe_stderr_r() const
Get stderr pipe file descriptor.
Definition: proc.h:83
int pipe_stdin_w() const
Get stdin pipe file descriptor.
Definition: proc.h:67
int exit_status()
Get exit status of process once it ended.
Definition: proc.cpp:365
~SubProcess()
Destructor.
Definition: proc.cpp:177
bool alive()
Check if process is alive.
Definition: proc.cpp:353
boost::asio::posix::stream_descriptor & sd_stderr()
Get stderr stream descriptor.
Definition: proc.h:107
pid_t pid() const
Get PID of sub-process.
Definition: proc.h:59
Sub-process execution with stdin/stdout/stderr redirection.
Definition: proc.h:36
boost::asio::posix::stream_descriptor & sd_stdin()
Get stdin stream descriptor.
Definition: proc.h:91
Interface for logging.
Definition: logger.h:41