---
name: "pmem2_source_pread_mcsafe"
section: "3"
platform: "linux"
package: "libpmem2-dev"
version: "1.13.1-1.1+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/pmem2_source_pread_mcsafe"
---

# pmem2\_source\_pread\_mcsafe(3)

read source contents or write to the source in a safe manner

## NAME

**pmem2\_source\_pread\_mcsafe**(), **pmem2\_source\_pwrite\_mcsafe**() - read source contents or write to the source in a safe manner

## 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

The **pmem2\_source\_pread\_mcsafe**() function reads *size* bytes from the source *src* starting at offset *offset* into the buffer *buf*. The **pmem2\_source\_pwrite\_mcsafe**() function writes *size* bytes from the buffer *buf* to the source *src* starting at the offset *offset*.

Above functions are capable of detecting bad blocks and handling the *SIGBUS* signal thrown when accessing a bad block. When a bad block is encountered, **pmem2\_source\_pread\_mcsafe**() and **pmem2\_source\_pwrite\_mcsafe**() functions return corresponding error. A signal handler for *SIGBUS* signal is registered using [**sigaction**(2)](/sigaction/2.md) for the running time of those operations. This capability is limited to POSIX systems.

For bad block detection and clearing, see [**pmem2\_badblock\_context\_new**(3)](/pmem2_badblock_context_new/3.md), [**pmem2\_badblock\_next**(3)](/pmem2_badblock_next/3.md) and [**pmem2\_badblock\_clear**(3)](/pmem2_badblock_clear/3.md).

## RETURN VALUE

The **pmem2\_source\_pread\_mcsafe**() and **pmem2\_source\_pwrite\_mcsafe**() functions return 0 on success or a negative error code on failure.

## ERRORS

The **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.

- •

  **PMEM2\_E\_LENGTH\_OUT\_OF\_RANGE** - read/write operation size *size* from offset *offset* goes beyond the file length.

- •

  **PMEM2\_E\_SOURCE\_TYPE\_NOT\_SUPPORTED** - read/write operation doesn’t support provided source, only sources created with [**pmem2\_source\_from\_fd**(3)](/pmem2_source_from_fd/3.md) and [**pmem2\_source\_from\_handle**(3)](/pmem2_source_from_handle/3.md) are supported.

Those operations can also return all errors from the underlying [**pread**(2)](/pread/2.md), [**pwrite**(2)](/pwrite/2.md), [**sigaction**(2)](/sigaction/2.md) functions on POSIX systems and **ReadFile**(), **WriteFile**() functions on Windows.

## SEE ALSO

[**pread**(2)](/pread/2.md), [**pwrite**(2)](/pwrite/2.md), **ReadFile**(), **WriteFile**(), [**pmem2\_badblock\_clear**(3)](/pmem2_badblock_clear/3.md), [**pmem2\_badblock\_context\_new**(3)](/pmem2_badblock_context_new/3.md), [**pmem2\_badblock\_next**(3)](/pmem2_badblock_next/3.md), [**pmem2\_source\_from\_fd**(3)](/pmem2_source_from_fd/3.md), [**pmem2\_source\_from\_handle**(3)](/pmem2_source_from_handle/3.md), [**libpmem2**(7)](/libpmem2/7.md) and **\<https://pmem.io\>**
