mirror of
https://github.com/stickynotememo/signalstream.git
synced 2026-07-29 22:36:03 +10:00
init
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+102
@@ -0,0 +1,102 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "bitvec"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
|
||||
dependencies = [
|
||||
"funty",
|
||||
"radium",
|
||||
"tap",
|
||||
"wyz",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "funty"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.178"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
|
||||
|
||||
[[package]]
|
||||
name = "radium"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a37d01603c37b5466f808de79f845c7116049b0579adb70a6b7d47c1fa3a952"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
|
||||
dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signalstream"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bitvec",
|
||||
"libc",
|
||||
"signal-hook",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tap"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wyz"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
|
||||
dependencies = [
|
||||
"tap",
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "signalstream"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bitvec = "1.0.1"
|
||||
libc = "0.2.178"
|
||||
signal-hook = "0.4.1"
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
use bitvec::prelude::*;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::os::unix::process;
|
||||
|
||||
use signal_hook::consts::*;
|
||||
use signal_hook::iterator::Signals;
|
||||
pub struct SignalStream {
|
||||
signals: Signals,
|
||||
pid: u32,
|
||||
}
|
||||
|
||||
impl SignalStream {
|
||||
pub fn new(pid: u32) -> Self {
|
||||
Self {
|
||||
signals: Signals::new([SIGUSR1, SIGUSR2]).unwrap(),
|
||||
pid: pid,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Read for SignalStream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let mut n = 0;
|
||||
|
||||
dbg!(std::process::id());
|
||||
|
||||
let pending = self.signals.forever();
|
||||
let mut bitlist = bitvec![];
|
||||
for signal in pending {
|
||||
dbg!(&signal);
|
||||
n += 1;
|
||||
bitlist.push(match signal {
|
||||
SIGUSR1 => false,
|
||||
SIGUSR2 => true,
|
||||
_ => {
|
||||
panic!("Program recieved an unexpected signal, despite it not being registered")
|
||||
}
|
||||
});
|
||||
if n >= buf.len() * 8 {
|
||||
dbg!("broken");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dbg!(bitlist.to_string());
|
||||
|
||||
|
||||
// std::process::exit(1);
|
||||
// if bytelist.len() % buf.len() != 0 {
|
||||
// return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "Could not read enough bits."));
|
||||
// };
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for SignalStream {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
dbg!(std::process::id());
|
||||
for num in buf {
|
||||
let bit_representation = String::from(format!("{num:b}"));
|
||||
for bit in bit_representation.chars() {
|
||||
let signal = match bit
|
||||
.to_digit(2)
|
||||
.expect("Non-binary digit in binary representation")
|
||||
{
|
||||
0 => SIGUSR1,
|
||||
1 => SIGUSR2,
|
||||
_ => panic!("Non-binary digit in binary representation"),
|
||||
};
|
||||
unsafe {
|
||||
libc::kill(self.pid as i32, signal);
|
||||
};
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
}
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::{process::Command, thread::sleep, time::Duration};
|
||||
|
||||
#[test]
|
||||
fn read_test() {
|
||||
let mut sigstream = SignalStream::new(std::process::id());
|
||||
// sigstream.write(&[4]).unwrap();
|
||||
let mut buf = [0; 3];
|
||||
dbg!(sigstream.read(&mut buf).unwrap());
|
||||
dbg!(buf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user