ipckit 0.1.6

A cross-platform IPC (Inter-Process Communication) library for Rust and Python
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Shared Memory implementation for IPC
//!
//! Provides memory-mapped shared memory regions for fast data exchange between processes.

use crate::error::{IpcError, Result};
use std::ptr::NonNull;

/// Shared memory region for inter-process communication
pub struct SharedMemory {
    name: String,
    ptr: NonNull<u8>,
    size: usize,
    is_owner: bool,
    #[cfg(unix)]
    fd: std::os::unix::io::RawFd,
    #[cfg(windows)]
    handle: windows_sys::Win32::Foundation::HANDLE,
}

// Safety: SharedMemory uses proper synchronization
unsafe impl Send for SharedMemory {}
unsafe impl Sync for SharedMemory {}

impl SharedMemory {
    /// Create a new shared memory region with the given name and size
    ///
    /// The name should be unique across the system. On Unix, it will be prefixed
    /// with `/` if not already. On Windows, it will be used as-is.
    pub fn create(name: &str, size: usize) -> Result<Self> {
        if size == 0 {
            return Err(IpcError::InvalidName("Size must be greater than 0".into()));
        }

        #[cfg(unix)]
        {
            unix::create_shm(name, size)
        }
        #[cfg(windows)]
        {
            windows::create_shm(name, size)
        }
    }

    /// Open an existing shared memory region
    pub fn open(name: &str) -> Result<Self> {
        #[cfg(unix)]
        {
            unix::open_shm(name)
        }
        #[cfg(windows)]
        {
            windows::open_shm(name)
        }
    }

    /// Get the name of the shared memory region
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the size of the shared memory region
    pub fn size(&self) -> usize {
        self.size
    }

    /// Check if this instance is the owner (creator) of the shared memory
    pub fn is_owner(&self) -> bool {
        self.is_owner
    }

    /// Get a pointer to the shared memory
    ///
    /// # Safety
    /// The caller must ensure proper synchronization when accessing the memory.
    pub fn as_ptr(&self) -> *const u8 {
        self.ptr.as_ptr()
    }

    /// Get a mutable pointer to the shared memory
    ///
    /// # Safety
    /// The caller must ensure proper synchronization when accessing the memory.
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.ptr.as_ptr()
    }

    /// Get a slice view of the shared memory
    ///
    /// # Safety
    /// The caller must ensure no other process is writing to this region.
    pub unsafe fn as_slice(&self) -> &[u8] {
        std::slice::from_raw_parts(self.ptr.as_ptr(), self.size)
    }

    /// Get a mutable slice view of the shared memory
    ///
    /// # Safety
    /// The caller must ensure exclusive access to this region.
    pub unsafe fn as_mut_slice(&mut self) -> &mut [u8] {
        std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.size)
    }

    /// Write data to the shared memory at the given offset
    ///
    /// Returns error if offset + data.len() exceeds the size.
    pub fn write(&mut self, offset: usize, data: &[u8]) -> Result<()> {
        if offset + data.len() > self.size {
            return Err(IpcError::BufferTooSmall {
                needed: offset + data.len(),
                got: self.size,
            });
        }

        unsafe {
            std::ptr::copy_nonoverlapping(data.as_ptr(), self.ptr.as_ptr().add(offset), data.len());
        }
        Ok(())
    }

    /// Read data from the shared memory at the given offset
    ///
    /// Returns error if offset + len exceeds the size.
    pub fn read(&self, offset: usize, len: usize) -> Result<Vec<u8>> {
        if offset + len > self.size {
            return Err(IpcError::BufferTooSmall {
                needed: offset + len,
                got: self.size,
            });
        }

        let mut buf = vec![0u8; len];
        unsafe {
            std::ptr::copy_nonoverlapping(self.ptr.as_ptr().add(offset), buf.as_mut_ptr(), len);
        }
        Ok(buf)
    }

    /// Read data into an existing buffer
    pub fn read_into(&self, offset: usize, buf: &mut [u8]) -> Result<()> {
        if offset + buf.len() > self.size {
            return Err(IpcError::BufferTooSmall {
                needed: offset + buf.len(),
                got: self.size,
            });
        }

        unsafe {
            std::ptr::copy_nonoverlapping(
                self.ptr.as_ptr().add(offset),
                buf.as_mut_ptr(),
                buf.len(),
            );
        }
        Ok(())
    }
}

