From 46a65388dc11242774e48281eb1fc62866e9898e Mon Sep 17 00:00:00 2001 From: Gustavo Lopes Date: Tue, 17 Sep 2024 17:21:31 +0100 Subject: [PATCH] rem cfg glopes wip --- ipc/src/platform/unix/mem_handle_macos.rs | 10 ++-- remote-config/src/fetch/multitarget.rs | 8 +++- sidecar/src/one_way_shared_memory.rs | 4 +- sidecar/src/shm_remote_config.rs | 58 ++++++++++++++++++++++- 4 files changed, 69 insertions(+), 11 deletions(-) diff --git a/ipc/src/platform/unix/mem_handle_macos.rs b/ipc/src/platform/unix/mem_handle_macos.rs index 014c98833..2f0d600cc 100644 --- a/ipc/src/platform/unix/mem_handle_macos.rs +++ b/ipc/src/platform/unix/mem_handle_macos.rs @@ -10,7 +10,7 @@ use nix::fcntl::OFlag; use nix::sys::mman::{mmap, munmap, shm_open, shm_unlink, MapFlags, ProtFlags}; use nix::sys::stat::Mode; use nix::unistd::ftruncate; -use std::ffi::CString; +use std::ffi::{CStr, CString}; use std::io; use std::num::NonZeroUsize; use std::os::unix::prelude::{AsRawFd, FromRawFd, RawFd}; @@ -87,9 +87,9 @@ impl ShmHandle { }) } } -fn path_slice(path: &CString) -> &[u8] { - assert_eq!(path.as_bytes()[0], b'/'); - &path.as_bytes()[1..] +fn path_slice(path: &CStr) -> &[u8] { + assert_eq!(path.to_bytes()[0], b'/'); + &path.to_bytes()[1..] } impl NamedShmHandle { @@ -109,7 +109,7 @@ impl NamedShmHandle { Self::new(fd, Some(path), size) } - pub fn open(path: &CString) -> io::Result { + pub fn open(path: &CStr) -> io::Result { let fd = shm_open(path_slice(path), OFlag::O_RDWR, Mode::empty())?; Self::new(fd, None, 0) } diff --git a/remote-config/src/fetch/multitarget.rs b/remote-config/src/fetch/multitarget.rs index 6e887eab7..70c4a43c9 100644 --- a/remote-config/src/fetch/multitarget.rs +++ b/remote-config/src/fetch/multitarget.rs @@ -90,7 +90,7 @@ struct RuntimeInfo { targets: HashMap, u32>, } -type InProcNotifyFn = extern "C" fn(*const u8, usize); +type InProcNotifyFn = extern "C" fn(*const ConfigInvariants, *const Arc); static mut IN_PROC_NOTIFY_FUN: Option = None; #[no_mangle] @@ -370,6 +370,7 @@ where let (remove_future, remove_completer) = ManualFuture::new(); let shared_future = remove_future.shared(); + let invariants = this.storage.invariants().clone(); let inner_fetcher = fetcher.clone(); let inner_this = this.clone(); let fetcher_fut = fetcher @@ -384,7 +385,10 @@ where ); if let Some(in_proc_notify) = unsafe { IN_PROC_NOTIFY_FUN } { - in_proc_notify(runtime_id.as_ptr(), runtime_id.len()); + in_proc_notify( + &invariants as *const ConfigInvariants, + &inner_fetcher.target as *const Arc, + ); } if notify { diff --git a/sidecar/src/one_way_shared_memory.rs b/sidecar/src/one_way_shared_memory.rs index d5490dcba..608ea5452 100644 --- a/sidecar/src/one_way_shared_memory.rs +++ b/sidecar/src/one_way_shared_memory.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use datadog_ipc::platform::{FileBackedHandle, MappedMem, NamedShmHandle, ShmHandle}; -use std::ffi::CString; +use std::ffi::{CStr, CString}; use std::io; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Mutex; @@ -101,7 +101,7 @@ where } } -pub fn open_named_shm(path: &CString) -> io::Result> { +pub fn open_named_shm(path: &CStr) -> io::Result> { NamedShmHandle::open(path)?.map() } diff --git a/sidecar/src/shm_remote_config.rs b/sidecar/src/shm_remote_config.rs index aa6dfc6aa..6c40ce2ee 100644 --- a/sidecar/src/shm_remote_config.rs +++ b/sidecar/src/shm_remote_config.rs @@ -21,10 +21,9 @@ use std::cmp::Reverse; use std::collections::hash_map::Entry; use std::collections::HashMap; use std::default::Default; -use std::ffi::{CStr, CString}; +use std::ffi::{c_char, CStr, CString}; use std::hash::{Hash, Hasher}; use std::io; -use std::io::Write; use std::str::FromStr; use std::sync::atomic::Ordering; use std::sync::{Arc, Mutex}; @@ -50,12 +49,33 @@ fn path_for_remote_config(id: &ConfigInvariants, target: &Arc) -> CStrin .unwrap() } +#[no_mangle] +extern "C" fn ddog_remote_config_path( + id: *const ConfigInvariants, + target: *const Arc, +) -> *mut c_char { + let id = unsafe { &*id }; + let target = unsafe { &*target }; + path_for_remote_config(id, target).into_raw() +} +#[no_mangle] +extern "C" fn ddog_remote_config_path_free(path: *mut c_char) { + drop(unsafe { CString::from_raw(path) }); +} + impl RemoteConfigReader { pub fn new(id: &ConfigInvariants, target: &Arc) -> RemoteConfigReader { let path = path_for_remote_config(id, target); RemoteConfigReader(OneWayShmReader::new(open_named_shm(&path).ok(), path)) } + pub fn from_path(path: &CStr) -> Self { + RemoteConfigReader(OneWayShmReader::new( + open_named_shm(path).ok(), + CString::new(path.to_bytes()).unwrap(), + )) + } + pub fn get_path(&self) -> &CStr { &self.0.extra } @@ -65,6 +85,40 @@ impl RemoteConfigReader { } } +#[no_mangle] +pub extern "C" fn dd_remote_config_new_reader(path: *const c_char) -> Box { + Box::new(RemoteConfigReader::from_path(unsafe { + CStr::from_ptr(path) + })) +} + +#[no_mangle] +pub extern "C" fn dd_remote_config_reader_drop(reader: *mut RemoteConfigReader) { + if !reader.is_null() { + unsafe { + drop(Box::from_raw(reader)); + } + } +} + +#[no_mangle] +pub extern "C" fn dd_remote_config_reader_read( + reader: *mut RemoteConfigReader, + buf: *mut *const u8, + len: *mut usize, +) -> bool { + if reader.is_null() || buf.is_null() || len.is_null() { + return false; + } + let reader = unsafe { &mut *reader }; + let (changed, data) = reader.read(); + unsafe { + *buf = data.as_ptr(); + *len = data.len(); + } + changed +} + impl RemoteConfigWriter { pub fn new(id: &ConfigInvariants, target: &Arc) -> io::Result { Ok(RemoteConfigWriter(OneWayShmWriter::::new(