Skip to content

Commit

Permalink
remove nix
Browse files Browse the repository at this point in the history
  • Loading branch information
f1shl3gs committed May 25, 2024
1 parent 31fc8b0 commit c93e98b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 66 deletions.
33 changes: 2 additions & 31 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,6 @@ serde_path_to_error = { version = "0.1.16", default-features = false }
testify = { path = "lib/testify" }
tokio = { version = "1.37.0", default-features = false, features = ["test-util", "time"] }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", default-features = false, features = ["net"] }

[dependencies]
# Workspaces
buffers = { path = "lib/buffers", optional = true }
Expand Down
4 changes: 1 addition & 3 deletions lib/framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ indexmap = { version = "2.2.6", default-features = false, features = ["serde"] }
inventory = { version = "0.3.15", default-features = false }
ipnet = { version = "2.9.0", default-features = false, features = ["std"] }
listenfd = { version = "1.0.1", default-features = false, optional = true }
libc = { version = "0.2.155", default-features = false }
log_schema = { path = "../log_schema" }
bytesize = { path = "../bytesize" }
memchr = { version = "2.7.2", default-features = false }
Expand Down Expand Up @@ -89,6 +90,3 @@ twox-hash = { version = "1.6.3", default-features = false }
typetag = { version = "0.2.16" }
url = { version = "2.5.0", features = ["serde"] }
zstd = { version = "0.13.1", default-features = false }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", default-features = false, features = ["signal"] }
12 changes: 6 additions & 6 deletions lib/framework/src/config/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ pub fn spawn_thread<'a>(
}

fn raise_sighup() {
use nix::sys::signal;

let _ = signal::raise(signal::Signal::SIGHUP).map_err(|err| {
let ret = unsafe { libc::raise(libc::SIGHUP) };
if ret == -1 {
let err = std::io::Error::last_os_error();
error!(
message = "Unable to reload configuration file. Restart Vertex to reload it",
cause = %err
)
});
?err
);
}
}

fn create_watcher(
Expand Down
78 changes: 59 additions & 19 deletions src/extensions/heartbeat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ops::Deref;
use std::time::Duration;

Expand All @@ -12,7 +13,6 @@ use framework::tls::TlsConfig;
use framework::{Extension, ShutdownSignal};
use http::Request;
use hyper::Body;
use nix::net::if_::InterfaceFlags;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use serde::Serialize;
Expand Down Expand Up @@ -53,7 +53,7 @@ impl ExtensionConfig for Config {
status.version = crate::get_version();
status.lease = humanize::duration::duration(&(self.interval + Duration::from_secs(15)));
status.os = sysinfo::os().unwrap_or_default();
status.address = get_advertise_addr()?;
status.address = get_advertise_addr()?.to_string();
status.kernel = sysinfo::kernel().unwrap_or_default();
status.tags = self.tags.clone();

Expand Down Expand Up @@ -184,27 +184,67 @@ async fn run(
}
}

fn get_advertise_addr() -> std::io::Result<String> {
let ifaddrs = nix::ifaddrs::getifaddrs()?;
fn get_advertise_addr() -> std::io::Result<IpAddr> {
let mut advertised = None;

let addrs = ifaddrs
.filter_map(|addr| {
if addr.flags.intersects(InterfaceFlags::IFF_LOOPBACK)
|| !addr.flags.intersects(InterfaceFlags::IFF_RUNNING)
{
return None;
unsafe {
let mut addrs = std::mem::MaybeUninit::<*mut libc::ifaddrs>::uninit();
let ret = libc::getifaddrs(addrs.as_mut_ptr());
if ret == -1 {
panic!("{}", std::io::Error::last_os_error());
}

let base = addrs.assume_init();
let mut next = addrs.assume_init();

while let Some(addr) = next.as_ref() {
if addr.ifa_flags & libc::IFF_LOOPBACK as libc::c_uint != 0 {
next = addr.ifa_next;
continue;
}

if addr.ifa_flags & libc::IFF_RUNNING as libc::c_uint == 0 {
next = addr.ifa_next;
continue;
}

let sockaddr = addr.address?.as_sockaddr_in()?.ip();
Some(sockaddr.to_string())
})
.collect::<Vec<_>>();
if addr.ifa_addr.is_null() {
next = addr.ifa_next;
continue;
}

Ok(if addrs.is_empty() {
"".to_string()
} else {
addrs[0].to_string()
})
match (*addr.ifa_addr).sa_family as libc::c_int {
libc::AF_INET => {
let sockaddr: libc::sockaddr_in =
std::ptr::read_unaligned(addr.ifa_addr as *const _);
let ip = Ipv4Addr::from(sockaddr.sin_addr.s_addr.to_ne_bytes());
advertised = Some(IpAddr::V4(ip));
break;
}
libc::AF_INET6 => {
let sockaddr: libc::sockaddr_in6 =
std::ptr::read_unaligned(addr.ifa_addr as *const _);
let ip = Ipv6Addr::from(sockaddr.sin6_addr.s6_addr);
advertised = Some(IpAddr::V6(ip));
break;
}
_ => {
next = addr.ifa_next;
continue;
}
}
}

libc::freeifaddrs(base);
}

match advertised {
Some(addr) => Ok(addr),
None => Err(std::io::Error::new(
std::io::ErrorKind::AddrNotAvailable,
"cannot find a valid addr",
)),
}
}

#[cfg(test)]
Expand Down
6 changes: 2 additions & 4 deletions src/sources/journald.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use framework::shutdown::ShutdownSignal;
use framework::Source;
use futures::{stream::BoxStream, StreamExt};
use log_schema::log_schema;
use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid;
use tokio::fs::OpenOptions;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use tokio::{process::Command, time::sleep};
Expand Down Expand Up @@ -383,9 +381,9 @@ fn start_journalctl(
let mut child = command.spawn()?;
let stream = FramedRead::new(child.stdout.take().unwrap(), EntryCodec::new()).boxed();

let pid = Pid::from_raw(child.id().unwrap() as _);
let pid = child.id().unwrap();
let stop = Box::new(move || {
let _ = kill(pid, Signal::SIGTERM);
let _ = unsafe { libc::kill(pid as libc::pid_t, libc::SIGTERM) };
});

Ok((stream, stop))
Expand Down

0 comments on commit c93e98b

Please sign in to comment.