14#include "ruby/internal/config.h"
42# define EXIT_SUCCESS 0
46# define EXIT_FAILURE 1
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
75#ifdef HAVE_SYS_TIMES_H
76# include <sys/times.h>
86int initgroups(
const char *, rb_gid_t);
95# include <mach/mach_time.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)
135static 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
169int setreuid(rb_uid_t ruid, rb_uid_t euid);
171#ifdef BROKEN_SETREGID
172#define setregid ruby_setregid
173int 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
185static void check_uid_switch(
void);
186static void check_gid_switch(
void);
187static int exec_async_signal_safe(
const struct rb_execarg *,
char *,
size_t);
189VALUE rb_envtbl(
void);
190VALUE 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)
239static rb_uid_t obj2uid(
VALUE id,
VALUE *getpw_buf);
240static inline rb_uid_t
250# define PREPARE_GETPWNAM
251# define FINISH_GETPWNAM
252# define OBJ2UID1(id) obj2uid((id))
253# define OBJ2UID(id) obj2uid((id))
254static 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)
281static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
282static inline rb_gid_t
291static 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))
297static 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
311typedef unsigned int unsigned_clock_t;
312#elif SIZEOF_CLOCK_T == SIZEOF_LONG
313typedef unsigned long unsigned_clock_t;
314#elif defined(HAVE_LONG_LONG) && SIZEOF_CLOCK_T == SIZEOF_LONG_LONG
315typedef unsigned LONG_LONG unsigned_clock_t;
318typedef void (*sig_t) (int);
321#define id_exception idException
322static ID id_in, id_out, id_err, id_pid, id_uid, id_gid;
323static ID id_close, id_child;
328static ID id_new_pgroup;
330static ID id_unsetenv_others, id_chdir, id_umask, id_close_others;
331static ID id_nanosecond, id_microsecond, id_millisecond, id_second;
332static ID id_float_microsecond, id_float_millisecond, id_float_second;
333static ID id_GETTIMEOFDAY_BASED_CLOCK_REALTIME, id_TIME_BASED_CLOCK_REALTIME;
335static ID id_CLOCK_REALTIME;
336# define RUBY_CLOCK_REALTIME ID2SYM(id_CLOCK_REALTIME)
338#ifdef CLOCK_MONOTONIC
339static ID id_CLOCK_MONOTONIC;
340# define RUBY_CLOCK_MONOTONIC ID2SYM(id_CLOCK_MONOTONIC)
342#ifdef CLOCK_PROCESS_CPUTIME_ID
343static 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
347static ID id_CLOCK_THREAD_CPUTIME_ID;
348# define RUBY_CLOCK_THREAD_CPUTIME_ID ID2SYM(id_CLOCK_THREAD_CPUTIME_ID)
351static ID id_TIMES_BASED_CLOCK_MONOTONIC;
352static ID id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID;
355static ID id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID;
357static ID id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID;
359static 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)
364static 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
376assert_close_on_exec(
int fd)
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");
395close_unless_reserved(
int fd)
398 assert_close_on_exec(fd);
405#if defined(DEBUG_REDIRECT)
408ttyprintf(
const char *fmt, ...)
414 tty = fopen(
"con",
"w");
416 tty = fopen(
"/dev/tty",
"w");
422 vfprintf(tty, fmt, ap);
429redirect_dup(
int oldfd)
433 ttyprintf(
"dup(%d) => %d\n", oldfd, ret);
438redirect_dup2(
int oldfd,
int newfd)
441 ret = dup2(oldfd, newfd);
442 ttyprintf(
"dup2(%d, %d) => %d\n", oldfd, newfd, ret);
447redirect_cloexec_dup(
int oldfd)
451 ttyprintf(
"cloexec_dup(%d) => %d\n", oldfd, ret);
456redirect_cloexec_dup2(
int oldfd,
int newfd)
460 ttyprintf(
"cloexec_dup2(%d, %d) => %d\n", oldfd, newfd, ret);
465redirect_close(
int fd)
468 ret = close_unless_reserved(fd);
469 ttyprintf(
"close(%d) => %d\n", fd, ret);
474parent_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);
483parent_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
585static VALUE rb_cProcessStatus;
594 .wrap_struct_name =
"Process::Status",
600 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
604rb_process_status_allocate(
VALUE klass)
613 return GET_THREAD()->last_status;
639proc_s_last_status(
VALUE mod)
645rb_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;
653 rb_obj_freeze(last_status);
658process_status_dump(
VALUE status)
671process_status_load(
VALUE real_obj,
VALUE load_obj)
674 VALUE status = rb_attr_get(load_obj, id_status);
675 VALUE pid = rb_attr_get(load_obj, id_pid);
684 GET_THREAD()->last_status = rb_process_status_new(pid, status, 0);
688last_status_clear(rb_thread_t *th)
690 th->last_status =
Qnil;
694rb_last_status_clear(
void)
696 last_status_clear(GET_THREAD());
708pst_status(
VALUE status)
728 int status = pst_status(self);
732#define PST2INT(st) pst_status(st)
748 rb_pid_t pid = pst_pid(self);
752static VALUE pst_message_status(
VALUE str,
int status);
755pst_message(
VALUE str, rb_pid_t pid,
int status)
757 rb_str_catf(str,
"pid %ld", (
long)pid);
758 pst_message_status(str, status);
762pst_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);
771 rb_str_catf(str,
" stopped signal %d", stopsig);
774 if (WIFSIGNALED(status)) {
775 int termsig = WTERMSIG(status);
778 rb_str_catf(str,
" SIG%s (signal %d)", signame, termsig);
781 rb_str_catf(str,
" signal %d", termsig);
784 if (WIFEXITED(status)) {
785 rb_str_catf(str,
" exit %d", WEXITSTATUS(status));
788 if (WCOREDUMP(status)) {
816 status = PST2INT(st);
818 str = rb_str_buf_new(0);
819 pst_message(str, pid, status);
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);
901 rb_raise(rb_eArgError,
"negative mask value: %d", mask);
903#define WARN_SUGGEST(suggest) \
904 rb_warn_deprecated_to_remove_at(3.4, "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);
956 rb_raise(rb_eArgError,
"negative shift value: %d", places);
958#define WARN_SUGGEST(suggest) \
959 rb_warn_deprecated_to_remove_at(3.4, "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");
989pst_wifstopped(
VALUE st)
991 int status = PST2INT(st);
993 return RBOOL(WIFSTOPPED(status));
1006pst_wstopsig(
VALUE st)
1008 int status = PST2INT(st);
1010 if (WIFSTOPPED(status))
1011 return INT2NUM(WSTOPSIG(status));
1025pst_wifsignaled(
VALUE st)
1027 int status = PST2INT(st);
1029 return RBOOL(WIFSIGNALED(status));
1042pst_wtermsig(
VALUE st)
1044 int status = PST2INT(st);
1046 if (WIFSIGNALED(status))
1047 return INT2NUM(WTERMSIG(status));
1062pst_wifexited(
VALUE st)
1064 int status = PST2INT(st);
1066 return RBOOL(WIFEXITED(status));
1084pst_wexitstatus(
VALUE st)
1086 int status = PST2INT(st);
1088 if (WIFEXITED(status))
1089 return INT2NUM(WEXITSTATUS(status));
1107pst_success_p(
VALUE st)
1109 int status = PST2INT(st);
1111 if (!WIFEXITED(status))
1113 return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
1128pst_wcoredump(
VALUE st)
1131 int status = PST2INT(st);
1133 return RBOOL(WCOREDUMP(status));
1140do_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;
1153 rb_execution_context_t *ec;
1154 rb_nativethread_cond_t *cond;
1163waitpid_state_init(
struct waitpid_state *w, rb_pid_t pid,
int options)
1167 w->options = options;
1173waitpid_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;
1250rb_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;
1290proc_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);
1516 result = rb_ary_new();
1517 rb_last_status_clear();
1532static VALUE rb_cWaiter;
1535detach_process_pid(
VALUE thread)
1541detach_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);
1609before_exec_async_signal_safe(
void)
1614before_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)
1630int rb_w32_set_nonblock2(
int fd,
int nonblock);
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);
1652stdfd_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
1677before_fork_ruby(
void)
1683after_fork_ruby(rb_pid_t pid)
1697#if defined(HAVE_WORKING_FORK)
1699COMPILER_WARNING_PUSH
1700#if __has_warning("-Wdeprecated-declarations") || RBIMPL_COMPILER_IS(GCC)
1701COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
1703static inline rb_pid_t
1711#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1713exec_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
1729proc_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);
1760proc_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);
1807mark_exec_arg(
void *ptr)
1810 if (eargp->use_shell)
1811 rb_gc_mark(eargp->invoke.sh.shell_script);
1813 rb_gc_mark(eargp->invoke.cmd.command_name);
1814 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1815 rb_gc_mark(eargp->invoke.cmd.argv_str);
1816 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1818 rb_gc_mark(eargp->redirect_fds);
1819 rb_gc_mark(eargp->envp_str);
1820 rb_gc_mark(eargp->envp_buf);
1821 rb_gc_mark(eargp->dup2_tmpbuf);
1822 rb_gc_mark(eargp->rlimit_limits);
1823 rb_gc_mark(eargp->fd_dup2);
1824 rb_gc_mark(eargp->fd_close);
1825 rb_gc_mark(eargp->fd_open);
1826 rb_gc_mark(eargp->fd_dup2_child);
1827 rb_gc_mark(eargp->env_modification);
1828 rb_gc_mark(eargp->path_env);
1829 rb_gc_mark(eargp->chdir_dir);
1833memsize_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)
1851export_dup(
VALUE str)
1853 VALUE newstr = EXPORT_STR(str);
1854 if (newstr == str) newstr = rb_str_dup(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))
1876proc_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)
1923proc_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);
1945check_exec_redirect_fd(
VALUE v,
int iskey)
1956 else if (
id == id_out)
1958 else if (
id == id_err)
1967 rb_raise(rb_eArgError,
"duplex IO redirection");
1974 rb_raise(rb_eArgError,
"negative file descriptor");
1977 else if (fd >= 3 && iskey) {
1978 rb_raise(rb_eArgError,
"wrong file descriptor (%d)", fd);
1984 rb_raise(rb_eArgError,
"wrong exec redirect");
1992 ary = hide_obj(rb_ary_new());
1994 if (!RB_TYPE_P(key,
T_ARRAY)) {
1995 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
1996 rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
2002 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
2003 rb_ary_push(ary, hide_obj(rb_assoc_new(fd, 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);
2037 rb_raise(rb_eArgError,
"wrong exec redirect symbol: %"PRIsVALUE,
2044 val = check_exec_redirect_fd(val, 0);
2048 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2052 path = rb_ary_entry(val, 0);
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);
2060 flags = rb_ary_entry(val, 1);
2063 else if (RB_TYPE_P(flags,
T_STRING))
2067 perm = rb_ary_entry(val, 2);
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);
2078 if (RB_TYPE_P(key,
T_FILE))
2079 key = check_exec_redirect_fd(key, 1);
2081 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2082 else if (RB_TYPE_P(key,
T_ARRAY)) {
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;
2106 rb_raise(rb_eArgError,
"wrong exec redirect action");
2111#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2112static int rlimit_type_by_sym(
VALUE key);
2115rb_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;
2123 tmp = rb_check_array_type(val);
2126 softlim = hardlim =
rb_to_int(rb_ary_entry(tmp, 0));
2128 softlim =
rb_to_int(rb_ary_entry(tmp, 0));
2129 hardlim =
rb_to_int(rb_ary_entry(tmp, 1));
2132 rb_raise(rb_eArgError,
"wrong exec rlimit option");
2139 rb_ary_push(ary, tmp);
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) {
2168 rb_raise(rb_eArgError,
"pgroup option specified twice");
2172 else if (val ==
Qtrue)
2177 rb_raise(rb_eArgError,
"negative process group ID : %ld", (
long)pgroup);
2180 eargp->pgroup_given = 1;
2181 eargp->pgroup_pgid = pgroup;
2186 if (
id == id_new_pgroup) {
2187 if (eargp->new_pgroup_given) {
2188 rb_raise(rb_eArgError,
"new_pgroup option specified twice");
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) {
2197 rb_raise(rb_eArgError,
"unsetenv_others option specified twice");
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) {
2204 rb_raise(rb_eArgError,
"chdir option specified twice");
2207 val = rb_str_encode_ospath(val);
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) {
2214 rb_raise(rb_eArgError,
"umask option specified twice");
2216 eargp->umask_given = 1;
2217 eargp->umask_mask = cmask;
2219 else if (
id == id_close_others) {
2220 if (eargp->close_others_given) {
2221 rb_raise(rb_eArgError,
"close_others option specified twice");
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) {
2241 rb_raise(rb_eArgError,
"uid option specified twice");
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) {
2256 rb_raise(rb_eArgError,
"gid option specified twice");
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) {
2270 rb_raise(rb_eArgError,
"exception option specified twice");
2272 eargp->exception_given = 1;
2273 eargp->exception = TO_BOOL(val,
"exception");
2284 check_exec_redirect(key, val, eargp);
2296check_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) {
2303 rb_raise(rb_eArgError,
"wrong exec option symbol: % "PRIsVALUE,
2305 rb_raise(rb_eArgError,
"wrong exec option");
2311check_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];
2319 if (
NIL_P(nonopts)) args[1] = nonopts = rb_hash_new();
2320 rb_hash_aset(nonopts, key, val);
2335 rb_raise(rb_eArgError,
"fd %d specified twice", fd);
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) {
2358 VALUE h = rb_hash_new();
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;
2378 val = rb_hash_lookup(h, val);
2380 rb_raise(rb_eArgError,
"cyclic child fd redirection from %d", oldfd);
2384 rb_raise(rb_eArgError,
"child fd %d is not redirected", oldfd);
2385 if (oldfd != lastfd) {
2387 rb_ary_store(elt, 1,
INT2FIX(lastfd));
2390 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2391 rb_hash_aset(h, val,
INT2FIX(lastfd));
2398 eargp->close_others_maxhint = maxhint;
2403rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2407 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2411rb_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)
2429check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2439 rb_raise(rb_eArgError,
"environment name contains a equal : %"PRIsVALUE, key);
2444 key = EXPORT_STR(key);
2445 if (!
NIL_P(val)) val = EXPORT_STR(val);
2450 rb_ary_push(env, hide_obj(rb_assoc_new(key, val)));
2460 env[0] = hide_obj(rb_ary_new());
2462 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2469rb_check_argv(
int argc,
VALUE *argv)
2477 tmp = rb_check_array_type(argv[0]);
2480 rb_raise(rb_eArgError,
"wrong first argument");
2486 prog = rb_str_new_frozen(prog);
2488 for (i = 0; i < argc; i++) {
2490 argv[i] = rb_str_new_frozen(argv[i]);
2497check_hash(
VALUE obj)
2507 return rb_check_hash_type(obj);
2511rb_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) {
2549compare_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] = {
2641 for (p = RSTRING_PTR(prog); *p; p++) {
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) {
2673 argv_buf = hide_obj(rb_str_buf_new(0));
2674 p = RSTRING_PTR(prog);
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 =
2688 hide_obj(rb_str_subseq(argv_buf, 0, strlen(RSTRING_PTR(argv_buf))));
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;
2697 if (
RTEST(eargp->path_env)) path_env = RSTRING_PTR(eargp->path_env);
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) {
2709 argv_buf = rb_str_buf_new(0);
2711 for (i = 0; i < argc; i++) {
2712 VALUE arg = argv[i];
2714#ifdef DEFAULT_PROCESS_ENCODING
2715 arg = EXPORT_STR(arg);
2716 s = RSTRING_PTR(arg);
2720 eargp->invoke.cmd.argv_buf = argv_buf;
2723 if (!eargp->use_shell) {
2724 const char *p, *ep, *null=NULL;
2726 argv_str = hide_obj(rb_str_buf_new(
sizeof(
char*) * (argc + 2)));
2728 p = RSTRING_PTR(eargp->invoke.cmd.argv_buf);
2729 ep = p + RSTRING_LEN(eargp->invoke.cmd.argv_buf);
2735 eargp->invoke.cmd.argv_str =
2736 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(argv_str);
2742rb_execarg_get(
VALUE execarg_obj)
2750rb_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;
2767rb_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) {
2774 rb_raise(rb_eArgError,
"exception option is not allowed");
2780rb_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;
2789fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2804static long run_exec_dup2_tmpbuf_size(
long n);
2818 const char *fname = RSTRING_PTR(data->fname);
2819 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2825rb_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;
2833rb_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) {
2893 envtbl = rb_hash_new();
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);
2917 envp_buf = rb_str_buf_new(0);
2919 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
2920 envp_str = rb_str_buf_new(
sizeof(
char*) * (
RHASH_SIZE(envtbl) + 1));
2922 p = RSTRING_PTR(envp_buf);
2923 ep = p + RSTRING_LEN(envp_buf);
2931 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(envp_str);
2932 eargp->envp_buf = envp_buf;
2948rb_execarg_parent_start(
VALUE execarg_obj)
2951 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2953 rb_execarg_parent_end(execarg_obj);
2959execarg_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);
2988rb_execarg_parent_end(
VALUE execarg_obj)
2990 execarg_parent_end(execarg_obj);
2995rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
2997 if (!errmsg || !*errmsg)
return;
2998 if (strcmp(errmsg,
"chdir") == 0) {
2999 rb_sys_fail_str(eargp->chdir_dir);
3001 rb_sys_fail(errmsg);
3006rb_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);
3141#define ERRMSG(str) do { if (errmsg && 0 < errmsg_buflen) strlcpy(errmsg, (str), errmsg_buflen); } while (0)
3142#define ERRMSG1(str, a) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a)); } while (0)
3143#define ERRMSG2(str, a, b) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a), (b)); } while (0)
3145static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3146static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3147static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3150save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3153 VALUE newary, redirection;
3154 int save_fd = redirect_cloexec_dup(fd), cloexec;
3155 if (save_fd == -1) {
3162 newary = sargp->fd_dup2;
3164 newary = hide_obj(rb_ary_new());
3165 sargp->fd_dup2 = newary;
3167 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3168 redirection = hide_obj(rb_assoc_new(
INT2FIX(fd),
INT2FIX(save_fd)));
3169 if (cloexec) rb_ary_push(redirection,
Qtrue);
3170 rb_ary_push(newary, redirection);
3172 newary = sargp->fd_close;
3174 newary = hide_obj(rb_ary_new());
3175 sargp->fd_close = newary;
3177 rb_ary_push(newary, hide_obj(rb_assoc_new(
INT2FIX(save_fd),
Qnil)));
3184intcmp(
const void *a,
const void *b)
3186 return *(
int*)a - *(
int*)b;
3190intrcmp(
const void *a,
const void *b)
3192 return *(
int*)b - *(
int*)a;
3204run_exec_dup2_tmpbuf_size(
long n)
3211fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3215 ret = fcntl(fd, F_GETFD);
3217 ERRMSG(
"fcntl(F_GETFD)");
3220 if (ret & FD_CLOEXEC)
return 1;
3227fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3231 ret = fcntl(fd, F_GETFD);
3233 ERRMSG(
"fcntl(F_GETFD)");
3236 if (!(ret & FD_CLOEXEC)) {
3238 ret = fcntl(fd, F_SETFD, ret);
3240 ERRMSG(
"fcntl(F_SETFD)");
3250fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3254 ret = fcntl(fd, F_GETFD);
3256 ERRMSG(
"fcntl(F_GETFD)");
3259 if (ret & FD_CLOEXEC) {
3261 ret = fcntl(fd, F_SETFD, ret);
3263 ERRMSG(
"fcntl(F_SETFD)");
3273run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3284 for (i = 0; i < n; i++) {
3289 pairs[i].older_index = -1;
3299 for (i = 0; i < n; i++) {
3300 int newfd = pairs[i].newfd;
3304 pairs[i].num_newer = 0;
3306 while (pairs < found && (found-1)->oldfd == newfd)
3308 while (found < pairs+n && found->oldfd == newfd) {
3309 pairs[i].num_newer++;
3310 found->older_index = i;
3317 for (i = 0; i < n; i++) {
3319 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3320 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3322 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3327 if (pairs[j].cloexec &&
3328 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3332 pairs[j].oldfd = -1;
3333 j = pairs[j].older_index;
3335 pairs[j].num_newer--;
3340 for (i = 0; i < n; i++) {
3342 if (pairs[i].oldfd == -1)
3344 if (pairs[i].oldfd == pairs[i].newfd) {
3345 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3347 pairs[i].oldfd = -1;
3350 if (extra_fd == -1) {
3351 extra_fd = redirect_dup(pairs[i].oldfd);
3352 if (extra_fd == -1) {
3358 if (fd_get_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen)) {
3359 if (fd_set_cloexec(extra_fd, errmsg, errmsg_buflen)) {
3366 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3373 pairs[i].oldfd = extra_fd;
3374 j = pairs[i].older_index;
3375 pairs[i].older_index = -1;
3377 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3383 pairs[j].oldfd = -1;
3384 j = pairs[j].older_index;
3387 if (extra_fd != -1) {
3388 ret = redirect_close(extra_fd);
3403run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3411 ret = redirect_close(fd);
3422run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3432 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3434 ret = redirect_dup2(oldfd, newfd);
3447run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3459 pgroup = eargp->pgroup_pgid;
3465 sargp->pgroup_given = 1;
3466 sargp->pgroup_pgid = getpgrp();
3472 ret = setpgid(getpid(), pgroup);
3473 if (ret == -1) ERRMSG(
"setpgid");
3478#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3481run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3490 if (getrlimit(rtype, &rlim) == -1) {
3491 ERRMSG(
"getrlimit");
3495 RLIM2NUM(rlim.rlim_cur),
3496 RLIM2NUM(rlim.rlim_max)));
3497 if (sargp->rlimit_limits ==
Qfalse)
3498 newary = sargp->rlimit_limits = hide_obj(rb_ary_new());
3500 newary = sargp->rlimit_limits;
3501 rb_ary_push(newary, tmp);
3505 if (setrlimit(rtype, &rlim) == -1) {
3506 ERRMSG(
"setrlimit");
3514#if !defined(HAVE_WORKING_FORK)
3518 rb_ary_push(ary, hide_obj(rb_ary_dup(argv[0])));
3527 if (sargp->env_modification ==
Qfalse) {
3528 VALUE env = rb_envtbl();
3530 VALUE ary = hide_obj(rb_ary_new());
3533 sargp->env_modification = ary;
3535 sargp->unsetenv_others_given = 1;
3536 sargp->unsetenv_others_do = 1;
3543#define chdir(p) rb_w32_uchdir(p)
3548rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3555 sargp->redirect_fds =
Qnil;
3559 if (eargp->pgroup_given) {
3560 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3565#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3566 obj = eargp->rlimit_limits;
3568 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3573#if !defined(HAVE_WORKING_FORK)
3574 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3579 obj = eargp->env_modification;
3595 if (eargp->umask_given) {
3596 mode_t mask = eargp->umask_mask;
3597 mode_t oldmask = umask(mask);
3599 sargp->umask_given = 1;
3600 sargp->umask_mask = oldmask;
3604 obj = eargp->fd_dup2;
3606 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3610 obj = eargp->fd_close;
3613 rb_warn(
"cannot close fd before spawn");
3615 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3620#ifdef HAVE_WORKING_FORK
3621 if (eargp->close_others_do) {
3626 obj = eargp->fd_dup2_child;
3628 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3632 if (eargp->chdir_given) {
3634 sargp->chdir_given = 1;
3635 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3637 if (chdir(RSTRING_PTR(eargp->chdir_dir)) == -1) {
3644 if (eargp->gid_given) {
3645 if (setgid(eargp->gid) < 0) {
3652 if (eargp->uid_given) {
3653 if (setuid(eargp->uid) < 0) {
3661 VALUE ary = sargp->fd_dup2;
3663 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3667 int preserve =
errno;
3668 stdfd_clear_nonblock();
3677rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3679 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3684exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3686#if !defined(HAVE_WORKING_FORK)
3687 struct rb_execarg sarg, *
const sargp = &sarg;
3693 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3697 if (eargp->use_shell) {
3698 err = proc_exec_sh(RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3701 char *abspath = NULL;
3702 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3703 abspath = RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3704 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3706#if !defined(HAVE_WORKING_FORK)
3707 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3713#ifdef HAVE_WORKING_FORK
3716rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3718 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3722proc_syswait(
VALUE pid)
3729move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3733 for (i = 0; i < n; i++) {
3752pipe_nocrash(
int filedes[2],
VALUE fds)
3760 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3775rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3782handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3788 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3794#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3797 if (!status && !ep) {
3802 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3803 if (status) status->status = state;
3804 if (!state)
return 0;
3813 if (state && !status) rb_jump_tag(state);
3817#define prefork() ( \
3818 rb_io_flush(rb_stdout), \
3819 rb_io_flush(rb_stderr) \
3849write_retry(
int fd,
const void *buf,
size_t len)
3854 w = write(fd, buf,
len);
3855 }
while (w < 0 &&
errno == EINTR);
3861read_retry(
int fd,
void *buf,
size_t len)
3865 if (set_blocking(fd) != 0) {
3867 rb_async_bug_errno(
"set_blocking failed reading child error",
errno);
3872 r = read(fd, buf,
len);
3873 }
while (r < 0 &&
errno == EINTR);
3879send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3884 if (write_retry(fd, &err,
sizeof(err)) < 0) err =
errno;
3885 if (errmsg && 0 < errmsg_buflen) {
3886 errmsg[errmsg_buflen-1] =
'\0';
3887 errmsg_buflen = strlen(errmsg);
3888 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3894recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3898 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3902 if (size ==
sizeof(err) &&
3903 errmsg && 0 < errmsg_buflen) {
3904 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3913#ifdef HAVE_WORKING_VFORK
3914#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3917getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3923 ret = getuidx(ID_SAVED);
3924 if (ret == (rb_uid_t)-1)
3929#define HAVE_GETRESUID
3932#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
3935getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
3941 ret = getgidx(ID_SAVED);
3942 if (ret == (rb_gid_t)-1)
3947#define HAVE_GETRESGID
3965 rb_uid_t ruid, euid;
3966 rb_gid_t rgid, egid;
3968#if defined HAVE_ISSETUGID
3973#ifdef HAVE_GETRESUID
3977 ret = getresuid(&ruid, &euid, &suid);
3979 rb_sys_fail(
"getresuid(2)");
3988 if (euid == 0 || euid != ruid)
3991#ifdef HAVE_GETRESGID
3995 ret = getresgid(&rgid, &egid, &sgid);
3997 rb_sys_fail(
"getresgid(2)");
4013struct child_handler_disabler_state
4019disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
4021#ifdef HAVE_PTHREAD_SIGMASK
4025 ret = sigfillset(&all);
4027 rb_sys_fail(
"sigfillset");
4029 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
4034# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4039disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
4041#ifdef HAVE_PTHREAD_SIGMASK
4044 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
4049# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4055disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
4060 for (sig = 1; sig < NSIG; sig++) {
4061 sig_t handler = signal(sig, SIG_DFL);
4063 if (handler == SIG_ERR &&
errno == EINVAL) {
4066 if (handler == SIG_ERR) {
4067 ERRMSG(
"signal to obtain old action");
4071 if (sig == SIGPIPE) {
4076 if (handler == SIG_IGN) {
4077 signal(sig, SIG_IGN);
4082 sigemptyset(&old->sigmask);
4083 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
4085 ERRMSG(
"sigprocmask");
4093 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4094 char *errmsg,
size_t errmsg_buflen,
4098 volatile int try_gc = 1;
4099 struct child_handler_disabler_state old;
4104 disable_child_handler_before_fork(&old);
4105#ifdef HAVE_WORKING_VFORK
4106 if (!has_privilege())
4116 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
4118 ret = chfunc(charg, errmsg, errmsg_buflen);
4119 if (!ret) _exit(EXIT_SUCCESS);
4121 send_child_error(ep[1], errmsg, errmsg_buflen);
4122#if EXIT_SUCCESS == 127
4123 _exit(EXIT_FAILURE);
4129 disable_child_handler_fork_parent(&old);
4133 if (handle_fork_error(err, status, ep, &try_gc))
4139fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4140 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4148 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4150 if (status) status->status = 0;
4152 if (pipe_nocrash(ep, fds))
return -1;
4154 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4156 if (status) status->pid = pid;
4159 if (status) status->error =
errno;
4166 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4168 if (error_occurred) {
4171 status->error = err;
4173 VM_ASSERT((w == 0) &&
"only used by extensions");
4174 rb_protect(proc_syswait, (
VALUE)pid, &state);
4176 status->status = state;
4197rb_fork_async_signal_safe(
int *status,
4198 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4199 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4203 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4206 *status = process_status.status;
4216 int try_gc = 1, err;
4217 struct child_handler_disabler_state old;
4219 if (status) status->status = 0;
4225 disable_child_handler_before_fork(&old);
4231 status->error = err;
4234 disable_child_handler_fork_parent(&old);
4235 after_fork_ruby(pid);
4242 if (handle_fork_error(err, status, NULL, &try_gc)) {
4249rb_fork_ruby(
int *status)
4253 rb_pid_t pid = rb_fork_ruby2(&process_status);
4255 if (status) *status = process_status.status;
4263 rb_pid_t pid = rb_fork_ruby(NULL);
4266 rb_sys_fail(
"fork(2)");
4273rb_call_proc__fork(
void)
4278 return proc_fork_pid();
4287#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4308rb_proc__fork(
VALUE _obj)
4310 rb_pid_t pid = proc_fork_pid();
4379 pid = rb_call_proc__fork();
4393#define rb_proc__fork rb_f_notimplement
4394#define rb_f_fork rb_f_notimplement
4398exit_status_code(
VALUE status)
4404 istatus = EXIT_SUCCESS;
4407 istatus = EXIT_FAILURE;
4411#if EXIT_SUCCESS != 0
4413 istatus = EXIT_SUCCESS;
4420NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4438rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4443 istatus = exit_status_code(argv[0]);
4446 istatus = EXIT_FAILURE;
4456 if (GET_EC()->tag) {
4472 istatus = exit_status_code(argv[0]);
4475 istatus = EXIT_SUCCESS;
4540 rb_execution_context_t *ec = GET_EC();
4541 VALUE errinfo = rb_ec_get_errinfo(ec);
4542 if (!
NIL_P(errinfo)) {
4543 rb_ec_error_print(ec, errinfo);
4550 args[1] = args[0] = argv[0];
4552 rb_io_puts(1, args, rb_ractor_stderr());
4553 args[0] =
INT2NUM(EXIT_FAILURE);
4591#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4596 if (eargp && !eargp->use_shell) {
4597 VALUE str = eargp->invoke.cmd.argv_str;
4598 VALUE buf = eargp->invoke.cmd.argv_buf;
4599 char *p, **argv = ARGVSTR2ARGV(str);
4600 long i, argc = ARGVSTR2ARGC(str);
4601 const char *start = RSTRING_PTR(buf);
4603 p = RSTRING_PTR(cmd);
4604 for (i = 1; i < argc; ++i) {
4605 p[argv[i] - start - 1] =
' ';
4615rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4618#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4621# if !defined HAVE_SPAWNV
4626#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4627 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4629 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4631 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4635 if (prog && !eargp->use_shell) {
4636 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4637 argv[0] = RSTRING_PTR(prog);
4639# if defined HAVE_SPAWNV
4640 if (eargp->use_shell) {
4641 pid = proc_spawn_sh(RSTRING_PTR(prog));
4644 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4645 pid = proc_spawn_cmd(argv, prog, eargp);
4652 status = system(rb_execarg_commandline(eargp, &prog));
4657 if (eargp->waitpid_state) {
4658 eargp->waitpid_state->pid = pid;
4661 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4676do_spawn_process(
VALUE arg)
4680 rb_execarg_parent_start1(argp->execarg);
4682 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4683 argp->errmsg.ptr, argp->errmsg.buflen);
4686NOINLINE(
static rb_pid_t
4687 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4690rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4694 args.execarg = execarg_obj;
4695 args.errmsg.ptr = errmsg;
4696 args.errmsg.buflen = errmsg_buflen;
4699 execarg_parent_end, execarg_obj);
4704rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4708 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4709 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4715 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4721 return rb_spawn_internal(argc, argv, NULL, 0);
4843 rb_thread_t *th = GET_THREAD();
4844 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4845 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4848 eargp->status = &status;
4850 last_status_clear(th);
4854 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4860 rb_obj_freeze(status);
4861 th->last_status = status;
4863 if (data->status == EXIT_SUCCESS) {
4867 if (data->error != 0) {
4868 if (eargp->exception) {
4869 VALUE command = eargp->invoke.sh.shell_script;
4877 else if (eargp->exception) {
4878 VALUE command = eargp->invoke.sh.shell_script;
4892 if (eargp->exception) {
4893 VALUE command = eargp->invoke.sh.shell_script;
5015 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
5016 VALUE execarg_obj, fail_str;
5019 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
5020 eargp = rb_execarg_get(execarg_obj);
5021 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
5023 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
5027 rb_exec_fail(eargp, err, errmsg);
5031#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
5057 time_t beg = time(0);
5060 if (scheduler !=
Qnil) {
5064 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
5073 time_t end = time(0) - beg;
5075 return TIMET2NUM(end);
5079#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
5096#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
5098 if (pgrp < 0) rb_sys_fail(0);
5102 if (pgrp < 0) rb_sys_fail(0);
5107#define proc_getpgrp rb_f_notimplement
5111#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
5129 if (setpgid(0,0) < 0) rb_sys_fail(0);
5130#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
5131 if (setpgrp() < 0) rb_sys_fail(0);
5136#define proc_setpgrp rb_f_notimplement
5140#if defined(HAVE_GETPGID)
5158 if (i < 0) rb_sys_fail(0);
5162#define proc_getpgid rb_f_notimplement
5180 rb_pid_t ipid, ipgrp;
5185 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5189#define proc_setpgid rb_f_notimplement
5217 if (sid < 0) rb_sys_fail(0);
5221#define proc_getsid rb_f_notimplement
5225#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5226#if !defined(HAVE_SETSID)
5227static rb_pid_t ruby_setsid(
void);
5228#define setsid() ruby_setsid()
5249 if (pid < 0) rb_sys_fail(0);
5253#if !defined(HAVE_SETSID)
5254#define HAVE_SETSID 1
5262#if defined(SETPGRP_VOID)
5268 ret = setpgrp(0, pid);
5270 if (ret == -1)
return -1;
5274 ioctl(fd, TIOCNOTTY, NULL);
5281#define proc_setsid rb_f_notimplement
5285#ifdef HAVE_GETPRIORITY
5313 int prio, iwhich, iwho;
5319 prio = getpriority(iwhich, iwho);
5320 if (
errno) rb_sys_fail(0);
5324#define proc_getpriority rb_f_notimplement
5328#ifdef HAVE_GETPRIORITY
5348 int iwhich, iwho, iprio;
5354 if (setpriority(iwhich, iwho, iprio) < 0)
5359#define proc_setpriority rb_f_notimplement
5362#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5364rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5368#define RESCHECK(r) \
5370 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5371 resource = RLIMIT_##r; \
5405#ifdef RLIMIT_MEMLOCK
5408#ifdef RLIMIT_MSGQUEUE
5447#ifdef RLIMIT_SIGPENDING
5448 RESCHECK(SIGPENDING);
5457 for (p = name; *p; p++)
5463 for (p = name; *p; p++)
5469 rb_bug(
"unexpected casetype");
5476rlimit_type_by_hname(
const char *name,
long len)
5478 return rlimit_resource_name2int(name,
len, 0);
5482rlimit_type_by_lname(
const char *name,
long len)
5484 return rlimit_resource_name2int(name,
len, 1);
5488rlimit_type_by_sym(
VALUE key)
5491 const char *rname = RSTRING_PTR(name);
5492 long len = RSTRING_LEN(name);
5494 static const char prefix[] =
"rlimit_";
5495 enum {prefix_len =
sizeof(prefix)-1};
5497 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5498 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5506rlimit_resource_type(
VALUE rtype)
5513 switch (
TYPE(rtype)) {
5516 name = RSTRING_PTR(v);
5517 len = RSTRING_LEN(v);
5526 len = RSTRING_LEN(rtype);
5536 r = rlimit_type_by_hname(name,
len);
5540 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5546rlimit_resource_value(
VALUE rval)
5551 switch (
TYPE(rval)) {
5554 name = RSTRING_PTR(v);
5569 return NUM2RLIM(rval);
5573 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5575#ifdef RLIM_SAVED_MAX
5576 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5578#ifdef RLIM_SAVED_CUR
5579 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5581 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5587#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5615 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5616 rb_sys_fail(
"getrlimit");
5618 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5621#define proc_getrlimit rb_f_notimplement
5624#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5678proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5680 VALUE resource, rlim_cur, rlim_max;
5686 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5687 rlim_max = rlim_cur;
5689 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5690 rlim.rlim_max = rlimit_resource_value(rlim_max);
5692 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5693 rb_sys_fail(
"setrlimit");
5698#define proc_setrlimit rb_f_notimplement
5701static int under_uid_switch = 0;
5703check_uid_switch(
void)
5705 if (under_uid_switch) {
5706 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5710static int under_gid_switch = 0;
5712check_gid_switch(
void)
5714 if (under_gid_switch) {
5715 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5720#if defined(HAVE_PWD_H)
5729#if ( !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN) )
5732 char MAYBE_UNUSED(*login) = NULL;
5734# ifdef USE_GETLOGIN_R
5736#if defined(__FreeBSD__)
5737 typedef int getlogin_r_size_t;
5739 typedef size_t getlogin_r_size_t;
5742 long loginsize = GETLOGIN_R_SIZE_INIT;
5745 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5747 VALUE maybe_result = rb_str_buf_new(loginsize);
5749 login = RSTRING_PTR(maybe_result);
5751 rb_str_set_len(maybe_result, loginsize);
5755 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5757 if (gle == ENOTTY || gle == ENXIO || gle == ENOENT) {
5758 rb_str_resize(maybe_result, 0);
5762 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5763 rb_str_resize(maybe_result, 0);
5768 login = RSTRING_PTR(maybe_result);
5772 if (login == NULL) {
5773 rb_str_resize(maybe_result, 0);
5777 return maybe_result;
5797rb_getpwdirnam_for_login(
VALUE login_name)
5799#if ( !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM) )
5803 if (
NIL_P(login_name)) {
5808 char *login = RSTRING_PTR(login_name);
5810 struct passwd *pwptr;
5812# ifdef USE_GETPWNAM_R
5814 struct passwd pwdnm;
5816 long bufsizenm = GETPW_R_SIZE_INIT;
5819 bufsizenm = GETPW_R_SIZE_DEFAULT;
5823 bufnm = RSTRING_PTR(getpwnm_tmp);
5825 rb_str_set_len(getpwnm_tmp, bufsizenm);
5829 while ((enm = getpwnam_r(login, &pwdnm, bufnm, bufsizenm, &pwptr)) != 0) {
5831 if (enm == ENOENT || enm== ESRCH || enm == EBADF || enm == EPERM) {
5833 rb_str_resize(getpwnm_tmp, 0);
5837 if (enm != ERANGE || bufsizenm >= GETPW_R_SIZE_LIMIT) {
5838 rb_str_resize(getpwnm_tmp, 0);
5843 bufnm = RSTRING_PTR(getpwnm_tmp);
5847 if (pwptr == NULL) {
5849 rb_str_resize(getpwnm_tmp, 0);
5855 rb_str_resize(getpwnm_tmp, 0);
5861 pwptr = getpwnam(login);
5884# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5888 uid_t ruid = getuid();
5890 struct passwd *pwptr;
5892# ifdef USE_GETPWUID_R
5894 struct passwd pwdid;
5896 long bufsizeid = GETPW_R_SIZE_INIT;
5899 bufsizeid = GETPW_R_SIZE_DEFAULT;
5903 bufid = RSTRING_PTR(getpwid_tmp);
5905 rb_str_set_len(getpwid_tmp, bufsizeid);
5909 while ((eid = getpwuid_r(ruid, &pwdid, bufid, bufsizeid, &pwptr)) != 0) {
5911 if (eid == ENOENT || eid== ESRCH || eid == EBADF || eid == EPERM) {
5913 rb_str_resize(getpwid_tmp, 0);
5917 if (eid != ERANGE || bufsizeid >= GETPW_R_SIZE_LIMIT) {
5918 rb_str_resize(getpwid_tmp, 0);
5923 bufid = RSTRING_PTR(getpwid_tmp);
5927 if (pwptr == NULL) {
5929 rb_str_resize(getpwid_tmp, 0);
5935 rb_str_resize(getpwid_tmp, 0);
5938# elif defined(USE_GETPWUID)
5941 pwptr = getpwuid(ruid);
5970#if defined(HAVE_PWD_H)
5973# ifdef USE_GETPWNAM_R
5986 struct passwd *pwptr;
5987#ifdef USE_GETPWNAM_R
5988 struct passwd pwbuf;
5993 getpw_buf_len = GETPW_R_SIZE_INIT;
5994 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5997 getpw_buf = RSTRING_PTR(*getpw_tmp);
5999 rb_str_set_len(*getpw_tmp, getpw_buf_len);
6001 while ((e = getpwnam_r(usrname, &pwbuf, getpw_buf, getpw_buf_len, &pwptr)) != 0) {
6002 if (e != ERANGE || getpw_buf_len >= GETPW_R_SIZE_LIMIT) {
6003 rb_str_resize(*getpw_tmp, 0);
6007 getpw_buf = RSTRING_PTR(*getpw_tmp);
6011 pwptr = getpwnam(usrname);
6014#ifndef USE_GETPWNAM_R
6017 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
6019 uid = pwptr->pw_uid;
6020#ifndef USE_GETPWNAM_R
6027# ifdef p_uid_from_name
6047#if defined(HAVE_GRP_H)
6050# ifdef USE_GETGRNAM_R
6063 struct group *grptr;
6064#ifdef USE_GETGRNAM_R
6070 getgr_buf_len = GETGR_R_SIZE_INIT;
6071 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6074 getgr_buf = RSTRING_PTR(*getgr_tmp);
6076 rb_str_set_len(*getgr_tmp, getgr_buf_len);
6078 while ((e = getgrnam_r(grpname, &grbuf, getgr_buf, getgr_buf_len, &grptr)) != 0) {
6079 if (e != ERANGE || getgr_buf_len >= GETGR_R_SIZE_LIMIT) {
6080 rb_str_resize(*getgr_tmp, 0);
6084 getgr_buf = RSTRING_PTR(*getgr_tmp);
6087#elif defined(HAVE_GETGRNAM)
6088 grptr = getgrnam(grpname);
6093#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6096 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6098 gid = grptr->gr_gid;
6099#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6106# ifdef p_gid_from_name
6126#if defined HAVE_SETUID
6140 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6144#define p_sys_setuid rb_f_notimplement
6148#if defined HAVE_SETRUID
6162 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6166#define p_sys_setruid rb_f_notimplement
6170#if defined HAVE_SETEUID
6184 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6188#define p_sys_seteuid rb_f_notimplement
6192#if defined HAVE_SETREUID
6207 rb_uid_t ruid, euid;
6210 ruid = OBJ2UID1(rid);
6211 euid = OBJ2UID1(eid);
6213 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6217#define p_sys_setreuid rb_f_notimplement
6221#if defined HAVE_SETRESUID
6236 rb_uid_t ruid, euid, suid;
6239 ruid = OBJ2UID1(rid);
6240 euid = OBJ2UID1(eid);
6241 suid = OBJ2UID1(sid);
6243 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6247#define p_sys_setresuid rb_f_notimplement
6264proc_getuid(
VALUE obj)
6266 rb_uid_t uid = getuid();
6271#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6291#if defined(HAVE_SETRESUID)
6292 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6293#elif defined HAVE_SETREUID
6294 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6295#elif defined HAVE_SETRUID
6296 if (setruid(uid) < 0) rb_sys_fail(0);
6297#elif defined HAVE_SETUID
6299 if (geteuid() == uid) {
6300 if (setuid(uid) < 0) rb_sys_fail(0);
6310#define proc_setuid rb_f_notimplement
6324static rb_uid_t SAVED_USER_ID = -1;
6326#ifdef BROKEN_SETREUID
6328setreuid(rb_uid_t ruid, rb_uid_t euid)
6330 if (ruid != (rb_uid_t)-1 && ruid != getuid()) {
6331 if (euid == (rb_uid_t)-1) euid = geteuid();
6332 if (setuid(ruid) < 0)
return -1;
6334 if (euid != (rb_uid_t)-1 && euid != geteuid()) {
6335 if (seteuid(euid) < 0)
return -1;
6363 if (geteuid() == 0) {
6364#if defined(HAVE_SETRESUID)
6365 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6366 SAVED_USER_ID = uid;
6367#elif defined(HAVE_SETUID)
6368 if (setuid(uid) < 0) rb_sys_fail(0);
6369 SAVED_USER_ID = uid;
6370#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6371 if (getuid() == uid) {
6372 if (SAVED_USER_ID == uid) {
6373 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6377 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6378 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6380 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6381 SAVED_USER_ID = uid;
6384 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6386 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6387 SAVED_USER_ID = uid;
6392 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6393 SAVED_USER_ID = uid;
6395#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6396 if (getuid() == uid) {
6397 if (SAVED_USER_ID == uid) {
6398 if (seteuid(uid) < 0) rb_sys_fail(0);
6402 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6404 if (setruid(0) < 0) rb_sys_fail(0);
6407 if (setruid(0) < 0) rb_sys_fail(0);
6409 if (seteuid(uid) < 0) rb_sys_fail(0);
6410 if (setruid(uid) < 0) rb_sys_fail(0);
6411 SAVED_USER_ID = uid;
6416 if (seteuid(uid) < 0) rb_sys_fail(0);
6417 if (setruid(uid) < 0) rb_sys_fail(0);
6418 SAVED_USER_ID = uid;
6426#if defined(HAVE_SETRESUID)
6427 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6428 (geteuid() == uid)? (rb_uid_t)-1: uid,
6429 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6430 SAVED_USER_ID = uid;
6431#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6432 if (SAVED_USER_ID == uid) {
6433 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6434 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6437 else if (getuid() != uid) {
6438 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6440 SAVED_USER_ID = uid;
6442 else if ( geteuid() != uid) {
6443 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6444 SAVED_USER_ID = uid;
6445 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6448 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6449 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6450 SAVED_USER_ID = uid;
6451 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6453#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6454 if (SAVED_USER_ID == uid) {
6455 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6456 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6458 else if ( geteuid() == uid) {
6459 if (getuid() != uid) {
6460 if (setruid(uid) < 0) rb_sys_fail(0);
6461 SAVED_USER_ID = uid;
6464 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6465 SAVED_USER_ID = uid;
6466 if (setruid(uid) < 0) rb_sys_fail(0);
6469 else if ( getuid() == uid) {
6470 if (seteuid(uid) < 0) rb_sys_fail(0);
6471 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6472 SAVED_USER_ID = uid;
6473 if (setruid(uid) < 0) rb_sys_fail(0);
6478#elif defined HAVE_44BSD_SETUID
6479 if (getuid() == uid) {
6481 if (setuid(uid) < 0) rb_sys_fail(0);
6482 SAVED_USER_ID = uid;
6487#elif defined HAVE_SETEUID
6488 if (getuid() == uid && SAVED_USER_ID == uid) {
6489 if (seteuid(uid) < 0) rb_sys_fail(0);
6494#elif defined HAVE_SETUID
6495 if (getuid() == uid && SAVED_USER_ID == uid) {
6496 if (setuid(uid) < 0) rb_sys_fail(0);
6510#if defined HAVE_SETGID
6524 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6528#define p_sys_setgid rb_f_notimplement
6532#if defined HAVE_SETRGID
6546 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6550#define p_sys_setrgid rb_f_notimplement
6554#if defined HAVE_SETEGID
6568 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6572#define p_sys_setegid rb_f_notimplement
6576#if defined HAVE_SETREGID
6591 rb_gid_t rgid, egid;
6593 rgid = OBJ2GID(rid);
6594 egid = OBJ2GID(eid);
6595 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6599#define p_sys_setregid rb_f_notimplement
6602#if defined HAVE_SETRESGID
6617 rb_gid_t rgid, egid, sgid;
6619 rgid = OBJ2GID(rid);
6620 egid = OBJ2GID(eid);
6621 sgid = OBJ2GID(sid);
6622 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6626#define p_sys_setresgid rb_f_notimplement
6630#if defined HAVE_ISSETUGID
6644p_sys_issetugid(
VALUE obj)
6646 return RBOOL(issetugid());
6649#define p_sys_issetugid rb_f_notimplement
6666proc_getgid(
VALUE obj)
6668 rb_gid_t gid = getgid();
6673#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6692#if defined(HAVE_SETRESGID)
6693 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6694#elif defined HAVE_SETREGID
6695 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6696#elif defined HAVE_SETRGID
6697 if (setrgid(gid) < 0) rb_sys_fail(0);
6698#elif defined HAVE_SETGID
6700 if (getegid() == gid) {
6701 if (setgid(gid) < 0) rb_sys_fail(0);
6711#define proc_setgid rb_f_notimplement
6715#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6735static int _maxgroups = -1;
6737get_sc_ngroups_max(
void)
6739#ifdef _SC_NGROUPS_MAX
6740 return (
int)sysconf(_SC_NGROUPS_MAX);
6741#elif defined(NGROUPS_MAX)
6742 return (
int)NGROUPS_MAX;
6750 if (_maxgroups < 0) {
6751 _maxgroups = get_sc_ngroups_max();
6753 _maxgroups = RB_MAX_GROUPS;
6762#ifdef HAVE_GETGROUPS
6786proc_getgroups(
VALUE obj)
6792 ngroups = getgroups(0, NULL);
6796 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6798 ngroups = getgroups(ngroups, groups);
6803 for (i = 0; i < ngroups; i++)
6804 rb_ary_push(ary,
GIDT2NUM(groups[i]));
6811#define proc_getgroups rb_f_notimplement
6815#ifdef HAVE_SETGROUPS
6840 if (ngroups > maxgroups())
6841 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6843 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6845 for (i = 0; i < ngroups; i++) {
6848 groups[i] = OBJ2GID1(g);
6852 if (setgroups(ngroups, groups) == -1)
6857 return proc_getgroups(obj);
6860#define proc_setgroups rb_f_notimplement
6864#ifdef HAVE_INITGROUPS
6890 return proc_getgroups(obj);
6893#define proc_initgroups rb_f_notimplement
6896#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6909proc_getmaxgroups(
VALUE obj)
6914#define proc_getmaxgroups rb_f_notimplement
6917#ifdef HAVE_SETGROUPS
6930 int ngroups_max = get_sc_ngroups_max();
6933 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6935 if (ngroups > RB_MAX_GROUPS)
6936 ngroups = RB_MAX_GROUPS;
6938 if (ngroups_max > 0 && ngroups > ngroups_max)
6939 ngroups = ngroups_max;
6941 _maxgroups = ngroups;
6946#define proc_setmaxgroups rb_f_notimplement
6949#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6950static int rb_daemon(
int nochdir,
int noclose);
6975 int n, nochdir = FALSE, noclose = FALSE;
6978 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6979 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6983 n = rb_daemon(nochdir, noclose);
6984 if (n < 0) rb_sys_fail(
"daemon");
6988extern const char ruby_null_device[];
6991rb_daemon(
int nochdir,
int noclose)
6996 err = daemon(nochdir, noclose);
7001 switch (rb_fork_ruby(NULL)) {
7004 default: _exit(EXIT_SUCCESS);
7008 if (setsid() < 0) (void)0;
7013 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
7025#define proc_daemon rb_f_notimplement
7038static rb_gid_t SAVED_GROUP_ID = -1;
7040#ifdef BROKEN_SETREGID
7042setregid(rb_gid_t rgid, rb_gid_t egid)
7044 if (rgid != (rb_gid_t)-1 && rgid != getgid()) {
7045 if (egid == (rb_gid_t)-1) egid = getegid();
7046 if (setgid(rgid) < 0)
return -1;
7048 if (egid != (rb_gid_t)-1 && egid != getegid()) {
7049 if (setegid(egid) < 0)
return -1;
7077 if (geteuid() == 0) {
7078#if defined(HAVE_SETRESGID)
7079 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
7080 SAVED_GROUP_ID = gid;
7081#elif defined HAVE_SETGID
7082 if (setgid(gid) < 0) rb_sys_fail(0);
7083 SAVED_GROUP_ID = gid;
7084#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7085 if (getgid() == gid) {
7086 if (SAVED_GROUP_ID == gid) {
7087 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7091 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7092 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
7094 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7095 SAVED_GROUP_ID = gid;
7098 if (setregid(0, 0) < 0) rb_sys_fail(0);
7100 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7101 SAVED_GROUP_ID = gid;
7106 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7107 SAVED_GROUP_ID = gid;
7109#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7110 if (getgid() == gid) {
7111 if (SAVED_GROUP_ID == gid) {
7112 if (setegid(gid) < 0) rb_sys_fail(0);
7116 if (setegid(gid) < 0) rb_sys_fail(0);
7117 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7119 if (setrgid(0) < 0) rb_sys_fail(0);
7122 if (setrgid(0) < 0) rb_sys_fail(0);
7124 if (setegid(gid) < 0) rb_sys_fail(0);
7125 if (setrgid(gid) < 0) rb_sys_fail(0);
7126 SAVED_GROUP_ID = gid;
7131 if (setegid(gid) < 0) rb_sys_fail(0);
7132 if (setrgid(gid) < 0) rb_sys_fail(0);
7133 SAVED_GROUP_ID = gid;
7140#if defined(HAVE_SETRESGID)
7141 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7142 (getegid() == gid)? (rb_gid_t)-1: gid,
7143 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7144 SAVED_GROUP_ID = gid;
7145#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7146 if (SAVED_GROUP_ID == gid) {
7147 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7148 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7151 else if (getgid() != gid) {
7152 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7154 SAVED_GROUP_ID = gid;
7156 else if ( getegid() != gid) {
7157 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7158 SAVED_GROUP_ID = gid;
7159 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7162 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7163 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7164 SAVED_GROUP_ID = gid;
7165 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7167#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7168 if (SAVED_GROUP_ID == gid) {
7169 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7170 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7172 else if ( getegid() == gid) {
7173 if (getgid() != gid) {
7174 if (setrgid(gid) < 0) rb_sys_fail(0);
7175 SAVED_GROUP_ID = gid;
7178 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7179 SAVED_GROUP_ID = gid;
7180 if (setrgid(gid) < 0) rb_sys_fail(0);
7183 else if ( getgid() == gid) {
7184 if (setegid(gid) < 0) rb_sys_fail(0);
7185 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7186 SAVED_GROUP_ID = gid;
7187 if (setrgid(gid) < 0) rb_sys_fail(0);
7192#elif defined HAVE_44BSD_SETGID
7193 if (getgid() == gid) {
7195 if (setgid(gid) < 0) rb_sys_fail(0);
7196 SAVED_GROUP_ID = gid;
7201#elif defined HAVE_SETEGID
7202 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7203 if (setegid(gid) < 0) rb_sys_fail(0);
7208#elif defined HAVE_SETGID
7209 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7210 if (setgid(gid) < 0) rb_sys_fail(0);
7237proc_geteuid(
VALUE obj)
7239 rb_uid_t euid = geteuid();
7243#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7245proc_seteuid(rb_uid_t uid)
7247#if defined(HAVE_SETRESUID)
7248 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7249#elif defined HAVE_SETREUID
7250 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7251#elif defined HAVE_SETEUID
7252 if (seteuid(uid) < 0) rb_sys_fail(0);
7253#elif defined HAVE_SETUID
7254 if (uid == getuid()) {
7255 if (setuid(uid) < 0) rb_sys_fail(0);
7266#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7280 proc_seteuid(OBJ2UID(euid));
7284#define proc_seteuid_m rb_f_notimplement
7288rb_seteuid_core(rb_uid_t euid)
7290#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7296#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7300#if defined(HAVE_SETRESUID)
7302 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7303 SAVED_USER_ID = euid;
7306 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7308#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7309 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7311 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7312 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7313 SAVED_USER_ID = euid;
7315#elif defined HAVE_SETEUID
7316 if (seteuid(euid) < 0) rb_sys_fail(0);
7317#elif defined HAVE_SETUID
7318 if (geteuid() == 0) rb_sys_fail(0);
7319 if (setuid(euid) < 0) rb_sys_fail(0);
7344 rb_seteuid_core(OBJ2UID(
id));
7363proc_getegid(
VALUE obj)
7365 rb_gid_t egid = getegid();
7370#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7383#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7389#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7390 gid = OBJ2GID(egid);
7393#if defined(HAVE_SETRESGID)
7394 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7395#elif defined HAVE_SETREGID
7396 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7397#elif defined HAVE_SETEGID
7398 if (setegid(gid) < 0) rb_sys_fail(0);
7399#elif defined HAVE_SETGID
7400 if (gid == getgid()) {
7401 if (setgid(gid) < 0) rb_sys_fail(0);
7413#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7414#define proc_setegid_m proc_setegid
7416#define proc_setegid_m rb_f_notimplement
7420rb_setegid_core(rb_gid_t egid)
7422#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7428#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7432#if defined(HAVE_SETRESGID)
7434 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7435 SAVED_GROUP_ID = egid;
7438 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7440#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7441 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7443 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7444 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7445 SAVED_GROUP_ID = egid;
7447#elif defined HAVE_SETEGID
7448 if (setegid(egid) < 0) rb_sys_fail(0);
7449#elif defined HAVE_SETGID
7450 if (geteuid() == 0 ) rb_sys_fail(0);
7451 if (setgid(egid) < 0) rb_sys_fail(0);
7476 rb_setegid_core(OBJ2GID(
id));
7491p_uid_exchangeable(
VALUE _)
7493#if defined(HAVE_SETRESUID)
7495#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7516p_uid_exchange(
VALUE obj)
7519#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7526#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7530#if defined(HAVE_SETRESUID)
7531 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7532 SAVED_USER_ID = uid;
7533#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7534 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7535 SAVED_USER_ID = uid;
7553p_gid_exchangeable(
VALUE _)
7555#if defined(HAVE_SETRESGID)
7557#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7578p_gid_exchange(
VALUE obj)
7581#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7588#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7592#if defined(HAVE_SETRESGID)
7593 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7594 SAVED_GROUP_ID = gid;
7595#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7596 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7597 SAVED_GROUP_ID = gid;
7616p_uid_have_saved_id(
VALUE _)
7618#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7626#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7628p_uid_sw_ensure(
VALUE i)
7630 rb_uid_t
id = (rb_uid_t)i;
7631 under_uid_switch = 0;
7632 id = rb_seteuid_core(
id);
7651p_uid_switch(
VALUE obj)
7663 under_uid_switch = 1;
7670 else if (euid != SAVED_USER_ID) {
7671 proc_seteuid(SAVED_USER_ID);
7673 under_uid_switch = 1;
7688p_uid_sw_ensure(
VALUE obj)
7690 under_uid_switch = 0;
7691 return p_uid_exchange(obj);
7695p_uid_switch(
VALUE obj)
7707 p_uid_exchange(obj);
7709 under_uid_switch = 1;
7731p_gid_have_saved_id(
VALUE _)
7733#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7740#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7742p_gid_sw_ensure(
VALUE i)
7744 rb_gid_t
id = (rb_gid_t)i;
7745 under_gid_switch = 0;
7746 id = rb_setegid_core(
id);
7765p_gid_switch(
VALUE obj)
7777 under_gid_switch = 1;
7784 else if (egid != SAVED_GROUP_ID) {
7785 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7787 under_gid_switch = 1;
7802p_gid_sw_ensure(
VALUE obj)
7804 under_gid_switch = 0;
7805 return p_gid_exchange(obj);
7809p_gid_switch(
VALUE obj)
7821 p_gid_exchange(obj);
7823 under_gid_switch = 1;
7833#if defined(HAVE_TIMES)
7837#ifdef HAVE__SC_CLK_TCK
7838 return sysconf(_SC_CLK_TCK);
7839#elif defined CLK_TCK
7862rb_proc_times(
VALUE obj)
7864 VALUE utime, stime, cutime, cstime, ret;
7865#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7866 struct rusage usage_s, usage_c;
7868 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7869 rb_sys_fail(
"getrusage");
7870 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7871 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7872 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7873 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7875 const double hertz = (double)get_clk_tck();
7879 utime =
DBL2NUM(buf.tms_utime / hertz);
7880 stime =
DBL2NUM(buf.tms_stime / hertz);
7881 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7882 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7884 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7892#define rb_proc_times rb_f_notimplement
7895#ifdef HAVE_LONG_LONG
7897#define TIMETICK_INT_MIN LLONG_MIN
7898#define TIMETICK_INT_MAX LLONG_MAX
7899#define TIMETICK_INT2NUM(v) LL2NUM(v)
7900#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7902typedef long timetick_int_t;
7903#define TIMETICK_INT_MIN LONG_MIN
7904#define TIMETICK_INT_MAX LONG_MAX
7905#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7906#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7909CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7910static timetick_int_t
7911gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7931reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7933 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7941reduce_factors(timetick_int_t *numerators,
int num_numerators,
7942 timetick_int_t *denominators,
int num_denominators)
7945 for (i = 0; i < num_numerators; i++) {
7946 if (numerators[i] == 1)
7948 for (j = 0; j < num_denominators; j++) {
7949 if (denominators[j] == 1)
7951 reduce_fraction(&numerators[i], &denominators[j]);
7957 timetick_int_t giga_count;
7962timetick2dblnum(
struct timetick *ttp,
7963 timetick_int_t *numerators,
int num_numerators,
7964 timetick_int_t *denominators,
int num_denominators)
7969 reduce_factors(numerators, num_numerators,
7970 denominators, num_denominators);
7972 d = ttp->giga_count * 1e9 + ttp->count;
7974 for (i = 0; i < num_numerators; i++)
7976 for (i = 0; i < num_denominators; i++)
7977 d /= denominators[i];
7983timetick2dblnum_reciprocal(
struct timetick *ttp,
7984 timetick_int_t *numerators,
int num_numerators,
7985 timetick_int_t *denominators,
int num_denominators)
7990 reduce_factors(numerators, num_numerators,
7991 denominators, num_denominators);
7994 for (i = 0; i < num_denominators; i++)
7995 d *= denominators[i];
7996 for (i = 0; i < num_numerators; i++)
7998 d /= ttp->giga_count * 1e9 + ttp->count;
8003#define NDIV(x,y) (-(-((x)+1)/(y))-1)
8004#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
8007timetick2integer(
struct timetick *ttp,
8008 timetick_int_t *numerators,
int num_numerators,
8009 timetick_int_t *denominators,
int num_denominators)
8014 reduce_factors(numerators, num_numerators,
8015 denominators, num_denominators);
8017 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
8018 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
8019 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
8020 for (i = 0; i < num_numerators; i++) {
8021 timetick_int_t factor = numerators[i];
8022 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
8026 for (i = 0; i < num_denominators; i++) {
8027 t = DIV(t, denominators[i]);
8029 return TIMETICK_INT2NUM(t);
8033 v = TIMETICK_INT2NUM(ttp->giga_count);
8036 for (i = 0; i < num_numerators; i++) {
8037 timetick_int_t factor = numerators[i];
8040 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
8042 for (i = 0; i < num_denominators; i++) {
8043 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
8049make_clock_result(
struct timetick *ttp,
8050 timetick_int_t *numerators,
int num_numerators,
8051 timetick_int_t *denominators,
int num_denominators,
8054 if (unit ==
ID2SYM(id_nanosecond)) {
8055 numerators[num_numerators++] = 1000000000;
8056 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8058 else if (unit ==
ID2SYM(id_microsecond)) {
8059 numerators[num_numerators++] = 1000000;
8060 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8062 else if (unit ==
ID2SYM(id_millisecond)) {
8063 numerators[num_numerators++] = 1000;
8064 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8066 else if (unit ==
ID2SYM(id_second)) {
8067 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8069 else if (unit ==
ID2SYM(id_float_microsecond)) {
8070 numerators[num_numerators++] = 1000000;
8071 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8073 else if (unit ==
ID2SYM(id_float_millisecond)) {
8074 numerators[num_numerators++] = 1000;
8075 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8077 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
8078 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8081 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
8085static const mach_timebase_info_data_t *
8086get_mach_timebase_info(
void)
8088 static mach_timebase_info_data_t sTimebaseInfo;
8090 if ( sTimebaseInfo.denom == 0 ) {
8091 (void) mach_timebase_info(&sTimebaseInfo);
8094 return &sTimebaseInfo;
8098ruby_real_ms_time(
void)
8100 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8101 uint64_t t = mach_absolute_time();
8102 return (
double)t * info->numer / info->denom / 1e6;
8106#if defined(NUM2CLOCKID)
8107# define NUMERIC_CLOCKID 1
8109# define NUMERIC_CLOCKID 0
8110# define NUM2CLOCKID(x) 0
8113#define clock_failed(name, err, arg) do { \
8114 int clock_error = (err); \
8115 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8288 timetick_int_t numerators[2];
8289 timetick_int_t denominators[2];
8290 int num_numerators = 0;
8291 int num_denominators = 0;
8294 VALUE clk_id = argv[0];
8295#ifdef HAVE_CLOCK_GETTIME
8300#ifdef CLOCK_REALTIME
8301 if (clk_id == RUBY_CLOCK_REALTIME) {
8307#ifdef CLOCK_MONOTONIC
8308 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8309 c = CLOCK_MONOTONIC;
8314#ifdef CLOCK_PROCESS_CPUTIME_ID
8315 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8316 c = CLOCK_PROCESS_CPUTIME_ID;
8321#ifdef CLOCK_THREAD_CPUTIME_ID
8322 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8323 c = CLOCK_THREAD_CPUTIME_ID;
8331#ifdef HAVE_GETTIMEOFDAY
8336#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8337 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8339 ret = gettimeofday(&tv, 0);
8341 rb_sys_fail(
"gettimeofday");
8342 tt.giga_count = tv.tv_sec;
8343 tt.count = (int32_t)tv.tv_usec * 1000;
8344 denominators[num_denominators++] = 1000000000;
8349#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8350 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8353 if (t == (time_t)-1)
8354 rb_sys_fail(
"time");
8357 denominators[num_denominators++] = 1000000000;
8362#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8363 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8364 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8367 unsigned_clock_t uc;
8369 if (c == (clock_t)-1)
8370 rb_sys_fail(
"times");
8371 uc = (unsigned_clock_t)c;
8372 tt.count = (int32_t)(uc % 1000000000);
8373 tt.giga_count = (uc / 1000000000);
8374 denominators[num_denominators++] = get_clk_tck();
8380#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8381 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8382 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8383 struct rusage usage;
8385 ret = getrusage(RUSAGE_SELF, &usage);
8387 rb_sys_fail(
"getrusage");
8388 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8389 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8390 if (1000000 <= usec) {
8394 tt.count = usec * 1000;
8395 denominators[num_denominators++] = 1000000000;
8401#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8402 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8403 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8405 unsigned_clock_t utime, stime;
8406 if (times(&buf) == (clock_t)-1)
8407 rb_sys_fail(
"times");
8408 utime = (unsigned_clock_t)buf.tms_utime;
8409 stime = (unsigned_clock_t)buf.tms_stime;
8410 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8411 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8412 if (1000000000 <= tt.count) {
8413 tt.count -= 1000000000;
8416 denominators[num_denominators++] = get_clk_tck();
8421#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8422 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8423 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8425 unsigned_clock_t uc;
8428 if (c == (clock_t)-1)
8429 rb_sys_fail(
"clock");
8430 uc = (unsigned_clock_t)c;
8431 tt.count = (int32_t)(uc % 1000000000);
8432 tt.giga_count = uc / 1000000000;
8433 denominators[num_denominators++] = CLOCKS_PER_SEC;
8438 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8439 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8440 uint64_t t = mach_absolute_time();
8441 tt.count = (int32_t)(t % 1000000000);
8442 tt.giga_count = t / 1000000000;
8443 numerators[num_numerators++] = info->numer;
8444 denominators[num_denominators++] = info->denom;
8445 denominators[num_denominators++] = 1000000000;
8450 else if (NUMERIC_CLOCKID) {
8451#if defined(HAVE_CLOCK_GETTIME)
8453 c = NUM2CLOCKID(clk_id);
8455 ret = clock_gettime(c, &ts);
8457 clock_failed(
"gettime",
errno, clk_id);
8458 tt.count = (int32_t)ts.tv_nsec;
8459 tt.giga_count = ts.tv_sec;
8460 denominators[num_denominators++] = 1000000000;
8467 clock_failed(
"gettime", EINVAL, clk_id);
8470 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8515 timetick_int_t numerators[2];
8516 timetick_int_t denominators[2];
8517 int num_numerators = 0;
8518 int num_denominators = 0;
8519#ifdef HAVE_CLOCK_GETRES
8524 VALUE clk_id = argv[0];
8527#ifdef CLOCK_REALTIME
8528 if (clk_id == RUBY_CLOCK_REALTIME) {
8534#ifdef CLOCK_MONOTONIC
8535 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8536 c = CLOCK_MONOTONIC;
8541#ifdef CLOCK_PROCESS_CPUTIME_ID
8542 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8543 c = CLOCK_PROCESS_CPUTIME_ID;
8548#ifdef CLOCK_THREAD_CPUTIME_ID
8549 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8550 c = CLOCK_THREAD_CPUTIME_ID;
8555#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8556 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8559 denominators[num_denominators++] = 1000000000;
8564#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8565 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8568 denominators[num_denominators++] = 1000000000;
8573#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8574 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8577 denominators[num_denominators++] = get_clk_tck();
8582#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8583 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8586 denominators[num_denominators++] = 1000000000;
8591#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8592 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8595 denominators[num_denominators++] = get_clk_tck();
8600#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8601 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8604 denominators[num_denominators++] = CLOCKS_PER_SEC;
8609#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8610 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8611 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8614 numerators[num_numerators++] = info->numer;
8615 denominators[num_denominators++] = info->denom;
8616 denominators[num_denominators++] = 1000000000;
8621 else if (NUMERIC_CLOCKID) {
8622#if defined(HAVE_CLOCK_GETRES)
8624 c = NUM2CLOCKID(clk_id);
8626 ret = clock_getres(c, &ts);
8628 clock_failed(
"getres",
errno, clk_id);
8629 tt.count = (int32_t)ts.tv_nsec;
8630 tt.giga_count = ts.tv_sec;
8631 denominators[num_denominators++] = 1000000000;
8638 clock_failed(
"getres", EINVAL, clk_id);
8641 if (unit ==
ID2SYM(id_hertz)) {
8642 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8645 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8650get_CHILD_STATUS(
ID _x,
VALUE *_y)
8656get_PROCESS_ID(
ID _x,
VALUE *_y)
8748static VALUE rb_mProcUID;
8749static VALUE rb_mProcGID;
8750static VALUE rb_mProcID_Syscall;
8782 rb_gc_prepare_heap();
9117 rb_gvar_ractor_local(
"$$");
9118 rb_gvar_ractor_local(
"$?");
9173 process_status_dump, process_status_load);
9211#ifdef HAVE_GETPRIORITY
9222#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9224 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9225#ifdef RLIM_SAVED_MAX
9227 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9234#ifdef RLIM_SAVED_CUR
9236 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9277#ifdef RLIMIT_MEMLOCK
9284#ifdef RLIMIT_MSGQUEUE
9350#ifdef RLIMIT_SIGPENDING
9385#if defined(RUBY_CLOCK_REALTIME)
9386#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9387# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9388#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9389# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9391#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9394#elif defined(RUBY_CLOCK_REALTIME)
9398#if defined(RUBY_CLOCK_MONOTONIC)
9399#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9400# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9402#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9405#elif defined(RUBY_CLOCK_MONOTONIC)
9409#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9410#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9411# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9413#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9416#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9420#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9423#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9436#ifdef CLOCK_REALTIME_FAST
9440#ifdef CLOCK_REALTIME_PRECISE
9444#ifdef CLOCK_REALTIME_COARSE
9448#ifdef CLOCK_REALTIME_ALARM
9452#ifdef CLOCK_MONOTONIC_FAST
9456#ifdef CLOCK_MONOTONIC_PRECISE
9460#ifdef CLOCK_MONOTONIC_RAW
9464#ifdef CLOCK_MONOTONIC_RAW_APPROX
9468#ifdef CLOCK_MONOTONIC_COARSE
9472#ifdef CLOCK_BOOTTIME
9476#ifdef CLOCK_BOOTTIME_ALARM
9484#ifdef CLOCK_UPTIME_FAST
9488#ifdef CLOCK_UPTIME_PRECISE
9492#ifdef CLOCK_UPTIME_RAW
9496#ifdef CLOCK_UPTIME_RAW_APPROX
9512#if defined(HAVE_TIMES) || defined(_WIN32)
9526 SAVED_USER_ID = geteuid();
9527 SAVED_GROUP_ID = getegid();
9550#ifdef p_uid_from_name
9553#ifdef p_gid_from_name
9584#define define_id(name) id_##name = rb_intern_const(#name)
9597 define_id(new_pgroup);
9599 define_id(unsetenv_others);
9602 define_id(close_others);
9603 define_id(nanosecond);
9604 define_id(microsecond);
9605 define_id(millisecond);
9607 define_id(float_microsecond);
9608 define_id(float_millisecond);
9609 define_id(float_second);
9610 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9611 define_id(TIME_BASED_CLOCK_REALTIME);
9612#ifdef CLOCK_REALTIME
9613 define_id(CLOCK_REALTIME);
9615#ifdef CLOCK_MONOTONIC
9616 define_id(CLOCK_MONOTONIC);
9618#ifdef CLOCK_PROCESS_CPUTIME_ID
9619 define_id(CLOCK_PROCESS_CPUTIME_ID);
9621#ifdef CLOCK_THREAD_CPUTIME_ID
9622 define_id(CLOCK_THREAD_CPUTIME_ID);
9625 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9626 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9629 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9631 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9633 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #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_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.
int rb_block_given_p(void)
Determines if the current method is given a block.
#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 rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#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)
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
VALUE rb_eSystemExit
SystemExit exception.
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_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_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.
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_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.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#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.
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.
int rb_proc_exec(const char *cmd)
Executes a shell command.
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.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
Just another name of rb_str_cat.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_str_new_cstr(str)
Identical to rb_str_new, 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.
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_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.
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_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.
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.
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.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
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.
VALUE rb_thread_create(type *q, void *w)
Creates a rb_cThread instance.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#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 SafeStringValue(v)
#define StringValue(v)
Ensures that the parameter object is a 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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
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.