kill.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <Kernel/Process.h>
  27. namespace Kernel {
  28. KResult Process::do_kill(Process& process, int signal)
  29. {
  30. // FIXME: Allow sending SIGCONT to everyone in the process group.
  31. // FIXME: Should setuid processes have some special treatment here?
  32. if (!is_superuser() && m_euid != process.m_uid && m_uid != process.m_uid)
  33. return KResult(-EPERM);
  34. if (process.is_ring0() && signal == SIGKILL) {
  35. klog() << "attempted to send SIGKILL to ring 0 process " << process.name().characters() << "(" << process.pid() << ")";
  36. return KResult(-EPERM);
  37. }
  38. if (signal != 0)
  39. return process.send_signal(signal, this);
  40. return KSuccess;
  41. }
  42. KResult Process::do_killpg(pid_t pgrp, int signal)
  43. {
  44. InterruptDisabler disabler;
  45. ASSERT(pgrp >= 0);
  46. // Send the signal to all processes in the given group.
  47. if (pgrp == 0) {
  48. // Send the signal to our own pgrp.
  49. pgrp = pgid();
  50. }
  51. bool group_was_empty = true;
  52. bool any_succeeded = false;
  53. KResult error = KSuccess;
  54. Process::for_each_in_pgrp(pgrp, [&](auto& process) {
  55. group_was_empty = false;
  56. KResult res = do_kill(process, signal);
  57. if (res.is_success())
  58. any_succeeded = true;
  59. else
  60. error = res;
  61. return IterationDecision::Continue;
  62. });
  63. if (group_was_empty)
  64. return KResult(-ESRCH);
  65. if (any_succeeded)
  66. return KSuccess;
  67. return error;
  68. }
  69. KResult Process::do_killall(int signal)
  70. {
  71. InterruptDisabler disabler;
  72. bool any_succeeded = false;
  73. KResult error = KSuccess;
  74. // Send the signal to all processes we have access to for.
  75. ScopedSpinLock lock(g_processes_lock);
  76. for (auto& process : *g_processes) {
  77. KResult res = KSuccess;
  78. if (process.pid() == m_pid)
  79. res = do_killself(signal);
  80. else
  81. res = do_kill(process, signal);
  82. if (res.is_success())
  83. any_succeeded = true;
  84. else
  85. error = res;
  86. }
  87. if (any_succeeded)
  88. return KSuccess;
  89. return error;
  90. }
  91. KResult Process::do_killself(int signal)
  92. {
  93. if (signal == 0)
  94. return KSuccess;
  95. auto current_thread = Thread::current();
  96. if (!current_thread->should_ignore_signal(signal)) {
  97. current_thread->send_signal(signal, this);
  98. (void)current_thread->block<Thread::SemiPermanentBlocker>(nullptr, Thread::SemiPermanentBlocker::Reason::Signal);
  99. }
  100. return KSuccess;
  101. }
  102. int Process::sys$kill(pid_t pid, int signal)
  103. {
  104. if (pid == m_pid)
  105. REQUIRE_PROMISE(stdio);
  106. else
  107. REQUIRE_PROMISE(proc);
  108. if (signal < 0 || signal >= 32)
  109. return -EINVAL;
  110. if (pid < -1) {
  111. if (pid == NumericLimits<i32>::min())
  112. return -EINVAL;
  113. return do_killpg(-pid, signal);
  114. }
  115. if (pid == -1)
  116. return do_killall(signal);
  117. if (pid == m_pid) {
  118. return do_killself(signal);
  119. }
  120. ScopedSpinLock lock(g_processes_lock);
  121. auto peer = Process::from_pid(pid);
  122. if (!peer)
  123. return -ESRCH;
  124. return do_kill(*peer, signal);
  125. }
  126. int Process::sys$killpg(int pgrp, int signum)
  127. {
  128. REQUIRE_PROMISE(proc);
  129. if (signum < 1 || signum >= 32)
  130. return -EINVAL;
  131. if (pgrp < 0)
  132. return -EINVAL;
  133. return do_killpg(pgrp, signum);
  134. }
  135. }