Compare commits

..

4 Commits

Author SHA1 Message Date
3a45f4af53
su: Change the current directory to the user's home on login
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-11 22:15:46 +02:00
2a967f4b8b
kernel+libc: Add chdir() 2023-04-11 22:15:21 +02:00
2d30935fdb
kernel: Give each thread a working directory 2023-04-11 22:14:57 +02:00
dfce93c18f
ls: List the current directory by default instead of the root directory 2023-04-11 22:13:54 +02:00
18 changed files with 139 additions and 19 deletions

View File

@ -12,7 +12,7 @@ int main(int argc, char** argv)
bool show_almost_all { false }; bool show_almost_all { false };
os::ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(pathname, "directory"_sv, "/"_sv); parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv); parser.add_switch_argument(show_all, 'a', "all"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv); parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
parser.parse(argc, argv); parser.parse(argc, argv);

View File

@ -89,5 +89,7 @@ int main(int argc, char** argv)
setgid(entry->pw_gid); setgid(entry->pw_gid);
setuid(entry->pw_uid); setuid(entry->pw_uid);
chdir(entry->pw_dir);
execl(entry->pw_shell, entry->pw_shell, NULL); execl(entry->pw_shell, entry->pw_shell, NULL);
} }

View File

@ -35,6 +35,7 @@ set(SOURCES
src/sys/waitpid.cpp src/sys/waitpid.cpp
src/sys/getdents.cpp src/sys/getdents.cpp
src/sys/stat.cpp src/sys/stat.cpp
src/sys/chdir.cpp
src/fs/VFS.cpp src/fs/VFS.cpp
src/fs/tmpfs/FileSystem.cpp src/fs/tmpfs/FileSystem.cpp
src/fs/devices/DeviceRegistry.cpp src/fs/devices/DeviceRegistry.cpp

View File

@ -13,13 +13,15 @@ namespace VFS
return *root_fs->root_inode(); return *root_fs->root_inode();
} }
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth) Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{ {
auto parser = TRY(PathParser::create(path)); auto parser = TRY(PathParser::create(path));
SharedPtr<Inode> current_inode = root_fs->root_inode(); SharedPtr<Inode> current_inode;
// FIXME: Properly handle relative paths. if (parser.is_absolute() || !working_directory) current_inode = root_fs->root_inode();
else
current_inode = working_directory;
const char* section; const char* section;
while (parser.next().try_set_value(section)) while (parser.next().try_set_value(section))
@ -31,12 +33,12 @@ namespace VFS
return current_inode; return current_inode;
} }
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth) Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{ {
auto parser = TRY(PathParser::create(path)); auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname()); auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth)); auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
if (!can_write(parent_inode, auth)) return err(EACCES); if (!can_write(parent_inode, auth)) return err(EACCES);
@ -47,12 +49,12 @@ namespace VFS
return parent_inode->create_subdirectory(child_name.chars()); return parent_inode->create_subdirectory(child_name.chars());
} }
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth) Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
{ {
auto parser = TRY(PathParser::create(path)); auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname()); auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth)); auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
if (!can_write(parent_inode, auth)) return err(EACCES); if (!can_write(parent_inode, auth)) return err(EACCES);

View File

@ -175,10 +175,14 @@ namespace VFS
extern SharedPtr<FileSystem> root_fs; extern SharedPtr<FileSystem> root_fs;
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth); Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth); Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth,
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth); SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
bool can_execute(SharedPtr<Inode> inode, Credentials auth); bool can_execute(SharedPtr<Inode> inode, Credentials auth);
bool can_read(SharedPtr<Inode> inode, Credentials auth); bool can_read(SharedPtr<Inode> inode, Credentials auth);

38
kernel/src/sys/chdir.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <luna/PathParser.h>
Result<u64> sys_chdir(Registers*, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
Thread* current = Scheduler::current();
if (PathParser::is_absolute(path.view()))
{
SharedPtr<VFS::Inode> inode = TRY(VFS::resolve_path(path.chars(), current->auth));
if (inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
current->current_directory = inode;
current->current_directory_path = move(path);
return 0;
}
else
{
SharedPtr<VFS::Inode> inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
if (inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
auto old_wdir = current->current_directory_path.view();
String new_path = TRY(PathParser::join(old_wdir.is_empty() ? "/"_sv : old_wdir, path.view()));
current->current_directory = inode;
current->current_directory_path = move(new_path);
return 0;
}
}

View File

@ -104,6 +104,8 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
memcpy(&current->regs, regs, sizeof(*regs)); memcpy(&current->regs, regs, sizeof(*regs));
auto current_directory_path = TRY(current->current_directory_path.clone());
auto image = TRY(ThreadImage::clone_from_thread(current)); auto image = TRY(ThreadImage::clone_from_thread(current));
auto thread = TRY(new_thread()); auto thread = TRY(new_thread());
@ -114,6 +116,8 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
thread->fp_data.save(); thread->fp_data.save();
thread->name = current->name; thread->name = current->name;
thread->auth = current->auth; thread->auth = current->auth;
thread->current_directory = current->current_directory;
thread->current_directory_path = move(current_directory_path);
for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; } for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; }

View File

@ -99,7 +99,7 @@ Result<u64> sys_chmod(Registers*, SyscallArgs args)
Credentials& auth = Scheduler::current()->auth; Credentials& auth = Scheduler::current()->auth;
auto inode = TRY(VFS::resolve_path(path.chars(), auth)); auto inode = TRY(VFS::resolve_path(path.chars(), auth, Scheduler::current()->current_directory));
if (auth.euid != 0 && auth.euid != inode->uid()) return err(EPERM); if (auth.euid != 0 && auth.euid != inode->uid()) return err(EPERM);

View File

