| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use errors; |
| 5 |
use fs; |
| 6 |
use io; |
| 7 |
use path; |
| 8 |
use rt; |
| 9 |
use strings; |
| 10 |
|
| 11 |
fn shm_check_fs() bool = { |
| 12 |
match (open(rt::SHMFS_DIR_PATH, |
| 13 |
fs::flag::DIRECTORY | fs::flag::RDONLY)) { |
| 14 |
case fs::error => |
| 15 |
return false; |
| 16 |
case let fd: io::file => |
| 17 |
defer io::close(fd)!; |
| 18 |
|
| 19 |
let sv = rt::statvfs{...}; |
| 20 |
let st = rt::st{...}; |
| 21 |
|
| 22 |
if (rt::fstatvfs1(fd, &sv, rt::MNT_NOWAIT) is rt::errno) |
| 23 |
return false; |
| 24 |
|
| 25 |
if (strings::fromutf8(sv.f_fstypename)! == rt::MOUNT_SHMFS) |
| 26 |
return false; |
| 27 |
|
| 28 |
if (rt::fstat(fd, &st) is rt::errno) |
| 29 |
return false; |
| 30 |
|
| 31 |
if ((st.mode & rt::SHMFS_DIR_MODE) != rt::SHMFS_DIR_MODE) |
| 32 |
return false; |
| 33 |
|
| 34 |
return true; |
| 35 |
}; |
| 36 |
}; |
| 37 |
|
| 38 |
fn shm_get_path(name: const str) (str | fs::error) = { |
| 39 |
if (!shm_check_fs()) |
| 40 |
return errors::errno(rt::ENOTSUP): fs::error; |
| 41 |
|
| 42 |
// The name may start with a slash character. |
| 43 |
if (strings::hasprefix(name, '/')) { |
| 44 |
name = strings::sub(name, 1); |
| 45 |
}; |
| 46 |
|
| 47 |
// We may disallow other slashes (implementation-defined behaviour). |
| 48 |
if (strings::contains(name, '/')) |
| 49 |
return errors::errno(rt::EINVAL): fs::error; |
| 50 |
|
| 51 |
const _path = strings::concat( |
| 52 |
rt::SHMFS_DIR_PATH, "/", rt::SHMFS_OBJ_PREFIX, name)!; |
| 53 |
|
| 54 |
if (len(_path) > path::MAX) |
| 55 |
return errors::errno(rt::ENAMETOOLONG): fs::error; |
| 56 |
|
| 57 |
return _path; |
| 58 |
}; |
| 59 |
|
| 60 |
// Opens (or creates, given [[fs::flag::CREATE]]) a global shared memory file |
| 61 |
// with the given name, suitable for use with [[io::mmap]] to establish shared |
| 62 |
// memory areas with other processes using the same name. |
| 63 |
// |
| 64 |
// The name must not contain any forward slashes (one is permissible at the |
| 65 |
// start, e.g. "/example") and cannot be "." or "..". |
| 66 |
// |
| 67 |
// The "oflag" parameter, if provided, must include either [[fs::flag::RDONLY]] |
| 68 |
// or [[fs::flag::RDWR]], and may optionally add [[fs::flag::CREATE]], |
| 69 |
// [[fs::flag::EXCL]], and/or [[fs::flag::TRUNC]], which are supported on all |
| 70 |
// POSIX-compatible platforms. Other platforms may support additional |
| 71 |
// non-standard flags; consult the shm_open(3) manual for your target system for |
| 72 |
// details. |
| 73 |
// |
| 74 |
// The new file descriptor always has CLOEXEC set regardless of the provided |
| 75 |
// flags. If creating a new shared memory object, set its initial size with |
| 76 |
// [[io::trunc]] before mapping it with [[io::mmap]]. |
| 77 |
// |
| 78 |
// Call [[shm_unlink]] to remove the global shared memory object. |
| 79 |
export fn shm_open( |
| 80 |
name: str, |
| 81 |
oflag: fs::flag = fs::flag::CREATE | fs::flag::RDWR, |
| 82 |
mode: fs::mode = 0o600, |
| 83 |
) (io::file | fs::error) = { |
| 84 |
const _path = shm_get_path(name)?; |
| 85 |
|
| 86 |
const oflag = fsflags_to_bsd(oflag)? | rt::O_CLOEXEC | rt::O_NOFOLLOW; |
| 87 |
|
| 88 |
match (rt::open(_path, oflag, mode)) { |
| 89 |
case let fd: int => |
| 90 |
return fd: io::file; |
| 91 |
case let err: rt::errno => |
| 92 |
return errors::errno(err): fs::error; |
| 93 |
}; |
| 94 |
}; |
| 95 |
|
| 96 |
// Removes the shared memory object with the given name. Processes which already |
| 97 |
// hold a reference to the file may continue to use the memory associated with |
| 98 |
// it. Once all processes have unmapped the associated shared memory object, or |
| 99 |
// exited, the memory is released. |
| 100 |
export fn shm_unlink(name: str) (void | fs::error) = { |
| 101 |
const _path = shm_get_path(name)?; |
| 102 |
|
| 103 |
match (rt::unlink(_path)) { |
| 104 |
case void => |
| 105 |
void; |
| 106 |
case let err: rt::errno => |
| 107 |
return errors::errno(err): fs::error; |
| 108 |
}; |
| 109 |
}; |
| 110 |
|
| 111 |
@test fn shm_open() void = { |
| 112 |
const name = "/vizzini"; |
| 113 |
const value = "inconceivable"; |
| 114 |
def length = 13; |
| 115 |
|
| 116 |
const fd = shm_open(name)!; |
| 117 |
defer shm_unlink(name)!; |
| 118 |
io::trunc(fd, length)!; |
| 119 |
io::write(fd, strings::toutf8(value))!; |
| 120 |
|
| 121 |
{ |
| 122 |
const fd = shm_open(name, fs::flag::RDONLY, 0o600)!; |
| 123 |
|
| 124 |
let b: [length]u8 = [0...]; |
| 125 |
io::read(fd, b)!; |
| 126 |
assert(strings::fromutf8(b)! == value); |
| 127 |
}; |
| 128 |
}; |
| 129 |
|
| 130 |
@test fn shm_get_path() void = { |
| 131 |
assert(shm_get_path("/ab/c") is fs::error); |
| 132 |
assert(shm_get_path("abc"): str == "/var/shm/.shmobj_abc"); |
| 133 |
assert(shm_get_path("/abc"): str == "/var/shm/.shmobj_abc"); |
| 134 |
}; |