| 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_SYSTEM_H__ |
|---|
| 33 |
#define __COMMON_SYNC_SYSTEM_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <vector> |
|---|
| 37 |
#include <whisperlib/common/base/types.h> |
|---|
| 38 |
#include <whisperlib/common/base/callback.h> |
|---|
| 39 |
|
|---|
| 40 |
#include <whisperlib/common/sync/thread.h> |
|---|
| 41 |
#include <whisperlib/common/sync/event.h> |
|---|
| 42 |
|
|---|
| 43 |
namespace process { |
|---|
| 44 |
|
|---|
| 45 |
// A class that executes a shell command or an external process using the shell. |
|---|
| 46 |
// It uses the ::system(..) function. |
|---|
| 47 |
|
|---|
| 48 |
class System { |
|---|
| 49 |
public: |
|---|
| 50 |
static const int kInvalidExitValue = 1000; |
|---|
| 51 |
static const int kErrorExitValue = 1001; |
|---|
| 52 |
|
|---|
| 53 |
// constructs a process from a shell command |
|---|
| 54 |
explicit System(const char* cmd); |
|---|
| 55 |
explicit System(const string& cmd); |
|---|
| 56 |
~System(); |
|---|
| 57 |
|
|---|
| 58 |
// Starts executing the command or starts the process. |
|---|
| 59 |
// Returns success status. On failure, call GetLastSystemError() for |
|---|
| 60 |
// errno code. |
|---|
| 61 |
bool Start(); |
|---|
| 62 |
|
|---|
| 63 |
// Wait for the running process to terminate. |
|---|
| 64 |
// Returns: |
|---|
| 65 |
// true: the process terminated. exit_status contains the |
|---|
| 66 |
// process exit status. |
|---|
| 67 |
// false: timeout or the process was not started. Call GetLastSystemError() |
|---|
| 68 |
// for errno code. |
|---|
| 69 |
bool Wait(uint32 timeout_ms, int* exit_status); |
|---|
| 70 |
|
|---|
| 71 |
// The callback parameter is the process exit status. |
|---|
| 72 |
typedef Callback1<int> ExitCallback; |
|---|
| 73 |
|
|---|
| 74 |
// Sets a callback to be run when the process terminated. |
|---|
| 75 |
void SetExitCallback(ExitCallback* exit_callback); |
|---|
| 76 |
|
|---|
| 77 |
private: |
|---|
| 78 |
// Test if the process is running. |
|---|
| 79 |
bool IsRunning() const; |
|---|
| 80 |
|
|---|
| 81 |
// Kills the process. |
|---|
| 82 |
void Kill(); |
|---|
| 83 |
|
|---|
| 84 |
// Executor thread. |
|---|
| 85 |
void ExecutorRun(); |
|---|
| 86 |
|
|---|
| 87 |
private: |
|---|
| 88 |
const string cmd_; |
|---|
| 89 |
int exit_status_; |
|---|
| 90 |
|
|---|
| 91 |
ExitCallback* exit_callback_; |
|---|
| 92 |
|
|---|
| 93 |
// callback to ExecutorRun |
|---|
| 94 |
Closure* executor_run_callback_; |
|---|
| 95 |
// The thread which runns the process and wait(blocking) |
|---|
| 96 |
// for process termination. |
|---|
| 97 |
thread::Thread executor_thread_; |
|---|
| 98 |
// Signaled by the inner thread after initialization. |
|---|
| 99 |
// Used by the application to wait for thread initialization. |
|---|
| 100 |
synch::Event executor_thread_start_; |
|---|
| 101 |
// Signaled by the inner thread on termination. |
|---|
| 102 |
// Used by the application to wait (with timeout) for thread termination. |
|---|
| 103 |
synch::Event executor_thread_stop_; |
|---|
| 104 |
// true while the executor_thread_ is running (i.e. the system command is |
|---|
| 105 |
// running), false otherwise. |
|---|
| 106 |
bool executor_running_; |
|---|
| 107 |
|
|---|
| 108 |
private: |
|---|
| 109 |
DISALLOW_EVIL_CONSTRUCTORS(System); |
|---|
| 110 |
}; |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
#endif // __COMMON_SYNC_SYSTEM_H__ |
|---|