nidas v1.2.3
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
50namespace nidas { namespace util {
51
58class Process {
59public:
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
127 static Process spawn(const std::string& cmd,
128 const std::vector<std::string>& args,
129 const std::map<std::string, std::string>& env,
130 int niceval=0);
131
143 static Process spawn(const std::string& cmd,
144 const std::vector<std::string>& args,
145 int niceval=0);
146
153 static Process spawn(const std::string& cmd);
154
160 void kill(int signal);
161
176 int wait(bool hang, int *status);
177
185 int getInFd() const { return _infd; }
186
192 std::ostream& inStream() { return *_instream_ap.get(); }
193
200 void closeIn();
201
208 int getOutFd() const { return _outfd; }
209
216 std::istream& outStream() { return *_outstream_ap.get(); }
217
222 void closeOut();
223
230 int getErrFd() const { return _errfd; }
231
238 std::istream& errStream() { return *_errstream_ap.get(); }
239
244 void closeErr();
245
251 pid_t getPid() const { return _pid; }
252
278 static pid_t checkPidFile(const std::string& pidFile);
279
284 static void removePidFile();
285
294 static void addEffectiveCapability(int cap);
295
299 static void clearEffectiveCapability(int cap);
300
306 static bool getEffectiveCapability(int cap);
307
308 static std::string expandEnvVars(std::string input);
309
315 static bool getEnvVar(const std::string& name,std::string& value);
316
317 static void setEnvVar(const std::string& name,const std::string& value);
318
323 static void clearEnv();
324
329 static unsigned long getVMemSize();
330
337 static unsigned long getMaxRSS();
338
339private:
340
341 static std::string _pidFile;
342
343 pid_t _pid;
344
352 mutable int _infd;
353
354 void setInFd(int val);
355
360 mutable auto_ptr<__gnu_cxx::stdio_filebuf<char> > _inbuf_ap;
361
362 mutable auto_ptr<std::ostream> _instream_ap;
363
368 mutable int _outfd;
369
370 void setOutFd(int val);
371
372 mutable auto_ptr<__gnu_cxx::stdio_filebuf<char> > _outbuf_ap;
373
374 mutable auto_ptr<std::istream> _outstream_ap;
375
380 mutable int _errfd;
381
382 void setErrFd(int val);
383
384 mutable auto_ptr<__gnu_cxx::stdio_filebuf<char> > _errbuf_ap;
385
386 mutable auto_ptr<std::istream> _errstream_ap;
387
388 static std::map<std::string,char*> _environment;
389
391};
392
393}} // namespace nidas namespace util
394
395#endif
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
Process provides an encapsulation of a spawned process, allowing the parent process to perform I/O wi...
Definition Process.h:58
static Mutex _envLock
Definition Process.h:390
static std::string expandEnvVars(std::string input)
Definition Process.cc:566
pid_t _pid
Definition Process.h:343
void closeOut()
Close the file descriptor and istream of the pipe connected to the standard out of the Process.
Definition Process.cc:167
static std::string _pidFile
Definition Process.h:341
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
std::istream & errStream()
Get the istream of the pipe that is connected to the standard error of the Process.
Definition Process.h:238
static void clearEffectiveCapability(int cap)
Definition Process.cc:495
auto_ptr< __gnu_cxx::stdio_filebuf< char > > _outbuf_ap
Definition Process.h:372
int getErrFd() const
Get the file descriptor of the pipe that is connected to the standard error of the Process.
Definition Process.h:230
static void removePidFile()
Remove the pid file.
Definition Process.cc:449
void closeErr()
Close the file descriptor and istream of the pipe connected to the standard error of the Process.
Definition Process.cc:175
void setOutFd(int val)
Definition Process.cc:143
int _errfd
File descriptor that is connected via a pipe to the standard error file descriptor of a spawned proce...
Definition Process.h:380
auto_ptr< std::istream > _outstream_ap
Definition Process.h:374
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:360
int getOutFd() const
Get the file descriptor of the pipe that is connected to the standard out of the Process.
Definition Process.h:208
int _outfd
File descriptor that is connected via a pipe to the standard out file descriptor of a spawned process...
Definition Process.h:368
static bool getEnvVar(const std::string &name, std::string &value)
Get an environment variable given a variable name like "HOST", without the '$', or any brackets,...
Definition Process.cc:622
int getInFd() const
Get the file descriptor of the pipe that is connected to the standard in of the Process.
Definition Process.h:185
Process & operator=(const Process &x)
Assignment operator.
Definition Process.cc:105
std::ostream & inStream()
Get the ostream of the pipe that is connected to the standard in of the Process.
Definition Process.h:192
int wait(bool hang, int *status)
Do a system wait on a process.
Definition Process.cc:364
auto_ptr< std::istream > _errstream_ap
Definition Process.h:386
void setErrFd(int val)
Definition Process.cc:151
int _infd
File descriptor that is connected via a pipe to the standard in file descriptor of a spawned process.
Definition Process.h:352
void closeIn()
Close the file descriptor and ostream of the pipe connected to the standard in of the Process.
Definition Process.cc:159
static unsigned long getMaxRSS()
Return the maximum resident set size of the current process, in bytes.
Definition Process.cc:703
static void setEnvVar(const std::string &name, const std::string &value)
Definition Process.cc:631
std::istream & outStream()
Get the istream of the pipe that is connected to the standard out of the Process.
Definition Process.h:216
auto_ptr< std::ostream > _instream_ap
Definition Process.h:362
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:680
~Process()
Destructor, which does very little.
Definition Process.cc:131
Process()
Default constructor.
Definition Process.cc:77
void setInFd(int val)
Definition Process.cc:135
pid_t getPid() const
The process id of the Process.
Definition Process.h:251
static void addEffectiveCapability(int cap)
Add an effective capability to this process.
Definition Process.cc:456
void kill(int signal)
Send a signal to the process.
Definition Process.cc:352
auto_ptr< __gnu_cxx::stdio_filebuf< char > > _errbuf_ap
Definition Process.h:384
static bool getEffectiveCapability(int cap)
Check if this process has an effective capability.
Definition Process.cc:532
static void clearEnv()
Remove any environment settings made by setEnvVar and release any memory allocated to put those setti...
Definition Process.cc:651
static std::map< std::string, char * > _environment
Definition Process.h:388
static pid_t checkPidFile(const std::string &pidFile)
Check if another process is running, using the named process id file.
Definition Process.cc:386
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31