summaryrefslogtreecommitdiffstats
path: root/include/raul/Process.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/raul/Process.hpp')
-rw-r--r--include/raul/Process.hpp89
1 files changed, 45 insertions, 44 deletions
diff --git a/include/raul/Process.hpp b/include/raul/Process.hpp
index d77c3f3..1c5deed 100644
--- a/include/raul/Process.hpp
+++ b/include/raul/Process.hpp
@@ -30,52 +30,53 @@ namespace Raul {
class Process : Noncopyable
{
public:
- /** Launch a sub process.
- *
- * @param argv List of arguments, where argv[0] is the command name.
- * @return True on success.
- */
- static bool launch(const char* const argv[]) {
- // Use the same double fork() trick as JACK to prevent zombie children
- const int child = fork();
-
- if (child == 0) {
- // (in child)
-
- // Close all nonstandard file descriptors
- struct rlimit max_fds{};
- getrlimit(RLIMIT_NOFILE, &max_fds);
- for (rlim_t fd = 3; fd < max_fds.rlim_cur; ++fd) {
- close(static_cast<int>(fd));
- }
-
- // Fork child
- const int grandchild = fork();
- switch (grandchild) {
- case 0:
- // (in grandchild)
- setsid();
- execvp(argv[0], const_cast<char*const*>(argv));
- _exit(-1);
-
- case -1:
- // Fork failed, there is no grandchild
- _exit(-1);
-
- default:
- // Fork succeeded, return grandchild PID
- _exit(grandchild);
- }
- } else if (child < 0) {
- // Fork failed, there is no child
- return false;
- }
-
- return true;
- }
+ /** Launch a sub process.
+ *
+ * @param argv List of arguments, where argv[0] is the command name.
+ * @return True on success.
+ */
+ static bool launch(const char* const argv[])
+ {
+ // Use the same double fork() trick as JACK to prevent zombie children
+ const int child = fork();
+
+ if (child == 0) {
+ // (in child)
+
+ // Close all nonstandard file descriptors
+ struct rlimit max_fds {};
+ getrlimit(RLIMIT_NOFILE, &max_fds);
+ for (rlim_t fd = 3; fd < max_fds.rlim_cur; ++fd) {
+ close(static_cast<int>(fd));
+ }
+
+ // Fork child
+ const int grandchild = fork();
+ switch (grandchild) {
+ case 0:
+ // (in grandchild)
+ setsid();
+ execvp(argv[0], const_cast<char* const*>(argv));
+ _exit(-1);
+
+ case -1:
+ // Fork failed, there is no grandchild
+ _exit(-1);
+
+ default:
+ // Fork succeeded, return grandchild PID
+ _exit(grandchild);
+ }
+ } else if (child < 0) {
+ // Fork failed, there is no child
+ return false;
+ }
+
+ return true;
+ }
private:
- Process() = default;
+ Process() = default;
};
} // namespace Raul