2018-11-05 15:40:48 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
2018-10-22 11:57:25 +00:00
|
|
|
#include <Kernel/Syscall.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-11-02 19:41:58 +00:00
|
|
|
pid_t fork()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_fork);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int execve(const char* filename, const char** argv, const char** envp)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_execve, (dword)filename, (dword)argv, (dword)envp);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 19:41:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 11:57:25 +00:00
|
|
|
uid_t getuid()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
return Syscall::invoke(Syscall::SC_getuid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:00:37 +00:00
|
|
|
gid_t getgid()
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
return Syscall::invoke(Syscall::SC_getgid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 14:04:19 +00:00
|
|
|
uid_t geteuid()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
return Syscall::invoke(Syscall::SC_geteuid);
|
2018-11-05 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getegid()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
return Syscall::invoke(Syscall::SC_getegid);
|
2018-11-05 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:00:37 +00:00
|
|
|
pid_t getpid()
|
2018-10-22 11:57:25 +00:00
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
return Syscall::invoke(Syscall::SC_getpid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 12:33:06 +00:00
|
|
|
pid_t getppid()
|
|
|
|
{
|
|
|
|
return Syscall::invoke(Syscall::SC_getppid);
|
|
|
|
}
|
|
|
|
|
2018-11-02 11:56:51 +00:00
|
|
|
pid_t setsid()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(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
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_tcgetpgrp, (dword)fd);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 12:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tcsetpgrp(int fd, pid_t pgid)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_tcsetpgrp, (dword)fd, (dword)pgid);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int setpgid(pid_t pid, pid_t pgid)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_setpgid, (dword)pid, (dword)pgid);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t getpgid(pid_t pid)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_getpgid, (dword)pid);
|
2018-11-03 00:49:40 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-02 11:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t getpgrp()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(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
|
|
|
}
|
|
|
|
|
2018-11-05 15:40:48 +00:00
|
|
|
int open(const char* path, int options, ...)
|
2018-10-23 08:12:50 +00:00
|
|
|
{
|
2018-11-05 15:40:48 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, options);
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_open, (dword)path, (dword)options, (dword)ap);
|
2018-11-05 15:40:48 +00:00
|
|
|
va_end(ap);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t read(int fd, void* buf, size_t count)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_read, (dword)fd, (dword)buf, (dword)count);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:33:37 +00:00
|
|
|
ssize_t write(int fd, const void* buf, size_t count)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_write, (dword)fd, (dword)buf, (dword)count);
|
2018-10-30 14:33:37 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-30 21:03:02 +00:00
|
|
|
int ttyname_r(int fd, char* buffer, size_t size)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_ttyname_r, (dword)fd, (dword)buffer, (dword)size);
|
2018-10-30 21:03:02 +00: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 08:12:50 +00:00
|
|
|
int close(int fd)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(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
|
|
|
}
|
|
|
|
|
2018-10-26 23:24:22 +00:00
|
|
|
pid_t waitpid(pid_t waitee, int* wstatus, int options)
|
2018-10-23 22:20:34 +00:00
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_waitpid, waitee, (dword)wstatus);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-23 22:20:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:14:56 +00:00
|
|
|
int lstat(const char* path, struct stat* statbuf)
|
2018-10-24 11:19:36 +00:00
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_lstat, (dword)path, (dword)statbuf);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-24 11:19:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 09:14:56 +00:00
|
|
|
int stat(const char* path, struct stat* statbuf)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_stat, (dword)path, (dword)statbuf);
|
2018-10-31 09:14:56 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:24:11 +00:00
|
|
|
int chdir(const char* path)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_chdir, (dword)path);
|
2018-10-26 12:24:11 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-24 12:28:22 +00:00
|
|
|
char* getcwd(char* buffer, size_t size)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_getcwd, (dword)buffer, (dword)size);
|
2018-10-25 10:06:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, buffer, nullptr);
|
2018-10-24 12:28:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 14:59:57 +00:00
|
|
|
char* getwd(char* buf)
|
|
|
|
{
|
|
|
|
return getcwd(buf, PATH_MAX);
|
|
|
|
}
|
|
|
|
|
2018-10-25 11:53:49 +00:00
|
|
|
int sleep(unsigned seconds)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
return Syscall::invoke(Syscall::SC_sleep, (dword)seconds);
|
2018-10-25 11:53:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 07:54:29 +00:00
|
|
|
int gethostname(char* buffer, size_t size)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_gethostname, (dword)buffer, (dword)size);
|
2018-10-26 07:54:29 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-28 13:11:51 +00:00
|
|
|
ssize_t readlink(const char* path, char* buffer, size_t size)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_readlink, (dword)path, (dword)buffer, (dword)size);
|
2018-10-28 13:11:51 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:50:43 +00:00
|
|
|
off_t lseek(int fd, off_t offset, int whence)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_lseek, (dword)fd, (dword)offset, (dword)whence);
|
2018-10-31 16:50:43 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:40:48 +00:00
|
|
|
int link(const char*, const char*)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
int unlink(const char*)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2018-11-05 17:16:00 +00:00
|
|
|
int isatty(int fd)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_isatty, (dword)fd);
|
2018-11-05 17:16:00 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, 1, 0);
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:22 +00:00
|
|
|
int getdtablesize()
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_getdtablesize);
|
2018-11-05 18:01:22 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dup(int old_fd)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_dup, (dword)old_fd);
|
2018-11-05 18:01:22 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:22 +00:00
|
|
|
int dup2(int old_fd, int new_fd)
|
|
|
|
{
|
2018-11-06 12:23:22 +00:00
|
|
|
int rc = Syscall::invoke(Syscall::SC_dup2, (dword)old_fd, (dword)new_fd);
|
2018-11-05 18:01:22 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|