root/trunk/whisperlib/common/sync/process.h

Revision 7, 5.4 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #ifndef __COMMON_SYNC_PROCESS_H__
33 #define __COMMON_SYNC_PROCESS_H__
34
35 #include <string>
36 #include <vector>
37
38 #include <whisperlib/common/base/types.h>
39 #include <whisperlib/common/base/callback.h>
40 #include <whisperlib/common/base/common.h>
41 #include <whisperlib/common/sync/thread.h>
42 #include <whisperlib/common/sync/event.h>
43
44 namespace process {
45
46 class Process {
47  public:
48   static const pid_t kInvalidPid = pid_t(-1);
49
50   // the process is still running
51   static const int kInvalidExitValue   = 1000;
52   // error retrieving process exit code
53   static const int kErrorExitValue     = 1001;
54   // detached from process
55   static const int kDetachedExitValue  = 1002;
56
57
58   // constructs a process from executable image and parameters
59   // arg[0] should be your first argument, and NOT the program name
60   // The constructors which don't specify "envp" will use current "environ".
61   //
62   // IMPORTANT NOTE: You MUST set the last argument as NULL in all cases
63   //   e.g.
64   //     new Process("/usr/sleep", "10", NULL);
65   //   or with no arguments:
66   //     new Process("/usr/bin/uname", NULL, NULL);
67   //     // The first NULL is enough for the call to behave correctly,
68   //     // the second NULL exists just to satisfy
69   //     // the G_GNUC_NULL_TERMINATED sentinel attribute.
70   //
71   Process(const char* path, const char* arg, ...) G_GNUC_NULL_TERMINATED;
72   Process(const char* path, char* const argv[], char* const envp[]);
73   Process(const string& path, const vector<string>& argv);
74   Process(const string& path, const
75           vector<string>& argv,
76           const vector<string>& envp);
77   // binds to an existing process. You still have to call Start() to bind.
78   explicit Process(pid_t pid);
79   ~Process();
80
81   // Either starts the process or binds to an existing process (if you
82   // specified a pid).
83   // Returns success status. On failure, call GetLastSystemError() for
84   // errno code.
85   bool Start();
86
87   // Returns the running process pid.
88   // If the process is not started, returns INVALID_PID_VALUE.
89   pid_t Pid() const;
90
91   // Sends a signal to the running process.
92   // Returns success status. On failure, call GetLastSystemError() for
93   // errno code.
94   bool Signal(int signum);
95
96   // Kills the process.
97   void Kill();
98
99   // Detaches from the executing process. So you can delete this
100   // object and the process keeps running.
101   void Detach();
102
103   //  Wait for the running process to terminate.
104   // Returns:
105   //  true: the process terminated.
106   //        exit_status contains the process exit status.
107   //  false: timeout or the process was not started. Call GetLastSystemError()
108   //         for errno code.
109   bool Wait(uint32 timeout_ms, int* exit_status);
110
111   // The callback parameter is the process exit status.
112   typedef Callback1<int> ExitCallback;
113
114   // Sets a callback to be run when the process terminated.
115   void SetExitCallback(ExitCallback* exit_callback);
116
117  private:
118   // Test if the process has been started once.
119   bool IsStarted() const;
120
121   // Tests if the executor thread is running (i.e. the external process
122   // is running).
123   bool IsRunning() const;
124
125   // Executor thread.
126   void ExecutorRun();
127
128  private:
129   const string path_;
130   vector<string> argv_;
131   vector<string> envp_;
132   const pid_t bind_pid_;
133   pid_t pid_;
134   int exit_status_;
135
136   ExitCallback* exit_callback_;
137
138   // The thread which runns the process and wait(blocking)
139   // for process termination.
140   thread::Thread executor_thread_;
141   // Signaled by the inner thread after initialization.
142   // Used by the application to wait for thread initialization.
143   synch::Event executor_thread_start_;
144   // Signaled by the inner thread on termination.
145   // Used by the application to wait (with timeout) for thread termination.
146   synch::Event executor_thread_stop_;
147   // when signaled, tells the executor thread it should terminate
148   synch::Event executor_end_;
149
150  private:
151   DISALLOW_EVIL_CONSTRUCTORS(Process);
152 };
153 }
154
155 #endif  //  __COMMON_SYNC_PROCESS_H__
Note: See TracBrowser for help on using the browser.