| 1 |
#ifndef RBIMPL_INTERN_SIGNAL_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_SIGNAL_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Signal handling APIs. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/attr/nonnull.h" |
| 24 |
#include "ruby/internal/attr/pure.h" |
| 25 |
#include "ruby/internal/dllexport.h" |
| 26 |
#include "ruby/internal/value.h" |
| 27 |
|
| 28 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 29 |
|
| 30 |
/* signal.c */ |
| 31 |
|
| 32 |
RBIMPL_ATTR_NONNULL(()) |
| 33 |
/** |
| 34 |
* Sends a signal ("kills") to processes. |
| 35 |
* |
| 36 |
* The first argument is the signal, either in: |
| 37 |
* |
| 38 |
* - Numerical representation (e.g. `9`), or |
| 39 |
* - Textual representation of canonical (e.g. `:SIGKILL`) name or |
| 40 |
* abbreviated (e.g. `:KILL`) name, either in ::rb_cSymbol or ::rb_cString. |
| 41 |
* |
| 42 |
* All the remaining arguments are numerical representations of process IDs. |
| 43 |
* This function iterates over them to send the specified signal. |
| 44 |
* |
| 45 |
* You can specify both negative PIDs and negative signo to this function: |
| 46 |
* |
| 47 |
* ``` |
| 48 |
* sig \ pid | >= 1 | == 0 | == -1 | <= -2 |
| 49 |
* ===========+======+======+=======+======= |
| 50 |
* > 0 | #1 | #2 | #3 | #4 |
| 51 |
* == 0 | #5 | #6 | #7 | #8 |
| 52 |
* < 0 | #9 | #10 | #11 |
| 53 |
* ``` |
| 54 |
* |
| 55 |
* - Case #1: When signo and PID are both positive, this function sends the |
| 56 |
* specified signal to the specified process (intuitive). |
| 57 |
* |
| 58 |
* - Case #2: When signo is positive and PID is zero, this function sends |
| 59 |
* that signal to the current process group. |
| 60 |
* |
| 61 |
* - Case #3: When signo is positive and PID is -1, this function sends that |
| 62 |
* signal to everything that the current process is allowed to kill. |
| 63 |
* |
| 64 |
* - Case #4: When signo is positive and PID is negative (but not -1), this |
| 65 |
* function sends that signal to every processes in a process group, whose |
| 66 |
* process group ID is the absolute value of the passed PID. |
| 67 |
* |
| 68 |
* - Case #5: When signo is zero and PID is positive, this function just |
| 69 |
* checks for the existence of the specified process and doesn't send |
| 70 |
* anything to anyone. In case the process is absent `Errno::ESRCH` is |
| 71 |
* raised. |
| 72 |
* |
| 73 |
* - Case #6: When signo and PID are both zero, this function checks for the |
| 74 |
* existence of the current process group. And it must do. This function |
| 75 |
* is effectively a no-op then. |
| 76 |
* |
| 77 |
* - Case #7: When signo is zero and PID is -1, this function checks if there |
| 78 |
* is any other process that the current process can kill. At least init |
| 79 |
* (PID 1) must exist, so this must not fail. |
| 80 |
* |
| 81 |
* - Case #8: When signo is zero and PID is negative (but not -1), this |
| 82 |
* function checks if there is a process group whose process group ID is |
| 83 |
* the absolute value of the passed PID. In case the process group is |
| 84 |
* absent `Errno::ESRCH` is raised. |
| 85 |
* |
| 86 |
* - Case #9: When signo is negative and PID is positive, this function sends |
| 87 |
* the absolute value of the passed signo to the process group specified as |
| 88 |
* the PID. |
| 89 |
* |
| 90 |
* - Case #10: When signo is negative and PID is zero, it is highly expected |
| 91 |
* that this function sends the absolute value of the passed signo to the |
| 92 |
* current process group. Strictly speaking, IEEE Std 1003.1-2017 |
| 93 |
* specifies that this (`killpg(3posix)` with an argument of zero) is an |
| 94 |
* undefined behaviour. But no operating system is known so far that does |
| 95 |
* things differently. |
| 96 |
* |
| 97 |
* - Case #11: When signo and PID are both negative, the behaviour of this |
| 98 |
* function depends on how `killpg(3)` works. On Linux, it seems such |
| 99 |
* attempt is strictly prohibited and `Errno::EINVAL` is raised. But on |
| 100 |
* macOS, it seems it tries to to send the signal actually to the process |
| 101 |
* group. |
| 102 |
* |
| 103 |
* @note Above description is in fact different from how `kill(2)` works. |
| 104 |
* We interpret the passed arguments before passing them through to |
| 105 |
* system calls. |
| 106 |
* @param[in] argc Number of objects in `argv`. |
| 107 |
* @param[in] argv Signal, followed by target PIDs. |
| 108 |
* @exception rb_eArgError Unknown signal name. |
| 109 |
* @exception rb_eSystemCallError Various errors sending signal to processes. |
| 110 |
* @return Something numeric. The meaning of this return value is unclear. |
| 111 |
* It seems in case of #1 above, this could be the body count. But |
| 112 |
* other cases remain mysterious. |
| 113 |
*/ |
| 114 |
VALUE rb_f_kill(int argc, const VALUE *argv); |
| 115 |
|
| 116 |
RBIMPL_ATTR_PURE() |
| 117 |
/** |
| 118 |
* Queries the name of the signal. It returns for instance `"KILL"` for |
| 119 |
* SIGKILL. |
| 120 |
* |
| 121 |
* @param[in] signo Signal number to query. |
| 122 |
* @retval 0 No such signal. |
| 123 |
* @retval otherwise A pointer to a static C string that is the name of |
| 124 |
* the signal. |
| 125 |
* @warning Don't free the return value. |
| 126 |
*/ |
| 127 |
const char *ruby_signal_name(int signo); |
| 128 |
|
| 129 |
/** |
| 130 |
* Pretends as if there was no custom signal handler. This function sets the |
| 131 |
* signal action to SIG_DFL, then kills itself. |
| 132 |
* |
| 133 |
* @param[in] sig The signal. |
| 134 |
* @post Previous signal handler is lost. |
| 135 |
* @post Passed signal is sent to the current process. |
| 136 |
* |
| 137 |
* @internal |
| 138 |
* |
| 139 |
* @shyouhei doesn't understand the needs of this function being visible from |
| 140 |
* extension libraries. |
| 141 |
*/ |
| 142 |
void ruby_default_signal(int sig); |
| 143 |
|
| 144 |
RBIMPL_SYMBOL_EXPORT_END() |
| 145 |
|
| 146 |
#endif /* RBIMPL_INTERN_SIGNAL_H */ |