Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Jul 20, 2024
1 parent af7fc45 commit 8ea717a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 18 deletions.
20 changes: 2 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
name: Build
name: Test & Build

on:
push:
tags:
branches:
- '*'
workflow_dispatch:

Expand Down Expand Up @@ -45,19 +45,3 @@ jobs:
- 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 }}
63 changes: 63 additions & 0 deletions .github/workflows/deploy.yml
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 }}
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ edition = "2021"

[dependencies]
libc = "0.2"

[dev-dependencies]
os_pipe = "1.2"
60 changes: 60 additions & 0 deletions examples/example_pipe.rs
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),
}
}

0 comments on commit 8ea717a

Please sign in to comment.