| 1 |
#ifndef RBIMPL_INTERN_PROCESS_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_PROCESS_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 Public APIs related to ::rb_mProcess. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/attr/nonnull.h" |
| 24 |
#include "ruby/internal/attr/noreturn.h" |
| 25 |
#include "ruby/internal/config.h" /* rb_pid_t is defined here. */ |
| 26 |
#include "ruby/internal/dllexport.h" |
| 27 |
#include "ruby/internal/value.h" |
| 28 |
|
| 29 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 30 |
|
| 31 |
/* process.c */ |
| 32 |
|
| 33 |
/** |
| 34 |
* Wait for the specified process to terminate, reap it, and return its status. |
| 35 |
* |
| 36 |
* @param[in] pid The process ID to wait for. |
| 37 |
* @param[in] flags The flags to pass to waitpid(2). |
| 38 |
* @return VALUE An instance of Process::Status. |
| 39 |
*/ |
| 40 |
VALUE rb_process_status_wait(rb_pid_t pid, int flags); |
| 41 |
|
| 42 |
/** |
| 43 |
* Sets the "last status", or the `$?`. |
| 44 |
* |
| 45 |
* @param[in] status The termination status, as defined in `waitpid(3posix)`. |
| 46 |
* @param[in] pid The last child of the current process. |
| 47 |
* @post `$?` is updated. |
| 48 |
*/ |
| 49 |
void rb_last_status_set(int status, rb_pid_t pid); |
| 50 |
|
| 51 |
/** |
| 52 |
* Queries the "last status", or the `$?`. |
| 53 |
* |
| 54 |
* @retval RUBY_Qnil The current thread has no dead children. |
| 55 |
* @retval otherwise An instance of Process::Status describing the status of |
| 56 |
* the child that was most recently `wait`-ed. |
| 57 |
*/ |
| 58 |
VALUE rb_last_status_get(void); |
| 59 |
|
| 60 |
RBIMPL_ATTR_NONNULL(()) |
| 61 |
/** |
| 62 |
* Executes a shell command. |
| 63 |
* |
| 64 |
* @warning THIS FUNCTION RETURNS on error! |
| 65 |
* @param[in] cmd Passed to the shell. |
| 66 |
* @retval -1 Something prevented the command execution. |
| 67 |
* @post Upon successful execution this function doesn't return. |
| 68 |
* @post In case it returns the `errno` is set properly. |
| 69 |
*/ |
| 70 |
int rb_proc_exec(const char *cmd); |
| 71 |
|
| 72 |
RBIMPL_ATTR_NORETURN() |
| 73 |
/** |
| 74 |
* Replaces the current process by running the given external command. This is |
| 75 |
* the implementation of `Kernel#exec`. |
| 76 |
* |
| 77 |
* @param[in] argc Number of objects in `argv`. |
| 78 |
* @param[in] argv Command and its options to execute. |
| 79 |
* @exception rb_eTypeError Invalid options e.g. non-String argv. |
| 80 |
* @exception rb_eArgError Invalid options e.g. redirection cycle. |
| 81 |
* @exception rb_eNotImpError Not implemented e.g. no `setuid(2)`. |
| 82 |
* @exception rb_eRuntimeError `Process::UID.switch` in operation. |
| 83 |
* @exception rb_eSystemCallError `execve(2)` failed. |
| 84 |
* @warning This function doesn't return. |
| 85 |
* @warning On failure it raises. On success the process is replaced. |
| 86 |
* |
| 87 |
* @internal |
| 88 |
* |
| 89 |
* @shyouhei have to say that the rdoc for `Kernel#exec` is fairly incomplete. |
| 90 |
* AFAIK this function ultimately takes the following signature: |
| 91 |
* |
| 92 |
* ```rbs |
| 93 |
* type boolx = bool | nil # != `boolish` |
| 94 |
* |
| 95 |
* type rlim_t = Integer # rlim_cur |
| 96 |
* | [ Integer, Integer ] # rlim_cur, rlim_max |
| 97 |
* |
| 98 |
* type uid_t = String # e.g. "root" |
| 99 |
* | Integer # e.g. 0 |
| 100 |
* |
| 101 |
* type gid_t = String # e.g. "wheel" |
| 102 |
* | Integer # e.g. 0 |
| 103 |
* |
| 104 |
* type fmode = String # e.g. "rb" |
| 105 |
* | Integer # e.g. O_RDONLY | O_BINARY |
| 106 |
* |
| 107 |
* type mode_t = Integer # e.g. 0644 |
| 108 |
* |
| 109 |
* type pgrp = true # Creates a dedicated pgroup |
| 110 |
* | 0 # ditto |
| 111 |
* | nil # Uses the current one |
| 112 |
* | Integer # Uses this specific pgroup |
| 113 |
* |
| 114 |
* type fd = :in # STDIN |
| 115 |
* | :out # STDOUT |
| 116 |
* | :err # STDERR |
| 117 |
* | IO # This specific IO |
| 118 |
* | Integer # A file descriptor of this # |
| 119 |
* |
| 120 |
* type src = fd | [ fd ] |
| 121 |
* type dst = :close # Intuitive |
| 122 |
* | fd # Intuitive |
| 123 |
* | String # Open a file at this path |
| 124 |
* | [ String ] # ... using O_RDONLY |
| 125 |
* | [ String, fmode ] # ... using this mode |
| 126 |
* | [ String, fmode, mode_t ] # ... with a permission |
| 127 |
* | [ :child, fd ] # fd of child side |
| 128 |
* |
| 129 |
* type redir = Hash[ src, dst ] |
| 130 |
* |
| 131 |
* # ---- |
| 132 |
* |
| 133 |
* # Key-value pair of environment variables |
| 134 |
* type envp = Hash[ String, String ] |
| 135 |
* |
| 136 |
* # Actual name (and the name passed to the subprocess if any) |
| 137 |
* type arg0 = String | [ String, String ] |
| 138 |
* |
| 139 |
* # Arbitrary string parameters |
| 140 |
* type argv = String |
| 141 |
* |
| 142 |
* # Exec options: |
| 143 |
* type argh = redir | { |
| 144 |
* chdir: String, # Working directory |
| 145 |
* close_others: boolx, # O_CLOEXEC like behaviour |
| 146 |
* gid: gid_t, # setegid(2) |
| 147 |
* pgrooup: pgrp, # setpgrp(2) |
| 148 |
* rlimit_as: rlim_t, # setrlimit(2) |
| 149 |
* rlimit_core: rlim_t, # ditto |
| 150 |
* rlimit_cpu: rlim_t, # ditto |
| 151 |
* rlimit_data: rlim_t, # ditto |
| 152 |
* rlimit_fsize: rlim_t, # ditto |
| 153 |
* rlimit_memlock: rlim_t, # ditto |
| 154 |
* rlimit_msgqueue: rlim_t, # ditto |
| 155 |
* rlimit_nice: rlim_t, # ditto |
| 156 |
* rlimit_nofile: rlim_t, # ditto |
| 157 |
* rlimit_nproc: rlim_t, # ditto |
| 158 |
* rlimit_rss: rlim_t, # ditto |
| 159 |
* rlimit_rtprio: rlim_t, # ditto |
| 160 |
* rlimit_rttime: rlim_t, # ditto |
| 161 |
* rlimit_sbsize: rlim_t, # ditto |
| 162 |
* rlimit_sigpending: rlim_t, # ditto |
| 163 |
* rlimit_stack: rlim_t, # ditto |
| 164 |
* uid: uid_t, # seteuid(2) |
| 165 |
* umask: mode_t, # umask(2) |
| 166 |
* unsetenv_others: boolx # Unset everything except the passed envp |
| 167 |
* } |
| 168 |
* |
| 169 |
* # ==== |
| 170 |
* |
| 171 |
* class Kernel |
| 172 |
* def self?.exec |
| 173 |
* : ( arg0 cmd, *argv args ) -> void |
| 174 |
* | ( arg0 cmd, *argv args, argh opts) -> void |
| 175 |
* | (envp env, arg0 cmd, *argv args ) -> void |
| 176 |
* | (envp env, arg0 cmd, *argv args, argh opts) -> void |
| 177 |
* end |
| 178 |
* ``` |
| 179 |
*/ |
| 180 |
VALUE rb_f_exec(int argc, const VALUE *argv); |
| 181 |
|
| 182 |
/** |
| 183 |
* Waits for a process, with releasing GVL. |
| 184 |
* |
| 185 |
* @param[in] pid Process ID. |
| 186 |
* @param[out] status The wait status is filled back. |
| 187 |
* @param[in] flags Wait options. |
| 188 |
* @retval -1 System call failed, errno set. |
| 189 |
* @retval 0 WNOHANG but no waitable children. |
| 190 |
* @retval otherwise A process ID that was `wait()`-ed. |
| 191 |
* @post Upon successful return `status` is updated to have the process' |
| 192 |
* status. |
| 193 |
* @note `status` can be NULL. |
| 194 |
* @note The arguments are passed through to underlying system call(s). |
| 195 |
* Can have special meanings. For instance passing `(rb_pid_t)-1` |
| 196 |
* to `pid` means it waits for any processes, under |
| 197 |
* POSIX-compliant situations. |
| 198 |
*/ |
| 199 |
rb_pid_t rb_waitpid(rb_pid_t pid, int *status, int flags); |
| 200 |
|
| 201 |
/** |
| 202 |
* This is a shorthand of rb_waitpid without status and flags. It has been |
| 203 |
* like this since the very beginning. The initial revision already did the |
| 204 |
* same thing. Not sure why, then, it has been named `syswait`. AFAIK this is |
| 205 |
* different from how `wait(3posix)` works. |
| 206 |
* |
| 207 |
* @param[in] pid Passed to rb_waitpid(). |
| 208 |
*/ |
| 209 |
void rb_syswait(rb_pid_t pid); |
| 210 |
|
| 211 |
/** |
| 212 |
* Identical to rb_f_exec(), except it spawns a child process instead of |
| 213 |
* replacing the current one. |
| 214 |
* |
| 215 |
* @param[in] argc Number of objects in `argv`. |
| 216 |
* @param[in] argv Command and its options to execute. |
| 217 |
* @exception rb_eTypeError Invalid options e.g. non-String argv. |
| 218 |
* @exception rb_eArgError Invalid options e.g. redirection cycle. |
| 219 |
* @exception rb_eNotImpError Not implemented e.g. no `setuid(2)`. |
| 220 |
* @exception rb_eRuntimeError `Process::UID.switch` in operation. |
| 221 |
* @retval -1 Child process died for some reason. |
| 222 |
* @retval otherwise The ID of the born child. |
| 223 |
* |
| 224 |
* @internal |
| 225 |
* |
| 226 |
* This is _really_ identical to rb_f_exec() until ultimately calling the |
| 227 |
* system call. Almost everything are shared among these two (and |
| 228 |
* rb_f_system()). |
| 229 |
*/ |
| 230 |
rb_pid_t rb_spawn(int argc, const VALUE *argv); |
| 231 |
|
| 232 |
/** |
| 233 |
* Identical to rb_spawn(), except you can additionally know the detailed |
| 234 |
* situation in case of abnormal parturitions. |
| 235 |
* |
| 236 |
* @param[in] argc Number of objects in `argv`. |
| 237 |
* @param[in] argv Command and its options to execute. |
| 238 |
* @param[out] errbuf Error description write-back buffer. |
| 239 |
* @param[in] buflen Number of bytes of `errbuf`, including NUL. |
| 240 |
* @exception rb_eTypeError Invalid options e.g. non-String argv. |
| 241 |
* @exception rb_eArgError Invalid options e.g. redirection cycle. |
| 242 |
* @exception rb_eNotImpError Not implemented e.g. no `setuid(2)`. |
| 243 |
* @exception rb_eRuntimeError `Process::UID.switch` in operation. |
| 244 |
* @retval -1 Child process died for some reason. |
| 245 |
* @retval otherwise The ID of the born child. |
| 246 |
* @post In case of `-1`, at most `buflen` bytes of the reason why is |
| 247 |
* written back to `errbuf`. |
| 248 |
*/ |
| 249 |
rb_pid_t rb_spawn_err(int argc, const VALUE *argv, char *errbuf, size_t buflen); |
| 250 |
|
| 251 |
/** |
| 252 |
* Gathers info about resources consumed by the current process. |
| 253 |
* |
| 254 |
* @param[in] _ Not used. Pass anything. |
| 255 |
* @return An instance of `Process::Tms`. |
| 256 |
* |
| 257 |
* @internal |
| 258 |
* |
| 259 |
* This function might or might not exist depending on `./configure` result. |
| 260 |
* It must be a portability hell. Better not use. |
| 261 |
*/ |
| 262 |
VALUE rb_proc_times(VALUE _); |
| 263 |
|
| 264 |
/** |
| 265 |
* "Detaches" a subprocess. In POSIX systems every child processes that a |
| 266 |
* process creates must be `wait(2)`-ed. A child process that died yet has not |
| 267 |
* been waited so far is called a "zombie", which more or less consumes |
| 268 |
* resources. This function automates reclamation of such processes. Once |
| 269 |
* after this function successfully returns you can basically forget about the |
| 270 |
* child process. |
| 271 |
* |
| 272 |
* @param[in] pid Process to wait. |
| 273 |
* @return An instance of ::rb_cThread which is `waitpid(2)`-ing `pid`. |
| 274 |
* @post You can just forget about the return value. GC reclaims it. |
| 275 |
* @post You can know the exit status by querying `#value` of the |
| 276 |
* return value (which is a blocking operation). |
| 277 |
*/ |
| 278 |
VALUE rb_detach_process(rb_pid_t pid); |
| 279 |
|
| 280 |
RBIMPL_SYMBOL_EXPORT_END() |
| 281 |
|
| 282 |
#endif /* RBIMPL_INTERN_PROCESS_H */ |