rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
exec.h
Go to the documentation of this file.
1 #ifndef __exec_h__
2 #define __exec_h__
3 
4 #include <iostream>
5 #include <vector>
6 #include <string>
7 
8 #ifdef HAVE_SYS_TYPES_H
9 #include <sys/types.h>
10 #endif
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #ifdef HAVE_SIGNAL_H
15 #include <signal.h>
16 #endif
17 
18 #include "asserts.h"
19 
20 /** Fork a child process or execute an external program */
21 class execute
22 {
23 public:
24  execute();
25  ~execute();
26 
27  void fork(void);
28  void clear(void);
29  bool is_child(void);
30  bool is_parent(void);
31  pid_t my_pid(void);
32 
33  // Child-specific functions:
34  void exit(int code = 0); // For child, exit with code
35  void reroute_stdio(void); // Re-route stdin/out/err to parent
36 
37  // Parent-specific functions:
38  pid_t child_pid(void); // PID of child process
39  void signal_child(int signal_no); // Send signal to child
40  void hup_child(void); // Send child HUP signal
41  void kill_child(void); // Send child KILL signal
42  void wait(void); // Wait for child to exit
43  bool child_started(void) const; // True if child has been started
44  bool child_running(void); // True if child is still running
45  bool child_exited(void); // True if child has exited
46  bool child_exited_normally(void); // Exit code = 0, signal = ?
47  bool child_signaled(void); // Exit code = ?, signal = yes
48  bool child_exited_success(void); // Exit code = 0, signal = no
49  int child_exit_code(void); // Child's exit code
50  int child_signal_no(void); // Child's uncaught signal
51 
52  // Command execution
53  void exec(const std::string command);
54  void exec(const std::string binary, const std::vector<std::string> argv);
55 
56  int in_fd(void);
57  int out_fd(void);
58  int err_fd(void);
59  bool in_ready(void);
60  bool out_ready(void);
61  bool err_ready(void);
62 
63  bool in_eof(void);
64  bool out_eof(void);
65  bool err_eof(void);
66  int in_read(char* buf, const int len);
67  int in_write(const char* buf, const int len);
68  int out_read(char* buf, const int len);
69  int out_write(const char* buf, const int len);
70  int err_read(char* buf, const int len);
71  int err_write(const char* buf, const int len);
72 
73  void print(std::ostream& out);
74 
75 private:
76  int m_fd1[2];
77  int m_fd2[2];
78  int m_fd3[2];
79  pid_t m_pid;
80  int m_status;
81  bool m_in_eof;
82  bool m_out_eof;
83  bool m_err_eof;
85 
86  pid_t check_child_(void);
87  bool check_write_ready_(int fd);
88  bool check_read_ready_(int fd);
89 };
90 
91 std::ostream& operator << (std::ostream& out, execute& exe);
92 
93 #endif
void reroute_stdio(void)
Called by the child to reroute the child's stdin, stdout, and stderr to the parent.
Definition: exec.cc:138
execute()
C'tor.
Definition: exec.cc:33
int child_signal_no(void)
If the child was signaled, return the signal number.
Definition: exec.cc:284
~execute()
D'tor.
Definition: exec.cc:39
pid_t child_pid(void)
Returns the child's PID.
Definition: exec.cc:170
int m_fd2[2]
Definition: exec.h:77
int m_fd3[2]
Definition: exec.h:78
bool err_eof(void)
Check for err EOF.
Definition: exec.cc:533
bool out_eof(void)
Check for output EOF.
Definition: exec.cc:527
void fork(void)
Fork a child process.
Definition: exec.cc:72
void kill_child(void)
Send a KILL signal to the child.
Definition: exec.cc:188
int err_read(char *buf, const int len)
Allow parent to read from err_fd()
Definition: exec.cc:607
void exit(int code=0)
Called by the child to exit with a particular code.
Definition: exec.cc:130
void signal_child(int signal_no)
Send a signal to the child.
Definition: exec.cc:176
pid_t m_pid
Definition: exec.h:79
bool err_ready(void)
Check I/O for output.
Definition: exec.cc:508
bool out_ready(void)
Check I/O for output.
Definition: exec.cc:488
bool child_started(void) const
Returns true if the child has been started.
Definition: exec.cc:214
int in_write(const char *buf, const int len)
Allow parent to write output to in_fd()
Definition: exec.cc:556
int out_fd(void)
Return a file descriptor for I/O between parent a child.
Definition: exec.cc:327
void hup_child(void)
Send a HUP signal to the child.
Definition: exec.cc:182
bool m_out_eof
Definition: exec.h:82
bool child_running(void)
Returns true if the child is running.
Definition: exec.cc:220
int m_status
Definition: exec.h:80
void clear(void)
Reset the execute class to default values, kill the child processif one is running.
Definition: exec.cc:46
bool child_exited_success(void)
Returns true if the child returned exit code 0 and no caught signals.
Definition: exec.cc:263
bool m_in_eof
Definition: exec.h:81
void exec(const std::string command)
Execute a command, rerouting stdin, stdout, and stderr to parent.
Definition: exec.cc:390
int err_write(const char *buf, const int len)
Allow child to write to err_fd()
Definition: exec.cc:624
int out_read(char *buf, const int len)
Allow parent to read out_fd()
Definition: exec.cc:573
bool m_err_eof
Definition: exec.h:83
bool child_signaled(void)
Returns true if the child was signaled.
Definition: exec.cc:252
int child_exit_code(void)
Return the child's exit code.
Definition: exec.cc:273
void print(std::ostream &out)
Dump execute object information – used for debugging.
Definition: exec.cc:641
bool child_exited_normally(void)
Returns true if the child has exited normally.
Definition: exec.cc:242
bool check_read_ready_(int fd)
Return true if the file descriptor has input ready to be read.
Definition: exec.cc:375
Fork a child process or execute an external program.
Definition: exec.h:21
pid_t check_child_(void)
Check the child's status.
Definition: exec.cc:203
int in_read(char *buf, const int len)
Allow child to read input from in_fd()
Definition: exec.cc:539
bool in_ready(void)
Check I/O for input.
Definition: exec.cc:468
bool in_eof(void)
Check for input EOF.
Definition: exec.cc:521
bool child_exited(void)
Returns true of the child has existed.
Definition: exec.cc:231
pid_t my_pid(void)
Returns the PID.
Definition: exec.cc:120
int err_fd(void)
Return a file descriptor for I/O between parent and child.
Definition: exec.cc:347
int out_write(const char *buf, const int len)
Allow child to write to out_fd()
Definition: exec.cc:590
bool check_write_ready_(int fd)
Return true if the file descriptor is ready to be written to.
Definition: exec.cc:360
void wait(void)
Wait for the child to exit.
Definition: exec.cc:194
std::ostream & operator<<(std::ostream &out, execute &exe)
Convenience function to call execute::print()
Definition: exec.cc:664
bool is_child(void)
Returns true if called by the child.
Definition: exec.cc:101
int m_fd1[2]
Definition: exec.h:76
bool is_parent(void)
Returns true if called by the parent.
Definition: exec.cc:110
int in_fd(void)
Return a file descriptor for I/O between parent and child.
Definition: exec.cc:307
bool m_child_started
Definition: exec.h:84