hostname.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. extern String* g_hostname;
  9. extern Mutex* g_hostname_lock;
  10. KResultOr<FlatPtr> Process::sys$gethostname(Userspace<char*> buffer, size_t size)
  11. {
  12. REQUIRE_PROMISE(stdio);
  13. if (size > NumericLimits<ssize_t>::max())
  14. return EINVAL;
  15. Locker locker(*g_hostname_lock, Mutex::Mode::Shared);
  16. if (size < (g_hostname->length() + 1))
  17. return ENAMETOOLONG;
  18. if (!copy_to_user(buffer, g_hostname->characters(), g_hostname->length() + 1))
  19. return EFAULT;
  20. return 0;
  21. }
  22. KResultOr<FlatPtr> Process::sys$sethostname(Userspace<const char*> hostname, size_t length)
  23. {
  24. REQUIRE_NO_PROMISES;
  25. if (!is_superuser())
  26. return EPERM;
  27. Locker locker(*g_hostname_lock, Mutex::Mode::Exclusive);
  28. if (length > 64)
  29. return ENAMETOOLONG;
  30. auto copied_hostname = copy_string_from_user(hostname, length);
  31. if (copied_hostname.is_null())
  32. return EFAULT;
  33. *g_hostname = move(copied_hostname);
  34. return 0;
  35. }
  36. }