1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

LibC: Add POSIX spec links for unistd APIs

This commit is contained in:
Brian Gianforcaro 2022-01-07 00:33:11 -08:00 committed by Brian Gianforcaro
parent c6c59653ce
commit 84f74647ac

View file

@ -38,6 +38,7 @@ static __thread int s_cached_tid = 0;
static int s_cached_pid = 0; static int s_cached_pid = 0;
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/lchown.html
int lchown(const char* pathname, uid_t uid, gid_t gid) int lchown(const char* pathname, uid_t uid, gid_t gid)
{ {
if (!pathname) { if (!pathname) {
@ -49,6 +50,7 @@ int lchown(const char* pathname, uid_t uid, gid_t gid)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chown.html
int chown(const char* pathname, uid_t uid, gid_t gid) int chown(const char* pathname, uid_t uid, gid_t gid)
{ {
if (!pathname) { if (!pathname) {
@ -60,6 +62,7 @@ int chown(const char* pathname, uid_t uid, gid_t gid)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchown.html
int fchown(int fd, uid_t uid, gid_t gid) int fchown(int fd, uid_t uid, gid_t gid)
{ {
int rc = syscall(SC_fchown, fd, uid, gid); int rc = syscall(SC_fchown, fd, uid, gid);
@ -77,6 +80,7 @@ int fchownat(int fd, const char* pathname, uid_t uid, gid_t gid, int flags)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html
pid_t fork() pid_t fork()
{ {
__pthread_fork_prepare(); __pthread_fork_prepare();
@ -92,16 +96,19 @@ pid_t fork()
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfork.html
pid_t vfork() pid_t vfork()
{ {
return fork(); return fork();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execv.html
int execv(const char* path, char* const argv[]) int execv(const char* path, char* const argv[])
{ {
return execve(path, argv, environ); return execve(path, argv, environ);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execve.html
int execve(const char* filename, char* const argv[], char* const envp[]) int execve(const char* filename, char* const argv[], char* const envp[])
{ {
size_t arg_count = 0; size_t arg_count = 0;
@ -156,6 +163,7 @@ int execvpe(const char* filename, char* const argv[], char* const envp[])
return -1; return -1;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html
int execvp(const char* filename, char* const argv[]) int execvp(const char* filename, char* const argv[])
{ {
int rc = execvpe(filename, argv, environ); int rc = execvpe(filename, argv, environ);
@ -165,6 +173,7 @@ int execvp(const char* filename, char* const argv[])
return rc; return rc;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execl.html
int execl(const char* filename, const char* arg0, ...) int execl(const char* filename, const char* arg0, ...)
{ {
Vector<const char*, 16> args; Vector<const char*, 16> args;
@ -183,6 +192,7 @@ int execl(const char* filename, const char* arg0, ...)
return execve(filename, const_cast<char* const*>(args.data()), environ); return execve(filename, const_cast<char* const*>(args.data()), environ);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execle.html
int execle(char const* filename, char const* arg0, ...) int execle(char const* filename, char const* arg0, ...)
{ {
Vector<char const*> args; Vector<char const*> args;
@ -203,6 +213,7 @@ int execle(char const* filename, char const* arg0, ...)
return execve(filename, argv, envp); return execve(filename, argv, envp);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/execlp.html
int execlp(const char* filename, const char* arg0, ...) int execlp(const char* filename, const char* arg0, ...)
{ {
Vector<const char*, 16> args; Vector<const char*, 16> args;
@ -221,26 +232,31 @@ int execlp(const char* filename, const char* arg0, ...)
return execvpe(filename, const_cast<char* const*>(args.data()), environ); return execvpe(filename, const_cast<char* const*>(args.data()), environ);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
uid_t geteuid() uid_t geteuid()
{ {
return syscall(SC_geteuid); return syscall(SC_geteuid);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getegid.html
gid_t getegid() gid_t getegid()
{ {
return syscall(SC_getegid); return syscall(SC_getegid);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
uid_t getuid() uid_t getuid()
{ {
return syscall(SC_getuid); return syscall(SC_getuid);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getgid.html
gid_t getgid() gid_t getgid()
{ {
return syscall(SC_getgid); return syscall(SC_getgid);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpid.html
pid_t getpid() pid_t getpid()
{ {
int cached_pid = s_cached_pid; int cached_pid = s_cached_pid;
@ -251,6 +267,7 @@ pid_t getpid()
return cached_pid; return cached_pid;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getppid.html
pid_t getppid() pid_t getppid()
{ {
return syscall(SC_getppid); return syscall(SC_getppid);
@ -266,12 +283,14 @@ int getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid)
return syscall(SC_getresgid, rgid, egid, sgid); return syscall(SC_getresgid, rgid, egid, sgid);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html
pid_t getsid(pid_t pid) pid_t getsid(pid_t pid)
{ {
int rc = syscall(SC_getsid, pid); int rc = syscall(SC_getsid, pid);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setsid.html
pid_t setsid() pid_t setsid()
{ {
int rc = syscall(SC_setsid); int rc = syscall(SC_setsid);
@ -287,47 +306,55 @@ pid_t tcgetpgrp(int fd)
return pgrp; return pgrp;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetpgrp.html
int tcsetpgrp(int fd, pid_t pgid) int tcsetpgrp(int fd, pid_t pgid)
{ {
return ioctl(fd, TIOCSPGRP, pgid); return ioctl(fd, TIOCSPGRP, pgid);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/setpgid.html
int setpgid(pid_t pid, pid_t pgid) int setpgid(pid_t pid, pid_t pgid)
{ {
int rc = syscall(SC_setpgid, pid, pgid); int rc = syscall(SC_setpgid, pid, pgid);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgid.html
pid_t getpgid(pid_t pid) pid_t getpgid(pid_t pid)
{ {
int rc = syscall(SC_getpgid, pid); int rc = syscall(SC_getpgid, pid);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgrp.html
pid_t getpgrp() pid_t getpgrp()
{ {
int rc = syscall(SC_getpgrp); int rc = syscall(SC_getpgrp);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
ssize_t read(int fd, void* buf, size_t count) ssize_t read(int fd, void* buf, size_t count)
{ {
int rc = syscall(SC_read, fd, buf, count); int rc = syscall(SC_read, fd, buf, count);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html
ssize_t pread(int fd, void* buf, size_t count, off_t offset) ssize_t pread(int fd, void* buf, size_t count, off_t offset)
{ {
int rc = syscall(SC_pread, fd, buf, count, &offset); int rc = syscall(SC_pread, fd, buf, count, &offset);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
ssize_t write(int fd, const void* buf, size_t count) ssize_t write(int fd, const void* buf, size_t count)
{ {
int rc = syscall(SC_write, fd, buf, count); int rc = syscall(SC_write, fd, buf, count);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html
ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset)
{ {
// FIXME: This is not thread safe and should be implemented in the kernel instead. // FIXME: This is not thread safe and should be implemented in the kernel instead.
@ -338,6 +365,7 @@ ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset)
return nwritten; return nwritten;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname_r.html
int ttyname_r(int fd, char* buffer, size_t size) int ttyname_r(int fd, char* buffer, size_t size)
{ {
int rc = syscall(SC_ttyname, fd, buffer, size); int rc = syscall(SC_ttyname, fd, buffer, size);
@ -345,6 +373,7 @@ int ttyname_r(int fd, char* buffer, size_t size)
} }
static char ttyname_buf[32]; static char ttyname_buf[32];
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ttyname.html
char* ttyname(int fd) char* ttyname(int fd)
{ {
if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0) if (ttyname_r(fd, ttyname_buf, sizeof(ttyname_buf)) < 0)
@ -352,12 +381,14 @@ char* ttyname(int fd)
return ttyname_buf; return ttyname_buf;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
int close(int fd) int close(int fd)
{ {
int rc = syscall(SC_close, fd); int rc = syscall(SC_close, fd);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chdir.html
int chdir(const char* path) int chdir(const char* path)
{ {
if (!path) { if (!path) {
@ -368,12 +399,14 @@ int chdir(const char* path)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchdir.html
int fchdir(int fd) int fchdir(int fd)
{ {
int rc = syscall(SC_fchdir, fd); int rc = syscall(SC_fchdir, fd);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html
char* getcwd(char* buffer, size_t size) char* getcwd(char* buffer, size_t size)
{ {
if (buffer && size == 0) { if (buffer && size == 0) {
@ -438,6 +471,7 @@ char* getcwd(char* buffer, size_t size)
return buffer; return buffer;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getwd.html
char* getwd(char* buf) char* getwd(char* buf)
{ {
if (buf == nullptr) { if (buf == nullptr) {
@ -452,6 +486,7 @@ char* getwd(char* buf)
return p; return p;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html
unsigned int sleep(unsigned int seconds) unsigned int sleep(unsigned int seconds)
{ {
struct timespec ts = { seconds, 0 }; struct timespec ts = { seconds, 0 };
@ -460,6 +495,7 @@ unsigned int sleep(unsigned int seconds)
return 0; return 0;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/usleep.html
int usleep(useconds_t usec) int usleep(useconds_t usec)
{ {
struct timespec ts = { (long)(usec / 1000000), (long)(usec % 1000000) * 1000 }; struct timespec ts = { (long)(usec / 1000000), (long)(usec % 1000000) * 1000 };
@ -478,6 +514,7 @@ int sethostname(const char* hostname, ssize_t size)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html
ssize_t readlink(const char* path, char* buffer, size_t size) ssize_t readlink(const char* path, char* buffer, size_t size)
{ {
Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } }; Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };
@ -486,12 +523,13 @@ ssize_t readlink(const char* path, char* buffer, size_t size)
__RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1); __RETURN_WITH_ERRNO(rc, min((size_t)rc, size), -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html
off_t lseek(int fd, off_t offset, int whence) off_t lseek(int fd, off_t offset, int whence)
{ {
int rc = syscall(SC_lseek, fd, &offset, whence); int rc = syscall(SC_lseek, fd, &offset, whence);
__RETURN_WITH_ERRNO(rc, offset, -1); __RETURN_WITH_ERRNO(rc, offset, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html
int link(const char* old_path, const char* new_path) int link(const char* old_path, const char* new_path)
{ {
if (!old_path || !new_path) { if (!old_path || !new_path) {
@ -503,12 +541,14 @@ int link(const char* old_path, const char* new_path)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html
int unlink(const char* pathname) int unlink(const char* pathname)
{ {
int rc = syscall(SC_unlink, pathname, strlen(pathname)); int rc = syscall(SC_unlink, pathname, strlen(pathname));
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html
int symlink(const char* target, const char* linkpath) int symlink(const char* target, const char* linkpath)
{ {
if (!target || !linkpath) { if (!target || !linkpath) {
@ -520,6 +560,7 @@ int symlink(const char* target, const char* linkpath)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/rmdir.html
int rmdir(const char* pathname) int rmdir(const char* pathname)
{ {
if (!pathname) { if (!pathname) {
@ -530,16 +571,19 @@ int rmdir(const char* pathname)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/isatty.html
int isatty(int fd) int isatty(int fd)
{ {
return fcntl(fd, F_ISTTY); return fcntl(fd, F_ISTTY);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html
int dup(int old_fd) int dup(int old_fd)
{ {
return fcntl(old_fd, F_DUPFD, 0); return fcntl(old_fd, F_DUPFD, 0);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html
int dup2(int old_fd, int new_fd) int dup2(int old_fd, int new_fd)
{ {
int rc = syscall(SC_dup2, old_fd, new_fd); int rc = syscall(SC_dup2, old_fd, new_fd);
@ -558,11 +602,13 @@ int getgroups(int size, gid_t list[])
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html
int pipe(int pipefd[2]) int pipe(int pipefd[2])
{ {
return pipe2(pipefd, 0); return pipe2(pipefd, 0);
} }
//
int pipe2(int pipefd[2], int flags) int pipe2(int pipefd[2], int flags)
{ {
int rc = syscall(SC_pipe, pipefd, flags); int rc = syscall(SC_pipe, pipefd, flags);
@ -616,6 +662,7 @@ int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html
int access(const char* pathname, int mode) int access(const char* pathname, int mode)
{ {
if (!pathname) { if (!pathname) {
@ -637,6 +684,7 @@ int mknod(const char* pathname, mode_t mode, dev_t dev)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fpathconf.html
long fpathconf([[maybe_unused]] int fd, int name) long fpathconf([[maybe_unused]] int fd, int name)
{ {
switch (name) { switch (name) {
@ -653,6 +701,7 @@ long fpathconf([[maybe_unused]] int fd, int name)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pathconf.html
long pathconf([[maybe_unused]] const char* path, int name) long pathconf([[maybe_unused]] const char* path, int name)
{ {
switch (name) { switch (name) {
@ -669,12 +718,14 @@ long pathconf([[maybe_unused]] const char* path, int name)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/_exit.html
void _exit(int status) void _exit(int status)
{ {
syscall(SC_exit, status); syscall(SC_exit, status);
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sync.html
void sync() void sync()
{ {
syscall(SC_sync); syscall(SC_sync);
@ -693,12 +744,14 @@ char* getlogin()
return const_cast<char*>(getlogin_buffer.characters()); return const_cast<char*>(getlogin_buffer.characters());
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
int ftruncate(int fd, off_t length) int ftruncate(int fd, off_t length)
{ {
int rc = syscall(SC_ftruncate, fd, &length); int rc = syscall(SC_ftruncate, fd, &length);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html
int truncate(const char* path, off_t length) int truncate(const char* path, off_t length)
{ {
int fd = open(path, O_RDWR | O_CREAT, 0666); int fd = open(path, O_RDWR | O_CREAT, 0666);
@ -728,6 +781,7 @@ int sysbeep()
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html
int fsync(int fd) int fsync(int fd)
{ {
int rc = syscall(SC_fsync, fd); int rc = syscall(SC_fsync, fd);
@ -794,28 +848,33 @@ int unveil(const char* path, const char* permissions)
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpass.html
char* getpass(const char* prompt) char* getpass(const char* prompt)
{ {
dbgln("FIXME: getpass('{}')", prompt); dbgln("FIXME: getpass('{}')", prompt);
TODO(); TODO();
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
long sysconf(int name) long sysconf(int name)
{ {
int rc = syscall(SC_sysconf, name); int rc = syscall(SC_sysconf, name);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpagesize.html
int getpagesize() int getpagesize()
{ {
return PAGE_SIZE; return PAGE_SIZE;
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html
int pause() int pause()
{ {
return select(0, nullptr, nullptr, nullptr, nullptr); return select(0, nullptr, nullptr, nullptr, nullptr);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chroot.html
int chroot(const char* path) int chroot(const char* path)
{ {
dbgln("FIXME: chroot(\"{}\")", path); dbgln("FIXME: chroot(\"{}\")", path);