Skip to content

Commit

Permalink
Always use Unix domain sockets for IPC (#1107)
Browse files Browse the repository at this point in the history
Fixes build for ARM targets.
  • Loading branch information
pfoerster authored May 1, 2024
1 parent eb86f20 commit 8d513ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ jobs:
run: cargo test --all-features --no-run --locked
- name: Test
run: cargo test --all-features -- --nocapture --quiet
build_cross:
name: Build (${{ matrix.target }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
target: [aarch64-unknown-linux-gnu, x86_64-unknown-linux-musl]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo install cross
- name: Compile
run: cross build --target ${{ matrix.target }}
msrv:
name: MSRV
runs-on: ubuntu-latest
Expand Down
37 changes: 12 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/ipc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ rust-version.workspace = true

[dependencies]
crossbeam-channel = "0.5.12"
interprocess = "2.0.0"
serde = "1.0.199"
serde_json = "1.0.116"
log = "0.4.21"
uds_windows = "1.1.0"

[lib]
doctest = false
Expand Down
40 changes: 19 additions & 21 deletions crates/ipc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use std::io::{self, BufRead, BufReader, BufWriter};
use std::{
io::{self, BufRead, BufReader, BufWriter, Read},
path::PathBuf,
};

use interprocess::local_socket::{prelude::*, GenericNamespaced, ListenerOptions, Stream};
use serde::{de::DeserializeOwned, Serialize};

const SOCKET_NAME: &str = "texlab.sock";
#[cfg(unix)]
use std::os::unix::net::{UnixListener, UnixStream};

#[cfg(windows)]
use uds_windows::{UnixListener, UnixStream};

fn socket_path() -> PathBuf {
std::env::temp_dir().join("texlab.sock")
}

pub fn send_request<T: Serialize>(msg: T) -> io::Result<()> {
let name = SOCKET_NAME.to_ns_name::<GenericNamespaced>()?;
let conn = Stream::connect(name)?;
let mut conn = BufWriter::new(conn);
let stream = UnixStream::connect(socket_path())?;
let mut conn = BufWriter::new(stream);
serde_json::to_writer(&mut conn, &msg)?;
Ok(())
}
Expand All @@ -18,20 +27,9 @@ where
T: DeserializeOwned,
F: FnMut(T) + Send + 'static,
{
if cfg!(unix) {
let sock_file = std::env::temp_dir().join(format!("{SOCKET_NAME}.sock"));
let _ = std::fs::remove_file(sock_file);
}

let name = SOCKET_NAME.to_ns_name::<GenericNamespaced>()?;
let opts = ListenerOptions::new().name(name);
let listener = match opts.create_sync() {
Err(e) if e.kind() == io::ErrorKind::AddrInUse => {
log::warn!("Unable to open socket {SOCKET_NAME} because it is already in use.");
return Err(e.into());
}
x => x?,
};
let socket_path = socket_path();
let _ = std::fs::remove_file(&socket_path);
let listener = UnixListener::bind(socket_path)?;

std::thread::spawn(move || {
for conn in listener.incoming().flatten() {
Expand All @@ -42,7 +40,7 @@ where
Ok(())
}

fn handle_request<T, F>(conn: Stream, event_handler: &mut F) -> io::Result<()>
fn handle_request<T, F>(conn: impl Read, event_handler: &mut F) -> io::Result<()>
where
T: DeserializeOwned,
F: FnMut(T),
Expand Down

0 comments on commit 8d513ca

Please sign in to comment.