impl Drop for SharedMemory {
    fn drop(&mut self) {
        #[cfg(unix)]
        {
            unsafe {
                libc::munmap(self.ptr.as_ptr() as *mut _, self.size);
                libc::close(self.fd);
                if self.is_owner {
                    let c_name = std::ffi::CString::new(self.name.clone()).unwrap();
                    libc::shm_unlink(c_name.as_ptr());
                }
            }
        }
        #[cfg(windows)]
        {
            unsafe {
                use windows_sys::Win32::System::Memory::MEMORY_MAPPED_VIEW_ADDRESS;
                let addr = MEMORY_MAPPED_VIEW_ADDRESS {
                    Value: self.ptr.as_ptr() as *mut _,
                };
                windows_sys::Win32::System::Memory::UnmapViewOfFile(addr);
                windows_sys::Win32::Foundation::CloseHandle(self.handle);
            }
        }
    }
}

#[cfg(unix)]
mod unix {
    use super::*;
    use std::ffi::CString;

    pub fn create_shm(name: &str, size: usize) -> Result<SharedMemory> {
        let shm_name = if name.starts_with('/') {
            name.to_string()
        } else {
            format!("/{}", name)
        };

        let c_name = CString::new(shm_name.clone())
            .map_err(|_| IpcError::InvalidName("Invalid shared memory name".into()))?;

        // Create shared memory object
        let fd = unsafe {
            libc::shm_open(
                c_name.as_ptr(),
                libc::O_CREAT | libc::O_RDWR | libc::O_EXCL,
                0o666,
            )
        };

        if fd < 0 {
            let err = std::io::Error::last_os_error();
            return Err(match err.kind() {
                std::io::ErrorKind::AlreadyExists => IpcError::AlreadyExists(shm_name),
                std::io::ErrorKind::PermissionDenied => IpcError::PermissionDenied(shm_name),
                _ => IpcError::Io(err),
            });
        }

        // Set size
        if unsafe { libc::ftruncate(fd, size as libc::off_t) } < 0 {
            unsafe {
                libc::close(fd);
                libc::shm_unlink(c_name.as_ptr());
            }
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        // Map memory
        let ptr = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                size,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_SHARED,
                fd,
                0,
            )
        };

        if ptr == libc::MAP_FAILED {
            unsafe {
                libc::close(fd);
                libc::shm_unlink(c_name.as_ptr());
            }
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        Ok(SharedMemory {
            name: shm_name,
            ptr: NonNull::new(ptr as *mut u8).unwrap(),
            size,
            is_owner: true,
            fd,
        })
    }

    pub fn open_shm(name: &str) -> Result<SharedMemory> {
        let shm_name = if name.starts_with('/') {
            name.to_string()
        } else {
            format!("/{}", name)
        };

        let c_name = CString::new(shm_name.clone())
            .map_err(|_| IpcError::InvalidName("Invalid shared memory name".into()))?;

        // Open existing shared memory object
        let fd = unsafe { libc::shm_open(c_name.as_ptr(), libc::O_RDWR, 0) };

        if fd < 0 {
            let err = std::io::Error::last_os_error();
            return Err(match err.kind() {
                std::io::ErrorKind::NotFound => IpcError::NotFound(shm_name),
                std::io::ErrorKind::PermissionDenied => IpcError::PermissionDenied(shm_name),
                _ => IpcError::Io(err),
            });
        }

        // Get size
        let mut stat: libc::stat = unsafe { std::mem::zeroed() };
        if unsafe { libc::fstat(fd, &mut stat) } < 0 {
            unsafe { libc::close(fd) };
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }
        let size = stat.st_size as usize;

        // Map memory
        let ptr = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                size,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_SHARED,
                fd,
                0,
            )
        };

        if ptr == libc::MAP_FAILED {
            unsafe { libc::close(fd) };
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        Ok(SharedMemory {
            name: shm_name,
            ptr: NonNull::new(ptr as *mut u8).unwrap(),
            size,
            is_owner: false,
            fd,
        })
    }
}

