chroot.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <Kernel/FileSystem/Custody.h>
  8. #include <Kernel/FileSystem/VirtualFileSystem.h>
  9. #include <Kernel/Process.h>
  10. namespace Kernel {
  11. KResultOr<FlatPtr> Process::sys$chroot(Userspace<const char*> user_path, size_t path_length, int mount_flags)
  12. {
  13. if (!is_superuser())
  14. return EPERM;
  15. REQUIRE_PROMISE(chroot);
  16. auto path = get_syscall_path_argument(user_path, path_length);
  17. if (path.is_error())
  18. return path.error();
  19. auto directory_or_error = VFS::the().open_directory(path.value()->view(), current_directory());
  20. if (directory_or_error.is_error())
  21. return directory_or_error.error();
  22. auto directory = directory_or_error.value();
  23. m_root_directory_relative_to_global_root = directory;
  24. int chroot_mount_flags = mount_flags == -1 ? directory->mount_flags() : mount_flags;
  25. auto custody_or_error = Custody::try_create(nullptr, "", directory->inode(), chroot_mount_flags);
  26. if (custody_or_error.is_error())
  27. return custody_or_error.error();
  28. set_root_directory(custody_or_error.release_value());
  29. return 0;
  30. }
  31. }