PMEM2_SOURCE_PWRITE_MCSAFE(3)
π pmem2_source_pread_mcsafe(3) β pmem2_source_pwrite_mcsafe(3) π
okay bestie, welcome to the PMDK Programmer's Manual β¨ you're literally about to learn how to read from and write to a source without your whole program getting bodied by a bad block. two functions, one vibe: safe I/O. let's get into it π«Ά
πΈ NAME πΈ
pmem2_source_pread_mcsafe(), pmem2_source_pwrite_mcsafe() β read source contents or write to the source in a safe manner π
no cap, "in a safe manner" is doing a LOT of heavy lifting in that sentence and we're about to find out why.
β¨ SYNOPSIS β¨
#include <libpmem2.h>
struct pmem2_source;
int pmem2_source_pread_mcsafe(struct pmem2_source *src, void *buf, size_t size,
size_t offset);
int pmem2_source_pwrite_mcsafe(struct pmem2_source *src, void *buf, size_t size,
size_t offset);
π¦ DESCRIPTION π¦
okay so here's the tea β
pmem2_source_pread_mcsafe() reads size bytes from the source src, starting at offset offset, and dumps them into the buffer buf. iconic behavior. π
pmem2_source_pwrite_mcsafe() goes the other direction β it writes size bytes from the buffer buf into the source src, starting at the offset offset. queen shit. π
now the mcsafe part, which is the whole point, and honestly? it's giving guardian angel π§
Both of these functions are capable of detecting bad blocks and handling the SIGBUS signal that gets thrown when you try to access a bad block. so instead of your process just getting merked by a signal, when a bad block is encountered, pmem2_source_pread_mcsafe() and pmem2_source_pwrite_mcsafe() return a corresponding error. much more chill. much more demure. π
how does it do that? a signal handler for the SIGBUS signal is registered using sigaction(2) for the running time of those operations. so it's like a little bodyguard that clocks in when the call starts and clocks out when it's done. πͺ
β οΈ heads up bestie: this capability is limited to POSIX systems. if you're not on POSIX, this whole mcsafe moment is not happening for you. sorry. π
if you wanna go deeper on bad block detection and clearing, go look at pmem2_badblock_context_new(3), pmem2_badblock_next(3) and pmem2_badblock_clear(3). the whole badblock squad. π
π RETURN VALUE π
Both pmem2_source_pread_mcsafe() and pmem2_source_pwrite_mcsafe() return 0 on success, or a negative error code on failure. that's it. that's the whole contract. simple. clean. periodt. π
π₯ ERRORS π₯
pmem2_source_pread_mcsafe() and pmem2_source_pwrite_mcsafe() can fail with the following errors:
PMEM2_E_IO_FAILβ a physical I/O error occurred during the read/write operation. a possible bad block encountered. π yeah, this is the one you were scared of.PMEM2_E_LENGTH_OUT_OF_RANGEβ read/write operation sizesizefrom offsetoffsetgoes beyond the file length. you asked for bytes that simply do not exist, bestie. πPMEM2_E_SOURCE_TYPE_NOT_SUPPORTEDβ read/write operation doesn't support provided source. only sources created withpmem2_source_from_fd(3)is supported. so likeβ¦ if your source came from anywhere else? no. not today. π«
and also β those operations can return all errors from the underlying pread(2), pwrite(2), sigaction(2) functions. so if you're debugging and the error code is confusing, check those manpages too. the plot thickens. π΅οΈ
π SEE ALSO π
pread(2), pwrite(2), ReadFile(), WriteFile(), pmem2_badblock_clear(3), pmem2_badblock_context_new(3), pmem2_badblock_next(3), pmem2_source_from_fd(3), libpmem2(7) and https://pmem.io π«Ά
and that's the whole page, bestie!! you ate that. go forth and do safe I/O. π
this page is from PMDK (the PMDK Programmer's Manual, dated 2025-07-24). love that for us. mwah. ππ β¨
π¦ where this page comes from
- fedora 43: libpmem2-devel 2.1.0-4.fc43 Β· BSD-3-Clause
- π the page's license: BSD-3-Clause
- Copyright 2021-2023, Intel Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms