Skip to content

Commit 1b47c15

Browse files
ZexbeJoshua Nelson
authored and
Joshua Nelson
committed
Only use fork on non-windows OS
1 parent a89864a commit 1b47c15

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/utils/daemon.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55

66
use std::{env, thread};
77
use std::panic::{catch_unwind, AssertUnwindSafe};
8-
use std::process::exit;
9-
use std::fs::File;
10-
use std::io::Write;
118
use std::time::Duration;
129
use std::path::PathBuf;
13-
use libc::fork;
1410
use time;
1511
use docbuilder::RustwideBuilder;
1612
use DocBuilderOptions;
1713
use DocBuilder;
1814
use utils::{update_release_activity, github_updater, pubsubhubbub};
1915
use db::{connect_db, update_search_index};
2016

21-
17+
#[cfg(not(target_os = "windows"))]
18+
use ::{
19+
libc::fork,
20+
std::process::exit,
21+
std::fs::File,
22+
std::io::Write
23+
};
2224

2325
pub fn start_daemon(background: bool) {
2426
// first check required environment variables
@@ -36,15 +38,22 @@ pub fn start_daemon(background: bool) {
3638
dbopts.check_paths().unwrap();
3739

3840
if background {
39-
// fork the process
40-
let pid = unsafe { fork() };
41-
if pid > 0 {
42-
let mut file = File::create(dbopts.prefix.join("cratesfyi.pid"))
43-
.expect("Failed to create pid file");
44-
writeln!(&mut file, "{}", pid).expect("Failed to write pid");
45-
46-
info!("cratesfyi {} daemon started on: {}", ::BUILD_VERSION, pid);
47-
exit(0);
41+
#[cfg(target_os = "windows")]
42+
{
43+
panic!("running in background not supported on windows");
44+
}
45+
#[cfg(not(target_os = "windows"))]
46+
{
47+
// fork the process
48+
let pid = unsafe { fork() };
49+
if pid > 0 {
50+
let mut file = File::create(dbopts.prefix.join("cratesfyi.pid"))
51+
.expect("Failed to create pid file");
52+
writeln!(&mut file, "{}", pid).expect("Failed to write pid");
53+
54+
info!("cratesfyi {} daemon started on: {}", ::BUILD_VERSION, pid);
55+
exit(0);
56+
}
4857
}
4958
}
5059

0 commit comments

Comments
 (0)