-
Notifications
You must be signed in to change notification settings - Fork 679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add System V shared memory APIs #1989
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ use crate::Result; | |
#[cfg(not(target_os = "android"))] | ||
#[cfg(feature = "fs")] | ||
use crate::{fcntl::OFlag, sys::stat::Mode}; | ||
use libc::{self, c_int, c_void, off_t, size_t}; | ||
use libc::{self, c_int, c_void, key_t, off_t, shmid_ds, size_t}; | ||
use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}}; | ||
|
||
libc_bitflags! { | ||
|
@@ -603,3 +603,64 @@ pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> { | |
|
||
Errno::result(ret).map(drop) | ||
} | ||
|
||
/// Creates and returns a new, or returns an existing, System V shared memory | ||
/// segment identifier. | ||
/// | ||
/// For more information, see [`shmget(2)`]. | ||
/// | ||
/// [`shmget(2)`]: https://man7.org/linux/man-pages/man2/shmget.2.html | ||
pub fn shmget(key: key_t, size: size_t, shmflg: c_int) -> Result<c_int> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the very least the result type should be a newtype pattern. I.e: -> Result<ShmKey> where #[derive(Debug, PartialEq, Eq, Hash)]
struct ShmKey(c_int)
impl ShmKey {
unsafe fn to_raw(&self) -> c_int {
self.0
}
// unsafe from_raw, other methods
} This helps ensure that you will get sensible compiler errors. |
||
Errno::result(unsafe { libc::shmget(key, size, shmflg) }) | ||
} | ||
|
||
/// Attaches the System V shared memory segment identified by `shmid` to the | ||
/// address space of the calling process. | ||
/// | ||
/// For more information, see [`shmat(2)`]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// `shmid` should be a valid shared memory identifier and | ||
/// `shmaddr` must meet the requirements described in the [`shmat(2)`] man page. | ||
/// | ||
/// [`shmat(2)`]: https://man7.org/linux/man-pages/man2/shmat.2.html | ||
pub unsafe fn shmat( | ||
shmid: c_int, | ||
shmaddr: *const c_void, | ||
shmflg: c_int, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again: |
||
) -> Result<*mut c_void> { | ||
Errno::result(libc::shmat(shmid, shmaddr, shmflg)) | ||
} | ||
|
||
/// Performs the reverse of [`shmat`], detaching the shared memory segment at | ||
/// the given address from the address space of the calling process. | ||
/// | ||
/// For more information, see [`shmdt(2)`]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// `shmaddr` must meet the requirements described in the [`shmdt(2)`] man page. | ||
/// | ||
/// [`shmdt(2)`]: https://man7.org/linux/man-pages/man2/shmdt.2.html | ||
pub unsafe fn shmdt(shmaddr: *const c_void) -> Result<()> { | ||
Errno::result(libc::shmdt(shmaddr)).map(drop) | ||
} | ||
|
||
/// Performs control operation specified by `cmd` on the System V shared | ||
/// memory segment given by `shmid`. | ||
/// | ||
/// For more information, see [`shmctl(2)`]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// All arguments should be valid and meet the requirements described in the [`shmctl(2)`] man page. | ||
/// | ||
/// [`shmctl(2)`]: https://man7.org/linux/man-pages/man2/shmctl.2.html | ||
pub unsafe fn shmctl( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again: We also don't want to expose shmid_ds directly to the user I would say. It too needs rustification (though I haven't looked into the details of that). |
||
shmid: c_int, | ||
cmd: c_int, | ||
buf: *mut shmid_ds, | ||
) -> Result<c_int> { | ||
Errno::result(libc::shmctl(shmid, cmd, buf)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also seems the meaning of the return value changes depending on the specific command, so it is not correct to treat it as errno unconditionally: Quoting man 2 shmctl on my system:
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general (not just for shmget): I would not expose key_t directly. It is just a type alias, and thus not safe. Looking at the docs, some sort of newtype or enum would be more appropriate.