2018-11-05 15:40:48 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <assert.h>
|
2018-11-07 00:38:51 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
2018-11-16 12:11:21 +00:00
|
|
|
#include <sys/ioctl.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-12-21 02:02:06 +00:00
|
|
|
int rc = 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_execve, filename, argv, 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-12-21 02:02:06 +00:00
|
|
|
return 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-12-21 02:02:06 +00:00
|
|
|
return syscall(SC_getgid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 14:04:19 +00:00
|
|
|
uid_t geteuid()
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
return syscall(SC_geteuid);
|
2018-11-05 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gid_t getegid()
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
return 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-12-21 02:02:06 +00:00
|
|
|
return syscall(SC_getpid);
|
2018-10-22 11:57:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-11-16 12:11:21 +00:00
|
|
|
return ioctl(fd, TIOCGPGRP);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_open, path, options, 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-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
|
|
|
}
|
|
|
|
|
2018-10-30 14:33:37 +00:00
|
|
|
ssize_t write(int fd, const void* buf, size_t count)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-30 21:03:02 +00:00
|
|
|
int ttyname_r(int fd, char* buffer, size_t size)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_ttyname_r, fd, buffer, 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-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
|
|
|
}
|
|
|
|
|
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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_waitpid, waitee, wstatus, options);
|
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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_lstat, path, 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_stat, path, statbuf);
|
2018-10-31 09:14:56 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:20:53 +00:00
|
|
|
int fstat(int fd, struct stat *statbuf)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_fstat, fd, statbuf);
|
2018-11-10 23:20:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:24:11 +00:00
|
|
|
int chdir(const char* path)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_chdir, 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_getcwd, buffer, 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)
|
|
|
|
{
|
2018-11-11 14:36:40 +00:00
|
|
|
auto* p = getcwd(buf, PATH_MAX);
|
|
|
|
return p;
|
2018-11-06 14:59:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 11:53:49 +00:00
|
|
|
int sleep(unsigned seconds)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
return syscall(SC_sleep, 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-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);
|
|
|
|
}
|
|
|
|
|
2018-10-28 13:11:51 +00:00
|
|
|
ssize_t readlink(const char* path, char* buffer, size_t size)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_readlink, path, buffer, 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_lseek, fd, offset, 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_isatty, 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_getdtablesize);
|
2018-11-11 09:11:09 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2018-11-05 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dup(int old_fd)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_dup, 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-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_dup2, old_fd, new_fd);
|
2018-11-05 18:01:22 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-07 00:38:51 +00:00
|
|
|
int setgroups(size_t size, const 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-11-10 23:20:53 +00:00
|
|
|
int pipe(int pipefd[2])
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_pipe, pipefd);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int setgid(uid_t gid)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int access(const char* pathname, int mode)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
int rc = syscall(SC_access, pathname, mode);
|
2018-11-10 23:20:53 +00:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:11:08 +00:00
|
|
|
int mknod(const char* pathname, mode_t, dev_t)
|
|
|
|
{
|
|
|
|
(void) pathname;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2018-11-17 14:56:09 +00:00
|
|
|
long fpathconf(int fd, int name)
|
|
|
|
{
|
|
|
|
(void) fd;
|
|
|
|
(void) name;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
long pathconf(const char* path, int name)
|
|
|
|
{
|
|
|
|
(void) path;
|
|
|
|
(void) name;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _exit(int status)
|
|
|
|
{
|
2018-12-21 02:02:06 +00:00
|
|
|
syscall(SC_exit, status);
|
2018-11-17 14:56:09 +00:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:22 +00:00
|
|
|
}
|