2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-02-15 19:39:01 +00:00
|
|
|
#include <AK/ScopeGuard.h>
|
2019-07-25 13:21:50 +00:00
|
|
|
#include <AK/ScopedValueRollback.h>
|
2019-06-07 09:49:03 +00:00
|
|
|
#include <AK/Vector.h>
|
2022-11-04 17:20:11 +00:00
|
|
|
#include <Kernel/API/Unveil.h>
|
2022-08-20 15:01:53 +00:00
|
|
|
#include <LibCore/File.h>
|
2020-01-10 11:20:36 +00:00
|
|
|
#include <alloca.h>
|
2018-11-05 15:40:48 +00:00
|
|
|
#include <assert.h>
|
2022-06-12 13:15:09 +00:00
|
|
|
#include <bits/pthread_cancel.h>
|
2021-02-14 15:04:18 +00:00
|
|
|
#include <bits/pthread_integration.h>
|
2022-02-15 19:39:01 +00:00
|
|
|
#include <dirent.h>
|
2019-06-07 09:49:03 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2020-09-05 17:47:40 +00:00
|
|
|
#include <getopt.h>
|
2018-11-07 00:38:51 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
2019-06-07 09:49:03 +00:00
|
|
|
#include <stdarg.h>
|
2018-11-07 00:38:51 +00:00
|
|
|
#include <stdio.h>
|
2019-02-08 01:38:21 +00:00
|
|
|
#include <stdlib.h>
|
2019-06-07 09:49:03 +00:00
|
|
|
#include <string.h>
|
2018-11-16 12:11:21 +00:00
|
|
|
#include <sys/ioctl.h>
|
2020-04-12 18:23:18 +00:00
|
|
|
#include <sys/mman.h>
|
2022-03-29 20:47:06 +00:00
|
|
|
#include <sys/resource.h>
|
2021-08-11 16:49:13 +00:00
|
|
|
#include <sys/select.h>
|
2022-02-15 19:39:01 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sysmacros.h>
|
2019-01-21 23:58:13 +00:00
|
|
|
#include <sys/types.h>
|
2021-02-05 11:16:30 +00:00
|
|
|
#include <syscall.h>
|
2019-11-17 19:01:03 +00:00
|
|
|
#include <termios.h>
|
2020-08-30 08:43:15 +00:00
|
|
|
#include <time.h>
|
2019-06-07 09:49:03 +00:00
|
|
|
#include <unistd.h>
|
2018-10-22 11:57:25 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2020-10-10 14:48:27 +00:00
|
|
|
#ifdef NO_TLS
|
|
|
|
static int s_cached_tid = 0;
|
|
|
|
#else
|
2020-04-13 08:17:11 +00:00
|
|
|
static __thread int s_cached_tid = 0;
|
2020-10-10 14:48:27 +00:00
|
|
|
#endif
|
|
|
|
|
2020-07-27 16:05:48 +00:00
|
|
|
static int s_cached_pid = 0;
|
2020-04-13 08:17:11 +00:00
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/lchown.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int lchown(char const* pathname, uid_t uid, gid_t gid)
|
2021-12-31 18:20:17 +00:00
|
|
|
{
|
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid, AT_FDCWD, false };
|
|
|
|
int rc = syscall(SC_chown, ¶ms);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chown.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int chown(char const* pathname, uid_t uid, gid_t gid)
|
2019-02-23 16:24:50 +00:00
|
|
|
{
|
2020-01-11 09:17:08 +00:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-12-31 18:20:17 +00:00
|
|
|
Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid, AT_FDCWD, true };
|
2020-01-11 09:17:08 +00:00
|
|
|
int rc = syscall(SC_chown, ¶ms);
|
2019-02-27 11:32:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-02-23 16:24:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchown.html
|
2019-06-01 18:31:36 +00:00
|
|
|
int fchown(int fd, uid_t uid, gid_t gid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_fchown, fd, uid, gid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int fchownat(int fd, char const* pathname, uid_t uid, gid_t gid, int flags)
|
2021-12-31 18:20:17 +00:00
|
|
|
{
|
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid, fd, !(flags & AT_SYMLINK_NOFOLLOW) };
|
|
|
|
int rc = syscall(SC_chown, ¶ms);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html
|
2018-11-02 19:41:58 +00:00
|
|
|
pid_t fork()
|
|
|
|
{
|
2021-02-14 15:04:18 +00:00
|
|
|
__pthread_fork_prepare();
|
|
|
|
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_fork);
|
2020-07-21 16:45:06 +00:00
|
|
|
if (rc == 0) {
|
2020-04-13 08:17:11 +00:00
|
|
|
s_cached_tid = 0;
|
2020-07-21 16:45:06 +00:00
|
|
|
s_cached_pid = 0;
|
2021-02-14 15:04:18 +00:00
|
|
|
__pthread_fork_child();
|
|
|
|
} else if (rc != -1) {
|
|
|
|
__pthread_fork_parent();
|
2020-07-21 16:45:06 +00:00
|
|
|
}
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfork.html
|
2021-03-27 23:01:51 +00:00
|
|
|
pid_t vfork()
|
|
|
|
{
|
|
|
|
return fork();
|
|
|
|
}
|
|
|
|
|
2022-01-11 08:50:09 +00:00
|
|
|
// Non-POSIX, but present in BSDs and Linux
|
|
|
|
// https://man.openbsd.org/daemon.3
|
|
|
|
int daemon(int nochdir, int noclose)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// exit parent, continue execution in child
|
|
|
|
if (pid > 0)
|
|
|
|
_exit(0);
|
|
|
|
|
|
|
|
pid = setsid();
|
|
|
|
if (pid < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (nochdir == 0)
|
|
|
|
(void)chdir("/");
|
|
|
|
|
|
|
|
if (noclose == 0) {
|
|
|
|
// redirect stdout and stderr to /dev/null
|
|
|
|
int fd = open("/dev/null", O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
(void)close(STDOUT_FILENO);
|
|
|
|
(void)close(STDERR_FILENO);
|
|
|
|
(void)dup2(fd, STDOUT_FILENO);
|
|
|
|
(void)dup2(fd, STDERR_FILENO);
|
|
|
|
(void)close(fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execv.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int execv(char const* path, char* const argv[])
|
2019-03-14 14:18:15 +00:00
|
|
|
{
|
2019-03-27 00:39:13 +00:00
|
|
|
return execve(path, argv, environ);
|
2019-03-14 14:18:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execve.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int execve(char const* filename, char* const argv[], char* const envp[])
|
2018-11-03 00:49:40 +00:00
|
|
|
{
|
2020-01-10 11:20:36 +00:00
|
|
|
size_t arg_count = 0;
|
|
|
|
for (size_t i = 0; argv[i]; ++i)
|
|
|
|
++arg_count;
|
|
|
|
|
|
|
|
size_t env_count = 0;
|
|
|
|
for (size_t i = 0; envp[i]; ++i)
|
|
|
|
++env_count;
|
|
|
|
|
|
|
|
auto copy_strings = [&](auto& vec, size_t count, auto& output) {
|
|
|
|
output.length = count;
|
|
|
|
for (size_t i = 0; vec[i]; ++i) {
|
2020-09-12 03:11:07 +00:00
|
|
|
output.strings[i].characters = vec[i];
|
|
|
|
output.strings[i].length = strlen(vec[i]);
|
2020-01-10 11:20:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Syscall::SC_execve_params params;
|
2020-01-10 19:15:58 +00:00
|
|
|
params.arguments.strings = (Syscall::StringArgument*)alloca(arg_count * sizeof(Syscall::StringArgument));
|
|
|
|
params.environment.strings = (Syscall::StringArgument*)alloca(env_count * sizeof(Syscall::StringArgument));
|
2020-01-10 11:20:36 +00:00
|
|
|
|
|
|
|
params.path = { filename, strlen(filename) };
|
|
|
|
copy_strings(argv, arg_count, params.arguments);
|
|
|
|
copy_strings(envp, env_count, params.environment);
|
|
|
|
|
|
|
|
int rc = syscall(SC_execve, ¶ms);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 19:41:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 16:31:03 +00:00
|
|
|
// https://linux.die.net/man/3/execvpe (GNU extension)
|
2022-04-01 17:58:27 +00:00
|
|
|
int execvpe(char const* filename, char* const argv[], char* const envp[])
|
2019-01-23 05:35:34 +00:00
|
|
|
{
|
2020-02-01 15:05:04 +00:00
|
|
|
if (strchr(filename, '/'))
|
|
|
|
return execve(filename, argv, envp);
|
|
|
|
|
2019-07-25 13:21:50 +00:00
|
|
|
ScopedValueRollback errno_rollback(errno);
|
2022-08-20 16:31:03 +00:00
|
|
|
|
|
|
|
// TODO: Make this use the PATH search implementation from Core::File.
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString path = getenv("PATH");
|
2019-07-25 13:21:50 +00:00
|
|
|
if (path.is_empty())
|
2022-08-20 15:01:53 +00:00
|
|
|
path = DEFAULT_PATH;
|
2019-07-25 13:21:50 +00:00
|
|
|
auto parts = path.split(':');
|
|
|
|
for (auto& part : parts) {
|
2022-12-04 18:02:33 +00:00
|
|
|
auto candidate = DeprecatedString::formatted("{}/{}", part, filename);
|
2019-07-25 13:21:50 +00:00
|
|
|
int rc = execve(candidate.characters(), argv, envp);
|
2019-03-27 00:39:13 +00:00
|
|
|
if (rc < 0 && errno != ENOENT) {
|
2019-07-25 13:21:50 +00:00
|
|
|
errno_rollback.set_override_rollback_value(errno);
|
2021-01-11 12:04:22 +00:00
|
|
|
dbgln("execvpe() failed on attempt ({}) with {}", candidate, strerror(errno));
|
2019-03-27 00:39:13 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 13:21:50 +00:00
|
|
|
errno_rollback.set_override_rollback_value(ENOENT);
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("execvpe() leaving :(");
|
2019-03-27 00:39:13 +00:00
|
|
|
return -1;
|
2019-01-23 05:35:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int execvp(char const* filename, char* const argv[])
|
2019-04-26 01:16:26 +00:00
|
|
|
{
|
2019-07-25 05:01:00 +00:00
|
|
|
int rc = execvpe(filename, argv, environ);
|
2020-05-15 09:08:39 +00:00
|
|
|
int saved_errno = errno;
|
2021-04-19 15:28:17 +00:00
|
|
|
dbgln("execvp({}, ...) about to return {} with errno={}", filename, rc, saved_errno);
|
2020-05-15 09:08:39 +00:00
|
|
|
errno = saved_errno;
|
2019-07-25 05:01:00 +00:00
|
|
|
return rc;
|
2019-04-26 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execl.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int execl(char const* filename, char const* arg0, ...)
|
2019-02-03 03:32:31 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
Vector<char const*, 16> args;
|
2019-02-03 03:32:31 +00:00
|
|
|
args.append(arg0);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
for (;;) {
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* arg = va_arg(ap, char const*);
|
2019-02-03 03:32:31 +00:00
|
|
|
if (!arg)
|
|
|
|
break;
|
|
|
|
args.append(arg);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
2019-02-07 23:12:12 +00:00
|
|
|
args.append(nullptr);
|
2019-04-23 11:00:53 +00:00
|
|
|
return execve(filename, const_cast<char* const*>(args.data()), environ);
|
2019-02-03 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execle.html
|
2021-06-03 23:47:17 +00:00
|
|
|
int execle(char const* filename, char const* arg0, ...)
|
|
|
|
{
|
|
|
|
Vector<char const*> args;
|
|
|
|
args.append(arg0);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
char const* arg;
|
|
|
|
do {
|
|
|
|
arg = va_arg(ap, char const*);
|
|
|
|
args.append(arg);
|
|
|
|
} while (arg);
|
|
|
|
|
|
|
|
auto argv = const_cast<char* const*>(args.data());
|
|
|
|
auto envp = const_cast<char* const*>(va_arg(ap, char* const*));
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return execve(filename, argv, envp);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execlp.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int execlp(char const* filename, char const* arg0, ...)
|
2019-11-16 08:35:48 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
Vector<char const*, 16> args;
|
2019-11-16 08:35:48 +00:00
|
|
|
args.append(arg0);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
for (;;) {
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* arg = va_arg(ap, char const*);
|
2019-11-16 08:35:48 +00:00
|
|
|
if (!arg)
|
|
|
|
break;
|
|
|
|
args.append(arg);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
args.append(nullptr);
|
|
|
|
return execvpe(filename, const_cast<char* const*>(args.data()), environ);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
|
2020-06-17 12:58:00 +00:00
|
|
|
uid_t geteuid()
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2020-06-17 12:58:00 +00:00
|
|
|
return syscall(SC_geteuid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getegid.html
|
2020-06-17 12:58:00 +00:00
|
|
|
gid_t getegid()
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2020-06-17 12:58:00 +00:00
|
|
|
return syscall(SC_getegid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
|
2020-06-17 12:58:00 +00:00
|
|
|
uid_t getuid()
|
2018-11-05 14:04:19 +00:00
|
|
|
{
|
2020-06-17 12:58:00 +00:00
|
|
|
return syscall(SC_getuid);
|
2018-11-05 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgid.html
|
2020-06-17 12:58:00 +00:00
|
|
|
gid_t getgid()
|
2018-11-05 14:04:19 +00:00
|
|
|
{
|
2020-06-17 12:58:00 +00:00
|
|
|
return syscall(SC_getgid);
|
2018-11-05 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpid.html
|
2018-10-25 08:00:37 +00:00
|
|
|
pid_t getpid()
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2020-07-23 07:27:33 +00:00
|
|
|
int cached_pid = s_cached_pid;
|
|
|
|
if (!cached_pid) {
|
|
|
|
cached_pid = syscall(SC_getpid);
|
|
|
|
s_cached_pid = cached_pid;
|
|
|
|
}
|
|
|
|
return cached_pid;
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getppid.html
|
2018-11-06 12:33:06 +00:00
|
|
|
pid_t getppid()
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
return syscall(SC_getppid);
|
2018-11-06 12:33:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 12:58:00 +00:00
|
|
|
int getresuid(uid_t* ruid, uid_t* euid, uid_t* suid)
|
|
|
|
{
|
|
|
|
return syscall(SC_getresuid, ruid, euid, suid);
|
|
|
|
}
|
|
|
|
|
2020-06-30 23:34:20 +00:00
|
|
|
int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid)
|
2020-06-17 12:58:00 +00:00
|
|
|
{
|
|
|
|
return syscall(SC_getresgid, rgid, egid, sgid);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html
|
2020-02-03 15:52:23 +00:00
|
|
|
pid_t getsid(pid_t pid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_getsid, pid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setsid.html
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t setsid()
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_setsid);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 12:14:25 +00:00
|
|
|
pid_t tcgetpgrp(int fd)
|
2018-11-02 11:56:51 +00:00
|
|
|
{
|
2021-07-26 10:42:16 +00:00
|
|
|
pid_t pgrp;
|
|
|
|
int rc = ioctl(fd, TIOCGPGRP, &pgrp);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
return pgrp;
|
2018-11-02 12:14:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetpgrp.html
|
2018-11-02 12:14:25 +00:00
|
|
|
int tcsetpgrp(int fd, pid_t pgid)
|
|
|
|
{
|
2018-11-16 12:11:21 +00:00
|
|
|
return ioctl(fd, TIOCSPGRP, pgid);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setpgid.html
|
2018-11-02 11:56:51 +00:00
|
|
|
int setpgid(pid_t pid, pid_t pgid)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_setpgid, pid, pgid);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgid.html
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t getpgid(pid_t pid)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_getpgid, pid);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgrp.html
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t getpgrp()
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_getpgrp);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
|
2018-10-23 08:12:50 +00:00
|
|
|
ssize_t read(int fd, void* buf, size_t count)
|
|
|
|
{
|
2022-06-12 13:15:09 +00:00
|
|
|
__pthread_maybe_cancel();
|
|
|
|
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_read, fd, buf, count);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html
|
2021-04-21 09:11:04 +00:00
|
|
|
ssize_t pread(int fd, void* buf, size_t count, off_t offset)
|
|
|
|
{
|
2022-06-12 13:15:09 +00:00
|
|
|
__pthread_maybe_cancel();
|
|
|
|
|
2021-11-13 08:28:56 +00:00
|
|
|
int rc = syscall(SC_pread, fd, buf, count, &offset);
|
2021-10-12 13:15:18 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2021-04-21 09:11:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
|
2022-04-01 17:58:27 +00:00
|
|
|
ssize_t write(int fd, void const* buf, size_t count)
|
2018-10-30 14:33:37 +00:00
|
|
|
{
|
2022-06-12 13:15:09 +00:00
|
|
|
__pthread_maybe_cancel();
|
|
|
|
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_write, fd, buf, count);
|
2018-10-30 14:33:37 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html
|
2022-04-01 17:58:27 +00:00
|
|
|
ssize_t pwrite(int fd, void const* buf, size_t count, off_t offset)
|
2021-04-21 09:11:04 +00:00
|
|
|
{
|
2022-06-12 13:15:09 +00:00
|
|
|
__pthread_maybe_cancel();
|
|
|
|
|
2021-04-21 09:11:04 +00:00
|
|
|
// FIXME: This is not thread safe and should be implemented in the kernel instead.
|
|
|
|
off_t old_offset = lseek(fd, 0, SEEK_CUR);
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
ssize_t nwritten = write(fd, buf, count);
|
|
|
|
lseek(fd, old_offset, SEEK_SET);
|
|
|
|
return nwritten;
|
|
|
|
}
|
|
|
|
|
2022-02-15 19:39:01 +00:00
|
|
|
// Note: Be sure to send to directory_name parameter a directory name ended with trailing slash.
|
2022-04-01 17:58:27 +00:00
|
|
|
static int ttyname_r_for_directory(char const* directory_name, dev_t device_mode, ino_t inode_number, char* buffer, size_t size)
|
2022-02-15 19:39:01 +00:00
|
|
|
{
|
|
|
|
DIR* dirstream = opendir(directory_name);
|
|
|
|
if (!dirstream) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto close_dir_stream_on_exit = ScopeGuard([dirstream] {
|
|
|
|
closedir(dirstream);
|
|
|
|
});
|
|
|
|
|
|
|
|
struct dirent* entry = nullptr;
|
|
|
|
char* name_path = nullptr;
|
|
|
|
|
|
|
|
// FIXME: Use LibCore DirIterator here instead
|
|
|
|
while ((entry = readdir(dirstream)) != nullptr) {
|
|
|
|
if (((ino_t)entry->d_ino == inode_number)
|
|
|
|
&& strcmp(entry->d_name, "stdin")
|
|
|
|
&& strcmp(entry->d_name, "stdout")
|
|
|
|
&& strcmp(entry->d_name, "stderr")) {
|
|
|
|
|
|
|
|
size_t name_length = strlen(directory_name) + strlen(entry->d_name) + 1;
|
|
|
|
|
|
|
|
if (name_length > size) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
name_path = (char*)malloc(name_length);
|
|
|
|
memset(name_path, 0, name_length);
|
|
|
|
memcpy(name_path, directory_name, strlen(directory_name));
|
|
|
|
memcpy(&name_path[strlen(directory_name)], entry->d_name, strlen(entry->d_name));
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(name_path, &st) < 0) {
|
|
|
|
free(name_path);
|
2022-03-27 22:48:16 +00:00
|
|
|
name_path = nullptr;
|
2022-02-15 19:39:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_mode == st.st_rdev) {
|
|
|
|
memset(buffer, 0, name_length);
|
|
|
|
memcpy(buffer, name_path, name_length);
|
|
|
|
free(name_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(name_path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname_r.html
|
2018-10-30 21:03:02 +00:00
|
|
|
int ttyname_r(int fd, char* buffer, size_t size)
|
|
|
|
{
|
2022-02-15 19:39:01 +00:00
|
|
|
struct stat stat;
|
|
|
|
if (fstat(fd, &stat) < 0)
|
|
|
|
return -1;
|
|
|
|
dev_t major_minor_numbers = stat.st_rdev;
|
|
|
|
ino_t inode_number = stat.st_ino;
|
|
|
|
if (ttyname_r_for_directory("/dev/", major_minor_numbers, inode_number, buffer, size) < 0) {
|
|
|
|
if (ttyname_r_for_directory("/dev/pts/", major_minor_numbers, inode_number, buffer, size) < 0) {
|
|
|
|
errno = ENOTTY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2018-10-30 21:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char ttyname_buf[32];
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname.html
|
2018-10-30 21:03:02 +00:00
|
|
|
char* ttyname(int fd)
|
|
|
|
{
|
|
|
|
if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
|
|
|
|
return nullptr;
|
|
|
|
return ttyname_buf;
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
|
2018-10-23 08:12:50 +00:00
|
|
|
int close(int fd)
|
|
|
|
{
|
2022-06-12 13:15:09 +00:00
|
|
|
__pthread_maybe_cancel();
|
|
|
|
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_close, fd);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chdir.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int chdir(char const* path)
|
2018-10-26 12:24:11 +00:00
|
|
|
{
|
2020-01-06 09:51:50 +00:00
|
|
|
if (!path) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-05 21:06:25 +00:00
|
|
|
int rc = syscall(SC_chdir, path, strlen(path));
|
2018-10-26 12:24:11 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchdir.html
|
2019-09-11 23:18:25 +00:00
|
|
|
int fchdir(int fd)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_fchdir, fd);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
|
2018-10-24 12:28:22 +00:00
|
|
|
char* getcwd(char* buffer, size_t size)
|
|
|
|
{
|
2021-01-16 14:48:56 +00:00
|
|
|
if (buffer && size == 0) {
|
|
|
|
// POSIX requires that we set errno to EINVAL here, but in our syscall it makes sense to
|
|
|
|
// allow "probing" the Kernel with a zero-sized buffer, and it does not return -EINVAL.
|
|
|
|
// So we have to inject EINVAL here.
|
|
|
|
errno = EINVAL;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-16 12:32:11 +00:00
|
|
|
bool self_allocated = false;
|
2019-02-08 01:38:21 +00:00
|
|
|
if (!buffer) {
|
2021-01-16 14:48:56 +00:00
|
|
|
size = size ? size : 64;
|
2019-02-08 01:38:21 +00:00
|
|
|
buffer = (char*)malloc(size);
|
2021-01-16 12:32:11 +00:00
|
|
|
self_allocated = true;
|
2019-02-08 01:38:21 +00:00
|
|
|
}
|
2021-01-16 14:48:56 +00:00
|
|
|
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_getcwd, buffer, size);
|
2021-01-16 14:48:56 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
if (self_allocated)
|
|
|
|
free(buffer);
|
|
|
|
errno = -rc;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t actual_size = static_cast<size_t>(rc);
|
|
|
|
if (actual_size <= size) {
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, the current directory path was silently truncated.
|
|
|
|
|
|
|
|
if (!self_allocated) {
|
|
|
|
// In this case, POSIX causes information loss: the caller cannot learn about the ideal
|
|
|
|
// buffer size. This is the reason we went with silently truncation instead.
|
|
|
|
errno = ERANGE;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try again.
|
|
|
|
free(buffer);
|
|
|
|
size = actual_size;
|
|
|
|
buffer = (char*)malloc(size);
|
|
|
|
rc = syscall(SC_getcwd, buffer, size);
|
|
|
|
if (rc < 0) {
|
|
|
|
// Can only happen if we lose a race. Let's pretend we lost the race in the first place.
|
2021-01-16 12:32:11 +00:00
|
|
|
free(buffer);
|
2021-01-16 14:48:56 +00:00
|
|
|
errno = -rc;
|
|
|
|
return nullptr;
|
2021-01-16 12:32:11 +00:00
|
|
|
}
|
2021-01-16 14:48:56 +00:00
|
|
|
|
|
|
|
actual_size = static_cast<size_t>(rc);
|
|
|
|
if (actual_size < size) {
|
|
|
|
// If we're here, then cwd has become longer while we were looking at it. (Race with another thread?)
|
|
|
|
// There's not much we can do, unless we want to loop endlessly
|
|
|
|
// in this case. Let's leave it up to the caller whether to loop.
|
|
|
|
free(buffer);
|
|
|
|
errno = EAGAIN;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
2018-10-24 12:28:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getwd.html
|
2018-11-06 14:59:57 +00:00
|
|
|
char* getwd(char* buf)
|
|
|
|
{
|
2021-01-16 14:48:56 +00:00
|
|
|
if (buf == nullptr) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-11 14:36:40 +00:00
|
|
|
auto* p = getcwd(buf, PATH_MAX);
|
2021-01-16 14:48:56 +00:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
// POSIX quirk
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
}
|
2018-11-11 14:36:40 +00:00
|
|
|
return p;
|
2018-11-06 14:59:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html
|
2021-04-11 17:00:36 +00:00
|
|
|
unsigned int sleep(unsigned int seconds)
|
2018-10-25 11:53:49 +00:00
|
|
|
{
|
2020-08-30 11:21:24 +00:00
|
|
|
struct timespec ts = { seconds, 0 };
|
2020-12-04 05:12:50 +00:00
|
|
|
if (clock_nanosleep(CLOCK_MONOTONIC_COARSE, 0, &ts, nullptr) < 0)
|
2020-08-30 11:21:24 +00:00
|
|
|
return ts.tv_sec;
|
|
|
|
return 0;
|
2018-10-25 11:53:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/usleep.html
|
2019-02-03 15:11:28 +00:00
|
|
|
int usleep(useconds_t usec)
|
|
|
|
{
|
2020-08-30 08:43:15 +00:00
|
|
|
struct timespec ts = { (long)(usec / 1000000), (long)(usec % 1000000) * 1000 };
|
2020-12-04 05:12:50 +00:00
|
|
|
return clock_nanosleep(CLOCK_MONOTONIC_COARSE, 0, &ts, nullptr);
|
2019-02-03 15:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:54:29 +00:00
|
|
|
int gethostname(char* buffer, size_t size)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_gethostname, buffer, size);
|
2018-10-26 07:54:29 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int sethostname(char const* hostname, ssize_t size)
|
2020-04-26 05:59:47 +00:00
|
|
|
{
|
|
|
|
int rc = syscall(SC_sethostname, hostname, size);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-10-01 11:20:08 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html
|
2022-04-01 17:58:27 +00:00
|
|
|
ssize_t readlink(char const* path, char* buffer, size_t size)
|
2018-10-28 13:11:51 +00:00
|
|
|
{
|
2022-10-01 11:20:08 +00:00
|
|
|
return readlinkat(AT_FDCWD, path, buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html
|
|
|
|
ssize_t readlinkat(int dirfd, char const* path, char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size }, dirfd };
|
2020-01-10 19:13:23 +00:00
|
|
|
int rc = syscall(SC_readlink, ¶ms);
|
2020-06-16 18:51:08 +00:00
|
|
|
// Return the number of bytes placed in the buffer, not the full path size.
|
|
|
|
__RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1);
|
2018-10-28 13:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html
|
2018-10-31 16:50:43 +00:00
|
|
|
off_t lseek(int fd, off_t offset, int whence)
|
|
|
|
{
|
2021-03-13 21:02:54 +00:00
|
|
|
int rc = syscall(SC_lseek, fd, &offset, whence);
|
|
|
|
__RETURN_WITH_ERRNO(rc, offset, -1);
|
2018-10-31 16:50:43 +00:00
|
|
|
}
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int link(char const* old_path, char const* new_path)
|
2018-11-05 15:40:48 +00:00
|
|
|
{
|
2020-01-10 20:26:47 +00:00
|
|
|
if (!old_path || !new_path) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Syscall::SC_link_params params { { old_path, strlen(old_path) }, { new_path, strlen(new_path) } };
|
|
|
|
int rc = syscall(SC_link, ¶ms);
|
2019-02-21 12:26:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 15:40:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int unlink(char const* pathname)
|
2018-11-05 15:40:48 +00:00
|
|
|
{
|
2022-02-10 11:30:33 +00:00
|
|
|
return unlinkat(AT_FDCWD, pathname, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html
|
|
|
|
int unlinkat(int dirfd, char const* pathname, int flags)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_unlink, dirfd, pathname, strlen(pathname), flags);
|
2019-01-22 06:03:44 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 15:40:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int symlink(char const* target, char const* linkpath)
|
2022-10-01 11:15:02 +00:00
|
|
|
{
|
|
|
|
return symlinkat(target, AT_FDCWD, linkpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlinkat.html
|
|
|
|
int symlinkat(char const* target, int newdirfd, char const* linkpath)
|
2019-03-02 00:50:34 +00:00
|
|
|
{
|
2020-01-11 09:31:33 +00:00
|
|
|
if (!target || !linkpath) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2022-10-01 11:15:02 +00:00
|
|
|
Syscall::SC_symlink_params params { { target, strlen(target) }, { linkpath, strlen(linkpath) }, newdirfd };
|
2020-01-11 09:31:33 +00:00
|
|
|
int rc = syscall(SC_symlink, ¶ms);
|
2019-03-02 00:50:34 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/rmdir.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int rmdir(char const* pathname)
|
2019-01-28 03:16:01 +00:00
|
|
|
{
|
2020-01-06 10:05:59 +00:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int rc = syscall(SC_rmdir, pathname, strlen(pathname));
|
2019-01-28 03:16:01 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/isatty.html
|
2018-11-05 17:16:00 +00:00
|
|
|
int isatty(int fd)
|
|
|
|
{
|
2020-05-19 17:34:45 +00:00
|
|
|
return fcntl(fd, F_ISTTY);
|
2018-11-05 17:16:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html
|
2018-11-05 18:01:22 +00:00
|
|
|
int dup(int old_fd)
|
|
|
|
{
|
2020-08-14 23:17:00 +00:00
|
|
|
return fcntl(old_fd, F_DUPFD, 0);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html
|
2018-11-05 18:01:22 +00:00
|
|
|
int dup2(int old_fd, int new_fd)
|
|
|
|
{
|
2020-08-15 08:54:00 +00:00
|
|
|
int rc = syscall(SC_dup2, old_fd, new_fd);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int setgroups(size_t size, gid_t const* list)
|
2018-11-07 00:38:51 +00:00
|
|
|
{
|
2020-01-04 12:00:50 +00:00
|
|
|
int rc = syscall(SC_setgroups, size, list);
|
2018-11-07 00:38:51 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getgroups(int size, gid_t list[])
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_getgroups, size, list);
|
2018-11-07 00:38:51 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html
|
2018-11-10 23:20:53 +00:00
|
|
|
int pipe(int pipefd[2])
|
|
|
|
{
|
2019-08-05 12:29:05 +00:00
|
|
|
return pipe2(pipefd, 0);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
//
|
2019-08-05 12:29:05 +00:00
|
|
|
int pipe2(int pipefd[2], int flags)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_pipe, pipefd, flags);
|
2018-11-10 23:20:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int alarm(unsigned int seconds)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
return syscall(SC_alarm, seconds);
|
2018-11-10 23:20:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 12:58:00 +00:00
|
|
|
int seteuid(uid_t euid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_seteuid, euid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setegid(gid_t egid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_setegid, egid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:20:53 +00:00
|
|
|
int setuid(uid_t uid)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_setuid, uid);
|
2018-11-10 23:20:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2020-06-17 12:59:08 +00:00
|
|
|
int setgid(gid_t gid)
|
2018-11-10 23:20:53 +00:00
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_setgid, gid);
|
2018-11-10 23:20:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2021-04-09 11:13:15 +00:00
|
|
|
int setreuid(uid_t ruid, uid_t euid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_setreuid, ruid, euid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2020-06-17 12:58:00 +00:00
|
|
|
int setresuid(uid_t ruid, uid_t euid, uid_t suid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_setresuid, ruid, euid, suid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-10-01 12:47:38 +00:00
|
|
|
int setregid(gid_t rgid, gid_t egid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_setresgid, rgid, egid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2020-06-17 12:58:00 +00:00
|
|
|
int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_setresgid, rgid, egid, sgid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int access(char const* pathname, int mode)
|
2022-10-01 12:24:56 +00:00
|
|
|
{
|
|
|
|
return faccessat(AT_FDCWD, pathname, mode, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/faccessat.html
|
|
|
|
int faccessat(int dirfd, char const* pathname, int mode, int flags)
|
2018-11-10 23:20:53 +00:00
|
|
|
{
|
2020-01-06 09:43:53 +00:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2022-10-01 12:24:56 +00:00
|
|
|
|
|
|
|
Syscall::SC_faccessat_params params { dirfd, { pathname, strlen(pathname) }, mode, flags };
|
|
|
|
int rc = syscall(SC_faccessat, ¶ms);
|
2018-11-10 23:20:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-14 10:27:58 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int mknod(char const* pathname, mode_t mode, dev_t dev)
|
2018-11-16 23:11:08 +00:00
|
|
|
{
|
2020-01-11 09:27:37 +00:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Syscall::SC_mknod_params params { { pathname, strlen(pathname) }, mode, dev };
|
|
|
|
int rc = syscall(SC_mknod, ¶ms);
|
2019-05-03 20:59:58 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-16 23:11:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fpathconf.html
|
2021-07-23 06:34:22 +00:00
|
|
|
long fpathconf([[maybe_unused]] int fd, int name)
|
2018-11-17 14:56:09 +00:00
|
|
|
{
|
2019-09-02 05:36:22 +00:00
|
|
|
switch (name) {
|
2021-04-23 20:31:21 +00:00
|
|
|
case _PC_NAME_MAX:
|
|
|
|
return NAME_MAX;
|
2019-09-02 05:36:22 +00:00
|
|
|
case _PC_PATH_MAX:
|
|
|
|
return PATH_MAX;
|
2019-12-01 10:34:34 +00:00
|
|
|
case _PC_VDISABLE:
|
|
|
|
return _POSIX_VDISABLE;
|
2021-07-23 06:34:22 +00:00
|
|
|
case _PC_LINK_MAX:
|
|
|
|
return LINK_MAX;
|
2019-09-02 05:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2018-11-17 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pathconf.html
|
2022-04-01 17:58:27 +00:00
|
|
|
long pathconf([[maybe_unused]] char const* path, int name)
|
2018-11-17 14:56:09 +00:00
|
|
|
{
|
2019-09-02 05:36:22 +00:00
|
|
|
switch (name) {
|
2021-04-23 20:31:21 +00:00
|
|
|
case _PC_NAME_MAX:
|
|
|
|
return NAME_MAX;
|
2019-09-02 05:36:22 +00:00
|
|
|
case _PC_PATH_MAX:
|
|
|
|
return PATH_MAX;
|
2019-11-16 08:35:48 +00:00
|
|
|
case _PC_PIPE_BUF:
|
|
|
|
return PIPE_BUF;
|
2021-07-23 06:34:22 +00:00
|
|
|
case _PC_LINK_MAX:
|
|
|
|
return LINK_MAX;
|
2019-09-02 05:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2018-11-17 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/_exit.html
|
2018-11-17 14:56:09 +00:00
|
|
|
void _exit(int status)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
syscall(SC_exit, status);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2018-11-17 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sync.html
|
2018-12-19 23:39:29 +00:00
|
|
|
void sync()
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
syscall(SC_sync);
|
2018-12-19 23:39:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString getlogin_buffer;
|
2020-09-01 14:15:30 +00:00
|
|
|
|
2019-02-26 11:57:02 +00:00
|
|
|
char* getlogin()
|
|
|
|
{
|
2020-09-01 14:15:30 +00:00
|
|
|
if (getlogin_buffer.is_null()) {
|
2020-08-25 14:21:45 +00:00
|
|
|
if (auto* passwd = getpwuid(getuid())) {
|
2022-12-04 18:02:33 +00:00
|
|
|
getlogin_buffer = DeprecatedString(passwd->pw_name);
|
2020-08-25 14:21:45 +00:00
|
|
|
}
|
2020-01-08 15:01:51 +00:00
|
|
|
endpwent();
|
2019-03-10 02:15:36 +00:00
|
|
|
}
|
2020-09-01 14:15:30 +00:00
|
|
|
return const_cast<char*>(getlogin_buffer.characters());
|
2019-02-26 11:57:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
|
2019-03-23 23:53:39 +00:00
|
|
|
int ftruncate(int fd, off_t length)
|
|
|
|
{
|
2021-03-13 21:02:54 +00:00
|
|
|
int rc = syscall(SC_ftruncate, fd, &length);
|
2019-04-08 23:10:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-03-23 23:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int truncate(char const* path, off_t length)
|
2020-06-15 15:28:42 +00:00
|
|
|
{
|
|
|
|
int fd = open(path, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
int rc = ftruncate(fd, length);
|
|
|
|
int saved_errno = errno;
|
|
|
|
if (int close_rc = close(fd); close_rc < 0)
|
|
|
|
return close_rc;
|
|
|
|
errno = saved_errno;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-03-25 12:03:49 +00:00
|
|
|
int gettid()
|
|
|
|
{
|
2020-07-23 07:27:33 +00:00
|
|
|
int cached_tid = s_cached_tid;
|
|
|
|
if (!cached_tid) {
|
|
|
|
cached_tid = syscall(SC_gettid);
|
|
|
|
s_cached_tid = cached_tid;
|
|
|
|
}
|
|
|
|
return cached_tid;
|
2019-03-25 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 17:24:09 +00:00
|
|
|
int sysbeep()
|
2019-05-15 19:40:41 +00:00
|
|
|
{
|
2021-12-17 17:24:09 +00:00
|
|
|
int rc = syscall(SC_beep);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-05-15 19:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html
|
2021-09-12 03:28:59 +00:00
|
|
|
int fsync(int fd)
|
2019-05-19 17:54:20 +00:00
|
|
|
{
|
2022-06-12 13:15:09 +00:00
|
|
|
__pthread_maybe_cancel();
|
|
|
|
|
2021-09-12 03:28:59 +00:00
|
|
|
int rc = syscall(SC_fsync, fd);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-05-19 17:54:20 +00:00
|
|
|
}
|
2019-07-19 07:58:12 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int mount(int source_fd, char const* target, char const* fs_type, int flags)
|
2019-08-02 13:18:47 +00:00
|
|
|
{
|
2020-04-06 08:55:08 +00:00
|
|
|
if (!target || !fs_type) {
|
2020-01-11 09:46:21 +00:00
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-04-06 08:55:08 +00:00
|
|
|
|
2020-01-11 15:25:26 +00:00
|
|
|
Syscall::SC_mount_params params {
|
|
|
|
{ target, strlen(target) },
|
|
|
|
{ fs_type, strlen(fs_type) },
|
2021-09-16 06:44:07 +00:00
|
|
|
source_fd,
|
2020-01-11 15:25:26 +00:00
|
|
|
flags
|
|
|
|
};
|
2020-01-11 09:46:21 +00:00
|
|
|
int rc = syscall(SC_mount, ¶ms);
|
2019-08-02 13:18:47 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int umount(char const* mountpoint)
|
2019-08-11 13:56:39 +00:00
|
|
|
{
|
2020-01-09 11:41:15 +00:00
|
|
|
int rc = syscall(SC_umount, mountpoint, strlen(mountpoint));
|
2019-08-11 13:56:39 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-07-21 07:59:17 +00:00
|
|
|
void dump_backtrace()
|
|
|
|
{
|
|
|
|
syscall(SC_dump_backtrace);
|
|
|
|
}
|
2019-08-15 18:55:10 +00:00
|
|
|
|
|
|
|
int get_process_name(char* buffer, int buffer_size)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_get_process_name, buffer, buffer_size);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
2020-01-10 22:14:04 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int set_process_name(char const* name, size_t name_length)
|
2020-07-27 16:42:10 +00:00
|
|
|
{
|
|
|
|
int rc = syscall(SC_set_process_name, name, name_length);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int pledge(char const* promises, char const* execpromises)
|
2020-01-11 19:45:51 +00:00
|
|
|
{
|
|
|
|
Syscall::SC_pledge_params params {
|
|
|
|
{ promises, promises ? strlen(promises) : 0 },
|
|
|
|
{ execpromises, execpromises ? strlen(execpromises) : 0 }
|
|
|
|
};
|
|
|
|
int rc = syscall(SC_pledge, ¶ms);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 18:01:22 +00:00
|
|
|
}
|
2020-01-11 19:45:51 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int unveil(char const* path, char const* permissions)
|
Kernel: Add a basic implementation of unveil()
This syscall is a complement to pledge() and adds the same sort of
incremental relinquishing of capabilities for filesystem access.
The first call to unveil() will "drop a veil" on the process, and from
now on, only unveiled parts of the filesystem are visible to it.
Each call to unveil() specifies a path to either a directory or a file
along with permissions for that path. The permissions are a combination
of the following:
- r: Read access (like the "rpath" promise)
- w: Write access (like the "wpath" promise)
- x: Execute access
- c: Create/remove access (like the "cpath" promise)
Attempts to open a path that has not been unveiled with fail with
ENOENT. If the unveiled path lacks sufficient permissions, it will fail
with EACCES.
Like pledge(), subsequent calls to unveil() with the same path can only
remove permissions, not add them.
Once you call unveil(nullptr, nullptr), the veil is locked, and it's no
longer possible to unveil any more paths for the process, ever.
This concept comes from OpenBSD, and their implementation does various
things differently, I'm sure. This is just a first implementation for
SerenityOS, and we'll keep improving on it as we go. :^)
2020-01-20 21:12:04 +00:00
|
|
|
{
|
|
|
|
Syscall::SC_unveil_params params {
|
2022-11-04 17:20:11 +00:00
|
|
|
static_cast<int>(UnveilFlags::CurrentProgram),
|
Kernel: Add a basic implementation of unveil()
This syscall is a complement to pledge() and adds the same sort of
incremental relinquishing of capabilities for filesystem access.
The first call to unveil() will "drop a veil" on the process, and from
now on, only unveiled parts of the filesystem are visible to it.
Each call to unveil() specifies a path to either a directory or a file
along with permissions for that path. The permissions are a combination
of the following:
- r: Read access (like the "rpath" promise)
- w: Write access (like the "wpath" promise)
- x: Execute access
- c: Create/remove access (like the "cpath" promise)
Attempts to open a path that has not been unveiled with fail with
ENOENT. If the unveiled path lacks sufficient permissions, it will fail
with EACCES.
Like pledge(), subsequent calls to unveil() with the same path can only
remove permissions, not add them.
Once you call unveil(nullptr, nullptr), the veil is locked, and it's no
longer possible to unveil any more paths for the process, ever.
This concept comes from OpenBSD, and their implementation does various
things differently, I'm sure. This is just a first implementation for
SerenityOS, and we'll keep improving on it as we go. :^)
2020-01-20 21:12:04 +00:00
|
|
|
{ path, path ? strlen(path) : 0 },
|
|
|
|
{ permissions, permissions ? strlen(permissions) : 0 }
|
|
|
|
};
|
|
|
|
int rc = syscall(SC_unveil, ¶ms);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
2020-02-20 05:58:48 +00:00
|
|
|
|
2022-10-28 13:37:20 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/getpass.html
|
2022-04-01 17:58:27 +00:00
|
|
|
char* getpass(char const* prompt)
|
2020-02-20 05:58:48 +00:00
|
|
|
{
|
2022-10-28 13:37:20 +00:00
|
|
|
int tty = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
|
|
|
|
if (tty < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
static char password[PASS_MAX];
|
|
|
|
ssize_t chars_read;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto close_tty = ScopeGuard([tty] {
|
|
|
|
close(tty);
|
|
|
|
});
|
|
|
|
|
|
|
|
struct termios backup { };
|
|
|
|
if (tcgetattr(tty, &backup) < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto restore_termios = ScopeGuard([tty, backup] {
|
|
|
|
tcsetattr(tty, TCSAFLUSH, &backup);
|
|
|
|
});
|
|
|
|
|
|
|
|
struct termios noecho = backup;
|
|
|
|
noecho.c_lflag &= ~(ECHO);
|
|
|
|
noecho.c_lflag |= ICANON;
|
|
|
|
|
|
|
|
if (tcsetattr(tty, TCSAFLUSH, &noecho) < 0)
|
|
|
|
return nullptr;
|
|
|
|
if (tcdrain(tty) < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (prompt) {
|
|
|
|
if (write(tty, prompt, strlen(prompt)) < 0)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
chars_read = read(tty, password, sizeof(password));
|
|
|
|
}
|
|
|
|
|
|
|
|
write(tty, "\n", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chars_read < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (chars_read > 0 && (password[chars_read - 1] == '\n' || chars_read == sizeof(password)))
|
|
|
|
password[chars_read - 1] = '\0';
|
|
|
|
else
|
|
|
|
password[chars_read] = '\0';
|
|
|
|
|
|
|
|
return password;
|
2020-02-20 05:58:48 +00:00
|
|
|
}
|
2020-07-14 20:41:59 +00:00
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
|
2020-07-14 20:41:59 +00:00
|
|
|
long sysconf(int name)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_sysconf, name);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
2020-07-27 21:39:31 +00:00
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpagesize.html
|
2020-07-27 21:39:31 +00:00
|
|
|
int getpagesize()
|
|
|
|
{
|
|
|
|
return PAGE_SIZE;
|
|
|
|
}
|
2021-08-11 16:49:13 +00:00
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html
|
2021-08-11 16:49:13 +00:00
|
|
|
int pause()
|
|
|
|
{
|
|
|
|
return select(0, nullptr, nullptr, nullptr, nullptr);
|
|
|
|
}
|
2021-09-22 21:59:13 +00:00
|
|
|
|
2022-01-07 08:33:11 +00:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chroot.html
|
2022-04-01 17:58:27 +00:00
|
|
|
int chroot(char const* path)
|
2021-09-22 21:59:13 +00:00
|
|
|
{
|
|
|
|
dbgln("FIXME: chroot(\"{}\")", path);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-03-29 20:47:06 +00:00
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/getdtablesize.html
|
|
|
|
int getdtablesize()
|
|
|
|
{
|
|
|
|
rlimit dtablesize;
|
|
|
|
int rc = getrlimit(RLIMIT_NOFILE, &dtablesize);
|
|
|
|
__RETURN_WITH_ERRNO(rc, dtablesize.rlim_cur, rc);
|
|
|
|
}
|
2022-04-03 17:36:27 +00:00
|
|
|
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/007904975/functions/nice.html
|
|
|
|
int nice(int incr)
|
|
|
|
{
|
|
|
|
dbgln("FIXME: nice was called with: {}, not implemented", incr);
|
|
|
|
return incr;
|
|
|
|
}
|
2022-06-29 03:00:28 +00:00
|
|
|
|
|
|
|
int brk(void* addr)
|
|
|
|
{
|
|
|
|
dbgln("TODO: brk({:#x})", addr);
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* sbrk(intptr_t incr)
|
|
|
|
{
|
|
|
|
dbgln("TODO: sbrk({:#x})", incr);
|
|
|
|
errno = ENOMEM;
|
|
|
|
return reinterpret_cast<void*>(-1);
|
|
|
|
}
|
2020-01-11 19:45:51 +00:00
|
|
|
}
|