29 #include <sys/sysctl.h> 30 #include <sys/types.h> 37 #include <boost/shared_array.hpp> 39 #include <boost/format.hpp> 41 #include <boost/date_time/posix_time/posix_time.hpp> 59 size_t size =
sizeof(argmax);
60 if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
61 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 109 mib[1] = KERN_PROCARGS2;
111 size = (size_t)argmax;
112 boost::shared_array<char> procargs(
new char[argmax]);
113 if (sysctl(mib, 3, procargs.get(), &size, NULL, 0) == -1) {
114 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 122 memcpy(&nargs, procargs.get(),
sizeof(nargs));
126 cp = procargs.get() +
sizeof(nargs);
129 for (; cp < &procargs[size]; cp++) {
135 if (cp == &procargs[size]) {
136 throw new std::runtime_error(
"Could not determine command line:" 137 " End of info memory reached without" 138 " finding the string area.");
142 for (; cp < &procargs[size]; cp++) {
148 if (cp == &procargs[size]) {
149 throw new std::runtime_error(
"Could not determine command line:" 150 " End of info memory reached without" 151 " finding the string area.");
157 std::vector<std::string> commandLine;
158 for (
int i = 0; i < nargs && cp < &procargs[size]; ++i) {
159 std::string argument(cp);
160 commandLine.push_back(argument);
161 cp += commandLine.back().size() + 1;
168 std::vector<std::string> components;
171 }
catch (
const std::exception& e) {
172 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 173 " program name: %1%")
176 if (components.empty()) {
177 throw std::runtime_error(
"Could not determine program name: empty" 178 " commandline entry");
180 return components[0];
188 char pathBuffer[PROC_PIDPATHINFO_MAXSIZE];
189 if (proc_pidpath(pid, pathBuffer,
sizeof(pathBuffer)) <= 0) {
190 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 196 return std::string(pathBuffer);
204 std::vector<std::string> result;
207 }
catch (
const std::exception& e) {
208 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 209 " commandline arguments:" 213 if (result.empty()) {
214 throw std::runtime_error(
"Could not determine commandline arguments:" 215 " empty commandline entry");
218 result.erase(result.begin());
228 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
229 struct kinfo_proc kp;
230 size_t len = (size_t)
sizeof(kp);
231 if (sysctl(mib, 4, &kp, &len, NULL, 0) != 0) {
232 throw std::runtime_error(boost::str(boost::format(
"Could not determine" 233 " process start time:" 238 throw std::runtime_error(
"Could not determine process start time:" 242 return boost::posix_time::from_time_t(kp.kp_proc.p_starttime.tv_sec) +
243 boost::posix_time::microseconds(kp.kp_proc.p_starttime.tv_usec);
251 throw std::runtime_error(
"Could not determine executing user:" 256 passwd* entry = getpwuid(getuid());
257 return entry->pw_name;
std::vector< std::string > getCommandlineComponents(PID pid)
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::vector< std::string > currentCommandlineArguments()
Return the list of commandline arguments of the current process.
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.