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;
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)
1789 rb_threadptr_pending_interrupt_clear(GET_THREAD());
1794#if defined(HAVE_WORKING_FORK)
1797#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1799exec_with_sh(
const char *prog,
char **argv,
char **envp)
1801 *argv = (
char *)prog;
1802 *--argv = (
char *)
"sh";
1804 execve(
"/bin/sh", argv, envp);
1806 execv(
"/bin/sh", argv);
1810#define try_with_sh(err, prog, argv, envp) (void)0
1815proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1823 argv = ARGVSTR2ARGV(argv_str);
1830 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1833 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1835 execve(prog, argv, envp);
1839 try_with_sh(err, prog, argv, envp);
1846proc_exec_sh(
const char *str,
VALUE envp_str)
1851 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1859 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1860#elif defined(__CYGWIN32__)
1862 char fbuf[MAXPATHLEN];
1863 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
1866 execl(shell,
"sh",
"-c", str, (
char *) NULL);
1868 status = system(str);
1874 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1876 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1886 ret = proc_exec_sh(str,
Qfalse);
1893mark_exec_arg(
void *ptr)
1896 if (eargp->use_shell)
1897 rb_gc_mark(eargp->invoke.sh.shell_script);
1899 rb_gc_mark(eargp->invoke.cmd.command_name);
1900 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1901 rb_gc_mark(eargp->invoke.cmd.argv_str);
1902 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1904 rb_gc_mark(eargp->redirect_fds);
1905 rb_gc_mark(eargp->envp_str);
1906 rb_gc_mark(eargp->envp_buf);
1907 rb_gc_mark(eargp->dup2_tmpbuf);
1908 rb_gc_mark(eargp->rlimit_limits);
1909 rb_gc_mark(eargp->fd_dup2);
1910 rb_gc_mark(eargp->fd_close);
1911 rb_gc_mark(eargp->fd_open);
1912 rb_gc_mark(eargp->fd_dup2_child);
1913 rb_gc_mark(eargp->env_modification);
1914 rb_gc_mark(eargp->path_env);
1915 rb_gc_mark(eargp->chdir_dir);
1919memsize_exec_arg(
const void *ptr)
1927 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1931# define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1933#ifdef DEFAULT_PROCESS_ENCODING
1934# define EXPORT_STR(str) rb_str_export_to_enc((str), DEFAULT_PROCESS_ENCODING)
1935# define EXPORT_DUP(str) export_dup(str)
1937export_dup(
VALUE str)
1939 VALUE newstr = EXPORT_STR(str);
1944# define EXPORT_STR(str) (str)
1945# define EXPORT_DUP(str) rb_str_dup(str)
1948#if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1949# define USE_SPAWNV 1
1951# define USE_SPAWNV 0
1954# define P_NOWAIT _P_NOWAIT
1959#define proc_spawn_cmd_internal(argv, prog) rb_w32_uaspawn(P_NOWAIT, (prog), (argv))
1962proc_spawn_cmd_internal(
char **argv,
char *prog)
1964 char fbuf[MAXPATHLEN];
1969 prog = dln_find_exe_r(prog, 0, fbuf,
sizeof(fbuf));
1974 status = spawnv(P_NOWAIT, prog, (
const char **)argv);
1975 if (status == -1 && errno == ENOEXEC) {
1976 *argv = (
char *)prog;
1977 *--argv = (
char *)
"sh";
1978 status = spawnv(P_NOWAIT,
"/bin/sh", (
const char **)argv);
1980 if (status == -1) errno = ENOEXEC;
1994 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1995 flags = CREATE_NEW_PROCESS_GROUP;
1997 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ?
RSTRING_PTR(prog) : 0, argv, flags);
1999 pid = proc_spawn_cmd_internal(argv, prog ?
RSTRING_PTR(prog) : 0);
2006#define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
2009proc_spawn_sh(
char *str)
2011 char fbuf[MAXPATHLEN];
2014 char *shell = dln_find_exe_r(
"sh", 0, fbuf,
sizeof(fbuf));
2016 status = spawnl(P_NOWAIT, (shell ? shell :
"/bin/sh"),
"sh",
"-c", str, (char*)NULL);
2026 RBASIC_CLEAR_CLASS(obj);
2031check_exec_redirect_fd(
VALUE v,
int iskey)
2042 else if (
id == id_out)
2044 else if (
id == id_err)
2063 else if (fd >= 3 && iskey) {
2078 ary = hide_obj(rb_ary_new());
2081 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
2082 rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
2088 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
2089 rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
2099 VALUE path, flags, perm;
2103 switch (
TYPE(val)) {
2106 if (
id == id_close) {
2108 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
2110 else if (
id == id_in) {
2112 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2114 else if (
id == id_out) {
2116 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2118 else if (
id == id_err) {
2120 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2130 val = check_exec_redirect_fd(val, 0);
2134 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
2138 path = rb_ary_entry(val, 0);
2140 path ==
ID2SYM(id_child)) {
2141 param = check_exec_redirect_fd(rb_ary_entry(val, 1), 0);
2142 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
2146 flags = rb_ary_entry(val, 1);
2153 perm = rb_ary_entry(val, 2);
2155 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2156 flags, perm,
Qnil));
2157 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2165 key = check_exec_redirect_fd(key, 1);
2167 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2172 VALUE fd = check_exec_redirect_fd(v, 1);
2176 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
2183 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
2184 flags, perm,
Qnil));
2185 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
2191 if (!
NIL_P(val))
goto io;
2197#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2198static int rlimit_type_by_sym(
VALUE key);
2201rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
2203 VALUE ary = eargp->rlimit_limits;
2204 VALUE tmp, softlim, hardlim;
2205 if (eargp->rlimit_limits ==
Qfalse)
2206 ary = eargp->rlimit_limits = hide_obj(rb_ary_new());
2208 ary = eargp->rlimit_limits;
2209 tmp = rb_check_array_type(val);
2212 softlim = hardlim =
rb_to_int(rb_ary_entry(tmp, 0));
2214 softlim =
rb_to_int(rb_ary_entry(tmp, 0));
2215 hardlim =
rb_to_int(rb_ary_entry(tmp, 1));
2225 rb_ary_push(ary, tmp);
2229#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
2233 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2237 switch (
TYPE(key)) {
2239#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2241 int rtype = rlimit_type_by_sym(key);
2243 rb_execarg_addopt_rlimit(eargp, rtype, val);
2251 if (
id == id_pgroup) {
2253 if (eargp->pgroup_given) {
2258 else if (val ==
Qtrue)
2266 eargp->pgroup_given = 1;
2267 eargp->pgroup_pgid = pgroup;
2272 if (
id == id_new_pgroup) {
2273 if (eargp->new_pgroup_given) {
2276 eargp->new_pgroup_given = 1;
2277 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2281 if (
id == id_unsetenv_others) {
2282 if (eargp->unsetenv_others_given) {
2285 eargp->unsetenv_others_given = 1;
2286 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2288 else if (
id == id_chdir) {
2289 if (eargp->chdir_given) {
2293 val = rb_str_encode_ospath(val);
2294 eargp->chdir_given = 1;
2295 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2297 else if (
id == id_umask) {
2299 if (eargp->umask_given) {
2302 eargp->umask_given = 1;
2303 eargp->umask_mask = cmask;
2305 else if (
id == id_close_others) {
2306 if (eargp->close_others_given) {
2309 eargp->close_others_given = 1;
2310 eargp->close_others_do = TO_BOOL(val,
"close_others");
2312 else if (
id == id_in) {
2316 else if (
id == id_out) {
2320 else if (
id == id_err) {
2324 else if (
id == id_uid) {
2326 if (eargp->uid_given) {
2331 eargp->uid = OBJ2UID(val);
2332 eargp->uid_given = 1;
2336 "uid option is unimplemented on this machine");
2339 else if (
id == id_gid) {
2341 if (eargp->gid_given) {
2346 eargp->gid = OBJ2GID(val);
2347 eargp->gid_given = 1;
2351 "gid option is unimplemented on this machine");
2354 else if (
id == id_exception) {
2355 if (eargp->exception_given) {
2358 eargp->exception_given = 1;
2359 eargp->exception = TO_BOOL(val,
"exception");
2370 check_exec_redirect(key, val, eargp);
2382check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2387 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2397check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2402 VALUE execarg_obj = args[0];
2403 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2404 VALUE nonopts = args[1];
2405 if (
NIL_P(nonopts)) args[1] = nonopts = rb_hash_new();
2406 rb_hash_aset(nonopts, key, val);
2423 if (ary == eargp->fd_dup2)
2425 else if (ary == eargp->fd_dup2_child)
2431 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2444 VALUE h = rb_hash_new();
2449 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2450 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2451 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2453 if (eargp->fd_dup2_child) {
2454 ary = eargp->fd_dup2_child;
2464 val = rb_hash_lookup(h, val);
2471 if (oldfd != lastfd) {
2473 rb_ary_store(elt, 1,
INT2FIX(lastfd));
2476 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2477 rb_hash_aset(h, val,
INT2FIX(lastfd));
2484 eargp->close_others_maxhint = maxhint;
2489rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2493 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2497rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2502 args[0] = execarg_obj;
2504 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2508#ifdef ENV_IGNORECASE
2509#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2511#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2515check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2530 key = EXPORT_STR(key);
2531 if (!
NIL_P(val)) val = EXPORT_STR(val);
2536 rb_ary_push(env, hide_obj(rb_assoc_new(key, val)));
2546 env[0] = hide_obj(rb_ary_new());
2548 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2555rb_check_argv(
int argc,
VALUE *argv)
2563 tmp = rb_check_array_type(argv[0]);
2574 for (i = 0; i < argc; i++) {
2583check_hash(
VALUE obj)
2593 return rb_check_hash_type(obj);
2597rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2602 hash = check_hash((*argv_p)[*argc_p-1]);
2604 *opthash_ret = hash;
2610 hash = check_hash((*argv_p)[0]);
2617 prog = rb_check_argv(*argc_p, *argv_p);
2619 prog = (*argv_p)[0];
2620 if (accept_shell && *argc_p == 1) {
2635compare_posix_sh(
const void *key,
const void *el)
2638 int ret = strncmp(word->ptr, el, word->len);
2639 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2647 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2648 char fbuf[MAXPATHLEN];
2652 if (!
NIL_P(opthash)) {
2653 rb_check_exec_options(opthash, execarg_obj);
2656 env = rb_check_exec_env(env, &eargp->path_env);
2657 eargp->env_modification = env;
2660 prog = EXPORT_STR(prog);
2661 eargp->use_shell = argc == 0;
2662 if (eargp->use_shell)
2663 eargp->invoke.sh.shell_script = prog;
2665 eargp->invoke.cmd.command_name = prog;
2668 if (eargp->use_shell) {
2669 static const char posix_sh_cmds[][9] = {
2728 if (*p ==
' ' || *p ==
'\t') {
2729 if (first.ptr && !first.len) first.len = p - first.ptr;
2732 if (!first.ptr) first.ptr = p;
2734 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2740 else if (*p ==
'/') {
2747 if (!has_meta && first.ptr) {
2748 if (!first.len) first.len = p - first.ptr;
2749 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2750 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2755 eargp->use_shell = 0;
2757 if (!eargp->use_shell) {
2762 while (*p ==
' ' || *p ==
'\t')
2766 while (*p && *p !=
' ' && *p !=
'\t')
2772 eargp->invoke.cmd.argv_buf = argv_buf;
2773 eargp->invoke.cmd.command_name =
2775 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2780 if (!eargp->use_shell) {
2781 const char *abspath;
2782 const char *path_env = 0;
2784 abspath = dln_find_exe_r(
RSTRING_PTR(eargp->invoke.cmd.command_name),
2785 path_env, fbuf,
sizeof(fbuf));
2789 eargp->invoke.cmd.command_abspath =
Qnil;
2792 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2797 for (i = 0; i < argc; i++) {
2798 VALUE arg = argv[i];
2800#ifdef DEFAULT_PROCESS_ENCODING
2801 arg = EXPORT_STR(arg);
2806 eargp->invoke.cmd.argv_buf = argv_buf;
2809 if (!eargp->use_shell) {
2810 const char *p, *ep, *null=NULL;
2821 eargp->invoke.cmd.argv_str =
2822 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(argv_str);
2828rb_execarg_get(
VALUE execarg_obj)
2836rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2838 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2844 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2845 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2847 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2853rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2858 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2859 if (!allow_exc_opt && eargp->exception_given) {
2866rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2868 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2869 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2870 eargp->env_modification = env;
2874fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2889static long run_exec_dup2_tmpbuf_size(
long n);
2904 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2910rb_execarg_allocate_dup2_tmpbuf(
struct rb_execarg *eargp,
long len)
2912 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2913 rb_imemo_tmpbuf_set_ptr(tmpbuf, ruby_xmalloc(run_exec_dup2_tmpbuf_size(len)));
2914 eargp->dup2_tmpbuf = tmpbuf;
2918rb_execarg_parent_start1(
VALUE execarg_obj)
2920 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2921 int unsetenv_others;
2925 ary = eargp->fd_open;
2940 open_data.fname = vpath;
2941 open_data.oflags = flags;
2942 open_data.perm = perm;
2944 open_data.err = EINTR;
2946 if (open_data.ret == -1) {
2947 if (open_data.err == EINTR) {
2953 fd2 = open_data.ret;
2965 eargp->redirect_fds = check_exec_fds(eargp);
2967 ary = eargp->fd_dup2;
2969 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2972 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2973 envopts = eargp->env_modification;
2974 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2975 VALUE envtbl, envp_str, envp_buf;
2977 if (unsetenv_others) {
2978 envtbl = rb_hash_new();
2981 envtbl = rb_env_to_hash();
2985 st_table *stenv = RHASH_TBL_RAW(envtbl);
2992 st_data_t stkey = (st_data_t)key;
2993 st_delete(stenv, &stkey, NULL);
2996 st_insert(stenv, (st_data_t)key, (st_data_t)val);
3004 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
3016 rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(envp_str);
3017 eargp->envp_buf = envp_buf;
3033rb_execarg_parent_start(
VALUE execarg_obj)
3036 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
3038 rb_execarg_parent_end(execarg_obj);
3044execarg_parent_end(
VALUE execarg_obj)
3046 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
3050 ary = eargp->fd_open;
3061 parent_redirect_close(fd2);
3072rb_execarg_parent_end(
VALUE execarg_obj)
3074 execarg_parent_end(execarg_obj);
3079rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
3081 if (!errmsg || !*errmsg)
return;
3082 if (strcmp(errmsg,
"chdir") == 0) {
3090rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
3092 if (!errmsg || !*errmsg)
return;
3093 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
3101 VALUE execarg_obj, fail_str;
3103#define CHILD_ERRMSG_BUFLEN 80
3104 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
3107 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
3108 eargp = rb_execarg_get(execarg_obj);
3109 if (mjit_enabled) mjit_finish(
false);
3112 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
3114 execarg_parent_end(execarg_obj);
3119 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
3121 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
3124 rb_exec_fail(eargp, err, errmsg);
3214#define ERRMSG(str) do { if (errmsg && 0 < errmsg_buflen) strlcpy(errmsg, (str), errmsg_buflen); } while (0)
3215#define ERRMSG1(str, a) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a)); } while (0)
3216#define ERRMSG2(str, a, b) do { if (errmsg && 0 < errmsg_buflen) snprintf(errmsg, errmsg_buflen, (str), (a), (b)); } while (0)
3218static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3219static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3220static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3223save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3226 VALUE newary, redirection;
3227 int save_fd = redirect_cloexec_dup(fd), cloexec;
3228 if (save_fd == -1) {
3235 newary = sargp->fd_dup2;
3237 newary = hide_obj(rb_ary_new());
3238 sargp->fd_dup2 = newary;
3240 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3241 redirection = hide_obj(rb_assoc_new(
INT2FIX(fd),
INT2FIX(save_fd)));
3242 if (cloexec) rb_ary_push(redirection,
Qtrue);
3243 rb_ary_push(newary, redirection);
3245 newary = sargp->fd_close;
3247 newary = hide_obj(rb_ary_new());
3248 sargp->fd_close = newary;
3250 rb_ary_push(newary, hide_obj(rb_assoc_new(
INT2FIX(save_fd),
Qnil)));
3257intcmp(
const void *a,
const void *b)
3259 return *(
int*)a - *(
int*)b;
3263intrcmp(
const void *a,
const void *b)
3265 return *(
int*)b - *(
int*)a;
3277run_exec_dup2_tmpbuf_size(
long n)
3284fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3288 ret = fcntl(fd, F_GETFD);
3290 ERRMSG(
"fcntl(F_GETFD)");
3293 if (ret & FD_CLOEXEC)
return 1;
3300fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3304 ret = fcntl(fd, F_GETFD);
3306 ERRMSG(
"fcntl(F_GETFD)");
3309 if (!(ret & FD_CLOEXEC)) {
3311 ret = fcntl(fd, F_SETFD, ret);
3313 ERRMSG(
"fcntl(F_SETFD)");
3323fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3327 ret = fcntl(fd, F_GETFD);
3329 ERRMSG(
"fcntl(F_GETFD)");
3332 if (ret & FD_CLOEXEC) {
3334 ret = fcntl(fd, F_SETFD, ret);
3336 ERRMSG(
"fcntl(F_SETFD)");
3346run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3357 for (i = 0; i < n; i++) {
3362 pairs[i].older_index = -1;
3372 for (i = 0; i < n; i++) {
3373 int newfd = pairs[i].newfd;
3377 pairs[i].num_newer = 0;
3379 while (pairs < found && (found-1)->oldfd == newfd)
3381 while (found < pairs+n && found->oldfd == newfd) {
3382 pairs[i].num_newer++;
3383 found->older_index = i;
3390 for (i = 0; i < n; i++) {
3392 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3393 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3395 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3400 if (pairs[j].cloexec &&
3401 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3405 pairs[j].oldfd = -1;
3406 j = pairs[j].older_index;
3408 pairs[j].num_newer--;
3413 for (i = 0; i < n; i++) {
3415 if (pairs[i].oldfd == -1)
3417 if (pairs[i].oldfd == pairs[i].newfd) {
3418 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3420 pairs[i].oldfd = -1;
3423 if (extra_fd == -1) {
3424 extra_fd = redirect_dup(pairs[i].oldfd);
3425 if (extra_fd == -1) {
3432 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3439 pairs[i].oldfd = extra_fd;
3440 j = pairs[i].older_index;
3441 pairs[i].older_index = -1;
3443 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3449 pairs[j].oldfd = -1;
3450 j = pairs[j].older_index;
3453 if (extra_fd != -1) {
3454 ret = redirect_close(extra_fd);
3469run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3477 ret = redirect_close(fd);
3488run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3498 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3500 ret = redirect_dup2(oldfd, newfd);
3513run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3525 pgroup = eargp->pgroup_pgid;
3531 sargp->pgroup_given = 1;
3532 sargp->pgroup_pgid = getpgrp();
3538 ret = setpgid(getpid(), pgroup);
3539 if (ret == -1) ERRMSG(
"setpgid");
3544#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3547run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3556 if (getrlimit(rtype, &rlim) == -1) {
3557 ERRMSG(
"getrlimit");
3561 RLIM2NUM(rlim.rlim_cur),
3562 RLIM2NUM(rlim.rlim_max)));
3563 if (sargp->rlimit_limits ==
Qfalse)
3564 newary = sargp->rlimit_limits = hide_obj(rb_ary_new());
3566 newary = sargp->rlimit_limits;
3567 rb_ary_push(newary, tmp);
3571 if (setrlimit(rtype, &rlim) == -1) {
3572 ERRMSG(
"setrlimit");
3580#if !defined(HAVE_WORKING_FORK)
3584 rb_ary_push(ary, hide_obj(rb_ary_dup(argv[0])));
3593 if (sargp->env_modification ==
Qfalse) {
3594 VALUE env = rb_envtbl();
3596 VALUE ary = hide_obj(rb_ary_new());
3599 sargp->env_modification = ary;
3601 sargp->unsetenv_others_given = 1;
3602 sargp->unsetenv_others_do = 1;
3609#define chdir(p) rb_w32_uchdir(p)
3614rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3621 sargp->redirect_fds =
Qnil;
3625 if (eargp->pgroup_given) {
3626 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3631#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3632 obj = eargp->rlimit_limits;
3634 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3639#if !defined(HAVE_WORKING_FORK)
3640 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3645 obj = eargp->env_modification;
3661 if (eargp->umask_given) {
3662 mode_t mask = eargp->umask_mask;
3663 mode_t oldmask = umask(mask);
3665 sargp->umask_given = 1;
3666 sargp->umask_mask = oldmask;
3670 obj = eargp->fd_dup2;
3672 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3676 obj = eargp->fd_close;
3679 rb_warn(
"cannot close fd before spawn");
3681 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3686#ifdef HAVE_WORKING_FORK
3687 if (eargp->close_others_do) {
3692 obj = eargp->fd_dup2_child;
3694 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3698 if (eargp->chdir_given) {
3700 sargp->chdir_given = 1;
3701 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3710 if (eargp->gid_given) {
3711 if (setgid(eargp->gid) < 0) {
3718 if (eargp->uid_given) {
3719 if (setuid(eargp->uid) < 0) {
3727 VALUE ary = sargp->fd_dup2;
3729 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3733 int preserve = errno;
3734 stdfd_clear_nonblock();
3743rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3745 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3750exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3752#if !defined(HAVE_WORKING_FORK)
3753 struct rb_execarg sarg, *
const sargp = &sarg;
3759 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3763 if (eargp->use_shell) {
3764 err = proc_exec_sh(
RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3767 char *abspath = NULL;
3768 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3769 abspath =
RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3770 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3772#if !defined(HAVE_WORKING_FORK)
3773 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3779#ifdef HAVE_WORKING_FORK
3782rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3784 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3788proc_syswait(
VALUE pid)
3795move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3799 for (i = 0; i < n; i++) {
3818pipe_nocrash(
int filedes[2],
VALUE fds)
3826 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3841rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3848handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3854 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3860#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3863 if (!status && !ep) {
3868 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3869 if (status) status->status = state;
3870 if (!state)
return 0;
3879 if (state && !status) rb_jump_tag(state);
3883#define prefork() ( \
3884 rb_io_flush(rb_stdout), \
3885 rb_io_flush(rb_stderr) \
3915write_retry(
int fd,
const void *buf,
size_t len)
3920 w = write(fd, buf, len);
3921 }
while (w < 0 && errno == EINTR);
3927read_retry(
int fd,
void *buf,
size_t len)
3931 if (set_blocking(fd) != 0) {
3933 rb_async_bug_errno(
"set_blocking failed reading child error", errno);
3938 r = read(fd, buf, len);
3939 }
while (r < 0 && errno == EINTR);
3945send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3950 if (write_retry(fd, &err,
sizeof(err)) < 0) err = errno;
3951 if (errmsg && 0 < errmsg_buflen) {
3952 errmsg[errmsg_buflen-1] =
'\0';
3953 errmsg_buflen = strlen(errmsg);
3954 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3960recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3964 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3968 if (size ==
sizeof(err) &&
3969 errmsg && 0 < errmsg_buflen) {
3970 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3979#ifdef HAVE_WORKING_VFORK
3980#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3983getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3989 ret = getuidx(ID_SAVED);
3990 if (ret == (rb_uid_t)-1)
3995#define HAVE_GETRESUID
3998#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
4001getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
4007 ret = getgidx(ID_SAVED);
4008 if (ret == (rb_gid_t)-1)
4013#define HAVE_GETRESGID
4031 rb_uid_t ruid, euid;
4032 rb_gid_t rgid, egid;
4034#if defined HAVE_ISSETUGID
4039#ifdef HAVE_GETRESUID
4043 ret = getresuid(&ruid, &euid, &suid);
4054 if (euid == 0 || euid != ruid)
4057#ifdef HAVE_GETRESGID
4061 ret = getresgid(&rgid, &egid, &sgid);
4079struct child_handler_disabler_state
4085disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
4087#ifdef HAVE_PTHREAD_SIGMASK
4091 ret = sigfillset(&all);
4095 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
4100# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4105disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
4107#ifdef HAVE_PTHREAD_SIGMASK
4110 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
4115# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
4121disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
4126 for (sig = 1; sig < NSIG; sig++) {
4127 sig_t handler = signal(sig, SIG_DFL);
4129 if (handler == SIG_ERR && errno == EINVAL) {
4132 if (handler == SIG_ERR) {
4133 ERRMSG(
"signal to obtain old action");
4137 if (sig == SIGPIPE) {
4142 if (handler == SIG_IGN) {
4143 signal(sig, SIG_IGN);
4148 sigemptyset(&old->sigmask);
4149 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
4151 ERRMSG(
"sigprocmask");
4159 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4160 char *errmsg,
size_t errmsg_buflen,
4164 volatile int try_gc = 1;
4165 struct child_handler_disabler_state old;
4167 rb_nativethread_lock_t *
const volatile waitpid_lock_init =
4168 (w && WAITPID_USE_SIGCHLD) ? &GET_VM()->waitpid_lock : 0;
4171 rb_nativethread_lock_t *waitpid_lock = waitpid_lock_init;
4173 disable_child_handler_before_fork(&old);
4177#ifdef HAVE_WORKING_VFORK
4178 if (!has_privilege())
4188 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
4190 ret = chfunc(charg, errmsg, errmsg_buflen);
4191 if (!ret) _exit(EXIT_SUCCESS);
4193 send_child_error(ep[1], errmsg, errmsg_buflen);
4194#if EXIT_SUCCESS == 127
4195 _exit(EXIT_FAILURE);
4201 waitpid_lock = waitpid_lock_init;
4203 if (pid > 0 && w != WAITPID_LOCK_ONLY) {
4205 ccan_list_add(&GET_VM()->waiting_pids, &w->wnode);
4209 disable_child_handler_fork_parent(&old);
4213 if (handle_fork_error(err, status, ep, &try_gc))
4223 struct child_handler_disabler_state old;
4226 disable_child_handler_before_fork(&old);
4230 pid_t pid = rb_fork();
4231 if (pid > 0) mjit_add_waiting_pid(vm, pid);
4235 disable_child_handler_fork_parent(&old);
4243fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4244 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4252 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4254 if (status) status->status = 0;
4256 if (pipe_nocrash(ep, fds))
return -1;
4258 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4260 if (status) status->pid = pid;
4263 if (status) status->error = errno;
4270 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4272 if (error_occurred) {
4275 status->error = err;
4277 VM_ASSERT((w == 0 || w == WAITPID_LOCK_ONLY) &&
4278 "only used by extensions");
4279 rb_protect(proc_syswait, (
VALUE)pid, &state);
4281 status->status = state;
4283 else if (!w || w == WAITPID_LOCK_ONLY) {
4302rb_fork_async_signal_safe(
int *status,
4303 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4304 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4308 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4311 *status = process_status.status;
4321 int try_gc = 1, err;
4322 struct child_handler_disabler_state old;
4324 if (status) status->status = 0;
4328 if (mjit_enabled) mjit_pause(
false);
4329 disable_child_handler_before_fork(&old);
4335 status->error = err;
4338 disable_child_handler_fork_parent(&old);
4340 if (mjit_enabled && pid > 0) mjit_resume();
4348 if (handle_fork_error(err, status, NULL, &try_gc)) {
4355rb_fork_ruby(
int *status)
4359 rb_pid_t pid = rb_fork_ruby2(&process_status);
4361 if (status) *status = process_status.status;
4369 rb_pid_t pid = rb_fork_ruby(NULL);
4379rb_call_proc__fork(
void)
4384 return proc_fork_pid();
4393#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4414rb_proc__fork(
VALUE _obj)
4416 rb_pid_t pid = proc_fork_pid();
4450 pid = rb_call_proc__fork();
4464#define rb_proc__fork rb_f_notimplement
4465#define rb_f_fork rb_f_notimplement
4469exit_status_code(
VALUE status)
4475 istatus = EXIT_SUCCESS;
4478 istatus = EXIT_FAILURE;
4482#if EXIT_SUCCESS != 0
4484 istatus = EXIT_SUCCESS;
4491NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4504rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4509 istatus = exit_status_code(argv[0]);
4512 istatus = EXIT_FAILURE;
4522 if (GET_EC()->tag) {
4538 istatus = exit_status_code(argv[0]);
4541 istatus = EXIT_SUCCESS;
4603 VALUE errinfo = rb_ec_get_errinfo(ec);
4604 if (!
NIL_P(errinfo)) {
4605 rb_ec_error_print(ec, errinfo);
4612 args[1] = args[0] = argv[0];
4615 args[0] =
INT2NUM(EXIT_FAILURE);
4650#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4655 if (eargp && !eargp->use_shell) {
4656 VALUE str = eargp->invoke.cmd.argv_str;
4657 VALUE buf = eargp->invoke.cmd.argv_buf;
4658 char *p, **argv = ARGVSTR2ARGV(str);
4659 long i, argc = ARGVSTR2ARGC(str);
4663 for (i = 1; i < argc; ++i) {
4664 p[argv[i] - start - 1] =
' ';
4674rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4677#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4680# if !defined HAVE_SPAWNV
4685#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4686 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4688 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4690 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4694 if (prog && !eargp->use_shell) {
4695 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4698# if defined HAVE_SPAWNV
4699 if (eargp->use_shell) {
4703 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4704 pid = proc_spawn_cmd(argv, prog, eargp);
4711 status = system(rb_execarg_commandline(eargp, &prog));
4716 if (eargp->waitpid_state && eargp->waitpid_state != WAITPID_LOCK_ONLY) {
4717 eargp->waitpid_state->pid = pid;
4720 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4735do_spawn_process(
VALUE arg)
4738 rb_execarg_parent_start1(argp->execarg);
4740 argp->errmsg.ptr, argp->errmsg.buflen);
4744rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4747 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4753 if (!eargp->waitpid_state && mjit_enabled) {
4754 eargp->waitpid_state = WAITPID_LOCK_ONLY;
4757 args.execarg = execarg_obj;
4758 args.errmsg.ptr = errmsg;
4759 args.errmsg.buflen = errmsg_buflen;
4761 execarg_parent_end, execarg_obj);
4765rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4769 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4770 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4776 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4782 return rb_spawn_internal(argc, argv, NULL, 0);
4842 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4843 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4846 eargp->status = &status;
4848 rb_last_status_clear();
4852 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4855 VALUE status = rb_process_status_wait(pid, 0);
4861 GET_THREAD()->last_status = status;
4863 if (data->status == EXIT_SUCCESS) {
4867 if (data->error != 0) {
4868 if (eargp->exception) {
4869 VALUE command = eargp->invoke.sh.shell_script;
4877 else if (eargp->exception) {
4878 VALUE command = eargp->invoke.sh.shell_script;
4892 if (eargp->exception) {
4893 VALUE command = eargp->invoke.sh.shell_script;
5175 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
5176 VALUE execarg_obj, fail_str;
5179 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
5180 eargp = rb_execarg_get(execarg_obj);
5181 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
5183 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
5187 rb_exec_fail(eargp, err, errmsg);
5191#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
5218 time_t beg = time(0);
5221 if (scheduler !=
Qnil) {
5234 time_t end = time(0) - beg;
5236 return TIMET2NUM(end);
5240#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
5257#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
5268#define proc_getpgrp rb_f_notimplement
5272#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
5290#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
5296#define proc_setpgrp rb_f_notimplement
5300#if defined(HAVE_GETPGID)
5321#define proc_getpgid rb_f_notimplement
5337 rb_pid_t ipid, ipgrp;
5346#define proc_setpgid rb_f_notimplement
5377#define proc_getsid rb_f_notimplement
5381#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5382#if !defined(HAVE_SETSID)
5383static rb_pid_t ruby_setsid(
void);
5384#define setsid() ruby_setsid()
5407#if !defined(HAVE_SETSID)
5408#define HAVE_SETSID 1
5416#if defined(SETPGRP_VOID)
5422 ret = setpgrp(0, pid);
5424 if (ret == -1)
return -1;
5428 ioctl(fd, TIOCNOTTY, NULL);
5435#define proc_setsid rb_f_notimplement
5439#ifdef HAVE_GETPRIORITY
5460 int prio, iwhich, iwho;
5466 prio = getpriority(iwhich, iwho);
5471#define proc_getpriority rb_f_notimplement
5475#ifdef HAVE_GETPRIORITY
5491 int iwhich, iwho, iprio;
5497 if (setpriority(iwhich, iwho, iprio) < 0)
5502#define proc_setpriority rb_f_notimplement
5505#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5507rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5511#define RESCHECK(r) \
5513 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5514 resource = RLIMIT_##r; \
5548#ifdef RLIMIT_MEMLOCK
5551#ifdef RLIMIT_MSGQUEUE
5590#ifdef RLIMIT_SIGPENDING
5591 RESCHECK(SIGPENDING);
5600 for (p = name; *p; p++)
5606 for (p = name; *p; p++)
5612 rb_bug(
"unexpected casetype");
5619rlimit_type_by_hname(
const char *name,
long len)
5621 return rlimit_resource_name2int(name, len, 0);
5625rlimit_type_by_lname(
const char *name,
long len)
5627 return rlimit_resource_name2int(name, len, 1);
5631rlimit_type_by_sym(
VALUE key)
5637 static const char prefix[] =
"rlimit_";
5638 enum {prefix_len =
sizeof(prefix)-1};
5640 if (len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5641 rtype = rlimit_type_by_lname(rname + prefix_len, len - prefix_len);
5649rlimit_resource_type(
VALUE rtype)
5656 switch (
TYPE(rtype)) {
5679 r = rlimit_type_by_hname(name, len);
5689rlimit_resource_value(
VALUE rval)
5694 switch (
TYPE(rval)) {
5712 return NUM2RLIM(rval);
5716 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5718#ifdef RLIM_SAVED_MAX
5719 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5721#ifdef RLIM_SAVED_CUR
5722 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5730#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5756 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5759 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5762#define proc_getrlimit rb_f_notimplement
5765#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5819proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5821 VALUE resource, rlim_cur, rlim_max;
5827 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5828 rlim_max = rlim_cur;
5830 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5831 rlim.rlim_max = rlimit_resource_value(rlim_max);
5833 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5839#define proc_setrlimit rb_f_notimplement
5842static int under_uid_switch = 0;
5844check_uid_switch(
void)
5846 if (under_uid_switch) {
5851static int under_gid_switch = 0;
5853check_gid_switch(
void)
5855 if (under_gid_switch) {
5861#if defined(HAVE_PWD_H)
5870#if ( !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN) )
5873 char MAYBE_UNUSED(*login) = NULL;
5875# ifdef USE_GETLOGIN_R
5877#if defined(__FreeBSD__)
5878 typedef int getlogin_r_size_t;
5880 typedef size_t getlogin_r_size_t;
5883 long loginsize = GETLOGIN_R_SIZE_INIT;
5886 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5896 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5898 if (gle == ENOTTY || gle == ENXIO || gle == ENOENT) {
5903 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5913 if (login == NULL) {
5918 return maybe_result;
5925 if (errno == ENOTTY || errno == ENXIO || errno == ENOENT) {
5938rb_getpwdirnam_for_login(
VALUE login_name)
5940#if ( !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM) )
5944 if (
NIL_P(login_name)) {
5951 struct passwd *pwptr;
5953# ifdef USE_GETPWNAM_R
5955 struct passwd pwdnm;
5957 long bufsizenm = GETPW_R_SIZE_INIT;
5960 bufsizenm = GETPW_R_SIZE_DEFAULT;
5970 while ((enm = getpwnam_r(login, &pwdnm, bufnm, bufsizenm, &pwptr)) != 0) {
5972 if (enm == ENOENT || enm== ESRCH || enm == EBADF || enm == EPERM) {
5978 if (enm != ERANGE || bufsizenm >= GETPW_R_SIZE_LIMIT) {
5988 if (pwptr == NULL) {
6002 pwptr = getpwnam(login);
6009 && ( errno != ENOENT && errno != ESRCH && errno != EBADF && errno != EPERM)) {
6025# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
6029 uid_t ruid = getuid();
6031 struct passwd *pwptr;
6033# ifdef USE_GETPWUID_R
6035 struct passwd pwdid;
6037 long bufsizeid = GETPW_R_SIZE_INIT;
6040 bufsizeid = GETPW_R_SIZE_DEFAULT;
6050 while ((eid = getpwuid_r(ruid, &pwdid, bufid, bufsizeid, &pwptr)) != 0) {
6052 if (eid == ENOENT || eid== ESRCH || eid == EBADF || eid == EPERM) {
6058 if (eid != ERANGE || bufsizeid >= GETPW_R_SIZE_LIMIT) {
6068 if (pwptr == NULL) {
6079# elif defined(USE_GETPWUID)
6082 pwptr = getpwuid(ruid);
6089 && ( errno == ENOENT || errno == ESRCH || errno == EBADF || errno == EPERM)) {
6111#if defined(HAVE_PWD_H)
6114# ifdef USE_GETPWNAM_R
6127 struct passwd *pwptr;
6128#ifdef USE_GETPWNAM_R
6129 struct passwd pwbuf;
6134 getpw_buf_len = GETPW_R_SIZE_INIT;
6135 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
6142 while ((e = getpwnam_r(usrname, &pwbuf, getpw_buf, getpw_buf_len, &pwptr)) != 0) {
6143 if (e != ERANGE || getpw_buf_len >= GETPW_R_SIZE_LIMIT) {
6152 pwptr = getpwnam(usrname);
6155#ifndef USE_GETPWNAM_R
6160 uid = pwptr->pw_uid;
6161#ifndef USE_GETPWNAM_R
6168# ifdef p_uid_from_name
6188#if defined(HAVE_GRP_H)
6191# ifdef USE_GETGRNAM_R
6204 struct group *grptr;
6205#ifdef USE_GETGRNAM_R
6211 getgr_buf_len = GETGR_R_SIZE_INIT;
6212 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6219 while ((e = getgrnam_r(grpname, &grbuf, getgr_buf, getgr_buf_len, &grptr)) != 0) {
6220 if (e != ERANGE || getgr_buf_len >= GETGR_R_SIZE_LIMIT) {
6228#elif defined(HAVE_GETGRNAM)
6229 grptr = getgrnam(grpname);
6234#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6239 gid = grptr->gr_gid;
6240#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6247# ifdef p_gid_from_name
6267#if defined HAVE_SETUID
6285#define p_sys_setuid rb_f_notimplement
6289#if defined HAVE_SETRUID
6307#define p_sys_setruid rb_f_notimplement
6311#if defined HAVE_SETEUID
6329#define p_sys_seteuid rb_f_notimplement
6333#if defined HAVE_SETREUID
6348 rb_uid_t ruid, euid;
6351 ruid = OBJ2UID1(rid);
6352 euid = OBJ2UID1(eid);
6358#define p_sys_setreuid rb_f_notimplement
6362#if defined HAVE_SETRESUID
6377 rb_uid_t ruid, euid, suid;
6380 ruid = OBJ2UID1(rid);
6381 euid = OBJ2UID1(eid);
6382 suid = OBJ2UID1(sid);
6384 if (setresuid(ruid, euid, suid) != 0)
rb_sys_fail(0);
6388#define p_sys_setresuid rb_f_notimplement
6404proc_getuid(
VALUE obj)
6406 rb_uid_t uid = getuid();
6411#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6428#if defined(HAVE_SETRESUID)
6430#elif defined HAVE_SETREUID
6432#elif defined HAVE_SETRUID
6434#elif defined HAVE_SETUID
6436 if (geteuid() == uid) {
6447#define proc_setuid rb_f_notimplement
6461static rb_uid_t SAVED_USER_ID = -1;
6463#ifdef BROKEN_SETREUID
6465setreuid(rb_uid_t ruid, rb_uid_t euid)
6467 if (ruid != (rb_uid_t)-1 && ruid != getuid()) {
6468 if (euid == (rb_uid_t)-1) euid = geteuid();
6469 if (setuid(ruid) < 0)
return -1;
6471 if (euid != (rb_uid_t)-1 && euid != geteuid()) {
6472 if (seteuid(euid) < 0)
return -1;
6500 if (geteuid() == 0) {
6501#if defined(HAVE_SETRESUID)
6503 SAVED_USER_ID = uid;
6504#elif defined(HAVE_SETUID)
6506 SAVED_USER_ID = uid;
6507#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6508 if (getuid() == uid) {
6509 if (SAVED_USER_ID == uid) {
6514 if (setreuid(-1, SAVED_USER_ID) < 0)
rb_sys_fail(0);
6515 if (setreuid(SAVED_USER_ID, 0) < 0)
rb_sys_fail(0);
6518 SAVED_USER_ID = uid;
6524 SAVED_USER_ID = uid;
6530 SAVED_USER_ID = uid;
6532#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6533 if (getuid() == uid) {
6534 if (SAVED_USER_ID == uid) {
6548 SAVED_USER_ID = uid;
6555 SAVED_USER_ID = uid;
6563#if defined(HAVE_SETRESUID)
6564 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6565 (geteuid() == uid)? (rb_uid_t)-1: uid,
6566 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0)
rb_sys_fail(0);
6567 SAVED_USER_ID = uid;
6568#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6569 if (SAVED_USER_ID == uid) {
6570 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6571 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6574 else if (getuid() != uid) {
6575 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6577 SAVED_USER_ID = uid;
6579 else if ( geteuid() != uid) {
6581 SAVED_USER_ID = uid;
6585 if (setreuid(-1, SAVED_USER_ID) < 0)
rb_sys_fail(0);
6586 if (setreuid(SAVED_USER_ID, uid) < 0)
rb_sys_fail(0);
6587 SAVED_USER_ID = uid;
6590#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6591 if (SAVED_USER_ID == uid) {
6592 if (geteuid() != uid && seteuid(uid) < 0)
rb_sys_fail(0);
6593 if (getuid() != uid && setruid(uid) < 0)
rb_sys_fail(0);
6595 else if ( geteuid() == uid) {
6596 if (getuid() != uid) {
6598 SAVED_USER_ID = uid;
6602 SAVED_USER_ID = uid;
6606 else if ( getuid() == uid) {
6609 SAVED_USER_ID = uid;
6615#elif defined HAVE_44BSD_SETUID
6616 if (getuid() == uid) {
6619 SAVED_USER_ID = uid;
6624#elif defined HAVE_SETEUID
6625 if (getuid() == uid && SAVED_USER_ID == uid) {
6631#elif defined HAVE_SETUID
6632 if (getuid() == uid && SAVED_USER_ID == uid) {
6647#if defined HAVE_SETGID
6665#define p_sys_setgid rb_f_notimplement
6669#if defined HAVE_SETRGID
6687#define p_sys_setrgid rb_f_notimplement
6691#if defined HAVE_SETEGID
6709#define p_sys_setegid rb_f_notimplement
6713#if defined HAVE_SETREGID
6728 rb_gid_t rgid, egid;
6730 rgid = OBJ2GID(rid);
6731 egid = OBJ2GID(eid);
6736#define p_sys_setregid rb_f_notimplement
6739#if defined HAVE_SETRESGID
6754 rb_gid_t rgid, egid, sgid;
6756 rgid = OBJ2GID(rid);
6757 egid = OBJ2GID(eid);
6758 sgid = OBJ2GID(sid);
6759 if (setresgid(rgid, egid, sgid) != 0)
rb_sys_fail(0);
6763#define p_sys_setresgid rb_f_notimplement
6767#if defined HAVE_ISSETUGID
6781p_sys_issetugid(
VALUE obj)
6783 return RBOOL(issetugid());
6786#define p_sys_issetugid rb_f_notimplement
6802proc_getgid(
VALUE obj)
6804 rb_gid_t gid = getgid();
6809#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6825#if defined(HAVE_SETRESGID)
6827#elif defined HAVE_SETREGID
6829#elif defined HAVE_SETRGID
6831#elif defined HAVE_SETGID
6833 if (getegid() == gid) {
6844#define proc_setgid rb_f_notimplement
6848#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6868static int _maxgroups = -1;
6870get_sc_ngroups_max(
void)
6872#ifdef _SC_NGROUPS_MAX
6873 return (
int)sysconf(_SC_NGROUPS_MAX);
6874#elif defined(NGROUPS_MAX)
6875 return (
int)NGROUPS_MAX;
6883 if (_maxgroups < 0) {
6884 _maxgroups = get_sc_ngroups_max();
6886 _maxgroups = RB_MAX_GROUPS;
6895#ifdef HAVE_GETGROUPS
6922proc_getgroups(
VALUE obj)
6928 ngroups = getgroups(0, NULL);
6932 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6934 ngroups = getgroups(ngroups, groups);
6939 for (i = 0; i < ngroups; i++)
6940 rb_ary_push(ary,
GIDT2NUM(groups[i]));
6947#define proc_getgroups rb_f_notimplement
6951#ifdef HAVE_SETGROUPS
6976 if (ngroups > maxgroups())
6979 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6981 for (i = 0; i < ngroups; i++) {
6984 groups[i] = OBJ2GID1(g);
6988 if (setgroups(ngroups, groups) == -1)
6993 return proc_getgroups(obj);
6996#define proc_setgroups rb_f_notimplement
7000#ifdef HAVE_INITGROUPS
7024 return proc_getgroups(obj);
7027#define proc_initgroups rb_f_notimplement
7030#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
7042proc_getmaxgroups(
VALUE obj)
7047#define proc_getmaxgroups rb_f_notimplement
7050#ifdef HAVE_SETGROUPS
7063 int ngroups_max = get_sc_ngroups_max();
7068 if (ngroups > RB_MAX_GROUPS)
7069 ngroups = RB_MAX_GROUPS;
7071 if (ngroups_max > 0 && ngroups > ngroups_max)
7072 ngroups = ngroups_max;
7074 _maxgroups = ngroups;
7079#define proc_setmaxgroups rb_f_notimplement
7082#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
7083static int rb_daemon(
int nochdir,
int noclose);
7102 int n, nochdir = FALSE, noclose = FALSE;
7105 case 2: noclose = TO_BOOL(argv[1],
"noclose");
7106 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
7110 n = rb_daemon(nochdir, noclose);
7116rb_daemon(
int nochdir,
int noclose)
7120 if (mjit_enabled) mjit_pause(
false);
7122 err = daemon(nochdir, noclose);
7128 switch (rb_fork_ruby(NULL)) {
7131 default: _exit(EXIT_SUCCESS);
7135 if (setsid() < 0) (void)0;
7140 if (!noclose && (n =
rb_cloexec_open(
"/dev/null", O_RDWR, 0)) != -1) {
7152#define proc_daemon rb_f_notimplement
7165static rb_gid_t SAVED_GROUP_ID = -1;
7167#ifdef BROKEN_SETREGID
7169setregid(rb_gid_t rgid, rb_gid_t egid)
7171 if (rgid != (rb_gid_t)-1 && rgid != getgid()) {
7172 if (egid == (rb_gid_t)-1) egid = getegid();
7173 if (setgid(rgid) < 0)
return -1;
7175 if (egid != (rb_gid_t)-1 && egid != getegid()) {
7176 if (setegid(egid) < 0)
return -1;
7204 if (geteuid() == 0) {
7205#if defined(HAVE_SETRESGID)
7207 SAVED_GROUP_ID = gid;
7208#elif defined HAVE_SETGID
7210 SAVED_GROUP_ID = gid;
7211#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7212 if (getgid() == gid) {
7213 if (SAVED_GROUP_ID == gid) {
7218 if (setregid(-1, SAVED_GROUP_ID) < 0)
rb_sys_fail(0);
7219 if (setregid(SAVED_GROUP_ID, 0) < 0)
rb_sys_fail(0);
7222 SAVED_GROUP_ID = gid;
7228 SAVED_GROUP_ID = gid;
7234 SAVED_GROUP_ID = gid;
7236#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7237 if (getgid() == gid) {
7238 if (SAVED_GROUP_ID == gid) {
7253 SAVED_GROUP_ID = gid;
7260 SAVED_GROUP_ID = gid;
7267#if defined(HAVE_SETRESGID)
7268 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7269 (getegid() == gid)? (rb_gid_t)-1: gid,
7270 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0)
rb_sys_fail(0);
7271 SAVED_GROUP_ID = gid;
7272#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7273 if (SAVED_GROUP_ID == gid) {
7274 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7275 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7278 else if (getgid() != gid) {
7279 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7281 SAVED_GROUP_ID = gid;
7283 else if ( getegid() != gid) {
7285 SAVED_GROUP_ID = gid;
7289 if (setregid(-1, SAVED_GROUP_ID) < 0)
rb_sys_fail(0);
7290 if (setregid(SAVED_GROUP_ID, gid) < 0)
rb_sys_fail(0);
7291 SAVED_GROUP_ID = gid;
7294#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7295 if (SAVED_GROUP_ID == gid) {
7296 if (getegid() != gid && setegid(gid) < 0)
rb_sys_fail(0);
7297 if (getgid() != gid && setrgid(gid) < 0)
rb_sys_fail(0);
7299 else if ( getegid() == gid) {
7300 if (getgid() != gid) {
7302 SAVED_GROUP_ID = gid;
7306 SAVED_GROUP_ID = gid;
7310 else if ( getgid() == gid) {
7313 SAVED_GROUP_ID = gid;
7319#elif defined HAVE_44BSD_SETGID
7320 if (getgid() == gid) {
7323 SAVED_GROUP_ID = gid;
7328#elif defined HAVE_SETEGID
7329 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7335#elif defined HAVE_SETGID
7336 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7363proc_geteuid(
VALUE obj)
7365 rb_uid_t euid = geteuid();
7369#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7371proc_seteuid(rb_uid_t uid)
7373#if defined(HAVE_SETRESUID)
7375#elif defined HAVE_SETREUID
7377#elif defined HAVE_SETEUID
7379#elif defined HAVE_SETUID
7380 if (uid == getuid()) {
7392#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7405 proc_seteuid(OBJ2UID(euid));
7409#define proc_seteuid_m rb_f_notimplement
7413rb_seteuid_core(rb_uid_t euid)
7415#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7421#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7425#if defined(HAVE_SETRESUID)
7428 SAVED_USER_ID = euid;
7433#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7438 SAVED_USER_ID = euid;
7440#elif defined HAVE_SETEUID
7442#elif defined HAVE_SETUID
7469 rb_seteuid_core(OBJ2UID(
id));
7487proc_getegid(
VALUE obj)
7489 rb_gid_t egid = getegid();
7494#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7506#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7512#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7513 gid = OBJ2GID(egid);
7516#if defined(HAVE_SETRESGID)
7518#elif defined HAVE_SETREGID
7520#elif defined HAVE_SETEGID
7522#elif defined HAVE_SETGID
7523 if (gid == getgid()) {
7536#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7537#define proc_setegid_m proc_setegid
7539#define proc_setegid_m rb_f_notimplement
7543rb_setegid_core(rb_gid_t egid)
7545#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7551#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7555#if defined(HAVE_SETRESGID)
7558 SAVED_GROUP_ID = egid;
7563#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7568 SAVED_GROUP_ID = egid;
7570#elif defined HAVE_SETEGID
7572#elif defined HAVE_SETGID
7599 rb_setegid_core(OBJ2GID(
id));
7614p_uid_exchangeable(
VALUE _)
7616#if defined(HAVE_SETRESUID)
7618#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7639p_uid_exchange(
VALUE obj)
7642#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7649#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7653#if defined(HAVE_SETRESUID)
7654 if (setresuid(euid, uid, uid) < 0)
rb_sys_fail(0);
7655 SAVED_USER_ID = uid;
7656#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7658 SAVED_USER_ID = uid;
7676p_gid_exchangeable(
VALUE _)
7678#if defined(HAVE_SETRESGID)
7680#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7701p_gid_exchange(
VALUE obj)
7704#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7711#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7715#if defined(HAVE_SETRESGID)
7716 if (setresgid(egid, gid, gid) < 0)
rb_sys_fail(0);
7717 SAVED_GROUP_ID = gid;
7718#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7720 SAVED_GROUP_ID = gid;
7739p_uid_have_saved_id(
VALUE _)
7741#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7749#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7751p_uid_sw_ensure(
VALUE i)
7753 rb_uid_t
id = (rb_uid_t)i;
7754 under_uid_switch = 0;
7755 id = rb_seteuid_core(
id);
7774p_uid_switch(
VALUE obj)
7786 under_uid_switch = 1;
7793 else if (euid != SAVED_USER_ID) {
7794 proc_seteuid(SAVED_USER_ID);
7796 under_uid_switch = 1;
7811p_uid_sw_ensure(
VALUE obj)
7813 under_uid_switch = 0;
7814 return p_uid_exchange(obj);
7818p_uid_switch(
VALUE obj)
7830 p_uid_exchange(obj);
7832 under_uid_switch = 1;
7854p_gid_have_saved_id(
VALUE _)
7856#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7863#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7865p_gid_sw_ensure(
VALUE i)
7867 rb_gid_t
id = (rb_gid_t)i;
7868 under_gid_switch = 0;
7869 id = rb_setegid_core(
id);
7888p_gid_switch(
VALUE obj)
7900 under_gid_switch = 1;
7907 else if (egid != SAVED_GROUP_ID) {
7908 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7910 under_gid_switch = 1;
7925p_gid_sw_ensure(
VALUE obj)
7927 under_gid_switch = 0;
7928 return p_gid_exchange(obj);
7932p_gid_switch(
VALUE obj)
7944 p_gid_exchange(obj);
7946 under_gid_switch = 1;
7956#if defined(HAVE_TIMES)
7960#ifdef HAVE__SC_CLK_TCK
7961 return sysconf(_SC_CLK_TCK);
7962#elif defined CLK_TCK
7984rb_proc_times(
VALUE obj)
7986 VALUE utime, stime, cutime, cstime, ret;
7987#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7988 struct rusage usage_s, usage_c;
7990 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7992 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7993 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7994 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7995 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7997 const double hertz = (double)get_clk_tck();
8001 utime =
DBL2NUM(buf.tms_utime / hertz);
8002 stime =
DBL2NUM(buf.tms_stime / hertz);
8003 cutime =
DBL2NUM(buf.tms_cutime / hertz);
8004 cstime =
DBL2NUM(buf.tms_cstime / hertz);
8006 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
8014#define rb_proc_times rb_f_notimplement
8017#ifdef HAVE_LONG_LONG
8019#define TIMETICK_INT_MIN LLONG_MIN
8020#define TIMETICK_INT_MAX LLONG_MAX
8021#define TIMETICK_INT2NUM(v) LL2NUM(v)
8022#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
8024typedef long timetick_int_t;
8025#define TIMETICK_INT_MIN LONG_MIN
8026#define TIMETICK_INT_MAX LONG_MAX
8027#define TIMETICK_INT2NUM(v) LONG2NUM(v)
8028#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
8031CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
8032static timetick_int_t
8033gcd_timetick_int(timetick_int_t a, timetick_int_t b)
8053reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
8055 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
8063reduce_factors(timetick_int_t *numerators,
int num_numerators,
8064 timetick_int_t *denominators,
int num_denominators)
8067 for (i = 0; i < num_numerators; i++) {
8068 if (numerators[i] == 1)
8070 for (j = 0; j < num_denominators; j++) {
8071 if (denominators[j] == 1)
8073 reduce_fraction(&numerators[i], &denominators[j]);
8079 timetick_int_t giga_count;
8084timetick2dblnum(
struct timetick *ttp,
8085 timetick_int_t *numerators,
int num_numerators,
8086 timetick_int_t *denominators,
int num_denominators)
8091 reduce_factors(numerators, num_numerators,
8092 denominators, num_denominators);
8094 d = ttp->giga_count * 1e9 + ttp->count;
8096 for (i = 0; i < num_numerators; i++)
8098 for (i = 0; i < num_denominators; i++)
8099 d /= denominators[i];
8105timetick2dblnum_reciprocal(
struct timetick *ttp,
8106 timetick_int_t *numerators,
int num_numerators,
8107 timetick_int_t *denominators,
int num_denominators)
8112 reduce_factors(numerators, num_numerators,
8113 denominators, num_denominators);
8116 for (i = 0; i < num_denominators; i++)
8117 d *= denominators[i];
8118 for (i = 0; i < num_numerators; i++)
8120 d /= ttp->giga_count * 1e9 + ttp->count;
8125#define NDIV(x,y) (-(-((x)+1)/(y))-1)
8126#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
8129timetick2integer(
struct timetick *ttp,
8130 timetick_int_t *numerators,
int num_numerators,
8131 timetick_int_t *denominators,
int num_denominators)
8136 reduce_factors(numerators, num_numerators,
8137 denominators, num_denominators);
8139 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
8140 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
8141 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
8142 for (i = 0; i < num_numerators; i++) {
8143 timetick_int_t factor = numerators[i];
8144 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
8148 for (i = 0; i < num_denominators; i++) {
8149 t = DIV(t, denominators[i]);
8151 return TIMETICK_INT2NUM(t);
8155 v = TIMETICK_INT2NUM(ttp->giga_count);
8158 for (i = 0; i < num_numerators; i++) {
8159 timetick_int_t factor = numerators[i];
8162 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
8164 for (i = 0; i < num_denominators; i++) {
8165 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
8171make_clock_result(
struct timetick *ttp,
8172 timetick_int_t *numerators,
int num_numerators,
8173 timetick_int_t *denominators,
int num_denominators,
8176 if (unit ==
ID2SYM(id_nanosecond)) {
8177 numerators[num_numerators++] = 1000000000;
8178 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8180 else if (unit ==
ID2SYM(id_microsecond)) {
8181 numerators[num_numerators++] = 1000000;
8182 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8184 else if (unit ==
ID2SYM(id_millisecond)) {
8185 numerators[num_numerators++] = 1000;
8186 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8188 else if (unit ==
ID2SYM(id_second)) {
8189 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
8191 else if (unit ==
ID2SYM(id_float_microsecond)) {
8192 numerators[num_numerators++] = 1000000;
8193 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8195 else if (unit ==
ID2SYM(id_float_millisecond)) {
8196 numerators[num_numerators++] = 1000;
8197 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8199 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
8200 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
8207static const mach_timebase_info_data_t *
8208get_mach_timebase_info(
void)
8210 static mach_timebase_info_data_t sTimebaseInfo;
8212 if ( sTimebaseInfo.denom == 0 ) {
8213 (void) mach_timebase_info(&sTimebaseInfo);
8216 return &sTimebaseInfo;
8220ruby_real_ms_time(
void)
8222 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8223 uint64_t t = mach_absolute_time();
8224 return (
double)t * info->numer / info->denom / 1e6;
8228#if defined(NUM2CLOCKID)
8229# define NUMERIC_CLOCKID 1
8231# define NUMERIC_CLOCKID 0
8232# define NUM2CLOCKID(x) 0
8368 timetick_int_t numerators[2];
8369 timetick_int_t denominators[2];
8370 int num_numerators = 0;
8371 int num_denominators = 0;
8374 VALUE clk_id = argv[0];
8375#ifdef HAVE_CLOCK_GETTIME
8380#ifdef CLOCK_REALTIME
8381 if (clk_id == RUBY_CLOCK_REALTIME) {
8387#ifdef CLOCK_MONOTONIC
8388 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8389 c = CLOCK_MONOTONIC;
8394#ifdef CLOCK_PROCESS_CPUTIME_ID
8395 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8396 c = CLOCK_PROCESS_CPUTIME_ID;
8401#ifdef CLOCK_THREAD_CPUTIME_ID
8402 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8403 c = CLOCK_THREAD_CPUTIME_ID;
8411#ifdef HAVE_GETTIMEOFDAY
8416#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8417 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8419 ret = gettimeofday(&tv, 0);
8422 tt.giga_count = tv.tv_sec;
8423 tt.count = (int32_t)tv.tv_usec * 1000;
8424 denominators[num_denominators++] = 1000000000;
8429#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8430 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8433 if (t == (time_t)-1)
8437 denominators[num_denominators++] = 1000000000;
8442#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8443 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8444 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8447 unsigned_clock_t uc;
8449 if (c == (clock_t)-1)
8451 uc = (unsigned_clock_t)c;
8452 tt.count = (int32_t)(uc % 1000000000);
8453 tt.giga_count = (uc / 1000000000);
8454 denominators[num_denominators++] = get_clk_tck();
8460#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8461 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8462 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8463 struct rusage usage;
8465 ret = getrusage(RUSAGE_SELF, &usage);
8468 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8469 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8470 if (1000000 <= usec) {
8474 tt.count = usec * 1000;
8475 denominators[num_denominators++] = 1000000000;
8481#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8482 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8483 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8485 unsigned_clock_t utime, stime;
8486 if (times(&buf) == (clock_t)-1)
8488 utime = (unsigned_clock_t)buf.tms_utime;
8489 stime = (unsigned_clock_t)buf.tms_stime;
8490 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8491 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8492 if (1000000000 <= tt.count) {
8493 tt.count -= 1000000000;
8496 denominators[num_denominators++] = get_clk_tck();
8501#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8502 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8503 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8505 unsigned_clock_t uc;
8508 if (c == (clock_t)-1)
8510 uc = (unsigned_clock_t)c;
8511 tt.count = (int32_t)(uc % 1000000000);
8512 tt.giga_count = uc / 1000000000;
8513 denominators[num_denominators++] = CLOCKS_PER_SEC;
8518 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8519 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8520 uint64_t t = mach_absolute_time();
8521 tt.count = (int32_t)(t % 1000000000);
8522 tt.giga_count = t / 1000000000;
8523 numerators[num_numerators++] = info->numer;
8524 denominators[num_denominators++] = info->denom;
8525 denominators[num_denominators++] = 1000000000;
8530 else if (NUMERIC_CLOCKID) {
8531#if defined(HAVE_CLOCK_GETTIME)
8533 c = NUM2CLOCKID(clk_id);
8535 ret = clock_gettime(c, &ts);
8538 tt.count = (int32_t)ts.tv_nsec;
8539 tt.giga_count = ts.tv_sec;
8540 denominators[num_denominators++] = 1000000000;
8548 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8599 timetick_int_t numerators[2];
8600 timetick_int_t denominators[2];
8601 int num_numerators = 0;
8602 int num_denominators = 0;
8603#ifdef HAVE_CLOCK_GETRES
8608 VALUE clk_id = argv[0];
8611#ifdef CLOCK_REALTIME
8612 if (clk_id == RUBY_CLOCK_REALTIME) {
8618#ifdef CLOCK_MONOTONIC
8619 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8620 c = CLOCK_MONOTONIC;
8625#ifdef CLOCK_PROCESS_CPUTIME_ID
8626 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8627 c = CLOCK_PROCESS_CPUTIME_ID;
8632#ifdef CLOCK_THREAD_CPUTIME_ID
8633 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8634 c = CLOCK_THREAD_CPUTIME_ID;
8639#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8640 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8643 denominators[num_denominators++] = 1000000000;
8648#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8649 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8652 denominators[num_denominators++] = 1000000000;
8657#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8658 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8661 denominators[num_denominators++] = get_clk_tck();
8666#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8667 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8670 denominators[num_denominators++] = 1000000000;
8675#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8676 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8679 denominators[num_denominators++] = get_clk_tck();
8684#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8685 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8688 denominators[num_denominators++] = CLOCKS_PER_SEC;
8693#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8694 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8695 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8698 numerators[num_numerators++] = info->numer;
8699 denominators[num_denominators++] = info->denom;
8700 denominators[num_denominators++] = 1000000000;
8705 else if (NUMERIC_CLOCKID) {
8706#if defined(HAVE_CLOCK_GETRES)
8708 c = NUM2CLOCKID(clk_id);
8710 ret = clock_getres(c, &ts);
8713 tt.count = (int32_t)ts.tv_nsec;
8714 tt.giga_count = ts.tv_sec;
8715 denominators[num_denominators++] = 1000000000;
8723 if (unit ==
ID2SYM(id_hertz)) {
8724 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8727 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8732get_CHILD_STATUS(
ID _x,
VALUE *_y)
8738get_PROCESS_ID(
ID _x,
VALUE *_y)
8786static VALUE rb_mProcUID;
8787static VALUE rb_mProcGID;
8788static VALUE rb_mProcID_Syscall;
8802 rb_gvar_ractor_local(
"$$");
8803 rb_gvar_ractor_local(
"$?");
8858 process_status_dump, process_status_load);
8894#ifdef HAVE_GETPRIORITY
8905#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
8907 VALUE inf = RLIM2NUM(RLIM_INFINITY);
8908#ifdef RLIM_SAVED_MAX
8910 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
8917#ifdef RLIM_SAVED_CUR
8919 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
8960#ifdef RLIMIT_MEMLOCK
8967#ifdef RLIMIT_MSGQUEUE
9033#ifdef RLIMIT_SIGPENDING
9068#if defined(RUBY_CLOCK_REALTIME)
9069#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9070# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9071#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9072# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9074#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9077#elif defined(RUBY_CLOCK_REALTIME)
9081#if defined(RUBY_CLOCK_MONOTONIC)
9082#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9083# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9085#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9088#elif defined(RUBY_CLOCK_MONOTONIC)
9092#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9093#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9094# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9096#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9099#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9103#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9106#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9119#ifdef CLOCK_REALTIME_FAST
9123#ifdef CLOCK_REALTIME_PRECISE
9127#ifdef CLOCK_REALTIME_COARSE
9131#ifdef CLOCK_REALTIME_ALARM
9135#ifdef CLOCK_MONOTONIC_FAST
9139#ifdef CLOCK_MONOTONIC_PRECISE
9143#ifdef CLOCK_MONOTONIC_RAW
9147#ifdef CLOCK_MONOTONIC_RAW_APPROX
9151#ifdef CLOCK_MONOTONIC_COARSE
9155#ifdef CLOCK_BOOTTIME
9159#ifdef CLOCK_BOOTTIME_ALARM
9167#ifdef CLOCK_UPTIME_FAST
9171#ifdef CLOCK_UPTIME_PRECISE
9175#ifdef CLOCK_UPTIME_RAW
9179#ifdef CLOCK_UPTIME_RAW_APPROX
9195#if defined(HAVE_TIMES) || defined(_WIN32)
9209 SAVED_USER_ID = geteuid();
9210 SAVED_GROUP_ID = getegid();
9233#ifdef p_uid_from_name
9236#ifdef p_gid_from_name
9267#define define_id(name) id_##name = rb_intern_const(#name)
9280 define_id(new_pgroup);
9282 define_id(unsetenv_others);
9285 define_id(close_others);
9286 define_id(nanosecond);
9287 define_id(microsecond);
9288 define_id(millisecond);
9290 define_id(float_microsecond);
9291 define_id(float_millisecond);
9292 define_id(float_second);
9293 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9294 define_id(TIME_BASED_CLOCK_REALTIME);
9295#ifdef CLOCK_REALTIME
9296 define_id(CLOCK_REALTIME);
9298#ifdef CLOCK_MONOTONIC
9299 define_id(CLOCK_MONOTONIC);
9301#ifdef CLOCK_PROCESS_CPUTIME_ID
9302 define_id(CLOCK_PROCESS_CPUTIME_ID);
9304#ifdef CLOCK_THREAD_CPUTIME_ID
9305 define_id(CLOCK_THREAD_CPUTIME_ID);
9308 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9309 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9312 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9314 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9316 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.
const char * wrap_struct_name
Name of structs of this kind.
Ruby's IO, metadata and buffers.
VALUE tied_io_for_writing
Duplex IO object, if set.
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.