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);
1592release_fork_lock_after_fork(rb_pid_t pid)
1595#
if defined(__FreeBSD__)
1599 rb_thread_release_fork_lock();
1602 rb_thread_reset_fork_lock();
1607#if defined(HAVE_WORKING_FORK)
1609COMPILER_WARNING_PUSH
1610#if __has_warning("-Wdeprecated-declarations") || RBIMPL_COMPILER_IS(GCC)
1611COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
1613static inline rb_pid_t
1621#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1623exec_with_sh(
const char *prog,
char **argv,
char **envp)
1625 *argv = (
char *)prog;
1626 *--argv = (
char *)
"sh";
1628 execve(
"/bin/sh", argv, envp);
1630 execv(
"/bin/sh", argv);
1634#define try_with_sh(err, prog, argv, envp) (void)0
1639proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1647 argv = ARGVSTR2ARGV(argv_str);
1654 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1657 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1659 execve(prog, argv, envp);
1663 try_with_sh(err, prog, argv, envp);
1670proc_exec_sh(
const char *str,
VALUE envp_str)
1675 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1683 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1686 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1688 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1698 ret = proc_exec_sh(str,
Qfalse);
1705mark_exec_arg(
void *ptr)
1708 if (eargp->use_shell)
1709 rb_gc_mark(eargp->invoke.sh.shell_script);
1711 rb_gc_mark(eargp->invoke.cmd.command_name);
1712 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1713 rb_gc_mark(eargp->invoke.cmd.argv_str);
1714 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1716 rb_gc_mark(eargp->redirect_fds);
1717 rb_gc_mark(eargp->envp_str);
1718 rb_gc_mark(eargp->envp_buf);
1719 rb_gc_mark(eargp->dup2_tmpbuf);
1720 rb_gc_mark(eargp->rlimit_limits);
1721 rb_gc_mark(eargp->fd_dup2);
1722 rb_gc_mark(eargp->fd_close);
1723 rb_gc_mark(eargp->fd_open);
1724 rb_gc_mark(eargp->fd_dup2_child);
1725 rb_gc_mark(eargp->env_modification);
1726 rb_gc_mark(eargp->path_env);
1727 rb_gc_mark(eargp->chdir_dir);
1731memsize_exec_arg(
const void *ptr)
1739 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
1743# define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1745#ifdef DEFAULT_PROCESS_ENCODING
1746# define EXPORT_STR(str) rb_str_export_to_enc((str), DEFAULT_PROCESS_ENCODING)
1747# define EXPORT_DUP(str) export_dup(str)
1749export_dup(
VALUE str)
1751 VALUE newstr = EXPORT_STR(str);
1756# define EXPORT_STR(str) (str)
1757# define EXPORT_DUP(str) rb_str_dup(str)
1760#if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1761# define USE_SPAWNV 1
1763# define USE_SPAWNV 0
1766# define P_NOWAIT _P_NOWAIT
1777 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1778 flags = CREATE_NEW_PROCESS_GROUP;
1780 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ? RSTRING_PTR(prog) : 0, argv, flags);
1785#define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
1791 RBASIC_CLEAR_CLASS(obj);
1796check_exec_redirect_fd(
VALUE v,
int iskey)
1807 else if (
id == id_out)
1809 else if (
id == id_err)
1818 rb_raise(rb_eArgError,
"duplex IO redirection");
1825 rb_raise(rb_eArgError,
"negative file descriptor");
1828 else if (fd >= 3 && iskey) {
1829 rb_raise(rb_eArgError,
"wrong file descriptor (%d)", fd);
1835 rb_raise(rb_eArgError,
"wrong exec redirect");
1846 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
1853 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
1864 VALUE path, flags, perm;
1868 switch (
TYPE(val)) {
1871 if (
id == id_close) {
1873 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
1875 else if (
id == id_in) {
1877 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1879 else if (
id == id_out) {
1881 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1883 else if (
id == id_err) {
1885 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1888 rb_raise(rb_eArgError,
"wrong exec redirect symbol: %"PRIsVALUE,
1895 val = check_exec_redirect_fd(val, 0);
1899 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1905 path ==
ID2SYM(id_child)) {
1906 param = check_exec_redirect_fd(
rb_ary_entry(val, 1), 0);
1907 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
1920 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1921 flags, perm,
Qnil));
1922 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1930 key = check_exec_redirect_fd(key, 1);
1932 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1937 VALUE fd = check_exec_redirect_fd(v, 1);
1941 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1948 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1949 flags, perm,
Qnil));
1950 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1956 if (!
NIL_P(val))
goto io;
1957 rb_raise(rb_eArgError,
"wrong exec redirect action");
1962#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
1963static int rlimit_type_by_sym(
VALUE key);
1966rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
1968 VALUE ary = eargp->rlimit_limits;
1969 VALUE tmp, softlim, hardlim;
1970 if (eargp->rlimit_limits ==
Qfalse)
1971 ary = eargp->rlimit_limits = hide_obj(
rb_ary_new());
1973 ary = eargp->rlimit_limits;
1983 rb_raise(rb_eArgError,
"wrong exec rlimit option");
1994#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
1998 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2002 switch (
TYPE(key)) {
2004#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2006 int rtype = rlimit_type_by_sym(key);
2008 rb_execarg_addopt_rlimit(eargp, rtype, val);
2016 if (
id == id_pgroup) {
2018 if (eargp->pgroup_given) {
2019 rb_raise(rb_eArgError,
"pgroup option specified twice");
2023 else if (val ==
Qtrue)
2028 rb_raise(rb_eArgError,
"negative process group ID : %ld", (
long)pgroup);
2031 eargp->pgroup_given = 1;
2032 eargp->pgroup_pgid = pgroup;
2037 if (
id == id_new_pgroup) {
2038 if (eargp->new_pgroup_given) {
2039 rb_raise(rb_eArgError,
"new_pgroup option specified twice");
2041 eargp->new_pgroup_given = 1;
2042 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2046 if (
id == id_unsetenv_others) {
2047 if (eargp->unsetenv_others_given) {
2048 rb_raise(rb_eArgError,
"unsetenv_others option specified twice");
2050 eargp->unsetenv_others_given = 1;
2051 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2053 else if (
id == id_chdir) {
2054 if (eargp->chdir_given) {
2055 rb_raise(rb_eArgError,
"chdir option specified twice");
2058 val = rb_str_encode_ospath(val);
2059 eargp->chdir_given = 1;
2060 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2062 else if (
id == id_umask) {
2064 if (eargp->umask_given) {
2065 rb_raise(rb_eArgError,
"umask option specified twice");
2067 eargp->umask_given = 1;
2068 eargp->umask_mask = cmask;
2070 else if (
id == id_close_others) {
2071 if (eargp->close_others_given) {
2072 rb_raise(rb_eArgError,
"close_others option specified twice");
2074 eargp->close_others_given = 1;
2075 eargp->close_others_do = TO_BOOL(val,
"close_others");
2077 else if (
id == id_in) {
2081 else if (
id == id_out) {
2085 else if (
id == id_err) {
2089 else if (
id == id_uid) {
2091 if (eargp->uid_given) {
2092 rb_raise(rb_eArgError,
"uid option specified twice");
2096 eargp->uid = OBJ2UID(val);
2097 eargp->uid_given = 1;
2101 "uid option is unimplemented on this machine");
2104 else if (
id == id_gid) {
2106 if (eargp->gid_given) {
2107 rb_raise(rb_eArgError,
"gid option specified twice");
2111 eargp->gid = OBJ2GID(val);
2112 eargp->gid_given = 1;
2116 "gid option is unimplemented on this machine");
2119 else if (
id == id_exception) {
2120 if (eargp->exception_given) {
2121 rb_raise(rb_eArgError,
"exception option specified twice");
2123 eargp->exception_given = 1;
2124 eargp->exception = TO_BOOL(val,
"exception");
2135 check_exec_redirect(key, val, eargp);
2147check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2152 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2154 rb_raise(rb_eArgError,
"wrong exec option symbol: % "PRIsVALUE,
2156 rb_raise(rb_eArgError,
"wrong exec option: %"PRIsVALUE,
rb_obj_class(key));
2162check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2167 VALUE execarg_obj = args[0];
2168 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2169 VALUE nonopts = args[1];
2170 if (
NIL_P(nonopts)) args[1] = nonopts = rb_hash_new();
2171 rb_hash_aset(nonopts, key, val);
2186 rb_raise(rb_eArgError,
"fd %d specified twice", fd);
2188 if (ary == eargp->fd_dup2)
2190 else if (ary == eargp->fd_dup2_child)
2196 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2209 VALUE h = rb_hash_new();
2214 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2215 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2216 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2218 if (eargp->fd_dup2_child) {
2219 ary = eargp->fd_dup2_child;
2229 val = rb_hash_lookup(h, val);
2231 rb_raise(rb_eArgError,
"cyclic child fd redirection from %d", oldfd);
2235 rb_raise(rb_eArgError,
"child fd %d is not redirected", oldfd);
2236 if (oldfd != lastfd) {
2241 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2242 rb_hash_aset(h, val,
INT2FIX(lastfd));
2249 eargp->close_others_maxhint = maxhint;
2254rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2258 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2262rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2267 args[0] = execarg_obj;
2269 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2273#ifdef ENV_IGNORECASE
2274#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2276#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2280check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2290 rb_raise(rb_eArgError,
"environment name contains a equal : %"PRIsVALUE, key);
2295 key = EXPORT_STR(key);
2296 if (!
NIL_P(val)) val = EXPORT_STR(val);
2313 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2320rb_check_argv(
int argc,
VALUE *argv)
2331 rb_raise(rb_eArgError,
"wrong first argument");
2339 for (i = 0; i < argc; i++) {
2348check_hash(
VALUE obj)
2358 return rb_check_hash_type(obj);
2362rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2367 hash = check_hash((*argv_p)[*argc_p-1]);
2369 *opthash_ret = hash;
2375 hash = check_hash((*argv_p)[0]);
2382 prog = rb_check_argv(*argc_p, *argv_p);
2384 prog = (*argv_p)[0];
2385 if (accept_shell && *argc_p == 1) {
2400compare_posix_sh(
const void *key,
const void *el)
2403 int ret = strncmp(word->ptr, el, word->len);
2404 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2409#define append_terminator(buf) rb_str_buf_cat(buf, "", 1)
2414 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2415 char fbuf[MAXPATHLEN];
2419 if (!
NIL_P(opthash)) {
2420 rb_check_exec_options(opthash, execarg_obj);
2423 env = rb_check_exec_env(env, &eargp->path_env);
2424 eargp->env_modification = env;
2427 prog = EXPORT_STR(prog);
2428 eargp->use_shell = argc == 0;
2429 if (eargp->use_shell)
2430 eargp->invoke.sh.shell_script = prog;
2432 eargp->invoke.cmd.command_name = prog;
2435 if (eargp->use_shell) {
2436 static const char posix_sh_cmds[][9] = {
2468 const char *
const s = rb_str_null_check(prog);
2497 for (p = s; p < e; p++) {
2498 if (*p ==
' ' || *p ==
'\t') {
2499 if (first.ptr && !first.len) first.len = p - first.ptr;
2502 if (!first.ptr) first.ptr = p;
2504 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2510 else if (*p ==
'/') {
2518 if (!first.ptr) first.ptr = e;
2519 if (!first.len) first.len = p - first.ptr;
2520 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2522 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2527 eargp->use_shell = 0;
2529 if (!eargp->use_shell) {
2533 append_terminator(argv_buf);
2534 for (p = first.ptr + first.len; p < e;) {
2535 while (p < e && (*p ==
' ' || *p ==
'\t'))
2539 while (p < e && *p !=
' ' && *p !=
'\t')
2542 append_terminator(argv_buf);
2545 eargp->invoke.cmd.argv_buf = argv_buf;
2546 eargp->invoke.cmd.command_name =
2548 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2553 if (!eargp->use_shell) {
2554 const char *abspath;
2555 const char *path_env = 0;
2556 if (
RTEST(eargp->path_env)) path_env = RSTRING_PTR(eargp->path_env);
2557 abspath = dln_find_exe_r(RSTRING_PTR(eargp->invoke.cmd.command_name),
2558 path_env, fbuf,
sizeof(fbuf));
2562 eargp->invoke.cmd.command_abspath =
Qnil;
2565 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2570 for (i = 0; i < argc; i++) {
2571 VALUE arg = argv[i];
2573#ifdef DEFAULT_PROCESS_ENCODING
2574 arg = EXPORT_STR(arg);
2575 s = RSTRING_PTR(arg);
2578 append_terminator(argv_buf);
2580 eargp->invoke.cmd.argv_buf = argv_buf;
2583 if (!eargp->use_shell) {
2584 const char *p, *ep, *null=NULL;
2588 p = RSTRING_PTR(eargp->invoke.cmd.argv_buf);
2589 ep = p + RSTRING_LEN(eargp->invoke.cmd.argv_buf);
2595 eargp->invoke.cmd.argv_str =
2596 rb_imemo_tmpbuf_new_from_an_RString(argv_str);
2602rb_execarg_get(
VALUE execarg_obj)
2610rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2612 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2618 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2619 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2621 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2627rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2632 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2633 if (!allow_exc_opt && eargp->exception_given) {
2634 rb_raise(rb_eArgError,
"exception option is not allowed");
2640rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2642 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2643 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2644 eargp->env_modification = env;
2649fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2658 append_terminator(envp_buf);
2678 const char *fname = RSTRING_PTR(data->fname);
2679 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2687 rb_alloc_tmp_buffer(&eargp->dup2_tmpbuf, run_exec_dup2_tmpbuf_size(
len),
false);
2691rb_execarg_parent_start1(
VALUE execarg_obj)
2693 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2694 int unsetenv_others;
2698 ary = eargp->fd_open;
2713 open_data.fname = vpath;
2714 open_data.oflags = flags;
2715 open_data.perm = perm;
2717 open_data.err = EINTR;
2719 if (open_data.ret == -1) {
2720 if (open_data.err == EINTR) {
2726 fd2 = open_data.ret;
2738 eargp->redirect_fds = check_exec_fds(eargp);
2740 ary = eargp->fd_dup2;
2742 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2745 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2746 envopts = eargp->env_modification;
2747 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2748 VALUE envtbl, envp_str, envp_buf;
2750 if (unsetenv_others) {
2751 envtbl = rb_hash_new();
2754 envtbl = rb_env_to_hash();
2764 rb_hash_delete(envtbl, key);
2767 rb_hash_aset(envtbl, key, val);
2773 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
2776 p = RSTRING_PTR(envp_buf);
2777 ep = p + RSTRING_LEN(envp_buf);
2785 rb_imemo_tmpbuf_new_from_an_RString(envp_str);
2786 eargp->envp_buf = envp_buf;
2802rb_execarg_parent_start(
VALUE execarg_obj)
2805 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2807 rb_execarg_parent_end(execarg_obj);
2813execarg_parent_end(
VALUE execarg_obj)
2815 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2819 ary = eargp->fd_open;
2830 parent_redirect_close(fd2);
2842rb_execarg_parent_end(
VALUE execarg_obj)
2844 execarg_parent_end(execarg_obj);
2848rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
2850 if (!errmsg || !*errmsg)
return;
2851 if (strcmp(errmsg,
"chdir") == 0) {
2852 rb_sys_fail_str(eargp->chdir_dir);
2854 rb_sys_fail(errmsg);
2859rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
2861 if (!errmsg || !*errmsg)
return;
2862 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
2870 VALUE execarg_obj, fail_str;
2872#define CHILD_ERRMSG_BUFLEN 80
2873 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
2876 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
2877 eargp = rb_execarg_get(execarg_obj);
2880 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2882 execarg_parent_end(execarg_obj);
2887 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2889 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
2892 rb_exec_fail(eargp, err, errmsg);
2996#define ERRMSG(str) \
2997 ((errmsg && 0 < errmsg_buflen) ? \
2998 (void)strlcpy(errmsg, (str), errmsg_buflen) : (void)0)
3000#define ERRMSG_FMT(...) \
3001 ((errmsg && 0 < errmsg_buflen) ? \
3002 (void)snprintf(errmsg, errmsg_buflen, __VA_ARGS__) : (void)0)
3004static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3005static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3006static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3009save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3012 VALUE newary, redirection;
3013 int save_fd = redirect_cloexec_dup(fd), cloexec;
3014 if (save_fd == -1) {
3021 newary = sargp->fd_dup2;
3024 sargp->fd_dup2 = newary;
3026 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3031 newary = sargp->fd_close;
3034 sargp->fd_close = newary;
3043intcmp(
const void *a,
const void *b)
3045 return *(
int*)a - *(
int*)b;
3049intrcmp(
const void *a,
const void *b)
3051 return *(
int*)b - *(
int*)a;
3063run_exec_dup2_tmpbuf_size(
rb_len_t n)
3070fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3074 ret = fcntl(fd, F_GETFD);
3076 ERRMSG(
"fcntl(F_GETFD)");
3079 if (ret & FD_CLOEXEC)
return 1;
3086fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3090 ret = fcntl(fd, F_GETFD);
3092 ERRMSG(
"fcntl(F_GETFD)");
3095 if (!(ret & FD_CLOEXEC)) {
3097 ret = fcntl(fd, F_SETFD, ret);
3099 ERRMSG(
"fcntl(F_SETFD)");
3109fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3113 ret = fcntl(fd, F_GETFD);
3115 ERRMSG(
"fcntl(F_GETFD)");
3118 if (ret & FD_CLOEXEC) {
3120 ret = fcntl(fd, F_SETFD, ret);
3122 ERRMSG(
"fcntl(F_SETFD)");
3132run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3142 for (i = 0; i < n; i++) {
3147 pairs[i].older_index = -1;
3157 for (i = 0; i < n; i++) {
3158 int newfd = pairs[i].newfd;
3162 pairs[i].num_newer = 0;
3164 while (pairs < found && (found-1)->oldfd == newfd)
3166 while (found < pairs+n && found->oldfd == newfd) {
3167 pairs[i].num_newer++;
3168 found->older_index = i;
3175 for (i = 0; i < n; i++) {
3177 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3178 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3180 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3185 if (pairs[j].cloexec &&
3186 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3190 pairs[j].oldfd = -1;
3191 j = pairs[j].older_index;
3193 pairs[j].num_newer--;
3198 for (i = 0; i < n; i++) {
3200 if (pairs[i].oldfd == -1)
3202 if (pairs[i].oldfd == pairs[i].newfd) {
3203 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3205 pairs[i].oldfd = -1;
3208 if (extra_fd == -1) {
3209 extra_fd = redirect_dup(pairs[i].oldfd);
3210 if (extra_fd == -1) {
3216 if (fd_get_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen)) {
3217 if (fd_set_cloexec(extra_fd, errmsg, errmsg_buflen)) {
3225 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3232 pairs[i].oldfd = extra_fd;
3233 j = pairs[i].older_index;
3234 pairs[i].older_index = -1;
3236 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3242 pairs[j].oldfd = -1;
3243 j = pairs[j].older_index;
3246 if (extra_fd != -1) {
3247 ret = redirect_close(extra_fd);
3262run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3270 ret = redirect_close(fd);
3281run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3291 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3293 ret = redirect_dup2(oldfd, newfd);
3306run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3318 pgroup = eargp->pgroup_pgid;
3324 sargp->pgroup_given = 1;
3325 sargp->pgroup_pgid = getpgrp();
3331 ret = setpgid(getpid(), pgroup);
3332 if (ret == -1) ERRMSG(
"setpgid");
3337#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3340run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3349 if (getrlimit(rtype, &rlim) == -1) {
3350 ERRMSG(
"getrlimit");
3354 RLIM2NUM(rlim.rlim_cur),
3355 RLIM2NUM(rlim.rlim_max)));
3356 if (sargp->rlimit_limits ==
Qfalse)
3357 newary = sargp->rlimit_limits = hide_obj(
rb_ary_new());
3359 newary = sargp->rlimit_limits;
3364 if (setrlimit(rtype, &rlim) == -1) {
3365 ERRMSG(
"setrlimit");
3373#if !defined(HAVE_WORKING_FORK)
3386 if (sargp->env_modification ==
Qfalse) {
3387 VALUE env = rb_envtbl();
3392 sargp->env_modification = ary;
3394 sargp->unsetenv_others_given = 1;
3395 sargp->unsetenv_others_do = 1;
3402#define chdir(p) rb_w32_uchdir(p)
3407rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3414 sargp->redirect_fds =
Qnil;
3418 if (eargp->pgroup_given) {
3419 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3424#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3425 obj = eargp->rlimit_limits;
3427 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3432#if !defined(HAVE_WORKING_FORK)
3433 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3438 obj = eargp->env_modification;
3454 if (eargp->umask_given) {
3455 mode_t mask = eargp->umask_mask;
3456 mode_t oldmask = umask(mask);
3458 sargp->umask_given = 1;
3459 sargp->umask_mask = oldmask;
3463 obj = eargp->fd_dup2;
3465 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3469 obj = eargp->fd_close;
3472 rb_warn(
"cannot close fd before spawn");
3474 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3479#ifdef HAVE_WORKING_FORK
3480 if (eargp->close_others_do) {
3485 obj = eargp->fd_dup2_child;
3487 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3491 if (eargp->chdir_given) {
3493 sargp->chdir_given = 1;
3494 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3496 if (chdir(RSTRING_PTR(eargp->chdir_dir)) == -1) {
3503 if (eargp->gid_given) {
3504 if (setgid(eargp->gid) < 0) {
3511 if (eargp->uid_given) {
3512 if (setuid(eargp->uid) < 0) {
3520 VALUE ary = sargp->fd_dup2;
3522 rb_execarg_allocate_dup2_tmpbuf(sargp,
RARRAY_LEN(ary));
3526 int preserve =
errno;
3527 stdfd_clear_nonblock();
3536rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3538 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3543exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3545#if !defined(HAVE_WORKING_FORK)
3546 struct rb_execarg sarg, *
const sargp = &sarg;
3552 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3556 if (eargp->use_shell) {
3557 err = proc_exec_sh(RSTRING_PTR(eargp->invoke.sh.shell_script), eargp->envp_str);
3560 char *abspath = NULL;
3561 if (!
NIL_P(eargp->invoke.cmd.command_abspath))
3562 abspath = RSTRING_PTR(eargp->invoke.cmd.command_abspath);
3563 err = proc_exec_cmd(abspath, eargp->invoke.cmd.argv_str, eargp->envp_str);
3565#if !defined(HAVE_WORKING_FORK)
3566 rb_execarg_run_options(sargp, NULL, errmsg, errmsg_buflen);
3572#ifdef HAVE_WORKING_FORK
3575rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3577 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3581proc_syswait(
VALUE pid)
3588move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3592 for (i = 0; i < n; i++) {
3611pipe_nocrash(
int filedes[2],
VALUE fds)
3619 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3634rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3641handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3647 if ((*try_gc_p)-- > 0 && !rb_during_gc()) {
3653#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3656 if (!status && !ep) {
3661 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3662 if (status) status->status = state;
3663 if (!state)
return 0;
3672 if (state && !status) rb_jump_tag(state);
3676#define prefork() ( \
3677 rb_io_flush(rb_stdout), \
3678 rb_io_flush(rb_stderr) \
3708write_retry(
int fd,
const void *buf,
size_t len)
3713 w = write(fd, buf,
len);
3714 }
while (w < 0 &&
errno == EINTR);
3720read_retry(
int fd,
void *buf,
size_t len)
3724 if (set_blocking(fd) != 0) {
3726 rb_async_bug_errno(
"set_blocking failed reading child error",
errno);
3731 r = read(fd, buf,
len);
3732 }
while (r < 0 &&
errno == EINTR);
3738send_child_error(
int fd,
char *errmsg,
size_t errmsg_buflen)
3743 if (write_retry(fd, &err,
sizeof(err)) < 0) err =
errno;
3744 if (errmsg && 0 < errmsg_buflen) {
3745 errmsg[errmsg_buflen-1] =
'\0';
3746 errmsg_buflen = strlen(errmsg);
3747 if (errmsg_buflen > 0 && write_retry(fd, errmsg, errmsg_buflen) < 0)
3753recv_child_error(
int fd,
int *errp,
char *errmsg,
size_t errmsg_buflen)
3757 if ((size = read_retry(fd, &err,
sizeof(err))) < 0) {
3761 if (size ==
sizeof(err) &&
3762 errmsg && 0 < errmsg_buflen) {
3763 ssize_t ret = read_retry(fd, errmsg, errmsg_buflen-1);
3772#ifdef HAVE_WORKING_VFORK
3773#if !defined(HAVE_GETRESUID) && defined(HAVE_GETUIDX)
3776getresuid(rb_uid_t *ruid, rb_uid_t *euid, rb_uid_t *suid)
3782 ret = getuidx(ID_SAVED);
3783 if (ret == (rb_uid_t)-1)
3788#define HAVE_GETRESUID
3791#if !defined(HAVE_GETRESGID) && defined(HAVE_GETGIDX)
3794getresgid(rb_gid_t *rgid, rb_gid_t *egid, rb_gid_t *sgid)
3800 ret = getgidx(ID_SAVED);
3801 if (ret == (rb_gid_t)-1)
3806#define HAVE_GETRESGID
3809#if !defined(RUBY_ASAN_ENABLED)
3825 rb_uid_t ruid, euid;
3826 rb_gid_t rgid, egid;
3828#if defined HAVE_ISSETUGID
3833#ifdef HAVE_GETRESUID
3837 ret = getresuid(&ruid, &euid, &suid);
3839 rb_sys_fail(
"getresuid(2)");
3848 if (euid == 0 || euid != ruid)
3851#ifdef HAVE_GETRESGID
3855 ret = getresgid(&rgid, &egid, &sgid);
3857 rb_sys_fail(
"getresgid(2)");
3874struct child_handler_disabler_state
3880disable_child_handler_before_fork(
struct child_handler_disabler_state *old)
3882#ifdef HAVE_PTHREAD_SIGMASK
3886 ret = sigfillset(&all);
3888 rb_sys_fail(
"sigfillset");
3890 ret = pthread_sigmask(SIG_SETMASK, &all, &old->sigmask);
3895# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3900disable_child_handler_fork_parent(
struct child_handler_disabler_state *old)
3902#ifdef HAVE_PTHREAD_SIGMASK
3905 ret = pthread_sigmask(SIG_SETMASK, &old->sigmask, NULL);
3910# pragma GCC warning "pthread_sigmask on fork is not available. potentially dangerous"
3916disable_child_handler_fork_child(
struct child_handler_disabler_state *old,
char *errmsg,
size_t errmsg_buflen)
3921 for (sig = 1; sig < NSIG; sig++) {
3922 sig_t handler = signal(sig, SIG_DFL);
3924 if (handler == SIG_ERR &&
errno == EINVAL) {
3927 if (handler == SIG_ERR) {
3928 ERRMSG(
"signal to obtain old action");
3932 if (sig == SIGPIPE) {
3937 if (handler == SIG_IGN) {
3938 signal(sig, SIG_IGN);
3943 sigemptyset(&old->sigmask);
3944 ret = sigprocmask(SIG_SETMASK, &old->sigmask, NULL);
3946 ERRMSG(
"sigprocmask");
3954 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
3955 char *errmsg,
size_t errmsg_buflen,
3959 volatile int try_gc = 1;
3960 struct child_handler_disabler_state old;
3965 disable_child_handler_before_fork(&old);
3969#if defined(HAVE_WORKING_VFORK) && !defined(RUBY_ASAN_ENABLED)
3970 if (!has_privilege())
3980 ret = disable_child_handler_fork_child(&old, errmsg, errmsg_buflen);
3982 ret = chfunc(charg, errmsg, errmsg_buflen);
3983 if (!ret) _exit(EXIT_SUCCESS);
3985 send_child_error(ep[1], errmsg, errmsg_buflen);
3986#if EXIT_SUCCESS == 127
3987 _exit(EXIT_FAILURE);
3993 disable_child_handler_fork_parent(&old);
3997 if (handle_fork_error(err, status, ep, &try_gc))
4003fork_check_err(
struct rb_process_status *status,
int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4004 VALUE fds,
char *errmsg,
size_t errmsg_buflen,
4012 struct waitpid_state *w = eargp && eargp->waitpid_state ? eargp->waitpid_state : 0;
4014 if (status) status->status = 0;
4016 if (pipe_nocrash(ep, fds))
return -1;
4018 pid = retry_fork_async_signal_safe(status, ep, chfunc, charg, errmsg, errmsg_buflen, w);
4020 if (status) status->pid = pid;
4023 if (status) status->error =
errno;
4030 error_occurred = recv_child_error(ep[0], &err, errmsg, errmsg_buflen);
4032 if (error_occurred) {
4035 status->error = err;
4037 VM_ASSERT((w == 0) &&
"only used by extensions");
4038 rb_protect(proc_syswait, (
VALUE)pid, &state);
4040 status->status = state;
4061rb_fork_async_signal_safe(
int *status,
4062 int (*chfunc)(
void*,
char *,
size_t),
void *charg,
4063 VALUE fds,
char *errmsg,
size_t errmsg_buflen)
4067 rb_pid_t result = fork_check_err(&process_status, chfunc, charg, fds, errmsg, errmsg_buflen, 0);
4070 *status = process_status.status;
4077rb_fork_ruby(
int *status)
4079 if (UNLIKELY(!rb_ractor_main_p())) {
4080 rb_raise(rb_eRactorIsolationError,
"can not fork from non-main Ractors");
4085 int try_gc = 1, err = 0;
4086 struct child_handler_disabler_state old;
4092 rb_thread_acquire_fork_lock();
4093 disable_child_handler_before_fork(&old);
4096 child.pid = pid = rb_fork();
4097 child.error = err =
errno;
4100 disable_child_handler_fork_parent(&old);
4101 release_fork_lock_after_fork(pid);
4102 after_fork_ruby(pid);
4105 }
while (pid < 0 && handle_fork_error(err, &child, NULL, &try_gc) == 0);
4107 if (status) *status = child.status;
4115 rb_pid_t pid = rb_fork_ruby(NULL);
4118 rb_sys_fail(
"fork(2)");
4125call_proc__fork_protected(
VALUE arg)
4134rb_call_proc__fork(
void)
4137 return proc_fork_pid();
4140 rb_pid_t parent = getpid(), pid;
4143 if (
NIL_P(rb_protect(call_proc__fork_protected, (
VALUE)&pid, &state))) {
4144 if (getpid() != parent) {
4154#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4175rb_proc__fork(
VALUE _obj)
4177 rb_pid_t pid = proc_fork_pid();
4246 pid = rb_call_proc__fork();
4260#define rb_proc__fork rb_f_notimplement
4261#define rb_f_fork rb_f_notimplement
4265exit_status_code(
VALUE status)
4271 istatus = EXIT_SUCCESS;
4274 istatus = EXIT_FAILURE;
4278#if EXIT_SUCCESS != 0
4280 istatus = EXIT_SUCCESS;
4287NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4305rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4310 istatus = exit_status_code(argv[0]);
4313 istatus = EXIT_FAILURE;
4323 if (GET_EC()->tag) {
4339 istatus = exit_status_code(argv[0]);
4342 istatus = EXIT_SUCCESS;
4408 VALUE errinfo = rb_ec_get_errinfo(ec);
4409 if (!
NIL_P(errinfo)) {
4410 rb_ec_error_print(ec, errinfo);
4417 args[1] = args[0] = argv[0];
4419 rb_io_puts(1, args, rb_ractor_stderr());
4420 args[0] =
INT2NUM(EXIT_FAILURE);
4458#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4463 if (eargp && !eargp->use_shell) {
4464 VALUE str = eargp->invoke.cmd.argv_str;
4465 VALUE buf = eargp->invoke.cmd.argv_buf;
4466 char *p, **argv = ARGVSTR2ARGV(str);
4467 long i, argc = ARGVSTR2ARGC(str);
4468 const char *start = RSTRING_PTR(buf);
4470 p = RSTRING_PTR(cmd);
4471 for (i = 1; i < argc; ++i) {
4472 p[argv[i] - start - 1] =
' ';
4482rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4485#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4488# if !defined HAVE_SPAWNV
4493#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4494 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4496 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4498 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4502 if (prog && !eargp->use_shell) {
4503 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4504 argv[0] = RSTRING_PTR(prog);
4506# if defined HAVE_SPAWNV
4507 if (eargp->use_shell) {
4508 pid = proc_spawn_sh(RSTRING_PTR(prog));
4511 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4512 pid = proc_spawn_cmd(argv, prog, eargp);
4519 status = system(rb_execarg_commandline(eargp, &prog));
4524 if (eargp->waitpid_state) {
4525 eargp->waitpid_state->pid = pid;
4528 rb_execarg_run_options(&sarg, NULL, errmsg, errmsg_buflen);
4543do_spawn_process(
VALUE arg)
4547 rb_execarg_parent_start1(argp->execarg);
4549 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4550 argp->errmsg.ptr, argp->errmsg.buflen);
4553NOINLINE(
static rb_pid_t
4554 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4557rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4561 args.execarg = execarg_obj;
4562 args.errmsg.ptr = errmsg;
4563 args.errmsg.buflen = errmsg_buflen;
4566 execarg_parent_end, execarg_obj);
4571rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4575 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4576 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4582 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4588 return rb_spawn_internal(argc, argv, NULL, 0);
4710 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4711 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4714 eargp->status = &status;
4716 last_status_clear(th);
4720 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4727 th->last_status = status;
4729 if (data->status == EXIT_SUCCESS) {
4733 if (data->error != 0) {
4734 if (eargp->exception) {
4735 VALUE command = eargp->invoke.sh.shell_script;
4743 else if (eargp->exception) {
4744 VALUE command = eargp->invoke.sh.shell_script;
4758 if (eargp->exception) {
4759 VALUE command = eargp->invoke.sh.shell_script;
4875 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
4876 VALUE execarg_obj, fail_str;
4879 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4880 eargp = rb_execarg_get(execarg_obj);
4881 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4883 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
4887 rb_exec_fail(eargp, err, errmsg);
4891#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
4917 time_t beg = time(0);
4920 if (scheduler !=
Qnil) {
4924 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
4933 time_t end = time(0) - beg;
4935 return TIMET2NUM(end);
4939#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
4956#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
4958 if (pgrp < 0) rb_sys_fail(0);
4962 if (pgrp < 0) rb_sys_fail(0);
4967#define proc_getpgrp rb_f_notimplement
4971#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
4989 if (setpgid(0,0) < 0) rb_sys_fail(0);
4990#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
4991 if (setpgrp() < 0) rb_sys_fail(0);
4996#define proc_setpgrp rb_f_notimplement
5000#if defined(HAVE_GETPGID)
5018 if (i < 0) rb_sys_fail(0);
5022#define proc_getpgid rb_f_notimplement
5040 rb_pid_t ipid, ipgrp;
5045 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5049#define proc_setpgid rb_f_notimplement
5077 if (sid < 0) rb_sys_fail(0);
5081#define proc_getsid rb_f_notimplement
5085#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5086#if !defined(HAVE_SETSID)
5087static rb_pid_t ruby_setsid(
void);
5088#define setsid() ruby_setsid()
5109 if (pid < 0) rb_sys_fail(0);
5113#if !defined(HAVE_SETSID)
5114#define HAVE_SETSID 1
5122#if defined(SETPGRP_VOID)
5128 ret = setpgrp(0, pid);
5130 if (ret == -1)
return -1;
5134 ioctl(fd, TIOCNOTTY, NULL);
5141#define proc_setsid rb_f_notimplement
5145#ifdef HAVE_GETPRIORITY
5173 int prio, iwhich, iwho;
5179 prio = getpriority(iwhich, iwho);
5180 if (
errno) rb_sys_fail(0);
5184#define proc_getpriority rb_f_notimplement
5188#ifdef HAVE_GETPRIORITY
5208 int iwhich, iwho, iprio;
5214 if (setpriority(iwhich, iwho, iprio) < 0)
5219#define proc_setpriority rb_f_notimplement
5222#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5224rlimit_resource_name2int(
const char *name,
rb_len_t len,
int casetype)
5228#define RESCHECK(r) \
5230 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5231 resource = RLIMIT_##r; \
5265#ifdef RLIMIT_MEMLOCK
5268#ifdef RLIMIT_MSGQUEUE
5307#ifdef RLIMIT_SIGPENDING
5308 RESCHECK(SIGPENDING);
5317 for (p = name; *p; p++)
5323 for (p = name; *p; p++)
5329 rb_bug(
"unexpected casetype");
5336rlimit_type_by_hname(
const char *name,
rb_len_t len)
5338 return rlimit_resource_name2int(name,
len, 0);
5342rlimit_type_by_lname(
const char *name,
rb_len_t len)
5344 return rlimit_resource_name2int(name,
len, 1);
5348rlimit_type_by_sym(
VALUE key)
5351 const char *rname = RSTRING_PTR(name);
5354 static const char prefix[] =
"rlimit_";
5355 enum {prefix_len =
sizeof(prefix)-1};
5357 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5358 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5366rlimit_resource_type(
VALUE rtype)
5373 switch (
TYPE(rtype)) {
5376 name = RSTRING_PTR(v);
5377 len = RSTRING_LEN(v);
5386 len = RSTRING_LEN(rtype);
5396 r = rlimit_type_by_hname(name,
len);
5400 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5406rlimit_resource_value(
VALUE rval)
5411 switch (
TYPE(rval)) {
5414 name = RSTRING_PTR(v);
5429 return NUM2RLIM(rval);
5433 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5435#ifdef RLIM_SAVED_MAX
5436 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5438#ifdef RLIM_SAVED_CUR
5439 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5441 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5447#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5475 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5476 rb_sys_fail(
"getrlimit");
5478 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5481#define proc_getrlimit rb_f_notimplement
5484#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5538proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5540 VALUE resource, rlim_cur, rlim_max;
5546 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5547 rlim_max = rlim_cur;
5549 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5550 rlim.rlim_max = rlimit_resource_value(rlim_max);
5552 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5553 rb_sys_fail(
"setrlimit");
5558#define proc_setrlimit rb_f_notimplement
5561static int under_uid_switch = 0;
5563check_uid_switch(
void)
5565 if (under_uid_switch) {
5566 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5570static int under_gid_switch = 0;
5572check_gid_switch(
void)
5574 if (under_gid_switch) {
5575 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5580#if defined(HAVE_PWD_H)
5582login_not_found(
int err)
5584 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5595# if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5598 char MAYBE_UNUSED(*login) = NULL;
5600# ifdef USE_GETLOGIN_R
5602# if defined(__FreeBSD__)
5603 typedef int getlogin_r_size_t;
5605 typedef size_t getlogin_r_size_t;
5608 rb_len_t loginsize = GETLOGIN_R_SIZE_INIT;
5611 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5615 login = RSTRING_PTR(maybe_result);
5620 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5621 if (login_not_found(gle)) {
5622 rb_str_resize(maybe_result, 0);
5626 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5627 rb_str_resize(maybe_result, 0);
5632 login = RSTRING_PTR(maybe_result);
5636 if (login == NULL) {
5637 rb_str_resize(maybe_result, 0);
5642 return maybe_result;
5644# elif defined(USE_GETLOGIN)
5650 if (login_not_found(err)) {
5664pwd_not_found(
int err)
5678# if defined(USE_GETPWNAM_R)
5679struct getpwnam_r_args {
5683 struct passwd *result;
5684 struct passwd pwstore;
5687# define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5688 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5691nogvl_getpwnam_r(
void *args)
5693 struct getpwnam_r_args *arg = args;
5694 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5699rb_getpwdirnam_for_login(
VALUE login_name)
5701#if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5705 if (
NIL_P(login_name)) {
5710 const char *login = RSTRING_PTR(login_name);
5713# ifdef USE_GETPWNAM_R
5716 rb_len_t bufsizenm = GETPW_R_SIZE_INIT;
5719 bufsizenm = GETPW_R_SIZE_DEFAULT;
5723 bufnm = RSTRING_PTR(getpwnm_tmp);
5726 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5729 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5730 if (pwd_not_found(enm)) {
5731 rb_str_resize(getpwnm_tmp, 0);
5735 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5736 rb_str_resize(getpwnm_tmp, 0);
5741 args.buf = RSTRING_PTR(getpwnm_tmp);
5745 if (args.result == NULL) {
5747 rb_str_resize(getpwnm_tmp, 0);
5753 rb_str_resize(getpwnm_tmp, 0);
5756# elif defined(USE_GETPWNAM)
5758 struct passwd *pwptr;
5760 if (!(pwptr = getpwnam(login))) {
5763 if (pwd_not_found(err)) {
5777# if defined(USE_GETPWUID_R)
5778struct getpwuid_r_args {
5782 struct passwd *result;
5783 struct passwd pwstore;
5786# define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5787 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5790nogvl_getpwuid_r(
void *args)
5792 struct getpwuid_r_args *arg = args;
5793 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5803# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5807 uid_t ruid = getuid();
5809# ifdef USE_GETPWUID_R
5812 rb_len_t bufsizeid = GETPW_R_SIZE_INIT;
5815 bufsizeid = GETPW_R_SIZE_DEFAULT;
5819 bufid = RSTRING_PTR(getpwid_tmp);
5822 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5825 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5826 if (pwd_not_found(eid)) {
5827 rb_str_resize(getpwid_tmp, 0);
5831 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5832 rb_str_resize(getpwid_tmp, 0);
5837 args.buf = RSTRING_PTR(getpwid_tmp);
5841 if (args.result == NULL) {
5843 rb_str_resize(getpwid_tmp, 0);
5849 rb_str_resize(getpwid_tmp, 0);
5852# elif defined(USE_GETPWUID)
5854 struct passwd *pwptr;
5856 if (!(pwptr = getpwuid(ruid))) {
5859 if (pwd_not_found(err)) {
5885#if defined(HAVE_PWD_H)
5888# ifdef USE_GETPWNAM_R
5901 struct passwd *pwptr;
5902#ifdef USE_GETPWNAM_R
5907 getpw_buf_len = GETPW_R_SIZE_INIT;
5908 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5911 getpw_buf = RSTRING_PTR(*getpw_tmp);
5915 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
5917 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5918 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5919 rb_str_resize(*getpw_tmp, 0);
5923 args.buf = RSTRING_PTR(*getpw_tmp);
5926 pwptr = args.result;
5928 pwptr = getpwnam(usrname);
5931#ifndef USE_GETPWNAM_R
5934 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
5936 uid = pwptr->pw_uid;
5937#ifndef USE_GETPWNAM_R
5944# ifdef p_uid_from_name
5964#if defined(HAVE_GRP_H)
5965# if defined(USE_GETGRNAM_R)
5966struct getgrnam_r_args {
5970 struct group *result;
5974# define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
5975 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5978nogvl_getgrnam_r(
void *args)
5980 struct getgrnam_r_args *arg = args;
5981 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
5987# ifdef USE_GETGRNAM_R
6000 struct group *grptr;
6001#ifdef USE_GETGRNAM_R
6006 getgr_buf_len = GETGR_R_SIZE_INIT;
6007 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6010 getgr_buf = RSTRING_PTR(*getgr_tmp);
6014 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6016 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6017 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6018 rb_str_resize(*getgr_tmp, 0);
6022 args.buf = RSTRING_PTR(*getgr_tmp);
6025 grptr = args.result;
6026#elif defined(HAVE_GETGRNAM)
6027 grptr = getgrnam(grpname);
6032#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6035 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6037 gid = grptr->gr_gid;
6038#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6045# ifdef p_gid_from_name
6065#if defined HAVE_SETUID
6079 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6083#define p_sys_setuid rb_f_notimplement
6087#if defined HAVE_SETRUID
6101 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6105#define p_sys_setruid rb_f_notimplement
6109#if defined HAVE_SETEUID
6123 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6127#define p_sys_seteuid rb_f_notimplement
6131#if defined HAVE_SETREUID
6146 rb_uid_t ruid, euid;
6149 ruid = OBJ2UID1(rid);
6150 euid = OBJ2UID1(eid);
6152 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6156#define p_sys_setreuid rb_f_notimplement
6160#if defined HAVE_SETRESUID
6175 rb_uid_t ruid, euid, suid;
6178 ruid = OBJ2UID1(rid);
6179 euid = OBJ2UID1(eid);
6180 suid = OBJ2UID1(sid);
6182 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6186#define p_sys_setresuid rb_f_notimplement
6203proc_getuid(
VALUE obj)
6205 rb_uid_t uid = getuid();
6210#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6230#if defined(HAVE_SETRESUID)
6231 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6232#elif defined HAVE_SETREUID
6233 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6234#elif defined HAVE_SETRUID
6235 if (setruid(uid) < 0) rb_sys_fail(0);
6236#elif defined HAVE_SETUID
6238 if (geteuid() == uid) {
6239 if (setuid(uid) < 0) rb_sys_fail(0);
6249#define proc_setuid rb_f_notimplement
6263static rb_uid_t SAVED_USER_ID = -1;
6287 if (geteuid() == 0) {
6288#if defined(HAVE_SETRESUID)
6289 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6290 SAVED_USER_ID = uid;
6291#elif defined(HAVE_SETUID)
6292 if (setuid(uid) < 0) rb_sys_fail(0);
6293 SAVED_USER_ID = uid;
6294#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6295 if (getuid() == uid) {
6296 if (SAVED_USER_ID == uid) {
6297 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6301 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6302 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6304 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6305 SAVED_USER_ID = uid;
6308 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6310 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6311 SAVED_USER_ID = uid;
6316 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6317 SAVED_USER_ID = uid;
6319#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6320 if (getuid() == uid) {
6321 if (SAVED_USER_ID == uid) {
6322 if (seteuid(uid) < 0) rb_sys_fail(0);
6326 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6328 if (setruid(0) < 0) rb_sys_fail(0);
6331 if (setruid(0) < 0) rb_sys_fail(0);
6333 if (seteuid(uid) < 0) rb_sys_fail(0);
6334 if (setruid(uid) < 0) rb_sys_fail(0);
6335 SAVED_USER_ID = uid;
6340 if (seteuid(uid) < 0) rb_sys_fail(0);
6341 if (setruid(uid) < 0) rb_sys_fail(0);
6342 SAVED_USER_ID = uid;
6350#if defined(HAVE_SETRESUID)
6351 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6352 (geteuid() == uid)? (rb_uid_t)-1: uid,
6353 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6354 SAVED_USER_ID = uid;
6355#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6356 if (SAVED_USER_ID == uid) {
6357 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6358 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6361 else if (getuid() != uid) {
6362 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6364 SAVED_USER_ID = uid;
6366 else if ( geteuid() != uid) {
6367 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6368 SAVED_USER_ID = uid;
6369 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6372 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6373 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6374 SAVED_USER_ID = uid;
6375 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6377#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6378 if (SAVED_USER_ID == uid) {
6379 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6380 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6382 else if ( geteuid() == uid) {
6383 if (getuid() != uid) {
6384 if (setruid(uid) < 0) rb_sys_fail(0);
6385 SAVED_USER_ID = uid;
6388 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6389 SAVED_USER_ID = uid;
6390 if (setruid(uid) < 0) rb_sys_fail(0);
6393 else if ( getuid() == uid) {
6394 if (seteuid(uid) < 0) rb_sys_fail(0);
6395 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6396 SAVED_USER_ID = uid;
6397 if (setruid(uid) < 0) rb_sys_fail(0);
6402#elif defined HAVE_44BSD_SETUID
6403 if (getuid() == uid) {
6405 if (setuid(uid) < 0) rb_sys_fail(0);
6406 SAVED_USER_ID = uid;
6411#elif defined HAVE_SETEUID
6412 if (getuid() == uid && SAVED_USER_ID == uid) {
6413 if (seteuid(uid) < 0) rb_sys_fail(0);
6418#elif defined HAVE_SETUID
6419 if (getuid() == uid && SAVED_USER_ID == uid) {
6420 if (setuid(uid) < 0) rb_sys_fail(0);
6434#if defined HAVE_SETGID
6448 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6452#define p_sys_setgid rb_f_notimplement
6456#if defined HAVE_SETRGID
6470 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6474#define p_sys_setrgid rb_f_notimplement
6478#if defined HAVE_SETEGID
6492 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6496#define p_sys_setegid rb_f_notimplement
6500#if defined HAVE_SETREGID
6515 rb_gid_t rgid, egid;
6517 rgid = OBJ2GID(rid);
6518 egid = OBJ2GID(eid);
6519 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6523#define p_sys_setregid rb_f_notimplement
6526#if defined HAVE_SETRESGID
6541 rb_gid_t rgid, egid, sgid;
6543 rgid = OBJ2GID(rid);
6544 egid = OBJ2GID(eid);
6545 sgid = OBJ2GID(sid);
6546 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6550#define p_sys_setresgid rb_f_notimplement
6554#if defined HAVE_ISSETUGID
6568p_sys_issetugid(
VALUE obj)
6570 return RBOOL(issetugid());
6573#define p_sys_issetugid rb_f_notimplement
6590proc_getgid(
VALUE obj)
6592 rb_gid_t gid = getgid();
6597#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6616#if defined(HAVE_SETRESGID)
6617 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6618#elif defined HAVE_SETREGID
6619 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6620#elif defined HAVE_SETRGID
6621 if (setrgid(gid) < 0) rb_sys_fail(0);
6622#elif defined HAVE_SETGID
6624 if (getegid() == gid) {
6625 if (setgid(gid) < 0) rb_sys_fail(0);
6635#define proc_setgid rb_f_notimplement
6639#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6659static int _maxgroups = -1;
6661get_sc_ngroups_max(
void)
6663#ifdef _SC_NGROUPS_MAX
6664 return (
int)sysconf(_SC_NGROUPS_MAX);
6665#elif defined(NGROUPS_MAX)
6666 return (
int)NGROUPS_MAX;
6674 if (_maxgroups < 0) {
6675 _maxgroups = get_sc_ngroups_max();
6677 _maxgroups = RB_MAX_GROUPS;
6686#ifdef HAVE_GETGROUPS
6710proc_getgroups(
VALUE obj)
6716 ngroups = getgroups(0, NULL);
6720 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6722 ngroups = getgroups(ngroups, groups);
6727 for (i = 0; i < ngroups; i++)
6735#define proc_getgroups rb_f_notimplement
6739#ifdef HAVE_SETGROUPS
6764 if (ngroups > maxgroups())
6765 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6767 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6769 for (i = 0; i < ngroups; i++) {
6772 groups[i] = OBJ2GID1(g);
6776 if (setgroups(ngroups, groups) == -1)
6781 return proc_getgroups(obj);
6784#define proc_setgroups rb_f_notimplement
6788#ifdef HAVE_INITGROUPS
6814 return proc_getgroups(obj);
6817#define proc_initgroups rb_f_notimplement
6820#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6833proc_getmaxgroups(
VALUE obj)
6838#define proc_getmaxgroups rb_f_notimplement
6841#ifdef HAVE_SETGROUPS
6854 int ngroups_max = get_sc_ngroups_max();
6857 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6859 if (ngroups > RB_MAX_GROUPS)
6860 ngroups = RB_MAX_GROUPS;
6862 if (ngroups_max > 0 && ngroups > ngroups_max)
6863 ngroups = ngroups_max;
6865 _maxgroups = ngroups;
6870#define proc_setmaxgroups rb_f_notimplement
6873#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6874static int rb_daemon(
int nochdir,
int noclose);
6899 int n, nochdir = FALSE, noclose = FALSE;
6902 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6903 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6907 n = rb_daemon(nochdir, noclose);
6908 if (n < 0) rb_sys_fail(
"daemon");
6912extern const char ruby_null_device[];
6915rb_daemon(
int nochdir,
int noclose)
6919 rb_pid_t pid = getpid();
6921 rb_thread_acquire_fork_lock();
6922 err = daemon(nochdir, noclose);
6924 release_fork_lock_after_fork(getpid() == pid ? pid : 0);
6929 switch (rb_fork_ruby(NULL)) {
6932 default: _exit(EXIT_SUCCESS);
6936 if (setsid() < 0) (void)0;
6941 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
6953#define proc_daemon rb_f_notimplement
6966static rb_gid_t SAVED_GROUP_ID = -1;
6990 if (geteuid() == 0) {
6991#if defined(HAVE_SETRESGID)
6992 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
6993 SAVED_GROUP_ID = gid;
6994#elif defined HAVE_SETGID
6995 if (setgid(gid) < 0) rb_sys_fail(0);
6996 SAVED_GROUP_ID = gid;
6997#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
6998 if (getgid() == gid) {
6999 if (SAVED_GROUP_ID == gid) {
7000 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7004 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7005 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
7007 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7008 SAVED_GROUP_ID = gid;
7011 if (setregid(0, 0) < 0) rb_sys_fail(0);
7013 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7014 SAVED_GROUP_ID = gid;
7019 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7020 SAVED_GROUP_ID = gid;
7022#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7023 if (getgid() == gid) {
7024 if (SAVED_GROUP_ID == gid) {
7025 if (setegid(gid) < 0) rb_sys_fail(0);
7029 if (setegid(gid) < 0) rb_sys_fail(0);
7030 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7032 if (setrgid(0) < 0) rb_sys_fail(0);
7035 if (setrgid(0) < 0) rb_sys_fail(0);
7037 if (setegid(gid) < 0) rb_sys_fail(0);
7038 if (setrgid(gid) < 0) rb_sys_fail(0);
7039 SAVED_GROUP_ID = gid;
7044 if (setegid(gid) < 0) rb_sys_fail(0);
7045 if (setrgid(gid) < 0) rb_sys_fail(0);
7046 SAVED_GROUP_ID = gid;
7053#if defined(HAVE_SETRESGID)
7054 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7055 (getegid() == gid)? (rb_gid_t)-1: gid,
7056 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7057 SAVED_GROUP_ID = gid;
7058#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7059 if (SAVED_GROUP_ID == gid) {
7060 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7061 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7064 else if (getgid() != gid) {
7065 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7067 SAVED_GROUP_ID = gid;
7069 else if ( getegid() != gid) {
7070 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7071 SAVED_GROUP_ID = gid;
7072 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7075 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7076 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7077 SAVED_GROUP_ID = gid;
7078 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7080#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7081 if (SAVED_GROUP_ID == gid) {
7082 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7083 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7085 else if ( getegid() == gid) {
7086 if (getgid() != gid) {
7087 if (setrgid(gid) < 0) rb_sys_fail(0);
7088 SAVED_GROUP_ID = gid;
7091 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7092 SAVED_GROUP_ID = gid;
7093 if (setrgid(gid) < 0) rb_sys_fail(0);
7096 else if ( getgid() == gid) {
7097 if (setegid(gid) < 0) rb_sys_fail(0);
7098 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7099 SAVED_GROUP_ID = gid;
7100 if (setrgid(gid) < 0) rb_sys_fail(0);
7105#elif defined HAVE_44BSD_SETGID
7106 if (getgid() == gid) {
7108 if (setgid(gid) < 0) rb_sys_fail(0);
7109 SAVED_GROUP_ID = gid;
7114#elif defined HAVE_SETEGID
7115 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7116 if (setegid(gid) < 0) rb_sys_fail(0);
7121#elif defined HAVE_SETGID
7122 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7123 if (setgid(gid) < 0) rb_sys_fail(0);
7150proc_geteuid(
VALUE obj)
7152 rb_uid_t euid = geteuid();
7156#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7158proc_seteuid(rb_uid_t uid)
7160#if defined(HAVE_SETRESUID)
7161 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7162#elif defined HAVE_SETREUID
7163 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7164#elif defined HAVE_SETEUID
7165 if (seteuid(uid) < 0) rb_sys_fail(0);
7166#elif defined HAVE_SETUID
7167 if (uid == getuid()) {
7168 if (setuid(uid) < 0) rb_sys_fail(0);
7179#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7193 proc_seteuid(OBJ2UID(euid));
7197#define proc_seteuid_m rb_f_notimplement
7201rb_seteuid_core(rb_uid_t euid)
7203#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7209#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7213#if defined(HAVE_SETRESUID)
7215 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7216 SAVED_USER_ID = euid;
7219 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7221#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7222 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7224 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7225 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7226 SAVED_USER_ID = euid;
7228#elif defined HAVE_SETEUID
7229 if (seteuid(euid) < 0) rb_sys_fail(0);
7230#elif defined HAVE_SETUID
7231 if (geteuid() == 0) rb_sys_fail(0);
7232 if (setuid(euid) < 0) rb_sys_fail(0);
7257 rb_seteuid_core(OBJ2UID(
id));
7276proc_getegid(
VALUE obj)
7278 rb_gid_t egid = getegid();
7283#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7296#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7302#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7303 gid = OBJ2GID(egid);
7306#if defined(HAVE_SETRESGID)
7307 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7308#elif defined HAVE_SETREGID
7309 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7310#elif defined HAVE_SETEGID
7311 if (setegid(gid) < 0) rb_sys_fail(0);
7312#elif defined HAVE_SETGID
7313 if (gid == getgid()) {
7314 if (setgid(gid) < 0) rb_sys_fail(0);
7326#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7327#define proc_setegid_m proc_setegid
7329#define proc_setegid_m rb_f_notimplement
7333rb_setegid_core(rb_gid_t egid)
7335#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7341#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7345#if defined(HAVE_SETRESGID)
7347 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7348 SAVED_GROUP_ID = egid;
7351 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7353#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7354 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7356 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7357 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7358 SAVED_GROUP_ID = egid;
7360#elif defined HAVE_SETEGID
7361 if (setegid(egid) < 0) rb_sys_fail(0);
7362#elif defined HAVE_SETGID
7363 if (geteuid() == 0 ) rb_sys_fail(0);
7364 if (setgid(egid) < 0) rb_sys_fail(0);
7389 rb_setegid_core(OBJ2GID(
id));
7404p_uid_exchangeable(
VALUE _)
7406#if defined(HAVE_SETRESUID)
7408#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7429p_uid_exchange(
VALUE obj)
7432#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7439#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7443#if defined(HAVE_SETRESUID)
7444 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7445 SAVED_USER_ID = uid;
7446#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7447 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7448 SAVED_USER_ID = uid;
7466p_gid_exchangeable(
VALUE _)
7468#if defined(HAVE_SETRESGID)
7470#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7491p_gid_exchange(
VALUE obj)
7494#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7501#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7505#if defined(HAVE_SETRESGID)
7506 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7507 SAVED_GROUP_ID = gid;
7508#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7509 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7510 SAVED_GROUP_ID = gid;
7529p_uid_have_saved_id(
VALUE _)
7531#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7539#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7541p_uid_sw_ensure(
VALUE i)
7543 rb_uid_t
id = (rb_uid_t)i;
7544 under_uid_switch = 0;
7545 id = rb_seteuid_core(
id);
7564p_uid_switch(
VALUE obj)
7576 under_uid_switch = 1;
7583 else if (euid != SAVED_USER_ID) {
7584 proc_seteuid(SAVED_USER_ID);
7586 under_uid_switch = 1;
7601p_uid_sw_ensure(
VALUE obj)
7603 under_uid_switch = 0;
7604 return p_uid_exchange(obj);
7608p_uid_switch(
VALUE obj)
7620 p_uid_exchange(obj);
7622 under_uid_switch = 1;
7644p_gid_have_saved_id(
VALUE _)
7646#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7653#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7655p_gid_sw_ensure(
VALUE i)
7657 rb_gid_t
id = (rb_gid_t)i;
7658 under_gid_switch = 0;
7659 id = rb_setegid_core(
id);
7678p_gid_switch(
VALUE obj)
7690 under_gid_switch = 1;
7697 else if (egid != SAVED_GROUP_ID) {
7698 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7700 under_gid_switch = 1;
7715p_gid_sw_ensure(
VALUE obj)
7717 under_gid_switch = 0;
7718 return p_gid_exchange(obj);
7722p_gid_switch(
VALUE obj)
7734 p_gid_exchange(obj);
7736 under_gid_switch = 1;
7746#if defined(HAVE_TIMES)
7750#ifdef HAVE__SC_CLK_TCK
7751 return sysconf(_SC_CLK_TCK);
7752#elif defined CLK_TCK
7775rb_proc_times(
VALUE obj)
7777 VALUE utime, stime, cutime, cstime, ret;
7778#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7779 struct rusage usage_s, usage_c;
7781 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7782 rb_sys_fail(
"getrusage");
7783 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7784 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7785 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7786 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7788 const double hertz = (double)get_clk_tck();
7792 utime =
DBL2NUM(buf.tms_utime / hertz);
7793 stime =
DBL2NUM(buf.tms_stime / hertz);
7794 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7795 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7797 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7805#define rb_proc_times rb_f_notimplement
7808#ifdef HAVE_LONG_LONG
7810#define TIMETICK_INT_MIN LLONG_MIN
7811#define TIMETICK_INT_MAX LLONG_MAX
7812#define TIMETICK_INT2NUM(v) LL2NUM(v)
7813#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7815typedef long timetick_int_t;
7816#define TIMETICK_INT_MIN LONG_MIN
7817#define TIMETICK_INT_MAX LONG_MAX
7818#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7819#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7822CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7823static timetick_int_t
7824gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7844reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7846 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7854reduce_factors(timetick_int_t *numerators,
int num_numerators,
7855 timetick_int_t *denominators,
int num_denominators)
7858 for (i = 0; i < num_numerators; i++) {
7859 if (numerators[i] == 1)
7861 for (j = 0; j < num_denominators; j++) {
7862 if (denominators[j] == 1)
7864 reduce_fraction(&numerators[i], &denominators[j]);
7870 timetick_int_t giga_count;
7875timetick2dblnum(
struct timetick *ttp,
7876 timetick_int_t *numerators,
int num_numerators,
7877 timetick_int_t *denominators,
int num_denominators)
7882 reduce_factors(numerators, num_numerators,
7883 denominators, num_denominators);
7885 d = ttp->giga_count * 1e9 + ttp->count;
7887 for (i = 0; i < num_numerators; i++)
7889 for (i = 0; i < num_denominators; i++)
7890 d /= denominators[i];
7896timetick2dblnum_reciprocal(
struct timetick *ttp,
7897 timetick_int_t *numerators,
int num_numerators,
7898 timetick_int_t *denominators,
int num_denominators)
7903 reduce_factors(numerators, num_numerators,
7904 denominators, num_denominators);
7907 for (i = 0; i < num_denominators; i++)
7908 d *= denominators[i];
7909 for (i = 0; i < num_numerators; i++)
7911 d /= ttp->giga_count * 1e9 + ttp->count;
7916#define NDIV(x,y) (-(-((x)+1)/(y))-1)
7917#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
7920timetick2integer(
struct timetick *ttp,
7921 timetick_int_t *numerators,
int num_numerators,
7922 timetick_int_t *denominators,
int num_denominators)
7927 reduce_factors(numerators, num_numerators,
7928 denominators, num_denominators);
7930 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
7931 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
7932 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
7933 for (i = 0; i < num_numerators; i++) {
7934 timetick_int_t factor = numerators[i];
7935 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
7939 for (i = 0; i < num_denominators; i++) {
7940 t = DIV(t, denominators[i]);
7942 return TIMETICK_INT2NUM(t);
7946 v = TIMETICK_INT2NUM(ttp->giga_count);
7949 for (i = 0; i < num_numerators; i++) {
7950 timetick_int_t factor = numerators[i];
7953 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
7955 for (i = 0; i < num_denominators; i++) {
7956 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
7962make_clock_result(
struct timetick *ttp,
7963 timetick_int_t *numerators,
int num_numerators,
7964 timetick_int_t *denominators,
int num_denominators,
7967 if (unit ==
ID2SYM(id_nanosecond)) {
7968 numerators[num_numerators++] = 1000000000;
7969 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7971 else if (unit ==
ID2SYM(id_microsecond)) {
7972 numerators[num_numerators++] = 1000000;
7973 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7975 else if (unit ==
ID2SYM(id_millisecond)) {
7976 numerators[num_numerators++] = 1000;
7977 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7979 else if (unit ==
ID2SYM(id_second)) {
7980 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7982 else if (unit ==
ID2SYM(id_float_microsecond)) {
7983 numerators[num_numerators++] = 1000000;
7984 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7986 else if (unit ==
ID2SYM(id_float_millisecond)) {
7987 numerators[num_numerators++] = 1000;
7988 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7990 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
7991 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7994 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
7998static const mach_timebase_info_data_t *
7999get_mach_timebase_info(
void)
8001 static mach_timebase_info_data_t sTimebaseInfo;
8003 if ( sTimebaseInfo.denom == 0 ) {
8004 (void) mach_timebase_info(&sTimebaseInfo);
8007 return &sTimebaseInfo;
8011ruby_real_ms_time(
void)
8013 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8014 uint64_t t = mach_absolute_time();
8015 return (
double)t * info->numer / info->denom / 1e6;
8019#if defined(NUM2CLOCKID)
8020# define NUMERIC_CLOCKID 1
8022# define NUMERIC_CLOCKID 0
8023# define NUM2CLOCKID(x) 0
8026#define clock_failed(name, err, arg) do { \
8027 int clock_error = (err); \
8028 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8208 timetick_int_t numerators[2];
8209 timetick_int_t denominators[2];
8210 int num_numerators = 0;
8211 int num_denominators = 0;
8214 VALUE clk_id = argv[0];
8215#ifdef HAVE_CLOCK_GETTIME
8220#ifdef CLOCK_REALTIME
8221 if (clk_id == RUBY_CLOCK_REALTIME) {
8227#ifdef CLOCK_MONOTONIC
8228 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8229 c = CLOCK_MONOTONIC;
8234#ifdef CLOCK_PROCESS_CPUTIME_ID
8235 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8236 c = CLOCK_PROCESS_CPUTIME_ID;
8241#ifdef CLOCK_THREAD_CPUTIME_ID
8242 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8243 c = CLOCK_THREAD_CPUTIME_ID;
8251#ifdef HAVE_GETTIMEOFDAY
8256#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8257 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8259 ret = gettimeofday(&tv, 0);
8261 rb_sys_fail(
"gettimeofday");
8262 tt.giga_count = tv.tv_sec;
8263 tt.count = (int32_t)tv.tv_usec * 1000;
8264 denominators[num_denominators++] = 1000000000;
8269#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8270 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8273 if (t == (time_t)-1)
8274 rb_sys_fail(
"time");
8277 denominators[num_denominators++] = 1000000000;
8282#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8283 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8284 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8287 unsigned_clock_t uc;
8289 if (c == (clock_t)-1)
8290 rb_sys_fail(
"times");
8291 uc = (unsigned_clock_t)c;
8292 tt.count = (int32_t)(uc % 1000000000);
8293 tt.giga_count = (uc / 1000000000);
8294 denominators[num_denominators++] = get_clk_tck();
8300#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8301 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8302 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8303 struct rusage usage;
8305 ret = getrusage(RUSAGE_SELF, &usage);
8307 rb_sys_fail(
"getrusage");
8308 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8309 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8310 if (1000000 <= usec) {
8314 tt.count = usec * 1000;
8315 denominators[num_denominators++] = 1000000000;
8321#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8322 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8323 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8325 unsigned_clock_t utime, stime;
8326 if (times(&buf) == (clock_t)-1)
8327 rb_sys_fail(
"times");
8328 utime = (unsigned_clock_t)buf.tms_utime;
8329 stime = (unsigned_clock_t)buf.tms_stime;
8330 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8331 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8332 if (1000000000 <= tt.count) {
8333 tt.count -= 1000000000;
8336 denominators[num_denominators++] = get_clk_tck();
8341#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8342 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8343 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8345 unsigned_clock_t uc;
8348 if (c == (clock_t)-1)
8349 rb_sys_fail(
"clock");
8350 uc = (unsigned_clock_t)c;
8351 tt.count = (int32_t)(uc % 1000000000);
8352 tt.giga_count = uc / 1000000000;
8353 denominators[num_denominators++] = CLOCKS_PER_SEC;
8358 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8359 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8360 uint64_t t = mach_absolute_time();
8361 tt.count = (int32_t)(t % 1000000000);
8362 tt.giga_count = t / 1000000000;
8363 numerators[num_numerators++] = info->numer;
8364 denominators[num_denominators++] = info->denom;
8365 denominators[num_denominators++] = 1000000000;
8370 else if (NUMERIC_CLOCKID) {
8371#if defined(HAVE_CLOCK_GETTIME)
8373 c = NUM2CLOCKID(clk_id);
8375 ret = clock_gettime(c, &ts);
8377 clock_failed(
"gettime",
errno, clk_id);
8378 tt.count = (int32_t)ts.tv_nsec;
8379 tt.giga_count = ts.tv_sec;
8380 denominators[num_denominators++] = 1000000000;
8387 clock_failed(
"gettime", EINVAL, clk_id);
8390 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8435 timetick_int_t numerators[2];
8436 timetick_int_t denominators[2];
8437 int num_numerators = 0;
8438 int num_denominators = 0;
8439#ifdef HAVE_CLOCK_GETRES
8444 VALUE clk_id = argv[0];
8447#ifdef CLOCK_REALTIME
8448 if (clk_id == RUBY_CLOCK_REALTIME) {
8454#ifdef CLOCK_MONOTONIC
8455 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8456 c = CLOCK_MONOTONIC;
8461#ifdef CLOCK_PROCESS_CPUTIME_ID
8462 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8463 c = CLOCK_PROCESS_CPUTIME_ID;
8468#ifdef CLOCK_THREAD_CPUTIME_ID
8469 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8470 c = CLOCK_THREAD_CPUTIME_ID;
8475#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8476 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8479 denominators[num_denominators++] = 1000000000;
8484#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8485 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8488 denominators[num_denominators++] = 1000000000;
8493#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8494 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8497 denominators[num_denominators++] = get_clk_tck();
8502#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8503 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8506 denominators[num_denominators++] = 1000000000;
8511#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8512 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8515 denominators[num_denominators++] = get_clk_tck();
8520#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8521 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8524 denominators[num_denominators++] = CLOCKS_PER_SEC;
8529#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8530 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8531 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8534 numerators[num_numerators++] = info->numer;
8535 denominators[num_denominators++] = info->denom;
8536 denominators[num_denominators++] = 1000000000;
8541 else if (NUMERIC_CLOCKID) {
8542#if defined(HAVE_CLOCK_GETRES)
8544 c = NUM2CLOCKID(clk_id);
8546 ret = clock_getres(c, &ts);
8548 clock_failed(
"getres",
errno, clk_id);
8549 tt.count = (int32_t)ts.tv_nsec;
8550 tt.giga_count = ts.tv_sec;
8551 denominators[num_denominators++] = 1000000000;
8558 clock_failed(
"getres", EINVAL, clk_id);
8561 if (unit ==
ID2SYM(id_hertz)) {
8562 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8565 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8570get_CHILD_STATUS(
ID _x,
VALUE *_y)
8576get_PROCESS_ID(
ID _x,
VALUE *_y)
8668static VALUE rb_mProcUID;
8669static VALUE rb_mProcGID;
8670static VALUE rb_mProcID_Syscall;
8706 rb_eager_load_detailed_message_extension();
8709 rb_gc_prepare_heap();
9137 rb_gvar_ractor_local(
"$$");
9138 rb_gvar_ractor_local(
"$?");
9139 rb_gvar_box_ready(
"$$");
9140 rb_gvar_box_ready(
"$?");
9195 process_status_dump, process_status_load);
9231#ifdef HAVE_GETPRIORITY
9242#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9244 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9245#ifdef RLIM_SAVED_MAX
9247 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9249 rb_define_const(
rb_mProcess,
"RLIM_SAVED_MAX", v);
9253 rb_define_const(
rb_mProcess,
"RLIM_INFINITY", inf);
9254#ifdef RLIM_SAVED_CUR
9256 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9258 rb_define_const(
rb_mProcess,
"RLIM_SAVED_CUR", v);
9297#ifdef RLIMIT_MEMLOCK
9304#ifdef RLIMIT_MSGQUEUE
9370#ifdef RLIMIT_SIGPENDING
9405#if defined(RUBY_CLOCK_REALTIME)
9406#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9407# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9408#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9409# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9411#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9413 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
9414#elif defined(RUBY_CLOCK_REALTIME)
9415 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
9418#if defined(RUBY_CLOCK_MONOTONIC)
9419#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9420# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9422#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9424 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
9425#elif defined(RUBY_CLOCK_MONOTONIC)
9426 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", RUBY_CLOCK_MONOTONIC);
9429#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9430#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9431# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9433#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9435 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
9436#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9437 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", RUBY_CLOCK_PROCESS_CPUTIME_ID);
9440#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9442 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
9443#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9444 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", RUBY_CLOCK_THREAD_CPUTIME_ID);
9450 rb_define_const(
rb_mProcess,
"CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
9454 rb_define_const(
rb_mProcess,
"CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
9456#ifdef CLOCK_REALTIME_FAST
9458 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
9460#ifdef CLOCK_REALTIME_PRECISE
9462 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
9464#ifdef CLOCK_REALTIME_COARSE
9466 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_COARSE", CLOCKID2NUM(CLOCK_REALTIME_COARSE));
9468#ifdef CLOCK_REALTIME_ALARM
9470 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_ALARM", CLOCKID2NUM(CLOCK_REALTIME_ALARM));
9472#ifdef CLOCK_MONOTONIC_FAST
9474 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
9476#ifdef CLOCK_MONOTONIC_PRECISE
9478 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
9480#ifdef CLOCK_MONOTONIC_RAW
9482 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
9484#ifdef CLOCK_MONOTONIC_RAW_APPROX
9486 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW_APPROX", CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX));
9488#ifdef CLOCK_MONOTONIC_COARSE
9490 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_COARSE", CLOCKID2NUM(CLOCK_MONOTONIC_COARSE));
9492#ifdef CLOCK_BOOTTIME
9494 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME", CLOCKID2NUM(CLOCK_BOOTTIME));
9496#ifdef CLOCK_BOOTTIME_ALARM
9498 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME_ALARM", CLOCKID2NUM(CLOCK_BOOTTIME_ALARM));
9502 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
9504#ifdef CLOCK_UPTIME_FAST
9506 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
9508#ifdef CLOCK_UPTIME_PRECISE
9510 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
9512#ifdef CLOCK_UPTIME_RAW
9514 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW", CLOCKID2NUM(CLOCK_UPTIME_RAW));
9516#ifdef CLOCK_UPTIME_RAW_APPROX
9518 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW_APPROX", CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX));
9522 rb_define_const(
rb_mProcess,
"CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
9526 rb_define_const(
rb_mProcess,
"CLOCK_TAI", CLOCKID2NUM(CLOCK_TAI));
9532#if defined(HAVE_TIMES)
9546 SAVED_USER_ID = geteuid();
9547 SAVED_GROUP_ID = getegid();
9549 rb_mProcUID = rb_define_module_under(
rb_mProcess,
"UID");
9550 rb_mProcGID = rb_define_module_under(
rb_mProcess,
"GID");
9570#ifdef p_uid_from_name
9573#ifdef p_gid_from_name
9577 rb_mProcID_Syscall = rb_define_module_under(
rb_mProcess,
"Sys");
9604#define define_id(name) id_##name = rb_intern_const(#name)
9617 define_id(new_pgroup);
9619 define_id(unsetenv_others);
9622 define_id(close_others);
9623 define_id(nanosecond);
9624 define_id(microsecond);
9625 define_id(millisecond);
9627 define_id(float_microsecond);
9628 define_id(float_millisecond);
9629 define_id(float_second);
9630 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9631 define_id(TIME_BASED_CLOCK_REALTIME);
9632#ifdef CLOCK_REALTIME
9633 define_id(CLOCK_REALTIME);
9635#ifdef CLOCK_MONOTONIC
9636 define_id(CLOCK_MONOTONIC);
9638#ifdef CLOCK_PROCESS_CPUTIME_ID
9639 define_id(CLOCK_PROCESS_CPUTIME_ID);
9641#ifdef CLOCK_THREAD_CPUTIME_ID
9642 define_id(CLOCK_THREAD_CPUTIME_ID);
9645 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9646 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9649 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9651 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9653 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
9656#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_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
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_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#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.
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
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(rb_len_t len)
Allocates a "temporary" string.
#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.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_buf_new(rb_len_t capa)
Allocates a "string buffer".
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.
#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.
#define RARRAY_AREF(a, i)
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
#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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
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.