14 #include "ruby/internal/config.h"
42 # define EXIT_SUCCESS 0
46 # define EXIT_FAILURE 1
49 #ifdef HAVE_SYS_WAIT_H
50 # include <sys/wait.h>
53 #ifdef HAVE_SYS_RESOURCE_H
54 # include <sys/resource.h>
61 #ifdef HAVE_SYS_PARAM_H
62 # include <sys/param.h>
66 # define MAXPATHLEN 1024
71 #ifdef HAVE_SYS_TIME_H
72 # include <sys/time.h>
75 #ifdef HAVE_SYS_TIMES_H
76 # include <sys/times.h>
86 int initgroups(
const char *, rb_gid_t);
95 # include <mach/mach_time.h>
100 #include "internal.h"
101 #include "internal/bits.h"
102 #include "internal/dir.h"
103 #include "internal/error.h"
104 #include "internal/eval.h"
105 #include "internal/hash.h"
106 #include "internal/io.h"
107 #include "internal/numeric.h"
108 #include "internal/object.h"
109 #include "internal/process.h"
110 #include "internal/thread.h"
111 #include "internal/variable.h"
112 #include "internal/warnings.h"
125 #define open rb_w32_uopen
128 #if defined(HAVE_TIMES) || defined(_WIN32)
135 static VALUE rb_cProcessTms;
139 #define WIFEXITED(w) (((w) & 0xff) == 0)
142 #define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
145 #define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
148 #define WEXITSTATUS(w) (((w) >> 8) & 0xff)
151 #define WTERMSIG(w) ((w) & 0x7f)
154 #define WSTOPSIG WEXITSTATUS
157 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__)
158 #define HAVE_44BSD_SETUID 1
159 #define HAVE_44BSD_SETGID 1
167 #ifdef BROKEN_SETREUID
168 #define setreuid ruby_setreuid
169 int setreuid(rb_uid_t ruid, rb_uid_t euid);
171 #ifdef BROKEN_SETREGID
172 #define setregid ruby_setregid
173 int setregid(rb_gid_t rgid, rb_gid_t egid);
176 #if defined(HAVE_44BSD_SETUID) || defined(__APPLE__)
177 #if !defined(USE_SETREUID) && !defined(BROKEN_SETREUID)
178 #define OBSOLETE_SETREUID 1
180 #if !defined(USE_SETREGID) && !defined(BROKEN_SETREGID)
181 #define OBSOLETE_SETREGID 1
185 static void check_uid_switch(
void);
186 static void check_gid_switch(
void);
187 static int exec_async_signal_safe(
const struct rb_execarg *,
char *,
size_t);
189 VALUE rb_envtbl(
void);
190 VALUE rb_env_to_hash(
void);
193 #define p_uid_from_name p_uid_from_name
194 #define p_gid_from_name p_gid_from_name
197 #if defined(HAVE_UNISTD_H)
198 # if defined(HAVE_GETLOGIN_R)
199 # define USE_GETLOGIN_R 1
200 # define GETLOGIN_R_SIZE_DEFAULT 0x100
201 # define GETLOGIN_R_SIZE_LIMIT 0x1000
202 # if defined(_SC_LOGIN_NAME_MAX)
203 # define GETLOGIN_R_SIZE_INIT sysconf(_SC_LOGIN_NAME_MAX)
205 # define GETLOGIN_R_SIZE_INIT GETLOGIN_R_SIZE_DEFAULT
207 # elif defined(HAVE_GETLOGIN)
208 # define USE_GETLOGIN 1
212 #if defined(HAVE_PWD_H)
213 # if defined(HAVE_GETPWUID_R)
214 # define USE_GETPWUID_R 1
215 # elif defined(HAVE_GETPWUID)
216 # define USE_GETPWUID 1
218 # if defined(HAVE_GETPWNAM_R)
219 # define USE_GETPWNAM_R 1
220 # elif defined(HAVE_GETPWNAM)
221 # define USE_GETPWNAM 1
223 # if defined(HAVE_GETPWNAM_R) || defined(HAVE_GETPWUID_R)
224 # define GETPW_R_SIZE_DEFAULT 0x1000
225 # define GETPW_R_SIZE_LIMIT 0x10000
226 # if defined(_SC_GETPW_R_SIZE_MAX)
227 # define GETPW_R_SIZE_INIT sysconf(_SC_GETPW_R_SIZE_MAX)
229 # define GETPW_R_SIZE_INIT GETPW_R_SIZE_DEFAULT
232 # ifdef USE_GETPWNAM_R
233 # define PREPARE_GETPWNAM \
235 # define FINISH_GETPWNAM \
236 (getpw_buf ? (void)rb_str_resize(getpw_buf, 0) : (void)0)
237 # define OBJ2UID1(id) obj2uid((id), &getpw_buf)
238 # define OBJ2UID(id) obj2uid0(id)
239 static rb_uid_t obj2uid(
VALUE id,
VALUE *getpw_buf);
240 static inline rb_uid_t
250 # define PREPARE_GETPWNAM
251 # define FINISH_GETPWNAM
252 # define OBJ2UID1(id) obj2uid((id))
253 # define OBJ2UID(id) obj2uid((id))
254 static rb_uid_t obj2uid(
VALUE id);
257 # define PREPARE_GETPWNAM
258 # define FINISH_GETPWNAM
259 # define OBJ2UID1(id) NUM2UIDT(id)
260 # define OBJ2UID(id) NUM2UIDT(id)
261 # ifdef p_uid_from_name
262 # undef p_uid_from_name
263 # define p_uid_from_name rb_f_notimplement
267 #if defined(HAVE_GRP_H)
268 # if defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
269 # define USE_GETGRNAM_R
270 # define GETGR_R_SIZE_INIT sysconf(_SC_GETGR_R_SIZE_MAX)
271 # define GETGR_R_SIZE_DEFAULT 0x1000
272 # define GETGR_R_SIZE_LIMIT 0x10000
274 # ifdef USE_GETGRNAM_R
275 # define PREPARE_GETGRNAM \
277 # define FINISH_GETGRNAM \
278 (getgr_buf ? (void)rb_str_resize(getgr_buf, 0) : (void)0)
279 # define OBJ2GID1(id) obj2gid((id), &getgr_buf)
280 # define OBJ2GID(id) obj2gid0(id)
281 static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
282 static inline rb_gid_t
291 static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
293 # define PREPARE_GETGRNAM
294 # define FINISH_GETGRNAM
295 # define OBJ2GID1(id) obj2gid((id))
296 # define OBJ2GID(id) obj2gid((id))
297 static rb_gid_t obj2gid(
VALUE id);
300 # define PREPARE_GETGRNAM
301 # define FINISH_GETGRNAM
302 # define OBJ2GID1(id) NUM2GIDT(id)
303 # define OBJ2GID(id) NUM2GIDT(id)
304 # ifdef p_gid_from_name
305 # undef p_gid_from_name
306 # define p_gid_from_name rb_f_notimplement
310 #if SIZEOF_CLOCK_T == SIZEOF_INT
311 typedef unsigned int unsigned_clock_t;
312 #elif SIZEOF_CLOCK_T == SIZEOF_LONG
313 typedef unsigned long unsigned_clock_t;
314 #elif defined(HAVE_LONG_LONG) && SIZEOF_CLOCK_T == SIZEOF_LONG_LONG
315 typedef unsigned LONG_LONG unsigned_clock_t;
318 typedef void (*sig_t) (int);
321 #define id_exception idException
322 static ID id_in, id_out, id_err, id_pid, id_uid, id_gid;
323 static ID id_close, id_child;
328 static ID id_new_pgroup;
330 static ID id_unsetenv_others, id_chdir, id_umask, id_close_others;
331 static ID id_nanosecond, id_microsecond, id_millisecond, id_second;
332 static ID id_float_microsecond, id_float_millisecond, id_float_second;
333 static ID id_GETTIMEOFDAY_BASED_CLOCK_REALTIME, id_TIME_BASED_CLOCK_REALTIME;
334 #ifdef CLOCK_REALTIME
335 static ID id_CLOCK_REALTIME;
336 # define RUBY_CLOCK_REALTIME ID2SYM(id_CLOCK_REALTIME)
338 #ifdef CLOCK_MONOTONIC
339 static ID id_CLOCK_MONOTONIC;
340 # define RUBY_CLOCK_MONOTONIC ID2SYM(id_CLOCK_MONOTONIC)
342 #ifdef CLOCK_PROCESS_CPUTIME_ID
343 static ID id_CLOCK_PROCESS_CPUTIME_ID;
344 # define RUBY_CLOCK_PROCESS_CPUTIME_ID ID2SYM(id_CLOCK_PROCESS_CPUTIME_ID)
346 #ifdef CLOCK_THREAD_CPUTIME_ID
347 static ID id_CLOCK_THREAD_CPUTIME_ID;
348 # define RUBY_CLOCK_THREAD_CPUTIME_ID ID2SYM(id_CLOCK_THREAD_CPUTIME_ID)
351 static ID id_TIMES_BASED_CLOCK_MONOTONIC;
352 static ID id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID;
355 static ID id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID;
357 static ID id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID;
359 static ID id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC;
360 # define RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ID2SYM(id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
364 static rb_pid_t cached_pid;
367 #if defined(__sun) && !defined(_XPG7)
368 #define execv(path, argv) (rb_async_bug_errno("unreachable: async-signal-unsafe execv() is called", 0))
369 #define execl(path, arg0, arg1, arg2, term) do { extern char **environ; execle((path), (arg0), (arg1), (arg2), (term), (environ)); } while (0)
370 #define ALWAYS_NEED_ENVP 1
372 #define ALWAYS_NEED_ENVP 0
376 assert_close_on_exec(
int fd)
378 #if VM_CHECK_MODE > 0
379 #if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(FD_CLOEXEC)
380 int flags = fcntl(fd, F_GETFD);
382 static const char m[] =
"reserved FD closed unexpectedly?\n";
383 (void)!write(2, m,
sizeof(m) - 1);
386 if (flags & FD_CLOEXEC)
return;
387 rb_bug(
"reserved FD did not have close-on-exec set");
389 rb_bug(
"reserved FD without close-on-exec support");
395 close_unless_reserved(
int fd)
398 assert_close_on_exec(fd);
405 #if defined(DEBUG_REDIRECT)
408 ttyprintf(
const char *fmt, ...)
414 tty = fopen(
"con",
"w");
416 tty = fopen(
"/dev/tty",
"w");
422 vfprintf(tty, fmt, ap);
429 redirect_dup(
int oldfd)
433 ttyprintf(
"dup(%d) => %d\n", oldfd, ret);
438 redirect_dup2(
int oldfd,
int newfd)
441 ret = dup2(oldfd, newfd);
442 ttyprintf(
"dup2(%d, %d) => %d\n", oldfd, newfd, ret);
447 redirect_cloexec_dup(
int oldfd)
451 ttyprintf(
"cloexec_dup(%d) => %d\n", oldfd, ret);
456 redirect_cloexec_dup2(
int oldfd,
int newfd)
460 ttyprintf(
"cloexec_dup2(%d, %d) => %d\n", oldfd, newfd, ret);
465 redirect_close(
int fd)
468 ret = close_unless_reserved(fd);
469 ttyprintf(
"close(%d) => %d\n", fd, ret);
474 parent_redirect_open(
const char *pathname,
int flags, mode_t perm)
478 ttyprintf(
"parent_open(\"%s\", 0x%x, 0%o) => %d\n", pathname, flags, perm, ret);
483 parent_redirect_close(
int fd)
486 ret = close_unless_reserved(fd);
487 ttyprintf(
"parent_close(%d) => %d\n", fd, ret);
492 #define redirect_dup(oldfd) dup(oldfd)
493 #define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
494 #define redirect_cloexec_dup(oldfd) rb_cloexec_dup(oldfd)
495 #define redirect_cloexec_dup2(oldfd, newfd) rb_cloexec_dup2((oldfd), (newfd))
496 #define redirect_close(fd) close_unless_reserved(fd)
497 #define parent_redirect_open(pathname, flags, perm) rb_cloexec_open((pathname), (flags), (perm))
498 #define parent_redirect_close(fd) close_unless_reserved(fd)
504 if (UNLIKELY(!cached_pid)) {
505 cached_pid = getpid();
511 #if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
513 clear_pid_cache(
void)
585 static VALUE rb_cProcessStatus;
600 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
604 rb_process_status_allocate(
VALUE klass)
613 return GET_THREAD()->last_status;
639 proc_s_last_status(
VALUE mod)
645 rb_process_status_new(rb_pid_t pid,
int status,
int error)
647 VALUE last_status = rb_process_status_allocate(rb_cProcessStatus);
650 data->status = status;
658 process_status_dump(
VALUE status)
671 process_status_load(
VALUE real_obj,
VALUE load_obj)
684 GET_THREAD()->last_status = rb_process_status_new(pid, status, 0);
690 th->last_status =
Qnil;
694 rb_last_status_clear(
void)
696 last_status_clear(GET_THREAD());
700 pst_pid(
VALUE status)
708 pst_status(
VALUE status)
728 int status = pst_status(
self);
732 #define PST2INT(st) pst_status(st)
746 pst_pid_m(
VALUE self)
748 rb_pid_t pid = pst_pid(
self);
752 static VALUE pst_message_status(
VALUE str,
int status);
755 pst_message(
VALUE str, rb_pid_t pid,
int status)
758 pst_message_status(str, status);
762 pst_message_status(
VALUE str,
int status)
764 if (WIFSTOPPED(status)) {
765 int stopsig = WSTOPSIG(status);
768 rb_str_catf(str,
" stopped SIG%s (signal %d)", signame, stopsig);
774 if (WIFSIGNALED(status)) {
775 int termsig = WTERMSIG(status);
778 rb_str_catf(str,
" SIG%s (signal %d)", signame, termsig);
784 if (WIFEXITED(status)) {
788 if (WCOREDUMP(status)) {
816 status = PST2INT(st);
819 pst_message(str, pid, status);
836 pst_inspect(
VALUE st)
846 status = PST2INT(st);
849 pst_message(str, pid, status);
871 if (st1 == st2)
return Qtrue;
872 return rb_equal(pst_to_i(st1), st2);
897 int status = PST2INT(st1);
903 #define WARN_SUGGEST(suggest) \
904 rb_warn_deprecated_to_remove_at(3.5, "Process::Status#&", suggest)
908 WARN_SUGGEST(
"Process::Status#coredump?");
911 WARN_SUGGEST(
"Process::Status#signaled? or Process::Status#termsig");
914 WARN_SUGGEST(
"Process::Status#exited?, Process::Status#stopped? or Process::Status#coredump?");
917 WARN_SUGGEST(
"Process::Status#exitstatus or Process::Status#stopsig");
920 WARN_SUGGEST(
"other Process::Status predicates");
952 int status = PST2INT(st1);
958 #define WARN_SUGGEST(suggest) \
959 rb_warn_deprecated_to_remove_at(3.5, "Process::Status#>>", suggest)
963 WARN_SUGGEST(
"Process::Status#coredump?");
966 WARN_SUGGEST(
"Process::Status#exitstatus or Process::Status#stopsig");
969 WARN_SUGGEST(
"other Process::Status attributes");
989 pst_wifstopped(
VALUE st)
991 int status = PST2INT(st);
993 return RBOOL(WIFSTOPPED(status));
1006 pst_wstopsig(
VALUE st)
1008 int status = PST2INT(st);
1010 if (WIFSTOPPED(status))
1011 return INT2NUM(WSTOPSIG(status));
1025 pst_wifsignaled(
VALUE st)
1027 int status = PST2INT(st);
1029 return RBOOL(WIFSIGNALED(status));
1042 pst_wtermsig(
VALUE st)
1044 int status = PST2INT(st);
1046 if (WIFSIGNALED(status))
1047 return INT2NUM(WTERMSIG(status));
1062 pst_wifexited(
VALUE st)
1064 int status = PST2INT(st);
1066 return RBOOL(WIFEXITED(status));
1084 pst_wexitstatus(
VALUE st)
1086 int status = PST2INT(st);
1088 if (WIFEXITED(status))
1089 return INT2NUM(WEXITSTATUS(status));
1107 pst_success_p(
VALUE st)
1109 int status = PST2INT(st);
1111 if (!WIFEXITED(status))
1113 return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
1128 pst_wcoredump(
VALUE st)
1131 int status = PST2INT(st);
1133 return RBOOL(WCOREDUMP(status));
1140 do_waitpid(rb_pid_t pid,
int *st,
int flags)
1142 #if defined HAVE_WAITPID
1143 return waitpid(pid, st, flags);
1144 #elif defined HAVE_WAIT4
1145 return wait4(pid, st, flags, NULL);
1147 # error waitpid or wait4 is required.
1152 struct ccan_list_node wnode;
1154 rb_nativethread_cond_t *cond;
1163 waitpid_state_init(
struct waitpid_state *w, rb_pid_t pid,
int options)
1167 w->options = options;
1173 waitpid_blocking_no_SIGCHLD(
void *x)
1177 w->ret = do_waitpid(w->pid, &w->status, w->options);
1185 if (w->options & WNOHANG) {
1186 w->ret = do_waitpid(w->pid, &w->status, w->options);
1191 }
while (w->ret < 0 &&
errno == EINTR && (RUBY_VM_CHECK_INTS(w->ec),1));
1201 if (!(flags & WNOHANG)) {
1204 if (!UNDEF_P(result))
return result;
1250 rb_process_status_waitv(
int argc,
VALUE *argv,
VALUE _)
1272 if (
NIL_P(status))
return 0;
1277 if (st) *st = data->status;
1280 errno = data->error;
1283 GET_THREAD()->last_status = status;
1290 proc_wait(
int argc,
VALUE *argv)
1302 if (argc == 2 && !
NIL_P(vflags = argv[1])) {
1307 if ((pid =
rb_waitpid(pid, &status, flags)) < 0)
1311 rb_last_status_clear();
1468 return proc_wait(c, v);
1488 VALUE pid = proc_wait(argc, argv);
1517 rb_last_status_clear();
1532 static VALUE rb_cWaiter;
1535 detach_process_pid(
VALUE thread)
1541 detach_process_watcher(
void *arg)
1543 rb_pid_t cpid, pid = (rb_pid_t)(
VALUE)arg;
1546 while ((cpid =
rb_waitpid(pid, &status, 0)) == 0) {
1557 RBASIC_SET_CLASS(watcher, rb_cWaiter);
1609 before_exec_async_signal_safe(
void)
1614 before_exec_non_async_signal_safe(
void)
1625 rb_thread_stop_timer_thread();
1628 #define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
1630 int rb_w32_set_nonblock2(
int fd,
int nonblock);
1634 set_blocking(
int fd)
1637 return rb_w32_set_nonblock2(fd, 0);
1638 #elif defined(F_GETFL) && defined(F_SETFL)
1639 int fl = fcntl(fd, F_GETFL);
1642 if (fl == -1)
return fl;
1643 if (fl & O_NONBLOCK) {
1645 return fcntl(fd, F_SETFL, fl);
1652 stdfd_clear_nonblock(
void)
1656 for (fd = 0; fd < 3; fd++) {
1657 (void)set_blocking(fd);
1664 before_exec_non_async_signal_safe();
1665 before_exec_async_signal_safe();
1671 rb_thread_reset_timer_thread();
1672 rb_thread_start_timer_thread();
1675 #if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
1677 before_fork_ruby(
void)
1683 after_fork_ruby(rb_pid_t pid)
1697 #if defined(HAVE_WORKING_FORK)
1699 COMPILER_WARNING_PUSH
1700 #if __has_warning("-Wdeprecated-declarations") || RBIMPL_COMPILER_IS(GCC)
1701 COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
1703 static inline rb_pid_t
1708 COMPILER_WARNING_POP
1711 #define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1713 exec_with_sh(
const char *prog,
char **argv,
char **envp)
1715 *argv = (
char *)prog;
1716 *--argv = (
char *)
"sh";
1718 execve(
"/bin/sh", argv, envp);
1720 execv(
"/bin/sh", argv);
1724 #define try_with_sh(err, prog, argv, envp) (void)0
1729 proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1737 argv = ARGVSTR2ARGV(argv_str);
1744 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1747 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1749 execve(prog, argv, envp);
1753 try_with_sh(err, prog, argv, envp);
1760 proc_exec_sh(
const char *str,
VALUE envp_str)
1765 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1773 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1774 #elif defined(__CYGWIN32__)
1776 char fbuf[MAXPATHLEN];
1777 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
1780 execl(shell,
"sh",
"-c", str, (
char *) NULL);
1782 status = system(str);
1788 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1790 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1800 ret = proc_exec_sh(str,
Qfalse);
1807 mark_exec_arg(
void *
ptr)
1810 if (eargp->use_shell)
1814 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1833 memsize_exec_arg(
const void *
ptr)
1841 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
1845 # define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1847 #ifdef DEFAULT_PROCESS_ENCODING
1848 # define EXPORT_STR(str) rb_str_export_to_enc((str), DEFAULT_PROCESS_ENCODING)
1849 # define EXPORT_DUP(str) export_dup(str)
1851 export_dup(
VALUE str)
1853 VALUE newstr = EXPORT_STR(str);
1858 # define EXPORT_STR(str) (str)
1859 # define EXPORT_DUP(str) rb_str_dup(str)
1862 #if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1863 # define USE_SPAWNV 1
1865 # define USE_SPAWNV 0
1868 # define P_NOWAIT _P_NOWAIT
1873 #define proc_spawn_cmd_internal(argv, prog) rb_w32_uaspawn(P_NOWAIT, (prog), (argv))
1876 proc_spawn_cmd_internal(
char **argv,
char *prog)
1878 char fbuf[MAXPATHLEN];
1883 prog = dln_find_exe_r(prog, 0, fbuf,
sizeof(fbuf));
1888 status = spawnv(P_NOWAIT, prog, (
const char **)argv);
1889 if (status == -1 &&
errno == ENOEXEC) {
1890 *argv = (
char *)prog;
1891 *--argv = (
char *)
"sh";
1892 status = spawnv(P_NOWAIT,
"/bin/sh", (
const char **)argv);
1894 if (status == -1)
errno = ENOEXEC;
1908 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1909 flags = CREATE_NEW_PROCESS_GROUP;
1911 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ?
RSTRING_PTR(prog) : 0, argv, flags);
1913 pid = proc_spawn_cmd_internal(argv, prog ?
RSTRING_PTR(prog) : 0);
1920 #define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
1923 proc_spawn_sh(
char *str)
1925 char fbuf[MAXPATHLEN];
1928 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
1930 status = spawnl(P_NOWAIT, (shell ? shell :
"/bin/sh"),
"sh",
"-c", str, (
char*)NULL);
1940 RBASIC_CLEAR_CLASS(obj);
1945 check_exec_redirect_fd(
VALUE v,
int iskey)
1956 else if (
id == id_out)
1958 else if (
id == id_err)
1977 else if (fd >= 3 && iskey) {
1995 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
2002 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
2013 VALUE path, flags, perm;
2017 switch (
TYPE(val)) {
2020 if (
id == id_close) {
2022 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
2024 else if (
id == id_in) {
2026 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2028 else if (
id == id_out) {
2030 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2032 else if (
id == id_err) {
2034 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2044 val = check_exec_redirect_fd(val, 0);
2048 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2054 path ==
ID2SYM(id_child)) {
2055 param = check_exec_redirect_fd(
rb_ary_entry(val, 1), 0);
2056 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
2069 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2070 flags, perm,
Qnil));
2071 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2079 key = check_exec_redirect_fd(key, 1);
2081 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2086 VALUE fd = check_exec_redirect_fd(v, 1);
2090 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2097 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2098 flags, perm,
Qnil));
2099 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2105 if (!
NIL_P(val))
goto io;
2111 #if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2112 static int rlimit_type_by_sym(
VALUE key);
2115 rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
2117 VALUE ary = eargp->rlimit_limits;
2118 VALUE tmp, softlim, hardlim;
2119 if (eargp->rlimit_limits ==
Qfalse)
2120 ary = eargp->rlimit_limits = hide_obj(
rb_ary_new());
2122 ary = eargp->rlimit_limits;
2143 #define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
2147 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2151 switch (
TYPE(key)) {
2153 #if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2155 int rtype = rlimit_type_by_sym(key);
2157 rb_execarg_addopt_rlimit(eargp, rtype, val);
2165 if (
id == id_pgroup) {
2167 if (eargp->pgroup_given) {
2172 else if (val ==
Qtrue)
2180 eargp->pgroup_given = 1;
2181 eargp->pgroup_pgid = pgroup;
2186 if (
id == id_new_pgroup) {
2187 if (eargp->new_pgroup_given) {
2190 eargp->new_pgroup_given = 1;
2191 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2195 if (
id == id_unsetenv_others) {
2196 if (eargp->unsetenv_others_given) {
2199 eargp->unsetenv_others_given = 1;
2200 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2202 else if (
id == id_chdir) {
2203 if (eargp->chdir_given) {
2208 eargp->chdir_given = 1;
2209 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2211 else if (
id == id_umask) {
2213 if (eargp->umask_given) {
2216 eargp->umask_given = 1;
2217 eargp->umask_mask = cmask;
2219 else if (
id == id_close_others) {
2220 if (eargp->close_others_given) {
2223 eargp->close_others_given = 1;
2224 eargp->close_others_do = TO_BOOL(val,
"close_others");
2226 else if (
id == id_in) {
2230 else if (
id == id_out) {
2234 else if (
id == id_err) {
2238 else if (
id == id_uid) {
2240 if (eargp->uid_given) {
2245 eargp->uid = OBJ2UID(val);
2246 eargp->uid_given = 1;
2250 "uid option is unimplemented on this machine");
2253 else if (
id == id_gid) {
2255 if (eargp->gid_given) {
2260 eargp->gid = OBJ2GID(val);
2261 eargp->gid_given = 1;
2265 "gid option is unimplemented on this machine");
2268 else if (
id == id_exception) {
2269 if (eargp->exception_given) {
2272 eargp->exception_given = 1;
2273 eargp->exception = TO_BOOL(val,
"exception");
2284 check_exec_redirect(key, val, eargp);
2296 check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2301 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2311 check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2316 VALUE execarg_obj = args[0];
2317 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2318 VALUE nonopts = args[1];
2337 if (ary == eargp->fd_dup2)
2339 else if (ary == eargp->fd_dup2_child)
2345 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2363 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2364 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2365 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2367 if (eargp->fd_dup2_child) {
2368 ary = eargp->fd_dup2_child;
2385 if (oldfd != lastfd) {
2398 eargp->close_others_maxhint = maxhint;
2403 rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2407 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2411 rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2416 args[0] = execarg_obj;
2418 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2422 #ifdef ENV_IGNORECASE
2423 #define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2425 #define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2429 check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2444 key = EXPORT_STR(key);
2445 if (!
NIL_P(val)) val = EXPORT_STR(val);
2462 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2469 rb_check_argv(
int argc,
VALUE *argv)
2488 for (i = 0; i < argc; i++) {
2497 check_hash(
VALUE obj)
2511 rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2516 hash = check_hash((*argv_p)[*argc_p-1]);
2518 *opthash_ret = hash;
2524 hash = check_hash((*argv_p)[0]);
2531 prog = rb_check_argv(*argc_p, *argv_p);
2533 prog = (*argv_p)[0];
2534 if (accept_shell && *argc_p == 1) {
2549 compare_posix_sh(
const void *key,
const void *el)
2552 int ret = strncmp(word->ptr, el, word->len);
2553 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2561 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2562 char fbuf[MAXPATHLEN];
2566 if (!
NIL_P(opthash)) {
2567 rb_check_exec_options(opthash, execarg_obj);
2570 env = rb_check_exec_env(env, &eargp->path_env);
2571 eargp->env_modification = env;
2574 prog = EXPORT_STR(prog);
2575 eargp->use_shell = argc == 0;
2576 if (eargp->use_shell)
2577 eargp->invoke.sh.shell_script = prog;
2579 eargp->invoke.cmd.command_name = prog;
2582 if (eargp->use_shell) {
2583 static const char posix_sh_cmds[][9] = {
2642 if (*p ==
' ' || *p ==
'\t') {
2643 if (first.ptr && !first.len) first.len = p - first.ptr;
2646 if (!first.ptr) first.ptr = p;
2648 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2654 else if (*p ==
'/') {
2661 if (!has_meta && first.ptr) {
2662 if (!first.len) first.len = p - first.ptr;
2663 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2664 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2669 eargp->use_shell = 0;
2671 if (!eargp->use_shell) {
2676 while (*p ==
' ' || *p ==
'\t')
2680 while (*p && *p !=
' ' && *p !=
'\t')
2686 eargp->invoke.cmd.argv_buf = argv_buf;
2687 eargp->invoke.cmd.command_name =
2689 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2694 if (!eargp->use_shell) {
2695 const char *abspath;
2696 const char *path_env = 0;
2698 abspath = dln_find_exe_r(
RSTRING_PTR(eargp->invoke.cmd.command_name),
2699 path_env, fbuf,
sizeof(fbuf));
2703 eargp->invoke.cmd.command_abspath =
Qnil;
2706 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2711 for (i = 0; i < argc; i++) {
2712 VALUE arg = argv[i];
2714 #ifdef DEFAULT_PROCESS_ENCODING
2715 arg = EXPORT_STR(arg);
2720 eargp->invoke.cmd.argv_buf = argv_buf;
2723 if (!eargp->use_shell) {
2724 const char *p, *ep, *
null=NULL;
2735 eargp->invoke.cmd.argv_str =
2736 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(argv_str);
2742 rb_execarg_get(
VALUE execarg_obj)
2750 rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2752 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2758 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2759 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2761 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2767 rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2772 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2773 if (!allow_exc_opt && eargp->exception_given) {
2780 rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2782 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2783 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2784 eargp->env_modification = env;
2789 fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2804 static long run_exec_dup2_tmpbuf_size(
long n);
2815 open_func(
void *
ptr)
2819 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2825 rb_execarg_allocate_dup2_tmpbuf(
struct rb_execarg *eargp,
long len)
2827 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2828 rb_imemo_tmpbuf_set_ptr(tmpbuf,
ruby_xmalloc(run_exec_dup2_tmpbuf_size(
len)));
2829 eargp->dup2_tmpbuf = tmpbuf;
2833 rb_execarg_parent_start1(
VALUE execarg_obj)
2835 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2836 int unsetenv_others;
2840 ary = eargp->fd_open;
2855 open_data.fname = vpath;
2856 open_data.oflags = flags;
2857 open_data.perm = perm;
2859 open_data.err = EINTR;
2861 if (open_data.ret == -1) {
2862 if (open_data.err == EINTR) {
2868 fd2 = open_data.ret;
2880 eargp->redirect_fds = check_exec_fds(eargp);
2882 ary = eargp->fd_dup2;
2884 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2887 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2888 envopts = eargp->env_modification;
2889 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2890 VALUE envtbl, envp_str, envp_buf;
2892 if (unsetenv_others) {
2896 envtbl = rb_env_to_hash();
2900 st_table *stenv = RHASH_TBL_RAW(envtbl);
2907 st_data_t stkey = (st_data_t)key;
2908 st_delete(stenv, &stkey, NULL);
2911 st_insert(stenv, (st_data_t)key, (st_data_t)val);
2919 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
2931 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(envp_str);
2932 eargp->envp_buf = envp_buf;
2948 rb_execarg_parent_start(
VALUE execarg_obj)
2951 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2953 rb_execarg_parent_end(execarg_obj);
2959 execarg_parent_end(
VALUE execarg_obj)
2961 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2965 ary = eargp->fd_open;
2976 parent_redirect_close(fd2);
2988 rb_execarg_parent_end(
VALUE execarg_obj)
2990 execarg_parent_end(execarg_obj);
2995 rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
2997 if (!errmsg || !*errmsg)
return;
2998 if (strcmp(errmsg,
"chdir") == 0) {
3006 rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
3008 if (!errmsg || !*errmsg)
return;
3009 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
3017 VALUE execarg_obj, fail_str;
3019 #define CHILD_ERRMSG_BUFLEN 80
3020 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
3023 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
3024 eargp = rb_execarg_get(execarg_obj);
3027 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
3029 execarg_parent_end(execarg_obj);
3034 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
3036 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
3039 rb_exec_fail(eargp, err, errmsg);
3143 #define ERRMSG(str) \
3144 ((errmsg && 0 < errmsg_buflen) ? \
3145 (void)strlcpy(errmsg, (str), errmsg_buflen) : (void)0)
3147 #define ERRMSG_FMT(...) \
3148 ((errmsg && 0 < errmsg_buflen) ? \
3149 (void)snprintf(errmsg, errmsg_buflen, __VA_ARGS__) : (void)0)
3151 static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3152 static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3153 static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3156 save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3159 VALUE newary, redirection;
3160 int save_fd = redirect_cloexec_dup(fd), cloexec;
3161 if (save_fd == -1) {
3168 newary = sargp->fd_dup2;
3171 sargp->fd_dup2 = newary;
3173 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3178 newary = sargp->fd_close;
3181 sargp->fd_close = newary;
3190 intcmp(
const void *a,
const void *b)
3192 return *(
int*)a - *(
int*)b;
3196 intrcmp(
const void *a,
const void *b)
3198 return *(
int*)b - *(
int*)a;
3210 run_exec_dup2_tmpbuf_size(
long n)
3217 fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3221 ret = fcntl(fd, F_GETFD);
3223 ERRMSG(
"fcntl(F_GETFD)");
3226 if (ret & FD_CLOEXEC)
return 1;
3233 fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3237 ret = fcntl(fd, F_GETFD);
3239 ERRMSG(
"fcntl(F_GETFD)");
3242 if (!(ret & FD_CLOEXEC)) {
3244 ret = fcntl(fd, F_SETFD, ret);
3246 ERRMSG(
"fcntl(F_SETFD)");
3256 fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3260 ret = fcntl(fd, F_GETFD);
3262 ERRMSG(
"fcntl(F_GETFD)");
3265 if (ret & FD_CLOEXEC) {
3267 ret = fcntl(fd, F_SETFD, ret);
3269 ERRMSG(
"fcntl(F_SETFD)");
3279 run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3290 for (i = 0; i < n; i++) {
3295 pairs[i].older_index = -1;
3305 for (i = 0; i < n; i++) {
3306 int newfd = pairs[i].newfd;
3310 pairs[i].num_newer = 0;
3312 while (pairs < found && (found-1)->oldfd == newfd)
3314 while (found < pairs+n && found->oldfd == newfd) {
3315 pairs[i].num_newer++;
3316 found->older_index = i;
3323 for (i = 0; i < n; i++) {
3325 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3326 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3328 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3333 if (pairs[j].cloexec &&
3334 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3338 pairs[j].oldfd = -1;
3339 j = pairs[j].older_index;
3341 pairs[j].num_newer--;
3346 for (i = 0; i < n; i++) {
3348 if (pairs[i].oldfd == -1)
3350 if (pairs[i].oldfd == pairs[i].newfd) {
3351 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3353 pairs[i].oldfd = -1;
3356 if (extra_fd == -1) {
3357 extra_fd = redirect_dup(pairs[i].oldfd);
3358 if (extra_fd == -1) {
3364 if (fd_get_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen)) {
3365 if (fd_set_cloexec(extra_fd, errmsg, errmsg_buflen)) {
3372 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3379 pairs[i].oldfd = extra_fd;
3380 j = pairs[i].older_index;
3381 pairs[i].older_index = -1;
3383 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3389 pairs[j].oldfd = -1;
3390 j = pairs[j].older_index;
3393 if (extra_fd != -1) {
3394 ret = redirect_close(extra_fd);
3409 run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3417 ret = redirect_close(fd);
3428 run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3438 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3440 ret = redirect_dup2(oldfd, newfd);
3453 run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3465 pgroup = eargp->pgroup_pgid;
3471 sargp->pgroup_given = 1;
3472 sargp->pgroup_pgid = getpgrp();
3478 ret = setpgid(getpid(), pgroup);
3479 if (ret == -1) ERRMSG(
"setpgid");
3484 #if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3487 run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3496 if (getrlimit(rtype, &rlim) == -1) {
3497 ERRMSG(
"getrlimit");
3501 RLIM2NUM(rlim.rlim_cur),
3502 RLIM2NUM(rlim.rlim_max)));
3503 if (sargp->rlimit_limits ==
Qfalse)
3504 newary = sargp->rlimit_limits = hide_obj(
rb_ary_new());
3506 newary = sargp->rlimit_limits;
3511 if (setrlimit(rtype, &rlim) == -1) {
3512 ERRMSG(
"setrlimit");
3520 #if !defined(HAVE_WORKING_FORK)
3533 if (sargp->env_modification ==
Qfalse) {
3534 VALUE env = rb_envtbl();
3539 sargp->env_modification = ary;
3541 sargp->unsetenv_others_given = 1;
3542 sargp->unsetenv_others_do = 1;
3549 #define chdir(p) rb_w32_uchdir(p)
3554 rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3561 sargp->redirect_fds =
Qnil;
3565 if (eargp->pgroup_given) {
3566 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3571 #if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3572 obj = eargp->rlimit_limits;
3574 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3579 #if !defined(HAVE_WORKING_FORK)
3580 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3585 obj = eargp->env_modification;
3601 if (eargp->umask_given) {
3602 mode_t mask = eargp->umask_mask;
3603 mode_t oldmask = umask(mask);
3605 sargp->umask_given = 1;
3606 sargp->umask_mask = oldmask;
3610 obj = eargp->fd_dup2;
3612 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3616 obj = eargp->fd_close;
3619 rb_warn(
"cannot close fd before spawn");
3621 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3626 #ifdef HAVE_WORKING_FORK
3627 if (eargp->close_others_do) {
3632 obj = eargp->fd_dup2_child;
3634 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3638 if (eargp->chdir_given) {
3640 sargp->chdir_given = 1;
3641 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3650 if (eargp->gid_given) {
3651 if (setgid(eargp->gid) < 0) {
3658 if (eargp->uid_given) {
3659 if (setuid(eargp->uid) < 0) {
3667 VALUE ary = sargp->fd_dup2;
3669 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3673 int preserve =
errno;
3674 stdfd_clear_nonblock();
3683 rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3685 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3690 exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3692 #if !defined(HAVE_WORKING_FORK)
3693 struct rb_execarg sarg, *
const sargp = &sarg;
3699 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3703 if (eargp->use_shell) {
3704 err = proc_exec_sh(
RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3707 char *abspath = NULL;
3708 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3709 abspath =
RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3710 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3712 #if !defined(HAVE_WORKING_FORK)
3713 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3719 #ifdef HAVE_WORKING_FORK
3722 rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3724 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3728 proc_syswait(
VALUE pid)
3735 move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3739 for (i = 0; i < n; i++) {
3758 pipe_nocrash(
int filedes[2],
VALUE fds)
3766 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3781 rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3788 handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3800 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3803 if (!status && !ep) {
3808 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3809 if (status) status->status = state;
3810 if (!state)
return 0;
3823 #define prefork() ( \
3824 rb_io_flush(rb_stdout), \
3825 rb_io_flush(rb_stderr) \
3855 write_retry(
int fd,
const void *buf,
size_t len)
3860 w = write(fd, buf,
len);
3861 }
while (w < 0 &&
errno == EINTR);
3867 read_retry(
int fd,
void *buf,
size_t len)
3871 if (set_blocking(fd) != 0) {
3873 rb_async_bug_errno(
"set_blocking failed reading child error",
errno);
3878 r = read(fd, buf,
len);
3879 }
while (r < 0 &&
errno == EINTR);
3885 send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3890 if (write_retry(fd, &err,
sizeof(err)) < 0) err =
errno;
3891 if (errmsg && 0 < errmsg_buflen) {
3892 errmsg[errmsg_buflen-1] =
'\0';
3893 errmsg_buflen = strlen(errmsg);
3894 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3900 recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3904 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3908 if (size ==
sizeof(err) &&
3909 errmsg && 0 < errmsg_buflen) {
3910 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3919 #ifdef HAVE_WORKING_VFORK
3920 #if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3923 getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3929 ret = getuidx(ID_SAVED);
3930 if (ret == (rb_uid_t)-1)
3935 #define HAVE_GETRESUID
3938 #if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
3941 getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
3947 ret = getgidx(ID_SAVED);
3948 if (ret == (rb_gid_t)-1)
3953 #define HAVE_GETRESGID
3971 rb_uid_t ruid, euid;
3972 rb_gid_t rgid, egid;
3974 #if defined HAVE_ISSETUGID
3979 #ifdef HAVE_GETRESUID
3983 ret = getresuid(&ruid, &euid, &suid);
3994 if (euid == 0 || euid != ruid)
3997 #ifdef HAVE_GETRESGID
4001 ret = getresgid(&rgid, &egid, &sgid);
4019 struct child_handler_disabler_state
4025 disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
4027 #ifdef HAVE_PTHREAD_SIGMASK
4031 ret = sigfillset(&all);
4035 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
4040 # pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4045 disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
4047 #ifdef HAVE_PTHREAD_SIGMASK
4050 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
4055 # pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4061 disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
4066 for (sig = 1; sig < NSIG; sig++) {
4067 sig_t handler = signal(sig, SIG_DFL);
4069 if (handler == SIG_ERR &&
errno == EINVAL) {
4072 if (handler == SIG_ERR) {
4073 ERRMSG(
"signal to obtain old action");
4077 if (sig == SIGPIPE) {
4082 if (handler == SIG_IGN) {
4083 signal(sig, SIG_IGN);
4088 sigemptyset(&old->sigmask);
4089 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
4091 ERRMSG(
"sigprocmask");
4099 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4100 char *errmsg,
size_t errmsg_buflen,
4104 volatile int try_gc = 1;
4105 struct child_handler_disabler_state old;
4110 disable_child_handler_before_fork(&old);
4111 #ifdef HAVE_WORKING_VFORK
4112 if (!has_privilege())
4122 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
4124 ret = chfunc(charg, errmsg, errmsg_buflen);
4125 if (!ret) _exit(EXIT_SUCCESS);
4127 send_child_error(ep[1], errmsg, errmsg_buflen);
4128 #if EXIT_SUCCESS == 127
4129 _exit(EXIT_FAILURE);
4135 disable_child_handler_fork_parent(&old);
4139 if (handle_fork_error(err, status, ep, &try_gc))
4145 fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4146 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4154 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4156 if (status) status->status = 0;
4158 if (pipe_nocrash(ep, fds))
return -1;
4160 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4162 if (status) status->pid = pid;
4165 if (status) status->error =
errno;
4172 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4174 if (error_occurred) {
4177 status->error = err;
4179 VM_ASSERT((w == 0) &&
"only used by extensions");
4182 status->status = state;
4203 rb_fork_async_signal_safe(
int *status,
4204 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4205 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4209 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4212 *status = process_status.status;
4219 rb_fork_ruby(
int *status)
4223 int try_gc = 1, err;
4224 struct child_handler_disabler_state old;
4230 rb_thread_acquire_fork_lock();
4231 disable_child_handler_before_fork(&old);
4233 child.pid = pid = rb_fork();
4234 child.error = err =
errno;
4236 disable_child_handler_fork_parent(&old);
4238 #
if defined(__FreeBSD__)
4242 rb_thread_release_fork_lock();
4245 rb_thread_reset_fork_lock();
4247 after_fork_ruby(pid);
4250 }
while (pid < 0 && handle_fork_error(err, &child, NULL, &try_gc) == 0);
4252 if (status) *status = child.status;
4260 rb_pid_t pid = rb_fork_ruby(NULL);
4270 rb_call_proc__fork(
void)
4275 return proc_fork_pid();
4284 #if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4305 rb_proc__fork(
VALUE _obj)
4307 rb_pid_t pid = proc_fork_pid();
4372 rb_f_fork(
VALUE obj)
4376 pid = rb_call_proc__fork();
4390 #define rb_proc__fork rb_f_notimplement
4391 #define rb_f_fork rb_f_notimplement
4395 exit_status_code(
VALUE status)
4401 istatus = EXIT_SUCCESS;
4404 istatus = EXIT_FAILURE;
4408 #if EXIT_SUCCESS != 0
4410 istatus = EXIT_SUCCESS;
4417 NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4435 rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4440 istatus = exit_status_code(argv[0]);
4443 istatus = EXIT_FAILURE;
4453 if (GET_EC()->tag) {
4469 istatus = exit_status_code(argv[0]);
4472 istatus = EXIT_SUCCESS;
4538 VALUE errinfo = rb_ec_get_errinfo(ec);
4539 if (!
NIL_P(errinfo)) {
4540 rb_ec_error_print(ec, errinfo);
4547 args[1] = args[0] = argv[0];
4550 args[0] =
INT2NUM(EXIT_FAILURE);
4588 #if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4593 if (eargp && !eargp->use_shell) {
4594 VALUE str = eargp->invoke.cmd.argv_str;
4595 VALUE buf = eargp->invoke.cmd.argv_buf;
4596 char *p, **argv = ARGVSTR2ARGV(str);
4597 long i, argc = ARGVSTR2ARGC(str);
4601 for (i = 1; i < argc; ++i) {
4602 p[argv[i] - start - 1] =
' ';
4612 rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4615 #if !defined HAVE_WORKING_FORK || USE_SPAWNV
4618 # if !defined HAVE_SPAWNV
4623 #if defined HAVE_WORKING_FORK && !USE_SPAWNV
4624 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4626 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4628 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4632 if (prog && !eargp->use_shell) {
4633 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4636 # if defined HAVE_SPAWNV
4637 if (eargp->use_shell) {
4641 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4642 pid = proc_spawn_cmd(argv, prog, eargp);
4649 status = system(rb_execarg_commandline(eargp, &prog));
4654 if (eargp->waitpid_state) {
4655 eargp->waitpid_state->pid = pid;
4658 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4673 do_spawn_process(
VALUE arg)
4677 rb_execarg_parent_start1(argp->execarg);
4679 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4680 argp->errmsg.ptr, argp->errmsg.buflen);
4683 NOINLINE(
static rb_pid_t
4684 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4687 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4691 args.execarg = execarg_obj;
4692 args.errmsg.ptr = errmsg;
4693 args.errmsg.buflen = errmsg_buflen;
4696 execarg_parent_end, execarg_obj);
4701 rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4705 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4706 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4712 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4718 return rb_spawn_internal(argc, argv, NULL, 0);
4843 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4844 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4847 eargp->status = &status;
4849 last_status_clear(th);
4853 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4860 th->last_status = status;
4862 if (data->status == EXIT_SUCCESS) {
4866 if (data->error != 0) {
4867 if (eargp->exception) {
4868 VALUE command = eargp->invoke.sh.shell_script;
4876 else if (eargp->exception) {
4877 VALUE command = eargp->invoke.sh.shell_script;
4891 if (eargp->exception) {
4892 VALUE command = eargp->invoke.sh.shell_script;
5008 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
5009 VALUE execarg_obj, fail_str;
5012 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
5013 eargp = rb_execarg_get(execarg_obj);
5014 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
5016 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
5020 rb_exec_fail(eargp, err, errmsg);
5024 #if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
5050 time_t beg = time(0);
5053 if (scheduler !=
Qnil) {
5057 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
5066 time_t end = time(0) - beg;
5068 return TIMET2NUM(end);
5072 #if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
5089 #if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
5100 #define proc_getpgrp rb_f_notimplement
5104 #if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
5123 #elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
5129 #define proc_setpgrp rb_f_notimplement
5133 #if defined(HAVE_GETPGID)
5155 #define proc_getpgid rb_f_notimplement
5173 rb_pid_t ipid, ipgrp;
5182 #define proc_setpgid rb_f_notimplement
5214 #define proc_getsid rb_f_notimplement
5218 #if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5219 #if !defined(HAVE_SETSID)
5220 static rb_pid_t ruby_setsid(
void);
5221 #define setsid() ruby_setsid()
5246 #if !defined(HAVE_SETSID)
5247 #define HAVE_SETSID 1
5255 #if defined(SETPGRP_VOID)
5261 ret = setpgrp(0, pid);
5263 if (ret == -1)
return -1;
5267 ioctl(fd, TIOCNOTTY, NULL);
5274 #define proc_setsid rb_f_notimplement
5278 #ifdef HAVE_GETPRIORITY
5306 int prio, iwhich, iwho;
5312 prio = getpriority(iwhich, iwho);
5317 #define proc_getpriority rb_f_notimplement
5321 #ifdef HAVE_GETPRIORITY
5341 int iwhich, iwho, iprio;
5347 if (setpriority(iwhich, iwho, iprio) < 0)
5352 #define proc_setpriority rb_f_notimplement
5355 #if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5357 rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5361 #define RESCHECK(r) \
5363 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5364 resource = RLIMIT_##r; \
5398 #ifdef RLIMIT_MEMLOCK
5401 #ifdef RLIMIT_MSGQUEUE
5407 #ifdef RLIMIT_NOFILE
5425 #ifdef RLIMIT_RTPRIO
5428 #ifdef RLIMIT_RTTIME
5437 #ifdef RLIMIT_SBSIZE
5440 #ifdef RLIMIT_SIGPENDING
5441 RESCHECK(SIGPENDING);
5450 for (p = name; *p; p++)
5456 for (p = name; *p; p++)
5462 rb_bug(
"unexpected casetype");
5469 rlimit_type_by_hname(
const char *name,
long len)
5471 return rlimit_resource_name2int(name,
len, 0);
5475 rlimit_type_by_lname(
const char *name,
long len)
5477 return rlimit_resource_name2int(name,
len, 1);
5481 rlimit_type_by_sym(
VALUE key)
5487 static const char prefix[] =
"rlimit_";
5488 enum {prefix_len =
sizeof(prefix)-1};
5490 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5491 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5499 rlimit_resource_type(
VALUE rtype)
5506 switch (
TYPE(rtype)) {
5529 r = rlimit_type_by_hname(name,
len);
5539 rlimit_resource_value(
VALUE rval)
5544 switch (
TYPE(rval)) {
5562 return NUM2RLIM(rval);
5565 #ifdef RLIM_INFINITY
5566 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5568 #ifdef RLIM_SAVED_MAX
5569 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5571 #ifdef RLIM_SAVED_CUR
5572 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5580 #if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5608 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5611 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5614 #define proc_getrlimit rb_f_notimplement
5617 #if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5671 proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5673 VALUE resource, rlim_cur, rlim_max;
5679 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5680 rlim_max = rlim_cur;
5682 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5683 rlim.rlim_max = rlimit_resource_value(rlim_max);
5685 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5691 #define proc_setrlimit rb_f_notimplement
5694 static int under_uid_switch = 0;
5696 check_uid_switch(
void)
5698 if (under_uid_switch) {
5703 static int under_gid_switch = 0;
5705 check_gid_switch(
void)
5707 if (under_gid_switch) {
5713 #if defined(HAVE_PWD_H)
5715 login_not_found(
int err)
5717 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5728 # if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5731 char MAYBE_UNUSED(*login) = NULL;
5733 # ifdef USE_GETLOGIN_R
5735 # if defined(__FreeBSD__)
5736 typedef int getlogin_r_size_t;
5738 typedef size_t getlogin_r_size_t;
5741 long loginsize = GETLOGIN_R_SIZE_INIT;
5744 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5753 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5754 if (login_not_found(gle)) {
5759 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5769 if (login == NULL) {
5775 return maybe_result;
5777 # elif defined(USE_GETLOGIN)
5783 if (login_not_found(err)) {
5797 pwd_not_found(
int err)
5811 # if defined(USE_GETPWNAM_R)
5812 struct getpwnam_r_args {
5816 struct passwd *result;
5817 struct passwd pwstore;
5820 # define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5821 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5824 nogvl_getpwnam_r(
void *args)
5826 struct getpwnam_r_args *arg = args;
5827 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5832 rb_getpwdirnam_for_login(
VALUE login_name)
5834 #if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5838 if (
NIL_P(login_name)) {
5846 # ifdef USE_GETPWNAM_R
5849 long bufsizenm = GETPW_R_SIZE_INIT;
5852 bufsizenm = GETPW_R_SIZE_DEFAULT;
5859 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5862 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5863 if (pwd_not_found(enm)) {
5868 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5878 if (args.result == NULL) {
5889 # elif defined(USE_GETPWNAM)
5891 struct passwd *pwptr;
5893 if (!(pwptr = getpwnam(login))) {
5896 if (pwd_not_found(err)) {
5910 # if defined(USE_GETPWUID_R)
5911 struct getpwuid_r_args {
5915 struct passwd *result;
5916 struct passwd pwstore;
5919 # define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5920 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5923 nogvl_getpwuid_r(
void *args)
5925 struct getpwuid_r_args *arg = args;
5926 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5934 rb_getpwdiruid(
void)
5936 # if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5940 uid_t ruid = getuid();
5942 # ifdef USE_GETPWUID_R
5945 long bufsizeid = GETPW_R_SIZE_INIT;
5948 bufsizeid = GETPW_R_SIZE_DEFAULT;
5955 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5958 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5959 if (pwd_not_found(eid)) {
5964 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5974 if (args.result == NULL) {
5985 # elif defined(USE_GETPWUID)
5987 struct passwd *pwptr;
5989 if (!(pwptr = getpwuid(ruid))) {
5992 if (pwd_not_found(err)) {
6018 #if defined(HAVE_PWD_H)
6021 # ifdef USE_GETPWNAM_R
6034 struct passwd *pwptr;
6035 #ifdef USE_GETPWNAM_R
6040 getpw_buf_len = GETPW_R_SIZE_INIT;
6041 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
6048 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
6050 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
6051 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
6059 pwptr = args.result;
6061 pwptr = getpwnam(usrname);
6064 #ifndef USE_GETPWNAM_R
6069 uid = pwptr->pw_uid;
6070 #ifndef USE_GETPWNAM_R
6077 # ifdef p_uid_from_name
6097 #if defined(HAVE_GRP_H)
6098 # if defined(USE_GETGRNAM_R)
6099 struct getgrnam_r_args {
6103 struct group *result;
6107 # define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
6108 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
6111 nogvl_getgrnam_r(
void *args)
6113 struct getgrnam_r_args *arg = args;
6114 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
6120 # ifdef USE_GETGRNAM_R
6133 struct group *grptr;
6134 #ifdef USE_GETGRNAM_R
6139 getgr_buf_len = GETGR_R_SIZE_INIT;
6140 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6147 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6149 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6150 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6158 grptr = args.result;
6159 #elif defined(HAVE_GETGRNAM)
6160 grptr = getgrnam(grpname);
6165 #if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6170 gid = grptr->gr_gid;
6171 #if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6178 # ifdef p_gid_from_name
6198 #if defined HAVE_SETUID
6216 #define p_sys_setuid rb_f_notimplement
6220 #if defined HAVE_SETRUID
6238 #define p_sys_setruid rb_f_notimplement
6242 #if defined HAVE_SETEUID
6260 #define p_sys_seteuid rb_f_notimplement
6264 #if defined HAVE_SETREUID
6279 rb_uid_t ruid, euid;
6282 ruid = OBJ2UID1(rid);
6283 euid = OBJ2UID1(eid);
6289 #define p_sys_setreuid rb_f_notimplement
6293 #if defined HAVE_SETRESUID
6308 rb_uid_t ruid, euid, suid;
6311 ruid = OBJ2UID1(rid);
6312 euid = OBJ2UID1(eid);
6313 suid = OBJ2UID1(sid);
6315 if (setresuid(ruid, euid, suid) != 0)
rb_sys_fail(0);
6319 #define p_sys_setresuid rb_f_notimplement
6336 proc_getuid(
VALUE obj)
6338 rb_uid_t uid = getuid();
6343 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6363 #if defined(HAVE_SETRESUID)
6365 #elif defined HAVE_SETREUID
6367 #elif defined HAVE_SETRUID
6369 #elif defined HAVE_SETUID
6371 if (geteuid() == uid) {
6382 #define proc_setuid rb_f_notimplement
6396 static rb_uid_t SAVED_USER_ID = -1;
6398 #ifdef BROKEN_SETREUID
6400 setreuid(rb_uid_t ruid, rb_uid_t euid)
6402 if (ruid != (rb_uid_t)-1 && ruid != getuid()) {
6403 if (euid == (rb_uid_t)-1) euid = geteuid();
6404 if (setuid(ruid) < 0)
return -1;
6406 if (euid != (rb_uid_t)-1 && euid != geteuid()) {
6407 if (seteuid(euid) < 0)
return -1;
6435 if (geteuid() == 0) {
6436 #if defined(HAVE_SETRESUID)
6438 SAVED_USER_ID = uid;
6439 #elif defined(HAVE_SETUID)
6441 SAVED_USER_ID = uid;
6442 #elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6443 if (getuid() == uid) {
6444 if (SAVED_USER_ID == uid) {
6449 if (setreuid(-1, SAVED_USER_ID) < 0)
rb_sys_fail(0);
6450 if (setreuid(SAVED_USER_ID, 0) < 0)
rb_sys_fail(0);
6453 SAVED_USER_ID = uid;
6459 SAVED_USER_ID = uid;
6465 SAVED_USER_ID = uid;
6467 #elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6468 if (getuid() == uid) {
6469 if (SAVED_USER_ID == uid) {
6483 SAVED_USER_ID = uid;
6490 SAVED_USER_ID = uid;
6498 #if defined(HAVE_SETRESUID)
6499 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6500 (geteuid() == uid)? (rb_uid_t)-1: uid,
6501 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0)
rb_sys_fail(0);
6502 SAVED_USER_ID = uid;
6503 #elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6504 if (SAVED_USER_ID == uid) {
6505 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6506 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6509 else if (getuid() != uid) {
6510 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6512 SAVED_USER_ID = uid;
6514 else if ( geteuid() != uid) {
6516 SAVED_USER_ID = uid;
6520 if (setreuid(-1, SAVED_USER_ID) < 0)
rb_sys_fail(0);
6521 if (setreuid(SAVED_USER_ID, uid) < 0)
rb_sys_fail(0);
6522 SAVED_USER_ID = uid;
6525 #elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6526 if (SAVED_USER_ID == uid) {
6527 if (geteuid() != uid && seteuid(uid) < 0)
rb_sys_fail(0);
6528 if (getuid() != uid && setruid(uid) < 0)
rb_sys_fail(0);
6530 else if ( geteuid() == uid) {
6531 if (getuid() != uid) {
6533 SAVED_USER_ID = uid;
6537 SAVED_USER_ID = uid;
6541 else if ( getuid() == uid) {
6544 SAVED_USER_ID = uid;
6550 #elif defined HAVE_44BSD_SETUID
6551 if (getuid() == uid) {
6554 SAVED_USER_ID = uid;
6559 #elif defined HAVE_SETEUID
6560 if (getuid() == uid && SAVED_USER_ID == uid) {
6566 #elif defined HAVE_SETUID
6567 if (getuid() == uid && SAVED_USER_ID == uid) {
6582 #if defined HAVE_SETGID
6600 #define p_sys_setgid rb_f_notimplement
6604 #if defined HAVE_SETRGID
6622 #define p_sys_setrgid rb_f_notimplement
6626 #if defined HAVE_SETEGID
6644 #define p_sys_setegid rb_f_notimplement
6648 #if defined HAVE_SETREGID
6663 rb_gid_t rgid, egid;
6665 rgid = OBJ2GID(rid);
6666 egid = OBJ2GID(eid);
6671 #define p_sys_setregid rb_f_notimplement
6674 #if defined HAVE_SETRESGID
6689 rb_gid_t rgid, egid, sgid;
6691 rgid = OBJ2GID(rid);
6692 egid = OBJ2GID(eid);
6693 sgid = OBJ2GID(sid);
6694 if (setresgid(rgid, egid, sgid) != 0)
rb_sys_fail(0);
6698 #define p_sys_setresgid rb_f_notimplement
6702 #if defined HAVE_ISSETUGID
6716 p_sys_issetugid(
VALUE obj)
6718 return RBOOL(issetugid());
6721 #define p_sys_issetugid rb_f_notimplement
6738 proc_getgid(
VALUE obj)
6740 rb_gid_t gid = getgid();
6745 #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6764 #if defined(HAVE_SETRESGID)
6766 #elif defined HAVE_SETREGID
6768 #elif defined HAVE_SETRGID
6770 #elif defined HAVE_SETGID
6772 if (getegid() == gid) {
6783 #define proc_setgid rb_f_notimplement
6787 #if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6807 static int _maxgroups = -1;
6809 get_sc_ngroups_max(
void)
6811 #ifdef _SC_NGROUPS_MAX
6812 return (
int)sysconf(_SC_NGROUPS_MAX);
6813 #elif defined(NGROUPS_MAX)
6814 return (
int)NGROUPS_MAX;
6822 if (_maxgroups < 0) {
6823 _maxgroups = get_sc_ngroups_max();
6825 _maxgroups = RB_MAX_GROUPS;
6834 #ifdef HAVE_GETGROUPS
6858 proc_getgroups(
VALUE obj)
6864 ngroups = getgroups(0, NULL);
6868 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6870 ngroups = getgroups(ngroups, groups);
6875 for (i = 0; i < ngroups; i++)
6883 #define proc_getgroups rb_f_notimplement
6887 #ifdef HAVE_SETGROUPS
6912 if (ngroups > maxgroups())
6915 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6917 for (i = 0; i < ngroups; i++) {
6920 groups[i] = OBJ2GID1(g);
6924 if (setgroups(ngroups, groups) == -1)
6929 return proc_getgroups(obj);
6932 #define proc_setgroups rb_f_notimplement
6936 #ifdef HAVE_INITGROUPS
6962 return proc_getgroups(obj);
6965 #define proc_initgroups rb_f_notimplement
6968 #if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6981 proc_getmaxgroups(
VALUE obj)
6986 #define proc_getmaxgroups rb_f_notimplement
6989 #ifdef HAVE_SETGROUPS
7002 int ngroups_max = get_sc_ngroups_max();
7007 if (ngroups > RB_MAX_GROUPS)
7008 ngroups = RB_MAX_GROUPS;
7010 if (ngroups_max > 0 && ngroups > ngroups_max)
7011 ngroups = ngroups_max;
7013 _maxgroups = ngroups;
7018 #define proc_setmaxgroups rb_f_notimplement
7021 #if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
7022 static int rb_daemon(
int nochdir,
int noclose);
7047 int n, nochdir = FALSE, noclose = FALSE;
7050 case 2: noclose = TO_BOOL(argv[1],
"noclose");
7051 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
7055 n = rb_daemon(nochdir, noclose);
7060 extern const char ruby_null_device[];
7063 rb_daemon(
int nochdir,
int noclose)
7068 err = daemon(nochdir, noclose);
7073 switch (rb_fork_ruby(NULL)) {
7076 default: _exit(EXIT_SUCCESS);
7080 if (setsid() < 0) (void)0;
7085 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
7097 #define proc_daemon rb_f_notimplement
7110 static rb_gid_t SAVED_GROUP_ID = -1;
7112 #ifdef BROKEN_SETREGID
7114 setregid(rb_gid_t rgid, rb_gid_t egid)
7116 if (rgid != (rb_gid_t)-1 && rgid != getgid()) {
7117 if (egid == (rb_gid_t)-1) egid = getegid();
7118 if (setgid(rgid) < 0)
return -1;
7120 if (egid != (rb_gid_t)-1 && egid != getegid()) {
7121 if (setegid(egid) < 0)
return -1;
7149 if (geteuid() == 0) {
7150 #if defined(HAVE_SETRESGID)
7152 SAVED_GROUP_ID = gid;
7153 #elif defined HAVE_SETGID
7155 SAVED_GROUP_ID = gid;
7156 #elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7157 if (getgid() == gid) {
7158 if (SAVED_GROUP_ID == gid) {
7163 if (setregid(-1, SAVED_GROUP_ID) < 0)
rb_sys_fail(0);
7164 if (setregid(SAVED_GROUP_ID, 0) < 0)
rb_sys_fail(0);
7167 SAVED_GROUP_ID = gid;
7173 SAVED_GROUP_ID = gid;
7179 SAVED_GROUP_ID = gid;
7181 #elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7182 if (getgid() == gid) {
7183 if (SAVED_GROUP_ID == gid) {
7198 SAVED_GROUP_ID = gid;
7205 SAVED_GROUP_ID = gid;
7212 #if defined(HAVE_SETRESGID)
7213 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7214 (getegid() == gid)? (rb_gid_t)-1: gid,
7215 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0)
rb_sys_fail(0);
7216 SAVED_GROUP_ID = gid;
7217 #elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7218 if (SAVED_GROUP_ID == gid) {
7219 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7220 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7223 else if (getgid() != gid) {
7224 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7226 SAVED_GROUP_ID = gid;
7228 else if ( getegid() != gid) {
7230 SAVED_GROUP_ID = gid;
7234 if (setregid(-1, SAVED_GROUP_ID) < 0)
rb_sys_fail(0);
7235 if (setregid(SAVED_GROUP_ID, gid) < 0)
rb_sys_fail(0);
7236 SAVED_GROUP_ID = gid;
7239 #elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7240 if (SAVED_GROUP_ID == gid) {
7241 if (getegid() != gid && setegid(gid) < 0)
rb_sys_fail(0);
7242 if (getgid() != gid && setrgid(gid) < 0)
rb_sys_fail(0);
7244 else if ( getegid() == gid) {
7245 if (getgid() != gid) {
7247 SAVED_GROUP_ID = gid;
7251 SAVED_GROUP_ID = gid;
7255 else if ( getgid() == gid) {
7258 SAVED_GROUP_ID = gid;
7264 #elif defined HAVE_44BSD_SETGID
7265 if (getgid() == gid) {
7268 SAVED_GROUP_ID = gid;
7273 #elif defined HAVE_SETEGID
7274 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7280 #elif defined HAVE_SETGID
7281 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7309 proc_geteuid(
VALUE obj)
7311 rb_uid_t euid = geteuid();
7315 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7317 proc_seteuid(rb_uid_t uid)
7319 #if defined(HAVE_SETRESUID)
7321 #elif defined HAVE_SETREUID
7323 #elif defined HAVE_SETEUID
7325 #elif defined HAVE_SETUID
7326 if (uid == getuid()) {
7338 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7352 proc_seteuid(OBJ2UID(euid));
7356 #define proc_seteuid_m rb_f_notimplement
7360 rb_seteuid_core(rb_uid_t euid)
7362 #if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7368 #if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7372 #if defined(HAVE_SETRESUID)
7375 SAVED_USER_ID = euid;
7380 #elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7385 SAVED_USER_ID = euid;
7387 #elif defined HAVE_SETEUID
7389 #elif defined HAVE_SETUID
7416 rb_seteuid_core(OBJ2UID(
id));
7435 proc_getegid(
VALUE obj)
7437 rb_gid_t egid = getegid();
7442 #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7455 #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7461 #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7462 gid = OBJ2GID(egid);
7465 #if defined(HAVE_SETRESGID)
7467 #elif defined HAVE_SETREGID
7469 #elif defined HAVE_SETEGID
7471 #elif defined HAVE_SETGID
7472 if (gid == getgid()) {
7485 #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7486 #define proc_setegid_m proc_setegid
7488 #define proc_setegid_m rb_f_notimplement
7492 rb_setegid_core(rb_gid_t egid)
7494 #if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7500 #if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7504 #if defined(HAVE_SETRESGID)
7507 SAVED_GROUP_ID = egid;
7512 #elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7517 SAVED_GROUP_ID = egid;
7519 #elif defined HAVE_SETEGID
7521 #elif defined HAVE_SETGID
7548 rb_setegid_core(OBJ2GID(
id));
7563 p_uid_exchangeable(
VALUE _)
7565 #if defined(HAVE_SETRESUID)
7567 #elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7588 p_uid_exchange(
VALUE obj)
7591 #if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7598 #if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7602 #if defined(HAVE_SETRESUID)
7603 if (setresuid(euid, uid, uid) < 0)
rb_sys_fail(0);
7604 SAVED_USER_ID = uid;
7605 #elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7607 SAVED_USER_ID = uid;
7625 p_gid_exchangeable(
VALUE _)
7627 #if defined(HAVE_SETRESGID)
7629 #elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7650 p_gid_exchange(
VALUE obj)
7653 #if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7660 #if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7664 #if defined(HAVE_SETRESGID)
7665 if (setresgid(egid, gid, gid) < 0)
rb_sys_fail(0);
7666 SAVED_GROUP_ID = gid;
7667 #elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7669 SAVED_GROUP_ID = gid;
7688 p_uid_have_saved_id(
VALUE _)
7690 #if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7698 #if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7700 p_uid_sw_ensure(
VALUE i)
7702 rb_uid_t
id = (rb_uid_t)i;
7703 under_uid_switch = 0;
7704 id = rb_seteuid_core(
id);
7723 p_uid_switch(
VALUE obj)
7735 under_uid_switch = 1;
7742 else if (euid != SAVED_USER_ID) {
7743 proc_seteuid(SAVED_USER_ID);
7745 under_uid_switch = 1;
7760 p_uid_sw_ensure(
VALUE obj)
7762 under_uid_switch = 0;
7763 return p_uid_exchange(obj);
7767 p_uid_switch(
VALUE obj)
7779 p_uid_exchange(obj);
7781 under_uid_switch = 1;
7803 p_gid_have_saved_id(
VALUE _)
7805 #if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7812 #if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7814 p_gid_sw_ensure(
VALUE i)
7816 rb_gid_t
id = (rb_gid_t)i;
7817 under_gid_switch = 0;
7818 id = rb_setegid_core(
id);
7837 p_gid_switch(
VALUE obj)
7849 under_gid_switch = 1;
7856 else if (egid != SAVED_GROUP_ID) {
7857 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7859 under_gid_switch = 1;
7874 p_gid_sw_ensure(
VALUE obj)
7876 under_gid_switch = 0;
7877 return p_gid_exchange(obj);
7881 p_gid_switch(
VALUE obj)
7893 p_gid_exchange(obj);
7895 under_gid_switch = 1;
7905 #if defined(HAVE_TIMES)
7909 #ifdef HAVE__SC_CLK_TCK
7910 return sysconf(_SC_CLK_TCK);
7911 #elif defined CLK_TCK
7936 VALUE utime, stime, cutime, cstime, ret;
7937 #if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7938 struct rusage usage_s, usage_c;
7940 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7942 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7943 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7944 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7945 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7947 const double hertz = (double)get_clk_tck();
7951 utime =
DBL2NUM(buf.tms_utime / hertz);
7952 stime =
DBL2NUM(buf.tms_stime / hertz);
7953 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7954 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7956 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7964 #define rb_proc_times rb_f_notimplement
7967 #ifdef HAVE_LONG_LONG
7969 #define TIMETICK_INT_MIN LLONG_MIN
7970 #define TIMETICK_INT_MAX LLONG_MAX
7971 #define TIMETICK_INT2NUM(v) LL2NUM(v)
7972 #define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7974 typedef long timetick_int_t;
7975 #define TIMETICK_INT_MIN LONG_MIN
7976 #define TIMETICK_INT_MAX LONG_MAX
7977 #define TIMETICK_INT2NUM(v) LONG2NUM(v)
7978 #define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7981 CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7982 static timetick_int_t
7983 gcd_timetick_int(timetick_int_t a, timetick_int_t b)
8003 reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
8005 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
8013 reduce_factors(timetick_int_t *numerators,
int num_numerators,
8014 timetick_int_t *denominators,
int num_denominators)
8017 for (i = 0; i < num_numerators; i++) {
8018 if (numerators[i] == 1)
8020 for (j = 0; j < num_denominators; j++) {
8021 if (denominators[j] == 1)
8023 reduce_fraction(&numerators[i], &denominators[j]);
8029 timetick_int_t giga_count;
8034 timetick2dblnum(
struct timetick *ttp,
8035 timetick_int_t *numerators,
int num_numerators,
8036 timetick_int_t *denominators,
int num_denominators)
8041 reduce_factors(numerators, num_numerators,
8042 denominators, num_denominators);
8044 d = ttp->giga_count * 1e9 + ttp->count;
8046 for (i = 0; i < num_numerators; i++)
8048 for (i = 0; i < num_denominators; i++)
8049 d /= denominators[i];
8055 timetick2dblnum_reciprocal(
struct timetick *ttp,
8056 timetick_int_t *numerators,
int num_numerators,
8057 timetick_int_t *denominators,
int num_denominators)
8062 reduce_factors(numerators, num_numerators,
8063 denominators, num_denominators);
8066 for (i = 0; i < num_denominators; i++)
8067 d *= denominators[i];
8068 for (i = 0; i < num_numerators; i++)
8070 d /= ttp->giga_count * 1e9 + ttp->count;
8075 #define NDIV(x,y) (-(-((x)+1)/(y))-1)
8076 #define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
8079 timetick2integer(
struct timetick *ttp,
8080 timetick_int_t *numerators,
int num_numerators,
8081 timetick_int_t *denominators,
int num_denominators)
8086 reduce_factors(numerators, num_numerators,
8087 denominators, num_denominators);
8089 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
8090 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
8091 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
8092 for (i = 0; i < num_numerators; i++) {
8093 timetick_int_t factor = numerators[i];
8094 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
8098 for (i = 0; i < num_denominators; i++) {
8099 t = DIV(t, denominators[i]);
8101 return TIMETICK_INT2NUM(t);
8105 v = TIMETICK_INT2NUM(ttp->giga_count);
8108 for (i = 0; i < num_numerators; i++) {
8109 timetick_int_t factor = numerators[i];
8112 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
8114 for (i = 0; i < num_denominators; i++) {
8115 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
8121 make_clock_result(
struct timetick *ttp,
8122 timetick_int_t *numerators,
int num_numerators,
8123 timetick_int_t *denominators,
int num_denominators,
8126 if (unit ==
ID2SYM(id_nanosecond)) {
8127 numerators[num_numerators++] = 1000000000;
8128 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8130 else if (unit ==
ID2SYM(id_microsecond)) {
8131 numerators[num_numerators++] = 1000000;
8132 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8134 else if (unit ==
ID2SYM(id_millisecond)) {
8135 numerators[num_numerators++] = 1000;
8136 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8138 else if (unit ==
ID2SYM(id_second)) {
8139 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8141 else if (unit ==
ID2SYM(id_float_microsecond)) {
8142 numerators[num_numerators++] = 1000000;
8143 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8145 else if (unit ==
ID2SYM(id_float_millisecond)) {
8146 numerators[num_numerators++] = 1000;
8147 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8149 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
8150 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8157 static const mach_timebase_info_data_t *
8158 get_mach_timebase_info(
void)
8160 static mach_timebase_info_data_t sTimebaseInfo;
8162 if ( sTimebaseInfo.denom == 0 ) {
8163 (void) mach_timebase_info(&sTimebaseInfo);
8166 return &sTimebaseInfo;
8170 ruby_real_ms_time(
void)
8172 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8173 uint64_t t = mach_absolute_time();
8174 return (
double)t * info->numer / info->denom / 1e6;
8178 #if defined(NUM2CLOCKID)
8179 # define NUMERIC_CLOCKID 1
8181 # define NUMERIC_CLOCKID 0
8182 # define NUM2CLOCKID(x) 0
8185 #define clock_failed(name, err, arg) do { \
8186 int clock_error = (err); \
8187 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8360 timetick_int_t numerators[2];
8361 timetick_int_t denominators[2];
8362 int num_numerators = 0;
8363 int num_denominators = 0;
8366 VALUE clk_id = argv[0];
8367 #ifdef HAVE_CLOCK_GETTIME
8372 #ifdef CLOCK_REALTIME
8373 if (clk_id == RUBY_CLOCK_REALTIME) {
8379 #ifdef CLOCK_MONOTONIC
8380 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8381 c = CLOCK_MONOTONIC;
8386 #ifdef CLOCK_PROCESS_CPUTIME_ID
8387 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8388 c = CLOCK_PROCESS_CPUTIME_ID;
8393 #ifdef CLOCK_THREAD_CPUTIME_ID
8394 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8395 c = CLOCK_THREAD_CPUTIME_ID;
8403 #ifdef HAVE_GETTIMEOFDAY
8408 #define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8409 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8411 ret = gettimeofday(&tv, 0);
8414 tt.giga_count = tv.tv_sec;
8415 tt.count = (int32_t)tv.tv_usec * 1000;
8416 denominators[num_denominators++] = 1000000000;
8421 #define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8422 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8425 if (t == (time_t)-1)
8429 denominators[num_denominators++] = 1000000000;
8434 #define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8435 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8436 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8439 unsigned_clock_t uc;
8441 if (c == (clock_t)-1)
8443 uc = (unsigned_clock_t)c;
8444 tt.count = (int32_t)(uc % 1000000000);
8445 tt.giga_count = (uc / 1000000000);
8446 denominators[num_denominators++] = get_clk_tck();
8452 #define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8453 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8454 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8455 struct rusage usage;
8457 ret = getrusage(RUSAGE_SELF, &usage);
8460 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8461 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8462 if (1000000 <= usec) {
8466 tt.count = usec * 1000;
8467 denominators[num_denominators++] = 1000000000;
8473 #define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8474 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8475 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8477 unsigned_clock_t utime, stime;
8478 if (times(&buf) == (clock_t)-1)
8480 utime = (unsigned_clock_t)buf.tms_utime;
8481 stime = (unsigned_clock_t)buf.tms_stime;
8482 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8483 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8484 if (1000000000 <= tt.count) {
8485 tt.count -= 1000000000;
8488 denominators[num_denominators++] = get_clk_tck();
8493 #define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8494 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8495 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8497 unsigned_clock_t uc;
8500 if (c == (clock_t)-1)
8502 uc = (unsigned_clock_t)c;
8503 tt.count = (int32_t)(uc % 1000000000);
8504 tt.giga_count = uc / 1000000000;
8505 denominators[num_denominators++] = CLOCKS_PER_SEC;
8510 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8511 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8512 uint64_t t = mach_absolute_time();
8513 tt.count = (int32_t)(t % 1000000000);
8514 tt.giga_count = t / 1000000000;
8515 numerators[num_numerators++] = info->numer;
8516 denominators[num_denominators++] = info->denom;
8517 denominators[num_denominators++] = 1000000000;
8522 else if (NUMERIC_CLOCKID) {
8523 #if defined(HAVE_CLOCK_GETTIME)
8525 c = NUM2CLOCKID(clk_id);
8527 ret = clock_gettime(c, &ts);
8529 clock_failed(
"gettime",
errno, clk_id);
8530 tt.count = (int32_t)ts.tv_nsec;
8531 tt.giga_count = ts.tv_sec;
8532 denominators[num_denominators++] = 1000000000;
8539 clock_failed(
"gettime", EINVAL, clk_id);
8542 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8587 timetick_int_t numerators[2];
8588 timetick_int_t denominators[2];
8589 int num_numerators = 0;
8590 int num_denominators = 0;
8591 #ifdef HAVE_CLOCK_GETRES
8596 VALUE clk_id = argv[0];
8599 #ifdef CLOCK_REALTIME
8600 if (clk_id == RUBY_CLOCK_REALTIME) {
8606 #ifdef CLOCK_MONOTONIC
8607 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8608 c = CLOCK_MONOTONIC;
8613 #ifdef CLOCK_PROCESS_CPUTIME_ID
8614 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8615 c = CLOCK_PROCESS_CPUTIME_ID;
8620 #ifdef CLOCK_THREAD_CPUTIME_ID
8621 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8622 c = CLOCK_THREAD_CPUTIME_ID;
8627 #ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8628 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8631 denominators[num_denominators++] = 1000000000;
8636 #ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8637 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8640 denominators[num_denominators++] = 1000000000;
8645 #ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8646 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8649 denominators[num_denominators++] = get_clk_tck();
8654 #ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8655 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8658 denominators[num_denominators++] = 1000000000;
8663 #ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8664 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8667 denominators[num_denominators++] = get_clk_tck();
8672 #ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8673 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8676 denominators[num_denominators++] = CLOCKS_PER_SEC;
8681 #ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8682 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8683 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8686 numerators[num_numerators++] = info->numer;
8687 denominators[num_denominators++] = info->denom;
8688 denominators[num_denominators++] = 1000000000;
8693 else if (NUMERIC_CLOCKID) {
8694 #if defined(HAVE_CLOCK_GETRES)
8696 c = NUM2CLOCKID(clk_id);
8698 ret = clock_getres(c, &ts);
8700 clock_failed(
"getres",
errno, clk_id);
8701 tt.count = (int32_t)ts.tv_nsec;
8702 tt.giga_count = ts.tv_sec;
8703 denominators[num_denominators++] = 1000000000;
8710 clock_failed(
"getres", EINVAL, clk_id);
8713 if (unit ==
ID2SYM(id_hertz)) {
8714 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8717 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8722 get_CHILD_STATUS(
ID _x,
VALUE *_y)
8728 get_PROCESS_ID(
ID _x,
VALUE *_y)
8820 static VALUE rb_mProcUID;
8821 static VALUE rb_mProcGID;
8822 static VALUE rb_mProcID_Syscall;
8854 rb_gc_prepare_heap();
9277 InitVM_process(
void)
9282 rb_gvar_ractor_local(
"$$");
9283 rb_gvar_ractor_local(
"$?");
9338 process_status_dump, process_status_load);
9376 #ifdef HAVE_GETPRIORITY
9387 #if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9389 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9390 #ifdef RLIM_SAVED_MAX
9392 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9399 #ifdef RLIM_SAVED_CUR
9401 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9442 #ifdef RLIMIT_MEMLOCK
9449 #ifdef RLIMIT_MSGQUEUE
9464 #ifdef RLIMIT_NOFILE
9495 #ifdef RLIMIT_RTPRIO
9502 #ifdef RLIMIT_RTTIME
9510 #ifdef RLIMIT_SBSIZE
9515 #ifdef RLIMIT_SIGPENDING
9550 #if defined(RUBY_CLOCK_REALTIME)
9551 #elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9552 # define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9553 #elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9554 # define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9556 #if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9559 #elif defined(RUBY_CLOCK_REALTIME)
9563 #if defined(RUBY_CLOCK_MONOTONIC)
9564 #elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9565 # define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9567 #if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9570 #elif defined(RUBY_CLOCK_MONOTONIC)
9574 #if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9575 #elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9576 # define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9578 #if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9581 #elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9585 #if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9588 #elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9593 #ifdef CLOCK_VIRTUAL
9601 #ifdef CLOCK_REALTIME_FAST
9605 #ifdef CLOCK_REALTIME_PRECISE
9609 #ifdef CLOCK_REALTIME_COARSE
9613 #ifdef CLOCK_REALTIME_ALARM
9617 #ifdef CLOCK_MONOTONIC_FAST
9621 #ifdef CLOCK_MONOTONIC_PRECISE
9625 #ifdef CLOCK_MONOTONIC_RAW
9629 #ifdef CLOCK_MONOTONIC_RAW_APPROX
9633 #ifdef CLOCK_MONOTONIC_COARSE
9637 #ifdef CLOCK_BOOTTIME
9641 #ifdef CLOCK_BOOTTIME_ALARM
9649 #ifdef CLOCK_UPTIME_FAST
9653 #ifdef CLOCK_UPTIME_PRECISE
9657 #ifdef CLOCK_UPTIME_RAW
9661 #ifdef CLOCK_UPTIME_RAW_APPROX
9677 #if defined(HAVE_TIMES) || defined(_WIN32)
9691 SAVED_USER_ID = geteuid();
9692 SAVED_GROUP_ID = getegid();
9715 #ifdef p_uid_from_name
9718 #ifdef p_gid_from_name
9749 #define define_id(name) id_##name = rb_intern_const(#name)
9762 define_id(new_pgroup);
9764 define_id(unsetenv_others);
9767 define_id(close_others);
9768 define_id(nanosecond);
9769 define_id(microsecond);
9770 define_id(millisecond);
9772 define_id(float_microsecond);
9773 define_id(float_millisecond);
9774 define_id(float_second);
9775 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9776 define_id(TIME_BASED_CLOCK_REALTIME);
9777 #ifdef CLOCK_REALTIME
9778 define_id(CLOCK_REALTIME);
9780 #ifdef CLOCK_MONOTONIC
9781 define_id(CLOCK_MONOTONIC);
9783 #ifdef CLOCK_PROCESS_CPUTIME_ID
9784 define_id(CLOCK_PROCESS_CPUTIME_ID);
9786 #ifdef CLOCK_THREAD_CPUTIME_ID
9787 define_id(CLOCK_THREAD_CPUTIME_ID);
9790 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9791 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9794 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9796 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9798 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define GIDT2NUM
Converts a C's gid_t into an instance of rb_cInteger.
#define NUM2GIDT
Converts an instance of rb_cNumeric into C's gid_t.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
Defines a top-level module.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for a module.
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define T_FILE
Old name of RUBY_T_FILE.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ISUPPER
Old name of rb_isupper.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define TOUPPER
Old name of rb_toupper.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define ISLOWER
Old name of rb_islower.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
void rb_notimplement(void)
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eSystemExit
SystemExit exception.
void rb_sys_fail(const char *mesg)
Converts a C errno into a Ruby exception, then raises it.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
void rb_sys_fail_str(VALUE mesg)
Identical to rb_sys_fail(), except it takes the message in Ruby's String instead of C's.
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
void rb_exit(int status)
Terminates the current execution context.
VALUE rb_mProcess
Process module.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_cThread
Thread class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
void rb_enc_copy(VALUE dst, VALUE src)
Destructively copies the encoding of the latter object to that of former one.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
void rb_gc_mark(VALUE obj)
Marks an object.
int rb_during_gc(void)
Queries if the GC is busy.
void rb_gc(void)
Triggers a GC process.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_f_abort(int argc, const VALUE *argv)
This is similar to rb_f_exit().
VALUE rb_f_exit(int argc, const VALUE *argv)
Identical to rb_exit(), except how arguments are passed.
void rb_jump_tag(int state)
This function is to re-throw global escapes.
VALUE rb_str_encode_ospath(VALUE path)
Converts a string into an "OS Path" encoding, if any.
VALUE rb_check_hash_type(VALUE obj)
Try converting an object to its hash representation using its to_hash method, if any.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_env_clear(void)
Destructively removes every environment variables of the running process.
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_io_puts(int argc, const VALUE *argv, VALUE io)
Iterates over the passed array to apply rb_io_write() individually.
int rb_cloexec_dup2(int oldfd, int newfd)
Identical to rb_cloexec_dup(), except you can specify the destination file descriptor.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
Closes everything.
int rb_reserved_fd_p(int fd)
Queries if the given FD is reserved or not.
int rb_pipe(int *pipes)
This is an rb_cloexec_pipe() + rb_update_max_fd() combo.
int rb_cloexec_fcntl_dupfd(int fd, int minfd)
Duplicates a file descriptor with closing on exec.
int rb_cloexec_dup(int oldfd)
Identical to rb_cloexec_fcntl_dupfd(), except it implies minfd is 3.
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
int rb_proc_exec(const char *cmd)
Executes a shell command.
VALUE rb_proc_times(VALUE _)
Gathers info about resources consumed by the current process.
VALUE rb_last_status_get(void)
Queries the "last status", or the $?.
rb_pid_t rb_waitpid(rb_pid_t pid, int *status, int flags)
Waits for a process, with releasing GVL.
rb_pid_t rb_spawn_err(int argc, const VALUE *argv, char *errbuf, size_t buflen)
Identical to rb_spawn(), except you can additionally know the detailed situation in case of abnormal ...
void rb_syswait(rb_pid_t pid)
This is a shorthand of rb_waitpid without status and flags.
VALUE rb_f_exec(int argc, const VALUE *argv)
Replaces the current process by running the given external command.
rb_pid_t rb_spawn(int argc, const VALUE *argv)
Identical to rb_f_exec(), except it spawns a child process instead of replacing the current one.
VALUE rb_process_status_wait(rb_pid_t pid, int flags)
Wait for the specified process to terminate, reap it, and return its status.
void rb_last_status_set(int status, rb_pid_t pid)
Sets the "last status", or the $?.
VALUE rb_detach_process(rb_pid_t pid)
"Detaches" a subprocess.
const char * ruby_signal_name(int signo)
Queries the name of the signal.
VALUE rb_f_kill(int argc, const VALUE *argv)
Sends a signal ("kills") to processes.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_buf_cat(VALUE, const char *, long)
Just another name of rb_str_cat.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_buf_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_struct_define_under(VALUE space, const char *name,...)
Identical to rb_struct_define(), except it defines the class under the specified namespace instead of...
VALUE rb_struct_new(VALUE klass,...)
Creates an instance of the given struct.
VALUE rb_thread_local_aref(VALUE thread, ID key)
This badly named function reads from a Fiber local storage.
#define RUBY_UBF_IO
A special UBF for blocking IO operations.
void rb_thread_sleep_forever(void)
Blocks indefinitely.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
VALUE rb_thread_create(VALUE(*f)(void *g), void *g)
Creates a Ruby thread that is backended by a C function.
void rb_thread_check_ints(void)
Checks for interrupts.
void rb_thread_atfork(void)
A pthread_atfork(3posix)-like API.
VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val)
This badly named function writes to a Fiber local storage.
#define RUBY_UBF_PROCESS
A special UBF for blocking process operations.
void rb_thread_sleep(int sec)
Blocks for the given period of time.
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
VALUE rb_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well...
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
void rb_define_virtual_variable(const char *name, rb_gvar_getter_t *getter, rb_gvar_setter_t *setter)
Defines a global variable that is purely function-backended.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
int rb_io_modestr_oflags(const char *modestr)
Identical to rb_io_modestr_fmode(), except it returns a mixture of O_ flags.
#define GetOpenFile
This is an old name of RB_IO_POINTER.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
VALUE rb_io_check_io(VALUE io)
Try converting an object to its IO representation using its to_io method, if any.
int len
Length of the buffer.
VALUE rb_ractor_stderr(void)
Queries the standard error of the current Ractor that is calling this function.
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
void ruby_setenv(const char *key, const char *val)
Sets an environment variable.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_block_call(VALUE obj, ID mid, int argc, const VALUE *argv, rb_block_call_func_t proc, VALUE data2)
Identical to rb_funcallv(), except it additionally passes a function as a block.
VALUE rb_yield(VALUE val)
Yields the block.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
#define PIDT2NUM
Converts a C's pid_t into an instance of rb_cInteger.
#define NUM2PIDT
Converts an instance of rb_cNumeric into C's pid_t.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define errno
Ractor-aware version of errno.
#define InitVM(ext)
This macro is for internal use.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
VALUE rb_fiber_scheduler_kernel_sleepv(VALUE scheduler, int argc, VALUE *argv)
Identical to rb_fiber_scheduler_kernel_sleep(), except it can pass multiple arguments.
VALUE rb_fiber_scheduler_process_wait(VALUE scheduler, rb_pid_t pid, int flags)
Non-blocking waitpid.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
Ruby's IO, metadata and buffers.
VALUE tied_io_for_writing
Duplex IO object, if set.
#define UIDT2NUM
Converts a C's uid_t into an instance of rb_cInteger.
#define NUM2UIDT
Converts an instance of rb_cNumeric into C's uid_t.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
void * ruby_xmalloc(size_t size)
Allocates a storage instance.