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 #include "raul/log.hpp"
00028
00029 namespace Raul {
00030
00031
00036 class Process : boost::noncopyable
00037 {
00038 public:
00039
00044 static bool launch(const std::string& command) {
00045 const std::string executable = (command.find(" ") != std::string::npos)
00046 ? command.substr(0, command.find(" "))
00047 : command;
00048
00049 const std::string arguments = command.substr((command.find(" ") + 1));
00050
00051 info << "Launching child process '" << executable << "' with arguments '"
00052 << arguments << "'" << std::endl;
00053
00054
00055 const int err = fork();
00056
00057 if (err == 0) {
00058
00059
00060
00061 struct rlimit max_fds;
00062 getrlimit(RLIMIT_NOFILE, &max_fds);
00063
00064 for (rlim_t fd = 3; fd < max_fds.rlim_cur; ++fd)
00065 close(fd);
00066
00067 switch (fork()) {
00068 case 0:
00069
00070 setsid();
00071 execlp(executable.c_str(), arguments.c_str(), NULL);
00072 _exit(-1);
00073
00074 case -1:
00075
00076 _exit (-1);
00077
00078
00079 default:
00080 _exit (0);
00081 }
00082 }
00083
00084 return (err > 0);
00085 }
00086
00087 private:
00088 Process() {}
00089 };
00090
00091
00092 }
00093
00094 #endif // RAUL_PROCESS_HPP