ProcessUtils.cpp
Go to the documentation of this file.
1 #include <NovaDAQUtilities/ProcessUtils.h>
2 #include <boost/filesystem.hpp>
3 #include <boost/lexical_cast.hpp>
4 #include <iostream>
5 #include <fstream>
6 
7 namespace BFS = boost::filesystem;
8 
9 namespace novadaq {
10 
12 
13 /**
14  * Fetches all descendant process IDs for the specified process.
15  *
16  * @param parentPid The parent process ID.
17  * @param descendantPidList The resulting list of descendant process IDs.
18  *
19  * @return Status code (zero for success).
20  */
22 getDescendantProcessIds(const int& parentPid,
23  std::vector<int>& descendantPidList) {
24  std::vector<int> parentPidList;
25  parentPidList.push_back(parentPid);
26  return ProcessUtils::getDescendantProcessIds(parentPidList,
27  descendantPidList);
28 }
29 
30 /**
31  * Fetches all descendant process IDs for the specified list of processes.
32  *
33  * @param parentPidList The list of parent process IDs.
34  * @param descendantPidList The resulting list of descendant process IDs.
35  *
36  * @return Status code (zero for success).
37  */
39 getDescendantProcessIds(const std::vector<int>& parentPidList,
40  std::vector<int>& descendantPidList) {
41  int status;
42  status = ProcessUtils::getChildProcessIds(parentPidList,
43  descendantPidList);
44  if (status != 0) {return status;}
45 
46  if (descendantPidList.size() > 0) {
47  std::vector<int> childPidList;
48  status = ProcessUtils::
49  getDescendantProcessIds(descendantPidList,
50  childPidList);
51  if (status != 0) {return status;}
52  if (childPidList.size() > 0) {
53  for (uint32_t idx = 0; idx < childPidList.size(); ++idx) {
54  descendantPidList.push_back(childPidList[idx]);
55  }
56  }
57  }
58 
59  return 0;
60 }
61 
62 /**
63  * Fetches the child process IDs for the specified process.
64  *
65  * @param parentPid The parent process ID.
66  * @param childPidList The resulting list of child process IDs.
67  *
68  * @return Status code (zero for success).
69  */
70 int ProcessUtils::getChildProcessIds(const int& parentPid,
71  std::vector<int>& childPidList) {
72 
73  boost::mutex::scoped_lock lock(_childPidMutex);
74 
75  // there has to be a better general scheme than this...
76 
77  int thisPID = getpid();
78  std::string tmpFile("/tmp/novadaq_processutils_" +
79  boost::lexical_cast<std::string>(thisPID) +
80  ".tmpout");
81 
82  std::string cmdString("ps h -o pid --ppid");
83  cmdString .append(" " + boost::lexical_cast<std::string>(parentPid));
84  cmdString .append(" 1>" + tmpFile);
85  cmdString .append(" 2>/dev/null");
86 
87  int retcode = system(cmdString.c_str());
88  if (retcode == 0) {
89  std::ifstream tmpStream(tmpFile.c_str());
90  if (tmpStream.fail()) {
91  return -5;
92  }
93  else {
94  int pid;
95  tmpStream >> pid;
96  while (! tmpStream.eof() && ! tmpStream.fail()) {
97  childPidList.push_back(pid);
98  tmpStream >> pid;
99  }
100  tmpStream.close();
101 
102  BFS::remove(tmpFile);
103  }
104  }
105 
106  return 0;
107 }
108 
109 /**
110  * Fetches the child process IDs for the specified list of processes.
111  *
112  * @param parentPidList The list of parent process IDs.
113  * @param childPidList The resulting list of child process IDs.
114  *
115  * @return Status code (zero for success).
116  */
117 int ProcessUtils::getChildProcessIds(const std::vector<int>& parentPidList,
118  std::vector<int>& childPidList) {
119 
120  for (uint32_t idx = 0; idx < parentPidList.size(); ++idx) {
121  int status =
122  ProcessUtils::getChildProcessIds(parentPidList[idx],
123  childPidList);
124  if (status != 0) {return status;}
125  }
126  return 0;
127 }
128 
129 } // end of namespace novadaq
system("rm -rf microbeam.root")
int status
Definition: fabricate.py:1613
static boost::mutex _childPidMutex
Definition: ProcessUtils.h:34
static int getDescendantProcessIds(const int &parentPid, std::vector< int > &descendantPidList)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
static int getChildProcessIds(const int &parentPid, std::vector< int > &childPidList)