@ -13,7 +13,7 @@ Result<u64> sys_mkdir(Registers*, SyscallArgs args)
kinfoln("mkdir: attempting to create %s", path.chars()); kinfoln("mkdir: attempting to create %s", path.chars());
auto inode = TRY(VFS::create_directory(path.chars(), current->auth)); auto inode = TRY(VFS::create_directory(path.chars(), current->auth, current->current_directory));
inode->chmod(mode); inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid); inode->chown(current->auth.euid, current->auth.egid);

View File

@ -24,7 +24,7 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
auto dirname = TRY(parser.dirname()); auto dirname = TRY(parser.dirname());
auto basename = TRY(parser.basename()); auto basename = TRY(parser.basename());
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth)); auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
if (!VFS::can_write(parent, current->auth)) return err(EACCES); if (!VFS::can_write(parent, current->auth)) return err(EACCES);
auto inode = TRY(parent->fs().create_device_inode(maj, min)); auto inode = TRY(parent->fs().create_device_inode(maj, min));

View File

@ -25,12 +25,13 @@ Result<u64> sys_open(Registers*, SyscallArgs args)
if ((flags & O_RDWR) == 0) { return err(EINVAL); } if ((flags & O_RDWR) == 0) { return err(EINVAL); }
int error; int error;
bool ok = VFS::resolve_path(path.chars(), current->auth).try_set_value_or_error(inode, error); bool ok =
VFS::resolve_path(path.chars(), current->auth, current->current_directory).try_set_value_or_error(inode, error);
if (!ok) if (!ok)
{ {
if (error == ENOENT && (flags & O_CREAT)) if (error == ENOENT && (flags & O_CREAT))
{ {
inode = TRY(VFS::create_file(path.chars(), current->auth)); inode = TRY(VFS::create_file(path.chars(), current->auth, current->current_directory));
inode->chmod(mode); inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid); inode->chown(current->auth.euid, current->auth.egid);
} }

View File

@ -42,7 +42,7 @@ Result<u64> sys_stat(Registers*, SyscallArgs args)
Thread* current = Scheduler::current(); Thread* current = Scheduler::current();
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth)); auto inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
return do_stat(inode, st); return do_stat(inode, st);
} }

View File

@ -8,6 +8,7 @@
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/Stack.h> #include <luna/Stack.h>
#include <luna/StaticString.h> #include <luna/StaticString.h>
#include <luna/String.h>
#ifdef ARCH_X86_64 #ifdef ARCH_X86_64
#include "arch/x86_64/CPU.h" #include "arch/x86_64/CPU.h"
@ -83,6 +84,9 @@ struct Thread : public LinkedListNode<Thread>
StaticString<128> name; StaticString<128> name;
String current_directory_path = {};
SharedPtr<VFS::Inode> current_directory = {};
PageDirectory* directory; PageDirectory* directory;
bool is_idle() bool is_idle()

View File

@ -97,6 +97,9 @@ extern "C"
/* Duplicate a file descriptor. */ /* Duplicate a file descriptor. */
int dup(int fd); int dup(int fd);
/* Change the current working directory. */
int chdir(const char* path);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -229,4 +229,10 @@ extern "C"
{ {
return fcntl(fd, F_DUPFD, 0); return fcntl(fd, F_DUPFD, 0);
} }
int chdir(const char* path)
{
long rc = syscall(SYS_chdir, path);
__errno_return(rc, int);
}
} }

View File

@ -11,11 +11,20 @@ class PathParser
PathParser(PathParser&& other); PathParser(PathParser&& other);
PathParser(const PathParser&) = delete; PathParser(const PathParser&) = delete;
static Result<String> join(StringView path1, StringView path2);
static Result<String> realpath(StringView path);
static bool is_absolute(StringView path)
{
return *path.chars() == '/';
}
~PathParser(); ~PathParser();
bool is_absolute() const bool is_absolute() const
{ {
return *m_original == '/'; return is_absolute(StringView { m_original });
} }
Result<String> basename(); Result<String> basename();

View File

@ -4,7 +4,7 @@
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \ _e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
_e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \ _e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(chmod) _e(chown) \ _e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(chmod) _e(chown) \
_e(ioctl) _e(stat) _e(fstat) _e(ioctl) _e(stat) _e(fstat) _e(chdir)
enum Syscalls enum Syscalls
{ {

View File

@ -1,6 +1,7 @@
#include <luna/CPath.h> #include <luna/CPath.h>
#include <luna/PathParser.h> #include <luna/PathParser.h>
#include <luna/ScopeGuard.h> #include <luna/ScopeGuard.h>
#include <luna/StringBuilder.h>
Result<PathParser> PathParser::create(const char* path) Result<PathParser> PathParser::create(const char* path)
{ {
@ -59,3 +60,48 @@ Result<String> PathParser::dirname()
// We must copy this as we cannot rely on the original string. // We must copy this as we cannot rely on the original string.
return String::from_cstring(result); return String::from_cstring(result);
} }
Result<String> PathParser::join(StringView path1, StringView path2)
{
StringBuilder sb;
TRY(sb.add(path1));
if (path1[path1.length() - 1] != '/') TRY(sb.add('/'));
TRY(sb.add(path2));
String result = TRY(sb.string());
return realpath(result.view());
}
Result<String> PathParser::realpath(StringView path)
{
if (!is_absolute(path)) return String {};
Vector<StringView> parts;
PathParser parser = TRY(PathParser::create(path.chars()));
const char* part;
while (parser.next().try_set_value(part))
{
StringView view = part;
if (view == ".") continue;
if (view == "..")
{
parts.try_pop();
continue;
}
TRY(parts.try_append(view));
}
StringBuilder sb;
for (const auto& section : parts)
{
TRY(sb.add('/'));
TRY(sb.add(section));
}
return sb.string();
}