RSC  0.16.0
LinuxProcessInfo.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2014, 2016 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "ProcessInfo.h"
28 
29 #include <sys/types.h> // for getpwuid(3)
30 #include <pwd.h> // likewise
31 #include <unistd.h> // for getuid(2) and others
32 #include <linux/param.h> // for HZ in start-time calculation
33 
34 #include <stdexcept>
35 #include <algorithm>
36 #include <iomanip>
37 #include <sstream>
38 #include <fstream>
39 
40 #include <boost/format.hpp>
41 #include <boost/lexical_cast.hpp>
42 
43 #include <boost/date_time/posix_time/posix_time.hpp>
44 
45 #include "HostInfo.h"
46 
47 namespace rsc {
48 namespace os{
49 
50 std::string procFilename(PID pid, const std::string& filename) {
51  return boost::str(boost::format("/proc/%1%/%2%")
52  % pid % filename);
53 }
54 
56  return getpid(); // cannot fail according to getpid(2)
57 }
58 
59 std::vector<std::string> getCommandlineComponents(PID pid) {
60  std::ifstream self(procFilename(pid, "cmdline").c_str());
61  std::string cmdline;
62  try {
63  // in case enabling exceptions on this stream throws immediately,
64  // the file could not be read at all
65  self.exceptions(std::ios_base::badbit | std::ios_base::failbit);
66  self >> cmdline;
67  } catch (const std::ifstream::failure& e) {
68  throw std::runtime_error(boost::str(boost::format(
69  "Could not read the command line for PID %1%. The "
70  "process probably does not exist: %2%")
71  % pid % e.what()));
72  }
73 
74  std::vector<std::string> components;
75  std::string::iterator it = cmdline.begin();
76  while (it != cmdline.end()) {
77  std::string::iterator end = find(it, cmdline.end(), '\0');
78  std::string component;
79  std::copy(it, end, std::back_inserter(component));
80  components.push_back(component);
81  it = end;
82  if (it != cmdline.end()) {
83  ++it;
84  }
85  }
86  return components;
87 }
88 
89 std::string getProgramName(PID pid) {
90  std::vector<std::string> components;
91  try {
92  components = getCommandlineComponents(pid);
93  } catch (const std::exception& e) {
94  throw std::runtime_error(boost::str(boost::format("Could not determine"
95  " program name: %1%")
96  % e.what()));
97  }
98  if (components.empty()) {
99  throw std::runtime_error("Could not determine program name: empty"
100  " commandline entry");
101  }
102  return components[0];
103 }
104 
105 std::string currentProgramName() {
107 }
108 
109 std::string getExecutablePath(PID pid) {
110  // Try to read the target of the link /proc/PID/exe. This can fail
111  // if the main thread of the process has stopped. Fall back to
112  // using the cmdline pseudo-file for PID then.
113  char buffer[PATH_MAX];
114  int count = readlink(procFilename(pid, "exe").c_str(),
115  buffer, sizeof(buffer));
116  if (count >= 0) {
117  buffer[count] = '\0';
118  return std::string(buffer);
119  } else {
120  return getProgramName(pid);
121  }
122 }
123 
124 std::string currentExecutablePath() {
126 }
127 
128 std::vector<std::string> getCommandlineArguments(PID pid) {
129  std::vector<std::string> result;
130  try {
131  result = getCommandlineComponents(pid);
132  } catch (const std::exception& e) {
133  throw std::runtime_error(boost::str(boost::format("Could not determine"
134  " commandline arguments:"
135  " %1%")
136  % e.what()));
137  }
138  if (result.empty()) {
139  throw std::runtime_error("Could not determine commandline arguments:"
140  " empty commandline entry");
141  }
142 
143  result.erase(result.begin());
144  return result;
145 }
146 
147 std::vector<std::string> currentCommandlineArguments() {
149 }
150 
151 // FIXME this has a critical flaw: the HZ macro is obviously a
152 // compile-time constant referring to the system timer interval of the
153 // compilation host system. However, when a compiled binary is
154 // executed on a different system, the system timer interval of that
155 // system may be different. Unfortunately, there does not seem to be a
156 // means to determine the value a runtime.
157 boost::posix_time::ptime getProcessStartTime(PID pid) {
158  // Read process start time in jiffies since *system boot*.
159  // See /proc/[pid]/stat section in proc(5).
160  boost::uint64_t startTimeBootJiffies;
161  {
162  static const unsigned int START_TIME_BOOT_JIFFIES_FIELD_NUMBER = 22;
163  const std::string procSelfStat = procFilename(pid, "stat");
164  std::ifstream stream(procSelfStat.c_str());
165  std::vector<std::string> tokens;
166  std::copy(std::istream_iterator<std::string>(stream),
167  std::istream_iterator<std::string>(),
168  std::back_inserter(tokens));
169  if (tokens.size() < START_TIME_BOOT_JIFFIES_FIELD_NUMBER) {
170  throw std::runtime_error(boost::str(boost::format("%1% did not contain"
171  " the expected number"
172  " of fields")
173  % procSelfStat));
174  }
175  try {
176  startTimeBootJiffies
177  = boost::lexical_cast<boost::uint64_t>
178  (tokens[START_TIME_BOOT_JIFFIES_FIELD_NUMBER - 1]);
179  } catch (const boost::bad_lexical_cast& e) {
180  throw std::runtime_error(boost::str(boost::format("Could not"
181  "parse process"
182  " start-time entry"
183  " of %1%: %2%")
184  % procSelfStat % e.what()));
185  }
186  }
187 
188  // Add to system boot time the process start time relative to
189  // system boot time. Process integral seconds and milliseconds.
190  boost::posix_time::ptime bootTime = currentBootTime();
191  return boost::posix_time::ptime
192  (bootTime.date(),
193  bootTime.time_of_day()
194  + boost::posix_time::seconds(startTimeBootJiffies / HZ)
195  + boost::posix_time::milliseconds((1000/HZ * startTimeBootJiffies)
196  % 1000));
197 }
198 
199 boost::posix_time::ptime currentProcessStartTime() {
201 }
202 
203 
204 std::string uidToName(uid_t id) {
205  passwd* entry = getpwuid(id);
206  return entry->pw_name;
207 }
208 
209 std::string getExecutingUser(PID pid) {
210  const std::string procSelfStatus = procFilename(pid, "status");
211 
212  std::ifstream stream(procSelfStatus.c_str());
213  std::string label;
214  try {
215  while (stream) {
216  if ((stream >> label) && (label == "Uid:")) {
217  uid_t uid;
218  if (stream >> uid) {
219  return uidToName(uid);
220  }
221  }
222  }
223  } catch (std::exception& e) {
224  throw std::runtime_error(boost::str(boost::format("Could not read from %1%: %2%")
225  % procSelfStatus % e.what()));
226  }
227  throw std::runtime_error(boost::str(boost::format("Could not find \"Uid\" field in %1%")
228  % procSelfStatus));
229 }
230 
231 std::string currentExecutingUser() {
232  return uidToName(getuid());
233 }
234 
235 }
236 }
std::vector< std::string > getCommandlineComponents(PID pid)
std::string uidToName(uid_t id)
std::vector< std::string > getCommandlineArguments(PID pid)
Return the list of commandline arguments of the process designated by pid.
boost::posix_time::ptime getProcessStartTime(PID pid)
Return the start time of the process designated by pid.
PID currentProcessId()
Return the id the current process.
boost::posix_time::ptime currentProcessStartTime()
Return the start time of the current process.
std::string getExecutablePath(PID pid)
Return the absolute path of the executable file that is executed in the process designated by pid...
std::string procFilename(PID pid, const std::string &filename)
std::vector< std::string > currentCommandlineArguments()
Return the list of commandline arguments of the current process.
RSC_EXPORT boost::posix_time::ptime currentBootTime()
Return the boot time of the local machine.
std::string getExecutingUser(PID pid)
Return login- or account-name of the user executing pid.
std::string currentExecutingUser()
Return the login- or account-name of the user executing the current process.
std::string getProgramName(PID pid)
Return the name of the program executed in the process designated by pid.
unsigned int PID
Definition: ProcessInfo.h:47
std::string currentProgramName()
Return the name of the program executed in the current process.
std::string currentExecutablePath()
Return the absolute path of the executable file that is executed in the current process.