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"
117#include "ractor_core.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)
363#ifdef HAVE_WORKING_FORK
367static rb_pid_t cached_pid;
370#if defined(__sun) && !defined(_XPG7)
371#define execv(path, argv) (rb_async_bug_errno("unreachable: async-signal-unsafe execv() is called", 0))
372#define execl(path, arg0, arg1, arg2, term) do { extern char **environ; execle((path), (arg0), (arg1), (arg2), (term), (environ)); } while (0)
373#define ALWAYS_NEED_ENVP 1
375#define ALWAYS_NEED_ENVP 0
379assert_close_on_exec(
int fd)
382#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(FD_CLOEXEC)
383 int flags = fcntl(fd, F_GETFD);
385 static const char m[] =
"reserved FD closed unexpectedly?\n";
386 (void)!write(2, m,
sizeof(m) - 1);
389 if (flags & FD_CLOEXEC)
return;
390 rb_bug(
"reserved FD did not have close-on-exec set");
392 rb_bug(
"reserved FD without close-on-exec support");
398close_unless_reserved(
int fd)
401 assert_close_on_exec(fd);
408#if defined(DEBUG_REDIRECT)
411ttyprintf(
const char *fmt, ...)
417 tty = fopen(
"con",
"w");
419 tty = fopen(
"/dev/tty",
"w");
425 vfprintf(tty, fmt, ap);
432redirect_dup(
int oldfd)
436 ttyprintf(
"dup(%d) => %d\n", oldfd, ret);
441redirect_dup2(
int oldfd,
int newfd)
444 ret = dup2(oldfd, newfd);
445 ttyprintf(
"dup2(%d, %d) => %d\n", oldfd, newfd, ret);
450redirect_cloexec_dup(
int oldfd)
454 ttyprintf(
"cloexec_dup(%d) => %d\n", oldfd, ret);
459redirect_cloexec_dup2(
int oldfd,
int newfd)
463 ttyprintf(
"cloexec_dup2(%d, %d) => %d\n", oldfd, newfd, ret);
468redirect_close(
int fd)
471 ret = close_unless_reserved(fd);
472 ttyprintf(
"close(%d) => %d\n", fd, ret);
477parent_redirect_open(
const char *pathname,
int flags, mode_t perm)
481 ttyprintf(
"parent_open(\"%s\", 0x%x, 0%o) => %d\n", pathname, flags, perm, ret);
486parent_redirect_close(
int fd)
489 ret = close_unless_reserved(fd);
490 ttyprintf(
"parent_close(%d) => %d\n", fd, ret);
495#define redirect_dup(oldfd) dup(oldfd)
496#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
497#define redirect_cloexec_dup(oldfd) rb_cloexec_dup(oldfd)
498#define redirect_cloexec_dup2(oldfd, newfd) rb_cloexec_dup2((oldfd), (newfd))
499#define redirect_close(fd) close_unless_reserved(fd)
500#define parent_redirect_open(pathname, flags, perm) rb_cloexec_open((pathname), (flags), (perm))
501#define parent_redirect_close(fd) close_unless_reserved(fd)
507 if (UNLIKELY(!cached_pid)) {
508 cached_pid = getpid();
514#if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
588static VALUE rb_cProcessStatus;
597 .wrap_struct_name =
"Process::Status",
603 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
607rb_process_status_allocate(
VALUE klass)
616 return GET_THREAD()->last_status;
642proc_s_last_status(
VALUE mod)
648rb_process_status_new(rb_pid_t pid,
int status,
int error)
650 VALUE last_status = rb_process_status_allocate(rb_cProcessStatus);
653 data->status = status;
661process_status_dump(
VALUE status)
674process_status_load(
VALUE real_obj,
VALUE load_obj)
677 VALUE status = rb_attr_get(load_obj, id_status);
678 VALUE pid = rb_attr_get(load_obj, id_pid);
687 GET_THREAD()->last_status = rb_process_status_new(pid, status, 0);
691last_status_clear(rb_thread_t *th)
693 th->last_status =
Qnil;
697rb_last_status_clear(
void)
699 last_status_clear(GET_THREAD());
711pst_status(
VALUE status)
731 int status = pst_status(self);
735#define PST2INT(st) pst_status(st)
751 rb_pid_t pid = pst_pid(self);
755static VALUE pst_message_status(
VALUE str,
int status);
758pst_message(
VALUE str, rb_pid_t pid,
int status)
760 rb_str_catf(str,
"pid %ld", (
long)pid);
761 pst_message_status(str, status);
765pst_message_status(
VALUE str,
int status)
767 if (WIFSTOPPED(status)) {
768 int stopsig = WSTOPSIG(status);
771 rb_str_catf(str,
" stopped SIG%s (signal %d)", signame, stopsig);
774 rb_str_catf(str,
" stopped signal %d", stopsig);
777 if (WIFSIGNALED(status)) {
778 int termsig = WTERMSIG(status);
781 rb_str_catf(str,
" SIG%s (signal %d)", signame, termsig);
784 rb_str_catf(str,
" signal %d", termsig);
787 if (WIFEXITED(status)) {
788 rb_str_catf(str,
" exit %d", WEXITSTATUS(status));
791 if (WCOREDUMP(status)) {
819 status = PST2INT(st);
822 pst_message(str, pid, status);
849 status = PST2INT(st);
852 pst_message(str, pid, status);
874 if (st1 == st2)
return Qtrue;
875 return rb_equal(pst_to_i(st1), st2);
889pst_wifstopped(
VALUE st)
891 int status = PST2INT(st);
893 return RBOOL(WIFSTOPPED(status));
906pst_wstopsig(
VALUE st)
908 int status = PST2INT(st);
910 if (WIFSTOPPED(status))
911 return INT2NUM(WSTOPSIG(status));
925pst_wifsignaled(
VALUE st)
927 int status = PST2INT(st);
929 return RBOOL(WIFSIGNALED(status));
942pst_wtermsig(
VALUE st)
944 int status = PST2INT(st);
946 if (WIFSIGNALED(status))
947 return INT2NUM(WTERMSIG(status));
962pst_wifexited(
VALUE st)
964 int status = PST2INT(st);
966 return RBOOL(WIFEXITED(status));
984pst_wexitstatus(
VALUE st)
986 int status = PST2INT(st);
988 if (WIFEXITED(status))
989 return INT2NUM(WEXITSTATUS(status));
1007pst_success_p(
VALUE st)
1009 int status = PST2INT(st);
1011 if (!WIFEXITED(status))
1013 return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
1028pst_wcoredump(
VALUE st)
1031 int status = PST2INT(st);
1033 return RBOOL(WCOREDUMP(status));
1040do_waitpid(rb_pid_t pid,
int *st,
int flags)
1042#if defined HAVE_WAITPID
1043 return waitpid(pid, st, flags);
1044#elif defined HAVE_WAIT4
1045 return wait4(pid, st, flags, NULL);
1047# error waitpid or wait4 is required.
1052 struct ccan_list_node wnode;
1053 rb_execution_context_t *ec;
1054 rb_nativethread_cond_t *cond;
1063waitpid_state_init(
struct waitpid_state *w, rb_pid_t pid,
int options)
1067 w->options = options;
1073waitpid_blocking_no_SIGCHLD(
void *x)
1077 w->ret = do_waitpid(w->pid, &w->status, w->options);
1085 if (w->options & WNOHANG) {
1086 w->ret = do_waitpid(w->pid, &w->status, w->options);
1091 }
while (w->ret < 0 &&
errno == EINTR && (RUBY_VM_CHECK_INTS(w->ec),1));
1101 if (!(flags & WNOHANG)) {
1103 if (scheduler !=
Qnil) {
1105 if (!UNDEF_P(result))
return result;
1152rb_process_status_waitv(
int argc,
VALUE *argv,
VALUE _)
1154 rb_check_arity(argc, 0, 2);
1174 if (
NIL_P(status))
return 0;
1179 if (st) *st = data->status;
1182 errno = data->error;
1185 GET_THREAD()->last_status = status;
1192proc_wait(
int argc,
VALUE *argv)
1198 if (rb_check_arity(argc, 0, 2) == 0) {
1204 if (argc == 2 && !
NIL_P(vflags = argv[1])) {
1209 if ((pid =
rb_waitpid(pid, &status, flags)) < 0)
1213 rb_last_status_clear();
1370 return proc_wait(c, v);
1390 VALUE pid = proc_wait(argc, argv);
1419 rb_last_status_clear();
1434static VALUE rb_cWaiter;
1437detach_process_pid(
VALUE thread)
1443detach_process_watcher(
void *arg)
1445 rb_pid_t cpid, pid = (rb_pid_t)(
VALUE)arg;
1448 while ((cpid =
rb_waitpid(pid, &status, 0)) == 0) {
1459 RBASIC_SET_CLASS(watcher, rb_cWaiter);
1511before_exec_async_signal_safe(
void)
1516before_exec_non_async_signal_safe(
void)
1527 rb_thread_stop_timer_thread();
1530#define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
1532int rb_w32_set_nonblock2(
int fd,
int nonblock);
1539 return rb_w32_set_nonblock2(fd, 0);
1540#elif defined(F_GETFL) && defined(F_SETFL)
1541 int fl = fcntl(fd, F_GETFL);
1544 if (fl == -1)
return fl;
1545 if (fl & O_NONBLOCK) {
1547 return fcntl(fd, F_SETFL, fl);
1554stdfd_clear_nonblock(
void)
1558 for (fd = 0; fd < 3; fd++) {
1559 (void)set_blocking(fd);
1566 before_exec_non_async_signal_safe();
1567 before_exec_async_signal_safe();
1573 rb_thread_reset_timer_thread();
1574 rb_thread_start_timer_thread();
1577#if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
1579before_fork_ruby(
void)
1582 rb_gc_before_fork();
1586after_fork_ruby(rb_pid_t pid)
1588 rb_gc_after_fork(pid);
1602release_fork_lock_after_fork(rb_pid_t pid)
1605 rb_thread_reset_fork_lock();
1608 rb_thread_release_fork_lock();
1613#if defined(HAVE_WORKING_FORK)
1615COMPILER_WARNING_PUSH
1616#if __has_warning("-Wdeprecated-declarations") || RBIMPL_COMPILER_IS(GCC)
1617COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
1619static inline rb_pid_t
1627#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1629exec_with_sh(
const char *prog,
char **argv,
char **envp)
1631 *argv = (
char *)prog;
1632 *--argv = (
char *)
"sh";
1634 execve(
"/bin/sh", argv, envp);
1636 execv(
"/bin/sh", argv);
1640#define try_with_sh(err, prog, argv, envp) (void)0
1645proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1653 argv = ARGVSTR2ARGV(argv_str);
1660 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1663 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1665 execve(prog, argv, envp);
1669 try_with_sh(err, prog, argv, envp);
1676proc_exec_sh(
const char *str,
VALUE envp_str)
1681 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1689 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1690#elif defined(__CYGWIN32__)
1692 char fbuf[MAXPATHLEN];
1693 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
1696 execl(shell,
"sh",
"-c", str, (
char *) NULL);
1698 status = system(str);
1704 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1706 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1716 ret = proc_exec_sh(str,
Qfalse);
1723mark_exec_arg(
void *
ptr)
1726 if (eargp->use_shell)
1727 rb_gc_mark(eargp->invoke.sh.shell_script);
1729 rb_gc_mark(eargp->invoke.cmd.command_name);
1730 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1731 rb_gc_mark(eargp->invoke.cmd.argv_str);
1732 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1734 rb_gc_mark(eargp->redirect_fds);
1735 rb_gc_mark(eargp->envp_str);
1736 rb_gc_mark(eargp->envp_buf);
1737 rb_gc_mark(eargp->dup2_tmpbuf);
1738 rb_gc_mark(eargp->rlimit_limits);
1739 rb_gc_mark(eargp->fd_dup2);
1740 rb_gc_mark(eargp->fd_close);
1741 rb_gc_mark(eargp->fd_open);
1742 rb_gc_mark(eargp->fd_dup2_child);
1743 rb_gc_mark(eargp->env_modification);
1744 rb_gc_mark(eargp->path_env);
1745 rb_gc_mark(eargp->chdir_dir);
1749memsize_exec_arg(
const void *
ptr)
1757 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
1761# define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1763#ifdef DEFAULT_PROCESS_ENCODING
1764# define EXPORT_STR(str) rb_str_export_to_enc((str), DEFAULT_PROCESS_ENCODING)
1765# define EXPORT_DUP(str) export_dup(str)
1767export_dup(
VALUE str)
1769 VALUE newstr = EXPORT_STR(str);
1774# define EXPORT_STR(str) (str)
1775# define EXPORT_DUP(str) rb_str_dup(str)
1778#if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1779# define USE_SPAWNV 1
1781# define USE_SPAWNV 0
1784# define P_NOWAIT _P_NOWAIT
1789#define proc_spawn_cmd_internal(argv, prog) rb_w32_uaspawn(P_NOWAIT, (prog), (argv))
1792proc_spawn_cmd_internal(
char **argv,
char *prog)
1794 char fbuf[MAXPATHLEN];
1799 prog = dln_find_exe_r(prog, 0, fbuf,
sizeof(fbuf));
1804 status = spawnv(P_NOWAIT, prog, (
const char **)argv);
1805 if (status == -1 &&
errno == ENOEXEC) {
1806 *argv = (
char *)prog;
1807 *--argv = (
char *)
"sh";
1808 status = spawnv(P_NOWAIT,
"/bin/sh", (
const char **)argv);
1810 if (status == -1)
errno = ENOEXEC;
1824 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1825 flags = CREATE_NEW_PROCESS_GROUP;
1827 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ? RSTRING_PTR(prog) : 0, argv, flags);
1829 pid = proc_spawn_cmd_internal(argv, prog ? RSTRING_PTR(prog) : 0);
1836#define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
1839proc_spawn_sh(
char *str)
1841 char fbuf[MAXPATHLEN];
1844 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
1846 status = spawnl(P_NOWAIT, (shell ? shell :
"/bin/sh"),
"sh",
"-c", str, (
char*)NULL);
1856 RBASIC_CLEAR_CLASS(obj);
1861check_exec_redirect_fd(
VALUE v,
int iskey)
1872 else if (
id == id_out)
1874 else if (
id == id_err)
1883 rb_raise(rb_eArgError,
"duplex IO redirection");
1890 rb_raise(rb_eArgError,
"negative file descriptor");
1893 else if (fd >= 3 && iskey) {
1894 rb_raise(rb_eArgError,
"wrong file descriptor (%d)", fd);
1900 rb_raise(rb_eArgError,
"wrong exec redirect");
1910 if (!RB_TYPE_P(key,
T_ARRAY)) {
1911 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
1918 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
1929 VALUE path, flags, perm;
1933 switch (
TYPE(val)) {
1936 if (
id == id_close) {
1938 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
1940 else if (
id == id_in) {
1942 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1944 else if (
id == id_out) {
1946 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1948 else if (
id == id_err) {
1950 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1953 rb_raise(rb_eArgError,
"wrong exec redirect symbol: %"PRIsVALUE,
1960 val = check_exec_redirect_fd(val, 0);
1964 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1970 path ==
ID2SYM(id_child)) {
1971 param = check_exec_redirect_fd(
rb_ary_entry(val, 1), 0);
1972 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
1979 else if (RB_TYPE_P(flags,
T_STRING))
1985 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1986 flags, perm,
Qnil));
1987 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1994 if (RB_TYPE_P(key,
T_FILE))
1995 key = check_exec_redirect_fd(key, 1);
1997 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1998 else if (RB_TYPE_P(key,
T_ARRAY)) {
2002 VALUE fd = check_exec_redirect_fd(v, 1);
2006 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2013 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2014 flags, perm,
Qnil));
2015 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2021 if (!
NIL_P(val))
goto io;
2022 rb_raise(rb_eArgError,
"wrong exec redirect action");
2027#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2028static int rlimit_type_by_sym(
VALUE key);
2031rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
2033 VALUE ary = eargp->rlimit_limits;
2034 VALUE tmp, softlim, hardlim;
2035 if (eargp->rlimit_limits ==
Qfalse)
2036 ary = eargp->rlimit_limits = hide_obj(
rb_ary_new());
2038 ary = eargp->rlimit_limits;
2048 rb_raise(rb_eArgError,
"wrong exec rlimit option");
2059#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
2063 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2067 switch (
TYPE(key)) {
2069#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2071 int rtype = rlimit_type_by_sym(key);
2073 rb_execarg_addopt_rlimit(eargp, rtype, val);
2081 if (
id == id_pgroup) {
2083 if (eargp->pgroup_given) {
2084 rb_raise(rb_eArgError,
"pgroup option specified twice");
2088 else if (val ==
Qtrue)
2093 rb_raise(rb_eArgError,
"negative process group ID : %ld", (
long)pgroup);
2096 eargp->pgroup_given = 1;
2097 eargp->pgroup_pgid = pgroup;
2102 if (
id == id_new_pgroup) {
2103 if (eargp->new_pgroup_given) {
2104 rb_raise(rb_eArgError,
"new_pgroup option specified twice");
2106 eargp->new_pgroup_given = 1;
2107 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2111 if (
id == id_unsetenv_others) {
2112 if (eargp->unsetenv_others_given) {
2113 rb_raise(rb_eArgError,
"unsetenv_others option specified twice");
2115 eargp->unsetenv_others_given = 1;
2116 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2118 else if (
id == id_chdir) {
2119 if (eargp->chdir_given) {
2120 rb_raise(rb_eArgError,
"chdir option specified twice");
2123 val = rb_str_encode_ospath(val);
2124 eargp->chdir_given = 1;
2125 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2127 else if (
id == id_umask) {
2129 if (eargp->umask_given) {
2130 rb_raise(rb_eArgError,
"umask option specified twice");
2132 eargp->umask_given = 1;
2133 eargp->umask_mask = cmask;
2135 else if (
id == id_close_others) {
2136 if (eargp->close_others_given) {
2137 rb_raise(rb_eArgError,
"close_others option specified twice");
2139 eargp->close_others_given = 1;
2140 eargp->close_others_do = TO_BOOL(val,
"close_others");
2142 else if (
id == id_in) {
2146 else if (
id == id_out) {
2150 else if (
id == id_err) {
2154 else if (
id == id_uid) {
2156 if (eargp->uid_given) {
2157 rb_raise(rb_eArgError,
"uid option specified twice");
2161 eargp->uid = OBJ2UID(val);
2162 eargp->uid_given = 1;
2166 "uid option is unimplemented on this machine");
2169 else if (
id == id_gid) {
2171 if (eargp->gid_given) {
2172 rb_raise(rb_eArgError,
"gid option specified twice");
2176 eargp->gid = OBJ2GID(val);
2177 eargp->gid_given = 1;
2181 "gid option is unimplemented on this machine");
2184 else if (
id == id_exception) {
2185 if (eargp->exception_given) {
2186 rb_raise(rb_eArgError,
"exception option specified twice");
2188 eargp->exception_given = 1;
2189 eargp->exception = TO_BOOL(val,
"exception");
2200 check_exec_redirect(key, val, eargp);
2212check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2217 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2219 rb_raise(rb_eArgError,
"wrong exec option symbol: % "PRIsVALUE,
2221 rb_raise(rb_eArgError,
"wrong exec option");
2227check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2232 VALUE execarg_obj = args[0];
2233 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2234 VALUE nonopts = args[1];
2235 if (
NIL_P(nonopts)) args[1] = nonopts = rb_hash_new();
2236 rb_hash_aset(nonopts, key, val);
2251 rb_raise(rb_eArgError,
"fd %d specified twice", fd);
2253 if (ary == eargp->fd_dup2)
2255 else if (ary == eargp->fd_dup2_child)
2261 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2274 VALUE h = rb_hash_new();
2279 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2280 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2281 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2283 if (eargp->fd_dup2_child) {
2284 ary = eargp->fd_dup2_child;
2294 val = rb_hash_lookup(h, val);
2296 rb_raise(rb_eArgError,
"cyclic child fd redirection from %d", oldfd);
2300 rb_raise(rb_eArgError,
"child fd %d is not redirected", oldfd);
2301 if (oldfd != lastfd) {
2306 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2307 rb_hash_aset(h, val,
INT2FIX(lastfd));
2314 eargp->close_others_maxhint = maxhint;
2319rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2323 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2327rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2332 args[0] = execarg_obj;
2334 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2338#ifdef ENV_IGNORECASE
2339#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2341#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2345check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2355 rb_raise(rb_eArgError,
"environment name contains a equal : %"PRIsVALUE, key);
2360 key = EXPORT_STR(key);
2361 if (!
NIL_P(val)) val = EXPORT_STR(val);
2378 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2385rb_check_argv(
int argc,
VALUE *argv)
2396 rb_raise(rb_eArgError,
"wrong first argument");
2404 for (i = 0; i < argc; i++) {
2413check_hash(
VALUE obj)
2415 if (RB_SPECIAL_CONST_P(obj))
return Qnil;
2416 switch (RB_BUILTIN_TYPE(obj)) {
2423 return rb_check_hash_type(obj);
2427rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2432 hash = check_hash((*argv_p)[*argc_p-1]);
2434 *opthash_ret = hash;
2440 hash = check_hash((*argv_p)[0]);
2447 prog = rb_check_argv(*argc_p, *argv_p);
2449 prog = (*argv_p)[0];
2450 if (accept_shell && *argc_p == 1) {
2465compare_posix_sh(
const void *key,
const void *el)
2468 int ret = strncmp(word->ptr, el, word->len);
2469 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2477 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2478 char fbuf[MAXPATHLEN];
2482 if (!
NIL_P(opthash)) {
2483 rb_check_exec_options(opthash, execarg_obj);
2486 env = rb_check_exec_env(env, &eargp->path_env);
2487 eargp->env_modification = env;
2490 prog = EXPORT_STR(prog);
2491 eargp->use_shell = argc == 0;
2492 if (eargp->use_shell)
2493 eargp->invoke.sh.shell_script = prog;
2495 eargp->invoke.cmd.command_name = prog;
2498 if (eargp->use_shell) {
2499 static const char posix_sh_cmds[][9] = {
2557 for (p = RSTRING_PTR(prog); *p; p++) {
2558 if (*p ==
' ' || *p ==
'\t') {
2559 if (first.ptr && !first.len) first.len = p - first.ptr;
2562 if (!first.ptr) first.ptr = p;
2564 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2570 else if (*p ==
'/') {
2577 if (!has_meta && first.ptr) {
2578 if (!first.len) first.len = p - first.ptr;
2579 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2580 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2585 eargp->use_shell = 0;
2587 if (!eargp->use_shell) {
2590 p = RSTRING_PTR(prog);
2592 while (*p ==
' ' || *p ==
'\t')
2596 while (*p && *p !=
' ' && *p !=
'\t')
2602 eargp->invoke.cmd.argv_buf = argv_buf;
2603 eargp->invoke.cmd.command_name =
2604 hide_obj(
rb_str_subseq(argv_buf, 0, strlen(RSTRING_PTR(argv_buf))));
2605 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2610 if (!eargp->use_shell) {
2611 const char *abspath;
2612 const char *path_env = 0;
2613 if (
RTEST(eargp->path_env)) path_env = RSTRING_PTR(eargp->path_env);
2614 abspath = dln_find_exe_r(RSTRING_PTR(eargp->invoke.cmd.command_name),
2615 path_env, fbuf,
sizeof(fbuf));
2619 eargp->invoke.cmd.command_abspath =
Qnil;
2622 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2627 for (i = 0; i < argc; i++) {
2628 VALUE arg = argv[i];
2630#ifdef DEFAULT_PROCESS_ENCODING
2631 arg = EXPORT_STR(arg);
2632 s = RSTRING_PTR(arg);
2636 eargp->invoke.cmd.argv_buf = argv_buf;
2639 if (!eargp->use_shell) {
2640 const char *p, *ep, *null=NULL;
2644 p = RSTRING_PTR(eargp->invoke.cmd.argv_buf);
2645 ep = p + RSTRING_LEN(eargp->invoke.cmd.argv_buf);
2651 eargp->invoke.cmd.argv_str =
2652 rb_imemo_tmpbuf_new_from_an_RString(argv_str);
2658rb_execarg_get(
VALUE execarg_obj)
2666rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2668 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2674 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2675 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2677 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2683rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2688 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2689 if (!allow_exc_opt && eargp->exception_given) {
2690 rb_raise(rb_eArgError,
"exception option is not allowed");
2696rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2698 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2699 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2700 eargp->env_modification = env;
2705fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2720static long run_exec_dup2_tmpbuf_size(
long n);
2734 const char *fname = RSTRING_PTR(data->fname);
2735 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2741rb_execarg_allocate_dup2_tmpbuf(
struct rb_execarg *eargp,
long len)
2743 VALUE tmpbuf = rb_imemo_tmpbuf_new();
2744 rb_imemo_tmpbuf_set_ptr(tmpbuf, ruby_xmalloc(run_exec_dup2_tmpbuf_size(
len)));
2745 eargp->dup2_tmpbuf = tmpbuf;
2749rb_execarg_parent_start1(
VALUE execarg_obj)
2751 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2752 int unsetenv_others;
2756 ary = eargp->fd_open;
2771 open_data.fname = vpath;
2772 open_data.oflags = flags;
2773 open_data.perm = perm;
2775 open_data.err = EINTR;
2777 if (open_data.ret == -1) {
2778 if (open_data.err == EINTR) {
2784 fd2 = open_data.ret;
2786 RARRAY_ASET(param, 3,
INT2FIX(fd2));
2796 eargp->redirect_fds = check_exec_fds(eargp);
2798 ary = eargp->fd_dup2;
2800 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2803 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2804 envopts = eargp->env_modification;
2805 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2806 VALUE envtbl, envp_str, envp_buf;
2808 if (unsetenv_others) {
2809 envtbl = rb_hash_new();
2812 envtbl = rb_env_to_hash();
2816 st_table *stenv = RHASH_TBL_RAW(envtbl);
2823 st_data_t stkey = (st_data_t)key;
2824 st_delete(stenv, &stkey, NULL);
2827 st_insert(stenv, (st_data_t)key, (st_data_t)val);
2835 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
2838 p = RSTRING_PTR(envp_buf);
2839 ep = p + RSTRING_LEN(envp_buf);
2847 rb_imemo_tmpbuf_new_from_an_RString(envp_str);
2848 eargp->envp_buf = envp_buf;
2864rb_execarg_parent_start(
VALUE execarg_obj)
2867 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2869 rb_execarg_parent_end(execarg_obj);
2875execarg_parent_end(
VALUE execarg_obj)
2877 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2881 ary = eargp->fd_open;
2892 parent_redirect_close(fd2);
2893 RARRAY_ASET(param, 3,
Qnil);
2904rb_execarg_parent_end(
VALUE execarg_obj)
2906 execarg_parent_end(execarg_obj);
2911rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
2913 if (!errmsg || !*errmsg)
return;
2914 if (strcmp(errmsg,
"chdir") == 0) {
2915 rb_sys_fail_str(eargp->chdir_dir);
2917 rb_sys_fail(errmsg);
2922rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
2924 if (!errmsg || !*errmsg)
return;
2925 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
2933 VALUE execarg_obj, fail_str;
2935#define CHILD_ERRMSG_BUFLEN 80
2936 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
2939 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
2940 eargp = rb_execarg_get(execarg_obj);
2943 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2945 execarg_parent_end(execarg_obj);
2950 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2952 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
2955 rb_exec_fail(eargp, err, errmsg);
3059#define ERRMSG(str) \
3060 ((errmsg && 0 < errmsg_buflen) ? \
3061 (void)strlcpy(errmsg, (str), errmsg_buflen) : (void)0)
3063#define ERRMSG_FMT(...) \
3064 ((errmsg && 0 < errmsg_buflen) ? \
3065 (void)snprintf(errmsg, errmsg_buflen, __VA_ARGS__) : (void)0)
3067static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3068static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3069static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3072save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3075 VALUE newary, redirection;
3076 int save_fd = redirect_cloexec_dup(fd), cloexec;
3077 if (save_fd == -1) {
3084 newary = sargp->fd_dup2;
3087 sargp->fd_dup2 = newary;
3089 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3094 newary = sargp->fd_close;
3097 sargp->fd_close = newary;
3106intcmp(
const void *a,
const void *b)
3108 return *(
int*)a - *(
int*)b;
3112intrcmp(
const void *a,
const void *b)
3114 return *(
int*)b - *(
int*)a;
3126run_exec_dup2_tmpbuf_size(
long n)
3133fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3137 ret = fcntl(fd, F_GETFD);
3139 ERRMSG(
"fcntl(F_GETFD)");
3142 if (ret & FD_CLOEXEC)
return 1;
3149fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3153 ret = fcntl(fd, F_GETFD);
3155 ERRMSG(
"fcntl(F_GETFD)");
3158 if (!(ret & FD_CLOEXEC)) {
3160 ret = fcntl(fd, F_SETFD, ret);
3162 ERRMSG(
"fcntl(F_SETFD)");
3172fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3176 ret = fcntl(fd, F_GETFD);
3178 ERRMSG(
"fcntl(F_GETFD)");
3181 if (ret & FD_CLOEXEC) {
3183 ret = fcntl(fd, F_SETFD, ret);
3185 ERRMSG(
"fcntl(F_SETFD)");
3195run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3206 for (i = 0; i < n; i++) {
3211 pairs[i].older_index = -1;
3221 for (i = 0; i < n; i++) {
3222 int newfd = pairs[i].newfd;
3226 pairs[i].num_newer = 0;
3228 while (pairs < found && (found-1)->oldfd == newfd)
3230 while (found < pairs+n && found->oldfd == newfd) {
3231 pairs[i].num_newer++;
3232 found->older_index = i;
3239 for (i = 0; i < n; i++) {
3241 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3242 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3244 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3249 if (pairs[j].cloexec &&
3250 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3254 pairs[j].oldfd = -1;
3255 j = pairs[j].older_index;
3257 pairs[j].num_newer--;
3262 for (i = 0; i < n; i++) {
3264 if (pairs[i].oldfd == -1)
3266 if (pairs[i].oldfd == pairs[i].newfd) {
3267 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3269 pairs[i].oldfd = -1;
3272 if (extra_fd == -1) {
3273 extra_fd = redirect_dup(pairs[i].oldfd);
3274 if (extra_fd == -1) {
3280 if (fd_get_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen)) {
3281 if (fd_set_cloexec(extra_fd, errmsg, errmsg_buflen)) {
3289 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3296 pairs[i].oldfd = extra_fd;
3297 j = pairs[i].older_index;
3298 pairs[i].older_index = -1;
3300 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3306 pairs[j].oldfd = -1;
3307 j = pairs[j].older_index;
3310 if (extra_fd != -1) {
3311 ret = redirect_close(extra_fd);
3326run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3334 ret = redirect_close(fd);
3345run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3355 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3357 ret = redirect_dup2(oldfd, newfd);
3370run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3382 pgroup = eargp->pgroup_pgid;
3388 sargp->pgroup_given = 1;
3389 sargp->pgroup_pgid = getpgrp();
3395 ret = setpgid(getpid(), pgroup);
3396 if (ret == -1) ERRMSG(
"setpgid");
3401#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3404run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3413 if (getrlimit(rtype, &rlim) == -1) {
3414 ERRMSG(
"getrlimit");
3418 RLIM2NUM(rlim.rlim_cur),
3419 RLIM2NUM(rlim.rlim_max)));
3420 if (sargp->rlimit_limits ==
Qfalse)
3421 newary = sargp->rlimit_limits = hide_obj(
rb_ary_new());
3423 newary = sargp->rlimit_limits;
3428 if (setrlimit(rtype, &rlim) == -1) {
3429 ERRMSG(
"setrlimit");
3437#if !defined(HAVE_WORKING_FORK)
3450 if (sargp->env_modification ==
Qfalse) {
3451 VALUE env = rb_envtbl();
3456 sargp->env_modification = ary;
3458 sargp->unsetenv_others_given = 1;
3459 sargp->unsetenv_others_do = 1;
3466#define chdir(p) rb_w32_uchdir(p)
3471rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3478 sargp->redirect_fds =
Qnil;
3482 if (eargp->pgroup_given) {
3483 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3488#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3489 obj = eargp->rlimit_limits;
3491 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3496#if !defined(HAVE_WORKING_FORK)
3497 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3502 obj = eargp->env_modification;
3518 if (eargp->umask_given) {
3519 mode_t mask = eargp->umask_mask;
3520 mode_t oldmask = umask(mask);
3522 sargp->umask_given = 1;
3523 sargp->umask_mask = oldmask;
3527 obj = eargp->fd_dup2;
3529 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3533 obj = eargp->fd_close;
3536 rb_warn(
"cannot close fd before spawn");
3538 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3543#ifdef HAVE_WORKING_FORK
3544 if (eargp->close_others_do) {
3549 obj = eargp->fd_dup2_child;
3551 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3555 if (eargp->chdir_given) {
3557 sargp->chdir_given = 1;
3558 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3560 if (chdir(RSTRING_PTR(eargp->chdir_dir)) == -1) {
3567 if (eargp->gid_given) {
3568 if (setgid(eargp->gid) < 0) {
3575 if (eargp->uid_given) {
3576 if (setuid(eargp->uid) < 0) {
3584 VALUE ary = sargp->fd_dup2;
3586 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3590 int preserve =
errno;
3591 stdfd_clear_nonblock();
3600rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3602 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3607exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3609#if !defined(HAVE_WORKING_FORK)
3610 struct rb_execarg sarg, *
const sargp = &sarg;
3616 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3620 if (eargp->use_shell) {
3621 err = proc_exec_sh(RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3624 char *abspath = NULL;
3625 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3626 abspath = RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3627 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3629#if !defined(HAVE_WORKING_FORK)
3630 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3636#ifdef HAVE_WORKING_FORK
3639rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3641 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3645proc_syswait(
VALUE pid)
3652move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3656 for (i = 0; i < n; i++) {
3675pipe_nocrash(
int filedes[2],
VALUE fds)
3683 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3698rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3705handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3711 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3717#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3720 if (!status && !ep) {
3725 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3726 if (status) status->status = state;
3727 if (!state)
return 0;
3736 if (state && !status) rb_jump_tag(state);
3742#define prefork() ( \
3743 rb_io_flush(rb_ractor_stdout()), \
3744 rb_io_flush(rb_ractor_stderr()) \
3774write_retry(
int fd,
const void *buf,
size_t len)
3779 w = write(fd, buf,
len);
3780 }
while (w < 0 &&
errno == EINTR);
3786read_retry(
int fd,
void *buf,
size_t len)
3790 if (set_blocking(fd) != 0) {
3792 rb_async_bug_errno(
"set_blocking failed reading child error",
errno);
3797 r = read(fd, buf,
len);
3798 }
while (r < 0 &&
errno == EINTR);
3804send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3809 if (write_retry(fd, &err,
sizeof(err)) < 0) err =
errno;
3810 if (errmsg && 0 < errmsg_buflen) {
3811 errmsg[errmsg_buflen-1] =
'\0';
3812 errmsg_buflen = strlen(errmsg);
3813 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3819recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3823 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3827 if (size ==
sizeof(err) &&
3828 errmsg && 0 < errmsg_buflen) {
3829 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3838#ifdef HAVE_WORKING_VFORK
3839#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3842getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3848 ret = getuidx(ID_SAVED);
3849 if (ret == (rb_uid_t)-1)
3854#define HAVE_GETRESUID
3857#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
3860getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
3866 ret = getgidx(ID_SAVED);
3867 if (ret == (rb_gid_t)-1)
3872#define HAVE_GETRESGID
3890 rb_uid_t ruid, euid;
3891 rb_gid_t rgid, egid;
3893#if defined HAVE_ISSETUGID
3898#ifdef HAVE_GETRESUID
3902 ret = getresuid(&ruid, &euid, &suid);
3904 rb_sys_fail(
"getresuid(2)");
3913 if (euid == 0 || euid != ruid)
3916#ifdef HAVE_GETRESGID
3920 ret = getresgid(&rgid, &egid, &sgid);
3922 rb_sys_fail(
"getresgid(2)");
3938struct child_handler_disabler_state
3944disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
3946#ifdef HAVE_PTHREAD_SIGMASK
3950 ret = sigfillset(&all);
3952 rb_sys_fail(
"sigfillset");
3954 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
3959# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3964disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
3966#ifdef HAVE_PTHREAD_SIGMASK
3969 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
3974# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3980disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
3985 for (sig = 1; sig < NSIG; sig++) {
3986 sig_t handler = signal(sig, SIG_DFL);
3988 if (handler == SIG_ERR &&
errno == EINVAL) {
3991 if (handler == SIG_ERR) {
3992 ERRMSG(
"signal to obtain old action");
3996 if (sig == SIGPIPE) {
4001 if (handler == SIG_IGN) {
4002 signal(sig, SIG_IGN);
4007 sigemptyset(&old->sigmask);
4008 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
4010 ERRMSG(
"sigprocmask");
4018 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4019 char *errmsg,
size_t errmsg_buflen,
4023 volatile int try_gc = 1;
4024 struct child_handler_disabler_state old;
4029 disable_child_handler_before_fork(&old);
4033#if defined(HAVE_WORKING_VFORK) && !defined(RUBY_ASAN_ENABLED)
4034 if (!has_privilege())
4044 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
4046 ret = chfunc(charg, errmsg, errmsg_buflen);
4047 if (!ret) _exit(EXIT_SUCCESS);
4049 send_child_error(ep[1], errmsg, errmsg_buflen);
4050#if EXIT_SUCCESS == 127
4051 _exit(EXIT_FAILURE);
4057 disable_child_handler_fork_parent(&old);
4061 if (handle_fork_error(err, status, ep, &try_gc))
4067fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4068 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4076 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4078 if (status) status->status = 0;
4080 if (pipe_nocrash(ep, fds))
return -1;
4082 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4084 if (status) status->pid = pid;
4087 if (status) status->error =
errno;
4094 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4096 if (error_occurred) {
4099 status->error = err;
4101 VM_ASSERT((w == 0) &&
"only used by extensions");
4102 rb_protect(proc_syswait, (
VALUE)pid, &state);
4104 status->status = state;
4125rb_fork_async_signal_safe(
int *status,
4126 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4127 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4131 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4134 *status = process_status.status;
4141rb_fork_ruby(
int *status)
4143 if (UNLIKELY(!rb_ractor_main_p())) {
4144 rb_raise(rb_eRactorIsolationError,
"can not fork from non-main Ractors");
4149 int try_gc = 1, err = 0;
4150 struct child_handler_disabler_state old;
4156 rb_thread_acquire_fork_lock();
4157 disable_child_handler_before_fork(&old);
4160 child.pid = pid = rb_fork();
4161 child.error = err =
errno;
4164 disable_child_handler_fork_parent(&old);
4165 release_fork_lock_after_fork(pid);
4166 after_fork_ruby(pid);
4169 }
while (pid < 0 && handle_fork_error(err, &child, NULL, &try_gc) == 0);
4171 if (status) *status = child.status;
4179 rb_pid_t pid = rb_fork_ruby(NULL);
4182 rb_sys_fail(
"fork(2)");
4189call_proc__fork_protected(
VALUE arg)
4198rb_call_proc__fork(
void)
4201 return proc_fork_pid();
4204 rb_pid_t parent = getpid(), pid;
4207 if (
NIL_P(rb_protect(call_proc__fork_protected, (
VALUE)&pid, &state))) {
4208 if (getpid() != parent) {
4218#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4239rb_proc__fork(
VALUE _obj)
4241 rb_pid_t pid = proc_fork_pid();
4310 pid = rb_call_proc__fork();
4324#define rb_proc__fork rb_f_notimplement
4325#define rb_f_fork rb_f_notimplement
4329exit_status_code(
VALUE status)
4335 istatus = EXIT_SUCCESS;
4338 istatus = EXIT_FAILURE;
4342#if EXIT_SUCCESS != 0
4344 istatus = EXIT_SUCCESS;
4351NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4369rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4373 if (rb_check_arity(argc, 0, 1) == 1) {
4374 istatus = exit_status_code(argv[0]);
4377 istatus = EXIT_FAILURE;
4387 if (GET_EC()->tag) {
4402 if (rb_check_arity(argc, 0, 1) == 1) {
4403 istatus = exit_status_code(argv[0]);
4406 istatus = EXIT_SUCCESS;
4469 rb_check_arity(argc, 0, 1);
4471 rb_execution_context_t *ec = GET_EC();
4472 VALUE errinfo = rb_ec_get_errinfo(ec);
4473 if (!
NIL_P(errinfo)) {
4474 rb_ec_error_print(ec, errinfo);
4481 args[1] = args[0] = argv[0];
4483 rb_io_puts(1, args, rb_ractor_stderr());
4484 args[0] =
INT2NUM(EXIT_FAILURE);
4522#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4527 if (eargp && !eargp->use_shell) {
4528 VALUE str = eargp->invoke.cmd.argv_str;
4529 VALUE buf = eargp->invoke.cmd.argv_buf;
4530 char *p, **argv = ARGVSTR2ARGV(str);
4531 long i, argc = ARGVSTR2ARGC(str);
4532 const char *start = RSTRING_PTR(buf);
4534 p = RSTRING_PTR(cmd);
4535 for (i = 1; i < argc; ++i) {
4536 p[argv[i] - start - 1] =
' ';
4546rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4549#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4552# if !defined HAVE_SPAWNV
4557#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4558 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4560 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4562 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4566 if (prog && !eargp->use_shell) {
4567 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4568 argv[0] = RSTRING_PTR(prog);
4570# if defined HAVE_SPAWNV
4571 if (eargp->use_shell) {
4572 pid = proc_spawn_sh(RSTRING_PTR(prog));
4575 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4576 pid = proc_spawn_cmd(argv, prog, eargp);
4583 status = system(rb_execarg_commandline(eargp, &prog));
4588 if (eargp->waitpid_state) {
4589 eargp->waitpid_state->pid = pid;
4592 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4607do_spawn_process(
VALUE arg)
4611 rb_execarg_parent_start1(argp->execarg);
4613 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4614 argp->errmsg.ptr, argp->errmsg.buflen);
4617NOINLINE(
static rb_pid_t
4618 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4621rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4625 args.execarg = execarg_obj;
4626 args.errmsg.ptr = errmsg;
4627 args.errmsg.buflen = errmsg_buflen;
4630 execarg_parent_end, execarg_obj);
4635rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4639 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4640 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4646 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4652 return rb_spawn_internal(argc, argv, NULL, 0);
4776 rb_thread_t *th = GET_THREAD();
4777 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4778 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4781 eargp->status = &status;
4783 last_status_clear(th);
4787 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4794 th->last_status = status;
4796 if (data->status == EXIT_SUCCESS) {
4800 if (data->error != 0) {
4801 if (eargp->exception) {
4802 VALUE command = eargp->invoke.sh.shell_script;
4810 else if (eargp->exception) {
4811 VALUE command = eargp->invoke.sh.shell_script;
4825 if (eargp->exception) {
4826 VALUE command = eargp->invoke.sh.shell_script;
4942 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
4943 VALUE execarg_obj, fail_str;
4946 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4947 eargp = rb_execarg_get(execarg_obj);
4948 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4950 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
4954 rb_exec_fail(eargp, err, errmsg);
4958#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
4984 time_t beg = time(0);
4987 if (scheduler !=
Qnil) {
4991 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
4995 rb_check_arity(argc, 0, 1);
5000 time_t end = time(0) - beg;
5002 return TIMET2NUM(end);
5006#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
5023#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
5025 if (pgrp < 0) rb_sys_fail(0);
5029 if (pgrp < 0) rb_sys_fail(0);
5034#define proc_getpgrp rb_f_notimplement
5038#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
5056 if (setpgid(0,0) < 0) rb_sys_fail(0);
5057#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
5058 if (setpgrp() < 0) rb_sys_fail(0);
5063#define proc_setpgrp rb_f_notimplement
5067#if defined(HAVE_GETPGID)
5085 if (i < 0) rb_sys_fail(0);
5089#define proc_getpgid rb_f_notimplement
5107 rb_pid_t ipid, ipgrp;
5112 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5116#define proc_setpgid rb_f_notimplement
5140 if (rb_check_arity(argc, 0, 1) == 1 && !
NIL_P(argv[0]))
5144 if (sid < 0) rb_sys_fail(0);
5148#define proc_getsid rb_f_notimplement
5152#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5153#if !defined(HAVE_SETSID)
5154static rb_pid_t ruby_setsid(
void);
5155#define setsid() ruby_setsid()
5176 if (pid < 0) rb_sys_fail(0);
5180#if !defined(HAVE_SETSID)
5181#define HAVE_SETSID 1
5189#if defined(SETPGRP_VOID)
5195 ret = setpgrp(0, pid);
5197 if (ret == -1)
return -1;
5201 ioctl(fd, TIOCNOTTY, NULL);
5208#define proc_setsid rb_f_notimplement
5212#ifdef HAVE_GETPRIORITY
5240 int prio, iwhich, iwho;
5246 prio = getpriority(iwhich, iwho);
5247 if (
errno) rb_sys_fail(0);
5251#define proc_getpriority rb_f_notimplement
5255#ifdef HAVE_GETPRIORITY
5275 int iwhich, iwho, iprio;
5281 if (setpriority(iwhich, iwho, iprio) < 0)
5286#define proc_setpriority rb_f_notimplement
5289#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5291rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5295#define RESCHECK(r) \
5297 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5298 resource = RLIMIT_##r; \
5332#ifdef RLIMIT_MEMLOCK
5335#ifdef RLIMIT_MSGQUEUE
5374#ifdef RLIMIT_SIGPENDING
5375 RESCHECK(SIGPENDING);
5384 for (p = name; *p; p++)
5390 for (p = name; *p; p++)
5396 rb_bug(
"unexpected casetype");
5403rlimit_type_by_hname(
const char *name,
long len)
5405 return rlimit_resource_name2int(name,
len, 0);
5409rlimit_type_by_lname(
const char *name,
long len)
5411 return rlimit_resource_name2int(name,
len, 1);
5415rlimit_type_by_sym(
VALUE key)
5418 const char *rname = RSTRING_PTR(name);
5419 long len = RSTRING_LEN(name);
5421 static const char prefix[] =
"rlimit_";
5422 enum {prefix_len =
sizeof(prefix)-1};
5424 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5425 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5433rlimit_resource_type(
VALUE rtype)
5440 switch (
TYPE(rtype)) {
5443 name = RSTRING_PTR(v);
5444 len = RSTRING_LEN(v);
5453 len = RSTRING_LEN(rtype);
5463 r = rlimit_type_by_hname(name,
len);
5467 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5473rlimit_resource_value(
VALUE rval)
5478 switch (
TYPE(rval)) {
5481 name = RSTRING_PTR(v);
5496 return NUM2RLIM(rval);
5500 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5502#ifdef RLIM_SAVED_MAX
5503 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5505#ifdef RLIM_SAVED_CUR
5506 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5508 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5514#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5542 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5543 rb_sys_fail(
"getrlimit");
5545 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5548#define proc_getrlimit rb_f_notimplement
5551#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5605proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5607 VALUE resource, rlim_cur, rlim_max;
5610 rb_check_arity(argc, 2, 3);
5613 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5614 rlim_max = rlim_cur;
5616 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5617 rlim.rlim_max = rlimit_resource_value(rlim_max);
5619 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5620 rb_sys_fail(
"setrlimit");
5625#define proc_setrlimit rb_f_notimplement
5628static int under_uid_switch = 0;
5630check_uid_switch(
void)
5632 if (under_uid_switch) {
5633 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5637static int under_gid_switch = 0;
5639check_gid_switch(
void)
5641 if (under_gid_switch) {
5642 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5647#if defined(HAVE_PWD_H)
5649login_not_found(
int err)
5651 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5662# if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5665 char MAYBE_UNUSED(*login) = NULL;
5667# ifdef USE_GETLOGIN_R
5669# if defined(__FreeBSD__)
5670 typedef int getlogin_r_size_t;
5672 typedef size_t getlogin_r_size_t;
5675 long loginsize = GETLOGIN_R_SIZE_INIT;
5678 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5682 login = RSTRING_PTR(maybe_result);
5687 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5688 if (login_not_found(gle)) {
5689 rb_str_resize(maybe_result, 0);
5693 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5694 rb_str_resize(maybe_result, 0);
5699 login = RSTRING_PTR(maybe_result);
5703 if (login == NULL) {
5704 rb_str_resize(maybe_result, 0);
5709 return maybe_result;
5711# elif defined(USE_GETLOGIN)
5717 if (login_not_found(err)) {
5731pwd_not_found(
int err)
5745# if defined(USE_GETPWNAM_R)
5746struct getpwnam_r_args {
5750 struct passwd *result;
5751 struct passwd pwstore;
5754# define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5755 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5758nogvl_getpwnam_r(
void *args)
5760 struct getpwnam_r_args *arg = args;
5761 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5766rb_getpwdirnam_for_login(
VALUE login_name)
5768#if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5772 if (
NIL_P(login_name)) {
5777 const char *login = RSTRING_PTR(login_name);
5780# ifdef USE_GETPWNAM_R
5783 long bufsizenm = GETPW_R_SIZE_INIT;
5786 bufsizenm = GETPW_R_SIZE_DEFAULT;
5790 bufnm = RSTRING_PTR(getpwnm_tmp);
5793 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5796 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5797 if (pwd_not_found(enm)) {
5798 rb_str_resize(getpwnm_tmp, 0);
5802 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5803 rb_str_resize(getpwnm_tmp, 0);
5808 args.buf = RSTRING_PTR(getpwnm_tmp);
5812 if (args.result == NULL) {
5814 rb_str_resize(getpwnm_tmp, 0);
5820 rb_str_resize(getpwnm_tmp, 0);
5823# elif defined(USE_GETPWNAM)
5825 struct passwd *pwptr;
5827 if (!(pwptr = getpwnam(login))) {
5830 if (pwd_not_found(err)) {
5844# if defined(USE_GETPWUID_R)
5845struct getpwuid_r_args {
5849 struct passwd *result;
5850 struct passwd pwstore;
5853# define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5854 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5857nogvl_getpwuid_r(
void *args)
5859 struct getpwuid_r_args *arg = args;
5860 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5870# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5874 uid_t ruid = getuid();
5876# ifdef USE_GETPWUID_R
5879 long bufsizeid = GETPW_R_SIZE_INIT;
5882 bufsizeid = GETPW_R_SIZE_DEFAULT;
5886 bufid = RSTRING_PTR(getpwid_tmp);
5889 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5892 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5893 if (pwd_not_found(eid)) {
5894 rb_str_resize(getpwid_tmp, 0);
5898 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5899 rb_str_resize(getpwid_tmp, 0);
5904 args.buf = RSTRING_PTR(getpwid_tmp);
5908 if (args.result == NULL) {
5910 rb_str_resize(getpwid_tmp, 0);
5916 rb_str_resize(getpwid_tmp, 0);
5919# elif defined(USE_GETPWUID)
5921 struct passwd *pwptr;
5923 if (!(pwptr = getpwuid(ruid))) {
5926 if (pwd_not_found(err)) {
5952#if defined(HAVE_PWD_H)
5955# ifdef USE_GETPWNAM_R
5968 struct passwd *pwptr;
5969#ifdef USE_GETPWNAM_R
5974 getpw_buf_len = GETPW_R_SIZE_INIT;
5975 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5978 getpw_buf = RSTRING_PTR(*getpw_tmp);
5982 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
5984 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5985 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5986 rb_str_resize(*getpw_tmp, 0);
5990 args.buf = RSTRING_PTR(*getpw_tmp);
5993 pwptr = args.result;
5995 pwptr = getpwnam(usrname);
5998#ifndef USE_GETPWNAM_R
6001 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
6003 uid = pwptr->pw_uid;
6004#ifndef USE_GETPWNAM_R
6011# ifdef p_uid_from_name
6031#if defined(HAVE_GRP_H)
6032# if defined(USE_GETGRNAM_R)
6033struct getgrnam_r_args {
6037 struct group *result;
6041# define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
6042 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
6045nogvl_getgrnam_r(
void *args)
6047 struct getgrnam_r_args *arg = args;
6048 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
6054# ifdef USE_GETGRNAM_R
6067 struct group *grptr;
6068#ifdef USE_GETGRNAM_R
6073 getgr_buf_len = GETGR_R_SIZE_INIT;
6074 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6077 getgr_buf = RSTRING_PTR(*getgr_tmp);
6081 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6083 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6084 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6085 rb_str_resize(*getgr_tmp, 0);
6089 args.buf = RSTRING_PTR(*getgr_tmp);
6092 grptr = args.result;
6093#elif defined(HAVE_GETGRNAM)
6094 grptr = getgrnam(grpname);
6099#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6102 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6104 gid = grptr->gr_gid;
6105#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6112# ifdef p_gid_from_name
6132#if defined HAVE_SETUID
6146 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6150#define p_sys_setuid rb_f_notimplement
6154#if defined HAVE_SETRUID
6168 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6172#define p_sys_setruid rb_f_notimplement
6176#if defined HAVE_SETEUID
6190 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6194#define p_sys_seteuid rb_f_notimplement
6198#if defined HAVE_SETREUID
6213 rb_uid_t ruid, euid;
6216 ruid = OBJ2UID1(rid);
6217 euid = OBJ2UID1(eid);
6219 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6223#define p_sys_setreuid rb_f_notimplement
6227#if defined HAVE_SETRESUID
6242 rb_uid_t ruid, euid, suid;
6245 ruid = OBJ2UID1(rid);
6246 euid = OBJ2UID1(eid);
6247 suid = OBJ2UID1(sid);
6249 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6253#define p_sys_setresuid rb_f_notimplement
6270proc_getuid(
VALUE obj)
6272 rb_uid_t uid = getuid();
6277#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6297#if defined(HAVE_SETRESUID)
6298 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6299#elif defined HAVE_SETREUID
6300 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6301#elif defined HAVE_SETRUID
6302 if (setruid(uid) < 0) rb_sys_fail(0);
6303#elif defined HAVE_SETUID
6305 if (geteuid() == uid) {
6306 if (setuid(uid) < 0) rb_sys_fail(0);
6316#define proc_setuid rb_f_notimplement
6330static rb_uid_t SAVED_USER_ID = -1;
6332#ifdef BROKEN_SETREUID
6334setreuid(rb_uid_t ruid, rb_uid_t euid)
6336 if (ruid != (rb_uid_t)-1 && ruid != getuid()) {
6337 if (euid == (rb_uid_t)-1) euid = geteuid();
6338 if (setuid(ruid) < 0)
return -1;
6340 if (euid != (rb_uid_t)-1 && euid != geteuid()) {
6341 if (seteuid(euid) < 0)
return -1;
6369 if (geteuid() == 0) {
6370#if defined(HAVE_SETRESUID)
6371 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6372 SAVED_USER_ID = uid;
6373#elif defined(HAVE_SETUID)
6374 if (setuid(uid) < 0) rb_sys_fail(0);
6375 SAVED_USER_ID = uid;
6376#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6377 if (getuid() == uid) {
6378 if (SAVED_USER_ID == uid) {
6379 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6383 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6384 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6386 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6387 SAVED_USER_ID = uid;
6390 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6392 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6393 SAVED_USER_ID = uid;
6398 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6399 SAVED_USER_ID = uid;
6401#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6402 if (getuid() == uid) {
6403 if (SAVED_USER_ID == uid) {
6404 if (seteuid(uid) < 0) rb_sys_fail(0);
6408 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6410 if (setruid(0) < 0) rb_sys_fail(0);
6413 if (setruid(0) < 0) rb_sys_fail(0);
6415 if (seteuid(uid) < 0) rb_sys_fail(0);
6416 if (setruid(uid) < 0) rb_sys_fail(0);
6417 SAVED_USER_ID = uid;
6422 if (seteuid(uid) < 0) rb_sys_fail(0);
6423 if (setruid(uid) < 0) rb_sys_fail(0);
6424 SAVED_USER_ID = uid;
6432#if defined(HAVE_SETRESUID)
6433 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6434 (geteuid() == uid)? (rb_uid_t)-1: uid,
6435 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6436 SAVED_USER_ID = uid;
6437#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6438 if (SAVED_USER_ID == uid) {
6439 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6440 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6443 else if (getuid() != uid) {
6444 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6446 SAVED_USER_ID = uid;
6448 else if ( geteuid() != uid) {
6449 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6450 SAVED_USER_ID = uid;
6451 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6454 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6455 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6456 SAVED_USER_ID = uid;
6457 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6459#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6460 if (SAVED_USER_ID == uid) {
6461 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6462 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6464 else if ( geteuid() == uid) {
6465 if (getuid() != uid) {
6466 if (setruid(uid) < 0) rb_sys_fail(0);
6467 SAVED_USER_ID = uid;
6470 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6471 SAVED_USER_ID = uid;
6472 if (setruid(uid) < 0) rb_sys_fail(0);
6475 else if ( getuid() == uid) {
6476 if (seteuid(uid) < 0) rb_sys_fail(0);
6477 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6478 SAVED_USER_ID = uid;
6479 if (setruid(uid) < 0) rb_sys_fail(0);
6484#elif defined HAVE_44BSD_SETUID
6485 if (getuid() == uid) {
6487 if (setuid(uid) < 0) rb_sys_fail(0);
6488 SAVED_USER_ID = uid;
6493#elif defined HAVE_SETEUID
6494 if (getuid() == uid && SAVED_USER_ID == uid) {
6495 if (seteuid(uid) < 0) rb_sys_fail(0);
6500#elif defined HAVE_SETUID
6501 if (getuid() == uid && SAVED_USER_ID == uid) {
6502 if (setuid(uid) < 0) rb_sys_fail(0);
6516#if defined HAVE_SETGID
6530 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6534#define p_sys_setgid rb_f_notimplement
6538#if defined HAVE_SETRGID
6552 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6556#define p_sys_setrgid rb_f_notimplement
6560#if defined HAVE_SETEGID
6574 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6578#define p_sys_setegid rb_f_notimplement
6582#if defined HAVE_SETREGID
6597 rb_gid_t rgid, egid;
6599 rgid = OBJ2GID(rid);
6600 egid = OBJ2GID(eid);
6601 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6605#define p_sys_setregid rb_f_notimplement
6608#if defined HAVE_SETRESGID
6623 rb_gid_t rgid, egid, sgid;
6625 rgid = OBJ2GID(rid);
6626 egid = OBJ2GID(eid);
6627 sgid = OBJ2GID(sid);
6628 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6632#define p_sys_setresgid rb_f_notimplement
6636#if defined HAVE_ISSETUGID
6650p_sys_issetugid(
VALUE obj)
6652 return RBOOL(issetugid());
6655#define p_sys_issetugid rb_f_notimplement
6672proc_getgid(
VALUE obj)
6674 rb_gid_t gid = getgid();
6679#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6698#if defined(HAVE_SETRESGID)
6699 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6700#elif defined HAVE_SETREGID
6701 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6702#elif defined HAVE_SETRGID
6703 if (setrgid(gid) < 0) rb_sys_fail(0);
6704#elif defined HAVE_SETGID
6706 if (getegid() == gid) {
6707 if (setgid(gid) < 0) rb_sys_fail(0);
6717#define proc_setgid rb_f_notimplement
6721#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6741static int _maxgroups = -1;
6743get_sc_ngroups_max(
void)
6745#ifdef _SC_NGROUPS_MAX
6746 return (
int)sysconf(_SC_NGROUPS_MAX);
6747#elif defined(NGROUPS_MAX)
6748 return (
int)NGROUPS_MAX;
6756 if (_maxgroups < 0) {
6757 _maxgroups = get_sc_ngroups_max();
6759 _maxgroups = RB_MAX_GROUPS;
6768#ifdef HAVE_GETGROUPS
6792proc_getgroups(
VALUE obj)
6798 ngroups = getgroups(0, NULL);
6802 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6804 ngroups = getgroups(ngroups, groups);
6809 for (i = 0; i < ngroups; i++)
6817#define proc_getgroups rb_f_notimplement
6821#ifdef HAVE_SETGROUPS
6845 ngroups = RARRAY_LENINT(ary);
6846 if (ngroups > maxgroups())
6847 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6849 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6851 for (i = 0; i < ngroups; i++) {
6854 groups[i] = OBJ2GID1(g);
6858 if (setgroups(ngroups, groups) == -1)
6863 return proc_getgroups(obj);
6866#define proc_setgroups rb_f_notimplement
6870#ifdef HAVE_INITGROUPS
6896 return proc_getgroups(obj);
6899#define proc_initgroups rb_f_notimplement
6902#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6915proc_getmaxgroups(
VALUE obj)
6920#define proc_getmaxgroups rb_f_notimplement
6923#ifdef HAVE_SETGROUPS
6936 int ngroups_max = get_sc_ngroups_max();
6939 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6941 if (ngroups > RB_MAX_GROUPS)
6942 ngroups = RB_MAX_GROUPS;
6944 if (ngroups_max > 0 && ngroups > ngroups_max)
6945 ngroups = ngroups_max;
6947 _maxgroups = ngroups;
6952#define proc_setmaxgroups rb_f_notimplement
6955#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6956static int rb_daemon(
int nochdir,
int noclose);
6981 int n, nochdir = FALSE, noclose = FALSE;
6983 switch (rb_check_arity(argc, 0, 2)) {
6984 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6985 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6989 n = rb_daemon(nochdir, noclose);
6990 if (n < 0) rb_sys_fail(
"daemon");
6994extern const char ruby_null_device[];
6997rb_daemon(
int nochdir,
int noclose)
7001 rb_pid_t pid = getpid();
7003 rb_thread_acquire_fork_lock();
7004 err = daemon(nochdir, noclose);
7006 release_fork_lock_after_fork(getpid() == pid ? pid : 0);
7011 switch (rb_fork_ruby(NULL)) {
7014 default: _exit(EXIT_SUCCESS);
7018 if (setsid() < 0) (void)0;
7023 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
7035#define proc_daemon rb_f_notimplement
7048static rb_gid_t SAVED_GROUP_ID = -1;
7050#ifdef BROKEN_SETREGID
7052setregid(rb_gid_t rgid, rb_gid_t egid)
7054 if (rgid != (rb_gid_t)-1 && rgid != getgid()) {
7055 if (egid == (rb_gid_t)-1) egid = getegid();
7056 if (setgid(rgid) < 0)
return -1;
7058 if (egid != (rb_gid_t)-1 && egid != getegid()) {
7059 if (setegid(egid) < 0)
return -1;
7087 if (geteuid() == 0) {
7088#if defined(HAVE_SETRESGID)
7089 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
7090 SAVED_GROUP_ID = gid;
7091#elif defined HAVE_SETGID
7092 if (setgid(gid) < 0) rb_sys_fail(0);
7093 SAVED_GROUP_ID = gid;
7094#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7095 if (getgid() == gid) {
7096 if (SAVED_GROUP_ID == gid) {
7097 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7101 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7102 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
7104 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7105 SAVED_GROUP_ID = gid;
7108 if (setregid(0, 0) < 0) rb_sys_fail(0);
7110 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7111 SAVED_GROUP_ID = gid;
7116 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7117 SAVED_GROUP_ID = gid;
7119#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7120 if (getgid() == gid) {
7121 if (SAVED_GROUP_ID == gid) {
7122 if (setegid(gid) < 0) rb_sys_fail(0);
7126 if (setegid(gid) < 0) rb_sys_fail(0);
7127 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7129 if (setrgid(0) < 0) rb_sys_fail(0);
7132 if (setrgid(0) < 0) rb_sys_fail(0);
7134 if (setegid(gid) < 0) rb_sys_fail(0);
7135 if (setrgid(gid) < 0) rb_sys_fail(0);
7136 SAVED_GROUP_ID = gid;
7141 if (setegid(gid) < 0) rb_sys_fail(0);
7142 if (setrgid(gid) < 0) rb_sys_fail(0);
7143 SAVED_GROUP_ID = gid;
7150#if defined(HAVE_SETRESGID)
7151 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7152 (getegid() == gid)? (rb_gid_t)-1: gid,
7153 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7154 SAVED_GROUP_ID = gid;
7155#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7156 if (SAVED_GROUP_ID == gid) {
7157 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7158 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7161 else if (getgid() != gid) {
7162 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7164 SAVED_GROUP_ID = gid;
7166 else if ( getegid() != gid) {
7167 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7168 SAVED_GROUP_ID = gid;
7169 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7172 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7173 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7174 SAVED_GROUP_ID = gid;
7175 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7177#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7178 if (SAVED_GROUP_ID == gid) {
7179 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7180 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7182 else if ( getegid() == gid) {
7183 if (getgid() != gid) {
7184 if (setrgid(gid) < 0) rb_sys_fail(0);
7185 SAVED_GROUP_ID = gid;
7188 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7189 SAVED_GROUP_ID = gid;
7190 if (setrgid(gid) < 0) rb_sys_fail(0);
7193 else if ( getgid() == gid) {
7194 if (setegid(gid) < 0) rb_sys_fail(0);
7195 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7196 SAVED_GROUP_ID = gid;
7197 if (setrgid(gid) < 0) rb_sys_fail(0);
7202#elif defined HAVE_44BSD_SETGID
7203 if (getgid() == gid) {
7205 if (setgid(gid) < 0) rb_sys_fail(0);
7206 SAVED_GROUP_ID = gid;
7211#elif defined HAVE_SETEGID
7212 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7213 if (setegid(gid) < 0) rb_sys_fail(0);
7218#elif defined HAVE_SETGID
7219 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7220 if (setgid(gid) < 0) rb_sys_fail(0);
7247proc_geteuid(
VALUE obj)
7249 rb_uid_t euid = geteuid();
7253#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7255proc_seteuid(rb_uid_t uid)
7257#if defined(HAVE_SETRESUID)
7258 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7259#elif defined HAVE_SETREUID
7260 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7261#elif defined HAVE_SETEUID
7262 if (seteuid(uid) < 0) rb_sys_fail(0);
7263#elif defined HAVE_SETUID
7264 if (uid == getuid()) {
7265 if (setuid(uid) < 0) rb_sys_fail(0);
7276#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7290 proc_seteuid(OBJ2UID(euid));
7294#define proc_seteuid_m rb_f_notimplement
7298rb_seteuid_core(rb_uid_t euid)
7300#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7306#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7310#if defined(HAVE_SETRESUID)
7312 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7313 SAVED_USER_ID = euid;
7316 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7318#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7319 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7321 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7322 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7323 SAVED_USER_ID = euid;
7325#elif defined HAVE_SETEUID
7326 if (seteuid(euid) < 0) rb_sys_fail(0);
7327#elif defined HAVE_SETUID
7328 if (geteuid() == 0) rb_sys_fail(0);
7329 if (setuid(euid) < 0) rb_sys_fail(0);
7354 rb_seteuid_core(OBJ2UID(
id));
7373proc_getegid(
VALUE obj)
7375 rb_gid_t egid = getegid();
7380#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7393#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7399#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7400 gid = OBJ2GID(egid);
7403#if defined(HAVE_SETRESGID)
7404 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7405#elif defined HAVE_SETREGID
7406 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7407#elif defined HAVE_SETEGID
7408 if (setegid(gid) < 0) rb_sys_fail(0);
7409#elif defined HAVE_SETGID
7410 if (gid == getgid()) {
7411 if (setgid(gid) < 0) rb_sys_fail(0);
7423#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7424#define proc_setegid_m proc_setegid
7426#define proc_setegid_m rb_f_notimplement
7430rb_setegid_core(rb_gid_t egid)
7432#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7438#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7442#if defined(HAVE_SETRESGID)
7444 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7445 SAVED_GROUP_ID = egid;
7448 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7450#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7451 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7453 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7454 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7455 SAVED_GROUP_ID = egid;
7457#elif defined HAVE_SETEGID
7458 if (setegid(egid) < 0) rb_sys_fail(0);
7459#elif defined HAVE_SETGID
7460 if (geteuid() == 0 ) rb_sys_fail(0);
7461 if (setgid(egid) < 0) rb_sys_fail(0);
7486 rb_setegid_core(OBJ2GID(
id));
7501p_uid_exchangeable(
VALUE _)
7503#if defined(HAVE_SETRESUID)
7505#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7526p_uid_exchange(
VALUE obj)
7529#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7536#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7540#if defined(HAVE_SETRESUID)
7541 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7542 SAVED_USER_ID = uid;
7543#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7544 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7545 SAVED_USER_ID = uid;
7563p_gid_exchangeable(
VALUE _)
7565#if defined(HAVE_SETRESGID)
7567#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7588p_gid_exchange(
VALUE obj)
7591#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7598#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7602#if defined(HAVE_SETRESGID)
7603 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7604 SAVED_GROUP_ID = gid;
7605#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7606 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7607 SAVED_GROUP_ID = gid;
7626p_uid_have_saved_id(
VALUE _)
7628#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7636#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7638p_uid_sw_ensure(
VALUE i)
7640 rb_uid_t
id = (rb_uid_t)i;
7641 under_uid_switch = 0;
7642 id = rb_seteuid_core(
id);
7661p_uid_switch(
VALUE obj)
7673 under_uid_switch = 1;
7680 else if (euid != SAVED_USER_ID) {
7681 proc_seteuid(SAVED_USER_ID);
7683 under_uid_switch = 1;
7698p_uid_sw_ensure(
VALUE obj)
7700 under_uid_switch = 0;
7701 return p_uid_exchange(obj);
7705p_uid_switch(
VALUE obj)
7717 p_uid_exchange(obj);
7719 under_uid_switch = 1;
7741p_gid_have_saved_id(
VALUE _)
7743#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7750#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7752p_gid_sw_ensure(
VALUE i)
7754 rb_gid_t
id = (rb_gid_t)i;
7755 under_gid_switch = 0;
7756 id = rb_setegid_core(
id);
7775p_gid_switch(
VALUE obj)
7787 under_gid_switch = 1;
7794 else if (egid != SAVED_GROUP_ID) {
7795 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7797 under_gid_switch = 1;
7812p_gid_sw_ensure(
VALUE obj)
7814 under_gid_switch = 0;
7815 return p_gid_exchange(obj);
7819p_gid_switch(
VALUE obj)
7831 p_gid_exchange(obj);
7833 under_gid_switch = 1;
7843#if defined(HAVE_TIMES)
7847#ifdef HAVE__SC_CLK_TCK
7848 return sysconf(_SC_CLK_TCK);
7849#elif defined CLK_TCK
7872rb_proc_times(
VALUE obj)
7874 VALUE utime, stime, cutime, cstime, ret;
7875#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7876 struct rusage usage_s, usage_c;
7878 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7879 rb_sys_fail(
"getrusage");
7880 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7881 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7882 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7883 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7885 const double hertz = (double)get_clk_tck();
7889 utime =
DBL2NUM(buf.tms_utime / hertz);
7890 stime =
DBL2NUM(buf.tms_stime / hertz);
7891 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7892 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7894 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7902#define rb_proc_times rb_f_notimplement
7905#ifdef HAVE_LONG_LONG
7907#define TIMETICK_INT_MIN LLONG_MIN
7908#define TIMETICK_INT_MAX LLONG_MAX
7909#define TIMETICK_INT2NUM(v) LL2NUM(v)
7910#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7912typedef long timetick_int_t;
7913#define TIMETICK_INT_MIN LONG_MIN
7914#define TIMETICK_INT_MAX LONG_MAX
7915#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7916#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7919CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7920static timetick_int_t
7921gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7941reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7943 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7951reduce_factors(timetick_int_t *numerators,
int num_numerators,
7952 timetick_int_t *denominators,
int num_denominators)
7955 for (i = 0; i < num_numerators; i++) {
7956 if (numerators[i] == 1)
7958 for (j = 0; j < num_denominators; j++) {
7959 if (denominators[j] == 1)
7961 reduce_fraction(&numerators[i], &denominators[j]);
7967 timetick_int_t giga_count;
7972timetick2dblnum(
struct timetick *ttp,
7973 timetick_int_t *numerators,
int num_numerators,
7974 timetick_int_t *denominators,
int num_denominators)
7979 reduce_factors(numerators, num_numerators,
7980 denominators, num_denominators);
7982 d = ttp->giga_count * 1e9 + ttp->count;
7984 for (i = 0; i < num_numerators; i++)
7986 for (i = 0; i < num_denominators; i++)
7987 d /= denominators[i];
7993timetick2dblnum_reciprocal(
struct timetick *ttp,
7994 timetick_int_t *numerators,
int num_numerators,
7995 timetick_int_t *denominators,
int num_denominators)
8000 reduce_factors(numerators, num_numerators,
8001 denominators, num_denominators);
8004 for (i = 0; i < num_denominators; i++)
8005 d *= denominators[i];
8006 for (i = 0; i < num_numerators; i++)
8008 d /= ttp->giga_count * 1e9 + ttp->count;
8013#define NDIV(x,y) (-(-((x)+1)/(y))-1)
8014#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
8017timetick2integer(
struct timetick *ttp,
8018 timetick_int_t *numerators,
int num_numerators,
8019 timetick_int_t *denominators,
int num_denominators)
8024 reduce_factors(numerators, num_numerators,
8025 denominators, num_denominators);
8027 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
8028 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
8029 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
8030 for (i = 0; i < num_numerators; i++) {
8031 timetick_int_t factor = numerators[i];
8032 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
8036 for (i = 0; i < num_denominators; i++) {
8037 t = DIV(t, denominators[i]);
8039 return TIMETICK_INT2NUM(t);
8043 v = TIMETICK_INT2NUM(ttp->giga_count);
8046 for (i = 0; i < num_numerators; i++) {
8047 timetick_int_t factor = numerators[i];
8050 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
8052 for (i = 0; i < num_denominators; i++) {
8053 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
8059make_clock_result(
struct timetick *ttp,
8060 timetick_int_t *numerators,
int num_numerators,
8061 timetick_int_t *denominators,
int num_denominators,
8064 if (unit ==
ID2SYM(id_nanosecond)) {
8065 numerators[num_numerators++] = 1000000000;
8066 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8068 else if (unit ==
ID2SYM(id_microsecond)) {
8069 numerators[num_numerators++] = 1000000;
8070 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8072 else if (unit ==
ID2SYM(id_millisecond)) {
8073 numerators[num_numerators++] = 1000;
8074 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8076 else if (unit ==
ID2SYM(id_second)) {
8077 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8079 else if (unit ==
ID2SYM(id_float_microsecond)) {
8080 numerators[num_numerators++] = 1000000;
8081 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8083 else if (unit ==
ID2SYM(id_float_millisecond)) {
8084 numerators[num_numerators++] = 1000;
8085 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8087 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
8088 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8091 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
8095static const mach_timebase_info_data_t *
8096get_mach_timebase_info(
void)
8098 static mach_timebase_info_data_t sTimebaseInfo;
8100 if ( sTimebaseInfo.denom == 0 ) {
8101 (void) mach_timebase_info(&sTimebaseInfo);
8104 return &sTimebaseInfo;
8108ruby_real_ms_time(
void)
8110 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8111 uint64_t t = mach_absolute_time();
8112 return (
double)t * info->numer / info->denom / 1e6;
8116#if defined(NUM2CLOCKID)
8117# define NUMERIC_CLOCKID 1
8119# define NUMERIC_CLOCKID 0
8120# define NUM2CLOCKID(x) 0
8123#define clock_failed(name, err, arg) do { \
8124 int clock_error = (err); \
8125 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8298 timetick_int_t numerators[2];
8299 timetick_int_t denominators[2];
8300 int num_numerators = 0;
8301 int num_denominators = 0;
8303 VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] :
Qnil;
8304 VALUE clk_id = argv[0];
8305#ifdef HAVE_CLOCK_GETTIME
8310#ifdef CLOCK_REALTIME
8311 if (clk_id == RUBY_CLOCK_REALTIME) {
8317#ifdef CLOCK_MONOTONIC
8318 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8319 c = CLOCK_MONOTONIC;
8324#ifdef CLOCK_PROCESS_CPUTIME_ID
8325 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8326 c = CLOCK_PROCESS_CPUTIME_ID;
8331#ifdef CLOCK_THREAD_CPUTIME_ID
8332 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8333 c = CLOCK_THREAD_CPUTIME_ID;
8341#ifdef HAVE_GETTIMEOFDAY
8346#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8347 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8349 ret = gettimeofday(&tv, 0);
8351 rb_sys_fail(
"gettimeofday");
8352 tt.giga_count = tv.tv_sec;
8353 tt.count = (int32_t)tv.tv_usec * 1000;
8354 denominators[num_denominators++] = 1000000000;
8359#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8360 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8363 if (t == (time_t)-1)
8364 rb_sys_fail(
"time");
8367 denominators[num_denominators++] = 1000000000;
8372#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8373 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8374 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8377 unsigned_clock_t uc;
8379 if (c == (clock_t)-1)
8380 rb_sys_fail(
"times");
8381 uc = (unsigned_clock_t)c;
8382 tt.count = (int32_t)(uc % 1000000000);
8383 tt.giga_count = (uc / 1000000000);
8384 denominators[num_denominators++] = get_clk_tck();
8390#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8391 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8392 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8393 struct rusage usage;
8395 ret = getrusage(RUSAGE_SELF, &usage);
8397 rb_sys_fail(
"getrusage");
8398 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8399 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8400 if (1000000 <= usec) {
8404 tt.count = usec * 1000;
8405 denominators[num_denominators++] = 1000000000;
8411#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8412 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8413 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8415 unsigned_clock_t utime, stime;
8416 if (times(&buf) == (clock_t)-1)
8417 rb_sys_fail(
"times");
8418 utime = (unsigned_clock_t)buf.tms_utime;
8419 stime = (unsigned_clock_t)buf.tms_stime;
8420 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8421 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8422 if (1000000000 <= tt.count) {
8423 tt.count -= 1000000000;
8426 denominators[num_denominators++] = get_clk_tck();
8431#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8432 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8433 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8435 unsigned_clock_t uc;
8438 if (c == (clock_t)-1)
8439 rb_sys_fail(
"clock");
8440 uc = (unsigned_clock_t)c;
8441 tt.count = (int32_t)(uc % 1000000000);
8442 tt.giga_count = uc / 1000000000;
8443 denominators[num_denominators++] = CLOCKS_PER_SEC;
8448 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8449 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8450 uint64_t t = mach_absolute_time();
8451 tt.count = (int32_t)(t % 1000000000);
8452 tt.giga_count = t / 1000000000;
8453 numerators[num_numerators++] = info->numer;
8454 denominators[num_denominators++] = info->denom;
8455 denominators[num_denominators++] = 1000000000;
8460 else if (NUMERIC_CLOCKID) {
8461#if defined(HAVE_CLOCK_GETTIME)
8463 c = NUM2CLOCKID(clk_id);
8465 ret = clock_gettime(c, &ts);
8467 clock_failed(
"gettime",
errno, clk_id);
8468 tt.count = (int32_t)ts.tv_nsec;
8469 tt.giga_count = ts.tv_sec;
8470 denominators[num_denominators++] = 1000000000;
8477 clock_failed(
"gettime", EINVAL, clk_id);
8480 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8525 timetick_int_t numerators[2];
8526 timetick_int_t denominators[2];
8527 int num_numerators = 0;
8528 int num_denominators = 0;
8529#ifdef HAVE_CLOCK_GETRES
8533 VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] :
Qnil;
8534 VALUE clk_id = argv[0];
8537#ifdef CLOCK_REALTIME
8538 if (clk_id == RUBY_CLOCK_REALTIME) {
8544#ifdef CLOCK_MONOTONIC
8545 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8546 c = CLOCK_MONOTONIC;
8551#ifdef CLOCK_PROCESS_CPUTIME_ID
8552 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8553 c = CLOCK_PROCESS_CPUTIME_ID;
8558#ifdef CLOCK_THREAD_CPUTIME_ID
8559 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8560 c = CLOCK_THREAD_CPUTIME_ID;
8565#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8566 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8569 denominators[num_denominators++] = 1000000000;
8574#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8575 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8578 denominators[num_denominators++] = 1000000000;
8583#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8584 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8587 denominators[num_denominators++] = get_clk_tck();
8592#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8593 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8596 denominators[num_denominators++] = 1000000000;
8601#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8602 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8605 denominators[num_denominators++] = get_clk_tck();
8610#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8611 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8614 denominators[num_denominators++] = CLOCKS_PER_SEC;
8619#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8620 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8621 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8624 numerators[num_numerators++] = info->numer;
8625 denominators[num_denominators++] = info->denom;
8626 denominators[num_denominators++] = 1000000000;
8631 else if (NUMERIC_CLOCKID) {
8632#if defined(HAVE_CLOCK_GETRES)
8634 c = NUM2CLOCKID(clk_id);
8636 ret = clock_getres(c, &ts);
8638 clock_failed(
"getres",
errno, clk_id);
8639 tt.count = (int32_t)ts.tv_nsec;
8640 tt.giga_count = ts.tv_sec;
8641 denominators[num_denominators++] = 1000000000;
8648 clock_failed(
"getres", EINVAL, clk_id);
8651 if (unit ==
ID2SYM(id_hertz)) {
8652 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8655 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8660get_CHILD_STATUS(
ID _x,
VALUE *_y)
8666get_PROCESS_ID(
ID _x,
VALUE *_y)
8758static VALUE rb_mProcUID;
8759static VALUE rb_mProcGID;
8760static VALUE rb_mProcID_Syscall;
8792 rb_gc_prepare_heap();
9220 rb_gvar_ractor_local(
"$$");
9221 rb_gvar_ractor_local(
"$?");
9276 process_status_dump, process_status_load);
9312#ifdef HAVE_GETPRIORITY
9323#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9325 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9326#ifdef RLIM_SAVED_MAX
9328 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9330 rb_define_const(
rb_mProcess,
"RLIM_SAVED_MAX", v);
9334 rb_define_const(
rb_mProcess,
"RLIM_INFINITY", inf);
9335#ifdef RLIM_SAVED_CUR
9337 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9339 rb_define_const(
rb_mProcess,
"RLIM_SAVED_CUR", v);
9378#ifdef RLIMIT_MEMLOCK
9385#ifdef RLIMIT_MSGQUEUE
9451#ifdef RLIMIT_SIGPENDING
9486#if defined(RUBY_CLOCK_REALTIME)
9487#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9488# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9489#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9490# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9492#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9494 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
9495#elif defined(RUBY_CLOCK_REALTIME)
9496 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
9499#if defined(RUBY_CLOCK_MONOTONIC)
9500#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9501# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9503#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9505 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
9506#elif defined(RUBY_CLOCK_MONOTONIC)
9507 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", RUBY_CLOCK_MONOTONIC);
9510#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9511#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9512# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9514#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9516 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
9517#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9518 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", RUBY_CLOCK_PROCESS_CPUTIME_ID);
9521#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9523 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
9524#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9525 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", RUBY_CLOCK_THREAD_CPUTIME_ID);
9531 rb_define_const(
rb_mProcess,
"CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
9535 rb_define_const(
rb_mProcess,
"CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
9537#ifdef CLOCK_REALTIME_FAST
9539 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
9541#ifdef CLOCK_REALTIME_PRECISE
9543 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
9545#ifdef CLOCK_REALTIME_COARSE
9547 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_COARSE", CLOCKID2NUM(CLOCK_REALTIME_COARSE));
9549#ifdef CLOCK_REALTIME_ALARM
9551 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_ALARM", CLOCKID2NUM(CLOCK_REALTIME_ALARM));
9553#ifdef CLOCK_MONOTONIC_FAST
9555 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
9557#ifdef CLOCK_MONOTONIC_PRECISE
9559 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
9561#ifdef CLOCK_MONOTONIC_RAW
9563 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
9565#ifdef CLOCK_MONOTONIC_RAW_APPROX
9567 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW_APPROX", CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX));
9569#ifdef CLOCK_MONOTONIC_COARSE
9571 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_COARSE", CLOCKID2NUM(CLOCK_MONOTONIC_COARSE));
9573#ifdef CLOCK_BOOTTIME
9575 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME", CLOCKID2NUM(CLOCK_BOOTTIME));
9577#ifdef CLOCK_BOOTTIME_ALARM
9579 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME_ALARM", CLOCKID2NUM(CLOCK_BOOTTIME_ALARM));
9583 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
9585#ifdef CLOCK_UPTIME_FAST
9587 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
9589#ifdef CLOCK_UPTIME_PRECISE
9591 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
9593#ifdef CLOCK_UPTIME_RAW
9595 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW", CLOCKID2NUM(CLOCK_UPTIME_RAW));
9597#ifdef CLOCK_UPTIME_RAW_APPROX
9599 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW_APPROX", CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX));
9603 rb_define_const(
rb_mProcess,
"CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
9607 rb_define_const(
rb_mProcess,
"CLOCK_TAI", CLOCKID2NUM(CLOCK_TAI));
9613#if defined(HAVE_TIMES) || defined(_WIN32)
9627 SAVED_USER_ID = geteuid();
9628 SAVED_GROUP_ID = getegid();
9651#ifdef p_uid_from_name
9654#ifdef p_gid_from_name
9685#define define_id(name) id_##name = rb_intern_const(#name)
9698 define_id(new_pgroup);
9700 define_id(unsetenv_others);
9703 define_id(close_others);
9704 define_id(nanosecond);
9705 define_id(microsecond);
9706 define_id(millisecond);
9708 define_id(float_microsecond);
9709 define_id(float_millisecond);
9710 define_id(float_second);
9711 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9712 define_id(TIME_BASED_CLOCK_REALTIME);
9713#ifdef CLOCK_REALTIME
9714 define_id(CLOCK_REALTIME);
9716#ifdef CLOCK_MONOTONIC
9717 define_id(CLOCK_MONOTONIC);
9719#ifdef CLOCK_PROCESS_CPUTIME_ID
9720 define_id(CLOCK_PROCESS_CPUTIME_ID);
9722#ifdef CLOCK_THREAD_CPUTIME_ID
9723 define_id(CLOCK_THREAD_CPUTIME_ID);
9726 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9727 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9730 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9732 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9734 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
9737#ifdef HAVE_WORKING_FORK
#define ISUPPER
@old{rb_isupper}
#define TOUPPER
@old{rb_toupper}
#define ISLOWER
@old{rb_islower}
#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 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 NUM2UINT
Old name of RB_NUM2UINT.
#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 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_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
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.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
void rb_exit(int status)
Terminates the current execution context.
VALUE rb_mProcess
Process module.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_cThread
Thread class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
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.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
#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_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the 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.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
#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 symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
int rb_io_modestr_oflags(const char *modestr)
Identical to rb_io_modestr_fmode(), except it returns a mixture of O_ flags.
#define GetOpenFile
This is an old name of RB_IO_POINTER.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
VALUE rb_io_check_io(VALUE io)
Try converting an object to its IO representation using its to_io method, if any.
int len
Length of the buffer.
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.
#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.
#define RARRAY_AREF(a, i)
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValue(v)
Ensures that the parameter object is a String.
#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.
#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.