setpgid.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. REQUIRE_PROMISE(proc);
  13. if (pid == 0)
  14. return sid().value();
  15. ScopedSpinLock lock(g_processes_lock);
  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. REQUIRE_PROMISE(proc);
  26. InterruptDisabler disabler;
  27. bool found_process_with_same_pgid_as_my_pid = false;
  28. Process::for_each_in_pgrp(pid().value(), [&](auto&) {
  29. found_process_with_same_pgid_as_my_pid = true;
  30. return IterationDecision::Break;
  31. });
  32. if (found_process_with_same_pgid_as_my_pid)
  33. return EPERM;
  34. // Create a new Session and a new ProcessGroup.
  35. m_pg = ProcessGroup::create(ProcessGroupID(pid().value()));
  36. m_tty = nullptr;
  37. ProtectedDataMutationScope scope { *this };
  38. m_sid = pid().value();
  39. return sid().value();
  40. }
  41. KResultOr<FlatPtr> Process::sys$getpgid(pid_t pid)
  42. {
  43. REQUIRE_PROMISE(proc);
  44. if (pid == 0)
  45. return pgid().value();
  46. ScopedSpinLock lock(g_processes_lock); // FIXME: Use a ProcessHandle
  47. auto process = Process::from_pid(pid);
  48. if (!process)
  49. return ESRCH;
  50. return process->pgid().value();
  51. }
  52. KResultOr<FlatPtr> Process::sys$getpgrp()
  53. {
  54. REQUIRE_PROMISE(stdio);
  55. return pgid().value();
  56. }
  57. SessionID Process::get_sid_from_pgid(ProcessGroupID pgid)
  58. {
  59. // FIXME: This xor sys$setsid() uses the wrong locking mechanism.
  60. ScopedSpinLock lock(g_processes_lock);
  61. SessionID sid { -1 };
  62. Process::for_each_in_pgrp(pgid, [&](auto& process) {
  63. sid = process.sid();
  64. return IterationDecision::Break;
  65. });
  66. return sid;
  67. }
  68. KResultOr<FlatPtr> Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
  69. {
  70. REQUIRE_PROMISE(proc);
  71. ScopedSpinLock lock(g_processes_lock); // FIXME: Use a ProcessHandle
  72. ProcessID pid = specified_pid ? ProcessID(specified_pid) : this->pid();
  73. if (specified_pgid < 0) {
  74. // The value of the pgid argument is less than 0, or is not a value supported by the implementation.
  75. return EINVAL;
  76. }
  77. auto process = Process::from_pid(pid);
  78. if (!process)
  79. return ESRCH;
  80. if (process != this && process->ppid() != this->pid()) {
  81. // The value of the pid argument does not match the process ID
  82. // of the calling process or of a child process of the calling process.
  83. return ESRCH;
  84. }
  85. if (process->is_session_leader()) {
  86. // The process indicated by the pid argument is a session leader.
  87. return EPERM;
  88. }
  89. if (process->ppid() == this->pid() && process->sid() != sid()) {
  90. // The value of the pid argument matches the process ID of a child
  91. // process of the calling process and the child process is not in
  92. // the same session as the calling process.
  93. return EPERM;
  94. }
  95. ProcessGroupID new_pgid = specified_pgid ? ProcessGroupID(specified_pgid) : process->pid().value();
  96. SessionID current_sid = sid();
  97. SessionID new_sid = get_sid_from_pgid(new_pgid);
  98. if (new_sid != -1 && current_sid != new_sid) {
  99. // Can't move a process between sessions.
  100. return EPERM;
  101. }
  102. if (new_sid == -1 && new_pgid != process->pid().value()) {
  103. // The value of the pgid argument is valid, but is not
  104. // the calling pid, and is not an existing process group.
  105. return EPERM;
  106. }
  107. // FIXME: There are more EPERM conditions to check for here..
  108. process->m_pg = ProcessGroup::find_or_create(new_pgid);
  109. if (!process->m_pg) {
  110. return ENOMEM;
  111. }
  112. return 0;
  113. }
  114. }