nidas  v1.2-1520
Process.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_UTIL_PROCESS_H
28 #define NIDAS_UTIL_PROCESS_H
29 
30 #include "ThreadSupport.h"
31 #include "IOException.h"
32 #include "auto_ptr.h"
33 
34 #include <string>
35 #include <vector>
36 #include <map>
37 #include <memory>
38 #include <signal.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 
42 #include <nidas/Config.h>
43 
44 #ifdef HAVE_SYS_CAPABILITY_H
45 #include <sys/capability.h>
46 #endif
47 
48 #include <ext/stdio_filebuf.h>
49 
50 namespace nidas { namespace util {
51 
58 class Process {
59 public:
60 
72  Process(pid_t pid);
73 
77  Process();
78 
85  Process(const Process& x);
86 
93  Process& operator=(const Process& x);
94 
100  ~Process();
101 
125  static Process spawn(const std::string& cmd,
126  const std::vector<std::string>& args,
127  const std::map<std::string, std::string>& env,
128  int niceval=0) throw(IOException);
129 
139  static Process spawn(const std::string& cmd,
140  const std::vector<std::string>& args,
141  int niceval=0) throw(IOException);
142 
147  static Process spawn(const std::string& cmd) throw(IOException);
148 
152  void kill(int signal) throw(IOException);
153 
166  int wait(bool hang, int *status) throw(IOException);
167 
175  int getInFd() const { return _infd; }
176 
182  std::ostream& inStream() { return *_instream_ap.get(); }
183 
190  void closeIn();
191 
198  int getOutFd() const { return _outfd; }
199 
206  std::istream& outStream() { return *_outstream_ap.get(); }
207 
212  void closeOut();
213 
220  int getErrFd() const { return _errfd; }
221 
228  std::istream& errStream() { return *_errstream_ap.get(); }
229 
234  void closeErr();
235 
241  pid_t getPid() const { return _pid; }
242 
266  static pid_t checkPidFile(const std::string& pidFile)
267  throw(IOException);
268 
273  static void removePidFile();
274 
281  static void addEffectiveCapability(int cap) throw(Exception);
282 
283  static void clearEffectiveCapability(int cap) throw(Exception);
284 
288  static bool getEffectiveCapability(int cap) throw(Exception);
289 
290  static std::string expandEnvVars(std::string input);
291 
297  static bool getEnvVar(const std::string& name,std::string& value);
298 
299  static void setEnvVar(const std::string& name,const std::string& value);
300 
305  static void clearEnv();
306 
311  static unsigned long getVMemSize();
312 
319  static unsigned long getMaxRSS();
320 
321 private:
322 
323  static std::string _pidFile;
324 
325  pid_t _pid;
326 
334  mutable int _infd;
335 
336  void setInFd(int val);
337 
342  mutable auto_ptr<__gnu_cxx::stdio_filebuf<char> > _inbuf_ap;
343 
344  mutable auto_ptr<std::ostream> _instream_ap;
345 
350  mutable int _outfd;
351 
352  void setOutFd(int val);
353 
354  mutable auto_ptr<__gnu_cxx::stdio_filebuf<char> > _outbuf_ap;
355 
356  mutable auto_ptr<std::istream> _outstream_ap;
357 
362  mutable int _errfd;
363 
364  void setErrFd(int val);
365 
366  mutable auto_ptr<__gnu_cxx::stdio_filebuf<char> > _errbuf_ap;
367 
368  mutable auto_ptr<std::istream> _errstream_ap;
369 
370  static std::map<std::string,char*> _environment;
371 
372  static Mutex _envLock;
373 };
374 
375 }} // namespace nidas namespace util
376 
377 #endif
std::istream & errStream()
Get the istream of the pipe that is connected to the standard error of the Process.
Definition: Process.h:228
int _outfd
File descriptor that is connected via a pipe to the standard out file descriptor of a spawned process...
Definition: Process.h:350
int getOutFd() const
Get the file descriptor of the pipe that is connected to the standard out of the Process.
Definition: Process.h:198
std::ostream & inStream()
Get the ostream of the pipe that is connected to the standard in of the Process.
Definition: Process.h:182
static bool getEffectiveCapability(int cap)
Check if this process has an effective capability.
Definition: Process.cc:533
void setErrFd(int val)
Definition: Process.cc:151
static std::string _pidFile
Definition: Process.h:323
pid_t getPid() const
The process id of the Process.
Definition: Process.h:241
Process provides an encapsulation of a spawned process, allowing the parent process to perform I/O wi...
Definition: Process.h:58
void closeOut()
Close the file descriptor and istream of the pipe connected to the standard out of the Process...
Definition: Process.cc:167
auto_ptr< std::istream > _errstream_ap
Definition: Process.h:368
static void addEffectiveCapability(int cap)
Add an effective capability to this process.
Definition: Process.cc:457
static unsigned long getMaxRSS()
Return the maximum resident set size of the current process, in bytes.
Definition: Process.cc:704
static void clearEnv()
Remove any environment settings made by setEnvVar and release any memory allocated to put those setti...
Definition: Process.cc:652
int wait(bool hang, int *status)
Do a system wait on a process.
Definition: Process.cc:364
static std::string expandEnvVars(std::string input)
Definition: Process.cc:567
Definition: Exception.h:35
auto_ptr< __gnu_cxx::stdio_filebuf< char > > _errbuf_ap
Definition: Process.h:366
auto_ptr< __gnu_cxx::stdio_filebuf< char > > _outbuf_ap
Definition: Process.h:354
int getInFd() const
Get the file descriptor of the pipe that is connected to the standard in of the Process.
Definition: Process.h:175
static pid_t checkPidFile(const std::string &pidFile)
Check if another process is running, using the named process id file.
Definition: Process.cc:386
int _infd
File descriptor that is connected via a pipe to the standard in file descriptor of a spawned process...
Definition: Process.h:334
static void clearEffectiveCapability(int cap)
Definition: Process.cc:496
int _errfd
File descriptor that is connected via a pipe to the standard error file descriptor of a spawned proce...
Definition: Process.h:362
static std::map< std::string, char * > _environment
Definition: Process.h:370
int getErrFd() const
Get the file descriptor of the pipe that is connected to the standard error of the Process...
Definition: Process.h:220
void setOutFd(int val)
Definition: Process.cc:143
pid_t _pid
Definition: Process.h:325
static Process spawn(const std::string &cmd, const std::vector< std::string > &args, const std::map< std::string, std::string > &env, int niceval=0)
Fork and execute a command and associated arguments and environment.
Definition: Process.cc:184
Definition: IOException.h:37
std::istream & outStream()
Get the istream of the pipe that is connected to the standard out of the Process. ...
Definition: Process.h:206
void kill(int signal)
Send a signal to the process.
Definition: Process.cc:352
auto_ptr< __gnu_cxx::stdio_filebuf< char > > _inbuf_ap
GNU extension filebuf which is needed to create a ostream from a file descriptor. ...
Definition: Process.h:342
static Mutex _envLock
Definition: Process.h:372
auto_ptr< std::istream > _outstream_ap
Definition: Process.h:356
~Process()
Destructor, which does very little.
Definition: Process.cc:131
static bool getEnvVar(const std::string &name, std::string &value)
Get an environment variable given a variable name like &quot;HOST&quot;, without the &#39;$&#39;, or any brackets...
Definition: Process.cc:623
static void removePidFile()
Remove the pid file.
Definition: Process.cc:450
Process()
Default constructor.
Definition: Process.cc:77
void closeIn()
Close the file descriptor and ostream of the pipe connected to the standard in of the Process...
Definition: Process.cc:159
static void setEnvVar(const std::string &name, const std::string &value)
Definition: Process.cc:632
void setInFd(int val)
Definition: Process.cc:135
void closeErr()
Close the file descriptor and istream of the pipe connected to the standard error of the Process...
Definition: Process.cc:175
auto_ptr< std::ostream > _instream_ap
Definition: Process.h:344
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
static unsigned long getVMemSize()
Return the virtual memory size in bytes of the current process, as read from the /proc/pid/stat file...
Definition: Process.cc:681
Process & operator=(const Process &x)
Assignment operator.
Definition: Process.cc:105