]> git.localhorst.tv Git - blank.git/blob - src/app/Process.hpp
timeouts reading from spawned processes
[blank.git] / src / app / Process.hpp
1 #ifndef BLANK_APP_PROCESS_HPP_
2 #define BLANK_APP_PROCESS_HPP_
3
4 #include <memory>
5 #include <string>
6 #include <vector>
7
8
9 namespace blank {
10
11 class Process {
12
13 public:
14         using Arguments = std::vector<std::string>;
15         using Environment = std::vector<std::string>;
16
17 public:
18         /// launch process executing the file at given path with
19         /// given arguments and environment of parent process
20         Process(
21                 const std::string &path,
22                 const Arguments &args);
23         /// launch process executing the file at given path with
24         /// given arguments and given environment
25         Process(
26                 const std::string &path,
27                 const Arguments &args,
28                 const Environment &env);
29         ~Process();
30
31 public:
32         /// write to the process' input stream
33         /// data is taken from given buffer, at most max_len bytes
34         /// @return the number of bytes written
35         std::size_t WriteIn(const void *buffer, std::size_t max_len);
36         /// close program's input stream
37         void CloseIn();
38
39         /// read from the process' output stream
40         /// data is stored in the given buffer, at most max_len bytes
41         /// timeout is the number of milliseconds to wait for the process
42         /// to produce output, -1 for indefinite
43         /// @return the number of bytes read
44         std::size_t ReadOut(void *buffer, std::size_t max_len, int timeout);
45         /// close program's output stream
46         void CloseOut();
47
48         /// read from the process' error stream
49         /// data is stored in the given buffer, at most max_len bytes
50         /// timeout is the number of milliseconds to wait for the process
51         /// to produce output, -1 for indefinite
52         /// @return the number of bytes read
53         std::size_t ReadErr(void *buffer, std::size_t max_len, int timeout);
54         /// close program's output stream
55         void CloseErr();
56
57         /// ask the process nicely to terminate
58         /// (except on win32)
59         void Terminate();
60         /// check if the process has terminated
61         bool Terminated();
62         /// wait until the process exits and fetch its exit status
63         int Join();
64
65 private:
66         struct Impl;
67         std::unique_ptr<Impl> impl;
68
69 };
70
71 }
72
73 #endif