-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
name: Deploy | ||
|
||
on: | ||
push: | ||
tags: | ||
- '*' | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
test: | ||
uses: ./.github/workflows/test.yml | ||
|
||
build: | ||
name: Test build | ||
runs-on: ${{ matrix.os }} | ||
needs: test | ||
|
||
strategy: | ||
matrix: | ||
include: | ||
- build: linux | ||
os: ubuntu-latest | ||
target: x86_64-unknown-linux-musl | ||
|
||
- build: macos | ||
os: macos-latest | ||
target: x86_64-apple-darwin | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Get the release version from the tag | ||
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV | ||
|
||
- name: Install Rust | ||
uses: dtolnay/rust-toolchain@stable | ||
with: | ||
targets: ${{ matrix.target }} | ||
|
||
- name: Build | ||
run: | | ||
cargo build --release --target ${{ matrix.target }} | ||
publish: | ||
name: Publish | ||
runs-on: ubuntu-latest | ||
needs: | ||
- build | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install Rust | ||
uses: dtolnay/rust-toolchain@stable | ||
|
||
- run: cargo publish --token ${CRATES_TOKEN} | ||
env: | ||
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ edition = "2021" | |
|
||
[dependencies] | ||
libc = "0.2" | ||
|
||
[dev-dependencies] | ||
os_pipe = "1.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/// run with `cargo run --example example_pipe` | ||
use fork::{fork, setsid, Fork}; | ||
use os_pipe::pipe; | ||
use std::io::prelude::*; | ||
use std::process::{exit, Command, Stdio}; | ||
|
||
fn main() { | ||
// Create a pipe for communication | ||
let (mut reader, writer) = pipe().expect("Failed to create pipe"); | ||
|
||
match fork() { | ||
Ok(Fork::Child) => match fork() { | ||
Ok(Fork::Child) => { | ||
setsid().expect("Failed to setsid"); | ||
match Command::new("sleep") | ||
.arg("300") | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
{ | ||
Ok(child) => { | ||
println!("Child pid: {}", child.id()); | ||
|
||
// Write child pid to the pipe | ||
let mut writer = writer; // Shadowing to prevent move errors | ||
writeln!(writer, "{}", child.id()).expect("Failed to write to pipe"); | ||
|
||
exit(0); | ||
} | ||
Err(e) => { | ||
eprintln!("Error running command: {:?}", e); | ||
exit(1); | ||
} | ||
} | ||
} | ||
Ok(Fork::Parent(_)) => exit(0), | ||
Err(e) => { | ||
eprintln!("Error spawning process: {:?}", e); | ||
exit(1) | ||
} | ||
}, | ||
Ok(Fork::Parent(_)) => { | ||
drop(writer); | ||
|
||
// Read the child pid from the pipe | ||
let mut child_pid_str = String::new(); | ||
reader | ||
.read_to_string(&mut child_pid_str) | ||
.expect("Failed to read from pipe"); | ||
|
||
if let Ok(child_pid) = child_pid_str.trim().parse::<i32>() { | ||
println!("Received child pid: {}", child_pid); | ||
} else { | ||
eprintln!("Failed to parse child pid"); | ||
} | ||
} | ||
Err(e) => eprintln!("Error spawning process: {:?}", e), | ||
} | ||
} |