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