Skip to content

Commit

Permalink
rem cfg glopes wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cataphract committed Sep 17, 2024
1 parent eed4a67 commit 46a6538
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
10 changes: 5 additions & 5 deletions ipc/src/platform/unix/mem_handle_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -109,7 +109,7 @@ impl NamedShmHandle {
Self::new(fd, Some(path), size)
}

pub fn open(path: &CString) -> io::Result<NamedShmHandle> {
pub fn open(path: &CStr) -> io::Result<NamedShmHandle> {
let fd = shm_open(path_slice(path), OFlag::O_RDWR, Mode::empty())?;
Self::new(fd, None, 0)
}
Expand Down
8 changes: 6 additions & 2 deletions remote-config/src/fetch/multitarget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct RuntimeInfo<N: NotifyTarget> {
targets: HashMap<Arc<Target>, u32>,
}

type InProcNotifyFn = extern "C" fn(*const u8, usize);
type InProcNotifyFn = extern "C" fn(*const ConfigInvariants, *const Arc<Target>);
static mut IN_PROC_NOTIFY_FUN: Option<InProcNotifyFn> = None;

#[no_mangle]
Expand Down Expand Up @@ -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
Expand All @@ -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<Target>,
);
}

if notify {
Expand Down
4 changes: 2 additions & 2 deletions sidecar/src/one_way_shared_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,7 +101,7 @@ where
}
}

pub fn open_named_shm(path: &CString) -> io::Result<MappedMem<NamedShmHandle>> {
pub fn open_named_shm(path: &CStr) -> io::Result<MappedMem<NamedShmHandle>> {
NamedShmHandle::open(path)?.map()
}

Expand Down
58 changes: 56 additions & 2 deletions sidecar/src/shm_remote_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -50,12 +49,33 @@ fn path_for_remote_config(id: &ConfigInvariants, target: &Arc<Target>) -> CStrin
.unwrap()
}

#[no_mangle]
extern "C" fn ddog_remote_config_path(
id: *const ConfigInvariants,
target: *const Arc<Target>,
) -> *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<Target>) -> 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
}
Expand All @@ -65,6 +85,40 @@ impl RemoteConfigReader {
}
}

#[no_mangle]
pub extern "C" fn dd_remote_config_new_reader(path: *const c_char) -> Box<RemoteConfigReader> {
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<Target>) -> io::Result<RemoteConfigWriter> {
Ok(RemoteConfigWriter(OneWayShmWriter::<NamedShmHandle>::new(
Expand Down

0 comments on commit 46a6538

Please sign in to comment.