process.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Types.h>
  27. #include <Kernel/Process.h>
  28. namespace Kernel {
  29. KResultOr<pid_t> Process::sys$getpid()
  30. {
  31. REQUIRE_PROMISE(stdio);
  32. return pid().value();
  33. }
  34. KResultOr<pid_t> Process::sys$getppid()
  35. {
  36. REQUIRE_PROMISE(stdio);
  37. return m_ppid.value();
  38. }
  39. KResultOr<int> Process::sys$get_process_name(Userspace<char*> buffer, size_t buffer_size)
  40. {
  41. REQUIRE_PROMISE(stdio);
  42. if (m_name.length() + 1 > buffer_size)
  43. return ENAMETOOLONG;
  44. if (!copy_to_user(buffer, m_name.characters(), m_name.length() + 1))
  45. return EFAULT;
  46. return 0;
  47. }
  48. KResultOr<int> Process::sys$set_process_name(Userspace<const char*> user_name, size_t user_name_length)
  49. {
  50. REQUIRE_PROMISE(proc);
  51. if (user_name_length > 256)
  52. return ENAMETOOLONG;
  53. auto name = copy_string_from_user(user_name, user_name_length);
  54. if (name.is_null())
  55. return EFAULT;
  56. // Empty and whitespace-only names only exist to confuse users.
  57. if (name.is_whitespace())
  58. return EINVAL;
  59. m_name = move(name);
  60. return 0;
  61. }
  62. KResultOr<int> Process::sys$set_coredump_metadata(Userspace<const Syscall::SC_set_coredump_metadata_params*> user_params)
  63. {
  64. Syscall::SC_set_coredump_metadata_params params;
  65. if (!copy_from_user(&params, user_params))
  66. return EFAULT;
  67. if (params.key.length == 0 || params.key.length > 16 * KiB)
  68. return EINVAL;
  69. if (params.value.length > 16 * KiB)
  70. return EINVAL;
  71. auto copied_key = copy_string_from_user(params.key.characters, params.key.length);
  72. if (copied_key.is_null())
  73. return EFAULT;
  74. auto copied_value = copy_string_from_user(params.value.characters, params.value.length);
  75. if (copied_value.is_null())
  76. return EFAULT;
  77. if (!m_coredump_metadata.contains(copied_key) && m_coredump_metadata.size() >= 16)
  78. return EFAULT;
  79. m_coredump_metadata.set(move(copied_key), move(copied_value));
  80. return 0;
  81. }
  82. }