From 2db2d10cce5819444b433d27a760d8c35e6a30da Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Thu, 10 Nov 2016 20:02:51 -0700
Subject: [PATCH] Switch to real standard, fix daemonization on real standard

---
 Makefile                      | 14 +++++++-------
 drivers/ahcid/src/main.rs     |  7 ++++---
 drivers/e1000d/src/main.rs    |  7 ++++---
 drivers/ps2d/src/main.rs      |  7 ++++---
 drivers/rtl8168d/src/main.rs  |  7 ++++---
 drivers/vesad/src/main.rs     |  7 ++++---
 schemes/ethernetd/src/main.rs |  6 +++---
 schemes/example/src/main.rs   |  6 +++---
 schemes/ipd/src/main.rs       |  7 ++++---
 schemes/ptyd/src/main.rs      |  7 ++++---
 schemes/randd/src/main.rs     |  6 +++---
 schemes/redoxfs               |  2 +-
 schemes/tcpd/src/main.rs      |  7 ++++---
 schemes/udpd/src/main.rs      |  7 ++++---
 x86_64-unknown-redox.json     |  3 ++-
 15 files changed, 55 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile
index 3263c47ce..c073cc999 100644
--- a/Makefile
+++ b/Makefile
@@ -182,7 +182,7 @@ else
 %.list: %
 	objdump -C -M intel -D $< > $@
 
-$(KBUILD)/harddrive.bin: $(KBUILD)/kernel $(BUILD)/filesystem.bin bootloader/$(ARCH)/**
+$(KBUILD)/harddrive.bin: $(KBUILD)/kernel bootloader/$(ARCH)/** $(BUILD)/filesystem.bin
 	nasm -f bin -o $@ -D ARCH_$(ARCH) -ibootloader/$(ARCH)/ bootloader/$(ARCH)/harddrive.asm
 
 qemu: $(KBUILD)/harddrive.bin
@@ -280,13 +280,13 @@ $(BUILD)/libopenlibm.a: libstd/openlibm/libopenlibm.a
 	mkdir -p $(BUILD)
 	cp $< $@
 
-$(BUILD)/libstd.rlib: libstd/Cargo.toml libstd/src/** $(BUILD)/libcore.rlib $(BUILD)/liballoc.rlib $(BUILD)/librustc_unicode.rlib $(BUILD)/libcollections.rlib $(BUILD)/librand.rlib $(BUILD)/libopenlibm.a
-	$(CARGO) rustc --verbose --manifest-path $< $(CARGOFLAGS) -o $@
-	cp libstd/target/$(TARGET)/release/deps/*.rlib $(BUILD)
-
-#$(BUILD)/libstd.rlib: libstd_real/Cargo.toml rust/src/libstd/** $(BUILD)/libcore.rlib $(BUILD)/liballoc.rlib $(BUILD)/librustc_unicode.rlib $(BUILD)/libcollections.rlib $(BUILD)/librand.rlib $(BUILD)/libopenlibm.a
+#$(BUILD)/libstd.rlib: libstd/Cargo.toml libstd/src/** $(BUILD)/libcore.rlib $(BUILD)/liballoc.rlib $(BUILD)/librustc_unicode.rlib $(BUILD)/libcollections.rlib $(BUILD)/librand.rlib $(BUILD)/libopenlibm.a
 #	$(CARGO) rustc --verbose --manifest-path $< $(CARGOFLAGS) -o $@
-#	cp libstd_real/target/$(TARGET)/release/deps/*.rlib $(BUILD)
+#	cp libstd/target/$(TARGET)/release/deps/*.rlib $(BUILD)
+
+$(BUILD)/libstd.rlib: libstd_real/Cargo.toml rust/src/libstd/** $(BUILD)/libcore.rlib $(BUILD)/liballoc.rlib $(BUILD)/librustc_unicode.rlib $(BUILD)/libcollections.rlib $(BUILD)/librand.rlib $(BUILD)/libopenlibm.a
+	$(CARGO) rustc --verbose --manifest-path $< $(CARGOFLAGS) -o $@
+	cp libstd_real/target/$(TARGET)/release/deps/*.rlib $(BUILD)
 
 initfs/bin/%: drivers/%/Cargo.toml drivers/%/src/** $(BUILD)/libstd.rlib
 	mkdir -p initfs/bin
diff --git a/drivers/ahcid/src/main.rs b/drivers/ahcid/src/main.rs
index 0bad32239..bd8f699a1 100644
--- a/drivers/ahcid/src/main.rs
+++ b/drivers/ahcid/src/main.rs
@@ -7,7 +7,7 @@ extern crate io;
 extern crate spin;
 extern crate syscall;
 
-use std::{env, thread, usize};
+use std::{env, usize};
 use std::fs::File;
 use std::io::{Read, Write};
 use std::os::unix::io::{AsRawFd, FromRawFd};
@@ -29,7 +29,8 @@ fn main() {
 
     print!("{}", format!(" + AHCI on: {:X} IRQ: {}\n", bar, irq));
 
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let address = unsafe { syscall::physmap(bar, 4096, MAP_WRITE).expect("ahcid: failed to map address") };
         {
             let socket_fd = syscall::open(":disk", syscall::O_RDWR | syscall::O_CREAT | syscall::O_NONBLOCK).expect("ahcid: failed to create disk scheme");
@@ -69,5 +70,5 @@ fn main() {
             }
         }
         unsafe { let _ = syscall::physunmap(address); }
-    });
+    }
 }
diff --git a/drivers/e1000d/src/main.rs b/drivers/e1000d/src/main.rs
index f3baf1bba..d199ff97a 100644
--- a/drivers/e1000d/src/main.rs
+++ b/drivers/e1000d/src/main.rs
@@ -6,7 +6,7 @@ extern crate netutils;
 extern crate syscall;
 
 use std::cell::RefCell;
-use std::{env, thread};
+use std::env;
 use std::fs::File;
 use std::io::{Read, Write, Result};
 use std::os::unix::io::{AsRawFd, FromRawFd};
@@ -29,7 +29,8 @@ fn main() {
 
     print!("{}", format!(" + E1000 on: {:X}, IRQ: {}\n", bar, irq));
 
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let socket_fd = syscall::open(":network", syscall::O_RDWR | syscall::O_CREAT | syscall::O_NONBLOCK).expect("e1000d: failed to create network scheme");
         let socket = Arc::new(RefCell::new(unsafe { File::from_raw_fd(socket_fd) }));
 
@@ -128,5 +129,5 @@ fn main() {
             }
         }
         unsafe { let _ = syscall::physunmap(address); }
-    });
+    }
 }
diff --git a/drivers/ps2d/src/main.rs b/drivers/ps2d/src/main.rs
index 8ff07331a..7c67bd3a2 100644
--- a/drivers/ps2d/src/main.rs
+++ b/drivers/ps2d/src/main.rs
@@ -11,7 +11,7 @@ use std::env;
 use std::fs::File;
 use std::io::{Read, Write, Result};
 use std::os::unix::io::AsRawFd;
-use std::{mem, thread};
+use std::mem;
 
 use event::EventQueue;
 use orbclient::{KeyEvent, MouseEvent};
@@ -123,7 +123,8 @@ impl<'a> Ps2d<'a> {
 }
 
 fn main() {
-    thread::spawn(|| {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         unsafe {
             iopl(3).expect("ps2d: failed to get I/O permission");
             asm!("cli" : : : : "intel", "volatile");
@@ -188,5 +189,5 @@ fn main() {
             let (keyboard, data) = event_queue.run().expect("ps2d: failed to handle events");
             ps2d.handle(keyboard, data);
         }
-    });
+    }
 }
diff --git a/drivers/rtl8168d/src/main.rs b/drivers/rtl8168d/src/main.rs
index 00d6f3367..119828612 100644
--- a/drivers/rtl8168d/src/main.rs
+++ b/drivers/rtl8168d/src/main.rs
@@ -7,7 +7,7 @@ extern crate netutils;
 extern crate syscall;
 
 use std::cell::RefCell;
-use std::{env, thread};
+use std::env;
 use std::fs::File;
 use std::io::{Read, Write, Result};
 use std::os::unix::io::{AsRawFd, FromRawFd};
@@ -30,7 +30,8 @@ fn main() {
 
     print!("{}", format!(" + RTL8168 on: {:X}, IRQ: {}\n", bar, irq));
 
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let socket_fd = syscall::open(":network", syscall::O_RDWR | syscall::O_CREAT | syscall::O_NONBLOCK).expect("rtl8168d: failed to create network scheme");
         let socket = Arc::new(RefCell::new(unsafe { File::from_raw_fd(socket_fd) }));
 
@@ -133,5 +134,5 @@ fn main() {
             }
         }
         unsafe { let _ = syscall::physunmap(address); }
-    });
+    }
 }
diff --git a/drivers/vesad/src/main.rs b/drivers/vesad/src/main.rs
index b9defbf99..7f4347a1e 100644
--- a/drivers/vesad/src/main.rs
+++ b/drivers/vesad/src/main.rs
@@ -6,7 +6,7 @@ extern crate alloc;
 extern crate orbclient;
 extern crate syscall;
 
-use std::{env, mem, thread};
+use std::{env, mem};
 use std::fs::File;
 use std::io::{Read, Write};
 use syscall::{physmap, physunmap, Packet, SchemeMut, EVENT_READ, MAP_WRITE, MAP_WRITE_COMBINE};
@@ -49,7 +49,8 @@ fn main() {
     }
 
     if physbaseptr > 0 {
-        thread::spawn(move || {
+        // Daemonize
+        if unsafe { syscall::clone(0).unwrap() } == 0 {
             let mut socket = File::create(":display").expect("vesad: failed to create display scheme");
 
             let size = width * height;
@@ -103,6 +104,6 @@ fn main() {
                     }
                 }
             }
-        });
+        }
     }
 }
diff --git a/schemes/ethernetd/src/main.rs b/schemes/ethernetd/src/main.rs
index a238aee11..66357b690 100644
--- a/schemes/ethernetd/src/main.rs
+++ b/schemes/ethernetd/src/main.rs
@@ -8,7 +8,6 @@ use std::fs::File;
 use std::io::{Result, Read, Write};
 use std::os::unix::io::FromRawFd;
 use std::rc::Rc;
-use std::thread;
 
 use syscall::{Packet, SchemeMut, EWOULDBLOCK};
 
@@ -17,7 +16,8 @@ use scheme::EthernetScheme;
 mod scheme;
 
 fn main() {
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let network_fd = syscall::open("network:", syscall::O_RDWR | syscall::O_NONBLOCK).expect("ethernetd: failed to open network");
         let network = unsafe { File::from_raw_fd(network_fd) };
 
@@ -90,5 +90,5 @@ fn main() {
         event_queue.trigger_all(0).expect("ethernetd: failed to trigger events");
 
         event_queue.run().expect("ethernetd: failed to run event loop");
-    });
+    }
 }
diff --git a/schemes/example/src/main.rs b/schemes/example/src/main.rs
index 94db5d176..1ba8d5910 100644
--- a/schemes/example/src/main.rs
+++ b/schemes/example/src/main.rs
@@ -3,7 +3,6 @@ extern crate syscall;
 use std::fs::File;
 use std::io::{Read, Write};
 use std::str;
-use std::thread;
 
 use syscall::{Packet, Result, Scheme};
 
@@ -25,7 +24,8 @@ impl Scheme for ExampleScheme {
 }
 
 fn main(){
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let mut socket = File::create(":example").expect("example: failed to create example scheme");
         let scheme = ExampleScheme;
         loop {
@@ -35,5 +35,5 @@ fn main(){
             scheme.handle(&mut packet);
             socket.write(&packet).expect("example: failed to write responses to example scheme");
         }
-    });
+    }
 }
diff --git a/schemes/ipd/src/main.rs b/schemes/ipd/src/main.rs
index 96b6d62db..093dce5cb 100644
--- a/schemes/ipd/src/main.rs
+++ b/schemes/ipd/src/main.rs
@@ -10,7 +10,7 @@ use std::fs::File;
 use std::io::{self, Read, Write};
 use std::os::unix::io::FromRawFd;
 use std::rc::Rc;
-use std::{slice, str, thread};
+use std::{slice, str};
 use syscall::data::Packet;
 use syscall::error::{Error, Result, EACCES, EADDRNOTAVAIL, EBADF, EIO, EINVAL, ENOENT, EWOULDBLOCK};
 use syscall::flag::{EVENT_READ, O_NONBLOCK};
@@ -326,7 +326,8 @@ impl SchemeMut for Ipd {
 }
 
 fn main() {
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let scheme_fd = syscall::open(":ip", syscall::O_RDWR | syscall::O_CREAT | syscall::O_NONBLOCK).expect("ipd: failed to create :ip");
         let scheme_file = unsafe { File::from_raw_fd(scheme_fd) };
 
@@ -367,5 +368,5 @@ fn main() {
         event_queue.trigger_all(0).expect("ipd: failed to trigger event queue");
 
         event_queue.run().expect("ipd: failed to run event queue");
-    });
+    }
 }
diff --git a/schemes/ptyd/src/main.rs b/schemes/ptyd/src/main.rs
index 4d4c6494a..28f058d0f 100644
--- a/schemes/ptyd/src/main.rs
+++ b/schemes/ptyd/src/main.rs
@@ -7,7 +7,7 @@ use std::collections::{BTreeMap, VecDeque};
 use std::fs::File;
 use std::io::{Read, Write};
 use std::rc::{Rc, Weak};
-use std::{str, thread};
+use std::str;
 
 use syscall::data::Packet;
 use syscall::error::{Error, Result, EBADF, EINVAL, ENOENT, EPIPE, EWOULDBLOCK};
@@ -289,7 +289,8 @@ impl PtySlave {
 }
 
 fn main(){
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let mut socket = File::create(":pty").expect("pty: failed to create pty scheme");
         let mut scheme = PtyScheme::new();
         let mut todo = Vec::new();
@@ -375,5 +376,5 @@ fn main(){
                 }
             }
         }
-    });
+    }
 }
diff --git a/schemes/randd/src/main.rs b/schemes/randd/src/main.rs
index b39041494..116c66493 100644
--- a/schemes/randd/src/main.rs
+++ b/schemes/randd/src/main.rs
@@ -7,7 +7,6 @@ extern crate rand;
 
 use std::fs::File;
 use std::io::{Read, Write};
-use std::thread;
 
 use rand::chacha::ChaChaRng;
 use rand::Rng;
@@ -51,7 +50,8 @@ impl SchemeMut for RandScheme {
 fn main(){
     let has_rdrand = CpuId::new().get_feature_info().unwrap().has_rdrand();
 
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let mut socket = File::create(":rand").expect("rand: failed to create rand scheme");
 
         let mut rng = ChaChaRng::new_unseeded();
@@ -78,5 +78,5 @@ fn main(){
             scheme.handle(&mut packet);
             socket.write(&packet).expect("rand: failed to write responses to rand scheme");
         }
-    });
+    }
 }
diff --git a/schemes/redoxfs b/schemes/redoxfs
index d058e56d6..8449c5b6a 160000
--- a/schemes/redoxfs
+++ b/schemes/redoxfs
@@ -1 +1 @@
-Subproject commit d058e56d6e9dbc2fb95bcdc335bf2da04c3d56eb
+Subproject commit 8449c5b6ad87ced01bcb82627cb85a8981337210
diff --git a/schemes/tcpd/src/main.rs b/schemes/tcpd/src/main.rs
index 847acb14f..8c7e16e9a 100644
--- a/schemes/tcpd/src/main.rs
+++ b/schemes/tcpd/src/main.rs
@@ -8,7 +8,7 @@ use std::collections::{BTreeMap, VecDeque};
 use std::cell::RefCell;
 use std::fs::File;
 use std::io::{self, Read, Write};
-use std::{mem, slice, str, thread};
+use std::{mem, slice, str};
 use std::os::unix::io::FromRawFd;
 use std::rc::Rc;
 
@@ -652,7 +652,8 @@ impl SchemeMut for Tcpd {
 }
 
 fn main() {
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let scheme_fd = syscall::open(":tcp", O_RDWR | O_CREAT | O_NONBLOCK).expect("tcpd: failed to create :tcp");
         let scheme_file = unsafe { File::from_raw_fd(scheme_fd) };
 
@@ -677,5 +678,5 @@ fn main() {
         event_queue.trigger_all(0).expect("tcpd: failed to trigger event queue");
 
         event_queue.run().expect("tcpd: failed to run event queue");
-    });
+    }
 }
diff --git a/schemes/udpd/src/main.rs b/schemes/udpd/src/main.rs
index ba43abf49..7b39eecc6 100644
--- a/schemes/udpd/src/main.rs
+++ b/schemes/udpd/src/main.rs
@@ -8,7 +8,7 @@ use std::collections::{BTreeMap, VecDeque};
 use std::cell::RefCell;
 use std::fs::File;
 use std::io::{self, Read, Write};
-use std::{mem, slice, str, thread};
+use std::{mem, slice, str};
 use std::os::unix::io::FromRawFd;
 use std::rc::Rc;
 
@@ -319,7 +319,8 @@ impl SchemeMut for Udpd {
 }
 
 fn main() {
-    thread::spawn(move || {
+    // Daemonize
+    if unsafe { syscall::clone(0).unwrap() } == 0 {
         let scheme_fd = syscall::open(":udp", O_RDWR | O_CREAT | O_NONBLOCK).expect("udpd: failed to create :udp");
         let scheme_file = unsafe { File::from_raw_fd(scheme_fd) };
 
@@ -344,5 +345,5 @@ fn main() {
         event_queue.trigger_all(0).expect("udpd: failed to trigger event queue");
 
         event_queue.run().expect("udpd: failed to run event queue");
-    });
+    }
 }
diff --git a/x86_64-unknown-redox.json b/x86_64-unknown-redox.json
index 8878e06ed..49d7037fc 100644
--- a/x86_64-unknown-redox.json
+++ b/x86_64-unknown-redox.json
@@ -22,5 +22,6 @@
     "no-compiler-rt": true,
     "no-default-libraries": true,
     "position-independent-executables": false,
-    "has-elf-tls": true
+    "has-elf-tls": true,
+    "panic-strategy": "abort"
 }
-- 
GitLab