2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-05-09 14:09:30 -03:00
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2018-11-11 07:38:33 -02:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2021-08-14 10:36:26 -03:00
|
|
|
#include <limits.h>
|
2018-11-11 07:38:33 -02:00
|
|
|
#include <stdarg.h>
|
2019-11-16 13:08:11 -03:00
|
|
|
#include <string.h>
|
2021-02-05 08:16:30 -03:00
|
|
|
#include <syscall.h>
|
2018-11-11 07:38:33 -02:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
int fcntl(int fd, int cmd, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, cmd);
|
2019-07-03 16:17:35 -03:00
|
|
|
u32 extra_arg = va_arg(ap, u32);
|
2018-12-21 00:02:06 -02:00
|
|
|
int rc = syscall(SC_fcntl, fd, cmd, extra_arg);
|
2020-08-16 21:40:07 -03:00
|
|
|
va_end(ap);
|
2018-11-11 07:38:33 -02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
2019-07-22 15:01:11 -03:00
|
|
|
|
2021-05-09 14:09:30 -03:00
|
|
|
int create_inode_watcher(unsigned flags)
|
2019-07-22 15:01:11 -03:00
|
|
|
{
|
2021-05-09 14:09:30 -03:00
|
|
|
int rc = syscall(SC_create_inode_watcher, flags);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int inode_watcher_add_watch(int fd, const char* path, size_t path_length, unsigned event_mask)
|
|
|
|
|
{
|
2021-09-16 03:37:39 -03:00
|
|
|
Syscall::SC_inode_watcher_add_watch_params params { { path, path_length }, fd, event_mask };
|
2021-05-09 14:09:30 -03:00
|
|
|
int rc = syscall(SC_inode_watcher_add_watch, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int inode_watcher_remove_watch(int fd, int wd)
|
|
|
|
|
{
|
|
|
|
|
int rc = syscall(SC_inode_watcher_remove_watch, fd, wd);
|
2019-07-22 15:01:11 -03:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 13:08:11 -03:00
|
|
|
int creat(const char* path, mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 15:21:59 -03:00
|
|
|
int open(const char* path, int options, ...)
|
2019-11-16 13:08:11 -03:00
|
|
|
{
|
2020-01-11 08:47:47 -03:00
|
|
|
if (!path) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-01-12 15:21:59 -03:00
|
|
|
auto path_length = strlen(path);
|
2019-11-16 13:08:11 -03:00
|
|
|
if (path_length > INT32_MAX) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, options);
|
|
|
|
|
auto mode = (mode_t)va_arg(ap, unsigned);
|
|
|
|
|
va_end(ap);
|
2021-01-12 15:21:59 -03:00
|
|
|
Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode };
|
|
|
|
|
int rc = syscall(SC_open, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-11-16 13:08:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int openat(int dirfd, const char* path, int options, ...)
|
|
|
|
|
{
|
|
|
|
|
if (!path) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-01-12 15:21:59 -03:00
|
|
|
auto path_length = strlen(path);
|
|
|
|
|
if (path_length > INT32_MAX) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-11-16 13:08:11 -03:00
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, options);
|
|
|
|
|
auto mode = (mode_t)va_arg(ap, unsigned);
|
|
|
|
|
va_end(ap);
|
2021-01-12 15:21:59 -03:00
|
|
|
Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode };
|
|
|
|
|
int rc = syscall(SC_open, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-11-16 13:08:11 -03:00
|
|
|
}
|
2018-11-11 07:38:33 -02:00
|
|
|
}
|