2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-07-25 15:21:50 +02:00
|
|
|
#include <AK/ScopedValueRollback.h>
|
2019-11-13 21:49:24 +01:00
|
|
|
#include <AK/String.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <Kernel/Syscall.h>
|
2020-01-10 12:20:36 +01:00
|
|
|
#include <alloca.h>
|
2018-11-05 16:40:48 +01:00
|
|
|
#include <assert.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-11-07 01:38:51 +01:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <stdarg.h>
|
2018-11-07 01:38:51 +01:00
|
|
|
#include <stdio.h>
|
2019-02-08 02:38:21 +01:00
|
|
|
#include <stdlib.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <string.h>
|
2018-11-16 13:11:21 +01:00
|
|
|
#include <sys/ioctl.h>
|
2019-01-22 00:58:13 +01:00
|
|
|
#include <sys/types.h>
|
2019-11-17 20:01:03 +01:00
|
|
|
#include <termios.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <unistd.h>
|
2018-10-22 13:57:25 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2019-04-22 18:44:45 +02:00
|
|
|
int systrace(pid_t pid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_systrace, pid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-02-27 12:32:53 +01:00
|
|
|
int chown(const char* pathname, uid_t uid, gid_t gid)
|
2019-02-23 17:24:50 +01:00
|
|
|
{
|
2020-01-11 10:17:08 +01:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Syscall::SC_chown_params params { { pathname, strlen(pathname) }, uid, gid };
|
|
|
|
int rc = syscall(SC_chown, ¶ms);
|
2019-02-27 12:32:53 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-02-23 17:24:50 +01:00
|
|
|
}
|
|
|
|
|
2019-06-01 20:31:36 +02: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);
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:41:58 +01:00
|
|
|
pid_t fork()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_fork);
|
2018-11-03 01:49:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:18:15 +01:00
|
|
|
int execv(const char* path, char* const argv[])
|
|
|
|
{
|
2019-03-27 01:39:13 +01:00
|
|
|
return execve(path, argv, environ);
|
2019-03-14 15:18:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:03:14 +01:00
|
|
|
int execve(const char* filename, char* const argv[], char* const envp[])
|
2018-11-03 01:49:40 +01:00
|
|
|
{
|
2020-01-10 12:20:36 +01: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) {
|
|
|
|
output.strings[i].characters = vec[i];
|
|
|
|
output.strings[i].length = strlen(vec[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Syscall::SC_execve_params params;
|
2020-01-10 20:15:58 +01: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 12:20:36 +01: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 01:49:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 20:41:58 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:16:26 +02:00
|
|
|
int execvpe(const char* filename, char* const argv[], char* const envp[])
|
2019-01-23 06:35:34 +01:00
|
|
|
{
|
2020-02-01 16:05:04 +01:00
|
|
|
if (strchr(filename, '/'))
|
|
|
|
return execve(filename, argv, envp);
|
|
|
|
|
2019-07-25 15:21:50 +02:00
|
|
|
ScopedValueRollback errno_rollback(errno);
|
|
|
|
String path = getenv("PATH");
|
|
|
|
if (path.is_empty())
|
|
|
|
path = "/bin:/usr/bin";
|
|
|
|
auto parts = path.split(':');
|
|
|
|
for (auto& part : parts) {
|
|
|
|
auto candidate = String::format("%s/%s", part.characters(), filename);
|
|
|
|
int rc = execve(candidate.characters(), argv, envp);
|
2019-03-27 01:39:13 +01:00
|
|
|
if (rc < 0 && errno != ENOENT) {
|
2019-07-25 15:21:50 +02:00
|
|
|
errno_rollback.set_override_rollback_value(errno);
|
|
|
|
dbg() << "execvpe() failed on attempt (" << candidate << ") with " << strerror(errno);
|
2019-03-27 01:39:13 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 15:21:50 +02:00
|
|
|
errno_rollback.set_override_rollback_value(ENOENT);
|
|
|
|
dbg() << "execvpe() leaving :(";
|
2019-03-27 01:39:13 +01:00
|
|
|
return -1;
|
2019-01-23 06:35:34 +01:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:16:26 +02:00
|
|
|
int execvp(const char* filename, char* const argv[])
|
|
|
|
{
|
2019-07-25 07:01:00 +02:00
|
|
|
int rc = execvpe(filename, argv, environ);
|
|
|
|
dbg() << "execvp() about to return " << rc << " with errno=" << errno;
|
|
|
|
return rc;
|
2019-04-26 03:16:26 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 04:32:31 +01:00
|
|
|
int execl(const char* filename, const char* arg0, ...)
|
|
|
|
{
|
2019-04-20 14:02:19 +02:00
|
|
|
Vector<const char*, 16> args;
|
2019-02-03 04:32:31 +01:00
|
|
|
args.append(arg0);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
for (;;) {
|
|
|
|
const char* arg = va_arg(ap, const char*);
|
|
|
|
if (!arg)
|
|
|
|
break;
|
|
|
|
args.append(arg);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
2019-02-08 00:12:12 +01:00
|
|
|
args.append(nullptr);
|
2019-04-23 13:00:53 +02:00
|
|
|
return execve(filename, const_cast<char* const*>(args.data()), environ);
|
2019-02-03 04:32:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-16 02:35:48 -06:00
|
|
|
int execlp(const char* filename, const char* arg0, ...)
|
|
|
|
{
|
|
|
|
Vector<const char*, 16> args;
|
|
|
|
args.append(arg0);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
for (;;) {
|
|
|
|
const char* arg = va_arg(ap, const char*);
|
|
|
|
if (!arg)
|
|
|
|
break;
|
|
|
|
args.append(arg);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
args.append(nullptr);
|
|
|
|
return execvpe(filename, const_cast<char* const*>(args.data()), environ);
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:57:25 +02:00
|
|
|
uid_t getuid()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_getuid);
|
2018-10-22 13:57:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 10:00:37 +02:00
|
|
|
gid_t getgid()
|
2018-10-22 13:57:25 +02:00
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_getgid);
|
2018-10-22 13:57:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:04:19 +01:00
|
|
|
uid_t geteuid()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_geteuid);
|
2018-11-05 15:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getegid()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_getegid);
|
2018-11-05 15:04:19 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 10:00:37 +02:00
|
|
|
pid_t getpid()
|
2018-10-22 13:57:25 +02:00
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_getpid);
|
2018-10-22 13:57:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 13:33:06 +01:00
|
|
|
pid_t getppid()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_getppid);
|
2018-11-06 13:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:52:23 +03:00
|
|
|
pid_t getsid(pid_t pid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_getsid, pid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-02 12:56:51 +01:00
|
|
|
pid_t setsid()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_setsid);
|
2018-11-03 01:49:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 12:56:51 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 13:14:25 +01:00
|
|
|
pid_t tcgetpgrp(int fd)
|
2018-11-02 12:56:51 +01:00
|
|
|
{
|
2018-11-16 13:11:21 +01:00
|
|
|
return ioctl(fd, TIOCGPGRP);
|
2018-11-02 13:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int tcsetpgrp(int fd, pid_t pgid)
|
|
|
|
{
|
2018-11-16 13:11:21 +01:00
|
|
|
return ioctl(fd, TIOCSPGRP, pgid);
|
2018-11-02 12:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int setpgid(pid_t pid, pid_t pgid)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_setpgid, pid, pgid);
|
2018-11-03 01:49:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 12:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t getpgid(pid_t pid)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_getpgid, pid);
|
2018-11-03 01:49:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 12:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t getpgrp()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_getpgrp);
|
2018-11-03 01:49:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 12:56:51 +01:00
|
|
|
}
|
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
ssize_t read(int fd, void* buf, size_t count)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_read, fd, buf, count);
|
2018-10-25 12:06:00 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 15:33:37 +01:00
|
|
|
ssize_t write(int fd, const void* buf, size_t count)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_write, fd, buf, count);
|
2018-10-30 15:33:37 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-30 22:03:02 +01:00
|
|
|
int ttyname_r(int fd, char* buffer, size_t size)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_ttyname_r, fd, buffer, size);
|
2018-10-30 22:03:02 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char ttyname_buf[32];
|
|
|
|
char* ttyname(int fd)
|
|
|
|
{
|
|
|
|
if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
|
|
|
|
return nullptr;
|
|
|
|
return ttyname_buf;
|
|
|
|
}
|
|
|
|
|
2018-10-23 10:12:50 +02:00
|
|
|
int close(int fd)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_close, fd);
|
2018-10-25 12:06:00 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 10:14:56 +01:00
|
|
|
int lstat(const char* path, struct stat* statbuf)
|
2018-10-24 13:19:36 +02:00
|
|
|
{
|
2020-01-06 10:51:50 +01:00
|
|
|
if (!path) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-05 22:02:31 +01:00
|
|
|
int rc = syscall(SC_lstat, path, strlen(path), statbuf);
|
2018-10-25 12:06:00 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-24 13:19:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 10:14:56 +01:00
|
|
|
int stat(const char* path, struct stat* statbuf)
|
|
|
|
{
|
2020-01-06 10:51:50 +01:00
|
|
|
if (!path) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-05 22:02:31 +01:00
|
|
|
int rc = syscall(SC_stat, path, strlen(path), statbuf);
|
2018-10-31 10:14:56 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-06-07 11:49:03 +02:00
|
|
|
int fstat(int fd, struct stat* statbuf)
|
2018-11-11 00:20:53 +01:00
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_fstat, fd, statbuf);
|
2018-11-11 00:20:53 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-26 14:24:11 +02:00
|
|
|
int chdir(const char* path)
|
|
|
|
{
|
2020-01-06 10:51:50 +01:00
|
|
|
if (!path) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-05 22:06:25 +01:00
|
|
|
int rc = syscall(SC_chdir, path, strlen(path));
|
2018-10-26 14:24:11 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-09-11 20:18:25 -03:00
|
|
|
int fchdir(int fd)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_fchdir, fd);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:28:22 +02:00
|
|
|
char* getcwd(char* buffer, size_t size)
|
|
|
|
{
|
2019-02-08 02:38:21 +01:00
|
|
|
if (!buffer) {
|
|
|
|
size = size ? size : PATH_MAX;
|
|
|
|
buffer = (char*)malloc(size);
|
|
|
|
}
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_getcwd, buffer, size);
|
2018-10-25 12:06:00 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, buffer, nullptr);
|
2018-10-24 14:28:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 15:59:57 +01:00
|
|
|
char* getwd(char* buf)
|
|
|
|
{
|
2018-11-11 15:36:40 +01:00
|
|
|
auto* p = getcwd(buf, PATH_MAX);
|
|
|
|
return p;
|
2018-11-06 15:59:57 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:53:49 +02:00
|
|
|
int sleep(unsigned seconds)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_sleep, seconds);
|
2018-10-25 13:53:49 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 16:11:28 +01:00
|
|
|
int usleep(useconds_t usec)
|
|
|
|
{
|
|
|
|
return syscall(SC_usleep, usec);
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:54:29 +02:00
|
|
|
int gethostname(char* buffer, size_t size)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_gethostname, buffer, size);
|
2018-10-26 09:54:29 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:11:51 +01:00
|
|
|
ssize_t readlink(const char* path, char* buffer, size_t size)
|
|
|
|
{
|
2020-01-10 20:13:23 +01:00
|
|
|
Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };
|
|
|
|
int rc = syscall(SC_readlink, ¶ms);
|
2018-10-28 14:11:51 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-31 17:50:43 +01:00
|
|
|
off_t lseek(int fd, off_t offset, int whence)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_lseek, fd, offset, whence);
|
2018-10-31 17:50:43 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:26:40 +01:00
|
|
|
int link(const char* old_path, const char* new_path)
|
2018-11-05 16:40:48 +01:00
|
|
|
{
|
2020-01-10 21:26:47 +01: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 13:26:40 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 16:40:48 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 07:03:44 +01:00
|
|
|
int unlink(const char* pathname)
|
2018-11-05 16:40:48 +01:00
|
|
|
{
|
2020-01-09 12:41:15 +01:00
|
|
|
int rc = syscall(SC_unlink, pathname, strlen(pathname));
|
2019-01-22 07:03:44 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 16:40:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 01:50:34 +01:00
|
|
|
int symlink(const char* target, const char* linkpath)
|
|
|
|
{
|
2020-01-11 10:31:33 +01:00
|
|
|
if (!target || !linkpath) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Syscall::SC_symlink_params params { { target, strlen(target) }, { linkpath, strlen(linkpath) } };
|
|
|
|
int rc = syscall(SC_symlink, ¶ms);
|
2019-03-02 01:50:34 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-01-28 04:16:01 +01:00
|
|
|
int rmdir(const char* pathname)
|
|
|
|
{
|
2020-01-06 11:05:59 +01:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int rc = syscall(SC_rmdir, pathname, strlen(pathname));
|
2019-01-28 04:16:01 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:16:00 +01:00
|
|
|
int isatty(int fd)
|
|
|
|
{
|
2019-11-17 20:01:03 +01:00
|
|
|
struct termios dummy;
|
2019-11-17 20:11:56 +01:00
|
|
|
return tcgetattr(fd, &dummy) == 0;
|
2018-11-05 18:16:00 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 19:01:22 +01:00
|
|
|
int getdtablesize()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_getdtablesize);
|
2018-11-11 10:11:09 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 19:01:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int dup(int old_fd)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_dup, old_fd);
|
2018-11-05 19:01:22 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-22 13:57:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 19:01:22 +01:00
|
|
|
int dup2(int old_fd, int new_fd)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_dup2, old_fd, new_fd);
|
2018-11-05 19:01:22 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-07 01:38:51 +01:00
|
|
|
int setgroups(size_t size, const gid_t* list)
|
|
|
|
{
|
2020-01-04 13:00:50 +01:00
|
|
|
int rc = syscall(SC_setgroups, size, list);
|
2018-11-07 01:38:51 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getgroups(int size, gid_t list[])
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_getgroups, size, list);
|
2018-11-07 01:38:51 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-11 00:20:53 +01:00
|
|
|
int pipe(int pipefd[2])
|
|
|
|
{
|
2019-08-05 15:29:05 +03:00
|
|
|
return pipe2(pipefd, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pipe2(int pipefd[2], int flags)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_pipe, pipefd, flags);
|
2018-11-11 00:20:53 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int alarm(unsigned int seconds)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
return syscall(SC_alarm, seconds);
|
2018-11-11 00:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int setuid(uid_t uid)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_setuid, uid);
|
2018-11-11 00:20:53 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setgid(uid_t gid)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_setgid, gid);
|
2018-11-11 00:20:53 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int access(const char* pathname, int mode)
|
|
|
|
{
|
2020-01-06 10:43:53 +01:00
|
|
|
if (!pathname) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int rc = syscall(SC_access, pathname, strlen(pathname), mode);
|
2018-11-11 00:20:53 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:59:58 +02:00
|
|
|
int mknod(const char* pathname, mode_t mode, dev_t dev)
|
2018-11-17 00:11:08 +01:00
|
|
|
{
|
2020-01-11 10:27:37 +01: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 22:59:58 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-17 00:11:08 +01:00
|
|
|
}
|
|
|
|
|
2018-11-17 15:56:09 +01:00
|
|
|
long fpathconf(int fd, int name)
|
|
|
|
{
|
2019-06-07 11:49:03 +02:00
|
|
|
(void)fd;
|
|
|
|
(void)name;
|
2019-09-02 15:36:22 +10:00
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case _PC_PATH_MAX:
|
|
|
|
return PATH_MAX;
|
2019-12-01 04:34:34 -06:00
|
|
|
case _PC_VDISABLE:
|
|
|
|
return _POSIX_VDISABLE;
|
2019-09-02 15:36:22 +10:00
|
|
|
}
|
|
|
|
|
2019-04-23 21:52:02 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-17 15:56:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long pathconf(const char* path, int name)
|
|
|
|
{
|
2019-06-07 11:49:03 +02:00
|
|
|
(void)path;
|
2019-09-02 15:36:22 +10:00
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case _PC_PATH_MAX:
|
|
|
|
return PATH_MAX;
|
2019-11-16 02:35:48 -06:00
|
|
|
case _PC_PIPE_BUF:
|
|
|
|
return PIPE_BUF;
|
2019-09-02 15:36:22 +10:00
|
|
|
}
|
|
|
|
|
2019-04-23 21:52:02 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-17 15:56:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void _exit(int status)
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
syscall(SC_exit, status);
|
2019-04-23 21:52:02 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-17 15:56:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 00:39:29 +01:00
|
|
|
void sync()
|
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
syscall(SC_sync);
|
2018-12-20 00:39:29 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 09:52:22 +02:00
|
|
|
int create_shared_buffer(int size, void** buffer)
|
2019-02-16 12:13:43 +01:00
|
|
|
{
|
2019-07-18 09:52:22 +02:00
|
|
|
int rc = syscall(SC_create_shared_buffer, size, buffer);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int share_buffer_with(int shared_buffer_id, pid_t peer_pid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_share_buffer_with, shared_buffer_id, peer_pid);
|
2019-02-16 12:13:43 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-07-29 07:26:01 +02:00
|
|
|
int share_buffer_globally(int shared_buffer_id)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_share_buffer_globally, shared_buffer_id);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int set_process_icon(int icon_id)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_set_process_icon, icon_id);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:13:43 +01:00
|
|
|
void* get_shared_buffer(int shared_buffer_id)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_get_shared_buffer, shared_buffer_id);
|
2019-02-17 00:13:47 +01:00
|
|
|
if (rc < 0 && -rc < EMAXERRNO) {
|
|
|
|
errno = -rc;
|
|
|
|
return (void*)-1;
|
|
|
|
}
|
|
|
|
return (void*)rc;
|
2019-02-16 12:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int release_shared_buffer(int shared_buffer_id)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_release_shared_buffer, shared_buffer_id);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-03-08 12:22:55 +01:00
|
|
|
int get_shared_buffer_size(int shared_buffer_id)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_get_shared_buffer_size, shared_buffer_id);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int seal_shared_buffer(int shared_buffer_id)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_seal_shared_buffer, shared_buffer_id);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-02-26 12:57:02 +01:00
|
|
|
char* getlogin()
|
|
|
|
{
|
2019-03-10 03:15:36 +01:00
|
|
|
static char __getlogin_buffer[256];
|
|
|
|
if (auto* passwd = getpwuid(getuid())) {
|
|
|
|
strncpy(__getlogin_buffer, passwd->pw_name, sizeof(__getlogin_buffer));
|
2020-01-08 16:01:51 +01:00
|
|
|
endpwent();
|
2019-03-10 03:15:36 +01:00
|
|
|
return __getlogin_buffer;
|
|
|
|
}
|
2020-01-08 16:01:51 +01:00
|
|
|
endpwent();
|
2019-03-10 03:15:36 +01:00
|
|
|
return nullptr;
|
2019-02-26 12:57:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-24 00:53:39 +01:00
|
|
|
int ftruncate(int fd, off_t length)
|
|
|
|
{
|
2019-04-09 01:10:00 +02:00
|
|
|
int rc = syscall(SC_ftruncate, fd, length);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-03-24 00:53:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 13:03:49 +01:00
|
|
|
int gettid()
|
|
|
|
{
|
2020-01-06 14:38:48 +01:00
|
|
|
return syscall(SC_gettid);
|
2019-03-25 13:03:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int donate(int tid)
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_donate, tid);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:32:53 +02:00
|
|
|
void sysbeep()
|
2019-05-15 21:40:41 +02:00
|
|
|
{
|
|
|
|
syscall(SC_beep);
|
|
|
|
}
|
|
|
|
|
2019-05-19 19:54:20 +02:00
|
|
|
int fsync(int fd)
|
|
|
|
{
|
|
|
|
UNUSED_PARAM(fd);
|
|
|
|
dbgprintf("FIXME: Implement fsync()\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-19 17:58:12 +10:00
|
|
|
|
2019-07-19 21:08:26 +10:00
|
|
|
int halt()
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_halt);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:58:12 +10:00
|
|
|
int reboot()
|
|
|
|
{
|
|
|
|
int rc = syscall(SC_reboot);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
2019-07-21 09:59:17 +02:00
|
|
|
|
2020-01-11 18:25:26 +03:00
|
|
|
int mount(const char* source, const char* target, const char* fs_type, int flags)
|
2019-08-02 23:18:47 +10:00
|
|
|
{
|
2020-01-11 10:46:21 +01:00
|
|
|
if (!source || !target || !fs_type) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-11 18:25:26 +03:00
|
|
|
Syscall::SC_mount_params params {
|
|
|
|
{ source, strlen(source) },
|
|
|
|
{ target, strlen(target) },
|
|
|
|
{ fs_type, strlen(fs_type) },
|
|
|
|
flags
|
|
|
|
};
|
2020-01-11 10:46:21 +01:00
|
|
|
int rc = syscall(SC_mount, ¶ms);
|
2019-08-02 23:18:47 +10:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-08-11 23:56:39 +10:00
|
|
|
int umount(const char* mountpoint)
|
|
|
|
{
|
2020-01-09 12:41:15 +01:00
|
|
|
int rc = syscall(SC_umount, mountpoint, strlen(mountpoint));
|
2019-08-11 23:56:39 +10:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2019-07-21 09:59:17 +02:00
|
|
|
void dump_backtrace()
|
|
|
|
{
|
|
|
|
syscall(SC_dump_backtrace);
|
|
|
|
}
|
2019-08-15 20:55:10 +02: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 23:14:04 +01:00
|
|
|
|
|
|
|
int chroot(const char* path)
|
2020-01-12 21:03:42 +03:00
|
|
|
{
|
|
|
|
return chroot_with_mount_flags(path, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int chroot_with_mount_flags(const char* path, int mount_flags)
|
2020-01-10 23:14:04 +01:00
|
|
|
{
|
|
|
|
if (!path) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-12 21:03:42 +03:00
|
|
|
int rc = syscall(SC_chroot, path, strlen(path), mount_flags);
|
2020-01-10 23:14:04 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
2020-01-11 20:45:51 +01:00
|
|
|
|
|
|
|
int pledge(const char* promises, const char* execpromises)
|
|
|
|
{
|
|
|
|
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 19:01:22 +01:00
|
|
|
}
|
2020-01-11 20:45:51 +01:00
|
|
|
|
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 22:12:04 +01:00
|
|
|
int unveil(const char* path, const char* permissions)
|
|
|
|
{
|
|
|
|
Syscall::SC_unveil_params params {
|
|
|
|
{ path, path ? strlen(path) : 0 },
|
|
|
|
{ permissions, permissions ? strlen(permissions) : 0 }
|
|
|
|
};
|
|
|
|
int rc = syscall(SC_unveil, ¶ms);
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
2020-01-11 20:45:51 +01:00
|
|
|
}
|