Skip to content

Commit

Permalink
Use std API to create unbound datagram socket
Browse files Browse the repository at this point in the history
Unlike the nix API the std type has clear ownership semantics and thus
takes care to close the socket again.

Previously we leaked an FD here.

Fixes #141
  • Loading branch information
swsnr committed Apr 23, 2023
1 parent 009a47f commit ee15505
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use nix::sys::socket;
use nix::unistd;
use std::io::{self, IoSlice};
use std::os::unix::io::RawFd;
use std::os::unix::net::UnixDatagram;
use std::os::unix::prelude::AsRawFd;
use std::{env, fmt, fs, time};

/// Check for systemd presence at runtime.
Expand Down Expand Up @@ -98,14 +100,7 @@ pub fn notify_with_fds(
.with_context(|| format!("invalid Unix socket path address {}", env_sock))?,
};

let sock_fd = socket::socket(
socket::AddressFamily::Unix,
socket::SockType::Datagram,
socket::SockFlag::empty(),
None,
)
.context("failed to open Unix datagram socket")?;

let socket = UnixDatagram::unbound().context("failed to open Unix datagram socket")?;
let msg = state
.iter()
.fold(String::new(), |res, s| res + &format!("{}\n", s))
Expand All @@ -120,7 +115,7 @@ pub fn notify_with_fds(
};

let sent_len = socket::sendmsg(
sock_fd,
socket.as_raw_fd(),
&[msg_iov],
&ancillary,
socket::MsgFlags::empty(),
Expand Down

0 comments on commit ee15505

Please sign in to comment.