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 if (sargp && (eargp->fd_dup2 !=
Qfalse || eargp->fd_dup2_child !=
Qfalse)) {
3531 rb_execarg_allocate_dup2_tmpbuf(sargp, n);
3534 obj = eargp->fd_dup2;
3536 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3540 obj = eargp->fd_close;
3543 rb_warn(
"cannot close fd before spawn");
3545 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3550#ifdef HAVE_WORKING_FORK
3551 if (eargp->close_others_do) {
3556 obj = eargp->fd_dup2_child;
3558 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3562 if (eargp->chdir_given) {
3564 sargp->chdir_given = 1;
3565 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3567 if (chdir(RSTRING_PTR(eargp->chdir_dir)) == -1) {
3574 if (eargp->gid_given) {
3575 if (setgid(eargp->gid) < 0) {
3582 if (eargp->uid_given) {
3583 if (setuid(eargp->uid) < 0) {
3591 int preserve =
errno;
3592 stdfd_clear_nonblock();
3598#if !defined(HAVE_WORKING_FORK)
3599# define execarg_restore(sargp) rb_execarg_run_options((sargp), NULL, errmsg, errmsg_buflen)
3601# define execarg_restore(sargp) (void)0
3606rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3608 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3613exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3615#if !defined(HAVE_WORKING_FORK)
3616 struct rb_execarg sarg, *
const sargp = &sarg;
3622 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3624 execarg_restore(sargp);
3628 if (eargp->use_shell) {
3629 err = proc_exec_sh(RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3632 char *abspath = NULL;
3633 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3634 abspath = RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3635 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3637 execarg_restore(sargp);
3642#ifdef HAVE_WORKING_FORK
3645rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3647 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3651proc_syswait(
VALUE pid)
3658move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3662 for (i = 0; i < n; i++) {
3681pipe_nocrash(
int filedes[2],
VALUE fds)
3689 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3704rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3711handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3717 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3723#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3726 if (!status && !ep) {
3731 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3732 if (status) status->status = state;
3733 if (!state)
return 0;
3742 if (state && !status) rb_jump_tag(state);
3748#define prefork() ( \
3749 rb_io_flush(rb_ractor_stdout()), \
3750 rb_io_flush(rb_ractor_stderr()) \
3780write_retry(
int fd,
const void *buf,
size_t len)
3785 w = write(fd, buf,
len);
3786 }
while (w < 0 &&
errno == EINTR);
3792read_retry(
int fd,
void *buf,
size_t len)
3796 if (set_blocking(fd) != 0) {
3798 rb_async_bug_errno(
"set_blocking failed reading child error",
errno);
3803 r = read(fd, buf,
len);
3804 }
while (r < 0 &&
errno == EINTR);
3810send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3815 if (write_retry(fd, &err,
sizeof(err)) < 0) err =
errno;
3816 if (errmsg && 0 < errmsg_buflen) {
3817 errmsg[errmsg_buflen-1] =
'\0';
3818 errmsg_buflen = strlen(errmsg);
3819 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3825recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3829 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3833 if (size ==
sizeof(err) &&
3834 errmsg && 0 < errmsg_buflen) {
3835 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3844#ifdef HAVE_WORKING_VFORK
3845#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3848getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3854 ret = getuidx(ID_SAVED);
3855 if (ret == (rb_uid_t)-1)
3860#define HAVE_GETRESUID
3863#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
3866getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
3872 ret = getgidx(ID_SAVED);
3873 if (ret == (rb_gid_t)-1)
3878#define HAVE_GETRESGID
3896 rb_uid_t ruid, euid;
3897 rb_gid_t rgid, egid;
3899#if defined HAVE_ISSETUGID
3904#ifdef HAVE_GETRESUID
3908 ret = getresuid(&ruid, &euid, &suid);
3910 rb_sys_fail(
"getresuid(2)");
3919 if (euid == 0 || euid != ruid)
3922#ifdef HAVE_GETRESGID
3926 ret = getresgid(&rgid, &egid, &sgid);
3928 rb_sys_fail(
"getresgid(2)");
3944struct child_handler_disabler_state
3950disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
3952#ifdef HAVE_PTHREAD_SIGMASK
3956 ret = sigfillset(&all);
3958 rb_sys_fail(
"sigfillset");
3960 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
3965# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3970disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
3972#ifdef HAVE_PTHREAD_SIGMASK
3975 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
3980# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3986disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
3991 for (sig = 1; sig < NSIG; sig++) {
3992 sig_t handler = signal(sig, SIG_DFL);
3994 if (handler == SIG_ERR &&
errno == EINVAL) {
3997 if (handler == SIG_ERR) {
3998 ERRMSG(
"signal to obtain old action");
4002 if (sig == SIGPIPE) {
4007 if (handler == SIG_IGN) {
4008 signal(sig, SIG_IGN);
4013 sigemptyset(&old->sigmask);
4014 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
4016 ERRMSG(
"sigprocmask");
4024 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4025 char *errmsg,
size_t errmsg_buflen,
4029 volatile int try_gc = 1;
4030 struct child_handler_disabler_state old;
4035 disable_child_handler_before_fork(&old);
4039#if defined(HAVE_WORKING_VFORK) && !defined(RUBY_ASAN_ENABLED)
4040 if (!has_privilege())
4050 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
4052 ret = chfunc(charg, errmsg, errmsg_buflen);
4053 if (!ret) _exit(EXIT_SUCCESS);
4055 send_child_error(ep[1], errmsg, errmsg_buflen);
4056#if EXIT_SUCCESS == 127
4057 _exit(EXIT_FAILURE);
4063 disable_child_handler_fork_parent(&old);
4067 if (handle_fork_error(err, status, ep, &try_gc))
4073fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4074 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4082 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4084 if (status) status->status = 0;
4086 if (pipe_nocrash(ep, fds))
return -1;
4088 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4090 if (status) status->pid = pid;
4093 if (status) status->error =
errno;
4100 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4102 if (error_occurred) {
4105 status->error = err;
4107 VM_ASSERT((w == 0) &&
"only used by extensions");
4108 rb_protect(proc_syswait, (
VALUE)pid, &state);
4110 status->status = state;
4131rb_fork_async_signal_safe(
int *status,
4132 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4133 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4137 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4140 *status = process_status.status;
4147rb_fork_ruby(
int *status)
4149 if (UNLIKELY(!rb_ractor_main_p())) {
4150 rb_raise(rb_eRactorIsolationError,
"can not fork from non-main Ractors");
4155 int try_gc = 1, err = 0;
4156 struct child_handler_disabler_state old;
4162 rb_thread_acquire_fork_lock();
4163 disable_child_handler_before_fork(&old);
4166 child.pid = pid = rb_fork();
4167 child.error = err =
errno;
4170 disable_child_handler_fork_parent(&old);
4171 release_fork_lock_after_fork(pid);
4172 after_fork_ruby(pid);
4175 }
while (pid < 0 && handle_fork_error(err, &child, NULL, &try_gc) == 0);
4177 if (status) *status = child.status;
4185 rb_pid_t pid = rb_fork_ruby(NULL);
4188 rb_sys_fail(
"fork(2)");
4195call_proc__fork_protected(
VALUE arg)
4204rb_call_proc__fork(
void)
4207 return proc_fork_pid();
4210 rb_pid_t parent = getpid(), pid;
4213 if (
NIL_P(rb_protect(call_proc__fork_protected, (
VALUE)&pid, &state))) {
4214 if (getpid() != parent) {
4224#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4245rb_proc__fork(
VALUE _obj)
4247 rb_pid_t pid = proc_fork_pid();
4316 pid = rb_call_proc__fork();
4330#define rb_proc__fork rb_f_notimplement
4331#define rb_f_fork rb_f_notimplement
4335exit_status_code(
VALUE status)
4341 istatus = EXIT_SUCCESS;
4344 istatus = EXIT_FAILURE;
4348#if EXIT_SUCCESS != 0
4350 istatus = EXIT_SUCCESS;
4357NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4375rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4379 if (rb_check_arity(argc, 0, 1) == 1) {
4380 istatus = exit_status_code(argv[0]);
4383 istatus = EXIT_FAILURE;
4393 if (GET_EC()->tag) {
4408 if (rb_check_arity(argc, 0, 1) == 1) {
4409 istatus = exit_status_code(argv[0]);
4412 istatus = EXIT_SUCCESS;
4475 rb_check_arity(argc, 0, 1);
4477 rb_execution_context_t *ec = GET_EC();
4478 VALUE errinfo = rb_ec_get_errinfo(ec);
4479 if (!
NIL_P(errinfo)) {
4480 rb_ec_error_print(ec, errinfo);
4487 args[1] = args[0] = argv[0];
4489 rb_io_puts(1, args, rb_ractor_stderr());
4490 args[0] =
INT2NUM(EXIT_FAILURE);
4528#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4533 if (eargp && !eargp->use_shell) {
4534 VALUE str = eargp->invoke.cmd.argv_str;
4535 VALUE buf = eargp->invoke.cmd.argv_buf;
4536 char *p, **argv = ARGVSTR2ARGV(str);
4537 long i, argc = ARGVSTR2ARGC(str);
4538 const char *start = RSTRING_PTR(buf);
4540 p = RSTRING_PTR(cmd);
4541 for (i = 1; i < argc; ++i) {
4542 p[argv[i] - start - 1] =
' ';
4552rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4555#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4558# if !defined HAVE_SPAWNV
4563#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4564 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4566 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4568 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4569 execarg_restore(&sarg);
4573 if (prog && !eargp->use_shell) {
4574 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4575 argv[0] = RSTRING_PTR(prog);
4577# if defined HAVE_SPAWNV
4578 if (eargp->use_shell) {
4579 pid = proc_spawn_sh(RSTRING_PTR(prog));
4582 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4583 pid = proc_spawn_cmd(argv, prog, eargp);
4590 status = system(rb_execarg_commandline(eargp, &prog));
4595 if (eargp->waitpid_state) {
4596 eargp->waitpid_state->pid = pid;
4599 execarg_restore(&sarg);
4614do_spawn_process(
VALUE arg)
4618 rb_execarg_parent_start1(argp->execarg);
4620 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4621 argp->errmsg.ptr, argp->errmsg.buflen);
4624NOINLINE(
static rb_pid_t
4625 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4628rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4632 args.execarg = execarg_obj;
4633 args.errmsg.ptr = errmsg;
4634 args.errmsg.buflen = errmsg_buflen;
4637 execarg_parent_end, execarg_obj);
4642rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4646 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4647 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4653 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4659 return rb_spawn_internal(argc, argv, NULL, 0);
4783 rb_thread_t *th = GET_THREAD();
4784 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4785 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4788 eargp->status = &status;
4790 last_status_clear(th);
4794 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4801 th->last_status = status;
4803 if (data->status == EXIT_SUCCESS) {
4807 if (data->error != 0) {
4808 if (eargp->exception) {
4809 VALUE command = eargp->invoke.sh.shell_script;
4817 else if (eargp->exception) {
4818 VALUE command = eargp->invoke.sh.shell_script;
4832 if (eargp->exception) {
4833 VALUE command = eargp->invoke.sh.shell_script;
4949 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
4950 VALUE execarg_obj, fail_str;
4953 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4954 eargp = rb_execarg_get(execarg_obj);
4955 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4957 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
4961 rb_exec_fail(eargp, err, errmsg);
4965#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
4991 time_t beg = time(0);
4994 if (scheduler !=
Qnil) {
4998 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
5002 rb_check_arity(argc, 0, 1);
5007 time_t end = time(0) - beg;
5009 return TIMET2NUM(end);
5013#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
5030#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
5032 if (pgrp < 0) rb_sys_fail(0);
5036 if (pgrp < 0) rb_sys_fail(0);
5041#define proc_getpgrp rb_f_notimplement
5045#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
5063 if (setpgid(0,0) < 0) rb_sys_fail(0);
5064#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
5065 if (setpgrp() < 0) rb_sys_fail(0);
5070#define proc_setpgrp rb_f_notimplement
5074#if defined(HAVE_GETPGID)
5092 if (i < 0) rb_sys_fail(0);
5096#define proc_getpgid rb_f_notimplement
5114 rb_pid_t ipid, ipgrp;
5119 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5123#define proc_setpgid rb_f_notimplement
5147 if (rb_check_arity(argc, 0, 1) == 1 && !
NIL_P(argv[0]))
5151 if (sid < 0) rb_sys_fail(0);
5155#define proc_getsid rb_f_notimplement
5159#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5160#if !defined(HAVE_SETSID)
5161static rb_pid_t ruby_setsid(
void);
5162#define setsid() ruby_setsid()
5183 if (pid < 0) rb_sys_fail(0);
5187#if !defined(HAVE_SETSID)
5188#define HAVE_SETSID 1
5196#if defined(SETPGRP_VOID)
5202 ret = setpgrp(0, pid);
5204 if (ret == -1)
return -1;
5208 ioctl(fd, TIOCNOTTY, NULL);
5215#define proc_setsid rb_f_notimplement
5219#ifdef HAVE_GETPRIORITY
5247 int prio, iwhich, iwho;
5253 prio = getpriority(iwhich, iwho);
5254 if (
errno) rb_sys_fail(0);
5258#define proc_getpriority rb_f_notimplement
5262#ifdef HAVE_GETPRIORITY
5282 int iwhich, iwho, iprio;
5288 if (setpriority(iwhich, iwho, iprio) < 0)
5293#define proc_setpriority rb_f_notimplement
5296#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5298rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5302#define RESCHECK(r) \
5304 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5305 resource = RLIMIT_##r; \
5339#ifdef RLIMIT_MEMLOCK
5342#ifdef RLIMIT_MSGQUEUE
5381#ifdef RLIMIT_SIGPENDING
5382 RESCHECK(SIGPENDING);
5391 for (p = name; *p; p++)
5397 for (p = name; *p; p++)
5403 rb_bug(
"unexpected casetype");
5410rlimit_type_by_hname(
const char *name,
long len)
5412 return rlimit_resource_name2int(name,
len, 0);
5416rlimit_type_by_lname(
const char *name,
long len)
5418 return rlimit_resource_name2int(name,
len, 1);
5422rlimit_type_by_sym(
VALUE key)
5425 const char *rname = RSTRING_PTR(name);
5426 long len = RSTRING_LEN(name);
5428 static const char prefix[] =
"rlimit_";
5429 enum {prefix_len =
sizeof(prefix)-1};
5431 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5432 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5440rlimit_resource_type(
VALUE rtype)
5447 switch (
TYPE(rtype)) {
5450 name = RSTRING_PTR(v);
5451 len = RSTRING_LEN(v);
5460 len = RSTRING_LEN(rtype);
5470 r = rlimit_type_by_hname(name,
len);
5474 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5480rlimit_resource_value(
VALUE rval)
5485 switch (
TYPE(rval)) {
5488 name = RSTRING_PTR(v);
5503 return NUM2RLIM(rval);
5507 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5509#ifdef RLIM_SAVED_MAX
5510 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5512#ifdef RLIM_SAVED_CUR
5513 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5515 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5521#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5549 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5550 rb_sys_fail(
"getrlimit");
5552 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5555#define proc_getrlimit rb_f_notimplement
5558#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5612proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5614 VALUE resource, rlim_cur, rlim_max;
5617 rb_check_arity(argc, 2, 3);
5620 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5621 rlim_max = rlim_cur;
5623 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5624 rlim.rlim_max = rlimit_resource_value(rlim_max);
5626 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5627 rb_sys_fail(
"setrlimit");
5632#define proc_setrlimit rb_f_notimplement
5635static int under_uid_switch = 0;
5637check_uid_switch(
void)
5639 if (under_uid_switch) {
5640 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5644static int under_gid_switch = 0;
5646check_gid_switch(
void)
5648 if (under_gid_switch) {
5649 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5654#if defined(HAVE_PWD_H)
5656login_not_found(
int err)
5658 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5669# if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5672 char MAYBE_UNUSED(*login) = NULL;
5674# ifdef USE_GETLOGIN_R
5676# if defined(__FreeBSD__)
5677 typedef int getlogin_r_size_t;
5679 typedef size_t getlogin_r_size_t;
5682 long loginsize = GETLOGIN_R_SIZE_INIT;
5685 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5689 login = RSTRING_PTR(maybe_result);
5694 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5695 if (login_not_found(gle)) {
5696 rb_str_resize(maybe_result, 0);
5700 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5701 rb_str_resize(maybe_result, 0);
5706 login = RSTRING_PTR(maybe_result);
5710 if (login == NULL) {
5711 rb_str_resize(maybe_result, 0);
5716 return maybe_result;
5718# elif defined(USE_GETLOGIN)
5724 if (login_not_found(err)) {
5738pwd_not_found(
int err)
5752# if defined(USE_GETPWNAM_R)
5753struct getpwnam_r_args {
5757 struct passwd *result;
5758 struct passwd pwstore;
5761# define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5762 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5765nogvl_getpwnam_r(
void *args)
5767 struct getpwnam_r_args *arg = args;
5768 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5773rb_getpwdirnam_for_login(
VALUE login_name)
5775#if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5779 if (
NIL_P(login_name)) {
5784 const char *login = RSTRING_PTR(login_name);
5787# ifdef USE_GETPWNAM_R
5790 long bufsizenm = GETPW_R_SIZE_INIT;
5793 bufsizenm = GETPW_R_SIZE_DEFAULT;
5797 bufnm = RSTRING_PTR(getpwnm_tmp);
5800 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5803 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5804 if (pwd_not_found(enm)) {
5805 rb_str_resize(getpwnm_tmp, 0);
5809 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5810 rb_str_resize(getpwnm_tmp, 0);
5815 args.buf = RSTRING_PTR(getpwnm_tmp);
5819 if (args.result == NULL) {
5821 rb_str_resize(getpwnm_tmp, 0);
5827 rb_str_resize(getpwnm_tmp, 0);
5830# elif defined(USE_GETPWNAM)
5832 struct passwd *pwptr;
5834 if (!(pwptr = getpwnam(login))) {
5837 if (pwd_not_found(err)) {
5851# if defined(USE_GETPWUID_R)
5852struct getpwuid_r_args {
5856 struct passwd *result;
5857 struct passwd pwstore;
5860# define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5861 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5864nogvl_getpwuid_r(
void *args)
5866 struct getpwuid_r_args *arg = args;
5867 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5877# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5881 uid_t ruid = getuid();
5883# ifdef USE_GETPWUID_R
5886 long bufsizeid = GETPW_R_SIZE_INIT;
5889 bufsizeid = GETPW_R_SIZE_DEFAULT;
5893 bufid = RSTRING_PTR(getpwid_tmp);
5896 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5899 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5900 if (pwd_not_found(eid)) {
5901 rb_str_resize(getpwid_tmp, 0);
5905 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5906 rb_str_resize(getpwid_tmp, 0);
5911 args.buf = RSTRING_PTR(getpwid_tmp);
5915 if (args.result == NULL) {
5917 rb_str_resize(getpwid_tmp, 0);
5923 rb_str_resize(getpwid_tmp, 0);
5926# elif defined(USE_GETPWUID)
5928 struct passwd *pwptr;
5930 if (!(pwptr = getpwuid(ruid))) {
5933 if (pwd_not_found(err)) {
5959#if defined(HAVE_PWD_H)
5962# ifdef USE_GETPWNAM_R
5975 struct passwd *pwptr;
5976#ifdef USE_GETPWNAM_R
5981 getpw_buf_len = GETPW_R_SIZE_INIT;
5982 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5985 getpw_buf = RSTRING_PTR(*getpw_tmp);
5989 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
5991 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5992 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5993 rb_str_resize(*getpw_tmp, 0);
5997 args.buf = RSTRING_PTR(*getpw_tmp);
6000 pwptr = args.result;
6002 pwptr = getpwnam(usrname);
6005#ifndef USE_GETPWNAM_R
6008 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
6010 uid = pwptr->pw_uid;
6011#ifndef USE_GETPWNAM_R
6018# ifdef p_uid_from_name
6038#if defined(HAVE_GRP_H)
6039# if defined(USE_GETGRNAM_R)
6040struct getgrnam_r_args {
6044 struct group *result;
6048# define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
6049 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
6052nogvl_getgrnam_r(
void *args)
6054 struct getgrnam_r_args *arg = args;
6055 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
6061# ifdef USE_GETGRNAM_R
6074 struct group *grptr;
6075#ifdef USE_GETGRNAM_R
6080 getgr_buf_len = GETGR_R_SIZE_INIT;
6081 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6084 getgr_buf = RSTRING_PTR(*getgr_tmp);
6088 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6090 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6091 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6092 rb_str_resize(*getgr_tmp, 0);
6096 args.buf = RSTRING_PTR(*getgr_tmp);
6099 grptr = args.result;
6100#elif defined(HAVE_GETGRNAM)
6101 grptr = getgrnam(grpname);
6106#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6109 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6111 gid = grptr->gr_gid;
6112#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6119# ifdef p_gid_from_name
6139#if defined HAVE_SETUID
6153 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6157#define p_sys_setuid rb_f_notimplement
6161#if defined HAVE_SETRUID
6175 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6179#define p_sys_setruid rb_f_notimplement
6183#if defined HAVE_SETEUID
6197 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6201#define p_sys_seteuid rb_f_notimplement
6205#if defined HAVE_SETREUID
6220 rb_uid_t ruid, euid;
6223 ruid = OBJ2UID1(rid);
6224 euid = OBJ2UID1(eid);
6226 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6230#define p_sys_setreuid rb_f_notimplement
6234#if defined HAVE_SETRESUID
6249 rb_uid_t ruid, euid, suid;
6252 ruid = OBJ2UID1(rid);
6253 euid = OBJ2UID1(eid);
6254 suid = OBJ2UID1(sid);
6256 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6260#define p_sys_setresuid rb_f_notimplement
6277proc_getuid(
VALUE obj)
6279 rb_uid_t uid = getuid();
6284#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6304#if defined(HAVE_SETRESUID)
6305 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6306#elif defined HAVE_SETREUID
6307 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6308#elif defined HAVE_SETRUID
6309 if (setruid(uid) < 0) rb_sys_fail(0);
6310#elif defined HAVE_SETUID
6312 if (geteuid() == uid) {
6313 if (setuid(uid) < 0) rb_sys_fail(0);
6323#define proc_setuid rb_f_notimplement
6337static rb_uid_t SAVED_USER_ID = -1;
6339#ifdef BROKEN_SETREUID
6341setreuid(rb_uid_t ruid, rb_uid_t euid)
6343 if (ruid != (rb_uid_t)-1 && ruid != getuid()) {
6344 if (euid == (rb_uid_t)-1) euid = geteuid();
6345 if (setuid(ruid) < 0)
return -1;
6347 if (euid != (rb_uid_t)-1 && euid != geteuid()) {
6348 if (seteuid(euid) < 0)
return -1;
6376 if (geteuid() == 0) {
6377#if defined(HAVE_SETRESUID)
6378 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6379 SAVED_USER_ID = uid;
6380#elif defined(HAVE_SETUID)
6381 if (setuid(uid) < 0) rb_sys_fail(0);
6382 SAVED_USER_ID = uid;
6383#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6384 if (getuid() == uid) {
6385 if (SAVED_USER_ID == uid) {
6386 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6390 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6391 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6393 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6394 SAVED_USER_ID = uid;
6397 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6399 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6400 SAVED_USER_ID = uid;
6405 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6406 SAVED_USER_ID = uid;
6408#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6409 if (getuid() == uid) {
6410 if (SAVED_USER_ID == uid) {
6411 if (seteuid(uid) < 0) rb_sys_fail(0);
6415 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6417 if (setruid(0) < 0) rb_sys_fail(0);
6420 if (setruid(0) < 0) rb_sys_fail(0);
6422 if (seteuid(uid) < 0) rb_sys_fail(0);
6423 if (setruid(uid) < 0) rb_sys_fail(0);
6424 SAVED_USER_ID = uid;
6429 if (seteuid(uid) < 0) rb_sys_fail(0);
6430 if (setruid(uid) < 0) rb_sys_fail(0);
6431 SAVED_USER_ID = uid;
6439#if defined(HAVE_SETRESUID)
6440 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6441 (geteuid() == uid)? (rb_uid_t)-1: uid,
6442 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6443 SAVED_USER_ID = uid;
6444#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6445 if (SAVED_USER_ID == uid) {
6446 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6447 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6450 else if (getuid() != uid) {
6451 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6453 SAVED_USER_ID = uid;
6455 else if ( geteuid() != uid) {
6456 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6457 SAVED_USER_ID = uid;
6458 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6461 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6462 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6463 SAVED_USER_ID = uid;
6464 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6466#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6467 if (SAVED_USER_ID == uid) {
6468 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6469 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6471 else if ( geteuid() == uid) {
6472 if (getuid() != uid) {
6473 if (setruid(uid) < 0) rb_sys_fail(0);
6474 SAVED_USER_ID = uid;
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);
6482 else if ( getuid() == uid) {
6483 if (seteuid(uid) < 0) rb_sys_fail(0);
6484 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6485 SAVED_USER_ID = uid;
6486 if (setruid(uid) < 0) rb_sys_fail(0);
6491#elif defined HAVE_44BSD_SETUID
6492 if (getuid() == uid) {
6494 if (setuid(uid) < 0) rb_sys_fail(0);
6495 SAVED_USER_ID = uid;
6500#elif defined HAVE_SETEUID
6501 if (getuid() == uid && SAVED_USER_ID == uid) {
6502 if (seteuid(uid) < 0) rb_sys_fail(0);
6507#elif defined HAVE_SETUID
6508 if (getuid() == uid && SAVED_USER_ID == uid) {
6509 if (setuid(uid) < 0) rb_sys_fail(0);
6523#if defined HAVE_SETGID
6537 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6541#define p_sys_setgid rb_f_notimplement
6545#if defined HAVE_SETRGID
6559 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6563#define p_sys_setrgid rb_f_notimplement
6567#if defined HAVE_SETEGID
6581 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6585#define p_sys_setegid rb_f_notimplement
6589#if defined HAVE_SETREGID
6604 rb_gid_t rgid, egid;
6606 rgid = OBJ2GID(rid);
6607 egid = OBJ2GID(eid);
6608 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6612#define p_sys_setregid rb_f_notimplement
6615#if defined HAVE_SETRESGID
6630 rb_gid_t rgid, egid, sgid;
6632 rgid = OBJ2GID(rid);
6633 egid = OBJ2GID(eid);
6634 sgid = OBJ2GID(sid);
6635 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6639#define p_sys_setresgid rb_f_notimplement
6643#if defined HAVE_ISSETUGID
6657p_sys_issetugid(
VALUE obj)
6659 return RBOOL(issetugid());
6662#define p_sys_issetugid rb_f_notimplement
6679proc_getgid(
VALUE obj)
6681 rb_gid_t gid = getgid();
6686#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6705#if defined(HAVE_SETRESGID)
6706 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6707#elif defined HAVE_SETREGID
6708 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6709#elif defined HAVE_SETRGID
6710 if (setrgid(gid) < 0) rb_sys_fail(0);
6711#elif defined HAVE_SETGID
6713 if (getegid() == gid) {
6714 if (setgid(gid) < 0) rb_sys_fail(0);
6724#define proc_setgid rb_f_notimplement
6728#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6748static int _maxgroups = -1;
6750get_sc_ngroups_max(
void)
6752#ifdef _SC_NGROUPS_MAX
6753 return (
int)sysconf(_SC_NGROUPS_MAX);
6754#elif defined(NGROUPS_MAX)
6755 return (
int)NGROUPS_MAX;
6763 if (_maxgroups < 0) {
6764 _maxgroups = get_sc_ngroups_max();
6766 _maxgroups = RB_MAX_GROUPS;
6775#ifdef HAVE_GETGROUPS
6799proc_getgroups(
VALUE obj)
6805 ngroups = getgroups(0, NULL);
6809 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6811 ngroups = getgroups(ngroups, groups);
6816 for (i = 0; i < ngroups; i++)
6824#define proc_getgroups rb_f_notimplement
6828#ifdef HAVE_SETGROUPS
6852 ngroups = RARRAY_LENINT(ary);
6853 if (ngroups > maxgroups())
6854 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6856 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6858 for (i = 0; i < ngroups; i++) {
6861 groups[i] = OBJ2GID1(g);
6865 if (setgroups(ngroups, groups) == -1)
6870 return proc_getgroups(obj);
6873#define proc_setgroups rb_f_notimplement
6877#ifdef HAVE_INITGROUPS
6903 return proc_getgroups(obj);
6906#define proc_initgroups rb_f_notimplement
6909#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6922proc_getmaxgroups(
VALUE obj)
6927#define proc_getmaxgroups rb_f_notimplement
6930#ifdef HAVE_SETGROUPS
6943 int ngroups_max = get_sc_ngroups_max();
6946 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6948 if (ngroups > RB_MAX_GROUPS)
6949 ngroups = RB_MAX_GROUPS;
6951 if (ngroups_max > 0 && ngroups > ngroups_max)
6952 ngroups = ngroups_max;
6954 _maxgroups = ngroups;
6959#define proc_setmaxgroups rb_f_notimplement
6962#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6963static int rb_daemon(
int nochdir,
int noclose);
6988 int n, nochdir = FALSE, noclose = FALSE;
6990 switch (rb_check_arity(argc, 0, 2)) {
6991 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6992 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6996 n = rb_daemon(nochdir, noclose);
6997 if (n < 0) rb_sys_fail(
"daemon");
7001extern const char ruby_null_device[];
7004rb_daemon(
int nochdir,
int noclose)
7008 rb_pid_t pid = getpid();
7010 rb_thread_acquire_fork_lock();
7011 err = daemon(nochdir, noclose);
7013 release_fork_lock_after_fork(getpid() == pid ? pid : 0);
7018 switch (rb_fork_ruby(NULL)) {
7021 default: _exit(EXIT_SUCCESS);
7025 if (setsid() < 0) (void)0;
7030 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
7042#define proc_daemon rb_f_notimplement
7055static rb_gid_t SAVED_GROUP_ID = -1;
7057#ifdef BROKEN_SETREGID
7059setregid(rb_gid_t rgid, rb_gid_t egid)
7061 if (rgid != (rb_gid_t)-1 && rgid != getgid()) {
7062 if (egid == (rb_gid_t)-1) egid = getegid();
7063 if (setgid(rgid) < 0)
return -1;
7065 if (egid != (rb_gid_t)-1 && egid != getegid()) {
7066 if (setegid(egid) < 0)
return -1;
7094 if (geteuid() == 0) {
7095#if defined(HAVE_SETRESGID)
7096 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
7097 SAVED_GROUP_ID = gid;
7098#elif defined HAVE_SETGID
7099 if (setgid(gid) < 0) rb_sys_fail(0);
7100 SAVED_GROUP_ID = gid;
7101#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7102 if (getgid() == gid) {
7103 if (SAVED_GROUP_ID == gid) {
7104 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7108 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7109 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
7111 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7112 SAVED_GROUP_ID = gid;
7115 if (setregid(0, 0) < 0) rb_sys_fail(0);
7117 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7118 SAVED_GROUP_ID = gid;
7123 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7124 SAVED_GROUP_ID = gid;
7126#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7127 if (getgid() == gid) {
7128 if (SAVED_GROUP_ID == gid) {
7129 if (setegid(gid) < 0) rb_sys_fail(0);
7133 if (setegid(gid) < 0) rb_sys_fail(0);
7134 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7136 if (setrgid(0) < 0) rb_sys_fail(0);
7139 if (setrgid(0) < 0) rb_sys_fail(0);
7141 if (setegid(gid) < 0) rb_sys_fail(0);
7142 if (setrgid(gid) < 0) rb_sys_fail(0);
7143 SAVED_GROUP_ID = gid;
7148 if (setegid(gid) < 0) rb_sys_fail(0);
7149 if (setrgid(gid) < 0) rb_sys_fail(0);
7150 SAVED_GROUP_ID = gid;
7157#if defined(HAVE_SETRESGID)
7158 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7159 (getegid() == gid)? (rb_gid_t)-1: gid,
7160 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7161 SAVED_GROUP_ID = gid;
7162#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7163 if (SAVED_GROUP_ID == gid) {
7164 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7165 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7168 else if (getgid() != gid) {
7169 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7171 SAVED_GROUP_ID = gid;
7173 else if ( getegid() != gid) {
7174 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7175 SAVED_GROUP_ID = gid;
7176 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7179 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7180 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7181 SAVED_GROUP_ID = gid;
7182 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7184#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7185 if (SAVED_GROUP_ID == gid) {
7186 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7187 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7189 else if ( getegid() == gid) {
7190 if (getgid() != gid) {
7191 if (setrgid(gid) < 0) rb_sys_fail(0);
7192 SAVED_GROUP_ID = gid;
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);
7200 else if ( getgid() == gid) {
7201 if (setegid(gid) < 0) rb_sys_fail(0);
7202 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7203 SAVED_GROUP_ID = gid;
7204 if (setrgid(gid) < 0) rb_sys_fail(0);
7209#elif defined HAVE_44BSD_SETGID
7210 if (getgid() == gid) {
7212 if (setgid(gid) < 0) rb_sys_fail(0);
7213 SAVED_GROUP_ID = gid;
7218#elif defined HAVE_SETEGID
7219 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7220 if (setegid(gid) < 0) rb_sys_fail(0);
7225#elif defined HAVE_SETGID
7226 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7227 if (setgid(gid) < 0) rb_sys_fail(0);
7254proc_geteuid(
VALUE obj)
7256 rb_uid_t euid = geteuid();
7260#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7262proc_seteuid(rb_uid_t uid)
7264#if defined(HAVE_SETRESUID)
7265 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7266#elif defined HAVE_SETREUID
7267 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7268#elif defined HAVE_SETEUID
7269 if (seteuid(uid) < 0) rb_sys_fail(0);
7270#elif defined HAVE_SETUID
7271 if (uid == getuid()) {
7272 if (setuid(uid) < 0) rb_sys_fail(0);
7283#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7297 proc_seteuid(OBJ2UID(euid));
7301#define proc_seteuid_m rb_f_notimplement
7305rb_seteuid_core(rb_uid_t euid)
7307#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7313#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7317#if defined(HAVE_SETRESUID)
7319 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7320 SAVED_USER_ID = euid;
7323 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7325#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7326 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7328 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7329 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7330 SAVED_USER_ID = euid;
7332#elif defined HAVE_SETEUID
7333 if (seteuid(euid) < 0) rb_sys_fail(0);
7334#elif defined HAVE_SETUID
7335 if (geteuid() == 0) rb_sys_fail(0);
7336 if (setuid(euid) < 0) rb_sys_fail(0);
7361 rb_seteuid_core(OBJ2UID(
id));
7380proc_getegid(
VALUE obj)
7382 rb_gid_t egid = getegid();
7387#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7400#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7406#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7407 gid = OBJ2GID(egid);
7410#if defined(HAVE_SETRESGID)
7411 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7412#elif defined HAVE_SETREGID
7413 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7414#elif defined HAVE_SETEGID
7415 if (setegid(gid) < 0) rb_sys_fail(0);
7416#elif defined HAVE_SETGID
7417 if (gid == getgid()) {
7418 if (setgid(gid) < 0) rb_sys_fail(0);
7430#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7431#define proc_setegid_m proc_setegid
7433#define proc_setegid_m rb_f_notimplement
7437rb_setegid_core(rb_gid_t egid)
7439#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7445#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7449#if defined(HAVE_SETRESGID)
7451 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7452 SAVED_GROUP_ID = egid;
7455 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7457#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7458 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7460 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7461 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7462 SAVED_GROUP_ID = egid;
7464#elif defined HAVE_SETEGID
7465 if (setegid(egid) < 0) rb_sys_fail(0);
7466#elif defined HAVE_SETGID
7467 if (geteuid() == 0 ) rb_sys_fail(0);
7468 if (setgid(egid) < 0) rb_sys_fail(0);
7493 rb_setegid_core(OBJ2GID(
id));
7508p_uid_exchangeable(
VALUE _)
7510#if defined(HAVE_SETRESUID)
7512#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7533p_uid_exchange(
VALUE obj)
7536#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7543#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7547#if defined(HAVE_SETRESUID)
7548 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7549 SAVED_USER_ID = uid;
7550#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7551 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7552 SAVED_USER_ID = uid;
7570p_gid_exchangeable(
VALUE _)
7572#if defined(HAVE_SETRESGID)
7574#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7595p_gid_exchange(
VALUE obj)
7598#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7605#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7609#if defined(HAVE_SETRESGID)
7610 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7611 SAVED_GROUP_ID = gid;
7612#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7613 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7614 SAVED_GROUP_ID = gid;
7633p_uid_have_saved_id(
VALUE _)
7635#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7643#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7645p_uid_sw_ensure(
VALUE i)
7647 rb_uid_t
id = (rb_uid_t)i;
7648 under_uid_switch = 0;
7649 id = rb_seteuid_core(
id);
7668p_uid_switch(
VALUE obj)
7680 under_uid_switch = 1;
7687 else if (euid != SAVED_USER_ID) {
7688 proc_seteuid(SAVED_USER_ID);
7690 under_uid_switch = 1;
7705p_uid_sw_ensure(
VALUE obj)
7707 under_uid_switch = 0;
7708 return p_uid_exchange(obj);
7712p_uid_switch(
VALUE obj)
7724 p_uid_exchange(obj);
7726 under_uid_switch = 1;
7748p_gid_have_saved_id(
VALUE _)
7750#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7757#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7759p_gid_sw_ensure(
VALUE i)
7761 rb_gid_t
id = (rb_gid_t)i;
7762 under_gid_switch = 0;
7763 id = rb_setegid_core(
id);
7782p_gid_switch(
VALUE obj)
7794 under_gid_switch = 1;
7801 else if (egid != SAVED_GROUP_ID) {
7802 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7804 under_gid_switch = 1;
7819p_gid_sw_ensure(
VALUE obj)
7821 under_gid_switch = 0;
7822 return p_gid_exchange(obj);
7826p_gid_switch(
VALUE obj)
7838 p_gid_exchange(obj);
7840 under_gid_switch = 1;
7850#if defined(HAVE_TIMES)
7854#ifdef HAVE__SC_CLK_TCK
7855 return sysconf(_SC_CLK_TCK);
7856#elif defined CLK_TCK
7879rb_proc_times(
VALUE obj)
7881 VALUE utime, stime, cutime, cstime, ret;
7882#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7883 struct rusage usage_s, usage_c;
7885 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7886 rb_sys_fail(
"getrusage");
7887 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7888 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7889 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7890 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7892 const double hertz = (double)get_clk_tck();
7896 utime =
DBL2NUM(buf.tms_utime / hertz);
7897 stime =
DBL2NUM(buf.tms_stime / hertz);
7898 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7899 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7901 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7909#define rb_proc_times rb_f_notimplement
7912#ifdef HAVE_LONG_LONG
7914#define TIMETICK_INT_MIN LLONG_MIN
7915#define TIMETICK_INT_MAX LLONG_MAX
7916#define TIMETICK_INT2NUM(v) LL2NUM(v)
7917#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7919typedef long timetick_int_t;
7920#define TIMETICK_INT_MIN LONG_MIN
7921#define TIMETICK_INT_MAX LONG_MAX
7922#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7923#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7926CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7927static timetick_int_t
7928gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7948reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7950 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7958reduce_factors(timetick_int_t *numerators,
int num_numerators,
7959 timetick_int_t *denominators,
int num_denominators)
7962 for (i = 0; i < num_numerators; i++) {
7963 if (numerators[i] == 1)
7965 for (j = 0; j < num_denominators; j++) {
7966 if (denominators[j] == 1)
7968 reduce_fraction(&numerators[i], &denominators[j]);
7974 timetick_int_t giga_count;
7979timetick2dblnum(
struct timetick *ttp,
7980 timetick_int_t *numerators,
int num_numerators,
7981 timetick_int_t *denominators,
int num_denominators)
7986 reduce_factors(numerators, num_numerators,
7987 denominators, num_denominators);
7989 d = ttp->giga_count * 1e9 + ttp->count;
7991 for (i = 0; i < num_numerators; i++)
7993 for (i = 0; i < num_denominators; i++)
7994 d /= denominators[i];
8000timetick2dblnum_reciprocal(
struct timetick *ttp,
8001 timetick_int_t *numerators,
int num_numerators,
8002 timetick_int_t *denominators,
int num_denominators)
8007 reduce_factors(numerators, num_numerators,
8008 denominators, num_denominators);
8011 for (i = 0; i < num_denominators; i++)
8012 d *= denominators[i];
8013 for (i = 0; i < num_numerators; i++)
8015 d /= ttp->giga_count * 1e9 + ttp->count;
8020#define NDIV(x,y) (-(-((x)+1)/(y))-1)
8021#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
8024timetick2integer(
struct timetick *ttp,
8025 timetick_int_t *numerators,
int num_numerators,
8026 timetick_int_t *denominators,
int num_denominators)
8031 reduce_factors(numerators, num_numerators,
8032 denominators, num_denominators);
8034 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
8035 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
8036 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
8037 for (i = 0; i < num_numerators; i++) {
8038 timetick_int_t factor = numerators[i];
8039 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
8043 for (i = 0; i < num_denominators; i++) {
8044 t = DIV(t, denominators[i]);
8046 return TIMETICK_INT2NUM(t);
8050 v = TIMETICK_INT2NUM(ttp->giga_count);
8053 for (i = 0; i < num_numerators; i++) {
8054 timetick_int_t factor = numerators[i];
8057 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
8059 for (i = 0; i < num_denominators; i++) {
8060 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
8066make_clock_result(
struct timetick *ttp,
8067 timetick_int_t *numerators,
int num_numerators,
8068 timetick_int_t *denominators,
int num_denominators,
8071 if (unit ==
ID2SYM(id_nanosecond)) {
8072 numerators[num_numerators++] = 1000000000;
8073 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8075 else if (unit ==
ID2SYM(id_microsecond)) {
8076 numerators[num_numerators++] = 1000000;
8077 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8079 else if (unit ==
ID2SYM(id_millisecond)) {
8080 numerators[num_numerators++] = 1000;
8081 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8083 else if (unit ==
ID2SYM(id_second)) {
8084 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8086 else if (unit ==
ID2SYM(id_float_microsecond)) {
8087 numerators[num_numerators++] = 1000000;
8088 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8090 else if (unit ==
ID2SYM(id_float_millisecond)) {
8091 numerators[num_numerators++] = 1000;
8092 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8094 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
8095 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8098 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
8102static const mach_timebase_info_data_t *
8103get_mach_timebase_info(
void)
8105 static mach_timebase_info_data_t sTimebaseInfo;
8107 if ( sTimebaseInfo.denom == 0 ) {
8108 (void) mach_timebase_info(&sTimebaseInfo);
8111 return &sTimebaseInfo;
8115ruby_real_ms_time(
void)
8117 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8118 uint64_t t = mach_absolute_time();
8119 return (
double)t * info->numer / info->denom / 1e6;
8123#if defined(NUM2CLOCKID)
8124# define NUMERIC_CLOCKID 1
8126# define NUMERIC_CLOCKID 0
8127# define NUM2CLOCKID(x) 0
8130#define clock_failed(name, err, arg) do { \
8131 int clock_error = (err); \
8132 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8305 timetick_int_t numerators[2];
8306 timetick_int_t denominators[2];
8307 int num_numerators = 0;
8308 int num_denominators = 0;
8310 VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] :
Qnil;
8311 VALUE clk_id = argv[0];
8312#ifdef HAVE_CLOCK_GETTIME
8317#ifdef CLOCK_REALTIME
8318 if (clk_id == RUBY_CLOCK_REALTIME) {
8324#ifdef CLOCK_MONOTONIC
8325 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8326 c = CLOCK_MONOTONIC;
8331#ifdef CLOCK_PROCESS_CPUTIME_ID
8332 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8333 c = CLOCK_PROCESS_CPUTIME_ID;
8338#ifdef CLOCK_THREAD_CPUTIME_ID
8339 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8340 c = CLOCK_THREAD_CPUTIME_ID;
8348#ifdef HAVE_GETTIMEOFDAY
8353#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8354 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8356 ret = gettimeofday(&tv, 0);
8358 rb_sys_fail(
"gettimeofday");
8359 tt.giga_count = tv.tv_sec;
8360 tt.count = (int32_t)tv.tv_usec * 1000;
8361 denominators[num_denominators++] = 1000000000;
8366#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8367 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8370 if (t == (time_t)-1)
8371 rb_sys_fail(
"time");
8374 denominators[num_denominators++] = 1000000000;
8379#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8380 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8381 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8384 unsigned_clock_t uc;
8386 if (c == (clock_t)-1)
8387 rb_sys_fail(
"times");
8388 uc = (unsigned_clock_t)c;
8389 tt.count = (int32_t)(uc % 1000000000);
8390 tt.giga_count = (uc / 1000000000);
8391 denominators[num_denominators++] = get_clk_tck();
8397#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8398 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8399 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8400 struct rusage usage;
8402 ret = getrusage(RUSAGE_SELF, &usage);
8404 rb_sys_fail(
"getrusage");
8405 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8406 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8407 if (1000000 <= usec) {
8411 tt.count = usec * 1000;
8412 denominators[num_denominators++] = 1000000000;
8418#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8419 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8420 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8422 unsigned_clock_t utime, stime;
8423 if (times(&buf) == (clock_t)-1)
8424 rb_sys_fail(
"times");
8425 utime = (unsigned_clock_t)buf.tms_utime;
8426 stime = (unsigned_clock_t)buf.tms_stime;
8427 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8428 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8429 if (1000000000 <= tt.count) {
8430 tt.count -= 1000000000;
8433 denominators[num_denominators++] = get_clk_tck();
8438#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8439 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8440 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8442 unsigned_clock_t uc;
8445 if (c == (clock_t)-1)
8446 rb_sys_fail(
"clock");
8447 uc = (unsigned_clock_t)c;
8448 tt.count = (int32_t)(uc % 1000000000);
8449 tt.giga_count = uc / 1000000000;
8450 denominators[num_denominators++] = CLOCKS_PER_SEC;
8455 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8456 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8457 uint64_t t = mach_absolute_time();
8458 tt.count = (int32_t)(t % 1000000000);
8459 tt.giga_count = t / 1000000000;
8460 numerators[num_numerators++] = info->numer;
8461 denominators[num_denominators++] = info->denom;
8462 denominators[num_denominators++] = 1000000000;
8467 else if (NUMERIC_CLOCKID) {
8468#if defined(HAVE_CLOCK_GETTIME)
8470 c = NUM2CLOCKID(clk_id);
8472 ret = clock_gettime(c, &ts);
8474 clock_failed(
"gettime",
errno, clk_id);
8475 tt.count = (int32_t)ts.tv_nsec;
8476 tt.giga_count = ts.tv_sec;
8477 denominators[num_denominators++] = 1000000000;
8484 clock_failed(
"gettime", EINVAL, clk_id);
8487 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8532 timetick_int_t numerators[2];
8533 timetick_int_t denominators[2];
8534 int num_numerators = 0;
8535 int num_denominators = 0;
8536#ifdef HAVE_CLOCK_GETRES
8540 VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] :
Qnil;
8541 VALUE clk_id = argv[0];
8544#ifdef CLOCK_REALTIME
8545 if (clk_id == RUBY_CLOCK_REALTIME) {
8551#ifdef CLOCK_MONOTONIC
8552 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8553 c = CLOCK_MONOTONIC;
8558#ifdef CLOCK_PROCESS_CPUTIME_ID
8559 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8560 c = CLOCK_PROCESS_CPUTIME_ID;
8565#ifdef CLOCK_THREAD_CPUTIME_ID
8566 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8567 c = CLOCK_THREAD_CPUTIME_ID;
8572#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8573 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8576 denominators[num_denominators++] = 1000000000;
8581#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8582 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8585 denominators[num_denominators++] = 1000000000;
8590#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8591 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8594 denominators[num_denominators++] = get_clk_tck();
8599#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8600 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8603 denominators[num_denominators++] = 1000000000;
8608#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8609 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8612 denominators[num_denominators++] = get_clk_tck();
8617#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8618 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8621 denominators[num_denominators++] = CLOCKS_PER_SEC;
8626#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8627 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8628 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8631 numerators[num_numerators++] = info->numer;
8632 denominators[num_denominators++] = info->denom;
8633 denominators[num_denominators++] = 1000000000;
8638 else if (NUMERIC_CLOCKID) {
8639#if defined(HAVE_CLOCK_GETRES)
8641 c = NUM2CLOCKID(clk_id);
8643 ret = clock_getres(c, &ts);
8645 clock_failed(
"getres",
errno, clk_id);
8646 tt.count = (int32_t)ts.tv_nsec;
8647 tt.giga_count = ts.tv_sec;
8648 denominators[num_denominators++] = 1000000000;
8655 clock_failed(
"getres", EINVAL, clk_id);
8658 if (unit ==
ID2SYM(id_hertz)) {
8659 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8662 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8667get_CHILD_STATUS(
ID _x,
VALUE *_y)
8673get_PROCESS_ID(
ID _x,
VALUE *_y)
8765static VALUE rb_mProcUID;
8766static VALUE rb_mProcGID;
8767static VALUE rb_mProcID_Syscall;
8799 rb_gc_prepare_heap();
9227 rb_gvar_ractor_local(
"$$");
9228 rb_gvar_ractor_local(
"$?");
9283 process_status_dump, process_status_load);
9319#ifdef HAVE_GETPRIORITY
9330#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9332 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9333#ifdef RLIM_SAVED_MAX
9335 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9337 rb_define_const(
rb_mProcess,
"RLIM_SAVED_MAX", v);
9341 rb_define_const(
rb_mProcess,
"RLIM_INFINITY", inf);
9342#ifdef RLIM_SAVED_CUR
9344 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9346 rb_define_const(
rb_mProcess,
"RLIM_SAVED_CUR", v);
9385#ifdef RLIMIT_MEMLOCK
9392#ifdef RLIMIT_MSGQUEUE
9458#ifdef RLIMIT_SIGPENDING
9493#if defined(RUBY_CLOCK_REALTIME)
9494#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9495# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9496#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9497# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9499#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9501 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
9502#elif defined(RUBY_CLOCK_REALTIME)
9503 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
9506#if defined(RUBY_CLOCK_MONOTONIC)
9507#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9508# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9510#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9512 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
9513#elif defined(RUBY_CLOCK_MONOTONIC)
9514 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", RUBY_CLOCK_MONOTONIC);
9517#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9518#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9519# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9521#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9523 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
9524#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9525 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", RUBY_CLOCK_PROCESS_CPUTIME_ID);
9528#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9530 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
9531#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9532 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", RUBY_CLOCK_THREAD_CPUTIME_ID);
9538 rb_define_const(
rb_mProcess,
"CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
9542 rb_define_const(
rb_mProcess,
"CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
9544#ifdef CLOCK_REALTIME_FAST
9546 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
9548#ifdef CLOCK_REALTIME_PRECISE
9550 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
9552#ifdef CLOCK_REALTIME_COARSE
9554 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_COARSE", CLOCKID2NUM(CLOCK_REALTIME_COARSE));
9556#ifdef CLOCK_REALTIME_ALARM
9558 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_ALARM", CLOCKID2NUM(CLOCK_REALTIME_ALARM));
9560#ifdef CLOCK_MONOTONIC_FAST
9562 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
9564#ifdef CLOCK_MONOTONIC_PRECISE
9566 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
9568#ifdef CLOCK_MONOTONIC_RAW
9570 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
9572#ifdef CLOCK_MONOTONIC_RAW_APPROX
9574 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW_APPROX", CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX));
9576#ifdef CLOCK_MONOTONIC_COARSE
9578 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_COARSE", CLOCKID2NUM(CLOCK_MONOTONIC_COARSE));
9580#ifdef CLOCK_BOOTTIME
9582 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME", CLOCKID2NUM(CLOCK_BOOTTIME));
9584#ifdef CLOCK_BOOTTIME_ALARM
9586 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME_ALARM", CLOCKID2NUM(CLOCK_BOOTTIME_ALARM));
9590 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
9592#ifdef CLOCK_UPTIME_FAST
9594 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
9596#ifdef CLOCK_UPTIME_PRECISE
9598 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
9600#ifdef CLOCK_UPTIME_RAW
9602 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW", CLOCKID2NUM(CLOCK_UPTIME_RAW));
9604#ifdef CLOCK_UPTIME_RAW_APPROX
9606 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW_APPROX", CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX));
9610 rb_define_const(
rb_mProcess,
"CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
9614 rb_define_const(
rb_mProcess,
"CLOCK_TAI", CLOCKID2NUM(CLOCK_TAI));
9620#if defined(HAVE_TIMES) || defined(_WIN32)
9634 SAVED_USER_ID = geteuid();
9635 SAVED_GROUP_ID = getegid();
9658#ifdef p_uid_from_name
9661#ifdef p_gid_from_name
9692#define define_id(name) id_##name = rb_intern_const(#name)
9705 define_id(new_pgroup);
9707 define_id(unsetenv_others);
9710 define_id(close_others);
9711 define_id(nanosecond);
9712 define_id(microsecond);
9713 define_id(millisecond);
9715 define_id(float_microsecond);
9716 define_id(float_millisecond);
9717 define_id(float_second);
9718 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9719 define_id(TIME_BASED_CLOCK_REALTIME);
9720#ifdef CLOCK_REALTIME
9721 define_id(CLOCK_REALTIME);
9723#ifdef CLOCK_MONOTONIC
9724 define_id(CLOCK_MONOTONIC);
9726#ifdef CLOCK_PROCESS_CPUTIME_ID
9727 define_id(CLOCK_PROCESS_CPUTIME_ID);
9729#ifdef CLOCK_THREAD_CPUTIME_ID
9730 define_id(CLOCK_THREAD_CPUTIME_ID);
9733 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9734 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9737 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9739 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9741 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
9744#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.