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/numeric.h"
107#include "internal/object.h"
108#include "internal/process.h"
109#include "internal/thread.h"
110#include "internal/variable.h"
111#include "internal/warnings.h"
123#define open rb_w32_uopen
126#if defined(HAVE_TIMES) || defined(_WIN32)
133static VALUE rb_cProcessTms;
137#define WIFEXITED(w) (((w) & 0xff) == 0)
140#define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
143#define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
146#define WEXITSTATUS(w) (((w) >> 8) & 0xff)
149#define WTERMSIG(w) ((w) & 0x7f)
152#define WSTOPSIG WEXITSTATUS
155#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__)
156#define HAVE_44BSD_SETUID 1
157#define HAVE_44BSD_SETGID 1
165#ifdef BROKEN_SETREUID
166#define setreuid ruby_setreuid
167int setreuid(rb_uid_t ruid, rb_uid_t euid);
169#ifdef BROKEN_SETREGID
170#define setregid ruby_setregid
171int setregid(rb_gid_t rgid, rb_gid_t egid);
174#if defined(HAVE_44BSD_SETUID) || defined(__APPLE__)
175#if !defined(USE_SETREUID) && !defined(BROKEN_SETREUID)
176#define OBSOLETE_SETREUID 1
178#if !defined(USE_SETREGID) && !defined(BROKEN_SETREGID)
179#define OBSOLETE_SETREGID 1
183static void check_uid_switch(
void);
184static void check_gid_switch(
void);
185static int exec_async_signal_safe(
const struct rb_execarg *,
char *,
size_t);
187VALUE rb_envtbl(
void);
188VALUE rb_env_to_hash(
void);
191#define p_uid_from_name p_uid_from_name
192#define p_gid_from_name p_gid_from_name
195#if defined(HAVE_UNISTD_H)
196# if defined(HAVE_GETLOGIN_R)
197# define USE_GETLOGIN_R 1
198# define GETLOGIN_R_SIZE_DEFAULT 0x100
199# define GETLOGIN_R_SIZE_LIMIT 0x1000
200# if defined(_SC_LOGIN_NAME_MAX)
201# define GETLOGIN_R_SIZE_INIT sysconf(_SC_LOGIN_NAME_MAX)
203# define GETLOGIN_R_SIZE_INIT GETLOGIN_R_SIZE_DEFAULT
205# elif defined(HAVE_GETLOGIN)
206# define USE_GETLOGIN 1
210#if defined(HAVE_PWD_H)
211# if defined(HAVE_GETPWUID_R)
212# define USE_GETPWUID_R 1
213# elif defined(HAVE_GETPWUID)
214# define USE_GETPWUID 1
216# if defined(HAVE_GETPWNAM_R)
217# define USE_GETPWNAM_R 1
218# elif defined(HAVE_GETPWNAM)
219# define USE_GETPWNAM 1
221# if defined(HAVE_GETPWNAM_R) || defined(HAVE_GETPWUID_R)
222# define GETPW_R_SIZE_DEFAULT 0x1000
223# define GETPW_R_SIZE_LIMIT 0x10000
224# if defined(_SC_GETPW_R_SIZE_MAX)
225# define GETPW_R_SIZE_INIT sysconf(_SC_GETPW_R_SIZE_MAX)
227# define GETPW_R_SIZE_INIT GETPW_R_SIZE_DEFAULT
230# ifdef USE_GETPWNAM_R
231# define PREPARE_GETPWNAM \
233# define FINISH_GETPWNAM \
234 (getpw_buf ? (void)rb_str_resize(getpw_buf, 0) : (void)0)
235# define OBJ2UID1(id) obj2uid((id), &getpw_buf)
236# define OBJ2UID(id) obj2uid0(id)
237static rb_uid_t obj2uid(
VALUE id,
VALUE *getpw_buf);
238static inline rb_uid_t
248# define PREPARE_GETPWNAM
249# define FINISH_GETPWNAM
250# define OBJ2UID1(id) obj2uid((id))
251# define OBJ2UID(id) obj2uid((id))
252static rb_uid_t obj2uid(
VALUE id);
255# define PREPARE_GETPWNAM
256# define FINISH_GETPWNAM
257# define OBJ2UID1(id) NUM2UIDT(id)
258# define OBJ2UID(id) NUM2UIDT(id)
259# ifdef p_uid_from_name
260# undef p_uid_from_name
261# define p_uid_from_name rb_f_notimplement
265#if defined(HAVE_GRP_H)
266# if defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
267# define USE_GETGRNAM_R
268# define GETGR_R_SIZE_INIT sysconf(_SC_GETGR_R_SIZE_MAX)
269# define GETGR_R_SIZE_DEFAULT 0x1000
270# define GETGR_R_SIZE_LIMIT 0x10000
272# ifdef USE_GETGRNAM_R
273# define PREPARE_GETGRNAM \
275# define FINISH_GETGRNAM \
276 (getgr_buf ? (void)rb_str_resize(getgr_buf, 0) : (void)0)
277# define OBJ2GID1(id) obj2gid((id), &getgr_buf)
278# define OBJ2GID(id) obj2gid0(id)
279static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
280static inline rb_gid_t
289static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
291# define PREPARE_GETGRNAM
292# define FINISH_GETGRNAM
293# define OBJ2GID1(id) obj2gid((id))
294# define OBJ2GID(id) obj2gid((id))
295static rb_gid_t obj2gid(
VALUE id);
298# define PREPARE_GETGRNAM
299# define FINISH_GETGRNAM
300# define OBJ2GID1(id) NUM2GIDT(id)
301# define OBJ2GID(id) NUM2GIDT(id)
302# ifdef p_gid_from_name
303# undef p_gid_from_name
304# define p_gid_from_name rb_f_notimplement
308#if SIZEOF_CLOCK_T == SIZEOF_INT
309typedef unsigned int unsigned_clock_t;
310#elif SIZEOF_CLOCK_T == SIZEOF_LONG
311typedef unsigned long unsigned_clock_t;
312#elif defined(HAVE_LONG_LONG) && SIZEOF_CLOCK_T == SIZEOF_LONG_LONG
313typedef unsigned LONG_LONG unsigned_clock_t;
316typedef void (*sig_t) (int);
319#define id_exception idException
320static ID id_in, id_out, id_err, id_pid, id_uid, id_gid;
321static ID id_close, id_child;
326static ID id_new_pgroup;
328static ID id_unsetenv_others, id_chdir, id_umask, id_close_others;
329static ID id_nanosecond, id_microsecond, id_millisecond, id_second;
330static ID id_float_microsecond, id_float_millisecond, id_float_second;
331static ID id_GETTIMEOFDAY_BASED_CLOCK_REALTIME, id_TIME_BASED_CLOCK_REALTIME;
333static ID id_CLOCK_REALTIME;
334# define RUBY_CLOCK_REALTIME ID2SYM(id_CLOCK_REALTIME)
336#ifdef CLOCK_MONOTONIC
337static ID id_CLOCK_MONOTONIC;
338# define RUBY_CLOCK_MONOTONIC ID2SYM(id_CLOCK_MONOTONIC)
340#ifdef CLOCK_PROCESS_CPUTIME_ID
341static ID id_CLOCK_PROCESS_CPUTIME_ID;
342# define RUBY_CLOCK_PROCESS_CPUTIME_ID ID2SYM(id_CLOCK_PROCESS_CPUTIME_ID)
344#ifdef CLOCK_THREAD_CPUTIME_ID
345static ID id_CLOCK_THREAD_CPUTIME_ID;
346# define RUBY_CLOCK_THREAD_CPUTIME_ID ID2SYM(id_CLOCK_THREAD_CPUTIME_ID)
349static ID id_TIMES_BASED_CLOCK_MONOTONIC;
350static ID id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID;
353static ID id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID;
355static ID id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID;
357static ID id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC;
358# define RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ID2SYM(id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
363#if defined(__sun) && !defined(_XPG7)
364#define execv(path, argv) (rb_async_bug_errno("unreachable: async-signal-unsafe execv() is called", 0))
365#define execl(path, arg0, arg1, arg2, term) do { extern char **environ; execle((path), (arg0), (arg1), (arg2), (term), (environ)); } while (0)
366#define ALWAYS_NEED_ENVP 1
368#define ALWAYS_NEED_ENVP 0
372assert_close_on_exec(
int fd)
375#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(FD_CLOEXEC)
376 int flags = fcntl(fd, F_GETFD);
378 static const char m[] =
"reserved FD closed unexpectedly?\n";
379 (void)!write(2, m,
sizeof(m) - 1);
382 if (flags & FD_CLOEXEC)
return;
383 rb_bug(
"reserved FD did not have close-on-exec set");
385 rb_bug(
"reserved FD without close-on-exec support");
391close_unless_reserved(
int fd)
394 assert_close_on_exec(fd);
401#if defined(DEBUG_REDIRECT)
404ttyprintf(
const char *fmt, ...)
410 tty = fopen(
"con",
"w");
412 tty = fopen(
"/dev/tty",
"w");
418 vfprintf(tty, fmt, ap);
425redirect_dup(
int oldfd)
429 ttyprintf(
"dup(%d) => %d\n", oldfd, ret);
434redirect_dup2(
int oldfd,
int newfd)
437 ret = dup2(oldfd, newfd);
438 ttyprintf(
"dup2(%d, %d) => %d\n", oldfd, newfd, ret);
443redirect_cloexec_dup(
int oldfd)
447 ttyprintf(
"cloexec_dup(%d) => %d\n", oldfd, ret);
452redirect_cloexec_dup2(
int oldfd,
int newfd)
456 ttyprintf(
"cloexec_dup2(%d, %d) => %d\n", oldfd, newfd, ret);
461redirect_close(
int fd)
464 ret = close_unless_reserved(fd);
465 ttyprintf(
"close(%d) => %d\n", fd, ret);
470parent_redirect_open(
const char *pathname,
int flags, mode_t perm)
474 ttyprintf(
"parent_open(\"%s\", 0x%x, 0%o) => %d\n", pathname, flags, perm, ret);
479parent_redirect_close(
int fd)
482 ret = close_unless_reserved(fd);
483 ttyprintf(
"parent_close(%d) => %d\n", fd, ret);
488#define redirect_dup(oldfd) dup(oldfd)
489#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
490#define redirect_cloexec_dup(oldfd) rb_cloexec_dup(oldfd)
491#define redirect_cloexec_dup2(oldfd, newfd) rb_cloexec_dup2((oldfd), (newfd))
492#define redirect_close(fd) close_unless_reserved(fd)
493#define parent_redirect_open(pathname, flags, perm) rb_cloexec_open((pathname), (flags), (perm))
494#define parent_redirect_close(fd) close_unless_reserved(fd)
596static VALUE rb_cProcessStatus;
605 .wrap_struct_name =
"Process::Status",
610 .flags = RUBY_TYPED_FREE_IMMEDIATELY,
614rb_process_status_allocate(
VALUE klass)
624 return GET_THREAD()->last_status;
643proc_s_last_status(
VALUE mod)
649rb_process_status_new(rb_pid_t pid,
int status,
int error)
651 VALUE last_status = rb_process_status_allocate(rb_cProcessStatus);
655 data->status = status;
663process_status_dump(
VALUE status)
675process_status_load(
VALUE real_obj,
VALUE load_obj)
688 GET_THREAD()->last_status = rb_process_status_new(pid, status, 0);
692rb_last_status_clear(
void)
694 GET_THREAD()->last_status =
Qnil;
726 int status = pst_status(self);
730#define PST2INT(st) pst_status(st)
746 rb_pid_t pid = pst_pid(self);
750static VALUE pst_message_status(
VALUE str,
int status);
753pst_message(
VALUE str, rb_pid_t pid,
int status)
756 pst_message_status(str, status);
760pst_message_status(
VALUE str,
int status)
762 if (WIFSTOPPED(status)) {
763 int stopsig = WSTOPSIG(status);
766 rb_str_catf(str,
" stopped SIG%s (signal %d)", signame, stopsig);
772 if (WIFSIGNALED(status)) {
773 int termsig = WTERMSIG(status);
776 rb_str_catf(str,
" SIG%s (signal %d)", signame, termsig);
782 if (WIFEXITED(status)) {
786 if (WCOREDUMP(status)) {
813 status = PST2INT(st);
816 pst_message(str, pid, status);
843 status = PST2INT(st);
846 pst_message(str, pid, status);
863 if (st1 == st2)
return Qtrue;
864 return rb_equal(pst_to_i(st1), st2);
883 int status = PST2INT(st1) &
NUM2INT(st2);
904 int status = PST2INT(st1) >>
NUM2INT(st2);
920pst_wifstopped(
VALUE st)
922 int status = PST2INT(st);
924 return RBOOL(WIFSTOPPED(status));
937pst_wstopsig(
VALUE st)
939 int status = PST2INT(st);
941 if (WIFSTOPPED(status))
942 return INT2NUM(WSTOPSIG(status));
956pst_wifsignaled(
VALUE st)
958 int status = PST2INT(st);
960 return RBOOL(WIFSIGNALED(status));
974pst_wtermsig(
VALUE st)
976 int status = PST2INT(st);
978 if (WIFSIGNALED(status))
979 return INT2NUM(WTERMSIG(status));
994pst_wifexited(
VALUE st)
996 int status = PST2INT(st);
998 return RBOOL(WIFEXITED(status));
1021pst_wexitstatus(
VALUE st)
1023 int status = PST2INT(st);
1025 if (WIFEXITED(status))
1026 return INT2NUM(WEXITSTATUS(status));
1040pst_success_p(
VALUE st)
1042 int status = PST2INT(st);
1044 if (!WIFEXITED(status))
1046 return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
1059pst_wcoredump(
VALUE st)
1062 int status = PST2INT(st);
1064 return RBOOL(WCOREDUMP(status));
1071do_waitpid(rb_pid_t pid,
int *st,
int flags)
1073#if defined HAVE_WAITPID
1074 return waitpid(pid, st, flags);
1075#elif defined HAVE_WAIT4
1076 return wait4(pid, st, flags, NULL);
1078# error waitpid or wait4 is required.
1082#define WAITPID_LOCK_ONLY ((struct waitpid_state *)-1)
1085 struct ccan_list_node wnode;
1087 rb_nativethread_cond_t *cond;
1096void rb_sigwait_sleep(
const rb_thread_t *,
int fd,
const rb_hrtime_t *);
1097void rb_sigwait_fd_put(
const rb_thread_t *,
int fd);
1098void rb_thread_sleep_interruptible(
void);
1105bool mjit_waitpid_finished =
false;
1106int mjit_waitpid_status = 0;
1113 rb_threadptr_interrupt(rb_ec_thread_ptr(w->ec));
1117 else if (w == &mjit_waitpid_state && w->ret) {
1118 mjit_waitpid_finished =
true;
1119 mjit_waitpid_status = w->status;
1129rb_vm_memsize_waiting_list(
struct ccan_list_head *waiting_list)
1134 ccan_list_for_each(waiting_list, waitpid, wnode) {
1147sigwait_fd_migrate_sleeper(
rb_vm_t *vm)
1151 ccan_list_for_each(&vm->waiting_pids, w, wnode) {
1152 if (waitpid_signal(w))
return;
1154 ccan_list_for_each(&vm->waiting_grps, w, wnode) {
1155 if (waitpid_signal(w))
return;
1160rb_sigwait_fd_migrate(
rb_vm_t *vm)
1163 sigwait_fd_migrate_sleeper(vm);
1168extern volatile unsigned int ruby_nocldwait;
1171waitpid_each(
rb_vm_t *vm,
struct ccan_list_head *head)
1175 ccan_list_for_each_safe(head, w, next, wnode) {
1176 rb_pid_t ret = do_waitpid(w->pid, &w->status, w->options | WNOHANG);
1179 if (ret == -1) w->errnum = errno;
1185 ccan_list_for_each_safe(&vm->waiting_pids, w_inner, next_inner, wnode) {
1186 if (w_inner->pid == ret) {
1194 ccan_list_del_init(&w->wnode);
1199# define ruby_nocldwait 0
1207 waitpid_each(vm, &vm->waiting_pids);
1208 waitpid_each(vm, &vm->waiting_grps);
1210 if (ccan_list_empty(&vm->waiting_pids) && ccan_list_empty(&vm->waiting_grps)) {
1211 while (ruby_nocldwait && do_waitpid(-1, 0, WNOHANG) > 0)
1219waitpid_state_init(
struct waitpid_state *w, rb_pid_t pid,
int options)
1223 w->options = options;
1233mjit_add_waiting_pid(
rb_vm_t *vm, rb_pid_t pid)
1235 waitpid_state_init(&mjit_waitpid_state, pid, 0);
1236 mjit_waitpid_state.ec = 0;
1237 ccan_list_add(&vm->waiting_pids, &mjit_waitpid_state.wnode);
1242waitpid_sleep(
VALUE x)
1247 rb_thread_sleep_interruptible();
1254waitpid_cleanup(
VALUE x)
1262 if (TRUE || w->ret == 0) {
1263 rb_vm_t *vm = rb_ec_vm_ptr(w->ec);
1266 ccan_list_del(&w->wnode);
1276 rb_vm_t *vm = rb_ec_vm_ptr(w->ec);
1277 int need_sleep = FALSE;
1286 if (w->pid > 0 || ccan_list_empty(&vm->waiting_pids)) {
1287 w->ret = do_waitpid(w->pid, &w->status, w->options | WNOHANG);
1291 if (w->ret == -1) w->errnum = errno;
1293 else if (w->options & WNOHANG) {
1302 ccan_list_add(w->pid > 0 ? &vm->waiting_pids : &vm->waiting_grps, &w->wnode);
1313waitpid_blocking_no_SIGCHLD(
void *x)
1317 w->ret = do_waitpid(w->pid, &w->status, w->options);
1325 if (w->options & WNOHANG) {
1326 w->ret = do_waitpid(w->pid, &w->status, w->options);
1332 }
while (w->ret < 0 && errno == EINTR && (RUBY_VM_CHECK_INTS(w->ec),1));
1339rb_process_status_wait(rb_pid_t pid,
int flags)
1342 if (!(flags & WNOHANG)) {
1345 if (!UNDEF_P(result))
return result;
1353 if (WAITPID_USE_SIGCHLD) {
1415rb_process_status_waitv(
int argc,
VALUE *argv,
VALUE _)
1430 return rb_process_status_wait(pid, flags);
1436 VALUE status = rb_process_status_wait(pid, flags);
1437 if (
NIL_P(status))
return 0;
1442 if (st) *st = data->status;
1445 errno = data->error;
1448 GET_THREAD()->last_status = status;
1455proc_wait(
int argc,
VALUE *argv)
1467 if (argc == 2 && !
NIL_P(vflags = argv[1])) {
1472 if ((pid =
rb_waitpid(pid, &status, flags)) < 0)
1476 rb_last_status_clear();
1544 return proc_wait(c, v);
1567 VALUE pid = proc_wait(argc, argv);
1600 result = rb_ary_new();
1601 rb_last_status_clear();
1616static VALUE rb_cWaiter;
1619detach_process_pid(
VALUE thread)
1625detach_process_watcher(
void *arg)
1627 rb_pid_t cpid, pid = (rb_pid_t)(
VALUE)arg;
1630 while ((cpid =
rb_waitpid(pid, &status, 0)) == 0) {
1641 RBASIC_SET_CLASS(watcher, rb_cWaiter);
1700before_exec_async_signal_safe(
void)
1705before_exec_non_async_signal_safe(
void)
1716 rb_thread_stop_timer_thread();
1719#define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
1721int rb_w32_set_nonblock2(
int fd,
int nonblock);
1728 return rb_w32_set_nonblock2(fd, 0);
1729#elif defined(F_GETFL) && defined(F_SETFL)
1730 int fl = fcntl(fd, F_GETFL);
1733 if (fl == -1)
return fl;
1734 if (fl & O_NONBLOCK) {
1736 return fcntl(fd, F_SETFL, fl);
1743stdfd_clear_nonblock(
void)
1747 for (fd = 0; fd < 3; fd++) {
1748 (void)set_blocking(fd);
1755 before_exec_non_async_signal_safe();
1756 before_exec_async_signal_safe();
1761after_exec_async_signal_safe(
void)
1766after_exec_non_async_signal_safe(
void)
1768 rb_thread_reset_timer_thread();
1769 rb_thread_start_timer_thread();
1775 after_exec_async_signal_safe();
1776 after_exec_non_async_signal_safe();
1779#if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
1781before_fork_ruby(
void)
1787after_fork_ruby(
void)
1793#if defined(HAVE_WORKING_FORK)
1796#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1798exec_with_sh(
const char *prog,
char **argv,
char **envp)
1800 *argv = (
char *)prog;
1801 *--argv = (
char *)
"sh";
1803 execve(
"/bin/sh", argv, envp);
1805 execv(
"/bin/sh", argv);
1809#define try_with_sh(err, prog, argv, envp) (void)0
1814proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1822 argv = ARGVSTR2ARGV(argv_str);
1829 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1832 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1834 execve(prog, argv, envp);
1838 try_with_sh(err, prog, argv, envp);
1845proc_exec_sh(
const char *str,
VALUE envp_str)
1850 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1858 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1859#elif defined(__CYGWIN32__)
1861 char fbuf[MAXPATHLEN];
1862 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
1865 execl(shell,
"sh",
"-c", str, (
char *) NULL);
1867 status = system(str);
1873 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1875 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1885 ret = proc_exec_sh(str,
Qfalse);
1892mark_exec_arg(
void *ptr)
1895 if (eargp->use_shell)
1896 rb_gc_mark(eargp->invoke.sh.shell_script);
1898 rb_gc_mark(eargp->invoke.cmd.command_name);
1899 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1900 rb_gc_mark(eargp->invoke.cmd.argv_str);
1901 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1903 rb_gc_mark(eargp->redirect_fds);
1904 rb_gc_mark(eargp->envp_str);
1905 rb_gc_mark(eargp->envp_buf);
1906 rb_gc_mark(eargp->dup2_tmpbuf);
1907 rb_gc_mark(eargp->rlimit_limits);
1908 rb_gc_mark(eargp->fd_dup2);
1909 rb_gc_mark(eargp->fd_close);
1910 rb_gc_mark(eargp->fd_open);
1911 rb_gc_mark(eargp->fd_dup2_child);
1912 rb_gc_mark(eargp->env_modification);
1913 rb_gc_mark(eargp->path_env);
1914 rb_gc_mark(eargp->chdir_dir);
1918memsize_exec_arg(
const void *ptr)
1926 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1930# define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1932#ifdef DEFAULT_PROCESS_ENCODING
1933# define EXPORT_STR(str) rb_str_export_to_enc((str), DEFAULT_PROCESS_ENCODING)
1934# define EXPORT_DUP(str) export_dup(str)
1936export_dup(
VALUE str)
1938 VALUE newstr = EXPORT_STR(str);
1943# define EXPORT_STR(str) (str)
1944# define EXPORT_DUP(str) rb_str_dup(str)
1947#if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1948# define USE_SPAWNV 1
1950# define USE_SPAWNV 0
1953# define P_NOWAIT _P_NOWAIT
1958#define proc_spawn_cmd_internal(argv, prog) rb_w32_uaspawn(P_NOWAIT, (prog), (argv))
1961proc_spawn_cmd_internal(
char **argv,
char *prog)
1963 char fbuf[MAXPATHLEN];
1968 prog = dln_find_exe_r(prog, 0, fbuf,
sizeof(fbuf));
1973 status = spawnv(P_NOWAIT, prog, (
const char **)argv);
1974 if (status == -1 && errno == ENOEXEC) {
1975 *argv = (
char *)prog;
1976 *--argv = (
char *)
"sh";
1977 status = spawnv(P_NOWAIT,
"/bin/sh", (
const char **)argv);
1979 if (status == -1) errno = ENOEXEC;
1993 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1994 flags = CREATE_NEW_PROCESS_GROUP;
1996 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ?
RSTRING_PTR(prog) : 0, argv, flags);
1998 pid = proc_spawn_cmd_internal(argv, prog ?
RSTRING_PTR(prog) : 0);
2005#define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
2008proc_spawn_sh(
char *str)
2010 char fbuf[MAXPATHLEN];
2013 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
2015 status = spawnl(P_NOWAIT, (shell ? shell :
"/bin/sh"),
"sh",
"-c", str, (
char*)NULL);
2025 RBASIC_CLEAR_CLASS(obj);
2030check_exec_redirect_fd(
VALUE v,
int iskey)
2041 else if (
id == id_out)
2043 else if (
id == id_err)
2062 else if (fd >= 3 && iskey) {
2077 ary = hide_obj(rb_ary_new());
2080 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
2081 rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
2087 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
2088 rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
2098 VALUE path, flags, perm;
2102 switch (
TYPE(val)) {
2105 if (
id == id_close) {
2107 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
2109 else if (
id == id_in) {
2111 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2113 else if (
id == id_out) {
2115 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2117 else if (
id == id_err) {
2119 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2129 val = check_exec_redirect_fd(val, 0);
2133 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2137 path = rb_ary_entry(val, 0);
2139 path ==
ID2SYM(id_child)) {
2140 param = check_exec_redirect_fd(rb_ary_entry(val, 1), 0);
2141 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
2145 flags = rb_ary_entry(val, 1);
2152 perm = rb_ary_entry(val, 2);
2154 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2155 flags, perm,
Qnil));
2156 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2164 key = check_exec_redirect_fd(key, 1);
2166 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2171 VALUE fd = check_exec_redirect_fd(v, 1);
2175 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2182 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2183 flags, perm,
Qnil));
2184 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2190 if (!
NIL_P(val))
goto io;
2196#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2197static int rlimit_type_by_sym(
VALUE key);
2200rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
2202 VALUE ary = eargp->rlimit_limits;
2203 VALUE tmp, softlim, hardlim;
2204 if (eargp->rlimit_limits ==
Qfalse)
2205 ary = eargp->rlimit_limits = hide_obj(rb_ary_new());
2207 ary = eargp->rlimit_limits;
2208 tmp = rb_check_array_type(val);
2211 softlim = hardlim =
rb_to_int(rb_ary_entry(tmp, 0));
2213 softlim =
rb_to_int(rb_ary_entry(tmp, 0));
2214 hardlim =
rb_to_int(rb_ary_entry(tmp, 1));
2224 rb_ary_push(ary, tmp);
2228#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
2232 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2236 switch (
TYPE(key)) {
2238#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2240 int rtype = rlimit_type_by_sym(key);
2242 rb_execarg_addopt_rlimit(eargp, rtype, val);
2250 if (
id == id_pgroup) {
2252 if (eargp->pgroup_given) {
2257 else if (val ==
Qtrue)
2265 eargp->pgroup_given = 1;
2266 eargp->pgroup_pgid = pgroup;
2271 if (
id == id_new_pgroup) {
2272 if (eargp->new_pgroup_given) {
2275 eargp->new_pgroup_given = 1;
2276 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2280 if (
id == id_unsetenv_others) {
2281 if (eargp->unsetenv_others_given) {
2284 eargp->unsetenv_others_given = 1;
2285 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2287 else if (
id == id_chdir) {
2288 if (eargp->chdir_given) {
2292 val = rb_str_encode_ospath(val);
2293 eargp->chdir_given = 1;
2294 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2296 else if (
id == id_umask) {
2298 if (eargp->umask_given) {
2301 eargp->umask_given = 1;
2302 eargp->umask_mask = cmask;
2304 else if (
id == id_close_others) {
2305 if (eargp->close_others_given) {
2308 eargp->close_others_given = 1;
2309 eargp->close_others_do = TO_BOOL(val,
"close_others");
2311 else if (
id == id_in) {
2315 else if (
id == id_out) {
2319 else if (
id == id_err) {
2323 else if (
id == id_uid) {
2325 if (eargp->uid_given) {
2330 eargp->uid = OBJ2UID(val);
2331 eargp->uid_given = 1;
2335 "uid option is unimplemented on this machine");
2338 else if (
id == id_gid) {
2340 if (eargp->gid_given) {
2345 eargp->gid = OBJ2GID(val);
2346 eargp->gid_given = 1;
2350 "gid option is unimplemented on this machine");
2353 else if (
id == id_exception) {
2354 if (eargp->exception_given) {
2357 eargp->exception_given = 1;
2358 eargp->exception = TO_BOOL(val,
"exception");
2369 check_exec_redirect(key, val, eargp);
2381check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2386 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2396check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2401 VALUE execarg_obj = args[0];
2402 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2403 VALUE nonopts = args[1];
2404 if (
NIL_P(nonopts)) args[1] = nonopts = rb_hash_new();
2405 rb_hash_aset(nonopts, key, val);
2422 if (ary == eargp->fd_dup2)
2424 else if (ary == eargp->fd_dup2_child)
2430 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2443 VALUE h = rb_hash_new();
2448 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2449 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2450 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2452 if (eargp->fd_dup2_child) {
2453 ary = eargp->fd_dup2_child;
2463 val = rb_hash_lookup(h, val);
2470 if (oldfd != lastfd) {
2472 rb_ary_store(elt, 1,
INT2FIX(lastfd));
2475 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2476 rb_hash_aset(h, val,
INT2FIX(lastfd));
2483 eargp->close_others_maxhint = maxhint;
2488rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2492 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2496rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2501 args[0] = execarg_obj;
2503 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2507#ifdef ENV_IGNORECASE
2508#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2510#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2514check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2529 key = EXPORT_STR(key);
2530 if (!
NIL_P(val)) val = EXPORT_STR(val);
2535 rb_ary_push(env, hide_obj(rb_assoc_new(key, val)));
2545 env[0] = hide_obj(rb_ary_new());
2547 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2554rb_check_argv(
int argc,
VALUE *argv)
2562 tmp = rb_check_array_type(argv[0]);
2573 for (i = 0; i < argc; i++) {
2582check_hash(
VALUE obj)
2592 return rb_check_hash_type(obj);
2596rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2601 hash = check_hash((*argv_p)[*argc_p-1]);
2603 *opthash_ret = hash;
2609 hash = check_hash((*argv_p)[0]);
2616 prog = rb_check_argv(*argc_p, *argv_p);
2618 prog = (*argv_p)[0];
2619 if (accept_shell && *argc_p == 1) {
2634compare_posix_sh(
const void *key,
const void *el)
2637 int ret = strncmp(word->ptr, el, word->len);
2638 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2646 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2647 char fbuf[MAXPATHLEN];
2651 if (!
NIL_P(opthash)) {
2652 rb_check_exec_options(opthash, execarg_obj);
2655 env = rb_check_exec_env(env, &eargp->path_env);
2656 eargp->env_modification = env;
2659 prog = EXPORT_STR(prog);
2660 eargp->use_shell = argc == 0;
2661 if (eargp->use_shell)
2662 eargp->invoke.sh.shell_script = prog;
2664 eargp->invoke.cmd.command_name = prog;
2667 if (eargp->use_shell) {
2668 static const char posix_sh_cmds[][9] = {
2727 if (*p ==
' ' || *p ==
'\t') {
2728 if (first.ptr && !first.len) first.len = p - first.ptr;
2731 if (!first.ptr) first.ptr = p;
2733 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2739 else if (*p ==
'/') {
2746 if (!has_meta && first.ptr) {
2747 if (!first.len) first.len = p - first.ptr;
2748 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2749 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2754 eargp->use_shell = 0;
2756 if (!eargp->use_shell) {
2761 while (*p ==
' ' || *p ==
'\t')
2765 while (*p && *p !=
' ' && *p !=
'\t')
2771 eargp->invoke.cmd.argv_buf = argv_buf;
2772 eargp->invoke.cmd.command_name =
2774 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2779 if (!eargp->use_shell) {
2780 const char *abspath;
2781 const char *path_env = 0;
2783 abspath = dln_find_exe_r(
RSTRING_PTR(eargp->invoke.cmd.command_name),
2784 path_env, fbuf,
sizeof(fbuf));
2788 eargp->invoke.cmd.command_abspath =
Qnil;
2791 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2796 for (i = 0; i < argc; i++) {
2797 VALUE arg = argv[i];
2799#ifdef DEFAULT_PROCESS_ENCODING
2800 arg = EXPORT_STR(arg);
2805 eargp->invoke.cmd.argv_buf = argv_buf;
2808 if (!eargp->use_shell) {
2809 const char *p, *ep, *null=NULL;
2820 eargp->invoke.cmd.argv_str =
2821 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(argv_str);
2827rb_execarg_get(
VALUE execarg_obj)
2835rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2837 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2843 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2844 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2846 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2852rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2857 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2858 if (!allow_exc_opt && eargp->exception_given) {
2865rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2867 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2868 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2869 eargp->env_modification = env;
2873fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2888static long run_exec_dup2_tmpbuf_size(
long n);
2903 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2909rb_execarg_allocate_dup2_tmpbuf(
struct rb_execarg *eargp,
long len)
2911 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2912 rb_imemo_tmpbuf_set_ptr(tmpbuf, ruby_xmalloc(run_exec_dup2_tmpbuf_size(len)));
2913 eargp->dup2_tmpbuf = tmpbuf;
2917rb_execarg_parent_start1(
VALUE execarg_obj)
2919 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2920 int unsetenv_others;
2924 ary = eargp->fd_open;
2939 open_data.fname = vpath;
2940 open_data.oflags = flags;
2941 open_data.perm = perm;
2943 open_data.err = EINTR;
2945 if (open_data.ret == -1) {
2946 if (open_data.err == EINTR) {
2952 fd2 = open_data.ret;
2964 eargp->redirect_fds = check_exec_fds(eargp);
2966 ary = eargp->fd_dup2;
2968 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2971 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2972 envopts = eargp->env_modification;
2973 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2974 VALUE envtbl, envp_str, envp_buf;
2976 if (unsetenv_others) {
2977 envtbl = rb_hash_new();
2980 envtbl = rb_env_to_hash();
2984 st_table *stenv = RHASH_TBL_RAW(envtbl);
2991 st_data_t stkey = (st_data_t)key;
2992 st_delete(stenv, &stkey, NULL);
2995 st_insert(stenv, (st_data_t)key, (st_data_t)val);
3003 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
3015 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(envp_str);
3016 eargp->envp_buf = envp_buf;
3032rb_execarg_parent_start(
VALUE execarg_obj)
3035 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
3037 rb_execarg_parent_end(execarg_obj);
3043execarg_parent_end(
VALUE execarg_obj)
3045 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
3049 ary = eargp->fd_open;
3060 parent_redirect_close(fd2);
3071rb_execarg_parent_end(
VALUE execarg_obj)
3073 execarg_parent_end(execarg_obj);
3078rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
3080 if (!errmsg || !*errmsg)
return;
3081 if (strcmp(errmsg,
"chdir") == 0) {
3089rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
3091 if (!errmsg || !*errmsg)
return;
3092 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
3100 VALUE execarg_obj, fail_str;
3102#define CHILD_ERRMSG_BUFLEN 80
3103 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
3106 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
3107 eargp = rb_execarg_get(execarg_obj);
3108 if (mjit_enabled) mjit_finish(
false);
3111 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
3113 execarg_parent_end(execarg_obj);
3118 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
3120 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
3123 rb_exec_fail(eargp, err, errmsg);
3213#define ERRMSG(str) do { if (errmsg && 0 < errmsg_buflen) strlcpy(errmsg, (str), errmsg_buflen); } while (0)
3214#define ERRMSG1(str, a) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a)); } while (0)
3215#define ERRMSG2(str, a, b) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a), (b)); } while (0)
3217static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3218static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3219static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3222save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3225 VALUE newary, redirection;
3226 int save_fd = redirect_cloexec_dup(fd), cloexec;
3227 if (save_fd == -1) {
3234 newary = sargp->fd_dup2;
3236 newary = hide_obj(rb_ary_new());
3237 sargp->fd_dup2 = newary;
3239 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3240 redirection = hide_obj(rb_assoc_new(
INT2FIX(fd),
INT2FIX(save_fd)));
3241 if (cloexec) rb_ary_push(redirection,
Qtrue);
3242 rb_ary_push(newary, redirection);
3244 newary = sargp->fd_close;
3246 newary = hide_obj(rb_ary_new());
3247 sargp->fd_close = newary;
3249 rb_ary_push(newary, hide_obj(rb_assoc_new(
INT2FIX(save_fd),
Qnil)));
3256intcmp(
const void *a,
const void *b)
3258 return *(
int*)a - *(
int*)b;
3262intrcmp(
const void *a,
const void *b)
3264 return *(
int*)b - *(
int*)a;
3276run_exec_dup2_tmpbuf_size(
long n)
3283fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3287 ret = fcntl(fd, F_GETFD);
3289 ERRMSG(
"fcntl(F_GETFD)");
3292 if (ret & FD_CLOEXEC)
return 1;
3299fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3303 ret = fcntl(fd, F_GETFD);
3305 ERRMSG(
"fcntl(F_GETFD)");
3308 if (!(ret & FD_CLOEXEC)) {
3310 ret = fcntl(fd, F_SETFD, ret);
3312 ERRMSG(
"fcntl(F_SETFD)");
3322fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3326 ret = fcntl(fd, F_GETFD);
3328 ERRMSG(
"fcntl(F_GETFD)");
3331 if (ret & FD_CLOEXEC) {
3333 ret = fcntl(fd, F_SETFD, ret);
3335 ERRMSG(
"fcntl(F_SETFD)");
3345run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3356 for (i = 0; i < n; i++) {
3361 pairs[i].older_index = -1;
3371 for (i = 0; i < n; i++) {
3372 int newfd = pairs[i].newfd;
3376 pairs[i].num_newer = 0;
3378 while (pairs < found && (found-1)->oldfd == newfd)
3380 while (found < pairs+n && found->oldfd == newfd) {
3381 pairs[i].num_newer++;
3382 found->older_index = i;
3389 for (i = 0; i < n; i++) {
3391 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3392 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3394 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3399 if (pairs[j].cloexec &&
3400 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3404 pairs[j].oldfd = -1;
3405 j = pairs[j].older_index;
3407 pairs[j].num_newer--;
3412 for (i = 0; i < n; i++) {
3414 if (pairs[i].oldfd == -1)
3416 if (pairs[i].oldfd == pairs[i].newfd) {
3417 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3419 pairs[i].oldfd = -1;
3422 if (extra_fd == -1) {
3423 extra_fd = redirect_dup(pairs[i].oldfd);
3424 if (extra_fd == -1) {
3431 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3438 pairs[i].oldfd = extra_fd;
3439 j = pairs[i].older_index;
3440 pairs[i].older_index = -1;
3442 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3448 pairs[j].oldfd = -1;
3449 j = pairs[j].older_index;
3452 if (extra_fd != -1) {
3453 ret = redirect_close(extra_fd);
3468run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3476 ret = redirect_close(fd);
3487run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3497 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3499 ret = redirect_dup2(oldfd, newfd);
3512run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3524 pgroup = eargp->pgroup_pgid;
3530 sargp->pgroup_given = 1;
3531 sargp->pgroup_pgid = getpgrp();
3537 ret = setpgid(getpid(), pgroup);
3538 if (ret == -1) ERRMSG(
"setpgid");
3543#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3546run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3555 if (getrlimit(rtype, &rlim) == -1) {
3556 ERRMSG(
"getrlimit");
3560 RLIM2NUM(rlim.rlim_cur),
3561 RLIM2NUM(rlim.rlim_max)));
3562 if (sargp->rlimit_limits ==
Qfalse)
3563 newary = sargp->rlimit_limits = hide_obj(rb_ary_new());
3565 newary = sargp->rlimit_limits;
3566 rb_ary_push(newary, tmp);
3570 if (setrlimit(rtype, &rlim) == -1) {
3571 ERRMSG(
"setrlimit");
3579#if !defined(HAVE_WORKING_FORK)
3583 rb_ary_push(ary, hide_obj(rb_ary_dup(argv[0])));
3592 if (sargp->env_modification ==
Qfalse) {
3593 VALUE env = rb_envtbl();
3595 VALUE ary = hide_obj(rb_ary_new());
3598 sargp->env_modification = ary;
3600 sargp->unsetenv_others_given = 1;
3601 sargp->unsetenv_others_do = 1;
3608#define chdir(p) rb_w32_uchdir(p)
3613rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3620 sargp->redirect_fds =
Qnil;
3624 if (eargp->pgroup_given) {
3625 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3630#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3631 obj = eargp->rlimit_limits;
3633 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3638#if !defined(HAVE_WORKING_FORK)
3639 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3644 obj = eargp->env_modification;
3660 if (eargp->umask_given) {
3661 mode_t mask = eargp->umask_mask;
3662 mode_t oldmask = umask(mask);
3664 sargp->umask_given = 1;
3665 sargp->umask_mask = oldmask;
3669 obj = eargp->fd_dup2;
3671 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3675 obj = eargp->fd_close;
3678 rb_warn(
"cannot close fd before spawn");
3680 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3685#ifdef HAVE_WORKING_FORK
3686 if (eargp->close_others_do) {
3691 obj = eargp->fd_dup2_child;
3693 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3697 if (eargp->chdir_given) {
3699 sargp->chdir_given = 1;
3700 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3709 if (eargp->gid_given) {
3710 if (setgid(eargp->gid) < 0) {
3717 if (eargp->uid_given) {
3718 if (setuid(eargp->uid) < 0) {
3726 VALUE ary = sargp->fd_dup2;
3728 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3732 int preserve = errno;
3733 stdfd_clear_nonblock();
3742rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3744 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3749exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3751#if !defined(HAVE_WORKING_FORK)
3752 struct rb_execarg sarg, *
const sargp = &sarg;
3758 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3762 if (eargp->use_shell) {
3763 err = proc_exec_sh(
RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3766 char *abspath = NULL;
3767 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3768 abspath =
RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3769 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3771#if !defined(HAVE_WORKING_FORK)
3772 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3778#ifdef HAVE_WORKING_FORK
3781rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3783 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3787proc_syswait(
VALUE pid)
3794move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3798 for (i = 0; i < n; i++) {
3817pipe_nocrash(
int filedes[2],
VALUE fds)
3825 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3840rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3847handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3853 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3859#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3862 if (!status && !ep) {
3867 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3868 if (status) status->status = state;
3869 if (!state)
return 0;
3878 if (state && !status) rb_jump_tag(state);
3882#define prefork() ( \
3883 rb_io_flush(rb_stdout), \
3884 rb_io_flush(rb_stderr) \
3914write_retry(
int fd,
const void *buf,
size_t len)
3919 w = write(fd, buf, len);
3920 }
while (w < 0 && errno == EINTR);
3926read_retry(
int fd,
void *buf,
size_t len)
3930 if (set_blocking(fd) != 0) {
3932 rb_async_bug_errno(
"set_blocking failed reading child error", errno);
3937 r = read(fd, buf, len);
3938 }
while (r < 0 && errno == EINTR);
3944send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3949 if (write_retry(fd, &err,
sizeof(err)) < 0) err = errno;
3950 if (errmsg && 0 < errmsg_buflen) {
3951 errmsg[errmsg_buflen-1] =
'\0';
3952 errmsg_buflen = strlen(errmsg);
3953 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3959recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3963 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3967 if (size ==
sizeof(err) &&
3968 errmsg && 0 < errmsg_buflen) {
3969 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3978#ifdef HAVE_WORKING_VFORK
3979#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3982getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3988 ret = getuidx(ID_SAVED);
3989 if (ret == (rb_uid_t)-1)
3994#define HAVE_GETRESUID
3997#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
4000getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
4006 ret = getgidx(ID_SAVED);
4007 if (ret == (rb_gid_t)-1)
4012#define HAVE_GETRESGID
4030 rb_uid_t ruid, euid;
4031 rb_gid_t rgid, egid;
4033#if defined HAVE_ISSETUGID
4038#ifdef HAVE_GETRESUID
4042 ret = getresuid(&ruid, &euid, &suid);
4053 if (euid == 0 || euid != ruid)
4056#ifdef HAVE_GETRESGID
4060 ret = getresgid(&rgid, &egid, &sgid);
4078struct child_handler_disabler_state
4084disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
4086#ifdef HAVE_PTHREAD_SIGMASK
4090 ret = sigfillset(&all);
4094 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
4099# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4104disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
4106#ifdef HAVE_PTHREAD_SIGMASK
4109 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
4114# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4120disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
4125 for (sig = 1; sig < NSIG; sig++) {
4126 sig_t handler = signal(sig, SIG_DFL);
4128 if (handler == SIG_ERR && errno == EINVAL) {
4131 if (handler == SIG_ERR) {
4132 ERRMSG(
"signal to obtain old action");
4136 if (sig == SIGPIPE) {
4141 if (handler == SIG_IGN) {
4142 signal(sig, SIG_IGN);
4147 sigemptyset(&old->sigmask);
4148 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
4150 ERRMSG(
"sigprocmask");
4158 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4159 char *errmsg,
size_t errmsg_buflen,
4163 volatile int try_gc = 1;
4164 struct child_handler_disabler_state old;
4166 rb_nativethread_lock_t *
const volatile waitpid_lock_init =
4167 (w && WAITPID_USE_SIGCHLD) ? &GET_VM()->waitpid_lock : 0;
4170 rb_nativethread_lock_t *waitpid_lock = waitpid_lock_init;
4172 disable_child_handler_before_fork(&old);
4176#ifdef HAVE_WORKING_VFORK
4177 if (!has_privilege())
4187 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
4189 ret = chfunc(charg, errmsg, errmsg_buflen);
4190 if (!ret) _exit(EXIT_SUCCESS);
4192 send_child_error(ep[1], errmsg, errmsg_buflen);
4193#if EXIT_SUCCESS == 127
4194 _exit(EXIT_FAILURE);
4200 waitpid_lock = waitpid_lock_init;
4202 if (pid > 0 && w != WAITPID_LOCK_ONLY) {
4204 ccan_list_add(&GET_VM()->waiting_pids, &w->wnode);
4208 disable_child_handler_fork_parent(&old);
4212 if (handle_fork_error(err, status, ep, &try_gc))
4222 struct child_handler_disabler_state old;
4225 disable_child_handler_before_fork(&old);
4229 pid_t pid = rb_fork();
4230 if (pid > 0) mjit_add_waiting_pid(vm, pid);
4234 disable_child_handler_fork_parent(&old);
4242fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4243 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4251 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4253 if (status) status->status = 0;
4255 if (pipe_nocrash(ep, fds))
return -1;
4257 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4259 if (status) status->pid = pid;
4262 if (status) status->error = errno;
4269 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4271 if (error_occurred) {
4274 status->error = err;
4276 VM_ASSERT((w == 0 || w == WAITPID_LOCK_ONLY) &&
4277 "only used by extensions");
4278 rb_protect(proc_syswait, (
VALUE)pid, &state);
4280 status->status = state;
4282 else if (!w || w == WAITPID_LOCK_ONLY) {
4301rb_fork_async_signal_safe(
int *status,
4302 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4303 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4307 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4310 *status = process_status.status;
4320 int try_gc = 1, err;
4321 struct child_handler_disabler_state old;
4323 if (status) status->status = 0;
4327 if (mjit_enabled) mjit_pause(
false);
4328 disable_child_handler_before_fork(&old);
4334 status->error = err;
4337 disable_child_handler_fork_parent(&old);
4339 if (mjit_enabled && pid > 0) mjit_resume();
4347 if (handle_fork_error(err, status, NULL, &try_gc)) {
4354rb_fork_ruby(
int *status)
4358 rb_pid_t pid = rb_fork_ruby2(&process_status);
4360 if (status) *status = process_status.status;
4368 rb_pid_t pid = rb_fork_ruby(NULL);
4378rb_call_proc__fork(
void)
4383 return proc_fork_pid();
4392#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4413rb_proc__fork(
VALUE _obj)
4415 rb_pid_t pid = proc_fork_pid();
4449 pid = rb_call_proc__fork();
4463#define rb_proc__fork rb_f_notimplement
4464#define rb_f_fork rb_f_notimplement
4468exit_status_code(
VALUE status)
4474 istatus = EXIT_SUCCESS;
4477 istatus = EXIT_FAILURE;
4481#if EXIT_SUCCESS != 0
4483 istatus = EXIT_SUCCESS;
4490NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4503rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4508 istatus = exit_status_code(argv[0]);
4511 istatus = EXIT_FAILURE;
4521 if (GET_EC()->tag) {
4537 istatus = exit_status_code(argv[0]);
4540 istatus = EXIT_SUCCESS;
4602 VALUE errinfo = rb_ec_get_errinfo(ec);
4603 if (!
NIL_P(errinfo)) {
4604 rb_ec_error_print(ec, errinfo);
4611 args[1] = args[0] = argv[0];
4614 args[0] =
INT2NUM(EXIT_FAILURE);
4649#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4654 if (eargp && !eargp->use_shell) {
4655 VALUE str = eargp->invoke.cmd.argv_str;
4656 VALUE buf = eargp->invoke.cmd.argv_buf;
4657 char *p, **argv = ARGVSTR2ARGV(str);
4658 long i, argc = ARGVSTR2ARGC(str);
4662 for (i = 1; i < argc; ++i) {
4663 p[argv[i] - start - 1] =
' ';
4673rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4676#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4679# if !defined HAVE_SPAWNV
4684#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4685 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4687 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4689 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4693 if (prog && !eargp->use_shell) {
4694 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4697# if defined HAVE_SPAWNV
4698 if (eargp->use_shell) {
4702 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4703 pid = proc_spawn_cmd(argv, prog, eargp);
4710 status = system(rb_execarg_commandline(eargp, &prog));
4715 if (eargp->waitpid_state && eargp->waitpid_state != WAITPID_LOCK_ONLY) {
4716 eargp->waitpid_state->pid = pid;
4719 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4734do_spawn_process(
VALUE arg)
4737 rb_execarg_parent_start1(argp->execarg);
4739 argp->errmsg.ptr, argp->errmsg.buflen);
4743rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4746 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4752 if (!eargp->waitpid_state && mjit_enabled) {
4753 eargp->waitpid_state = WAITPID_LOCK_ONLY;
4756 args.execarg = execarg_obj;
4757 args.errmsg.ptr = errmsg;
4758 args.errmsg.buflen = errmsg_buflen;
4760 execarg_parent_end, execarg_obj);
4764rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4768 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4769 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4775 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4781 return rb_spawn_internal(argc, argv, NULL, 0);
4841 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4842 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4845 eargp->status = &status;
4847 rb_last_status_clear();
4851 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4854 VALUE status = rb_process_status_wait(pid, 0);
4860 GET_THREAD()->last_status = status;
4862 if (data->status == EXIT_SUCCESS) {
4866 if (data->error != 0) {
4867 if (eargp->exception) {
4868 VALUE command = eargp->invoke.sh.shell_script;
4876 else if (eargp->exception) {
4877 VALUE command = eargp->invoke.sh.shell_script;
4891 if (eargp->exception) {
4892 VALUE command = eargp->invoke.sh.shell_script;
5174 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
5175 VALUE execarg_obj, fail_str;
5178 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
5179 eargp = rb_execarg_get(execarg_obj);
5180 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
5182 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
5186 rb_exec_fail(eargp, err, errmsg);
5190#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
5217 time_t beg = time(0);
5220 if (scheduler !=
Qnil) {
5233 time_t end = time(0) - beg;
5235 return TIMET2NUM(end);
5239#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
5256#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
5267#define proc_getpgrp rb_f_notimplement
5271#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
5289#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
5295#define proc_setpgrp rb_f_notimplement
5299#if defined(HAVE_GETPGID)
5320#define proc_getpgid rb_f_notimplement
5336 rb_pid_t ipid, ipgrp;
5345#define proc_setpgid rb_f_notimplement
5376#define proc_getsid rb_f_notimplement
5380#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5381#if !defined(HAVE_SETSID)
5382static rb_pid_t ruby_setsid(
void);
5383#define setsid() ruby_setsid()
5406#if !defined(HAVE_SETSID)
5407#define HAVE_SETSID 1
5415#if defined(SETPGRP_VOID)
5421 ret = setpgrp(0, pid);
5423 if (ret == -1)
return -1;
5427 ioctl(fd, TIOCNOTTY, NULL);
5434#define proc_setsid rb_f_notimplement
5438#ifdef HAVE_GETPRIORITY
5459 int prio, iwhich, iwho;
5465 prio = getpriority(iwhich, iwho);
5470#define proc_getpriority rb_f_notimplement
5474#ifdef HAVE_GETPRIORITY
5490 int iwhich, iwho, iprio;
5496 if (setpriority(iwhich, iwho, iprio) < 0)
5501#define proc_setpriority rb_f_notimplement
5504#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5506rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5510#define RESCHECK(r) \
5512 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5513 resource = RLIMIT_##r; \
5547#ifdef RLIMIT_MEMLOCK
5550#ifdef RLIMIT_MSGQUEUE
5589#ifdef RLIMIT_SIGPENDING
5590 RESCHECK(SIGPENDING);
5599 for (p = name; *p; p++)
5605 for (p = name; *p; p++)
5611 rb_bug(
"unexpected casetype");
5618rlimit_type_by_hname(
const char *name,
long len)
5620 return rlimit_resource_name2int(name, len, 0);
5624rlimit_type_by_lname(
const char *name,
long len)
5626 return rlimit_resource_name2int(name, len, 1);
5630rlimit_type_by_sym(
VALUE key)
5636 static const char prefix[] =
"rlimit_";
5637 enum {prefix_len =
sizeof(prefix)-1};
5639 if (len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5640 rtype = rlimit_type_by_lname(rname + prefix_len, len - prefix_len);
5648rlimit_resource_type(
VALUE rtype)
5655 switch (
TYPE(rtype)) {
5678 r = rlimit_type_by_hname(name, len);
5688rlimit_resource_value(
VALUE rval)
5693 switch (
TYPE(rval)) {
5711 return NUM2RLIM(rval);
5715 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5717#ifdef RLIM_SAVED_MAX
5718 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5720#ifdef RLIM_SAVED_CUR
5721 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5729#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5755 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5758 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5761#define proc_getrlimit rb_f_notimplement
5764#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5818proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5820 VALUE resource, rlim_cur, rlim_max;
5826 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5827 rlim_max = rlim_cur;
5829 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5830 rlim.rlim_max = rlimit_resource_value(rlim_max);
5832 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5838#define proc_setrlimit rb_f_notimplement
5841static int under_uid_switch = 0;
5843check_uid_switch(
void)
5845 if (under_uid_switch) {
5850static int under_gid_switch = 0;
5852check_gid_switch(
void)
5854 if (under_gid_switch) {
5860#if defined(HAVE_PWD_H)
5869#if ( !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN) )
5872 char MAYBE_UNUSED(*login) = NULL;
5874# ifdef USE_GETLOGIN_R
5876#if defined(__FreeBSD__)
5877 typedef int getlogin_r_size_t;
5879 typedef size_t getlogin_r_size_t;
5882 long loginsize = GETLOGIN_R_SIZE_INIT;
5885 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5895 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5897 if (gle == ENOTTY || gle == ENXIO || gle == ENOENT) {
5902 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5912 if (login == NULL) {
5917 return maybe_result;
5924 if (errno == ENOTTY || errno == ENXIO || errno == ENOENT) {
5937rb_getpwdirnam_for_login(
VALUE login_name)
5939#if ( !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM) )
5943 if (
NIL_P(login_name)) {
5950 struct passwd *pwptr;
5952# ifdef USE_GETPWNAM_R
5954 struct passwd pwdnm;
5956 long bufsizenm = GETPW_R_SIZE_INIT;
5959 bufsizenm = GETPW_R_SIZE_DEFAULT;
5969 while ((enm = getpwnam_r(login, &pwdnm, bufnm, bufsizenm, &pwptr)) != 0) {
5971 if (enm == ENOENT || enm== ESRCH || enm == EBADF || enm == EPERM) {
5977 if (enm != ERANGE || bufsizenm >= GETPW_R_SIZE_LIMIT) {
5987 if (pwptr == NULL) {
6001 pwptr = getpwnam(login);
6008 && ( errno != ENOENT && errno != ESRCH && errno != EBADF && errno != EPERM)) {
6024# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
6028 uid_t ruid = getuid();
6030 struct passwd *pwptr;
6032# ifdef USE_GETPWUID_R
6034 struct passwd pwdid;
6036 long bufsizeid = GETPW_R_SIZE_INIT;
6039 bufsizeid = GETPW_R_SIZE_DEFAULT;
6049 while ((eid = getpwuid_r(ruid, &pwdid, bufid, bufsizeid, &pwptr)) != 0) {
6051 if (eid == ENOENT || eid== ESRCH || eid == EBADF || eid == EPERM) {
6057 if (eid != ERANGE || bufsizeid >= GETPW_R_SIZE_LIMIT) {
6067 if (pwptr == NULL) {
6078# elif defined(USE_GETPWUID)
6081 pwptr = getpwuid(ruid);
6088 && ( errno == ENOENT || errno == ESRCH || errno == EBADF || errno == EPERM)) {
6110#if defined(HAVE_PWD_H)
6113# ifdef USE_GETPWNAM_R
6126 struct passwd *pwptr;
6127#ifdef USE_GETPWNAM_R
6128 struct passwd pwbuf;
6133 getpw_buf_len = GETPW_R_SIZE_INIT;
6134 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
6141 while ((e = getpwnam_r(usrname, &pwbuf, getpw_buf, getpw_buf_len, &pwptr)) != 0) {
6142 if (e != ERANGE || getpw_buf_len >= GETPW_R_SIZE_LIMIT) {
6151 pwptr = getpwnam(usrname);
6154#ifndef USE_GETPWNAM_R
6159 uid = pwptr->pw_uid;
6160#ifndef USE_GETPWNAM_R
6167# ifdef p_uid_from_name
6187#if defined(HAVE_GRP_H)
6190# ifdef USE_GETGRNAM_R
6203 struct group *grptr;
6204#ifdef USE_GETGRNAM_R
6210 getgr_buf_len = GETGR_R_SIZE_INIT;
6211 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6218 while ((e = getgrnam_r(grpname, &grbuf, getgr_buf, getgr_buf_len, &grptr)) != 0) {
6219 if (e != ERANGE || getgr_buf_len >= GETGR_R_SIZE_LIMIT) {
6227#elif defined(HAVE_GETGRNAM)
6228 grptr = getgrnam(grpname);
6233#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6238 gid = grptr->gr_gid;
6239#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6246# ifdef p_gid_from_name
6266#if defined HAVE_SETUID
6284#define p_sys_setuid rb_f_notimplement
6288#if defined HAVE_SETRUID
6306#define p_sys_setruid rb_f_notimplement
6310#if defined HAVE_SETEUID
6328#define p_sys_seteuid rb_f_notimplement
6332#if defined HAVE_SETREUID
6347 rb_uid_t ruid, euid;
6350 ruid = OBJ2UID1(rid);
6351 euid = OBJ2UID1(eid);
6357#define p_sys_setreuid rb_f_notimplement
6361#if defined HAVE_SETRESUID
6376 rb_uid_t ruid, euid, suid;
6379 ruid = OBJ2UID1(rid);
6380 euid = OBJ2UID1(eid);
6381 suid = OBJ2UID1(sid);
6383 if (setresuid(ruid, euid, suid) != 0)
rb_sys_fail(0);
6387#define p_sys_setresuid rb_f_notimplement
6403proc_getuid(
VALUE obj)
6405 rb_uid_t uid = getuid();
6410#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6427#if defined(HAVE_SETRESUID)
6429#elif defined HAVE_SETREUID
6431#elif defined HAVE_SETRUID
6433#elif defined HAVE_SETUID
6435 if (geteuid() == uid) {
6446#define proc_setuid rb_f_notimplement
6460static rb_uid_t SAVED_USER_ID = -1;
6462#ifdef BROKEN_SETREUID
6464setreuid(rb_uid_t ruid, rb_uid_t euid)
6466 if (ruid != (rb_uid_t)-1 && ruid != getuid()) {
6467 if (euid == (rb_uid_t)-1) euid = geteuid();
6468 if (setuid(ruid) < 0)
return -1;
6470 if (euid != (rb_uid_t)-1 && euid != geteuid()) {
6471 if (seteuid(euid) < 0)
return -1;
6499 if (geteuid() == 0) {
6500#if defined(HAVE_SETRESUID)
6502 SAVED_USER_ID = uid;
6503#elif defined(HAVE_SETUID)
6505 SAVED_USER_ID = uid;
6506#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6507 if (getuid() == uid) {
6508 if (SAVED_USER_ID == uid) {
6513 if (setreuid(-1, SAVED_USER_ID) < 0)
rb_sys_fail(0);
6514 if (setreuid(SAVED_USER_ID, 0) < 0)
rb_sys_fail(0);
6517 SAVED_USER_ID = uid;
6523 SAVED_USER_ID = uid;
6529 SAVED_USER_ID = uid;
6531#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6532 if (getuid() == uid) {
6533 if (SAVED_USER_ID == uid) {
6547 SAVED_USER_ID = uid;
6554 SAVED_USER_ID = uid;
6562#if defined(HAVE_SETRESUID)
6563 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6564 (geteuid() == uid)? (rb_uid_t)-1: uid,
6565 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0)
rb_sys_fail(0);
6566 SAVED_USER_ID = uid;
6567#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6568 if (SAVED_USER_ID == uid) {
6569 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6570 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6573 else if (getuid() != uid) {
6574 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6576 SAVED_USER_ID = uid;
6578 else if ( geteuid() != uid) {
6580 SAVED_USER_ID = uid;
6584 if (setreuid(-1, SAVED_USER_ID) < 0)
rb_sys_fail(0);
6585 if (setreuid(SAVED_USER_ID, uid) < 0)
rb_sys_fail(0);
6586 SAVED_USER_ID = uid;
6589#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6590 if (SAVED_USER_ID == uid) {
6591 if (geteuid() != uid && seteuid(uid) < 0)
rb_sys_fail(0);
6592 if (getuid() != uid && setruid(uid) < 0)
rb_sys_fail(0);
6594 else if ( geteuid() == uid) {
6595 if (getuid() != uid) {
6597 SAVED_USER_ID = uid;
6601 SAVED_USER_ID = uid;
6605 else if ( getuid() == uid) {
6608 SAVED_USER_ID = uid;
6614#elif defined HAVE_44BSD_SETUID
6615 if (getuid() == uid) {
6618 SAVED_USER_ID = uid;
6623#elif defined HAVE_SETEUID
6624 if (getuid() == uid && SAVED_USER_ID == uid) {
6630#elif defined HAVE_SETUID
6631 if (getuid() == uid && SAVED_USER_ID == uid) {
6646#if defined HAVE_SETGID
6664#define p_sys_setgid rb_f_notimplement
6668#if defined HAVE_SETRGID
6686#define p_sys_setrgid rb_f_notimplement
6690#if defined HAVE_SETEGID
6708#define p_sys_setegid rb_f_notimplement
6712#if defined HAVE_SETREGID
6727 rb_gid_t rgid, egid;
6729 rgid = OBJ2GID(rid);
6730 egid = OBJ2GID(eid);
6735#define p_sys_setregid rb_f_notimplement
6738#if defined HAVE_SETRESGID
6753 rb_gid_t rgid, egid, sgid;
6755 rgid = OBJ2GID(rid);
6756 egid = OBJ2GID(eid);
6757 sgid = OBJ2GID(sid);
6758 if (setresgid(rgid, egid, sgid) != 0)
rb_sys_fail(0);
6762#define p_sys_setresgid rb_f_notimplement
6766#if defined HAVE_ISSETUGID
6780p_sys_issetugid(
VALUE obj)
6782 return RBOOL(issetugid());
6785#define p_sys_issetugid rb_f_notimplement
6801proc_getgid(
VALUE obj)
6803 rb_gid_t gid = getgid();
6808#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6824#if defined(HAVE_SETRESGID)
6826#elif defined HAVE_SETREGID
6828#elif defined HAVE_SETRGID
6830#elif defined HAVE_SETGID
6832 if (getegid() == gid) {
6843#define proc_setgid rb_f_notimplement
6847#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6867static int _maxgroups = -1;
6869get_sc_ngroups_max(
void)
6871#ifdef _SC_NGROUPS_MAX
6872 return (
int)sysconf(_SC_NGROUPS_MAX);
6873#elif defined(NGROUPS_MAX)
6874 return (
int)NGROUPS_MAX;
6882 if (_maxgroups < 0) {
6883 _maxgroups = get_sc_ngroups_max();
6885 _maxgroups = RB_MAX_GROUPS;
6894#ifdef HAVE_GETGROUPS
6921proc_getgroups(
VALUE obj)
6927 ngroups = getgroups(0, NULL);
6931 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6933 ngroups = getgroups(ngroups, groups);
6938 for (i = 0; i < ngroups; i++)
6939 rb_ary_push(ary,
GIDT2NUM(groups[i]));
6946#define proc_getgroups rb_f_notimplement
6950#ifdef HAVE_SETGROUPS
6975 if (ngroups > maxgroups())
6978 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6980 for (i = 0; i < ngroups; i++) {
6983 groups[i] = OBJ2GID1(g);
6987 if (setgroups(ngroups, groups) == -1)
6992 return proc_getgroups(obj);
6995#define proc_setgroups rb_f_notimplement
6999#ifdef HAVE_INITGROUPS
7023 return proc_getgroups(obj);
7026#define proc_initgroups rb_f_notimplement
7029#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
7041proc_getmaxgroups(
VALUE obj)
7046#define proc_getmaxgroups rb_f_notimplement
7049#ifdef HAVE_SETGROUPS
7062 int ngroups_max = get_sc_ngroups_max();
7067 if (ngroups > RB_MAX_GROUPS)
7068 ngroups = RB_MAX_GROUPS;
7070 if (ngroups_max > 0 && ngroups > ngroups_max)
7071 ngroups = ngroups_max;
7073 _maxgroups = ngroups;
7078#define proc_setmaxgroups rb_f_notimplement
7081#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
7082static int rb_daemon(
int nochdir,
int noclose);
7101 int n, nochdir = FALSE, noclose = FALSE;
7104 case 2: noclose = TO_BOOL(argv[1],
"noclose");
7105 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
7109 n = rb_daemon(nochdir, noclose);
7115rb_daemon(
int nochdir,
int noclose)
7119 if (mjit_enabled) mjit_pause(
false);
7121 err = daemon(nochdir, noclose);
7127 switch (rb_fork_ruby(NULL)) {
7130 default: _exit(EXIT_SUCCESS);
7134 if (setsid() < 0) (void)0;
7139 if (!noclose && (n =
rb_cloexec_open(
"/dev/null", O_RDWR, 0)) != -1) {
7151#define proc_daemon rb_f_notimplement
7164static rb_gid_t SAVED_GROUP_ID = -1;
7166#ifdef BROKEN_SETREGID
7168setregid(rb_gid_t rgid, rb_gid_t egid)
7170 if (rgid != (rb_gid_t)-1 && rgid != getgid()) {
7171 if (egid == (rb_gid_t)-1) egid = getegid();
7172 if (setgid(rgid) < 0)
return -1;
7174 if (egid != (rb_gid_t)-1 && egid != getegid()) {
7175 if (setegid(egid) < 0)
return -1;
7203 if (geteuid() == 0) {
7204#if defined(HAVE_SETRESGID)
7206 SAVED_GROUP_ID = gid;
7207#elif defined HAVE_SETGID
7209 SAVED_GROUP_ID = gid;
7210#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7211 if (getgid() == gid) {
7212 if (SAVED_GROUP_ID == gid) {
7217 if (setregid(-1, SAVED_GROUP_ID) < 0)
rb_sys_fail(0);
7218 if (setregid(SAVED_GROUP_ID, 0) < 0)
rb_sys_fail(0);
7221 SAVED_GROUP_ID = gid;
7227 SAVED_GROUP_ID = gid;
7233 SAVED_GROUP_ID = gid;
7235#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7236 if (getgid() == gid) {
7237 if (SAVED_GROUP_ID == gid) {
7252 SAVED_GROUP_ID = gid;
7259 SAVED_GROUP_ID = gid;
7266#if defined(HAVE_SETRESGID)
7267 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7268 (getegid() == gid)? (rb_gid_t)-1: gid,
7269 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0)
rb_sys_fail(0);
7270 SAVED_GROUP_ID = gid;
7271#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7272 if (SAVED_GROUP_ID == gid) {
7273 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7274 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7277 else if (getgid() != gid) {
7278 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7280 SAVED_GROUP_ID = gid;
7282 else if ( getegid() != gid) {
7284 SAVED_GROUP_ID = gid;
7288 if (setregid(-1, SAVED_GROUP_ID) < 0)
rb_sys_fail(0);
7289 if (setregid(SAVED_GROUP_ID, gid) < 0)
rb_sys_fail(0);
7290 SAVED_GROUP_ID = gid;
7293#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7294 if (SAVED_GROUP_ID == gid) {
7295 if (getegid() != gid && setegid(gid) < 0)
rb_sys_fail(0);
7296 if (getgid() != gid && setrgid(gid) < 0)
rb_sys_fail(0);
7298 else if ( getegid() == gid) {
7299 if (getgid() != gid) {
7301 SAVED_GROUP_ID = gid;
7305 SAVED_GROUP_ID = gid;
7309 else if ( getgid() == gid) {
7312 SAVED_GROUP_ID = gid;
7318#elif defined HAVE_44BSD_SETGID
7319 if (getgid() == gid) {
7322 SAVED_GROUP_ID = gid;
7327#elif defined HAVE_SETEGID
7328 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7334#elif defined HAVE_SETGID
7335 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7362proc_geteuid(
VALUE obj)
7364 rb_uid_t euid = geteuid();
7368#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7370proc_seteuid(rb_uid_t uid)
7372#if defined(HAVE_SETRESUID)
7374#elif defined HAVE_SETREUID
7376#elif defined HAVE_SETEUID
7378#elif defined HAVE_SETUID
7379 if (uid == getuid()) {
7391#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7404 proc_seteuid(OBJ2UID(euid));
7408#define proc_seteuid_m rb_f_notimplement
7412rb_seteuid_core(rb_uid_t euid)
7414#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7420#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7424#if defined(HAVE_SETRESUID)
7427 SAVED_USER_ID = euid;
7432#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7437 SAVED_USER_ID = euid;
7439#elif defined HAVE_SETEUID
7441#elif defined HAVE_SETUID
7468 rb_seteuid_core(OBJ2UID(
id));
7486proc_getegid(
VALUE obj)
7488 rb_gid_t egid = getegid();
7493#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7505#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7511#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7512 gid = OBJ2GID(egid);
7515#if defined(HAVE_SETRESGID)
7517#elif defined HAVE_SETREGID
7519#elif defined HAVE_SETEGID
7521#elif defined HAVE_SETGID
7522 if (gid == getgid()) {
7535#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7536#define proc_setegid_m proc_setegid
7538#define proc_setegid_m rb_f_notimplement
7542rb_setegid_core(rb_gid_t egid)
7544#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7550#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7554#if defined(HAVE_SETRESGID)
7557 SAVED_GROUP_ID = egid;
7562#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7567 SAVED_GROUP_ID = egid;
7569#elif defined HAVE_SETEGID
7571#elif defined HAVE_SETGID
7598 rb_setegid_core(OBJ2GID(
id));
7613p_uid_exchangeable(
VALUE _)
7615#if defined(HAVE_SETRESUID)
7617#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7638p_uid_exchange(
VALUE obj)
7641#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7648#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7652#if defined(HAVE_SETRESUID)
7653 if (setresuid(euid, uid, uid) < 0)
rb_sys_fail(0);
7654 SAVED_USER_ID = uid;
7655#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7657 SAVED_USER_ID = uid;
7675p_gid_exchangeable(
VALUE _)
7677#if defined(HAVE_SETRESGID)
7679#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7700p_gid_exchange(
VALUE obj)
7703#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7710#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7714#if defined(HAVE_SETRESGID)
7715 if (setresgid(egid, gid, gid) < 0)
rb_sys_fail(0);
7716 SAVED_GROUP_ID = gid;
7717#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7719 SAVED_GROUP_ID = gid;
7738p_uid_have_saved_id(
VALUE _)
7740#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7748#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7750p_uid_sw_ensure(
VALUE i)
7752 rb_uid_t
id = (rb_uid_t)i;
7753 under_uid_switch = 0;
7754 id = rb_seteuid_core(
id);
7773p_uid_switch(
VALUE obj)
7785 under_uid_switch = 1;
7792 else if (euid != SAVED_USER_ID) {
7793 proc_seteuid(SAVED_USER_ID);
7795 under_uid_switch = 1;
7810p_uid_sw_ensure(
VALUE obj)
7812 under_uid_switch = 0;
7813 return p_uid_exchange(obj);
7817p_uid_switch(
VALUE obj)
7829 p_uid_exchange(obj);
7831 under_uid_switch = 1;
7853p_gid_have_saved_id(
VALUE _)
7855#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7862#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7864p_gid_sw_ensure(
VALUE i)
7866 rb_gid_t
id = (rb_gid_t)i;
7867 under_gid_switch = 0;
7868 id = rb_setegid_core(
id);
7887p_gid_switch(
VALUE obj)
7899 under_gid_switch = 1;
7906 else if (egid != SAVED_GROUP_ID) {
7907 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7909 under_gid_switch = 1;
7924p_gid_sw_ensure(
VALUE obj)
7926 under_gid_switch = 0;
7927 return p_gid_exchange(obj);
7931p_gid_switch(
VALUE obj)
7943 p_gid_exchange(obj);
7945 under_gid_switch = 1;
7955#if defined(HAVE_TIMES)
7959#ifdef HAVE__SC_CLK_TCK
7960 return sysconf(_SC_CLK_TCK);
7961#elif defined CLK_TCK
7983rb_proc_times(
VALUE obj)
7985 VALUE utime, stime, cutime, cstime, ret;
7986#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7987 struct rusage usage_s, usage_c;
7989 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7991 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7992 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7993 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7994 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7996 const double hertz = (double)get_clk_tck();
8000 utime =
DBL2NUM(buf.tms_utime / hertz);
8001 stime =
DBL2NUM(buf.tms_stime / hertz);
8002 cutime =
DBL2NUM(buf.tms_cutime / hertz);
8003 cstime =
DBL2NUM(buf.tms_cstime / hertz);
8005 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
8013#define rb_proc_times rb_f_notimplement
8016#ifdef HAVE_LONG_LONG
8018#define TIMETICK_INT_MIN LLONG_MIN
8019#define TIMETICK_INT_MAX LLONG_MAX
8020#define TIMETICK_INT2NUM(v) LL2NUM(v)
8021#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
8023typedef long timetick_int_t;
8024#define TIMETICK_INT_MIN LONG_MIN
8025#define TIMETICK_INT_MAX LONG_MAX
8026#define TIMETICK_INT2NUM(v) LONG2NUM(v)
8027#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
8030CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
8031static timetick_int_t
8032gcd_timetick_int(timetick_int_t a, timetick_int_t b)
8052reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
8054 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
8062reduce_factors(timetick_int_t *numerators,
int num_numerators,
8063 timetick_int_t *denominators,
int num_denominators)
8066 for (i = 0; i < num_numerators; i++) {
8067 if (numerators[i] == 1)
8069 for (j = 0; j < num_denominators; j++) {
8070 if (denominators[j] == 1)
8072 reduce_fraction(&numerators[i], &denominators[j]);
8078 timetick_int_t giga_count;
8083timetick2dblnum(
struct timetick *ttp,
8084 timetick_int_t *numerators,
int num_numerators,
8085 timetick_int_t *denominators,
int num_denominators)
8090 reduce_factors(numerators, num_numerators,
8091 denominators, num_denominators);
8093 d = ttp->giga_count * 1e9 + ttp->count;
8095 for (i = 0; i < num_numerators; i++)
8097 for (i = 0; i < num_denominators; i++)
8098 d /= denominators[i];
8104timetick2dblnum_reciprocal(
struct timetick *ttp,
8105 timetick_int_t *numerators,
int num_numerators,
8106 timetick_int_t *denominators,
int num_denominators)
8111 reduce_factors(numerators, num_numerators,
8112 denominators, num_denominators);
8115 for (i = 0; i < num_denominators; i++)
8116 d *= denominators[i];
8117 for (i = 0; i < num_numerators; i++)
8119 d /= ttp->giga_count * 1e9 + ttp->count;
8124#define NDIV(x,y) (-(-((x)+1)/(y))-1)
8125#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
8128timetick2integer(
struct timetick *ttp,
8129 timetick_int_t *numerators,
int num_numerators,
8130 timetick_int_t *denominators,
int num_denominators)
8135 reduce_factors(numerators, num_numerators,
8136 denominators, num_denominators);
8138 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
8139 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
8140 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
8141 for (i = 0; i < num_numerators; i++) {
8142 timetick_int_t factor = numerators[i];
8143 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
8147 for (i = 0; i < num_denominators; i++) {
8148 t = DIV(t, denominators[i]);
8150 return TIMETICK_INT2NUM(t);
8154 v = TIMETICK_INT2NUM(ttp->giga_count);
8157 for (i = 0; i < num_numerators; i++) {
8158 timetick_int_t factor = numerators[i];
8161 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
8163 for (i = 0; i < num_denominators; i++) {
8164 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
8170make_clock_result(
struct timetick *ttp,
8171 timetick_int_t *numerators,
int num_numerators,
8172 timetick_int_t *denominators,
int num_denominators,
8175 if (unit ==
ID2SYM(id_nanosecond)) {
8176 numerators[num_numerators++] = 1000000000;
8177 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8179 else if (unit ==
ID2SYM(id_microsecond)) {
8180 numerators[num_numerators++] = 1000000;
8181 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8183 else if (unit ==
ID2SYM(id_millisecond)) {
8184 numerators[num_numerators++] = 1000;
8185 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8187 else if (unit ==
ID2SYM(id_second)) {
8188 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8190 else if (unit ==
ID2SYM(id_float_microsecond)) {
8191 numerators[num_numerators++] = 1000000;
8192 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8194 else if (unit ==
ID2SYM(id_float_millisecond)) {
8195 numerators[num_numerators++] = 1000;
8196 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8198 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
8199 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8206static const mach_timebase_info_data_t *
8207get_mach_timebase_info(
void)
8209 static mach_timebase_info_data_t sTimebaseInfo;
8211 if ( sTimebaseInfo.denom == 0 ) {
8212 (void) mach_timebase_info(&sTimebaseInfo);
8215 return &sTimebaseInfo;
8219ruby_real_ms_time(
void)
8221 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8222 uint64_t t = mach_absolute_time();
8223 return (
double)t * info->numer / info->denom / 1e6;
8227#if defined(NUM2CLOCKID)
8228# define NUMERIC_CLOCKID 1
8230# define NUMERIC_CLOCKID 0
8231# define NUM2CLOCKID(x) 0
8367 timetick_int_t numerators[2];
8368 timetick_int_t denominators[2];
8369 int num_numerators = 0;
8370 int num_denominators = 0;
8373 VALUE clk_id = argv[0];
8374#ifdef HAVE_CLOCK_GETTIME
8379#ifdef CLOCK_REALTIME
8380 if (clk_id == RUBY_CLOCK_REALTIME) {
8386#ifdef CLOCK_MONOTONIC
8387 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8388 c = CLOCK_MONOTONIC;
8393#ifdef CLOCK_PROCESS_CPUTIME_ID
8394 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8395 c = CLOCK_PROCESS_CPUTIME_ID;
8400#ifdef CLOCK_THREAD_CPUTIME_ID
8401 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8402 c = CLOCK_THREAD_CPUTIME_ID;
8410#ifdef HAVE_GETTIMEOFDAY
8415#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8416 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8418 ret = gettimeofday(&tv, 0);
8421 tt.giga_count = tv.tv_sec;
8422 tt.count = (int32_t)tv.tv_usec * 1000;
8423 denominators[num_denominators++] = 1000000000;
8428#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8429 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8432 if (t == (time_t)-1)
8436 denominators[num_denominators++] = 1000000000;
8441#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8442 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8443 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8446 unsigned_clock_t uc;
8448 if (c == (clock_t)-1)
8450 uc = (unsigned_clock_t)c;
8451 tt.count = (int32_t)(uc % 1000000000);
8452 tt.giga_count = (uc / 1000000000);
8453 denominators[num_denominators++] = get_clk_tck();
8459#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8460 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8461 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8462 struct rusage usage;
8464 ret = getrusage(RUSAGE_SELF, &usage);
8467 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8468 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8469 if (1000000 <= usec) {
8473 tt.count = usec * 1000;
8474 denominators[num_denominators++] = 1000000000;
8480#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8481 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8482 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8484 unsigned_clock_t utime, stime;
8485 if (times(&buf) == (clock_t)-1)
8487 utime = (unsigned_clock_t)buf.tms_utime;
8488 stime = (unsigned_clock_t)buf.tms_stime;
8489 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8490 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8491 if (1000000000 <= tt.count) {
8492 tt.count -= 1000000000;
8495 denominators[num_denominators++] = get_clk_tck();
8500#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8501 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8502 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8504 unsigned_clock_t uc;
8507 if (c == (clock_t)-1)
8509 uc = (unsigned_clock_t)c;
8510 tt.count = (int32_t)(uc % 1000000000);
8511 tt.giga_count = uc / 1000000000;
8512 denominators[num_denominators++] = CLOCKS_PER_SEC;
8517 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8518 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8519 uint64_t t = mach_absolute_time();
8520 tt.count = (int32_t)(t % 1000000000);
8521 tt.giga_count = t / 1000000000;
8522 numerators[num_numerators++] = info->numer;
8523 denominators[num_denominators++] = info->denom;
8524 denominators[num_denominators++] = 1000000000;
8529 else if (NUMERIC_CLOCKID) {
8530#if defined(HAVE_CLOCK_GETTIME)
8532 c = NUM2CLOCKID(clk_id);
8534 ret = clock_gettime(c, &ts);
8537 tt.count = (int32_t)ts.tv_nsec;
8538 tt.giga_count = ts.tv_sec;
8539 denominators[num_denominators++] = 1000000000;
8547 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8598 timetick_int_t numerators[2];
8599 timetick_int_t denominators[2];
8600 int num_numerators = 0;
8601 int num_denominators = 0;
8602#ifdef HAVE_CLOCK_GETRES
8607 VALUE clk_id = argv[0];
8610#ifdef CLOCK_REALTIME
8611 if (clk_id == RUBY_CLOCK_REALTIME) {
8617#ifdef CLOCK_MONOTONIC
8618 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8619 c = CLOCK_MONOTONIC;
8624#ifdef CLOCK_PROCESS_CPUTIME_ID
8625 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8626 c = CLOCK_PROCESS_CPUTIME_ID;
8631#ifdef CLOCK_THREAD_CPUTIME_ID
8632 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8633 c = CLOCK_THREAD_CPUTIME_ID;
8638#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8639 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8642 denominators[num_denominators++] = 1000000000;
8647#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8648 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8651 denominators[num_denominators++] = 1000000000;
8656#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8657 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8660 denominators[num_denominators++] = get_clk_tck();
8665#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8666 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8669 denominators[num_denominators++] = 1000000000;
8674#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8675 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8678 denominators[num_denominators++] = get_clk_tck();
8683#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8684 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8687 denominators[num_denominators++] = CLOCKS_PER_SEC;
8692#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8693 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8694 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8697 numerators[num_numerators++] = info->numer;
8698 denominators[num_denominators++] = info->denom;
8699 denominators[num_denominators++] = 1000000000;
8704 else if (NUMERIC_CLOCKID) {
8705#if defined(HAVE_CLOCK_GETRES)
8707 c = NUM2CLOCKID(clk_id);
8709 ret = clock_getres(c, &ts);
8712 tt.count = (int32_t)ts.tv_nsec;
8713 tt.giga_count = ts.tv_sec;
8714 denominators[num_denominators++] = 1000000000;
8722 if (unit ==
ID2SYM(id_hertz)) {
8723 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8726 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8731get_CHILD_STATUS(
ID _x,
VALUE *_y)
8737get_PROCESS_ID(
ID _x,
VALUE *_y)
8785static VALUE rb_mProcUID;
8786static VALUE rb_mProcGID;
8787static VALUE rb_mProcID_Syscall;
8801 rb_gvar_ractor_local(
"$$");
8802 rb_gvar_ractor_local(
"$?");
8857 process_status_dump, process_status_load);
8893#ifdef HAVE_GETPRIORITY
8904#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
8906 VALUE inf = RLIM2NUM(RLIM_INFINITY);
8907#ifdef RLIM_SAVED_MAX
8909 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
8916#ifdef RLIM_SAVED_CUR
8918 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
8959#ifdef RLIMIT_MEMLOCK
8966#ifdef RLIMIT_MSGQUEUE
9032#ifdef RLIMIT_SIGPENDING
9067#if defined(RUBY_CLOCK_REALTIME)
9068#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9069# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9070#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9071# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9073#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9076#elif defined(RUBY_CLOCK_REALTIME)
9080#if defined(RUBY_CLOCK_MONOTONIC)
9081#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9082# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9084#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9087#elif defined(RUBY_CLOCK_MONOTONIC)
9091#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9092#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9093# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9095#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9098#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9102#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9105#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9118#ifdef CLOCK_REALTIME_FAST
9122#ifdef CLOCK_REALTIME_PRECISE
9126#ifdef CLOCK_REALTIME_COARSE
9130#ifdef CLOCK_REALTIME_ALARM
9134#ifdef CLOCK_MONOTONIC_FAST
9138#ifdef CLOCK_MONOTONIC_PRECISE
9142#ifdef CLOCK_MONOTONIC_RAW
9146#ifdef CLOCK_MONOTONIC_RAW_APPROX
9150#ifdef CLOCK_MONOTONIC_COARSE
9154#ifdef CLOCK_BOOTTIME
9158#ifdef CLOCK_BOOTTIME_ALARM
9166#ifdef CLOCK_UPTIME_FAST
9170#ifdef CLOCK_UPTIME_PRECISE
9174#ifdef CLOCK_UPTIME_RAW
9178#ifdef CLOCK_UPTIME_RAW_APPROX
9194#if defined(HAVE_TIMES) || defined(_WIN32)
9208 SAVED_USER_ID = geteuid();
9209 SAVED_GROUP_ID = getegid();
9232#ifdef p_uid_from_name
9235#ifdef p_gid_from_name
9266#define define_id(name) id_##name = rb_intern_const(#name)
9279 define_id(new_pgroup);
9281 define_id(unsetenv_others);
9284 define_id(close_others);
9285 define_id(nanosecond);
9286 define_id(microsecond);
9287 define_id(millisecond);
9289 define_id(float_microsecond);
9290 define_id(float_millisecond);
9291 define_id(float_second);
9292 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9293 define_id(TIME_BASED_CLOCK_REALTIME);
9294#ifdef CLOCK_REALTIME
9295 define_id(CLOCK_REALTIME);
9297#ifdef CLOCK_MONOTONIC
9298 define_id(CLOCK_MONOTONIC);
9300#ifdef CLOCK_PROCESS_CPUTIME_ID
9301 define_id(CLOCK_PROCESS_CPUTIME_ID);
9303#ifdef CLOCK_THREAD_CPUTIME_ID
9304 define_id(CLOCK_THREAD_CPUTIME_ID);
9307 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9308 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9311 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9313 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9315 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define GIDT2NUM
Converts a C's gid_t into an instance of rb_cInteger.
#define NUM2GIDT
Converts an instance of rb_cNumeric into C's gid_t.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
Defines a top-level module.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define T_FILE
Old name of RUBY_T_FILE.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ISUPPER
Old name of rb_isupper.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define TOUPPER
Old name of rb_toupper.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define ISLOWER
Old name of rb_islower.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
void rb_notimplement(void)
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eSystemExit
SystemExit exception.
void rb_sys_fail(const char *mesg)
Converts a C errno into a Ruby exception, then raises it.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eRuntimeError
RuntimeError exception.
void * rb_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 always regardless of runtime -W flag.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eArgError
ArgumentError exception.
void rb_sys_fail_str(VALUE mesg)
Identical to rb_sys_fail(), except it takes the message in Ruby's String instead of C's.
void rb_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.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_f_abort(int argc, const VALUE *argv)
This is similar to rb_f_exit().
VALUE rb_f_exit(int argc, const VALUE *argv)
Identical to rb_exit(), except how arguments are passed.
VALUE rb_io_puts(int argc, const VALUE *argv, VALUE io)
Iterates over the passed array to apply rb_io_write() individually.
int rb_cloexec_dup2(int oldfd, int newfd)
Identical to rb_cloexec_dup(), except you can specify the destination file descriptor.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
Closes everything.
int rb_reserved_fd_p(int fd)
Queries if the given FD is reserved or not.
int rb_pipe(int *pipes)
This is an rb_cloexec_pipe() + rb_update_max_fd() combo.
int rb_cloexec_fcntl_dupfd(int fd, int minfd)
Duplicates a file descriptor with closing on exec.
int rb_cloexec_dup(int oldfd)
Identical to rb_cloexec_fcntl_dupfd(), except it implies minfd is 3.
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.
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.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
#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_attr_get(VALUE obj, ID name)
Identical to rb_ivar_get()
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
int rb_io_modestr_oflags(const char *modestr)
Identical to rb_io_modestr_fmode(), except it returns a mixture of O_ flags.
#define GetOpenFile
This is an old name of RB_IO_POINTER.
VALUE rb_io_check_io(VALUE io)
Try converting an object to its IO representation using its to_io method, if any.
VALUE rb_ractor_stderr(void)
Queries the standard error of the current Ractor that is calling this function.
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield(VALUE val)
Yields the block.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
VALUE rb_thread_create(type *q, void *w)
Creates a rb_cThread instance.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define PIDT2NUM
Converts a C's pid_t into an instance of rb_cInteger.
#define NUM2PIDT
Converts an instance of rb_cNumeric into C's pid_t.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define DATA_PTR(obj)
Convenient getter macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define SafeStringValue(v)
#define StringValue(v)
Ensures that the parameter object is a String.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define InitVM(ext)
This macro is for internal use.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
VALUE rb_fiber_scheduler_kernel_sleepv(VALUE scheduler, int argc, VALUE *argv)
Identical to rb_fiber_scheduler_kernel_sleep(), except it can pass multiple arguments.
VALUE rb_fiber_scheduler_process_wait(VALUE scheduler, rb_pid_t pid, int flags)
Non-blocking waitpid.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
Ruby's IO, metadata and buffers.
VALUE tied_io_for_writing
Duplex IO object, if set.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
#define UIDT2NUM
Converts a C's uid_t into an instance of rb_cInteger.
#define NUM2UIDT
Converts an instance of rb_cNumeric into C's uid_t.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.