RSC  0.12.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 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  static const unsigned int START_TIME_BOOT_JIFFIES_FIELD_NUMBER = 22;
159 
160  // Read process start time in jiffies since *system boot*.
161  // See /proc/[pid]/stat section in proc(5).
162  boost::uint64_t startTimeBootJiffies;
163  {
164  const std::string procSelfStat = procFilename(pid, "stat");
165  std::ifstream stream(procSelfStat.c_str());
166  std::vector<std::string> tokens;
167  std::copy(std::istream_iterator<std::string>(stream),
168  std::istream_iterator<std::string>(),
169  std::back_inserter(tokens));
170  if (tokens.size() < START_TIME_BOOT_JIFFIES_FIELD_NUMBER) {
171  throw std::runtime_error(boost::str(boost::format("%1% did not contain"
172  " the expected number"
173  " of fields")
174  % procSelfStat));
175  }
176  try {
177  startTimeBootJiffies
178  = boost::lexical_cast<boost::uint64_t>
179  (tokens[START_TIME_BOOT_JIFFIES_FIELD_NUMBER - 1]);
180  } catch (const boost::bad_lexical_cast& e) {
181  throw std::runtime_error(boost::str(boost::format("Could not"
182  "parse process"
183  " start-time entry"
184  " of %1%: %2%")
185  % procSelfStat % e.what()));
186  }
187  }
188 
189  // Add to system boot time the process start time relative to
190  // system boot time. Process integral seconds and milliseconds.
191  boost::posix_time::ptime bootTime = currentBootTime();
192  return boost::posix_time::ptime
193  (bootTime.date(),
194  bootTime.time_of_day()
195  + boost::posix_time::seconds(startTimeBootJiffies / HZ)
196  + boost::posix_time::milliseconds((1000/HZ * startTimeBootJiffies)
197  % 1000));
198 }
199 
200 boost::posix_time::ptime currentProcessStartTime() {
202 }
203 
204 
205 std::string uidToName(uid_t id) {
206  passwd* entry = getpwuid(id);
207  return entry->pw_name;
208 }
209 
210 std::string getExecutingUser(PID pid) {
211  const std::string procSelfStatus = procFilename(pid, "status");
212 
213  std::ifstream stream(procSelfStatus.c_str());
214  std::string label;
215  try {
216  while (stream) {
217  if ((stream >> label) && (label == "Uid:")) {
218  uid_t uid;
219  if (stream >> uid) {
220  return uidToName(uid);
221  }
222  }
223  }
224  } catch (std::exception& e) {
225  throw std::runtime_error(boost::str(boost::format("Could not read from %1%: %2%")
226  % procSelfStatus % e.what()));
227  }
228  throw std::runtime_error(boost::str(boost::format("Could not find \"Uid\" field in %1%")
229  % procSelfStatus));
230 }
231 
232 std::string currentExecutingUser() {
233  return uidToName(getuid());
234 }
235 
236 }
237 }
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.