1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![cfg(unix)]
#![no_std]
use libc::c_int;
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
fn errno() -> c_int {
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
use libc::___errno as errno_location;
#[cfg(any(target_os = "android", target_os = "netbsd", target_os = "openbsd"))]
use libc::__errno as errno_location;
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "dragonfly"))]
use libc::__errno_location as errno_location;
#[cfg(any(target_os = "freebsd", target_os = "ios", target_os = "macos"))]
use libc::__error as errno_location;
unsafe { *errno_location() as c_int }
}
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
fn shm_open_anonymous_posix() -> c_int {
use libc::c_char;
let mut filename = *b"/shm_open_anonymous-XXXX\0";
const OFFSET: usize = 20;
assert_eq!(&filename[OFFSET..], b"XXXX\0");
loop {
let path = filename.as_ptr() as *const c_char;
debug_assert!(filename.starts_with(b"/shm_open_anonymous-"));
assert!(filename.ends_with(b"\0"));
let fd = unsafe {
libc::shm_open(
path,
libc::O_RDWR | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW,
0o600,
)
};
if fd == -1 && errno() != libc::EEXIST {
return -1;
} else if fd != -1 {
unsafe {
if libc::shm_unlink(path) == -1 {
libc::close(fd);
return -1;
}
}
return fd;
}
let pseudorandom = unsafe {
let mut timespec = core::mem::MaybeUninit::uninit();
if libc::clock_gettime(libc::CLOCK_REALTIME, timespec.as_mut_ptr()) != 0 {
return -1;
}
timespec.assume_init().tv_nsec % 10000
};
let digits = [
b'0' + (pseudorandom / 1000 % 10) as u8,
b'0' + (pseudorandom / 100 % 10) as u8,
b'0' + (pseudorandom / 10 % 10) as u8,
b'0' + (pseudorandom % 10) as u8,
];
debug_assert!(digits.iter().all(|x| *x >= b'0' && *x <= b'9'));
filename[OFFSET..OFFSET + 4].copy_from_slice(&digits);
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn memfd_create() -> c_int {
static PATH: &'static str = "shm_open_anonymous\0";
let fd = unsafe {
libc::syscall(
libc::SYS_memfd_create,
PATH.as_ptr() as *const libc::c_char,
libc::MFD_CLOEXEC,
)
};
fd as c_int
}
#[cfg(target_os = "linux")]
fn memfd_create_fallback_posix() -> c_int {
let fd = memfd_create();
if fd == -1 {
if errno() == libc::ENOSYS {
shm_open_anonymous_posix()
} else {
-1
}
} else {
fd
}
}
#[cfg(target_os = "freebsd")]
fn shm_open_shm_anon() -> c_int {
unsafe { libc::shm_open(libc::SHM_ANON, libc::O_RDWR, 0) }
}
pub fn shm_open_anonymous() -> c_int {
#[cfg(target_os = "linux")]
return memfd_create_fallback_posix();
#[cfg(target_os = "android")]
return memfd_create();
#[cfg(target_os = "freebsd")]
return shm_open_shm_anon();
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd")))]
return shm_open_anonymous_posix();
}
#[cfg(test)]
mod test {
#[test]
fn shm_open_anonymous() {
let fd = super::shm_open_anonymous();
assert!(fd != -1);
assert!(unsafe { libc::close(fd) } != -1);
}
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
#[test]
fn shm_open_anonymous_posix() {
let fd = super::shm_open_anonymous_posix();
assert!(fd != -1);
assert!(unsafe { libc::close(fd) } != -1);
}
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
#[test]
fn shm_open_anonymous_posix_contention() {
let taken_fd = unsafe {
libc::shm_open(
b"/shm_open_anonymous-XXXX\0".as_ptr() as *const libc::c_char,
libc::O_RDWR | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW,
0o600,
)
};
assert!(taken_fd != -1);
let fd = super::shm_open_anonymous_posix();
unsafe {
libc::close(taken_fd);
}
assert!(fd != -1);
assert!(unsafe { libc::close(fd) } != -1);
}
}