/usr/include/arc/Run.h is in nordugrid-arc-dev 5.4.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | // -*- indent-tabs-mode: nil -*-
#ifndef __ARC_RUN_H__
#define __ARC_RUN_H__
#include <glibmm.h>
#include <arc/Thread.h>
#include <arc/DateTime.h>
namespace Arc {
class RunPump;
class Pid;
/// This class runs an external executable.
/** It is possible to read from or write to its standard handles or to
* redirect them to std::string elements.
* \ingroup common
* \headerfile Run.h arc/Run.h */
class Run {
friend class RunPump;
private:
Run(const Run&);
Run& operator=(Run&);
protected:
// working directory
std::string working_directory;
// Handles
int stdout_;
int stderr_;
int stdin_;
// Associated string containers
std::string *stdout_str_;
std::string *stderr_str_;
std::string *stdin_str_;
//
bool stdout_keep_;
bool stderr_keep_;
bool stdin_keep_;
// Signal connections
sigc::connection stdout_conn_;
sigc::connection stderr_conn_;
sigc::connection stdin_conn_;
sigc::connection child_conn_;
// PID of child
Pid *pid_;
// Arguments to execute
std::list<std::string> argv_;
std::list<std::string> envp_;
std::list<std::string> envx_;
void (*initializer_func_)(void*);
void *initializer_arg_;
void (*kicker_func_)(void*);
void *kicker_arg_;
// IO handlers
bool stdout_handler(Glib::IOCondition cond);
bool stderr_handler(Glib::IOCondition cond);
bool stdin_handler(Glib::IOCondition cond);
// Child exit handler
void child_handler(Glib::Pid pid, int result);
bool started_;
bool running_;
bool abandoned_;
int result_;
Glib::Mutex lock_;
Glib::Cond cond_;
int user_id_;
int group_id_;
Time run_time_;
Time exit_time_;
public:
/// Constructor prepares object to run cmdline.
Run(const std::string& cmdline);
/// Constructor prepares object to run executable and arguments specified in argv.
Run(const std::list<std::string>& argv);
/// Destructor kills running executable and releases associated resources.
~Run(void);
/// Returns true if object is valid.
operator bool(void) {
return argv_.size() != 0;
}
/// Returns true if object is invalid.
bool operator!(void) {
return argv_.size() == 0;
}
/// Starts running executable. This method may be called only once.
/** \return true if executable started without problems */
bool Start(void);
/// Wait till execution finished or till timeout seconds expires.
/** \return true if execution is complete. */
bool Wait(int timeout);
/// Wait till execution finished.
/** \return true if execution is complete, false if execution was not
started. */
bool Wait(void);
/// Returns exit code of execution.
/** If child process was killed then exit code is -1.
If code is compiled with support for detecting lost child
process this code is -1 also if track of child was lost. */
int Result(void) {
return result_;
}
/// Return true if execution is going on.
bool Running(void);
/// Return when executable was started.
Time RunTime(void) {
return run_time_;
};
/// Return when executable finished executing.
Time ExitTime(void) {
return exit_time_;
};
/// Read from stdout handle of running executable.
/** This method may be used while stdout is directed to string, but the
result is unpredictable.
\param timeout upper limit for which method will block in milliseconds.
Negative means infinite.
\param buf buffer to write the stdout to
\param size size of buf
\return number of read bytes. */
int ReadStdout(int timeout, char *buf, int size);
/// Read from stderr handle of running executable.
/** This method may be used while stderr is directed to string, but the
result is unpredictable.
\param timeout upper limit for which method will block in milliseconds.
Negative means infinite.
\param buf buffer to write the stderr to
\param size size of buf
\return number of read bytes. */
int ReadStderr(int timeout, char *buf, int size);
/// Write to stdin handle of running executable.
/** This method may be used while stdin is directed to string, but the
result is unpredictable.
\param timeout upper limit for which method will block in milliseconds.
Negative means infinite.
\param buf buffer to read the stdin from
\param size size of buf
\return number of written bytes. */
int WriteStdin(int timeout, const char *buf, int size);
/// Associate stdout handle of executable with string.
/** This method must be called before Start(). str object
must be valid as long as this object exists. */
void AssignStdout(std::string& str);
/// Associate stderr handle of executable with string.
/** This method must be called before Start(). str object
must be valid as long as this object exists. */
void AssignStderr(std::string& str);
/// Associate stdin handle of executable with string.
/** This method must be called before Start(). str object
must be valid as long as this object exists. */
void AssignStdin(std::string& str);
/// Keep stdout same as parent's if keep = true.
void KeepStdout(bool keep = true);
/// Keep stderr same as parent's if keep = true.
void KeepStderr(bool keep = true);
/// Keep stdin same as parent's if keep = true.
void KeepStdin(bool keep = true);
/// Closes pipe associated with stdout handle.
void CloseStdout(void);
/// Closes pipe associated with stderr handle.
void CloseStderr(void);
/// Closes pipe associated with stdin handle.
void CloseStdin(void);
//void DumpStdout(void);
//void DumpStderr(void);
/// Assign a function to be called just after process is forked but before execution starts.
void AssignInitializer(void (*initializer_func)(void*), void *initializer_arg);
/// Assign a function to be called just after execution ends
void AssignKicker(void (*kicker_func)(void*), void *kicker_arg);
/// Assign working directory of the running process.
void AssignWorkingDirectory(std::string& wd) {
working_directory = wd;
}
/// Assign uid for the process to run under.
void AssignUserId(int uid) {
user_id_ = uid;
}
/// Assign gid for the process to run under.
void AssignGroupId(int gid) {
group_id_ = gid;
}
/// Add environment variable to be passed to process being run
void AddEnvironment(const std::string& key, const std::string& value) {
AddEnvironment(key+"="+value);
}
/// Add environment variable to be passed to process being run
void AddEnvironment(const std::string& var) {
envp_.push_back(var);
}
/// Remove environment variable to be passed to process being run
void RemoveEnvironment(const std::string& key) {
envx_.push_back(key);
}
/// Kill running executable.
/** First soft kill signal (SIGTERM) is sent to executable. If
after timeout seconds executable is still running it's killed
completely. Currently this method does not work for Windows OS */
void Kill(int timeout);
/// Detach this object from running process.
/** After calling this method instance is not associated with external
process anymore. As result destructor will not kill process. */
void Abandon(void);
/// Call this method after fork() in child process.
/** It will reinitialize internal structures for new environment. Do
not call it in any other case than defined. */
static void AfterFork(void);
};
}
#endif // __ARC_RUN_H__
|