add write()

This commit is contained in:
2025-12-30 11:17:19 +11:00
parent c8e309a256
commit 29cc80f405
+11 -22
View File
@@ -1,6 +1,5 @@
use bitvec::prelude::*; use bitvec::prelude::*;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::os::unix::process;
use signal_hook::consts::*; use signal_hook::consts::*;
use signal_hook::iterator::Signals; use signal_hook::iterator::Signals;
@@ -21,48 +20,41 @@ impl Read for SignalStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut n = 0; let mut n = 0;
dbg!(std::process::id());
let pending = self.signals.forever(); let pending = self.signals.forever();
let mut bitlist = bitvec![]; let mut bitlist = bitvec![];
for signal in pending { for signal in pending {
dbg!(&signal);
n += 1; n += 1;
bitlist.push(match signal { bitlist.push(match signal {
SIGUSR1 => false, SIGUSR1 => false,
SIGUSR2 => true, SIGUSR2 => true,
_ => { _ => {
// This should be impossible, as only the SIGUSR1 and SIGUSR2 signals are
// registered to begin with.
panic!("Program recieved an unexpected signal, despite it not being registered") panic!("Program recieved an unexpected signal, despite it not being registered")
} }
}); });
if n >= buf.len() * 8 { if n >= buf.len() * 8 {
dbg!("broken");
break; break;
} }
} }
dbg!(bitlist.to_string()); // Converts a bitvec to a vec of bytes, with 1/8th the length.
// Adapted from https://stackoverflow.com/a/72572904
for (i, bit) in bitlist.iter().enumerate() { for (i, bit) in bitlist.iter().enumerate() {
let byte = i / 8; let byte = i / 8;
let shift = 7 - i % 8; let shift = 7 - i % 8;
buf[byte] |= ((*bit) as u8) << shift buf[byte] |= ((*bit) as u8) << shift
} }
// 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) Ok(n)
} }
} }
impl Write for SignalStream { impl Write for SignalStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
dbg!(std::process::id());
for num in buf { for num in buf {
let bit_representation = String::from(format!("{num:b}")); let bit_representation = String::from(format!("{num:08b}"));
for bit in bit_representation.chars() { for bit in bit_representation.chars() {
let signal = match bit let signal = match bit
.to_digit(2) .to_digit(2)
@@ -72,10 +64,10 @@ impl Write for SignalStream {
1 => SIGUSR2, 1 => SIGUSR2,
_ => panic!("Non-binary digit in binary representation"), _ => panic!("Non-binary digit in binary representation"),
}; };
unsafe { unsafe {
libc::kill(self.pid as i32, signal); libc::kill(self.pid as i32, signal);
}; };
std::thread::sleep(std::time::Duration::from_millis(100));
} }
} }
Ok(0) Ok(0)
@@ -92,11 +84,8 @@ mod tests {
use std::{process::Command, thread::sleep, time::Duration}; use std::{process::Command, thread::sleep, time::Duration};
#[test] #[test]
fn read_test() { fn read_test() {}
let mut sigstream = SignalStream::new(std::process::id());
// sigstream.write(&[4]).unwrap(); #[test]
let mut buf = [0; 3]; fn write_test() {}
dbg!(sigstream.read(&mut buf).unwrap());
dbg!(buf);
}
} }