Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <bits/pthread_cancel.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <limits.h>
12#include <stdarg.h>
13#include <string.h>
14#include <syscall.h>
15#include <time.h>
16
17extern "C" {
18
19int fcntl(int fd, int cmd, ...)
20{
21 __pthread_maybe_cancel();
22
23 va_list ap;
24 va_start(ap, cmd);
25 uintptr_t extra_arg = va_arg(ap, uintptr_t);
26 int rc = syscall(SC_fcntl, fd, cmd, extra_arg);
27 va_end(ap);
28 __RETURN_WITH_ERRNO(rc, rc, -1);
29}
30
31int create_inode_watcher(unsigned flags)
32{
33 int rc = syscall(SC_create_inode_watcher, flags);
34 __RETURN_WITH_ERRNO(rc, rc, -1);
35}
36
37int inode_watcher_add_watch(int fd, char const* path, size_t path_length, unsigned event_mask)
38{
39 Syscall::SC_inode_watcher_add_watch_params params { { path, path_length }, fd, event_mask };
40 int rc = syscall(SC_inode_watcher_add_watch, ¶ms);
41 __RETURN_WITH_ERRNO(rc, rc, -1);
42}
43
44int inode_watcher_remove_watch(int fd, int wd)
45{
46 int rc = syscall(SC_inode_watcher_remove_watch, fd, wd);
47 __RETURN_WITH_ERRNO(rc, rc, -1);
48}
49
50int creat(char const* path, mode_t mode)
51{
52 __pthread_maybe_cancel();
53
54 return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
55}
56
57int open(char const* path, int options, ...)
58{
59 __pthread_maybe_cancel();
60
61 if (!path) {
62 errno = EFAULT;
63 return -1;
64 }
65 auto path_length = strlen(path);
66 if (path_length > INT32_MAX) {
67 errno = EINVAL;
68 return -1;
69 }
70 va_list ap;
71 va_start(ap, options);
72 auto mode = (mode_t)va_arg(ap, unsigned);
73 va_end(ap);
74 Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode };
75 int rc = syscall(SC_open, ¶ms);
76 __RETURN_WITH_ERRNO(rc, rc, -1);
77}
78
79int openat(int dirfd, char const* path, int options, ...)
80{
81 __pthread_maybe_cancel();
82
83 if (!path) {
84 errno = EFAULT;
85 return -1;
86 }
87 auto path_length = strlen(path);
88 if (path_length > INT32_MAX) {
89 errno = EINVAL;
90 return -1;
91 }
92 va_list ap;
93 va_start(ap, options);
94 auto mode = (mode_t)va_arg(ap, unsigned);
95 va_end(ap);
96 Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode };
97 int rc = syscall(SC_open, ¶ms);
98 __RETURN_WITH_ERRNO(rc, rc, -1);
99}
100
101// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fadvise.html
102int posix_fadvise(int fd, off_t offset, off_t len, int advice)
103{
104 // Per POSIX:
105 // "The posix_fadvise() function shall have no effect on the semantics of other operations on the specified data,
106 // although it may affect the performance of other operations."
107
108 // For now, we simply ignore posix_fadvise() requests. In the future we may use them to optimize performance.
109 (void)fd;
110 (void)offset;
111 (void)len;
112 (void)advice;
113 return 0;
114}
115
116// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
117int posix_fallocate(int fd, off_t offset, off_t len)
118{
119 // posix_fallocate does not set errno.
120 return -static_cast<int>(syscall(SC_posix_fallocate, fd, &offset, &len));
121}
122
123// https://pubs.opengroup.org/onlinepubs/9699919799/functions/utimensat.html
124int utimensat(int dirfd, char const* path, struct timespec const times[2], int flag)
125{
126 if (!path) {
127 errno = EFAULT;
128 return -1;
129 }
130
131 size_t path_length = strlen(path);
132 if (path_length > INT32_MAX) {
133 errno = EINVAL;
134 return -1;
135 }
136
137 // POSIX allows AT_SYMLINK_NOFOLLOW flag or no flags.
138 if (flag & ~AT_SYMLINK_NOFOLLOW) {
139 errno = EINVAL;
140 return -1;
141 }
142
143 // Return early without error since both changes are to be omitted.
144 if (times && times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT)
145 return 0;
146
147 // According to POSIX, when times is a nullptr, it's equivalent to setting
148 // both last access time and last modification time to the current time.
149 // Setting the times argument to nullptr if it matches this case prevents
150 // the need to copy it in the kernel.
151 if (times && times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW)
152 times = nullptr;
153
154 if (times) {
155 for (int i = 0; i < 2; ++i) {
156 if ((times[i].tv_nsec != UTIME_NOW && times[i].tv_nsec != UTIME_OMIT)
157 && (times[i].tv_nsec < 0 || times[i].tv_nsec >= 1'000'000'000L)) {
158 errno = EINVAL;
159 return -1;
160 }
161 }
162 }
163
164 Syscall::SC_utimensat_params params { dirfd, { path, path_length }, times, flag };
165 int rc = syscall(SC_utimensat, ¶ms);
166 __RETURN_WITH_ERRNO(rc, rc, -1);
167}
168}