29 #include <sys/types.h> 32 #include <linux/param.h> 40 #include <boost/format.hpp> 41 #include <boost/lexical_cast.hpp> 43 #include <boost/date_time/posix_time/posix_time.hpp> 51 return boost::str(boost::format(
"/proc/%1%/%2%")
60 std::ifstream
self(
procFilename(pid,
"cmdline").c_str());
65 self.exceptions(std::ios_base::badbit | std::ios_base::failbit);
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%")
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);
82 if (it != cmdline.end()) {
90 std::vector<std::string> components;
93 }
catch (
const std::exception& e) {
94 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 98 if (components.empty()) {
99 throw std::runtime_error(
"Could not determine program name: empty" 100 " commandline entry");
102 return components[0];
113 char buffer[PATH_MAX];
115 buffer,
sizeof(buffer));
117 buffer[count] =
'\0';
118 return std::string(buffer);
129 std::vector<std::string> result;
132 }
catch (
const std::exception& e) {
133 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 134 " commandline arguments:" 138 if (result.empty()) {
139 throw std::runtime_error(
"Could not determine commandline arguments:" 140 " empty commandline entry");
143 result.erase(result.begin());
160 boost::uint64_t startTimeBootJiffies;
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" 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" 184 % procSelfStat % e.what()));
191 return boost::posix_time::ptime
193 bootTime.time_of_day()
194 + boost::posix_time::seconds(startTimeBootJiffies / HZ)
195 + boost::posix_time::milliseconds((1000/HZ * startTimeBootJiffies)
205 passwd* entry = getpwuid(
id);
206 return entry->pw_name;
210 const std::string procSelfStatus =
procFilename(pid,
"status");
212 std::ifstream stream(procSelfStatus.c_str());
216 if ((stream >> label) && (label ==
"Uid:")) {
223 }
catch (std::exception& e) {
224 throw std::runtime_error(boost::str(boost::format(
"Could not read from %1%: %2%")
225 % procSelfStatus % e.what()));
227 throw std::runtime_error(boost::str(boost::format(
"Could not find \"Uid\" field in %1%")
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.
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.