LibC: Add creat(), execvp() resolution, and exec*() environment inheritance.

This commit is contained in:
Andreas Kling 2019-03-27 01:39:13 +01:00
parent fb7c7829c2
commit 0c2face7b0
Notes: sideshowbarker 2024-07-19 14:56:08 +09:00
2 changed files with 29 additions and 4 deletions

View file

@ -9,8 +9,10 @@
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <Kernel/Syscall.h>
#include <AK/Vector.h>
#include <AK/AKString.h>
extern "C" {
@ -28,7 +30,7 @@ pid_t fork()
int execv(const char* path, char* const argv[])
{
return execve(path, argv, nullptr);
return execve(path, argv, environ);
}
int execve(const char* filename, char* const argv[], char* const envp[])
@ -39,8 +41,25 @@ int execve(const char* filename, char* const argv[], char* const envp[])
int execvp(const char* filename, char* const argv[])
{
// FIXME: This should do some sort of shell-like path resolution!
return execve(filename, argv, nullptr);
int rc = execve(filename, argv, nullptr);
if (rc < 0 && errno != ENOENT) {
printf("execvp failed on first with %s\n", strerror(errno));
return rc;
}
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);
rc = execve(candidate.characters(), argv, environ);
if (rc < 0 && errno != ENOENT) {
printf("execvp failed on attempt (%s) with %s\n", candidate.characters(), strerror(errno));
return rc;
}
}
errno = ENOENT;
return -1;
}
int execl(const char* filename, const char* arg0, ...)
@ -58,7 +77,7 @@ int execl(const char* filename, const char* arg0, ...)
}
va_end(ap);
args.append(nullptr);
return execve(filename, (char* const *)args.data(), nullptr);
return execve(filename, (char* const *)args.data(), environ);
}
uid_t getuid()
@ -125,6 +144,11 @@ pid_t getpgrp()
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int creat(const char* path, mode_t mode)
{
return open(path, O_CREAT, mode);
}
int open(const char* path, int options, ...)
{
va_list ap;

View file

@ -48,6 +48,7 @@ int setuid(uid_t);
int setgid(gid_t);
pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgid);
int creat(const char* path, mode_t);
int open(const char* path, int options, ...);
ssize_t read(int fd, void* buf, size_t count);
ssize_t write(int fd, const void* buf, size_t count);