From 8449c5b6ad87ced01bcb82627cb85a8981337210 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Thu, 10 Nov 2016 20:01:30 -0700
Subject: [PATCH] Switch to using fork

---
 scheme/main.rs | 41 ++++++++++++++++++-----------------------
 1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/scheme/main.rs b/scheme/main.rs
index b67bf92..329c228 100644
--- a/scheme/main.rs
+++ b/scheme/main.rs
@@ -4,12 +4,9 @@ extern crate redoxfs;
 extern crate spin;
 extern crate syscall;
 
-use std::env;
+use std::{env, process};
 use std::fs::File;
 use std::io::{Read, Write};
-use std::sync::Arc;
-use std::thread;
-use spin::Mutex;
 
 // use cache::Cache;
 use image::Image;
@@ -24,25 +21,22 @@ pub mod image;
 pub mod resource;
 pub mod scheme;
 
-enum Status {
-    Starting,
-    Running,
-    Stopping
-}
-
 fn main() {
     if let Some(path) = env::args().nth(1) {
-        let status_mutex = Arc::new(Mutex::new(Status::Starting));
+        let mut pipes = [0; 2];
+        syscall::pipe2(&mut pipes, 0).unwrap();
+
+        // Daemonize
+        if unsafe { syscall::clone(0).unwrap() } == 0 {
+            let _ = syscall::close(pipes[0]);
 
-        let status_daemon = status_mutex.clone();
-        thread::spawn(move || {
             match Image::open(&path) /* .map(|image| Cache::new(image)) */ {
                 Ok(disk) => match FileSystem::open(Box::new(disk)) {
                     Ok(fs) => match File::create(":file") {
                         Ok(mut socket) => {
                             println!("redoxfs: mounted filesystem {} on file:", path);
 
-                            *status_daemon.lock() = Status::Running;
+                            let _ = syscall::write(pipes[1], &[1]);
 
                             let scheme = FileScheme::new("file", fs);
                             loop {
@@ -59,17 +53,18 @@ fn main() {
                 Err(err) => println!("redoxfs: failed to open image {}: {}", path, err)
             }
 
-            *status_daemon.lock() = Status::Stopping;
-        });
+            let _ = syscall::write(pipes[1], &[0]);
 
-        'waiting: loop {
-            match *status_mutex.lock() {
-                Status::Starting => (),
-                Status::Running => break 'waiting,
-                Status::Stopping => break 'waiting,
-            }
+            let _ = syscall::close(pipes[1]);
+        } else {
+            let _ = syscall::close(pipes[1]);
+
+            let mut res = [0];
+            syscall::read(pipes[0], &mut res).unwrap();
+
+            let _ = syscall::close(pipes[0]);
 
-            thread::yield_now();
+            process::exit(res[0] as i32);
         }
     } else {
         println!("redoxfs: no disk image provided");
-- 
GitLab