setpgid.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Arch/x86/InterruptDisabler.h>
  7. #include <Kernel/Process.h>
  8. #include <Kernel/TTY/TTY.h>
  9. namespace Kernel {
  10. KResultOr<FlatPtr> Process::sys$getsid(pid_t pid)
  11. {
  12. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  13. REQUIRE_PROMISE(proc);
  14. if (pid == 0)
  15. return sid().value();
  16. auto process = Process::from_pid(pid);
  17. if (!process)
  18. return ESRCH;
  19. if (sid() != process->sid())
  20. return EPERM;
  21. return process->sid().value();
  22. }
  23. KResultOr<FlatPtr> Process::sys$setsid()
  24. {
  25. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  26. REQUIRE_PROMISE(proc);
  27. InterruptDisabler disabler;
  28. bool found_process_with_same_pgid_as_my_pid = false;
  29. Process::for_each_in_pgrp(pid().value(), [&](auto&) {
  30. found_process_with_same_pgid_as_my_pid = true;
  31. return IterationDecision::Break;
  32. });
  33. if (found_process_with_same_pgid_as_my_pid)
  34. return EPERM;
  35. // Create a new Session and a new ProcessGroup.
  36. auto new_pg = ProcessGroup::try_create(ProcessGroupID(pid().value()));
  37. if (!new_pg)
  38. return ENOMEM;
  39. m_pg = move(new_pg);
  40. m_tty = nullptr;
  41. ProtectedDataMutationScope scope { *this };
  42. m_protected_values.sid = pid().value();
  43. return sid().value();
  44. }
  45. KResultOr<FlatPtr> Process::sys$getpgid(pid_t pid)
  46. {
  47. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  48. REQUIRE_PROMISE(proc);
  49. if (pid == 0)
  50. return pgid().value();
  51. auto process = Process::from_pid(pid);
  52. if (!process)
  53. return ESRCH;
  54. return process->pgid().value();
  55. }
  56. KResultOr<FlatPtr> Process::sys$getpgrp()
  57. {
  58. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  59. REQUIRE_PROMISE(stdio);
  60. return pgid().value();
  61. }
  62. SessionID Process::get_sid_from_pgid(ProcessGroupID pgid)
  63. {
  64. // FIXME: This xor sys$setsid() uses the wrong locking mechanism.
  65. SessionID sid { -1 };
  66. Process::for_each_in_pgrp(pgid, [&](auto& process) {
  67. sid = process.sid();
  68. return IterationDecision::Break;
  69. });
  70. return sid;
  71. }
  72. KResultOr<FlatPtr> Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
  73. {
  74. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  75. REQUIRE_PROMISE(proc);
  76. ProcessID pid = specified_pid ? ProcessID(specified_pid) : this->pid();
  77. if (specified_pgid < 0) {
  78. // The value of the pgid argument is less than 0, or is not a value supported by the implementation.
  79. return EINVAL;
  80. }
  81. auto process = Process::from_pid(pid);
  82. if (!process)
  83. return ESRCH;
  84. if (process != this && process->ppid() != this->pid()) {
  85. // The value of the pid argument does not match the process ID
  86. // of the calling process or of a child process of the calling process.
  87. return ESRCH;
  88. }
  89. if (process->is_session_leader()) {
  90. // The process indicated by the pid argument is a session leader.
  91. return EPERM;
  92. }
  93. if (process->ppid() == this->pid() && process->sid() != sid()) {
  94. // The value of the pid argument matches the process ID of a child
  95. // process of the calling process and the child process is not in
  96. // the same session as the calling process.
  97. return EPERM;
  98. }
  99. ProcessGroupID new_pgid = specified_pgid ? ProcessGroupID(specified_pgid) : process->pid().value();
  100. SessionID current_sid = sid();
  101. SessionID new_sid = get_sid_from_pgid(new_pgid);
  102. if (new_sid != -1 && current_sid != new_sid) {
  103. // Can't move a process between sessions.
  104. return EPERM;
  105. }
  106. if (new_sid == -1 && new_pgid != process->pid().value()) {
  107. // The value of the pgid argument is valid, but is not
  108. // the calling pid, and is not an existing process group.
  109. return EPERM;
  110. }
  111. // FIXME: There are more EPERM conditions to check for here..
  112. auto process_group = ProcessGroup::try_find_or_create(new_pgid);
  113. if (!process_group)
  114. return ENOMEM;
  115. process->m_pg = move(process_group);
  116. return 0;
  117. }
  118. }