hostname.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Process.h>
  7. namespace Kernel {
  8. KResultOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
  9. {
  10. VERIFY_NO_PROCESS_BIG_LOCK(this)
  11. REQUIRE_PROMISE(stdio);
  12. if (size > NumericLimits<ssize_t>::max())
  13. return EINVAL;
  14. return hostname().with_shared([&](const auto& name) -> KResultOr<FlatPtr> {
  15. if (size < (name.length() + 1))
  16. return ENAMETOOLONG;
  17. if (!copy_to_user(buffer, name.characters(), name.length() + 1))
  18. return EFAULT;
  19. return 0;
  20. });
  21. }
  22. KResultOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> buffer, size_t length)
  23. {
  24. VERIFY_NO_PROCESS_BIG_LOCK(this)
  25. REQUIRE_NO_PROMISES;
  26. if (!is_superuser())
  27. return EPERM;
  28. if (length > 64)
  29. return ENAMETOOLONG;
  30. return hostname().with_exclusive([&](auto& name) -> KResultOr<FlatPtr> {
  31. auto name_or_error = try_copy_kstring_from_user(buffer, length);
  32. if (name_or_error.is_error())
  33. return name_or_error.error();
  34. // FIXME: Use KString instead of String here.
  35. name = name_or_error.value()->view();
  36. return 0;
  37. });
  38. }
  39. }