00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_PROCESS_HPP
00019 #define RAUL_PROCESS_HPP
00020
00021 #include <string>
00022 #include <iostream>
00023 #include <unistd.h>
00024 #include <sys/time.h>
00025 #include <sys/resource.h>
00026 #include <boost/utility.hpp>
00027
00028 namespace Raul {
00029
00030
00035 class Process : boost::noncopyable
00036 {
00037 public:
00038
00043 static bool launch(const std::string& command) {
00044 const std::string executable = (command.find(" ") != std::string::npos)
00045 ? command.substr(0, command.find(" "))
00046 : command;
00047
00048 const std::string arguments = command.substr((command.find(" ") + 1));
00049
00050 std::cerr << "Launching child process '" << executable << "' with arguments '"
00051 << arguments << "'" << std::endl;
00052
00053
00054 const int err = fork();
00055
00056 if (err == 0) {
00057
00058
00059
00060 struct rlimit max_fds;
00061 getrlimit(RLIMIT_NOFILE, &max_fds);
00062
00063 for (rlim_t fd = 3; fd < max_fds.rlim_cur; ++fd)
00064 close(fd);
00065
00066 switch (fork()) {
00067 case 0:
00068
00069 setsid();
00070 execlp(executable.c_str(), arguments.c_str(), NULL);
00071 _exit(-1);
00072
00073 case -1:
00074
00075 _exit (-1);
00076
00077
00078 default:
00079 _exit (0);
00080 }
00081 }
00082
00083 return (err > 0);
00084 }
00085
00086 private:
00087 Process() {}
00088 };
00089
00090
00091 }
00092
00093 #endif // RAUL_PROCESS_HPP