2023-04-12 17:53:02 +03:00
|
|
|
#include <BAN/Assert.h>
|
2023-05-26 22:21:07 +03:00
|
|
|
#include <kernel/Syscall.h>
|
2023-05-07 01:51:39 +03:00
|
|
|
#include <errno.h>
|
2024-05-23 15:05:22 +03:00
|
|
|
#include <pwd.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <stdarg.h>
|
2023-04-25 12:38:08 +03:00
|
|
|
#include <stdio.h>
|
2023-05-31 20:57:33 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-06-05 14:36:17 +03:00
|
|
|
#include <sys/stat.h>
|
2023-04-12 17:53:02 +03:00
|
|
|
#include <sys/syscall.h>
|
2023-04-05 23:58:40 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-06-05 20:34:08 +03:00
|
|
|
char** environ;
|
|
|
|
|
2023-09-23 02:26:23 +03:00
|
|
|
extern void init_malloc();
|
2023-06-05 20:34:08 +03:00
|
|
|
extern "C" void _init_libc(char** _environ)
|
2023-05-29 20:21:19 +03:00
|
|
|
{
|
2023-09-23 02:26:23 +03:00
|
|
|
init_malloc();
|
2023-09-23 02:43:02 +03:00
|
|
|
|
|
|
|
if (!_environ)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t env_count = 0;
|
|
|
|
while (_environ[env_count])
|
|
|
|
env_count++;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-09-23 02:43:02 +03:00
|
|
|
environ = (char**)malloc(sizeof(char*) * env_count + 1);
|
|
|
|
for (size_t i = 0; i < env_count; i++)
|
|
|
|
{
|
|
|
|
size_t bytes = strlen(_environ[i]) + 1;
|
|
|
|
environ[i] = (char*)malloc(bytes);
|
|
|
|
memcpy(environ[i], _environ[i], bytes);
|
|
|
|
}
|
|
|
|
environ[env_count] = nullptr;
|
2023-05-29 20:21:19 +03:00
|
|
|
}
|
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
void _exit(int status)
|
|
|
|
{
|
|
|
|
syscall(SYS_EXIT, status);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
long syscall(long syscall, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, syscall);
|
|
|
|
|
2023-07-31 22:24:11 +03:00
|
|
|
uintptr_t arg1 = va_arg(args, uintptr_t);
|
|
|
|
uintptr_t arg2 = va_arg(args, uintptr_t);
|
|
|
|
uintptr_t arg3 = va_arg(args, uintptr_t);
|
|
|
|
uintptr_t arg4 = va_arg(args, uintptr_t);
|
|
|
|
uintptr_t arg5 = va_arg(args, uintptr_t);
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
va_end(args);
|
2023-07-31 22:24:11 +03:00
|
|
|
|
|
|
|
long ret = Kernel::syscall(syscall, arg1, arg2, arg3, arg4, arg5);
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-05-07 01:51:39 +03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-04-12 17:53:02 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-11 01:10:28 +03:00
|
|
|
int close(int fd)
|
|
|
|
{
|
|
|
|
return syscall(SYS_CLOSE, fd);
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:15:55 +03:00
|
|
|
ssize_t read(int fildes, void* buf, size_t nbyte)
|
|
|
|
{
|
|
|
|
return syscall(SYS_READ, fildes, buf, nbyte);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t write(int fildes, const void* buf, size_t nbyte)
|
|
|
|
{
|
|
|
|
return syscall(SYS_WRITE, fildes, buf, nbyte);
|
|
|
|
}
|
|
|
|
|
2023-11-11 23:16:52 +02:00
|
|
|
ssize_t readlink(const char* __restrict path, char* __restrict buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
return syscall(SYS_READLINK, path, buf, bufsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t readlinkat(int fd, const char* __restrict path, char* __restrict buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
return syscall(SYS_READLINKAT, fd, path, buf, bufsize);
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:47:30 +02:00
|
|
|
ssize_t pread(int fildes, void* buf, size_t nbyte, off_t offset)
|
|
|
|
{
|
|
|
|
return syscall(SYS_PREAD, fildes, buf, nbyte, offset);
|
|
|
|
}
|
|
|
|
|
2024-05-23 14:49:23 +03:00
|
|
|
off_t lseek(int fildes, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SEEK, fildes, offset, whence);
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:08:04 +03:00
|
|
|
int ftruncate(int fildes, off_t length)
|
|
|
|
{
|
|
|
|
return syscall(SYS_TRUNCATE, fildes, length);
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:03:29 +03:00
|
|
|
int dup(int fildes)
|
|
|
|
{
|
|
|
|
return syscall(SYS_DUP, fildes);
|
|
|
|
}
|
|
|
|
|
2023-07-06 23:17:54 +03:00
|
|
|
int dup2(int fildes, int fildes2)
|
|
|
|
{
|
|
|
|
return syscall(SYS_DUP2, fildes, fildes2);
|
|
|
|
}
|
|
|
|
|
2024-06-03 03:36:25 +03:00
|
|
|
int isatty(int fildes)
|
|
|
|
{
|
|
|
|
return syscall(SYS_ISATTY, fildes) >= 0;
|
|
|
|
}
|
|
|
|
|
2023-05-31 22:36:26 +03:00
|
|
|
int execl(const char* pathname, const char* arg0, ...)
|
|
|
|
{
|
|
|
|
if (arg0 == nullptr)
|
|
|
|
{
|
|
|
|
char* temp = nullptr;
|
|
|
|
return execv(pathname, &temp);
|
|
|
|
}
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-05-31 22:36:26 +03:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, arg0);
|
|
|
|
int argc = 1;
|
|
|
|
while (va_arg(ap, const char*))
|
|
|
|
argc++;
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
|
|
|
|
if (argv == nullptr)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
argv[0] = (char*)arg0;
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
argv[i] = va_arg(ap, char*);
|
|
|
|
argv[argc] = nullptr;
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return execv(pathname, argv);
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:53:37 +03:00
|
|
|
int execle(const char* pathname, const char* arg0, ...)
|
2024-01-24 14:43:46 +02:00
|
|
|
{
|
2023-06-05 21:53:37 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
if (arg0)
|
|
|
|
{
|
|
|
|
va_start(ap, arg0);
|
|
|
|
argc = 1;
|
|
|
|
while (va_arg(ap, const char*))
|
|
|
|
argc++;
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
|
|
|
|
if (argv == nullptr)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char** envp = nullptr;
|
|
|
|
|
|
|
|
va_start(ap, arg0);
|
|
|
|
argv[0] = (char*)arg0;
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
argv[i] = va_arg(ap, char*);
|
|
|
|
argv[argc] = nullptr;
|
|
|
|
envp = va_arg(ap, char**);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return execve(pathname, argv, envp);
|
|
|
|
}
|
|
|
|
|
2023-05-31 20:57:33 +03:00
|
|
|
int execv(const char* pathname, char* const argv[])
|
|
|
|
{
|
2023-06-05 21:53:37 +03:00
|
|
|
return execve(pathname, argv, environ);
|
|
|
|
}
|
|
|
|
|
|
|
|
int execve(const char* pathname, char* const argv[], char* const envp[])
|
|
|
|
{
|
|
|
|
return syscall(SYS_EXEC, pathname, argv, envp);
|
2023-05-31 20:57:33 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 18:14:00 +02:00
|
|
|
int execvp(const char* file, char* const argv[])
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
2023-12-19 10:28:15 +02:00
|
|
|
const char* pathname = NULL;
|
2023-12-06 18:14:00 +02:00
|
|
|
|
|
|
|
// do path resolution if file doesn't contain /
|
|
|
|
if (strchr(file, '/') == nullptr)
|
|
|
|
{
|
|
|
|
const char* cur = getenv("PATH");
|
|
|
|
if (!cur)
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*cur)
|
|
|
|
{
|
|
|
|
const char* end = strchrnul(cur, ':');
|
|
|
|
size_t len = end - cur;
|
|
|
|
|
|
|
|
ASSERT(strlen(file) + 1 + len < sizeof(buffer));
|
|
|
|
|
|
|
|
strncpy(buffer, cur, len);
|
|
|
|
strcat(buffer, "/");
|
|
|
|
strcat(buffer, file);
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(buffer, &st) == 0)
|
|
|
|
{
|
|
|
|
pathname = buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = end;
|
|
|
|
if (*cur)
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 10:28:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pathname = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pathname)
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2023-12-06 18:14:00 +02:00
|
|
|
|
|
|
|
return execve(pathname, argv, environ);
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:58:40 +03:00
|
|
|
pid_t fork(void)
|
|
|
|
{
|
2023-05-28 18:08:26 +03:00
|
|
|
return syscall(SYS_FORK);
|
2023-04-05 23:58:40 +03:00
|
|
|
}
|
2023-05-28 22:34:48 +03:00
|
|
|
|
2023-07-06 22:16:26 +03:00
|
|
|
int pipe(int fildes[2])
|
|
|
|
{
|
|
|
|
return syscall(SYS_PIPE, fildes);
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:34:48 +03:00
|
|
|
unsigned int sleep(unsigned int seconds)
|
|
|
|
{
|
2023-12-06 13:13:43 +02:00
|
|
|
unsigned int ret = syscall(SYS_SLEEP, seconds);
|
|
|
|
if (ret > 0)
|
|
|
|
errno = EINTR;
|
|
|
|
return ret;
|
2023-05-29 20:21:19 +03:00
|
|
|
}
|
2023-06-11 22:15:35 +03:00
|
|
|
|
2023-06-12 01:24:46 +03:00
|
|
|
char* getcwd(char* buf, size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((char*)syscall(SYS_GET_PWD, buf, size) == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
setenv("PWD", buf, 1);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int chdir(const char* path)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_PWD, path);
|
|
|
|
}
|
|
|
|
|
2024-01-02 22:19:38 +02:00
|
|
|
int chown(const char* path, uid_t owner, gid_t group)
|
|
|
|
{
|
|
|
|
return syscall(SYS_CHOWN, path, owner, group);
|
|
|
|
}
|
|
|
|
|
2023-09-11 01:26:27 +03:00
|
|
|
void sync(void)
|
|
|
|
{
|
2023-09-27 00:35:36 +03:00
|
|
|
syscall(SYS_SYNC, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncsync(int should_block)
|
|
|
|
{
|
|
|
|
syscall(SYS_SYNC, should_block);
|
2023-09-11 01:26:27 +03:00
|
|
|
}
|
|
|
|
|
2023-10-25 21:45:04 +03:00
|
|
|
int unlink(const char* path)
|
|
|
|
{
|
|
|
|
return syscall(SYS_UNLINK, path);
|
|
|
|
}
|
|
|
|
|
2023-12-14 11:02:56 +02:00
|
|
|
int rmdir(const char* path)
|
|
|
|
{
|
|
|
|
return syscall(SYS_UNLINK, path);
|
|
|
|
}
|
|
|
|
|
2024-02-14 15:00:58 +02:00
|
|
|
char* optarg = nullptr;
|
|
|
|
int opterr = 1;
|
|
|
|
int optind = 1;
|
|
|
|
int optopt = 0;
|
|
|
|
|
|
|
|
int getopt(int argc, char* const argv[], const char* optstring)
|
|
|
|
{
|
|
|
|
if (optind >= argc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
static int idx = 1;
|
|
|
|
const char* current = argv[optind];
|
|
|
|
|
|
|
|
// if "--" is encountered, no more options are parsed
|
|
|
|
if (idx == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// if current is nullptr, does not start with '-' or is string "-", return -1
|
|
|
|
if (current == nullptr || current[0] != '-' || current[1] == '\0')
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// if current points to string "--" increment optind and return -1
|
|
|
|
if (current[1] == '-' && current[2] == '\0')
|
|
|
|
{
|
|
|
|
idx = -1;
|
|
|
|
optind++;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; optstring[i]; i++)
|
|
|
|
{
|
|
|
|
if (optstring[i] == ':')
|
|
|
|
continue;
|
|
|
|
if (current[idx] != optstring[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (optstring[i + 1] == ':')
|
|
|
|
{
|
|
|
|
if (current[idx + 1])
|
|
|
|
{
|
|
|
|
optarg = const_cast<char*>(current + idx + 1);
|
|
|
|
optind += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
optarg = const_cast<char*>(argv[optind + 1]);
|
|
|
|
optind += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = 1;
|
|
|
|
|
|
|
|
if (optind > argc)
|
|
|
|
{
|
|
|
|
if (opterr && optstring[0] != ':')
|
|
|
|
fprintf(stderr, "%s: option requires an argument -- %c\n", argv[0], optstring[i]);
|
|
|
|
optopt = optstring[i];
|
|
|
|
return optstring[0] == ':' ? ':' : '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
return optstring[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (current[++idx] == '\0')
|
|
|
|
{
|
|
|
|
idx = 1;
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return optstring[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opterr && optstring[0] != ':')
|
|
|
|
fprintf(stderr, "%s: illegal option -- %c\n", argv[0], current[idx]);
|
|
|
|
|
|
|
|
if (current[++idx] == '\0')
|
|
|
|
{
|
|
|
|
idx = 1;
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
2023-07-24 22:27:11 +03:00
|
|
|
pid_t getpid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_PID);
|
|
|
|
}
|
|
|
|
|
2023-06-11 22:27:53 +03:00
|
|
|
uid_t getuid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_UID);
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getgid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_GID);
|
|
|
|
}
|
|
|
|
|
|
|
|
uid_t geteuid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_EUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getegid(void)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_EGID);
|
|
|
|
}
|
|
|
|
|
2023-08-22 11:36:33 +03:00
|
|
|
pid_t getpgrp(void)
|
|
|
|
{
|
2023-08-22 14:53:12 +03:00
|
|
|
return getpgid(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t getpgid(pid_t pid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_GET_PGID, pid);
|
2023-08-22 11:36:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-11 22:15:35 +03:00
|
|
|
int seteuid(uid_t uid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_EUID, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setegid(gid_t gid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_EGID, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setuid(uid_t uid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_UID, uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setgid(gid_t gid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_GID, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setreuid(uid_t ruid, uid_t euid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_REUID, ruid, euid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setregid(gid_t rgid, gid_t egid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_REGID, rgid, egid);
|
|
|
|
}
|
2023-07-24 22:27:11 +03:00
|
|
|
|
2023-08-22 11:36:33 +03:00
|
|
|
pid_t setpgrp(void)
|
|
|
|
{
|
2023-08-22 14:53:12 +03:00
|
|
|
setpgid(0, 0);
|
|
|
|
return getpgrp();
|
|
|
|
}
|
|
|
|
|
|
|
|
int setpgid(pid_t pid, pid_t pgid)
|
|
|
|
{
|
|
|
|
return syscall(SYS_SET_PGID, pid, pgid);
|
2023-08-22 11:36:33 +03:00
|
|
|
}
|
|
|
|
|
2023-07-24 22:27:11 +03:00
|
|
|
int tcsetpgrp(int fildes, pid_t pgid_id)
|
|
|
|
{
|
|
|
|
return syscall(SYS_TCSETPGRP, fildes, pgid_id);
|
|
|
|
}
|
2024-05-23 15:05:22 +03:00
|
|
|
|
|
|
|
char* getlogin(void)
|
|
|
|
{
|
|
|
|
static char buffer[LOGIN_NAME_MAX];
|
|
|
|
auto* pw = getpwuid(geteuid());
|
|
|
|
if (pw == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
strncpy(buffer, pw->pw_name, LOGIN_NAME_MAX - 1);
|
|
|
|
buffer[LOGIN_NAME_MAX - 1] = '\0';
|
|
|
|
endpwent();
|
|
|
|
return buffer;
|
|
|
|
}
|