14#include "ruby/internal/config.h"
42# define EXIT_SUCCESS 0
46# define EXIT_FAILURE 1
53#ifdef HAVE_SYS_RESOURCE_H
54# include <sys/resource.h>
61#ifdef HAVE_SYS_PARAM_H
62# include <sys/param.h>
66# define MAXPATHLEN 1024
75#ifdef HAVE_SYS_TIMES_H
76# include <sys/times.h>
86int initgroups(
const char *, rb_gid_t);
95# include <mach/mach_time.h>
101#include "internal/bits.h"
102#include "internal/dir.h"
103#include "internal/error.h"
104#include "internal/eval.h"
105#include "internal/hash.h"
106#include "internal/io.h"
107#include "internal/numeric.h"
108#include "internal/object.h"
109#include "internal/process.h"
110#include "internal/thread.h"
111#include "internal/variable.h"
112#include "internal/warnings.h"
117#include "ractor_core.h"
125#define open rb_w32_uopen
128#if defined(HAVE_TIMES)
135static VALUE rb_cProcessTms;
139#define WIFEXITED(w) (((w) & 0xff) == 0)
142#define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
145#define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
148#define WEXITSTATUS(w) (((w) >> 8) & 0xff)
151#define WTERMSIG(w) ((w) & 0x7f)
154#define WSTOPSIG WEXITSTATUS
157#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
158#define HAVE_44BSD_SETUID 1
159#define HAVE_44BSD_SETGID 1
167#if defined(HAVE_44BSD_SETUID) || defined(__APPLE__)
168#if !defined(USE_SETREUID)
169#define OBSOLETE_SETREUID 1
171#if !defined(USE_SETREGID)
172#define OBSOLETE_SETREGID 1
176static void check_uid_switch(
void);
177static void check_gid_switch(
void);
178static int exec_async_signal_safe(
const struct rb_execarg *,
char *,
size_t);
180VALUE rb_envtbl(
void);
181VALUE rb_env_to_hash(
void);
184#define p_uid_from_name p_uid_from_name
185#define p_gid_from_name p_gid_from_name
188#if defined(HAVE_UNISTD_H)
189# if defined(HAVE_GETLOGIN_R)
190# define USE_GETLOGIN_R 1
191# define GETLOGIN_R_SIZE_DEFAULT 0x100
192# define GETLOGIN_R_SIZE_LIMIT 0x1000
193# if defined(_SC_LOGIN_NAME_MAX)
194# define GETLOGIN_R_SIZE_INIT sysconf(_SC_LOGIN_NAME_MAX)
196# define GETLOGIN_R_SIZE_INIT GETLOGIN_R_SIZE_DEFAULT
198# elif defined(HAVE_GETLOGIN)
199# define USE_GETLOGIN 1
203#if defined(HAVE_PWD_H)
204# if defined(HAVE_GETPWUID_R)
205# define USE_GETPWUID_R 1
206# elif defined(HAVE_GETPWUID)
207# define USE_GETPWUID 1
209# if defined(HAVE_GETPWNAM_R)
210# define USE_GETPWNAM_R 1
211# elif defined(HAVE_GETPWNAM)
212# define USE_GETPWNAM 1
214# if defined(HAVE_GETPWNAM_R) || defined(HAVE_GETPWUID_R)
215# define GETPW_R_SIZE_DEFAULT 0x1000
216# define GETPW_R_SIZE_LIMIT 0x10000
217# if defined(_SC_GETPW_R_SIZE_MAX)
218# define GETPW_R_SIZE_INIT sysconf(_SC_GETPW_R_SIZE_MAX)
220# define GETPW_R_SIZE_INIT GETPW_R_SIZE_DEFAULT
223# ifdef USE_GETPWNAM_R
224# define PREPARE_GETPWNAM \
226# define FINISH_GETPWNAM \
227 (getpw_buf ? (void)rb_str_resize(getpw_buf, 0) : (void)0)
228# define OBJ2UID1(id) obj2uid((id), &getpw_buf)
229# define OBJ2UID(id) obj2uid0(id)
230static rb_uid_t obj2uid(
VALUE id,
VALUE *getpw_buf);
231static inline rb_uid_t
241# define PREPARE_GETPWNAM
242# define FINISH_GETPWNAM
243# define OBJ2UID1(id) obj2uid((id))
244# define OBJ2UID(id) obj2uid((id))
245static rb_uid_t obj2uid(
VALUE id);
248# define PREPARE_GETPWNAM
249# define FINISH_GETPWNAM
250# define OBJ2UID1(id) NUM2UIDT(id)
251# define OBJ2UID(id) NUM2UIDT(id)
252# ifdef p_uid_from_name
253# undef p_uid_from_name
254# define p_uid_from_name rb_f_notimplement
258#if defined(HAVE_GRP_H)
259# if defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
260# define USE_GETGRNAM_R
261# define GETGR_R_SIZE_INIT sysconf(_SC_GETGR_R_SIZE_MAX)
262# define GETGR_R_SIZE_DEFAULT 0x1000
263# define GETGR_R_SIZE_LIMIT 0x10000
265# ifdef USE_GETGRNAM_R
266# define PREPARE_GETGRNAM \
268# define FINISH_GETGRNAM \
269 (getgr_buf ? (void)rb_str_resize(getgr_buf, 0) : (void)0)
270# define OBJ2GID1(id) obj2gid((id), &getgr_buf)
271# define OBJ2GID(id) obj2gid0(id)
272static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
273static inline rb_gid_t
282static rb_gid_t obj2gid(
VALUE id,
VALUE *getgr_buf);
284# define PREPARE_GETGRNAM
285# define FINISH_GETGRNAM
286# define OBJ2GID1(id) obj2gid((id))
287# define OBJ2GID(id) obj2gid((id))
288static rb_gid_t obj2gid(
VALUE id);
291# define PREPARE_GETGRNAM
292# define FINISH_GETGRNAM
293# define OBJ2GID1(id) NUM2GIDT(id)
294# define OBJ2GID(id) NUM2GIDT(id)
295# ifdef p_gid_from_name
296# undef p_gid_from_name
297# define p_gid_from_name rb_f_notimplement
301#if SIZEOF_CLOCK_T == SIZEOF_INT
302typedef unsigned int unsigned_clock_t;
303#elif SIZEOF_CLOCK_T == SIZEOF_LONG
304typedef unsigned long unsigned_clock_t;
305#elif defined(HAVE_LONG_LONG) && SIZEOF_CLOCK_T == SIZEOF_LONG_LONG
306typedef unsigned LONG_LONG unsigned_clock_t;
309typedef void (*sig_t) (int);
312#define id_exception idException
313static ID id_in, id_out, id_err, id_pid, id_uid, id_gid;
314static ID id_close, id_child;
319static ID id_new_pgroup;
321static ID id_unsetenv_others, id_chdir, id_umask, id_close_others;
322static ID id_nanosecond, id_microsecond, id_millisecond, id_second;
323static ID id_float_microsecond, id_float_millisecond, id_float_second;
324static ID id_GETTIMEOFDAY_BASED_CLOCK_REALTIME, id_TIME_BASED_CLOCK_REALTIME;
326static ID id_CLOCK_REALTIME;
327# define RUBY_CLOCK_REALTIME ID2SYM(id_CLOCK_REALTIME)
329#ifdef CLOCK_MONOTONIC
330static ID id_CLOCK_MONOTONIC;
331# define RUBY_CLOCK_MONOTONIC ID2SYM(id_CLOCK_MONOTONIC)
333#ifdef CLOCK_PROCESS_CPUTIME_ID
334static ID id_CLOCK_PROCESS_CPUTIME_ID;
335# define RUBY_CLOCK_PROCESS_CPUTIME_ID ID2SYM(id_CLOCK_PROCESS_CPUTIME_ID)
337#ifdef CLOCK_THREAD_CPUTIME_ID
338static ID id_CLOCK_THREAD_CPUTIME_ID;
339# define RUBY_CLOCK_THREAD_CPUTIME_ID ID2SYM(id_CLOCK_THREAD_CPUTIME_ID)
342static ID id_TIMES_BASED_CLOCK_MONOTONIC;
343static ID id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID;
346static ID id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID;
348static ID id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID;
350static ID id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC;
351# define RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ID2SYM(id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
354#ifdef HAVE_WORKING_FORK
358static rb_pid_t cached_pid;
361#if defined(__sun) && !defined(_XPG7)
362#define execv(path, argv) (rb_async_bug_errno("unreachable: async-signal-unsafe execv() is called", 0))
363#define execl(path, arg0, arg1, arg2, term) do { extern char **environ; execle((path), (arg0), (arg1), (arg2), (term), (environ)); } while (0)
364#define ALWAYS_NEED_ENVP 1
366#define ALWAYS_NEED_ENVP 0
370assert_close_on_exec(
int fd)
373#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(FD_CLOEXEC)
374 int flags = fcntl(fd, F_GETFD);
376 static const char m[] =
"reserved FD closed unexpectedly?\n";
377 (void)!write(2, m,
sizeof(m) - 1);
380 if (flags & FD_CLOEXEC)
return;
381 rb_bug(
"reserved FD did not have close-on-exec set");
383 rb_bug(
"reserved FD without close-on-exec support");
389close_unless_reserved(
int fd)
392 assert_close_on_exec(fd);
399#if defined(DEBUG_REDIRECT)
402ttyprintf(
const char *fmt, ...)
408 tty = fopen(
"con",
"w");
410 tty = fopen(
"/dev/tty",
"w");
416 vfprintf(tty, fmt, ap);
423redirect_dup(
int oldfd)
427 ttyprintf(
"dup(%d) => %d\n", oldfd, ret);
432redirect_dup2(
int oldfd,
int newfd)
435 ret = dup2(oldfd, newfd);
436 ttyprintf(
"dup2(%d, %d) => %d\n", oldfd, newfd, ret);
441redirect_cloexec_dup(
int oldfd)
445 ttyprintf(
"cloexec_dup(%d) => %d\n", oldfd, ret);
450redirect_cloexec_dup2(
int oldfd,
int newfd)
454 ttyprintf(
"cloexec_dup2(%d, %d) => %d\n", oldfd, newfd, ret);
459redirect_close(
int fd)
462 ret = close_unless_reserved(fd);
463 ttyprintf(
"close(%d) => %d\n", fd, ret);
468parent_redirect_open(
const char *pathname,
int flags, mode_t perm)
472 ttyprintf(
"parent_open(\"%s\", 0x%x, 0%o) => %d\n", pathname, flags, perm, ret);
477parent_redirect_close(
int fd)
480 ret = close_unless_reserved(fd);
481 ttyprintf(
"parent_close(%d) => %d\n", fd, ret);
486#define redirect_dup(oldfd) dup(oldfd)
487#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
488#define redirect_cloexec_dup(oldfd) rb_cloexec_dup(oldfd)
489#define redirect_cloexec_dup2(oldfd, newfd) rb_cloexec_dup2((oldfd), (newfd))
490#define redirect_close(fd) close_unless_reserved(fd)
491#define parent_redirect_open(pathname, flags, perm) rb_cloexec_open((pathname), (flags), (perm))
492#define parent_redirect_close(fd) close_unless_reserved(fd)
498 if (UNLIKELY(!cached_pid)) {
499 cached_pid = getpid();
505#if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
579static VALUE rb_cProcessStatus;
594 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_FROZEN_SHAREABLE,
598rb_process_status_allocate(
VALUE klass)
607 return GET_THREAD()->last_status;
633proc_s_last_status(
VALUE mod)
641 VALUE last_status = rb_process_status_allocate(rb_cProcessStatus);
644 data->status = status;
652process_status_dump(
VALUE status)
665process_status_load(
VALUE real_obj,
VALUE load_obj)
668 VALUE status = rb_attr_get(load_obj, id_status);
669 VALUE pid = rb_attr_get(load_obj, id_pid);
684 th->last_status =
Qnil;
688rb_last_status_clear(
void)
690 last_status_clear(GET_THREAD());
702pst_status(
VALUE status)
722 int status = pst_status(self);
726#define PST2INT(st) pst_status(st)
742 rb_pid_t pid = pst_pid(self);
746static VALUE pst_message_status(
VALUE str,
int status);
749pst_message(
VALUE str, rb_pid_t pid,
int status)
751 rb_str_catf(str,
"pid %ld", (
long)pid);
752 pst_message_status(str, status);
756pst_message_status(
VALUE str,
int status)
758 if (WIFSTOPPED(status)) {
759 int stopsig = WSTOPSIG(status);
762 rb_str_catf(str,
" stopped SIG%s (signal %d)", signame, stopsig);
765 rb_str_catf(str,
" stopped signal %d", stopsig);
768 if (WIFSIGNALED(status)) {
769 int termsig = WTERMSIG(status);
772 rb_str_catf(str,
" SIG%s (signal %d)", signame, termsig);
775 rb_str_catf(str,
" signal %d", termsig);
778 if (WIFEXITED(status)) {
779 rb_str_catf(str,
" exit %d", WEXITSTATUS(status));
782 if (WCOREDUMP(status)) {
810 status = PST2INT(st);
813 pst_message(str, pid, status);
840 status = PST2INT(st);
843 pst_message(str, pid, status);
865 if (st1 == st2)
return Qtrue;
866 return rb_equal(pst_to_i(st1), st2);
880pst_wifstopped(
VALUE st)
882 int status = PST2INT(st);
884 return RBOOL(WIFSTOPPED(status));
897pst_wstopsig(
VALUE st)
899 int status = PST2INT(st);
901 if (WIFSTOPPED(status))
902 return INT2NUM(WSTOPSIG(status));
916pst_wifsignaled(
VALUE st)
918 int status = PST2INT(st);
920 return RBOOL(WIFSIGNALED(status));
933pst_wtermsig(
VALUE st)
935 int status = PST2INT(st);
937 if (WIFSIGNALED(status))
938 return INT2NUM(WTERMSIG(status));
953pst_wifexited(
VALUE st)
955 int status = PST2INT(st);
957 return RBOOL(WIFEXITED(status));
975pst_wexitstatus(
VALUE st)
977 int status = PST2INT(st);
979 if (WIFEXITED(status))
980 return INT2NUM(WEXITSTATUS(status));
998pst_success_p(
VALUE st)
1000 int status = PST2INT(st);
1002 if (!WIFEXITED(status))
1004 return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
1019pst_wcoredump(
VALUE st)
1022 int status = PST2INT(st);
1024 return RBOOL(WCOREDUMP(status));
1031do_waitpid(rb_pid_t pid,
int *st,
int flags)
1033#if defined HAVE_WAITPID
1034 return waitpid(pid, st, flags);
1035#elif defined HAVE_WAIT4
1036 return wait4(pid, st, flags, NULL);
1038# error waitpid or wait4 is required.
1043 struct ccan_list_node wnode;
1045 rb_nativethread_cond_t *cond;
1054waitpid_state_init(
struct waitpid_state *w, rb_pid_t pid,
int options)
1058 w->options = options;
1064waitpid_blocking_no_SIGCHLD(
void *x)
1068 w->ret = do_waitpid(w->pid, &w->status, w->options);
1076 if (w->options & WNOHANG) {
1077 w->ret = do_waitpid(w->pid, &w->status, w->options);
1082 }
while (w->ret < 0 &&
errno == EINTR && (RUBY_VM_CHECK_INTS(w->ec),1));
1092 if (!(flags & WNOHANG)) {
1094 if (scheduler !=
Qnil) {
1096 if (!UNDEF_P(result))
return result;
1142rb_process_status_waitv(
int argc,
VALUE *argv,
VALUE _)
1164 if (
NIL_P(status))
return 0;
1169 if (st) *st = data->status;
1172 errno = data->error;
1175 GET_THREAD()->last_status = status;
1182proc_wait(
int argc,
VALUE *argv)
1194 if (argc == 2 && !
NIL_P(vflags = argv[1])) {
1199 if ((pid =
rb_waitpid(pid, &status, flags)) < 0)
1203 rb_last_status_clear();
1360 return proc_wait(c, v);
1380 VALUE pid = proc_wait(argc, argv);
1409 rb_last_status_clear();
1424static VALUE rb_cWaiter;
1427detach_process_pid(
VALUE thread)
1433detach_process_watcher(
void *arg)
1435 rb_pid_t cpid, pid = (rb_pid_t)(
VALUE)arg;
1438 while ((cpid =
rb_waitpid(pid, &status, 0)) == 0) {
1449 RBASIC_SET_CLASS(watcher, rb_cWaiter);
1501before_exec_async_signal_safe(
void)
1506before_exec_non_async_signal_safe(
void)
1517 rb_thread_stop_timer_thread();
1520#define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
1522int rb_w32_set_nonblock2(
int fd,
int nonblock);
1529 return rb_w32_set_nonblock2(fd, 0);
1530#elif defined(F_GETFL) && defined(F_SETFL)
1531 int fl = fcntl(fd, F_GETFL);
1534 if (fl == -1)
return fl;
1535 if (fl & O_NONBLOCK) {
1537 return fcntl(fd, F_SETFL, fl);
1544stdfd_clear_nonblock(
void)
1548 for (fd = 0; fd < 3; fd++) {
1549 (void)set_blocking(fd);
1556 before_exec_non_async_signal_safe();
1557 before_exec_async_signal_safe();
1563 rb_thread_reset_timer_thread();
1564 rb_thread_start_timer_thread();
1567#if defined HAVE_WORKING_FORK || defined HAVE_DAEMON
1569before_fork_ruby(
void)
1572 rb_gc_before_fork();
1576after_fork_ruby(rb_pid_t pid)
1578 rb_gc_after_fork(pid);
1592#if defined(HAVE_WORKING_FORK)
1594COMPILER_WARNING_PUSH
1595#if __has_warning("-Wdeprecated-declarations") || RBIMPL_COMPILER_IS(GCC)
1596COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
1598static inline rb_pid_t
1606#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1608exec_with_sh(
const char *prog,
char **argv,
char **envp)
1610 *argv = (
char *)prog;
1611 *--argv = (
char *)
"sh";
1613 execve(
"/bin/sh", argv, envp);
1615 execv(
"/bin/sh", argv);
1619#define try_with_sh(err, prog, argv, envp) (void)0
1624proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1632 argv = ARGVSTR2ARGV(argv_str);
1639 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1642 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1644 execve(prog, argv, envp);
1648 try_with_sh(err, prog, argv, envp);
1655proc_exec_sh(
const char *str,
VALUE envp_str)
1660 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1668 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1671 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1673 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1683 ret = proc_exec_sh(str,
Qfalse);
1690mark_exec_arg(
void *ptr)
1693 if (eargp->use_shell)
1694 rb_gc_mark(eargp->invoke.sh.shell_script);
1696 rb_gc_mark(eargp->invoke.cmd.command_name);
1697 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1698 rb_gc_mark(eargp->invoke.cmd.argv_str);
1699 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1701 rb_gc_mark(eargp->redirect_fds);
1702 rb_gc_mark(eargp->envp_str);
1703 rb_gc_mark(eargp->envp_buf);
1704 rb_gc_mark(eargp->dup2_tmpbuf);
1705 rb_gc_mark(eargp->rlimit_limits);
1706 rb_gc_mark(eargp->fd_dup2);
1707 rb_gc_mark(eargp->fd_close);
1708 rb_gc_mark(eargp->fd_open);
1709 rb_gc_mark(eargp->fd_dup2_child);
1710 rb_gc_mark(eargp->env_modification);
1711 rb_gc_mark(eargp->path_env);
1712 rb_gc_mark(eargp->chdir_dir);
1716memsize_exec_arg(
const void *ptr)
1724 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
1728# define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1730#ifdef DEFAULT_PROCESS_ENCODING
1731# define EXPORT_STR(str) rb_str_export_to_enc((str), DEFAULT_PROCESS_ENCODING)
1732# define EXPORT_DUP(str) export_dup(str)
1734export_dup(
VALUE str)
1736 VALUE newstr = EXPORT_STR(str);
1741# define EXPORT_STR(str) (str)
1742# define EXPORT_DUP(str) rb_str_dup(str)
1745#if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1746# define USE_SPAWNV 1
1748# define USE_SPAWNV 0
1751# define P_NOWAIT _P_NOWAIT
1762 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1763 flags = CREATE_NEW_PROCESS_GROUP;
1765 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ? RSTRING_PTR(prog) : 0, argv, flags);
1770#define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
1776 RBASIC_CLEAR_CLASS(obj);
1781check_exec_redirect_fd(
VALUE v,
int iskey)
1792 else if (
id == id_out)
1794 else if (
id == id_err)
1803 rb_raise(rb_eArgError,
"duplex IO redirection");
1810 rb_raise(rb_eArgError,
"negative file descriptor");
1813 else if (fd >= 3 && iskey) {
1814 rb_raise(rb_eArgError,
"wrong file descriptor (%d)", fd);
1820 rb_raise(rb_eArgError,
"wrong exec redirect");
1831 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
1838 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
1849 VALUE path, flags, perm;
1853 switch (
TYPE(val)) {
1856 if (
id == id_close) {
1858 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
1860 else if (
id == id_in) {
1862 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1864 else if (
id == id_out) {
1866 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1868 else if (
id == id_err) {
1870 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1873 rb_raise(rb_eArgError,
"wrong exec redirect symbol: %"PRIsVALUE,
1880 val = check_exec_redirect_fd(val, 0);
1884 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1890 path ==
ID2SYM(id_child)) {
1891 param = check_exec_redirect_fd(
rb_ary_entry(val, 1), 0);
1892 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
1905 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1906 flags, perm,
Qnil));
1907 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1915 key = check_exec_redirect_fd(key, 1);
1917 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1922 VALUE fd = check_exec_redirect_fd(v, 1);
1926 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1933 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1934 flags, perm,
Qnil));
1935 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1941 if (!
NIL_P(val))
goto io;
1942 rb_raise(rb_eArgError,
"wrong exec redirect action");
1947#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
1948static int rlimit_type_by_sym(
VALUE key);
1951rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
1953 VALUE ary = eargp->rlimit_limits;
1954 VALUE tmp, softlim, hardlim;
1955 if (eargp->rlimit_limits ==
Qfalse)
1956 ary = eargp->rlimit_limits = hide_obj(
rb_ary_new());
1958 ary = eargp->rlimit_limits;
1968 rb_raise(rb_eArgError,
"wrong exec rlimit option");
1979#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
1983 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
1987 switch (
TYPE(key)) {
1989#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
1991 int rtype = rlimit_type_by_sym(key);
1993 rb_execarg_addopt_rlimit(eargp, rtype, val);
2001 if (
id == id_pgroup) {
2003 if (eargp->pgroup_given) {
2004 rb_raise(rb_eArgError,
"pgroup option specified twice");
2008 else if (val ==
Qtrue)
2013 rb_raise(rb_eArgError,
"negative process group ID : %ld", (
long)pgroup);
2016 eargp->pgroup_given = 1;
2017 eargp->pgroup_pgid = pgroup;
2022 if (
id == id_new_pgroup) {
2023 if (eargp->new_pgroup_given) {
2024 rb_raise(rb_eArgError,
"new_pgroup option specified twice");
2026 eargp->new_pgroup_given = 1;
2027 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2031 if (
id == id_unsetenv_others) {
2032 if (eargp->unsetenv_others_given) {
2033 rb_raise(rb_eArgError,
"unsetenv_others option specified twice");
2035 eargp->unsetenv_others_given = 1;
2036 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2038 else if (
id == id_chdir) {
2039 if (eargp->chdir_given) {
2040 rb_raise(rb_eArgError,
"chdir option specified twice");
2043 val = rb_str_encode_ospath(val);
2044 eargp->chdir_given = 1;
2045 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2047 else if (
id == id_umask) {
2049 if (eargp->umask_given) {
2050 rb_raise(rb_eArgError,
"umask option specified twice");
2052 eargp->umask_given = 1;
2053 eargp->umask_mask = cmask;
2055 else if (
id == id_close_others) {
2056 if (eargp->close_others_given) {
2057 rb_raise(rb_eArgError,
"close_others option specified twice");
2059 eargp->close_others_given = 1;
2060 eargp->close_others_do = TO_BOOL(val,
"close_others");
2062 else if (
id == id_in) {
2066 else if (
id == id_out) {
2070 else if (
id == id_err) {
2074 else if (
id == id_uid) {
2076 if (eargp->uid_given) {
2077 rb_raise(rb_eArgError,
"uid option specified twice");
2081 eargp->uid = OBJ2UID(val);
2082 eargp->uid_given = 1;
2086 "uid option is unimplemented on this machine");
2089 else if (
id == id_gid) {
2091 if (eargp->gid_given) {
2092 rb_raise(rb_eArgError,
"gid option specified twice");
2096 eargp->gid = OBJ2GID(val);
2097 eargp->gid_given = 1;
2101 "gid option is unimplemented on this machine");
2104 else if (
id == id_exception) {
2105 if (eargp->exception_given) {
2106 rb_raise(rb_eArgError,
"exception option specified twice");
2108 eargp->exception_given = 1;
2109 eargp->exception = TO_BOOL(val,
"exception");
2120 check_exec_redirect(key, val, eargp);
2132check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2137 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2139 rb_raise(rb_eArgError,
"wrong exec option symbol: % "PRIsVALUE,
2141 rb_raise(rb_eArgError,
"wrong exec option: %"PRIsVALUE,
rb_obj_class(key));
2147check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2152 VALUE execarg_obj = args[0];
2153 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2154 VALUE nonopts = args[1];
2155 if (
NIL_P(nonopts)) args[1] = nonopts = rb_hash_new();
2156 rb_hash_aset(nonopts, key, val);
2171 rb_raise(rb_eArgError,
"fd %d specified twice", fd);
2173 if (ary == eargp->fd_dup2)
2175 else if (ary == eargp->fd_dup2_child)
2181 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2194 VALUE h = rb_hash_new();
2199 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2200 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2201 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2203 if (eargp->fd_dup2_child) {
2204 ary = eargp->fd_dup2_child;
2214 val = rb_hash_lookup(h, val);
2216 rb_raise(rb_eArgError,
"cyclic child fd redirection from %d", oldfd);
2220 rb_raise(rb_eArgError,
"child fd %d is not redirected", oldfd);
2221 if (oldfd != lastfd) {
2226 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2227 rb_hash_aset(h, val,
INT2FIX(lastfd));
2234 eargp->close_others_maxhint = maxhint;
2239rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2243 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2247rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2252 args[0] = execarg_obj;
2254 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2258#ifdef ENV_IGNORECASE
2259#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2261#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2265check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2275 rb_raise(rb_eArgError,
"environment name contains a equal : %"PRIsVALUE, key);
2280 key = EXPORT_STR(key);
2281 if (!
NIL_P(val)) val = EXPORT_STR(val);
2298 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2305rb_check_argv(
int argc,
VALUE *argv)
2316 rb_raise(rb_eArgError,
"wrong first argument");
2324 for (i = 0; i < argc; i++) {
2333check_hash(
VALUE obj)
2343 return rb_check_hash_type(obj);
2347rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2352 hash = check_hash((*argv_p)[*argc_p-1]);
2354 *opthash_ret = hash;
2360 hash = check_hash((*argv_p)[0]);
2367 prog = rb_check_argv(*argc_p, *argv_p);
2369 prog = (*argv_p)[0];
2370 if (accept_shell && *argc_p == 1) {
2385compare_posix_sh(
const void *key,
const void *el)
2388 int ret = strncmp(word->ptr, el, word->len);
2389 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2394#define append_terminator(buf) rb_str_buf_cat(buf, "", 1)
2399 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2400 char fbuf[MAXPATHLEN];
2404 if (!
NIL_P(opthash)) {
2405 rb_check_exec_options(opthash, execarg_obj);
2408 env = rb_check_exec_env(env, &eargp->path_env);
2409 eargp->env_modification = env;
2412 prog = EXPORT_STR(prog);
2413 eargp->use_shell = argc == 0;
2414 if (eargp->use_shell)
2415 eargp->invoke.sh.shell_script = prog;
2417 eargp->invoke.cmd.command_name = prog;
2420 if (eargp->use_shell) {
2421 static const char posix_sh_cmds[][9] = {
2453 const char *
const s = rb_str_null_check(prog);
2482 for (p = s; p < e; p++) {
2483 if (*p ==
' ' || *p ==
'\t') {
2484 if (first.ptr && !first.len) first.len = p - first.ptr;
2487 if (!first.ptr) first.ptr = p;
2489 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2495 else if (*p ==
'/') {
2503 if (!first.ptr) first.ptr = e;
2504 if (!first.len) first.len = p - first.ptr;
2505 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2507 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2512 eargp->use_shell = 0;
2514 if (!eargp->use_shell) {
2518 append_terminator(argv_buf);
2519 for (p = first.ptr + first.len; p < e;) {
2520 while (p < e && (*p ==
' ' || *p ==
'\t'))
2524 while (p < e && *p !=
' ' && *p !=
'\t')
2527 append_terminator(argv_buf);
2530 eargp->invoke.cmd.argv_buf = argv_buf;
2531 eargp->invoke.cmd.command_name =
2533 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2538 if (!eargp->use_shell) {
2539 const char *abspath;
2540 const char *path_env = 0;
2541 if (
RTEST(eargp->path_env)) path_env = RSTRING_PTR(eargp->path_env);
2542 abspath = dln_find_exe_r(RSTRING_PTR(eargp->invoke.cmd.command_name),
2543 path_env, fbuf,
sizeof(fbuf));
2547 eargp->invoke.cmd.command_abspath =
Qnil;
2550 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2555 for (i = 0; i < argc; i++) {
2556 VALUE arg = argv[i];
2558#ifdef DEFAULT_PROCESS_ENCODING
2559 arg = EXPORT_STR(arg);
2560 s = RSTRING_PTR(arg);
2563 append_terminator(argv_buf);
2565 eargp->invoke.cmd.argv_buf = argv_buf;
2568 if (!eargp->use_shell) {
2569 const char *p, *ep, *null=NULL;
2573 p = RSTRING_PTR(eargp->invoke.cmd.argv_buf);
2574 ep = p + RSTRING_LEN(eargp->invoke.cmd.argv_buf);
2580 eargp->invoke.cmd.argv_str =
2581 rb_imemo_tmpbuf_new_from_an_RString(argv_str);
2587rb_execarg_get(
VALUE execarg_obj)
2595rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2597 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2603 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2604 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2606 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2612rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2617 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2618 if (!allow_exc_opt && eargp->exception_given) {
2619 rb_raise(rb_eArgError,
"exception option is not allowed");
2625rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2627 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2628 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2629 eargp->env_modification = env;
2634fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2643 append_terminator(envp_buf);
2649static long run_exec_dup2_tmpbuf_size(
long n);
2663 const char *fname = RSTRING_PTR(data->fname);
2664 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2670rb_execarg_allocate_dup2_tmpbuf(
struct rb_execarg *eargp,
long len)
2672 rb_alloc_tmp_buffer(&eargp->dup2_tmpbuf, run_exec_dup2_tmpbuf_size(
len),
false);
2676rb_execarg_parent_start1(
VALUE execarg_obj)
2678 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2679 int unsetenv_others;
2683 ary = eargp->fd_open;
2698 open_data.fname = vpath;
2699 open_data.oflags = flags;
2700 open_data.perm = perm;
2702 open_data.err = EINTR;
2704 if (open_data.ret == -1) {
2705 if (open_data.err == EINTR) {
2711 fd2 = open_data.ret;
2723 eargp->redirect_fds = check_exec_fds(eargp);
2725 ary = eargp->fd_dup2;
2727 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2730 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2731 envopts = eargp->env_modification;
2732 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2733 VALUE envtbl, envp_str, envp_buf;
2735 if (unsetenv_others) {
2736 envtbl = rb_hash_new();
2739 envtbl = rb_env_to_hash();
2749 rb_hash_delete(envtbl, key);
2752 rb_hash_aset(envtbl, key, val);
2758 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
2761 p = RSTRING_PTR(envp_buf);
2762 ep = p + RSTRING_LEN(envp_buf);
2770 rb_imemo_tmpbuf_new_from_an_RString(envp_str);
2771 eargp->envp_buf = envp_buf;
2787rb_execarg_parent_start(
VALUE execarg_obj)
2790 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2792 rb_execarg_parent_end(execarg_obj);
2798execarg_parent_end(
VALUE execarg_obj)
2800 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2804 ary = eargp->fd_open;
2815 parent_redirect_close(fd2);
2827rb_execarg_parent_end(
VALUE execarg_obj)
2829 execarg_parent_end(execarg_obj);
2833rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
2835 if (!errmsg || !*errmsg)
return;
2836 if (strcmp(errmsg,
"chdir") == 0) {
2837 rb_sys_fail_str(eargp->chdir_dir);
2839 rb_sys_fail(errmsg);
2844rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
2846 if (!errmsg || !*errmsg)
return;
2847 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
2855 VALUE execarg_obj, fail_str;
2857#define CHILD_ERRMSG_BUFLEN 80
2858 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
2861 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
2862 eargp = rb_execarg_get(execarg_obj);
2865 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2867 execarg_parent_end(execarg_obj);
2872 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2874 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
2877 rb_exec_fail(eargp, err, errmsg);
2981#define ERRMSG(str) \
2982 ((errmsg && 0 < errmsg_buflen) ? \
2983 (void)strlcpy(errmsg, (str), errmsg_buflen) : (void)0)
2985#define ERRMSG_FMT(...) \
2986 ((errmsg && 0 < errmsg_buflen) ? \
2987 (void)snprintf(errmsg, errmsg_buflen, __VA_ARGS__) : (void)0)
2989static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
2990static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
2991static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
2994save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
2997 VALUE newary, redirection;
2998 int save_fd = redirect_cloexec_dup(fd), cloexec;
2999 if (save_fd == -1) {
3006 newary = sargp->fd_dup2;
3009 sargp->fd_dup2 = newary;
3011 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3016 newary = sargp->fd_close;
3019 sargp->fd_close = newary;
3028intcmp(
const void *a,
const void *b)
3030 return *(
int*)a - *(
int*)b;
3034intrcmp(
const void *a,
const void *b)
3036 return *(
int*)b - *(
int*)a;
3048run_exec_dup2_tmpbuf_size(
long n)
3055fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3059 ret = fcntl(fd, F_GETFD);
3061 ERRMSG(
"fcntl(F_GETFD)");
3064 if (ret & FD_CLOEXEC)
return 1;
3071fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3075 ret = fcntl(fd, F_GETFD);
3077 ERRMSG(
"fcntl(F_GETFD)");
3080 if (!(ret & FD_CLOEXEC)) {
3082 ret = fcntl(fd, F_SETFD, ret);
3084 ERRMSG(
"fcntl(F_SETFD)");
3094fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3098 ret = fcntl(fd, F_GETFD);
3100 ERRMSG(
"fcntl(F_GETFD)");
3103 if (ret & FD_CLOEXEC) {
3105 ret = fcntl(fd, F_SETFD, ret);
3107 ERRMSG(
"fcntl(F_SETFD)");
3117run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3127 for (i = 0; i < n; i++) {
3132 pairs[i].older_index = -1;
3142 for (i = 0; i < n; i++) {
3143 int newfd = pairs[i].newfd;
3147 pairs[i].num_newer = 0;
3149 while (pairs < found && (found-1)->oldfd == newfd)
3151 while (found < pairs+n && found->oldfd == newfd) {
3152 pairs[i].num_newer++;
3153 found->older_index = i;
3160 for (i = 0; i < n; i++) {
3162 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3163 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3165 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3170 if (pairs[j].cloexec &&
3171 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3175 pairs[j].oldfd = -1;
3176 j = pairs[j].older_index;
3178 pairs[j].num_newer--;
3183 for (i = 0; i < n; i++) {
3185 if (pairs[i].oldfd == -1)
3187 if (pairs[i].oldfd == pairs[i].newfd) {
3188 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3190 pairs[i].oldfd = -1;
3193 if (extra_fd == -1) {
3194 extra_fd = redirect_dup(pairs[i].oldfd);
3195 if (extra_fd == -1) {
3201 if (fd_get_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen)) {
3202 if (fd_set_cloexec(extra_fd, errmsg, errmsg_buflen)) {
3210 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3217 pairs[i].oldfd = extra_fd;
3218 j = pairs[i].older_index;
3219 pairs[i].older_index = -1;
3221 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3227 pairs[j].oldfd = -1;
3228 j = pairs[j].older_index;
3231 if (extra_fd != -1) {
3232 ret = redirect_close(extra_fd);
3247run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3255 ret = redirect_close(fd);
3266run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3276 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3278 ret = redirect_dup2(oldfd, newfd);
3291run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3303 pgroup = eargp->pgroup_pgid;
3309 sargp->pgroup_given = 1;
3310 sargp->pgroup_pgid = getpgrp();
3316 ret = setpgid(getpid(), pgroup);
3317 if (ret == -1) ERRMSG(
"setpgid");
3322#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3325run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3334 if (getrlimit(rtype, &rlim) == -1) {
3335 ERRMSG(
"getrlimit");
3339 RLIM2NUM(rlim.rlim_cur),
3340 RLIM2NUM(rlim.rlim_max)));
3341 if (sargp->rlimit_limits ==
Qfalse)
3342 newary = sargp->rlimit_limits = hide_obj(
rb_ary_new());
3344 newary = sargp->rlimit_limits;
3349 if (setrlimit(rtype, &rlim) == -1) {
3350 ERRMSG(
"setrlimit");
3358#if !defined(HAVE_WORKING_FORK)
3371 if (sargp->env_modification ==
Qfalse) {
3372 VALUE env = rb_envtbl();
3377 sargp->env_modification = ary;
3379 sargp->unsetenv_others_given = 1;
3380 sargp->unsetenv_others_do = 1;
3387#define chdir(p) rb_w32_uchdir(p)
3392rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3399 sargp->redirect_fds =
Qnil;
3403 if (eargp->pgroup_given) {
3404 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3409#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3410 obj = eargp->rlimit_limits;
3412 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3417#if !defined(HAVE_WORKING_FORK)
3418 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3423 obj = eargp->env_modification;
3439 if (eargp->umask_given) {
3440 mode_t mask = eargp->umask_mask;
3441 mode_t oldmask = umask(mask);
3443 sargp->umask_given = 1;
3444 sargp->umask_mask = oldmask;
3448 obj = eargp->fd_dup2;
3450 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3454 obj = eargp->fd_close;
3457 rb_warn(
"cannot close fd before spawn");
3459 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3464#ifdef HAVE_WORKING_FORK
3465 if (eargp->close_others_do) {
3470 obj = eargp->fd_dup2_child;
3472 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3476 if (eargp->chdir_given) {
3478 sargp->chdir_given = 1;
3479 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3481 if (chdir(RSTRING_PTR(eargp->chdir_dir)) == -1) {
3488 if (eargp->gid_given) {
3489 if (setgid(eargp->gid) < 0) {
3496 if (eargp->uid_given) {
3497 if (setuid(eargp->uid) < 0) {
3505 VALUE ary = sargp->fd_dup2;
3507 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3511 int preserve =
errno;
3512 stdfd_clear_nonblock();
3521rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3523 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3528exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3530#if !defined(HAVE_WORKING_FORK)
3531 struct rb_execarg sarg, *
const sargp = &sarg;
3537 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3541 if (eargp->use_shell) {
3542 err = proc_exec_sh(RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3545 char *abspath = NULL;
3546 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3547 abspath = RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3548 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3550#if !defined(HAVE_WORKING_FORK)
3551 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3557#ifdef HAVE_WORKING_FORK
3560rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3562 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3566proc_syswait(
VALUE pid)
3573move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3577 for (i = 0; i < n; i++) {
3596pipe_nocrash(
int filedes[2],
VALUE fds)
3604 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3619rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3626handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3632 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3638#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3641 if (!status && !ep) {
3646 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3647 if (status) status->status = state;
3648 if (!state)
return 0;
3657 if (state && !status) rb_jump_tag(state);
3661#define prefork() ( \
3662 rb_io_flush(rb_stdout), \
3663 rb_io_flush(rb_stderr) \
3693write_retry(
int fd,
const void *buf,
size_t len)
3698 w = write(fd, buf,
len);
3699 }
while (w < 0 &&
errno == EINTR);
3705read_retry(
int fd,
void *buf,
size_t len)
3709 if (set_blocking(fd) != 0) {
3711 rb_async_bug_errno(
"set_blocking failed reading child error",
errno);
3716 r = read(fd, buf,
len);
3717 }
while (r < 0 &&
errno == EINTR);
3723send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3728 if (write_retry(fd, &err,
sizeof(err)) < 0) err =
errno;
3729 if (errmsg && 0 < errmsg_buflen) {
3730 errmsg[errmsg_buflen-1] =
'\0';
3731 errmsg_buflen = strlen(errmsg);
3732 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3738recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3742 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3746 if (size ==
sizeof(err) &&
3747 errmsg && 0 < errmsg_buflen) {
3748 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3757#ifdef HAVE_WORKING_VFORK
3758#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3761getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3767 ret = getuidx(ID_SAVED);
3768 if (ret == (rb_uid_t)-1)
3773#define HAVE_GETRESUID
3776#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
3779getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
3785 ret = getgidx(ID_SAVED);
3786 if (ret == (rb_gid_t)-1)
3791#define HAVE_GETRESGID
3794#if !defined(RUBY_ASAN_ENABLED)
3810 rb_uid_t ruid, euid;
3811 rb_gid_t rgid, egid;
3813#if defined HAVE_ISSETUGID
3818#ifdef HAVE_GETRESUID
3822 ret = getresuid(&ruid, &euid, &suid);
3824 rb_sys_fail(
"getresuid(2)");
3833 if (euid == 0 || euid != ruid)
3836#ifdef HAVE_GETRESGID
3840 ret = getresgid(&rgid, &egid, &sgid);
3842 rb_sys_fail(
"getresgid(2)");
3859struct child_handler_disabler_state
3865disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
3867#ifdef HAVE_PTHREAD_SIGMASK
3871 ret = sigfillset(&all);
3873 rb_sys_fail(
"sigfillset");
3875 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
3880# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3885disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
3887#ifdef HAVE_PTHREAD_SIGMASK
3890 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
3895# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3901disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
3906 for (sig = 1; sig < NSIG; sig++) {
3907 sig_t handler = signal(sig, SIG_DFL);
3909 if (handler == SIG_ERR &&
errno == EINVAL) {
3912 if (handler == SIG_ERR) {
3913 ERRMSG(
"signal to obtain old action");
3917 if (sig == SIGPIPE) {
3922 if (handler == SIG_IGN) {
3923 signal(sig, SIG_IGN);
3928 sigemptyset(&old->sigmask);
3929 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
3931 ERRMSG(
"sigprocmask");
3939 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
3940 char *errmsg,
size_t errmsg_buflen,
3944 volatile int try_gc = 1;
3945 struct child_handler_disabler_state old;
3950 disable_child_handler_before_fork(&old);
3954#if defined(HAVE_WORKING_VFORK) && !defined(RUBY_ASAN_ENABLED)
3955 if (!has_privilege())
3965 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
3967 ret = chfunc(charg, errmsg, errmsg_buflen);
3968 if (!ret) _exit(EXIT_SUCCESS);
3970 send_child_error(ep[1], errmsg, errmsg_buflen);
3971#if EXIT_SUCCESS == 127
3972 _exit(EXIT_FAILURE);
3978 disable_child_handler_fork_parent(&old);
3982 if (handle_fork_error(err, status, ep, &try_gc))
3988fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
3989 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
3997 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
3999 if (status) status->status = 0;
4001 if (pipe_nocrash(ep, fds))
return -1;
4003 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4005 if (status) status->pid = pid;
4008 if (status) status->error =
errno;
4015 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4017 if (error_occurred) {
4020 status->error = err;
4022 VM_ASSERT((w == 0) &&
"only used by extensions");
4023 rb_protect(proc_syswait, (
VALUE)pid, &state);
4025 status->status = state;
4046rb_fork_async_signal_safe(
int *status,
4047 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4048 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4052 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4055 *status = process_status.status;
4062rb_fork_ruby(
int *status)
4064 if (UNLIKELY(!rb_ractor_main_p())) {
4065 rb_raise(rb_eRactorIsolationError,
"can not fork from non-main Ractors");
4070 int try_gc = 1, err = 0;
4071 struct child_handler_disabler_state old;
4077 rb_thread_acquire_fork_lock();
4078 disable_child_handler_before_fork(&old);
4081 child.pid = pid = rb_fork();
4082 child.error = err =
errno;
4085 disable_child_handler_fork_parent(&old);
4087#
if defined(__FreeBSD__)
4091 rb_thread_release_fork_lock();
4094 rb_thread_reset_fork_lock();
4096 after_fork_ruby(pid);
4099 }
while (pid < 0 && handle_fork_error(err, &child, NULL, &try_gc) == 0);
4101 if (status) *status = child.status;
4109 rb_pid_t pid = rb_fork_ruby(NULL);
4112 rb_sys_fail(
"fork(2)");
4119call_proc__fork_protected(
VALUE arg)
4128rb_call_proc__fork(
void)
4131 return proc_fork_pid();
4134 rb_pid_t parent = getpid(), pid;
4137 if (
NIL_P(rb_protect(call_proc__fork_protected, (
VALUE)&pid, &state))) {
4138 if (getpid() != parent) {
4148#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4169rb_proc__fork(
VALUE _obj)
4171 rb_pid_t pid = proc_fork_pid();
4240 pid = rb_call_proc__fork();
4254#define rb_proc__fork rb_f_notimplement
4255#define rb_f_fork rb_f_notimplement
4259exit_status_code(
VALUE status)
4265 istatus = EXIT_SUCCESS;
4268 istatus = EXIT_FAILURE;
4272#if EXIT_SUCCESS != 0
4274 istatus = EXIT_SUCCESS;
4281NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4299rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4304 istatus = exit_status_code(argv[0]);
4307 istatus = EXIT_FAILURE;
4317 if (GET_EC()->tag) {
4333 istatus = exit_status_code(argv[0]);
4336 istatus = EXIT_SUCCESS;
4402 VALUE errinfo = rb_ec_get_errinfo(ec);
4403 if (!
NIL_P(errinfo)) {
4404 rb_ec_error_print(ec, errinfo);
4411 args[1] = args[0] = argv[0];
4413 rb_io_puts(1, args, rb_ractor_stderr());
4414 args[0] =
INT2NUM(EXIT_FAILURE);
4452#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4457 if (eargp && !eargp->use_shell) {
4458 VALUE str = eargp->invoke.cmd.argv_str;
4459 VALUE buf = eargp->invoke.cmd.argv_buf;
4460 char *p, **argv = ARGVSTR2ARGV(str);
4461 long i, argc = ARGVSTR2ARGC(str);
4462 const char *start = RSTRING_PTR(buf);
4464 p = RSTRING_PTR(cmd);
4465 for (i = 1; i < argc; ++i) {
4466 p[argv[i] - start - 1] =
' ';
4476rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4479#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4482# if !defined HAVE_SPAWNV
4487#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4488 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4490 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4492 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4496 if (prog && !eargp->use_shell) {
4497 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4498 argv[0] = RSTRING_PTR(prog);
4500# if defined HAVE_SPAWNV
4501 if (eargp->use_shell) {
4502 pid = proc_spawn_sh(RSTRING_PTR(prog));
4505 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4506 pid = proc_spawn_cmd(argv, prog, eargp);
4513 status = system(rb_execarg_commandline(eargp, &prog));
4518 if (eargp->waitpid_state) {
4519 eargp->waitpid_state->pid = pid;
4522 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4537do_spawn_process(
VALUE arg)
4541 rb_execarg_parent_start1(argp->execarg);
4543 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4544 argp->errmsg.ptr, argp->errmsg.buflen);
4547NOINLINE(
static rb_pid_t
4548 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4551rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4555 args.execarg = execarg_obj;
4556 args.errmsg.ptr = errmsg;
4557 args.errmsg.buflen = errmsg_buflen;
4560 execarg_parent_end, execarg_obj);
4565rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4569 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4570 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4576 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4582 return rb_spawn_internal(argc, argv, NULL, 0);
4704 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4705 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4708 eargp->status = &status;
4710 last_status_clear(th);
4714 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4721 th->last_status = status;
4723 if (data->status == EXIT_SUCCESS) {
4727 if (data->error != 0) {
4728 if (eargp->exception) {
4729 VALUE command = eargp->invoke.sh.shell_script;
4737 else if (eargp->exception) {
4738 VALUE command = eargp->invoke.sh.shell_script;
4752 if (eargp->exception) {
4753 VALUE command = eargp->invoke.sh.shell_script;
4869 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
4870 VALUE execarg_obj, fail_str;
4873 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4874 eargp = rb_execarg_get(execarg_obj);
4875 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4877 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
4881 rb_exec_fail(eargp, err, errmsg);
4885#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
4911 time_t beg = time(0);
4914 if (scheduler !=
Qnil) {
4918 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
4927 time_t end = time(0) - beg;
4929 return TIMET2NUM(end);
4933#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
4950#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
4952 if (pgrp < 0) rb_sys_fail(0);
4956 if (pgrp < 0) rb_sys_fail(0);
4961#define proc_getpgrp rb_f_notimplement
4965#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
4983 if (setpgid(0,0) < 0) rb_sys_fail(0);
4984#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
4985 if (setpgrp() < 0) rb_sys_fail(0);
4990#define proc_setpgrp rb_f_notimplement
4994#if defined(HAVE_GETPGID)
5012 if (i < 0) rb_sys_fail(0);
5016#define proc_getpgid rb_f_notimplement
5034 rb_pid_t ipid, ipgrp;
5039 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5043#define proc_setpgid rb_f_notimplement
5071 if (sid < 0) rb_sys_fail(0);
5075#define proc_getsid rb_f_notimplement
5079#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5080#if !defined(HAVE_SETSID)
5081static rb_pid_t ruby_setsid(
void);
5082#define setsid() ruby_setsid()
5103 if (pid < 0) rb_sys_fail(0);
5107#if !defined(HAVE_SETSID)
5108#define HAVE_SETSID 1
5116#if defined(SETPGRP_VOID)
5122 ret = setpgrp(0, pid);
5124 if (ret == -1)
return -1;
5128 ioctl(fd, TIOCNOTTY, NULL);
5135#define proc_setsid rb_f_notimplement
5139#ifdef HAVE_GETPRIORITY
5167 int prio, iwhich, iwho;
5173 prio = getpriority(iwhich, iwho);
5174 if (
errno) rb_sys_fail(0);
5178#define proc_getpriority rb_f_notimplement
5182#ifdef HAVE_GETPRIORITY
5202 int iwhich, iwho, iprio;
5208 if (setpriority(iwhich, iwho, iprio) < 0)
5213#define proc_setpriority rb_f_notimplement
5216#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5218rlimit_resource_name2int(
const char *name,
long len,
int casetype)
5222#define RESCHECK(r) \
5224 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5225 resource = RLIMIT_##r; \
5259#ifdef RLIMIT_MEMLOCK
5262#ifdef RLIMIT_MSGQUEUE
5301#ifdef RLIMIT_SIGPENDING
5302 RESCHECK(SIGPENDING);
5311 for (p = name; *p; p++)
5317 for (p = name; *p; p++)
5323 rb_bug(
"unexpected casetype");
5330rlimit_type_by_hname(
const char *name,
long len)
5332 return rlimit_resource_name2int(name,
len, 0);
5336rlimit_type_by_lname(
const char *name,
long len)
5338 return rlimit_resource_name2int(name,
len, 1);
5342rlimit_type_by_sym(
VALUE key)
5345 const char *rname = RSTRING_PTR(name);
5346 long len = RSTRING_LEN(name);
5348 static const char prefix[] =
"rlimit_";
5349 enum {prefix_len =
sizeof(prefix)-1};
5351 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5352 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5360rlimit_resource_type(
VALUE rtype)
5367 switch (
TYPE(rtype)) {
5370 name = RSTRING_PTR(v);
5371 len = RSTRING_LEN(v);
5380 len = RSTRING_LEN(rtype);
5390 r = rlimit_type_by_hname(name,
len);
5394 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5400rlimit_resource_value(
VALUE rval)
5405 switch (
TYPE(rval)) {
5408 name = RSTRING_PTR(v);
5423 return NUM2RLIM(rval);
5427 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5429#ifdef RLIM_SAVED_MAX
5430 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5432#ifdef RLIM_SAVED_CUR
5433 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5435 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5441#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5469 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5470 rb_sys_fail(
"getrlimit");
5472 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5475#define proc_getrlimit rb_f_notimplement
5478#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5532proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5534 VALUE resource, rlim_cur, rlim_max;
5540 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5541 rlim_max = rlim_cur;
5543 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5544 rlim.rlim_max = rlimit_resource_value(rlim_max);
5546 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5547 rb_sys_fail(
"setrlimit");
5552#define proc_setrlimit rb_f_notimplement
5555static int under_uid_switch = 0;
5557check_uid_switch(
void)
5559 if (under_uid_switch) {
5560 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5564static int under_gid_switch = 0;
5566check_gid_switch(
void)
5568 if (under_gid_switch) {
5569 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5574#if defined(HAVE_PWD_H)
5576login_not_found(
int err)
5578 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5589# if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5592 char MAYBE_UNUSED(*login) = NULL;
5594# ifdef USE_GETLOGIN_R
5596# if defined(__FreeBSD__)
5597 typedef int getlogin_r_size_t;
5599 typedef size_t getlogin_r_size_t;
5602 long loginsize = GETLOGIN_R_SIZE_INIT;
5605 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5609 login = RSTRING_PTR(maybe_result);
5614 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5615 if (login_not_found(gle)) {
5616 rb_str_resize(maybe_result, 0);
5620 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5621 rb_str_resize(maybe_result, 0);
5626 login = RSTRING_PTR(maybe_result);
5630 if (login == NULL) {
5631 rb_str_resize(maybe_result, 0);
5636 return maybe_result;
5638# elif defined(USE_GETLOGIN)
5644 if (login_not_found(err)) {
5658pwd_not_found(
int err)
5672# if defined(USE_GETPWNAM_R)
5673struct getpwnam_r_args {
5677 struct passwd *result;
5678 struct passwd pwstore;
5681# define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5682 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5685nogvl_getpwnam_r(
void *args)
5687 struct getpwnam_r_args *arg = args;
5688 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5693rb_getpwdirnam_for_login(
VALUE login_name)
5695#if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5699 if (
NIL_P(login_name)) {
5704 const char *login = RSTRING_PTR(login_name);
5707# ifdef USE_GETPWNAM_R
5710 long bufsizenm = GETPW_R_SIZE_INIT;
5713 bufsizenm = GETPW_R_SIZE_DEFAULT;
5717 bufnm = RSTRING_PTR(getpwnm_tmp);
5720 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5723 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5724 if (pwd_not_found(enm)) {
5725 rb_str_resize(getpwnm_tmp, 0);
5729 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5730 rb_str_resize(getpwnm_tmp, 0);
5735 args.buf = RSTRING_PTR(getpwnm_tmp);
5739 if (args.result == NULL) {
5741 rb_str_resize(getpwnm_tmp, 0);
5747 rb_str_resize(getpwnm_tmp, 0);
5750# elif defined(USE_GETPWNAM)
5752 struct passwd *pwptr;
5754 if (!(pwptr = getpwnam(login))) {
5757 if (pwd_not_found(err)) {
5771# if defined(USE_GETPWUID_R)
5772struct getpwuid_r_args {
5776 struct passwd *result;
5777 struct passwd pwstore;
5780# define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5781 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5784nogvl_getpwuid_r(
void *args)
5786 struct getpwuid_r_args *arg = args;
5787 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5797# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5801 uid_t ruid = getuid();
5803# ifdef USE_GETPWUID_R
5806 long bufsizeid = GETPW_R_SIZE_INIT;
5809 bufsizeid = GETPW_R_SIZE_DEFAULT;
5813 bufid = RSTRING_PTR(getpwid_tmp);
5816 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5819 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5820 if (pwd_not_found(eid)) {
5821 rb_str_resize(getpwid_tmp, 0);
5825 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5826 rb_str_resize(getpwid_tmp, 0);
5831 args.buf = RSTRING_PTR(getpwid_tmp);
5835 if (args.result == NULL) {
5837 rb_str_resize(getpwid_tmp, 0);
5843 rb_str_resize(getpwid_tmp, 0);
5846# elif defined(USE_GETPWUID)
5848 struct passwd *pwptr;
5850 if (!(pwptr = getpwuid(ruid))) {
5853 if (pwd_not_found(err)) {
5879#if defined(HAVE_PWD_H)
5882# ifdef USE_GETPWNAM_R
5895 struct passwd *pwptr;
5896#ifdef USE_GETPWNAM_R
5901 getpw_buf_len = GETPW_R_SIZE_INIT;
5902 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5905 getpw_buf = RSTRING_PTR(*getpw_tmp);
5909 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
5911 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5912 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5913 rb_str_resize(*getpw_tmp, 0);
5917 args.buf = RSTRING_PTR(*getpw_tmp);
5920 pwptr = args.result;
5922 pwptr = getpwnam(usrname);
5925#ifndef USE_GETPWNAM_R
5928 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
5930 uid = pwptr->pw_uid;
5931#ifndef USE_GETPWNAM_R
5938# ifdef p_uid_from_name
5958#if defined(HAVE_GRP_H)
5959# if defined(USE_GETGRNAM_R)
5960struct getgrnam_r_args {
5964 struct group *result;
5968# define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
5969 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5972nogvl_getgrnam_r(
void *args)
5974 struct getgrnam_r_args *arg = args;
5975 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
5981# ifdef USE_GETGRNAM_R
5994 struct group *grptr;
5995#ifdef USE_GETGRNAM_R
6000 getgr_buf_len = GETGR_R_SIZE_INIT;
6001 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6004 getgr_buf = RSTRING_PTR(*getgr_tmp);
6008 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6010 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6011 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6012 rb_str_resize(*getgr_tmp, 0);
6016 args.buf = RSTRING_PTR(*getgr_tmp);
6019 grptr = args.result;
6020#elif defined(HAVE_GETGRNAM)
6021 grptr = getgrnam(grpname);
6026#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6029 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6031 gid = grptr->gr_gid;
6032#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6039# ifdef p_gid_from_name
6059#if defined HAVE_SETUID
6073 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6077#define p_sys_setuid rb_f_notimplement
6081#if defined HAVE_SETRUID
6095 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6099#define p_sys_setruid rb_f_notimplement
6103#if defined HAVE_SETEUID
6117 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6121#define p_sys_seteuid rb_f_notimplement
6125#if defined HAVE_SETREUID
6140 rb_uid_t ruid, euid;
6143 ruid = OBJ2UID1(rid);
6144 euid = OBJ2UID1(eid);
6146 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6150#define p_sys_setreuid rb_f_notimplement
6154#if defined HAVE_SETRESUID
6169 rb_uid_t ruid, euid, suid;
6172 ruid = OBJ2UID1(rid);
6173 euid = OBJ2UID1(eid);
6174 suid = OBJ2UID1(sid);
6176 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6180#define p_sys_setresuid rb_f_notimplement
6197proc_getuid(
VALUE obj)
6199 rb_uid_t uid = getuid();
6204#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6224#if defined(HAVE_SETRESUID)
6225 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6226#elif defined HAVE_SETREUID
6227 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6228#elif defined HAVE_SETRUID
6229 if (setruid(uid) < 0) rb_sys_fail(0);
6230#elif defined HAVE_SETUID
6232 if (geteuid() == uid) {
6233 if (setuid(uid) < 0) rb_sys_fail(0);
6243#define proc_setuid rb_f_notimplement
6257static rb_uid_t SAVED_USER_ID = -1;
6281 if (geteuid() == 0) {
6282#if defined(HAVE_SETRESUID)
6283 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6284 SAVED_USER_ID = uid;
6285#elif defined(HAVE_SETUID)
6286 if (setuid(uid) < 0) rb_sys_fail(0);
6287 SAVED_USER_ID = uid;
6288#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6289 if (getuid() == uid) {
6290 if (SAVED_USER_ID == uid) {
6291 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6295 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6296 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6298 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6299 SAVED_USER_ID = uid;
6302 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6304 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6305 SAVED_USER_ID = uid;
6310 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6311 SAVED_USER_ID = uid;
6313#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6314 if (getuid() == uid) {
6315 if (SAVED_USER_ID == uid) {
6316 if (seteuid(uid) < 0) rb_sys_fail(0);
6320 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6322 if (setruid(0) < 0) rb_sys_fail(0);
6325 if (setruid(0) < 0) rb_sys_fail(0);
6327 if (seteuid(uid) < 0) rb_sys_fail(0);
6328 if (setruid(uid) < 0) rb_sys_fail(0);
6329 SAVED_USER_ID = uid;
6334 if (seteuid(uid) < 0) rb_sys_fail(0);
6335 if (setruid(uid) < 0) rb_sys_fail(0);
6336 SAVED_USER_ID = uid;
6344#if defined(HAVE_SETRESUID)
6345 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6346 (geteuid() == uid)? (rb_uid_t)-1: uid,
6347 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6348 SAVED_USER_ID = uid;
6349#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6350 if (SAVED_USER_ID == uid) {
6351 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6352 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6355 else if (getuid() != uid) {
6356 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6358 SAVED_USER_ID = uid;
6360 else if ( geteuid() != uid) {
6361 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6362 SAVED_USER_ID = uid;
6363 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6366 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6367 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6368 SAVED_USER_ID = uid;
6369 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6371#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6372 if (SAVED_USER_ID == uid) {
6373 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6374 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6376 else if ( geteuid() == uid) {
6377 if (getuid() != uid) {
6378 if (setruid(uid) < 0) rb_sys_fail(0);
6379 SAVED_USER_ID = uid;
6382 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6383 SAVED_USER_ID = uid;
6384 if (setruid(uid) < 0) rb_sys_fail(0);
6387 else if ( getuid() == uid) {
6388 if (seteuid(uid) < 0) rb_sys_fail(0);
6389 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6390 SAVED_USER_ID = uid;
6391 if (setruid(uid) < 0) rb_sys_fail(0);
6396#elif defined HAVE_44BSD_SETUID
6397 if (getuid() == uid) {
6399 if (setuid(uid) < 0) rb_sys_fail(0);
6400 SAVED_USER_ID = uid;
6405#elif defined HAVE_SETEUID
6406 if (getuid() == uid && SAVED_USER_ID == uid) {
6407 if (seteuid(uid) < 0) rb_sys_fail(0);
6412#elif defined HAVE_SETUID
6413 if (getuid() == uid && SAVED_USER_ID == uid) {
6414 if (setuid(uid) < 0) rb_sys_fail(0);
6428#if defined HAVE_SETGID
6442 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6446#define p_sys_setgid rb_f_notimplement
6450#if defined HAVE_SETRGID
6464 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6468#define p_sys_setrgid rb_f_notimplement
6472#if defined HAVE_SETEGID
6486 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6490#define p_sys_setegid rb_f_notimplement
6494#if defined HAVE_SETREGID
6509 rb_gid_t rgid, egid;
6511 rgid = OBJ2GID(rid);
6512 egid = OBJ2GID(eid);
6513 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6517#define p_sys_setregid rb_f_notimplement
6520#if defined HAVE_SETRESGID
6535 rb_gid_t rgid, egid, sgid;
6537 rgid = OBJ2GID(rid);
6538 egid = OBJ2GID(eid);
6539 sgid = OBJ2GID(sid);
6540 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6544#define p_sys_setresgid rb_f_notimplement
6548#if defined HAVE_ISSETUGID
6562p_sys_issetugid(
VALUE obj)
6564 return RBOOL(issetugid());
6567#define p_sys_issetugid rb_f_notimplement
6584proc_getgid(
VALUE obj)
6586 rb_gid_t gid = getgid();
6591#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6610#if defined(HAVE_SETRESGID)
6611 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6612#elif defined HAVE_SETREGID
6613 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6614#elif defined HAVE_SETRGID
6615 if (setrgid(gid) < 0) rb_sys_fail(0);
6616#elif defined HAVE_SETGID
6618 if (getegid() == gid) {
6619 if (setgid(gid) < 0) rb_sys_fail(0);
6629#define proc_setgid rb_f_notimplement
6633#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6653static int _maxgroups = -1;
6655get_sc_ngroups_max(
void)
6657#ifdef _SC_NGROUPS_MAX
6658 return (
int)sysconf(_SC_NGROUPS_MAX);
6659#elif defined(NGROUPS_MAX)
6660 return (
int)NGROUPS_MAX;
6668 if (_maxgroups < 0) {
6669 _maxgroups = get_sc_ngroups_max();
6671 _maxgroups = RB_MAX_GROUPS;
6680#ifdef HAVE_GETGROUPS
6704proc_getgroups(
VALUE obj)
6710 ngroups = getgroups(0, NULL);
6714 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6716 ngroups = getgroups(ngroups, groups);
6721 for (i = 0; i < ngroups; i++)
6729#define proc_getgroups rb_f_notimplement
6733#ifdef HAVE_SETGROUPS
6758 if (ngroups > maxgroups())
6759 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6761 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6763 for (i = 0; i < ngroups; i++) {
6766 groups[i] = OBJ2GID1(g);
6770 if (setgroups(ngroups, groups) == -1)
6775 return proc_getgroups(obj);
6778#define proc_setgroups rb_f_notimplement
6782#ifdef HAVE_INITGROUPS
6808 return proc_getgroups(obj);
6811#define proc_initgroups rb_f_notimplement
6814#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6827proc_getmaxgroups(
VALUE obj)
6832#define proc_getmaxgroups rb_f_notimplement
6835#ifdef HAVE_SETGROUPS
6848 int ngroups_max = get_sc_ngroups_max();
6851 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6853 if (ngroups > RB_MAX_GROUPS)
6854 ngroups = RB_MAX_GROUPS;
6856 if (ngroups_max > 0 && ngroups > ngroups_max)
6857 ngroups = ngroups_max;
6859 _maxgroups = ngroups;
6864#define proc_setmaxgroups rb_f_notimplement
6867#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6868static int rb_daemon(
int nochdir,
int noclose);
6893 int n, nochdir = FALSE, noclose = FALSE;
6896 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6897 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6901 n = rb_daemon(nochdir, noclose);
6902 if (n < 0) rb_sys_fail(
"daemon");
6906extern const char ruby_null_device[];
6909rb_daemon(
int nochdir,
int noclose)
6914 err = daemon(nochdir, noclose);
6919 switch (rb_fork_ruby(NULL)) {
6922 default: _exit(EXIT_SUCCESS);
6926 if (setsid() < 0) (void)0;
6931 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
6943#define proc_daemon rb_f_notimplement
6956static rb_gid_t SAVED_GROUP_ID = -1;
6980 if (geteuid() == 0) {
6981#if defined(HAVE_SETRESGID)
6982 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
6983 SAVED_GROUP_ID = gid;
6984#elif defined HAVE_SETGID
6985 if (setgid(gid) < 0) rb_sys_fail(0);
6986 SAVED_GROUP_ID = gid;
6987#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
6988 if (getgid() == gid) {
6989 if (SAVED_GROUP_ID == gid) {
6990 if (setregid(-1, gid) < 0) rb_sys_fail(0);
6994 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
6995 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
6997 if (setregid(gid, gid) < 0) rb_sys_fail(0);
6998 SAVED_GROUP_ID = gid;
7001 if (setregid(0, 0) < 0) rb_sys_fail(0);
7003 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7004 SAVED_GROUP_ID = gid;
7009 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7010 SAVED_GROUP_ID = gid;
7012#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7013 if (getgid() == gid) {
7014 if (SAVED_GROUP_ID == gid) {
7015 if (setegid(gid) < 0) rb_sys_fail(0);
7019 if (setegid(gid) < 0) rb_sys_fail(0);
7020 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7022 if (setrgid(0) < 0) rb_sys_fail(0);
7025 if (setrgid(0) < 0) rb_sys_fail(0);
7027 if (setegid(gid) < 0) rb_sys_fail(0);
7028 if (setrgid(gid) < 0) rb_sys_fail(0);
7029 SAVED_GROUP_ID = gid;
7034 if (setegid(gid) < 0) rb_sys_fail(0);
7035 if (setrgid(gid) < 0) rb_sys_fail(0);
7036 SAVED_GROUP_ID = gid;
7043#if defined(HAVE_SETRESGID)
7044 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7045 (getegid() == gid)? (rb_gid_t)-1: gid,
7046 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7047 SAVED_GROUP_ID = gid;
7048#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7049 if (SAVED_GROUP_ID == gid) {
7050 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7051 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7054 else if (getgid() != gid) {
7055 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7057 SAVED_GROUP_ID = gid;
7059 else if ( getegid() != gid) {
7060 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7061 SAVED_GROUP_ID = gid;
7062 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7065 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7066 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7067 SAVED_GROUP_ID = gid;
7068 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7070#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7071 if (SAVED_GROUP_ID == gid) {
7072 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7073 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7075 else if ( getegid() == gid) {
7076 if (getgid() != gid) {
7077 if (setrgid(gid) < 0) rb_sys_fail(0);
7078 SAVED_GROUP_ID = gid;
7081 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7082 SAVED_GROUP_ID = gid;
7083 if (setrgid(gid) < 0) rb_sys_fail(0);
7086 else if ( getgid() == gid) {
7087 if (setegid(gid) < 0) rb_sys_fail(0);
7088 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7089 SAVED_GROUP_ID = gid;
7090 if (setrgid(gid) < 0) rb_sys_fail(0);
7095#elif defined HAVE_44BSD_SETGID
7096 if (getgid() == gid) {
7098 if (setgid(gid) < 0) rb_sys_fail(0);
7099 SAVED_GROUP_ID = gid;
7104#elif defined HAVE_SETEGID
7105 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7106 if (setegid(gid) < 0) rb_sys_fail(0);
7111#elif defined HAVE_SETGID
7112 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7113 if (setgid(gid) < 0) rb_sys_fail(0);
7140proc_geteuid(
VALUE obj)
7142 rb_uid_t euid = geteuid();
7146#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7148proc_seteuid(rb_uid_t uid)
7150#if defined(HAVE_SETRESUID)
7151 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7152#elif defined HAVE_SETREUID
7153 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7154#elif defined HAVE_SETEUID
7155 if (seteuid(uid) < 0) rb_sys_fail(0);
7156#elif defined HAVE_SETUID
7157 if (uid == getuid()) {
7158 if (setuid(uid) < 0) rb_sys_fail(0);
7169#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7183 proc_seteuid(OBJ2UID(euid));
7187#define proc_seteuid_m rb_f_notimplement
7191rb_seteuid_core(rb_uid_t euid)
7193#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7199#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7203#if defined(HAVE_SETRESUID)
7205 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7206 SAVED_USER_ID = euid;
7209 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7211#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7212 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7214 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7215 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7216 SAVED_USER_ID = euid;
7218#elif defined HAVE_SETEUID
7219 if (seteuid(euid) < 0) rb_sys_fail(0);
7220#elif defined HAVE_SETUID
7221 if (geteuid() == 0) rb_sys_fail(0);
7222 if (setuid(euid) < 0) rb_sys_fail(0);
7247 rb_seteuid_core(OBJ2UID(
id));
7266proc_getegid(
VALUE obj)
7268 rb_gid_t egid = getegid();
7273#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7286#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7292#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7293 gid = OBJ2GID(egid);
7296#if defined(HAVE_SETRESGID)
7297 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7298#elif defined HAVE_SETREGID
7299 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7300#elif defined HAVE_SETEGID
7301 if (setegid(gid) < 0) rb_sys_fail(0);
7302#elif defined HAVE_SETGID
7303 if (gid == getgid()) {
7304 if (setgid(gid) < 0) rb_sys_fail(0);
7316#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7317#define proc_setegid_m proc_setegid
7319#define proc_setegid_m rb_f_notimplement
7323rb_setegid_core(rb_gid_t egid)
7325#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7331#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7335#if defined(HAVE_SETRESGID)
7337 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7338 SAVED_GROUP_ID = egid;
7341 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7343#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7344 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7346 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7347 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7348 SAVED_GROUP_ID = egid;
7350#elif defined HAVE_SETEGID
7351 if (setegid(egid) < 0) rb_sys_fail(0);
7352#elif defined HAVE_SETGID
7353 if (geteuid() == 0 ) rb_sys_fail(0);
7354 if (setgid(egid) < 0) rb_sys_fail(0);
7379 rb_setegid_core(OBJ2GID(
id));
7394p_uid_exchangeable(
VALUE _)
7396#if defined(HAVE_SETRESUID)
7398#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7419p_uid_exchange(
VALUE obj)
7422#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7429#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7433#if defined(HAVE_SETRESUID)
7434 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7435 SAVED_USER_ID = uid;
7436#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7437 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7438 SAVED_USER_ID = uid;
7456p_gid_exchangeable(
VALUE _)
7458#if defined(HAVE_SETRESGID)
7460#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7481p_gid_exchange(
VALUE obj)
7484#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7491#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7495#if defined(HAVE_SETRESGID)
7496 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7497 SAVED_GROUP_ID = gid;
7498#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7499 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7500 SAVED_GROUP_ID = gid;
7519p_uid_have_saved_id(
VALUE _)
7521#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7529#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7531p_uid_sw_ensure(
VALUE i)
7533 rb_uid_t
id = (rb_uid_t)i;
7534 under_uid_switch = 0;
7535 id = rb_seteuid_core(
id);
7554p_uid_switch(
VALUE obj)
7566 under_uid_switch = 1;
7573 else if (euid != SAVED_USER_ID) {
7574 proc_seteuid(SAVED_USER_ID);
7576 under_uid_switch = 1;
7591p_uid_sw_ensure(
VALUE obj)
7593 under_uid_switch = 0;
7594 return p_uid_exchange(obj);
7598p_uid_switch(
VALUE obj)
7610 p_uid_exchange(obj);
7612 under_uid_switch = 1;
7634p_gid_have_saved_id(
VALUE _)
7636#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7643#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7645p_gid_sw_ensure(
VALUE i)
7647 rb_gid_t
id = (rb_gid_t)i;
7648 under_gid_switch = 0;
7649 id = rb_setegid_core(
id);
7668p_gid_switch(
VALUE obj)
7680 under_gid_switch = 1;
7687 else if (egid != SAVED_GROUP_ID) {
7688 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7690 under_gid_switch = 1;
7705p_gid_sw_ensure(
VALUE obj)
7707 under_gid_switch = 0;
7708 return p_gid_exchange(obj);
7712p_gid_switch(
VALUE obj)
7724 p_gid_exchange(obj);
7726 under_gid_switch = 1;
7736#if defined(HAVE_TIMES)
7740#ifdef HAVE__SC_CLK_TCK
7741 return sysconf(_SC_CLK_TCK);
7742#elif defined CLK_TCK
7765rb_proc_times(
VALUE obj)
7767 VALUE utime, stime, cutime, cstime, ret;
7768#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7769 struct rusage usage_s, usage_c;
7771 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7772 rb_sys_fail(
"getrusage");
7773 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7774 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7775 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7776 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7778 const double hertz = (double)get_clk_tck();
7782 utime =
DBL2NUM(buf.tms_utime / hertz);
7783 stime =
DBL2NUM(buf.tms_stime / hertz);
7784 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7785 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7787 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7795#define rb_proc_times rb_f_notimplement
7798#ifdef HAVE_LONG_LONG
7800#define TIMETICK_INT_MIN LLONG_MIN
7801#define TIMETICK_INT_MAX LLONG_MAX
7802#define TIMETICK_INT2NUM(v) LL2NUM(v)
7803#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7805typedef long timetick_int_t;
7806#define TIMETICK_INT_MIN LONG_MIN
7807#define TIMETICK_INT_MAX LONG_MAX
7808#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7809#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7812CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7813static timetick_int_t
7814gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7834reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7836 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7844reduce_factors(timetick_int_t *numerators,
int num_numerators,
7845 timetick_int_t *denominators,
int num_denominators)
7848 for (i = 0; i < num_numerators; i++) {
7849 if (numerators[i] == 1)
7851 for (j = 0; j < num_denominators; j++) {
7852 if (denominators[j] == 1)
7854 reduce_fraction(&numerators[i], &denominators[j]);
7860 timetick_int_t giga_count;
7865timetick2dblnum(
struct timetick *ttp,
7866 timetick_int_t *numerators,
int num_numerators,
7867 timetick_int_t *denominators,
int num_denominators)
7872 reduce_factors(numerators, num_numerators,
7873 denominators, num_denominators);
7875 d = ttp->giga_count * 1e9 + ttp->count;
7877 for (i = 0; i < num_numerators; i++)
7879 for (i = 0; i < num_denominators; i++)
7880 d /= denominators[i];
7886timetick2dblnum_reciprocal(
struct timetick *ttp,
7887 timetick_int_t *numerators,
int num_numerators,
7888 timetick_int_t *denominators,
int num_denominators)
7893 reduce_factors(numerators, num_numerators,
7894 denominators, num_denominators);
7897 for (i = 0; i < num_denominators; i++)
7898 d *= denominators[i];
7899 for (i = 0; i < num_numerators; i++)
7901 d /= ttp->giga_count * 1e9 + ttp->count;
7906#define NDIV(x,y) (-(-((x)+1)/(y))-1)
7907#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
7910timetick2integer(
struct timetick *ttp,
7911 timetick_int_t *numerators,
int num_numerators,
7912 timetick_int_t *denominators,
int num_denominators)
7917 reduce_factors(numerators, num_numerators,
7918 denominators, num_denominators);
7920 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
7921 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
7922 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
7923 for (i = 0; i < num_numerators; i++) {
7924 timetick_int_t factor = numerators[i];
7925 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
7929 for (i = 0; i < num_denominators; i++) {
7930 t = DIV(t, denominators[i]);
7932 return TIMETICK_INT2NUM(t);
7936 v = TIMETICK_INT2NUM(ttp->giga_count);
7939 for (i = 0; i < num_numerators; i++) {
7940 timetick_int_t factor = numerators[i];
7943 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
7945 for (i = 0; i < num_denominators; i++) {
7946 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
7952make_clock_result(
struct timetick *ttp,
7953 timetick_int_t *numerators,
int num_numerators,
7954 timetick_int_t *denominators,
int num_denominators,
7957 if (unit ==
ID2SYM(id_nanosecond)) {
7958 numerators[num_numerators++] = 1000000000;
7959 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7961 else if (unit ==
ID2SYM(id_microsecond)) {
7962 numerators[num_numerators++] = 1000000;
7963 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7965 else if (unit ==
ID2SYM(id_millisecond)) {
7966 numerators[num_numerators++] = 1000;
7967 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7969 else if (unit ==
ID2SYM(id_second)) {
7970 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7972 else if (unit ==
ID2SYM(id_float_microsecond)) {
7973 numerators[num_numerators++] = 1000000;
7974 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7976 else if (unit ==
ID2SYM(id_float_millisecond)) {
7977 numerators[num_numerators++] = 1000;
7978 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7980 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
7981 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7984 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
7988static const mach_timebase_info_data_t *
7989get_mach_timebase_info(
void)
7991 static mach_timebase_info_data_t sTimebaseInfo;
7993 if ( sTimebaseInfo.denom == 0 ) {
7994 (void) mach_timebase_info(&sTimebaseInfo);
7997 return &sTimebaseInfo;
8001ruby_real_ms_time(
void)
8003 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8004 uint64_t t = mach_absolute_time();
8005 return (
double)t * info->numer / info->denom / 1e6;
8009#if defined(NUM2CLOCKID)
8010# define NUMERIC_CLOCKID 1
8012# define NUMERIC_CLOCKID 0
8013# define NUM2CLOCKID(x) 0
8016#define clock_failed(name, err, arg) do { \
8017 int clock_error = (err); \
8018 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8198 timetick_int_t numerators[2];
8199 timetick_int_t denominators[2];
8200 int num_numerators = 0;
8201 int num_denominators = 0;
8204 VALUE clk_id = argv[0];
8205#ifdef HAVE_CLOCK_GETTIME
8210#ifdef CLOCK_REALTIME
8211 if (clk_id == RUBY_CLOCK_REALTIME) {
8217#ifdef CLOCK_MONOTONIC
8218 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8219 c = CLOCK_MONOTONIC;
8224#ifdef CLOCK_PROCESS_CPUTIME_ID
8225 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8226 c = CLOCK_PROCESS_CPUTIME_ID;
8231#ifdef CLOCK_THREAD_CPUTIME_ID
8232 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8233 c = CLOCK_THREAD_CPUTIME_ID;
8241#ifdef HAVE_GETTIMEOFDAY
8246#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8247 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8249 ret = gettimeofday(&tv, 0);
8251 rb_sys_fail(
"gettimeofday");
8252 tt.giga_count = tv.tv_sec;
8253 tt.count = (int32_t)tv.tv_usec * 1000;
8254 denominators[num_denominators++] = 1000000000;
8259#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8260 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8263 if (t == (time_t)-1)
8264 rb_sys_fail(
"time");
8267 denominators[num_denominators++] = 1000000000;
8272#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8273 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8274 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8277 unsigned_clock_t uc;
8279 if (c == (clock_t)-1)
8280 rb_sys_fail(
"times");
8281 uc = (unsigned_clock_t)c;
8282 tt.count = (int32_t)(uc % 1000000000);
8283 tt.giga_count = (uc / 1000000000);
8284 denominators[num_denominators++] = get_clk_tck();
8290#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8291 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8292 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8293 struct rusage usage;
8295 ret = getrusage(RUSAGE_SELF, &usage);
8297 rb_sys_fail(
"getrusage");
8298 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8299 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8300 if (1000000 <= usec) {
8304 tt.count = usec * 1000;
8305 denominators[num_denominators++] = 1000000000;
8311#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8312 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8313 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8315 unsigned_clock_t utime, stime;
8316 if (times(&buf) == (clock_t)-1)
8317 rb_sys_fail(
"times");
8318 utime = (unsigned_clock_t)buf.tms_utime;
8319 stime = (unsigned_clock_t)buf.tms_stime;
8320 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8321 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8322 if (1000000000 <= tt.count) {
8323 tt.count -= 1000000000;
8326 denominators[num_denominators++] = get_clk_tck();
8331#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8332 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8333 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8335 unsigned_clock_t uc;
8338 if (c == (clock_t)-1)
8339 rb_sys_fail(
"clock");
8340 uc = (unsigned_clock_t)c;
8341 tt.count = (int32_t)(uc % 1000000000);
8342 tt.giga_count = uc / 1000000000;
8343 denominators[num_denominators++] = CLOCKS_PER_SEC;
8348 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8349 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8350 uint64_t t = mach_absolute_time();
8351 tt.count = (int32_t)(t % 1000000000);
8352 tt.giga_count = t / 1000000000;
8353 numerators[num_numerators++] = info->numer;
8354 denominators[num_denominators++] = info->denom;
8355 denominators[num_denominators++] = 1000000000;
8360 else if (NUMERIC_CLOCKID) {
8361#if defined(HAVE_CLOCK_GETTIME)
8363 c = NUM2CLOCKID(clk_id);
8365 ret = clock_gettime(c, &ts);
8367 clock_failed(
"gettime",
errno, clk_id);
8368 tt.count = (int32_t)ts.tv_nsec;
8369 tt.giga_count = ts.tv_sec;
8370 denominators[num_denominators++] = 1000000000;
8377 clock_failed(
"gettime", EINVAL, clk_id);
8380 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8425 timetick_int_t numerators[2];
8426 timetick_int_t denominators[2];
8427 int num_numerators = 0;
8428 int num_denominators = 0;
8429#ifdef HAVE_CLOCK_GETRES
8434 VALUE clk_id = argv[0];
8437#ifdef CLOCK_REALTIME
8438 if (clk_id == RUBY_CLOCK_REALTIME) {
8444#ifdef CLOCK_MONOTONIC
8445 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8446 c = CLOCK_MONOTONIC;
8451#ifdef CLOCK_PROCESS_CPUTIME_ID
8452 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8453 c = CLOCK_PROCESS_CPUTIME_ID;
8458#ifdef CLOCK_THREAD_CPUTIME_ID
8459 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8460 c = CLOCK_THREAD_CPUTIME_ID;
8465#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8466 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8469 denominators[num_denominators++] = 1000000000;
8474#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8475 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8478 denominators[num_denominators++] = 1000000000;
8483#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8484 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8487 denominators[num_denominators++] = get_clk_tck();
8492#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8493 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8496 denominators[num_denominators++] = 1000000000;
8501#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8502 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8505 denominators[num_denominators++] = get_clk_tck();
8510#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8511 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8514 denominators[num_denominators++] = CLOCKS_PER_SEC;
8519#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8520 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8521 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8524 numerators[num_numerators++] = info->numer;
8525 denominators[num_denominators++] = info->denom;
8526 denominators[num_denominators++] = 1000000000;
8531 else if (NUMERIC_CLOCKID) {
8532#if defined(HAVE_CLOCK_GETRES)
8534 c = NUM2CLOCKID(clk_id);
8536 ret = clock_getres(c, &ts);
8538 clock_failed(
"getres",
errno, clk_id);
8539 tt.count = (int32_t)ts.tv_nsec;
8540 tt.giga_count = ts.tv_sec;
8541 denominators[num_denominators++] = 1000000000;
8548 clock_failed(
"getres", EINVAL, clk_id);
8551 if (unit ==
ID2SYM(id_hertz)) {
8552 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8555 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8560get_CHILD_STATUS(
ID _x,
VALUE *_y)
8566get_PROCESS_ID(
ID _x,
VALUE *_y)
8658static VALUE rb_mProcUID;
8659static VALUE rb_mProcGID;
8660static VALUE rb_mProcID_Syscall;
8696 rb_eager_load_detailed_message_extension();
8699 rb_gc_prepare_heap();
9127 rb_gvar_ractor_local(
"$$");
9128 rb_gvar_ractor_local(
"$?");
9129 rb_gvar_box_ready(
"$$");
9130 rb_gvar_box_ready(
"$?");
9185 process_status_dump, process_status_load);
9221#ifdef HAVE_GETPRIORITY
9232#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9234 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9235#ifdef RLIM_SAVED_MAX
9237 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9239 rb_define_const(
rb_mProcess,
"RLIM_SAVED_MAX", v);
9243 rb_define_const(
rb_mProcess,
"RLIM_INFINITY", inf);
9244#ifdef RLIM_SAVED_CUR
9246 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9248 rb_define_const(
rb_mProcess,
"RLIM_SAVED_CUR", v);
9287#ifdef RLIMIT_MEMLOCK
9294#ifdef RLIMIT_MSGQUEUE
9360#ifdef RLIMIT_SIGPENDING
9395#if defined(RUBY_CLOCK_REALTIME)
9396#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9397# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9398#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9399# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9401#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9403 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
9404#elif defined(RUBY_CLOCK_REALTIME)
9405 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
9408#if defined(RUBY_CLOCK_MONOTONIC)
9409#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9410# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9412#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9414 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
9415#elif defined(RUBY_CLOCK_MONOTONIC)
9416 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", RUBY_CLOCK_MONOTONIC);
9419#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9420#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9421# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9423#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9425 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
9426#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9427 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", RUBY_CLOCK_PROCESS_CPUTIME_ID);
9430#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9432 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
9433#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9434 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", RUBY_CLOCK_THREAD_CPUTIME_ID);
9440 rb_define_const(
rb_mProcess,
"CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
9444 rb_define_const(
rb_mProcess,
"CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
9446#ifdef CLOCK_REALTIME_FAST
9448 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
9450#ifdef CLOCK_REALTIME_PRECISE
9452 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
9454#ifdef CLOCK_REALTIME_COARSE
9456 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_COARSE", CLOCKID2NUM(CLOCK_REALTIME_COARSE));
9458#ifdef CLOCK_REALTIME_ALARM
9460 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_ALARM", CLOCKID2NUM(CLOCK_REALTIME_ALARM));
9462#ifdef CLOCK_MONOTONIC_FAST
9464 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
9466#ifdef CLOCK_MONOTONIC_PRECISE
9468 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
9470#ifdef CLOCK_MONOTONIC_RAW
9472 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
9474#ifdef CLOCK_MONOTONIC_RAW_APPROX
9476 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW_APPROX", CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX));
9478#ifdef CLOCK_MONOTONIC_COARSE
9480 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_COARSE", CLOCKID2NUM(CLOCK_MONOTONIC_COARSE));
9482#ifdef CLOCK_BOOTTIME
9484 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME", CLOCKID2NUM(CLOCK_BOOTTIME));
9486#ifdef CLOCK_BOOTTIME_ALARM
9488 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME_ALARM", CLOCKID2NUM(CLOCK_BOOTTIME_ALARM));
9492 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
9494#ifdef CLOCK_UPTIME_FAST
9496 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
9498#ifdef CLOCK_UPTIME_PRECISE
9500 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
9502#ifdef CLOCK_UPTIME_RAW
9504 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW", CLOCKID2NUM(CLOCK_UPTIME_RAW));
9506#ifdef CLOCK_UPTIME_RAW_APPROX
9508 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW_APPROX", CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX));
9512 rb_define_const(
rb_mProcess,
"CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
9516 rb_define_const(
rb_mProcess,
"CLOCK_TAI", CLOCKID2NUM(CLOCK_TAI));
9522#if defined(HAVE_TIMES)
9536 SAVED_USER_ID = geteuid();
9537 SAVED_GROUP_ID = getegid();
9539 rb_mProcUID = rb_define_module_under(
rb_mProcess,
"UID");
9540 rb_mProcGID = rb_define_module_under(
rb_mProcess,
"GID");
9560#ifdef p_uid_from_name
9563#ifdef p_gid_from_name
9567 rb_mProcID_Syscall = rb_define_module_under(
rb_mProcess,
"Sys");
9594#define define_id(name) id_##name = rb_intern_const(#name)
9607 define_id(new_pgroup);
9609 define_id(unsetenv_others);
9612 define_id(close_others);
9613 define_id(nanosecond);
9614 define_id(microsecond);
9615 define_id(millisecond);
9617 define_id(float_microsecond);
9618 define_id(float_millisecond);
9619 define_id(float_second);
9620 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9621 define_id(TIME_BASED_CLOCK_REALTIME);
9622#ifdef CLOCK_REALTIME
9623 define_id(CLOCK_REALTIME);
9625#ifdef CLOCK_MONOTONIC
9626 define_id(CLOCK_MONOTONIC);
9628#ifdef CLOCK_PROCESS_CPUTIME_ID
9629 define_id(CLOCK_PROCESS_CPUTIME_ID);
9631#ifdef CLOCK_THREAD_CPUTIME_ID
9632 define_id(CLOCK_THREAD_CPUTIME_ID);
9635 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9636 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9639 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9641 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9643 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
9646#ifdef HAVE_WORKING_FORK
#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.
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 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.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
VALUE rb_eSystemExit
SystemExit exception.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eRuntimeError
RuntimeError exception.
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
void rb_exit(int status)
Terminates the current execution context.
VALUE rb_cObject
Object class.
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_obj_class(VALUE obj)
Queries the class of an object.
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)
Same as RB_OBJ_FREEZE(), but returns the given object.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
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.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_f_abort(int argc, const VALUE *argv)
This is similar to rb_f_exit().
VALUE rb_f_exit(int argc, const VALUE *argv)
Identical to rb_exit(), except how arguments are passed.
int rb_cloexec_dup2(int oldfd, int newfd)
Identical to rb_cloexec_dup(), except you can specify the destination file descriptor.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
Closes everything.
int rb_reserved_fd_p(int fd)
Queries if the given FD is reserved or not.
int rb_pipe(int *pipes)
This is an rb_cloexec_pipe() + rb_update_max_fd() combo.
int rb_cloexec_fcntl_dupfd(int fd, int minfd)
Duplicates a file descriptor with closing on exec.
int rb_cloexec_dup(int oldfd)
Identical to rb_cloexec_fcntl_dupfd(), except it implies minfd is 3.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
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.
VALUE rb_process_status_for(rb_pid_t pid, int status, int error)
Returns the Ruby value representing a waitpid(2) outcome.
rb_pid_t rb_spawn_err(int argc, const VALUE *argv, char *errbuf, size_t buflen)
Identical to rb_spawn(), except you can additionally know the detailed situation in case of abnormal ...
void rb_syswait(rb_pid_t pid)
This is a shorthand of rb_waitpid without status and flags.
VALUE rb_f_exec(int argc, const VALUE *argv)
Replaces the current process by running the given external command.
rb_pid_t rb_spawn(int argc, const VALUE *argv)
Identical to rb_f_exec(), except it spawns a child process instead of replacing the current one.
VALUE rb_process_status_wait(rb_pid_t pid, int flags)
Wait for the specified process to terminate, reap it, and return its status.
void rb_last_status_set(int status, rb_pid_t pid)
Sets the "last status", or the $?.
VALUE rb_detach_process(rb_pid_t pid)
"Detaches" a subprocess.
const char * ruby_signal_name(int signo)
Queries the name of the signal.
VALUE rb_f_kill(int argc, const VALUE *argv)
Sends a signal ("kills") to processes.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
Just another name of rb_str_cat.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_struct_define_under(VALUE space, const char *name,...)
Identical to rb_struct_define(), except it defines the class under the specified namespace instead of...
VALUE rb_struct_new(VALUE klass,...)
Creates an instance of the given struct.
VALUE rb_thread_local_aref(VALUE thread, ID key)
This badly named function reads from a Fiber local storage.
#define RUBY_UBF_IO
A special UBF for blocking IO operations.
void rb_thread_sleep_forever(void)
Blocks indefinitely.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
void rb_thread_check_ints(void)
Checks for interrupts.
void rb_thread_atfork(void)
A pthread_atfork(3posix)-like API.
VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val)
This badly named function writes to a Fiber local storage.
#define RUBY_UBF_PROCESS
A special UBF for blocking process operations.
void rb_thread_sleep(int sec)
Blocks for the given period of time.
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
int rb_io_modestr_oflags(const char *modestr)
Identical to rb_io_modestr_fmode(), except it returns a mixture of O_ flags.
#define GetOpenFile
This is an old name of RB_IO_POINTER.
VALUE rb_io_check_io(VALUE io)
Try converting an object to its IO representation using its to_io method, if any.
int len
Length of the buffer.
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield(VALUE val)
Yields the block.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
VALUE rb_thread_create(type *q, void *w)
Creates a rb_cThread instance.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define PIDT2NUM
Converts a C's pid_t into an instance of rb_cInteger.
#define NUM2PIDT
Converts an instance of rb_cNumeric into C's pid_t.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of 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 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 errno
Ractor-aware version of errno.
#define InitVM(ext)
This macro is for internal use.
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.
#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.