#[cfg(windows)]
mod windows {
    use super::*;
    use std::ffi::OsStr;
    use std::os::windows::ffi::OsStrExt;
    use std::ptr;
    use windows_sys::Win32::Foundation::*;
    use windows_sys::Win32::System::Memory::*;

    fn to_wide(s: &str) -> Vec<u16> {
        OsStr::new(s).encode_wide().chain(Some(0)).collect()
    }

    pub fn create_shm(name: &str, size: usize) -> Result<SharedMemory> {
        let wide_name = to_wide(name);

        let handle = unsafe {
            CreateFileMappingW(
                INVALID_HANDLE_VALUE,
                ptr::null(),
                PAGE_READWRITE,
                (size >> 32) as u32,
                size as u32,
                wide_name.as_ptr(),
            )
        };

        if handle.is_null() {
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        // Check if it already existed
        let last_error = unsafe { GetLastError() };
        if last_error == ERROR_ALREADY_EXISTS {
            unsafe { CloseHandle(handle) };
            return Err(IpcError::AlreadyExists(name.to_string()));
        }

        let mapped = unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, size) };

        if mapped.Value.is_null() {
            unsafe { CloseHandle(handle) };
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        Ok(SharedMemory {
            name: name.to_string(),
            ptr: NonNull::new(mapped.Value as *mut u8).unwrap(),
            size,
            is_owner: true,
            handle,
        })
    }

    pub fn open_shm(name: &str) -> Result<SharedMemory> {
        let wide_name = to_wide(name);

        let handle = unsafe { OpenFileMappingW(FILE_MAP_ALL_ACCESS, 0, wide_name.as_ptr()) };

        if handle.is_null() {
            let err = std::io::Error::last_os_error();
            return Err(match err.raw_os_error() {
                Some(2) => IpcError::NotFound(name.to_string()),
                Some(5) => IpcError::PermissionDenied(name.to_string()),
                _ => IpcError::Io(err),
            });
        }

        // Map the entire file mapping
        let mapped = unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0) };

        if mapped.Value.is_null() {
            unsafe { CloseHandle(handle) };
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        // Get the size using VirtualQuery
        let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() };
        let ret = unsafe {
            VirtualQuery(
                mapped.Value,
                &mut info,
                std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
            )
        };

        if ret == 0 {
            unsafe {
                UnmapViewOfFile(mapped);
                CloseHandle(handle);
            }
            return Err(IpcError::Io(std::io::Error::last_os_error()));
        }

        Ok(SharedMemory {
            name: name.to_string(),
            ptr: NonNull::new(mapped.Value as *mut u8).unwrap(),
            size: info.RegionSize,
            is_owner: false,
            handle,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_shared_memory_create_and_write() {
        let name = format!("test_shm_{}", std::process::id());
        let mut shm = SharedMemory::create(&name, 1024).unwrap();

        let data = b"Hello, shared memory!";
        shm.write(0, data).unwrap();

        let read_data = shm.read(0, data.len()).unwrap();
        assert_eq!(read_data, data);
    }

    #[test]
    fn test_shared_memory_boundary() {
        let name = format!("test_shm_boundary_{}", std::process::id());
        let mut shm = SharedMemory::create(&name, 100).unwrap();

        // Should fail - writing beyond boundary
        let result = shm.write(90, &[0u8; 20]);
        assert!(result.is_err());
    }
}