xref: /netbsd-src/lib/libc/sys/memfd_create.2 (revision 7eace3da0cd50687e03e36df30a9c0ede7f6bfe1)
1.\"	$NetBSD: memfd_create.2,v 1.1 2023/07/10 02:31:54 christos Exp $
2.\"
3.\" Copyright (c) 2023 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Theodore Preduta.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd July 5, 2023
31.Dt MEMFD_CREATE 2
32.Os
33.Sh NAME
34.Nm memfd_create
35.Nd create anonymous files
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In sys/mman.h
40.Ft int
41.Fn memfd_create "const char *name" "unsigned int flags"
42.Sh DESCRIPTION
43The
44.Fn memfd_create
45system call returns a file descriptor to a file named
46.Fa name
47backed only by RAM.
48Initially, the size of the file is zero.
49.Pp
50The length of
51.Fa name
52must not exceed
53.Dv NAME_MAX-6
54characters in length, to allow for the prefix
55.Dq memfd:
56to be added.
57But since the file descriptor does not live on disk,
58.Fa name
59does not have to be unique.
60.Fa name
61is only intended to be used for debugging purposes and commands like
62.Xr fstat 1 .
63.Pp
64Additionally, any of the following may be specified as the
65.Fa flags :
66.Bl -tag -width MFD_ALLOW_SEALING
67.It Dv MFD_CLOEXEC
68Set the
69.Xr close 2
70on
71.Xr exec 3
72flag.
73.It Dv MFD_ALLOW_SEALING
74Allow adding seals to the file descriptor using the
75.Xr fcntl 2
76.Dv F_ADD_SEALS
77command.
78.El
79.Pp
80Otherwise, the returned file descriptor behaves the same as a regular file,
81including the ability to be mapped by
82.Xr mmap 2 .
83.Sh RETURN VALUES
84If successful, the
85.Fn memfd_create
86system call returns a non-negative integer.
87On failure -1 is returned and
88.Fa errno
89is set to indicate the error.
90.Sh ERRORS
91.Fn memfd_create
92will fail if:
93.Bl -tag -width Er
94.It Bq Er EFAULT
95The argument
96.Fa name
97is
98.Dv NULL
99or points to invalid memory.
100.It Bq Er EINVAL
101The argument
102.Fa flags
103has any bits set other than
104.Dv MFD_CLOEXEC
105or
106.Dv MFD_ALLOW_SEALING .
107.It Bq Er ENAMETOOLONG
108The length of
109.Fa name
110appended with the prefix
111.Dq memfd:
112would exceed
113.Dv NAME_MAX .
114.It Bq Er ENFILE
115The system file table is full.
116.El
117.Sh SEE ALSO
118.Xr fcntl 2 ,
119.Xr mmap 2 ,
120.Xr shmget 2 ,
121.Xr shm_open 3
122.Sh HISTORY
123.Fn memfd_create
124is compatible with the Linux system call of the same name that first appeared in
125Linux 3.17.
126