Skip to content

Commit 30f1966

Browse files
committed
Cleanup windows pid shm stuff
Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
1 parent 4fbcb70 commit 30f1966

3 files changed

Lines changed: 9 additions & 29 deletions

File tree

datadog-ipc/src/platform/windows/sockets.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use tokio::net::windows::named_pipe::{NamedPipeClient, NamedPipeServer};
3737
use winapi::shared::minwindef::ULONG;
3838
use winapi::shared::winerror::ERROR_PIPE_CONNECTED;
3939
use winapi::um::handleapi::{CloseHandle, DuplicateHandle, INVALID_HANDLE_VALUE};
40+
use winapi::um::minwinbase::SECURITY_ATTRIBUTES;
4041
use winapi::um::processthreadsapi::{GetCurrentProcess, GetCurrentProcessId, OpenProcess};
4142
use winapi::um::winbase::{GetNamedPipeClientProcessId, GetNamedPipeServerProcessId};
4243
use winapi::um::winnt::{DUPLICATE_SAME_ACCESS, HANDLE, PROCESS_DUP_HANDLE};
@@ -241,6 +242,11 @@ fn create_pipe_server(name: &[u8], first_instance: bool) -> io::Result<OwnedHand
241242

242243
let h = unsafe {
243244
let buf_size = PIPE_BUFFER_SIZE.load(Ordering::Relaxed) as u32;
245+
let mut sec_attributes = SECURITY_ATTRIBUTES {
246+
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
247+
lpSecurityDescriptor: null_mut(),
248+
bInheritHandle: 1, // We want this one to be inherited
249+
};
244250
CreateNamedPipeA(
245251
name.as_ptr(),
246252
open_mode,
@@ -249,7 +255,7 @@ fn create_pipe_server(name: &[u8], first_instance: bool) -> io::Result<OwnedHand
249255
buf_size,
250256
buf_size,
251257
0,
252-
null_mut(),
258+
&mut sec_attributes as *mut SECURITY_ATTRIBUTES as *mut _,
253259
)
254260
};
255261

datadog-sidecar/src/setup/windows.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::setup::Liaison;
66
use datadog_ipc::platform::PIPE_PATH;
77
use datadog_ipc::{SeqpacketConn, SeqpacketListener};
88
use libc::getpid;
9-
use std::ffi::CString;
109
use std::io;
1110

1211
pub type IpcClient = SeqpacketConn;
@@ -69,11 +68,6 @@ impl Default for NamedPipeLiaison {
6968

7069
pub type DefaultLiason = NamedPipeLiaison;
7170

72-
/// Helper: derive the shared-memory path used to publish the sidecar PID.
73-
pub fn pid_shm_path(pipe_path: &str) -> CString {
74-
#[allow(clippy::unwrap_used)]
75-
CString::new(&pipe_path[PIPE_PATH.len() - 1..]).unwrap()
76-
}
7771

7872
#[cfg(test)]
7973
mod tests {

datadog-sidecar/src/windows.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::enter_listener_loop;
5-
use crate::setup::pid_shm_path;
6-
use datadog_ipc::platform::{
7-
named_pipe_name_from_raw_handle, FileBackedHandle, MappedMem, NamedShmHandle,
8-
};
5+
use datadog_ipc::platform::named_pipe_name_from_raw_handle;
96
use datadog_ipc::{SeqpacketConn, SeqpacketListener};
107

118
use futures::FutureExt;
@@ -62,19 +59,6 @@ pub extern "C" fn ddog_daemon_entry_point(_trampoline_data: &TrampolineData) {
6259
let pid = unsafe { libc::getpid() };
6360

6461
if let Some(handle) = spawn_worker::recv_passed_handle() {
65-
let mut shm = match named_pipe_name_from_raw_handle(handle.as_raw_handle())
66-
.ok_or(io::Error::from(io::ErrorKind::InvalidInput))
67-
.and_then(|name| NamedShmHandle::create(pid_shm_path(&name), 4))
68-
.and_then(FileBackedHandle::map)
69-
{
70-
Ok(ok) => ok,
71-
Err(err) => {
72-
error!("Couldn't store pid to shared memory: {err}");
73-
return;
74-
}
75-
};
76-
shm.as_slice_mut().copy_from_slice(&pid.to_ne_bytes());
77-
7862
info!("Starting sidecar, pid: {}", pid);
7963

8064
let acquire_listener = move || {
@@ -88,10 +72,8 @@ pub extern "C" fn ddog_daemon_entry_point(_trampoline_data: &TrampolineData) {
8872
}
8973
};
9074

91-
// We pass the shm to ensure we drop the shm handle with the pid immediately after
92-
// cancellation To avoid actual race conditions
9375
Ok((
94-
|handler| accept_socket_loop(listener, closed_future, handler, shm),
76+
|handler| accept_socket_loop(listener, closed_future, handler),
9577
cancel,
9678
))
9779
};
@@ -112,7 +94,6 @@ async fn accept_socket_loop(
11294
listener: SeqpacketListener,
11395
cancellation: ManualFuture<()>,
11496
handler: Box<dyn Fn(SeqpacketConn)>,
115-
_: MappedMem<NamedShmHandle>,
11697
) -> io::Result<()> {
11798
// Wrap the first server instance as a Tokio NamedPipeServer for async connect polling.
11899
// After each accepted connection we create a fresh Tokio server for the next client.
@@ -144,7 +125,6 @@ async fn accept_socket_loop(
144125
let conn = SeqpacketConn::from_server_handle(owned, client_pid);
145126
handler(conn);
146127
}
147-
// drops pipe and shm here
148128
Ok(())
149129
}
150130

0 commit comments

Comments
 (0)