aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorVineeth Sagar <39757522+vsag96@users.noreply.github.com>2018-12-07 03:08:34 +0530
committerChristian Duerr <chrisduerr@users.noreply.github.com>2018-12-06 21:38:34 +0000
commitcadbb86eb78854d4aff2e7b38089d75067e41b96 (patch)
treeb5ef6bb926c1dd1bd638202b754090b9104a6501 /src/util.rs
parentf57bd6e12f7e94b767b34a208c66dc88c93f704e (diff)
downloadalacritty-cadbb86eb78854d4aff2e7b38089d75067e41b96.tar.gz
alacritty-cadbb86eb78854d4aff2e7b38089d75067e41b96.zip
Detach Child process to avoid zombie processes
This makes use of the common double-fork behavior to prevent spawning zombie processes every time a URL is clicked.
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs43
1 files changed, 28 insertions, 15 deletions
diff --git a/src/util.rs b/src/util.rs
index 6065e01b..3b981aa6 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -11,25 +11,20 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-use std::cmp;
-
-#[cfg(not(feature = "nightly"))]
-#[inline(always)]
-pub unsafe fn unlikely(x: bool) -> bool {
- x
-}
-
-#[cfg(feature = "nightly")]
-pub use ::std::intrinsics::unlikely;
+#[cfg(not(windows))]
+use std::os::unix::process::CommandExt;
+use std::process::Command;
+use std::{cmp, io};
/// Threading utilities
pub mod thread {
/// Like `thread::spawn`, but with a `name` argument
pub fn spawn_named<F, T, S>(name: S, f: F) -> ::std::thread::JoinHandle<T>
- where F: FnOnce() -> T,
- F: Send + 'static,
- T: Send + 'static,
- S: Into<String>
+ where
+ F: FnOnce() -> T,
+ F: Send + 'static,
+ T: Send + 'static,
+ S: Into<String>,
{
::std::thread::Builder::new()
.name(name.into())
@@ -37,7 +32,7 @@ pub mod thread {
.expect("thread spawn works")
}
- pub use ::std::thread::*;
+ pub use std::thread::*;
}
pub fn limit<T: Ord>(value: T, min: T, max: T) -> T {
@@ -81,6 +76,24 @@ pub mod fmt {
}
}
+#[cfg(not(windows))]
+pub fn start_daemon(program: &str, args: &[String]) -> io::Result<()> {
+ Command::new(program)
+ .args(args)
+ .before_exec(|| unsafe {
+ ::libc::daemon(1, 0);
+ Ok(())
+ })
+ .spawn()?
+ .wait()
+ .map(|_| ())
+}
+
+#[cfg(windows)]
+pub fn start_daemon(program: &str, args: &[String]) -> io::Result<()> {
+ Command::new(program).args(args).spawn().map(|_| ())
+}
+
#[cfg(test)]
mod tests {
use super::limit;