MEMFD_CREATE(2) System Calls Manual MEMFD_CREATE(2)

memfd_createcreate anonymous files

Standard C Library (libc, -lc)

#include <sys/mman.h>

int
memfd_create(const char *name, unsigned int flags);

The () system call returns a file descriptor to a file named name backed only by RAM. Initially, the size of the file is zero.

The length of name must not exceed NAME_MAX-6 characters in length, to allow for the prefix “memfd:” to be added. But since the file descriptor does not live on disk, name does not have to be unique. name is only intended to be used for debugging purposes and commands like fstat(1).

Additionally, any of the following may be specified as the flags:

Set the close(2) on exec(3) flag.
Set the close(2) on fork(2) flag.
Allow adding seals to the file descriptor using the fcntl(2) F_ADD_SEALS command.

Otherwise, the returned file descriptor behaves the same as a regular file, including the ability to be mapped by mmap(2).

If successful, the memfd_create() system call returns a non-negative integer. On failure -1 is returned and errno is set to indicate the error.

memfd_create() will fail if:

[]
The argument name is NULL or points to invalid memory.
[]
The argument flags has any bits set other than MFD_CLOEXEC, MFD_CLOFORK, or MFD_ALLOW_SEALING.
[]
The length of name appended with the prefix “memfd:” would exceed NAME_MAX.
[]
The system file table is full.

fcntl(2), mmap(2), shmget(2), shm_open(3)

memfd_create() is compatible with the Linux system call of the same name that first appeared in Linux 3.17. It was added in NetBSD 11.0.

November 14, 2025 NetBSD 11.0