shim: release fd when error happens in RemotePublisher::connect()
A RawFd won't be automatically released when going out of scope, so we need to close the just created RawFd when error happens in RemotePublisher::connect(). Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
parent
0eff017c40
commit
01e45800dc
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
//! Implements a client to publish events from the shim back to containerd.
|
//! Implements a client to publish events from the shim back to containerd.
|
||||||
|
|
||||||
use std::os::unix::io::RawFd;
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use containerd_shim_client as client;
|
use containerd_shim_client as client;
|
||||||
|
|
@ -38,18 +37,22 @@ pub struct RemotePublisher {
|
||||||
|
|
||||||
impl RemotePublisher {
|
impl RemotePublisher {
|
||||||
/// Connect to containerd's TTRPC endpoint.
|
/// Connect to containerd's TTRPC endpoint.
|
||||||
|
///
|
||||||
/// containerd uses `/run/containerd/containerd.sock.ttrpc` by default
|
/// containerd uses `/run/containerd/containerd.sock.ttrpc` by default
|
||||||
pub fn new(address: impl AsRef<str>) -> Result<RemotePublisher, Error> {
|
pub fn new(address: impl AsRef<str>) -> Result<RemotePublisher, Error> {
|
||||||
let fd = Self::connect(address)?;
|
let client = Self::connect(address)?;
|
||||||
let client = Client::new(fd);
|
|
||||||
|
|
||||||
Ok(RemotePublisher {
|
Ok(RemotePublisher {
|
||||||
client: EventsClient::new(client),
|
client: EventsClient::new(client),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn connect(address: impl AsRef<str>) -> Result<RawFd, nix::Error> {
|
fn connect(address: impl AsRef<str>) -> Result<Client, nix::Error> {
|
||||||
use nix::sys::socket::*;
|
use nix::sys::socket::*;
|
||||||
|
use nix::unistd::close;
|
||||||
|
|
||||||
|
let unix_addr = UnixAddr::new(address.as_ref())?;
|
||||||
|
let sock_addr = SockAddr::Unix(unix_addr);
|
||||||
|
|
||||||
// SOCK_CLOEXEC flag is Linux specific
|
// SOCK_CLOEXEC flag is Linux specific
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
|
|
@ -65,18 +68,23 @@ impl RemotePublisher {
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
{
|
{
|
||||||
use nix::fcntl::{fcntl, FcntlArg, FdFlag};
|
use nix::fcntl::{fcntl, FcntlArg, FdFlag};
|
||||||
fcntl(fd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))?;
|
fcntl(fd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)).map_err(|e| {
|
||||||
|
let _ = close(fd);
|
||||||
|
e
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let unix_addr = UnixAddr::new(address.as_ref())?;
|
connect(fd, &sock_addr).map_err(|e| {
|
||||||
let sock_addr = SockAddr::Unix(unix_addr);
|
let _ = close(fd);
|
||||||
|
e
|
||||||
|
})?;
|
||||||
|
|
||||||
connect(fd, &sock_addr)?;
|
// Client::new() takes ownership of the RawFd.
|
||||||
|
Ok(Client::new(fd))
|
||||||
Ok(fd)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Publish new event.
|
/// Publish a new event.
|
||||||
|
///
|
||||||
/// Event object can be anything that Protobuf able serialize (e.g. implement `Message` trait).
|
/// Event object can be anything that Protobuf able serialize (e.g. implement `Message` trait).
|
||||||
pub fn publish(
|
pub fn publish(
|
||||||
&self,
|
&self,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue