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;
588 .wrap_struct_name =
"Process::Status",
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);
682last_status_clear(rb_thread_t *th)
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;
1044 rb_execution_context_t *ec;
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 rb_thread_reset_fork_lock();
1598 rb_thread_release_fork_lock();
1603#if defined(HAVE_WORKING_FORK)
1605COMPILER_WARNING_PUSH
1606#if __has_warning("-Wdeprecated-declarations") || RBIMPL_COMPILER_IS(GCC)
1607COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
1609static inline rb_pid_t
1617#define try_with_sh(err, prog, argv, envp) ((err == ENOEXEC) ? exec_with_sh((prog), (argv), (envp)) : (void)0)
1619exec_with_sh(
const char *prog,
char **argv,
char **envp)
1621 *argv = (
char *)prog;
1622 *--argv = (
char *)
"sh";
1624 execve(
"/bin/sh", argv, envp);
1626 execv(
"/bin/sh", argv);
1630#define try_with_sh(err, prog, argv, envp) (void)0
1635proc_exec_cmd(
const char *prog,
VALUE argv_str,
VALUE envp_str)
1643 argv = ARGVSTR2ARGV(argv_str);
1650 rb_w32_uaspawn(P_OVERLAY, prog, argv);
1653 envp = envp_str ? RB_IMEMO_TMPBUF_PTR(envp_str) : NULL;
1655 execve(prog, argv, envp);
1659 try_with_sh(err, prog, argv, envp);
1666proc_exec_sh(
const char *str,
VALUE envp_str)
1671 while (*s ==
' ' || *s ==
'\t' || *s ==
'\n')
1679 rb_w32_uspawn(P_OVERLAY, (
char *)str, 0);
1682 execle(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL, RB_IMEMO_TMPBUF_PTR(envp_str));
1684 execl(
"/bin/sh",
"sh",
"-c", str, (
char *)NULL);
1694 ret = proc_exec_sh(str,
Qfalse);
1701mark_exec_arg(
void *ptr)
1704 if (eargp->use_shell)
1705 rb_gc_mark(eargp->invoke.sh.shell_script);
1707 rb_gc_mark(eargp->invoke.cmd.command_name);
1708 rb_gc_mark(eargp->invoke.cmd.command_abspath);
1709 rb_gc_mark(eargp->invoke.cmd.argv_str);
1710 rb_gc_mark(eargp->invoke.cmd.argv_buf);
1712 rb_gc_mark(eargp->redirect_fds);
1713 rb_gc_mark(eargp->envp_str);
1714 rb_gc_mark(eargp->envp_buf);
1715 rb_gc_mark(eargp->dup2_tmpbuf);
1716 rb_gc_mark(eargp->rlimit_limits);
1717 rb_gc_mark(eargp->fd_dup2);
1718 rb_gc_mark(eargp->fd_close);
1719 rb_gc_mark(eargp->fd_open);
1720 rb_gc_mark(eargp->fd_dup2_child);
1721 rb_gc_mark(eargp->env_modification);
1722 rb_gc_mark(eargp->path_env);
1723 rb_gc_mark(eargp->chdir_dir);
1727memsize_exec_arg(
const void *ptr)
1735 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE
1739# define DEFAULT_PROCESS_ENCODING rb_utf8_encoding()
1741#define EXPORT_STR(str) export_str(str)
1742#define EXPORT_DUP(str) export_dup(str)
1744WARN_UNUSED_RESULT(
static VALUE)
1745export_str(
VALUE str)
1749#ifdef DEFAULT_PROCESS_ENCODING
1751 mustnot_broken(str);
1753 rb_str_null_check(str);
1757WARN_UNUSED_RESULT(
static VALUE)
1758export_dup(
VALUE str)
1760 VALUE newstr = EXPORT_STR(str);
1765#if !defined(HAVE_WORKING_FORK) && defined(HAVE_SPAWNV)
1766# define USE_SPAWNV 1
1768# define USE_SPAWNV 0
1771# define P_NOWAIT _P_NOWAIT
1782 if (eargp->new_pgroup_given && eargp->new_pgroup_flag) {
1783 flags = CREATE_NEW_PROCESS_GROUP;
1785 pid = rb_w32_uaspawn_flags(P_NOWAIT, prog ? RSTRING_PTR(prog) : 0, argv, flags);
1790#define proc_spawn_sh(str) rb_w32_uspawn(P_NOWAIT, (str), 0)
1796 RBASIC_CLEAR_CLASS(obj);
1801check_exec_redirect_fd(
VALUE v,
int iskey)
1812 else if (
id == id_out)
1814 else if (
id == id_err)
1823 rb_raise(rb_eArgError,
"duplex IO redirection");
1830 rb_raise(rb_eArgError,
"negative file descriptor");
1833 else if (fd >= 3 && iskey) {
1834 rb_raise(rb_eArgError,
"wrong file descriptor (%d)", fd);
1840 rb_raise(rb_eArgError,
"wrong exec redirect");
1851 VALUE fd = check_exec_redirect_fd(key, !
NIL_P(param));
1858 VALUE fd = check_exec_redirect_fd(v, !
NIL_P(param));
1869 VALUE path, flags, perm;
1873 switch (
TYPE(val)) {
1876 if (
id == id_close) {
1878 eargp->fd_close = check_exec_redirect1(eargp->fd_close, key, param);
1880 else if (
id == id_in) {
1882 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1884 else if (
id == id_out) {
1886 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1888 else if (
id == id_err) {
1890 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1893 rb_raise(rb_eArgError,
"wrong exec redirect symbol: %"PRIsVALUE,
1900 val = check_exec_redirect_fd(val, 0);
1904 eargp->fd_dup2 = check_exec_redirect1(eargp->fd_dup2, key, param);
1910 path ==
ID2SYM(id_child)) {
1911 param = check_exec_redirect_fd(
rb_ary_entry(val, 1), 0);
1912 eargp->fd_dup2_child = check_exec_redirect1(eargp->fd_dup2_child, key, param);
1925 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1926 flags, perm,
Qnil));
1927 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1935 key = check_exec_redirect_fd(key, 1);
1937 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1942 VALUE fd = check_exec_redirect_fd(v, 1);
1946 flags =
INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
1953 param = hide_obj(
rb_ary_new3(4, hide_obj(EXPORT_DUP(path)),
1954 flags, perm,
Qnil));
1955 eargp->fd_open = check_exec_redirect1(eargp->fd_open, key, param);
1961 if (!
NIL_P(val))
goto io;
1962 rb_raise(rb_eArgError,
"wrong exec redirect action");
1967#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
1968static int rlimit_type_by_sym(
VALUE key);
1971rb_execarg_addopt_rlimit(
struct rb_execarg *eargp,
int rtype,
VALUE val)
1973 VALUE ary = eargp->rlimit_limits;
1974 VALUE tmp, softlim, hardlim;
1975 if (eargp->rlimit_limits ==
Qfalse)
1976 ary = eargp->rlimit_limits = hide_obj(
rb_ary_new());
1978 ary = eargp->rlimit_limits;
1988 rb_raise(rb_eArgError,
"wrong exec rlimit option");
1994 (void)NUM2RLIM(softlim);
1995 (void)NUM2RLIM(hardlim);
2001#define TO_BOOL(val, name) (NIL_P(val) ? 0 : rb_bool_expected((val), name, TRUE))
2005 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2009 switch (
TYPE(key)) {
2011#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
2013 int rtype = rlimit_type_by_sym(key);
2015 rb_execarg_addopt_rlimit(eargp, rtype, val);
2023 if (
id == id_pgroup) {
2025 if (eargp->pgroup_given) {
2026 rb_raise(rb_eArgError,
"pgroup option specified twice");
2030 else if (val ==
Qtrue)
2035 rb_raise(rb_eArgError,
"negative process group ID : %ld", (
long)pgroup);
2038 eargp->pgroup_given = 1;
2039 eargp->pgroup_pgid = pgroup;
2044 if (
id == id_new_pgroup) {
2045 if (eargp->new_pgroup_given) {
2046 rb_raise(rb_eArgError,
"new_pgroup option specified twice");
2048 eargp->new_pgroup_given = 1;
2049 eargp->new_pgroup_flag = TO_BOOL(val,
"new_pgroup");
2053 if (
id == id_unsetenv_others) {
2054 if (eargp->unsetenv_others_given) {
2055 rb_raise(rb_eArgError,
"unsetenv_others option specified twice");
2057 eargp->unsetenv_others_given = 1;
2058 eargp->unsetenv_others_do = TO_BOOL(val,
"unsetenv_others");
2060 else if (
id == id_chdir) {
2061 if (eargp->chdir_given) {
2062 rb_raise(rb_eArgError,
"chdir option specified twice");
2065 val = rb_str_encode_ospath(val);
2066 eargp->chdir_given = 1;
2067 eargp->chdir_dir = hide_obj(EXPORT_DUP(val));
2069 else if (
id == id_umask) {
2071 if (eargp->umask_given) {
2072 rb_raise(rb_eArgError,
"umask option specified twice");
2074 eargp->umask_given = 1;
2075 eargp->umask_mask = cmask;
2077 else if (
id == id_close_others) {
2078 if (eargp->close_others_given) {
2079 rb_raise(rb_eArgError,
"close_others option specified twice");
2081 eargp->close_others_given = 1;
2082 eargp->close_others_do = TO_BOOL(val,
"close_others");
2084 else if (
id == id_in) {
2088 else if (
id == id_out) {
2092 else if (
id == id_err) {
2096 else if (
id == id_uid) {
2098 if (eargp->uid_given) {
2099 rb_raise(rb_eArgError,
"uid option specified twice");
2103 eargp->uid = OBJ2UID(val);
2104 eargp->uid_given = 1;
2108 "uid option is unimplemented on this machine");
2111 else if (
id == id_gid) {
2113 if (eargp->gid_given) {
2114 rb_raise(rb_eArgError,
"gid option specified twice");
2118 eargp->gid = OBJ2GID(val);
2119 eargp->gid_given = 1;
2123 "gid option is unimplemented on this machine");
2126 else if (
id == id_exception) {
2127 if (eargp->exception_given) {
2128 rb_raise(rb_eArgError,
"exception option specified twice");
2130 eargp->exception_given = 1;
2131 eargp->exception = TO_BOOL(val,
"exception");
2142 check_exec_redirect(key, val, eargp);
2154check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2159 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2161 rb_raise(rb_eArgError,
"wrong exec option symbol: % "PRIsVALUE,
2163 rb_raise(rb_eArgError,
"wrong exec option: %"PRIsVALUE,
rb_obj_class(key));
2169check_exec_options_i_extract(st_data_t st_key, st_data_t st_val, st_data_t arg)
2174 VALUE execarg_obj = args[0];
2175 if (rb_execarg_addopt(execarg_obj, key, val) != ST_CONTINUE) {
2176 VALUE nonopts = args[1];
2178 rb_hash_aset(nonopts, key, val);
2193 rb_raise(rb_eArgError,
"fd %d specified twice", fd);
2195 if (ary == eargp->fd_dup2)
2197 else if (ary == eargp->fd_dup2_child)
2203 if (ary == eargp->fd_dup2 || ary == eargp->fd_dup2_child) {
2221 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2);
2222 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_close);
2223 maxhint = check_exec_fds_1(eargp, h, maxhint, eargp->fd_dup2_child);
2225 if (eargp->fd_dup2_child) {
2226 ary = eargp->fd_dup2_child;
2236 val = rb_hash_lookup(h, val);
2238 rb_raise(rb_eArgError,
"cyclic child fd redirection from %d", oldfd);
2242 rb_raise(rb_eArgError,
"child fd %d is not redirected", oldfd);
2243 if (oldfd != lastfd) {
2248 while (
FIXNUM_P(val2 = rb_hash_lookup(h, val))) {
2249 rb_hash_aset(h, val,
INT2FIX(lastfd));
2256 eargp->close_others_maxhint = maxhint;
2261rb_check_exec_options(
VALUE opthash,
VALUE execarg_obj)
2265 rb_hash_stlike_foreach(opthash, check_exec_options_i, (st_data_t)execarg_obj);
2269rb_execarg_extract_options(
VALUE execarg_obj,
VALUE opthash)
2274 args[0] = execarg_obj;
2276 rb_hash_stlike_foreach(opthash, check_exec_options_i_extract, (st_data_t)args);
2280#ifdef ENV_IGNORECASE
2281#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
2283#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
2287check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2294 const char *k = RSTRING_PTR(key = EXPORT_STR(key));
2296 rb_raise(rb_eArgError,
"environment name contains a equal : %"PRIsVALUE, key);
2298 if (!
NIL_P(val)) val = EXPORT_STR(val);
2315 rb_hash_stlike_foreach(hash, check_exec_env_i, (st_data_t)env);
2322rb_check_argv(
int argc,
VALUE *argv)
2333 rb_raise(rb_eArgError,
"wrong first argument");
2339 for (i = 0; i < argc; i++) {
2346check_hash(
VALUE obj)
2356 return rb_check_hash_type(obj);
2360rb_exec_getargs(
int *argc_p,
VALUE **argv_p,
int accept_shell,
VALUE *env_ret,
VALUE *opthash_ret)
2365 hash = check_hash((*argv_p)[*argc_p-1]);
2367 *opthash_ret = hash;
2373 hash = check_hash((*argv_p)[0]);
2380 prog = rb_check_argv(*argc_p, *argv_p);
2382 prog = (*argv_p)[0];
2383 if (accept_shell && *argc_p == 1) {
2398compare_posix_sh(
const void *key,
const void *el)
2401 int ret = strncmp(word->ptr, el, word->len);
2402 if (!ret && ((
const char *)el)[word->len]) ret = -1;
2407#define append_terminator(buf) rb_str_buf_cat(buf, "", 1)
2412 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2413 char fbuf[MAXPATHLEN];
2417 if (!
NIL_P(opthash)) {
2418 rb_check_exec_options(opthash, execarg_obj);
2421 env = rb_check_exec_env(env, &eargp->path_env);
2422 eargp->env_modification = env;
2425 prog = EXPORT_STR(prog);
2426 eargp->use_shell = argc == 0;
2427 if (eargp->use_shell)
2428 eargp->invoke.sh.shell_script = prog;
2430 eargp->invoke.cmd.command_name = prog;
2433 if (eargp->use_shell) {
2434 static const char posix_sh_cmds[][9] = {
2466 const char *
const s = rb_str_null_check(prog);
2495 for (p = s; p < e; p++) {
2496 if (*p ==
' ' || *p ==
'\t') {
2497 if (first.ptr && !first.len) first.len = p - first.ptr;
2500 if (!first.ptr) first.ptr = p;
2502 if (!has_meta && strchr(
"*?{}[]<>()~&|\\$;'`\"\n#", *p))
2508 else if (*p ==
'/') {
2516 if (!first.ptr) first.ptr = e;
2517 if (!first.len) first.len = p - first.ptr;
2518 if (first.len > 0 && first.len <=
sizeof(posix_sh_cmds[0]) &&
2520 bsearch(&first, posix_sh_cmds, numberof(posix_sh_cmds),
sizeof(posix_sh_cmds[0]), compare_posix_sh))
2525 eargp->use_shell = 0;
2527 if (!eargp->use_shell) {
2531 append_terminator(argv_buf);
2532 for (p = first.ptr + first.len; p < e;) {
2533 while (p < e && (*p ==
' ' || *p ==
'\t'))
2537 while (p < e && *p !=
' ' && *p !=
'\t')
2540 append_terminator(argv_buf);
2543 eargp->invoke.cmd.argv_buf = argv_buf;
2544 eargp->invoke.cmd.command_name =
2546 rb_enc_copy(eargp->invoke.cmd.command_name, prog);
2551 if (!eargp->use_shell) {
2552 const char *abspath;
2553 const char *path_env = 0;
2554 if (
RTEST(eargp->path_env)) path_env = RSTRING_PTR(eargp->path_env);
2555 abspath = dln_find_exe_r(RSTRING_PTR(eargp->invoke.cmd.command_name),
2556 path_env, fbuf,
sizeof(fbuf));
2560 eargp->invoke.cmd.command_abspath =
Qnil;
2563 if (!eargp->use_shell && !eargp->invoke.cmd.argv_buf) {
2568 for (i = 0; i < argc; i++) {
2569 VALUE arg = argv[i];
2570 const char *s = RSTRING_PTR(arg);
2572 append_terminator(argv_buf);
2574 eargp->invoke.cmd.argv_buf = argv_buf;
2577 if (!eargp->use_shell) {
2578 const char *p, *ep, *null=NULL;
2582 p = RSTRING_PTR(eargp->invoke.cmd.argv_buf);
2583 ep = p + RSTRING_LEN(eargp->invoke.cmd.argv_buf);
2589 eargp->invoke.cmd.argv_str =
2590 rb_imemo_tmpbuf_new_from_an_RString(argv_str);
2596rb_execarg_get(
VALUE execarg_obj)
2604rb_execarg_init(
int argc,
const VALUE *orig_argv,
int accept_shell,
VALUE execarg_obj)
2606 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2612 prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
2613 rb_exec_fillarg(prog, argc, argv, env, opthash, execarg_obj);
2615 ret = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2621rb_execarg_new(
int argc,
const VALUE *argv,
int accept_shell,
int allow_exc_opt)
2626 rb_execarg_init(argc, argv, accept_shell, execarg_obj);
2627 if (!allow_exc_opt && eargp->exception_given) {
2628 rb_raise(rb_eArgError,
"exception option is not allowed");
2634rb_execarg_setenv(
VALUE execarg_obj,
VALUE env)
2636 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2637 env = !
NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) :
Qfalse;
2638 eargp->env_modification = env;
2643fill_envp_buf_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
2652 append_terminator(envp_buf);
2672 const char *fname = RSTRING_PTR(data->fname);
2673 data->ret = parent_redirect_open(fname, data->oflags, data->perm);
2681 rb_alloc_tmp_buffer(&eargp->dup2_tmpbuf, run_exec_dup2_tmpbuf_size(
len),
false);
2685rb_execarg_parent_start1(
VALUE execarg_obj)
2687 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2688 int unsetenv_others;
2692 ary = eargp->fd_open;
2707 open_data.fname = vpath;
2708 open_data.oflags = flags;
2709 open_data.perm = perm;
2711 open_data.err = EINTR;
2713 if (open_data.ret == -1) {
2714 if (open_data.err == EINTR) {
2720 fd2 = open_data.ret;
2732 eargp->redirect_fds = check_exec_fds(eargp);
2734 ary = eargp->fd_dup2;
2736 rb_execarg_allocate_dup2_tmpbuf(eargp,
RARRAY_LEN(ary));
2739 unsetenv_others = eargp->unsetenv_others_given && eargp->unsetenv_others_do;
2740 envopts = eargp->env_modification;
2741 if (ALWAYS_NEED_ENVP || unsetenv_others || envopts !=
Qfalse) {
2742 VALUE envtbl, envp_str, envp_buf;
2744 if (unsetenv_others) {
2748 envtbl = rb_env_to_hash();
2758 rb_hash_delete(envtbl, key);
2761 rb_hash_aset(envtbl, key, EXPORT_STR(val));
2767 rb_hash_stlike_foreach(envtbl, fill_envp_buf_i, (st_data_t)envp_buf);
2770 p = RSTRING_PTR(envp_buf);
2771 ep = p + RSTRING_LEN(envp_buf);
2779 rb_imemo_tmpbuf_new_from_an_RString(envp_str);
2780 eargp->envp_buf = envp_buf;
2796rb_execarg_parent_start(
VALUE execarg_obj)
2799 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2801 rb_execarg_parent_end(execarg_obj);
2807execarg_parent_end(
VALUE execarg_obj)
2809 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
2813 ary = eargp->fd_open;
2824 parent_redirect_close(fd2);
2836rb_execarg_parent_end(
VALUE execarg_obj)
2838 execarg_parent_end(execarg_obj);
2842rb_exec_fail(
struct rb_execarg *eargp,
int err,
const char *errmsg)
2844 if (!errmsg || !*errmsg)
return;
2845 if (strcmp(errmsg,
"chdir") == 0) {
2846 rb_sys_fail_str(eargp->chdir_dir);
2848 rb_sys_fail(errmsg);
2853rb_execarg_fail(
VALUE execarg_obj,
int err,
const char *errmsg)
2855 if (!errmsg || !*errmsg)
return;
2856 rb_exec_fail(rb_execarg_get(execarg_obj), err, errmsg);
2864 VALUE execarg_obj, fail_str;
2866#define CHILD_ERRMSG_BUFLEN 80
2867 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
2870 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
2871 eargp = rb_execarg_get(execarg_obj);
2874 rb_protect(rb_execarg_parent_start1, execarg_obj, &state);
2876 execarg_parent_end(execarg_obj);
2881 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
2883 err = exec_async_signal_safe(eargp, errmsg,
sizeof(errmsg));
2886 rb_exec_fail(eargp, err, errmsg);
2990#define ERRMSG(str) \
2991 ((errmsg && 0 < errmsg_buflen) ? \
2992 (void)strlcpy(errmsg, (str), errmsg_buflen) : (void)0)
2994#define ERRMSG_FMT(...) \
2995 ((errmsg && 0 < errmsg_buflen) ? \
2996 (void)snprintf(errmsg, errmsg_buflen, __VA_ARGS__) : (void)0)
2998static int fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
2999static int fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3000static int fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen);
3003save_redirect_fd(
int fd,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3006 VALUE newary, redirection;
3007 int save_fd = redirect_cloexec_dup(fd), cloexec;
3008 if (save_fd == -1) {
3015 newary = sargp->fd_dup2;
3018 sargp->fd_dup2 = newary;
3020 cloexec = fd_get_cloexec(fd, errmsg, errmsg_buflen);
3025 newary = sargp->fd_close;
3028 sargp->fd_close = newary;
3037intcmp(
const void *a,
const void *b)
3039 return *(
int*)a - *(
int*)b;
3043intrcmp(
const void *a,
const void *b)
3045 return *(
int*)b - *(
int*)a;
3057run_exec_dup2_tmpbuf_size(
rb_len_t n)
3064fd_get_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3068 ret = fcntl(fd, F_GETFD);
3070 ERRMSG(
"fcntl(F_GETFD)");
3073 if (ret & FD_CLOEXEC)
return 1;
3080fd_set_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3084 ret = fcntl(fd, F_GETFD);
3086 ERRMSG(
"fcntl(F_GETFD)");
3089 if (!(ret & FD_CLOEXEC)) {
3091 ret = fcntl(fd, F_SETFD, ret);
3093 ERRMSG(
"fcntl(F_SETFD)");
3103fd_clear_cloexec(
int fd,
char *errmsg,
size_t errmsg_buflen)
3107 ret = fcntl(fd, F_GETFD);
3109 ERRMSG(
"fcntl(F_GETFD)");
3112 if (ret & FD_CLOEXEC) {
3114 ret = fcntl(fd, F_SETFD, ret);
3116 ERRMSG(
"fcntl(F_SETFD)");
3126run_exec_dup2(
VALUE ary,
VALUE tmpbuf,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3136 for (i = 0; i < n; i++) {
3141 pairs[i].older_index = -1;
3151 for (i = 0; i < n; i++) {
3152 int newfd = pairs[i].newfd;
3156 pairs[i].num_newer = 0;
3158 while (pairs < found && (found-1)->oldfd == newfd)
3160 while (found < pairs+n && found->oldfd == newfd) {
3161 pairs[i].num_newer++;
3162 found->older_index = i;
3169 for (i = 0; i < n; i++) {
3171 while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
3172 if (save_redirect_fd(pairs[j].newfd, sargp, errmsg, errmsg_buflen) < 0)
3174 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3179 if (pairs[j].cloexec &&
3180 fd_set_cloexec(pairs[j].newfd, errmsg, errmsg_buflen)) {
3184 pairs[j].oldfd = -1;
3185 j = pairs[j].older_index;
3187 pairs[j].num_newer--;
3192 for (i = 0; i < n; i++) {
3194 if (pairs[i].oldfd == -1)
3196 if (pairs[i].oldfd == pairs[i].newfd) {
3197 if (fd_clear_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen) == -1)
3199 pairs[i].oldfd = -1;
3202 if (extra_fd == -1) {
3203 extra_fd = redirect_dup(pairs[i].oldfd);
3204 if (extra_fd == -1) {
3210 if (fd_get_cloexec(pairs[i].oldfd, errmsg, errmsg_buflen)) {
3211 if (fd_set_cloexec(extra_fd, errmsg, errmsg_buflen)) {
3219 ret = redirect_dup2(pairs[i].oldfd, extra_fd);
3226 pairs[i].oldfd = extra_fd;
3227 j = pairs[i].older_index;
3228 pairs[i].older_index = -1;
3230 ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
3236 pairs[j].oldfd = -1;
3237 j = pairs[j].older_index;
3240 if (extra_fd != -1) {
3241 ret = redirect_close(extra_fd);
3256run_exec_close(
VALUE ary,
char *errmsg,
size_t errmsg_buflen)
3264 ret = redirect_close(fd);
3275run_exec_dup2_child(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3285 if (save_redirect_fd(newfd, sargp, errmsg, errmsg_buflen) < 0)
3287 ret = redirect_dup2(oldfd, newfd);
3300run_exec_pgroup(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3312 pgroup = eargp->pgroup_pgid;
3318 sargp->pgroup_given = 1;
3319 sargp->pgroup_pgid = getpgrp();
3325 ret = setpgid(getpid(), pgroup);
3326 if (ret == -1) ERRMSG(
"setpgid");
3331#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3334run_exec_rlimit(
VALUE ary,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3343 if (getrlimit(rtype, &rlim) == -1) {
3344 ERRMSG(
"getrlimit");
3348 RLIM2NUM(rlim.rlim_cur),
3349 RLIM2NUM(rlim.rlim_max)));
3350 if (sargp->rlimit_limits ==
Qfalse)
3351 newary = sargp->rlimit_limits = hide_obj(
rb_ary_new());
3353 newary = sargp->rlimit_limits;
3358 if (setrlimit(rtype, &rlim) == -1) {
3359 ERRMSG(
"setrlimit");
3367#if !defined(HAVE_WORKING_FORK)
3380 if (sargp->env_modification ==
Qfalse) {
3381 VALUE env = rb_envtbl();
3386 sargp->env_modification = ary;
3388 sargp->unsetenv_others_given = 1;
3389 sargp->unsetenv_others_do = 1;
3396#define chdir(p) rb_w32_uchdir(p)
3401rb_execarg_run_options(
const struct rb_execarg *eargp,
struct rb_execarg *sargp,
char *errmsg,
size_t errmsg_buflen)
3408 sargp->redirect_fds =
Qnil;
3412 if (eargp->pgroup_given) {
3413 if (run_exec_pgroup(eargp, sargp, errmsg, errmsg_buflen) == -1)
3418#if defined(HAVE_SETRLIMIT) && defined(RLIM2NUM)
3419 obj = eargp->rlimit_limits;
3421 if (run_exec_rlimit(obj, sargp, errmsg, errmsg_buflen) == -1)
3426#if !defined(HAVE_WORKING_FORK)
3427 if (eargp->unsetenv_others_given && eargp->unsetenv_others_do) {
3432 obj = eargp->env_modification;
3440 const char *name = RSTRING_PTR(key);
3441 ruby_setenv(name,
NIL_P(val) ? 0 : RSTRING_PTR(val));
3446 if (eargp->umask_given) {
3447 mode_t mask = eargp->umask_mask;
3448 mode_t oldmask = umask(mask);
3450 sargp->umask_given = 1;
3451 sargp->umask_mask = oldmask;
3455 if (sargp && (eargp->fd_dup2 !=
Qfalse || eargp->fd_dup2_child !=
Qfalse)) {
3459 rb_execarg_allocate_dup2_tmpbuf(sargp, n);
3462 obj = eargp->fd_dup2;
3464 if (run_exec_dup2(obj, eargp->dup2_tmpbuf, sargp, errmsg, errmsg_buflen) == -1)
3468 obj = eargp->fd_close;
3471 rb_warn(
"cannot close fd before spawn");
3473 if (run_exec_close(obj, errmsg, errmsg_buflen) == -1)
3478#ifdef HAVE_WORKING_FORK
3479 if (eargp->close_others_do) {
3484 obj = eargp->fd_dup2_child;
3486 if (run_exec_dup2_child(obj, sargp, errmsg, errmsg_buflen) == -1)
3490 if (eargp->chdir_given) {
3492 sargp->chdir_given = 1;
3493 sargp->chdir_dir = hide_obj(rb_dir_getwd_ospath());
3495 if (chdir(RSTRING_PTR(eargp->chdir_dir)) == -1) {
3502 if (eargp->gid_given) {
3503 if (setgid(eargp->gid) < 0) {
3510 if (eargp->uid_given) {
3511 if (setuid(eargp->uid) < 0) {
3519 int preserve =
errno;
3520 stdfd_clear_nonblock();
3526#if !defined(HAVE_WORKING_FORK)
3527# define execarg_restore(sargp) rb_execarg_run_options((sargp), NULL, errmsg, errmsg_buflen)
3529# define execarg_restore(sargp) (void)0
3534rb_exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3536 errno = exec_async_signal_safe(eargp, errmsg, errmsg_buflen);
3541exec_async_signal_safe(
const struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
3543#if !defined(HAVE_WORKING_FORK)
3544 struct rb_execarg sarg, *
const sargp = &sarg;
3550 if (rb_execarg_run_options(eargp, sargp, errmsg, errmsg_buflen) < 0) {
3552 execarg_restore(sargp);
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 execarg_restore(sargp);
3570#ifdef HAVE_WORKING_FORK
3573rb_exec_atfork(
void* arg,
char *errmsg,
size_t errmsg_buflen)
3575 return rb_exec_async_signal_safe(arg, errmsg, errmsg_buflen);
3579proc_syswait(
VALUE pid)
3586move_fds_to_avoid_crash(
int *fdp,
int n,
VALUE fds)
3590 for (i = 0; i < n; i++) {
3609pipe_nocrash(
int filedes[2],
VALUE fds)
3617 if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
3632rb_thread_sleep_that_takes_VALUE_as_sole_argument(
VALUE n)
3639handle_fork_error(
int err,
struct rb_process_status *status,
int *ep,
volatile int *try_gc_p)
3651#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3654 if (!status && !ep) {
3659 rb_protect(rb_thread_sleep_that_takes_VALUE_as_sole_argument,
INT2FIX(1), &state);
3660 if (status) status->status = state;
3661 if (!state)
return 0;
3670 if (state && !status) rb_jump_tag(state);
3676#define prefork() ( \
3677 rb_io_flush(rb_ractor_stdout()), \
3678 rb_io_flush(rb_ractor_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, status ? &child : NULL, NULL, &try_gc) == 0);
4107 if (status) *status = child.status;
4108 if (pid < 0)
errno = err;
4116 rb_pid_t pid = rb_fork_ruby(NULL);
4119 rb_sys_fail(
"fork(2)");
4126call_proc__fork_protected(
VALUE arg)
4135rb_call_proc__fork(
void)
4138 return proc_fork_pid();
4141 rb_pid_t parent = getpid(), pid;
4144 if (
NIL_P(rb_protect(call_proc__fork_protected, (
VALUE)&pid, &state))) {
4145 if (getpid() != parent) {
4155#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4176rb_proc__fork(
VALUE _obj)
4178 rb_pid_t pid = proc_fork_pid();
4247 pid = rb_call_proc__fork();
4261#define rb_proc__fork rb_f_notimplement
4262#define rb_f_fork rb_f_notimplement
4266exit_status_code(
VALUE status)
4272 istatus = EXIT_SUCCESS;
4275 istatus = EXIT_FAILURE;
4279#if EXIT_SUCCESS != 0
4281 istatus = EXIT_SUCCESS;
4288NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4306rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4311 istatus = exit_status_code(argv[0]);
4314 istatus = EXIT_FAILURE;
4324 if (GET_EC()->tag) {
4340 istatus = exit_status_code(argv[0]);
4343 istatus = EXIT_SUCCESS;
4408 rb_execution_context_t *ec = GET_EC();
4409 VALUE errinfo = rb_ec_get_errinfo(ec);
4410 if (!
NIL_P(errinfo)) {
4411 rb_ec_error_print(ec, errinfo);
4418 args[1] = args[0] = argv[0];
4421 args[0] =
INT2NUM(EXIT_FAILURE);
4459#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4464 if (eargp && !eargp->use_shell) {
4465 VALUE str = eargp->invoke.cmd.argv_str;
4466 VALUE buf = eargp->invoke.cmd.argv_buf;
4467 char *p, **argv = ARGVSTR2ARGV(str);
4468 long i, argc = ARGVSTR2ARGC(str);
4469 const char *start = RSTRING_PTR(buf);
4471 p = RSTRING_PTR(cmd);
4472 for (i = 1; i < argc; ++i) {
4473 p[argv[i] - start - 1] =
' ';
4478 return RSTRING_PTR(*prog);
4483rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4486#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4489# if !defined HAVE_SPAWNV
4494#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4495 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4497 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4499 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4500 execarg_restore(&sarg);
4504 if (prog && !eargp->use_shell) {
4505 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4506 argv[0] = RSTRING_PTR(prog);
4508# if defined HAVE_SPAWNV
4509 if (eargp->use_shell) {
4510 pid = proc_spawn_sh(RSTRING_PTR(prog));
4513 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4514 pid = proc_spawn_cmd(argv, prog, eargp);
4521 status = system(rb_execarg_commandline(eargp, &prog));
4526 if (eargp->waitpid_state) {
4527 eargp->waitpid_state->pid = pid;
4530 execarg_restore(&sarg);
4545do_spawn_process(
VALUE arg)
4549 rb_execarg_parent_start1(argp->execarg);
4551 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4552 argp->errmsg.ptr, argp->errmsg.buflen);
4555NOINLINE(
static rb_pid_t
4556 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4559rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4563 args.execarg = execarg_obj;
4564 args.errmsg.ptr = errmsg;
4565 args.errmsg.buflen = errmsg_buflen;
4568 execarg_parent_end, execarg_obj);
4573rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4577 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4578 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4584 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4590 return rb_spawn_internal(argc, argv, NULL, 0);
4711 rb_thread_t *th = GET_THREAD();
4712 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4713 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4716 eargp->status = &status;
4718 last_status_clear(th);
4722 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4729 th->last_status = status;
4731 if (data->status == EXIT_SUCCESS) {
4735 if (data->error != 0) {
4736 if (eargp->exception) {
4737 VALUE command = eargp->invoke.sh.shell_script;
4745 else if (eargp->exception) {
4746 VALUE command = eargp->invoke.sh.shell_script;
4760 if (eargp->exception) {
4761 VALUE command = eargp->invoke.sh.shell_script;
4877 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
4878 VALUE execarg_obj, fail_str;
4881 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4882 eargp = rb_execarg_get(execarg_obj);
4883 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4885 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
4889 rb_exec_fail(eargp, err, errmsg);
4893#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
4919 time_t beg = time(0);
4922 if (scheduler !=
Qnil) {
4926 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
4935 time_t end = time(0) - beg;
4937 return TIMET2NUM(end);
4941#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
4958#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
4960 if (pgrp < 0) rb_sys_fail(0);
4964 if (pgrp < 0) rb_sys_fail(0);
4969#define proc_getpgrp rb_f_notimplement
4973#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
4991 if (setpgid(0,0) < 0) rb_sys_fail(0);
4992#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
4993 if (setpgrp() < 0) rb_sys_fail(0);
4998#define proc_setpgrp rb_f_notimplement
5002#if defined(HAVE_GETPGID)
5020 if (i < 0) rb_sys_fail(0);
5024#define proc_getpgid rb_f_notimplement
5042 rb_pid_t ipid, ipgrp;
5047 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5051#define proc_setpgid rb_f_notimplement
5079 if (sid < 0) rb_sys_fail(0);
5083#define proc_getsid rb_f_notimplement
5087#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5088#if !defined(HAVE_SETSID)
5089static rb_pid_t ruby_setsid(
void);
5090#define setsid() ruby_setsid()
5111 if (pid < 0) rb_sys_fail(0);
5115#if !defined(HAVE_SETSID)
5116#define HAVE_SETSID 1
5124#if defined(SETPGRP_VOID)
5130 ret = setpgrp(0, pid);
5132 if (ret == -1)
return -1;
5136 ioctl(fd, TIOCNOTTY, NULL);
5143#define proc_setsid rb_f_notimplement
5147#ifdef HAVE_GETPRIORITY
5175 int prio, iwhich, iwho;
5181 prio = getpriority(iwhich, iwho);
5182 if (
errno) rb_sys_fail(0);
5186#define proc_getpriority rb_f_notimplement
5190#ifdef HAVE_GETPRIORITY
5210 int iwhich, iwho, iprio;
5216 if (setpriority(iwhich, iwho, iprio) < 0)
5221#define proc_setpriority rb_f_notimplement
5224#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5226rlimit_resource_name2int(
const char *name,
rb_len_t len,
int casetype)
5230#define RESCHECK(r) \
5232 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5233 resource = RLIMIT_##r; \
5267#ifdef RLIMIT_MEMLOCK
5270#ifdef RLIMIT_MSGQUEUE
5309#ifdef RLIMIT_SIGPENDING
5310 RESCHECK(SIGPENDING);
5319 for (p = name; *p; p++)
5325 for (p = name; *p; p++)
5331 rb_bug(
"unexpected casetype");
5338rlimit_type_by_hname(
const char *name,
rb_len_t len)
5340 return rlimit_resource_name2int(name,
len, 0);
5344rlimit_type_by_lname(
const char *name,
rb_len_t len)
5346 return rlimit_resource_name2int(name,
len, 1);
5350rlimit_type_by_sym(
VALUE key)
5353 const char *rname = RSTRING_PTR(name);
5356 static const char prefix[] =
"rlimit_";
5357 enum {prefix_len =
sizeof(prefix)-1};
5359 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5360 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5368rlimit_resource_type(
VALUE rtype)
5375 switch (
TYPE(rtype)) {
5378 name = RSTRING_PTR(v);
5379 len = RSTRING_LEN(v);
5388 len = RSTRING_LEN(rtype);
5398 r = rlimit_type_by_hname(name,
len);
5402 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5408rlimit_resource_value(
VALUE rval)
5413 switch (
TYPE(rval)) {
5416 name = RSTRING_PTR(v);
5431 return NUM2RLIM(rval);
5435 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5437#ifdef RLIM_SAVED_MAX
5438 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5440#ifdef RLIM_SAVED_CUR
5441 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5443 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5449#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5477 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5478 rb_sys_fail(
"getrlimit");
5480 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5483#define proc_getrlimit rb_f_notimplement
5486#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5540proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5542 VALUE resource, rlim_cur, rlim_max;
5548 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5549 rlim_max = rlim_cur;
5551 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5552 rlim.rlim_max = rlimit_resource_value(rlim_max);
5554 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5555 rb_sys_fail(
"setrlimit");
5560#define proc_setrlimit rb_f_notimplement
5563static int under_uid_switch = 0;
5565check_uid_switch(
void)
5567 if (under_uid_switch) {
5568 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5572static int under_gid_switch = 0;
5574check_gid_switch(
void)
5576 if (under_gid_switch) {
5577 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5582#if defined(HAVE_PWD_H)
5584login_not_found(
int err)
5586 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5597# if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5600 char MAYBE_UNUSED(*login) = NULL;
5602# ifdef USE_GETLOGIN_R
5604# if defined(__FreeBSD__)
5605 typedef int getlogin_r_size_t;
5607 typedef size_t getlogin_r_size_t;
5610 rb_len_t loginsize = GETLOGIN_R_SIZE_INIT;
5613 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5617 login = RSTRING_PTR(maybe_result);
5622 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5623 if (login_not_found(gle)) {
5624 rb_str_resize(maybe_result, 0);
5628 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5629 rb_str_resize(maybe_result, 0);
5634 login = RSTRING_PTR(maybe_result);
5638 if (login == NULL) {
5639 rb_str_resize(maybe_result, 0);
5644 return maybe_result;
5646# elif defined(USE_GETLOGIN)
5652 if (login_not_found(err)) {
5666pwd_not_found(
int err)
5680# if defined(USE_GETPWNAM_R)
5681struct getpwnam_r_args {
5685 struct passwd *result;
5686 struct passwd pwstore;
5689# define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5690 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5693nogvl_getpwnam_r(
void *args)
5695 struct getpwnam_r_args *arg = args;
5696 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5701rb_getpwdirnam_for_login(
VALUE login_name)
5703#if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5707 if (
NIL_P(login_name)) {
5712 const char *login = RSTRING_PTR(login_name);
5715# ifdef USE_GETPWNAM_R
5718 rb_len_t bufsizenm = GETPW_R_SIZE_INIT;
5721 bufsizenm = GETPW_R_SIZE_DEFAULT;
5725 bufnm = RSTRING_PTR(getpwnm_tmp);
5728 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5731 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5732 if (pwd_not_found(enm)) {
5733 rb_str_resize(getpwnm_tmp, 0);
5737 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5738 rb_str_resize(getpwnm_tmp, 0);
5743 args.buf = RSTRING_PTR(getpwnm_tmp);
5747 if (args.result == NULL) {
5749 rb_str_resize(getpwnm_tmp, 0);
5755 rb_str_resize(getpwnm_tmp, 0);
5758# elif defined(USE_GETPWNAM)
5760 struct passwd *pwptr;
5762 if (!(pwptr = getpwnam(login))) {
5765 if (pwd_not_found(err)) {
5779# if defined(USE_GETPWUID_R)
5780struct getpwuid_r_args {
5784 struct passwd *result;
5785 struct passwd pwstore;
5788# define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5789 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5792nogvl_getpwuid_r(
void *args)
5794 struct getpwuid_r_args *arg = args;
5795 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5805# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5809 uid_t ruid = getuid();
5811# ifdef USE_GETPWUID_R
5814 rb_len_t bufsizeid = GETPW_R_SIZE_INIT;
5817 bufsizeid = GETPW_R_SIZE_DEFAULT;
5821 bufid = RSTRING_PTR(getpwid_tmp);
5824 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5827 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5828 if (pwd_not_found(eid)) {
5829 rb_str_resize(getpwid_tmp, 0);
5833 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5834 rb_str_resize(getpwid_tmp, 0);
5839 args.buf = RSTRING_PTR(getpwid_tmp);
5843 if (args.result == NULL) {
5845 rb_str_resize(getpwid_tmp, 0);
5851 rb_str_resize(getpwid_tmp, 0);
5854# elif defined(USE_GETPWUID)
5856 struct passwd *pwptr;
5858 if (!(pwptr = getpwuid(ruid))) {
5861 if (pwd_not_found(err)) {
5887#if defined(HAVE_PWD_H)
5890# ifdef USE_GETPWNAM_R
5903 struct passwd *pwptr;
5904#ifdef USE_GETPWNAM_R
5909 getpw_buf_len = GETPW_R_SIZE_INIT;
5910 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5913 getpw_buf = RSTRING_PTR(*getpw_tmp);
5917 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
5919 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5920 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5921 rb_str_resize(*getpw_tmp, 0);
5925 args.buf = RSTRING_PTR(*getpw_tmp);
5928 pwptr = args.result;
5930 pwptr = getpwnam(usrname);
5933#ifndef USE_GETPWNAM_R
5936 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
5938 uid = pwptr->pw_uid;
5939#ifndef USE_GETPWNAM_R
5946# ifdef p_uid_from_name
5966#if defined(HAVE_GRP_H)
5967# if defined(USE_GETGRNAM_R)
5968struct getgrnam_r_args {
5972 struct group *result;
5976# define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
5977 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5980nogvl_getgrnam_r(
void *args)
5982 struct getgrnam_r_args *arg = args;
5983 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
5989# ifdef USE_GETGRNAM_R
6002 struct group *grptr;
6003#ifdef USE_GETGRNAM_R
6008 getgr_buf_len = GETGR_R_SIZE_INIT;
6009 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6012 getgr_buf = RSTRING_PTR(*getgr_tmp);
6016 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6018 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6019 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6020 rb_str_resize(*getgr_tmp, 0);
6024 args.buf = RSTRING_PTR(*getgr_tmp);
6027 grptr = args.result;
6028#elif defined(HAVE_GETGRNAM)
6029 grptr = getgrnam(grpname);
6034#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6037 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6039 gid = grptr->gr_gid;
6040#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6047# ifdef p_gid_from_name
6067#if defined HAVE_SETUID
6081 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6085#define p_sys_setuid rb_f_notimplement
6089#if defined HAVE_SETRUID
6103 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6107#define p_sys_setruid rb_f_notimplement
6111#if defined HAVE_SETEUID
6125 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6129#define p_sys_seteuid rb_f_notimplement
6133#if defined HAVE_SETREUID
6148 rb_uid_t ruid, euid;
6151 ruid = OBJ2UID1(rid);
6152 euid = OBJ2UID1(eid);
6154 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6158#define p_sys_setreuid rb_f_notimplement
6162#if defined HAVE_SETRESUID
6177 rb_uid_t ruid, euid, suid;
6180 ruid = OBJ2UID1(rid);
6181 euid = OBJ2UID1(eid);
6182 suid = OBJ2UID1(sid);
6184 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6188#define p_sys_setresuid rb_f_notimplement
6205proc_getuid(
VALUE obj)
6207 rb_uid_t uid = getuid();
6212#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6232#if defined(HAVE_SETRESUID)
6233 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6234#elif defined HAVE_SETREUID
6235 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6236#elif defined HAVE_SETRUID
6237 if (setruid(uid) < 0) rb_sys_fail(0);
6238#elif defined HAVE_SETUID
6240 if (geteuid() == uid) {
6241 if (setuid(uid) < 0) rb_sys_fail(0);
6251#define proc_setuid rb_f_notimplement
6265static rb_uid_t SAVED_USER_ID = -1;
6289 if (geteuid() == 0) {
6290#if defined(HAVE_SETRESUID)
6291 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6292 SAVED_USER_ID = uid;
6293#elif defined(HAVE_SETUID)
6294 if (setuid(uid) < 0) rb_sys_fail(0);
6295 SAVED_USER_ID = uid;
6296#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6297 if (getuid() == uid) {
6298 if (SAVED_USER_ID == uid) {
6299 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6303 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6304 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6306 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6307 SAVED_USER_ID = uid;
6310 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6312 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6313 SAVED_USER_ID = uid;
6318 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6319 SAVED_USER_ID = uid;
6321#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6322 if (getuid() == uid) {
6323 if (SAVED_USER_ID == uid) {
6324 if (seteuid(uid) < 0) rb_sys_fail(0);
6328 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6330 if (setruid(0) < 0) rb_sys_fail(0);
6333 if (setruid(0) < 0) rb_sys_fail(0);
6335 if (seteuid(uid) < 0) rb_sys_fail(0);
6336 if (setruid(uid) < 0) rb_sys_fail(0);
6337 SAVED_USER_ID = uid;
6342 if (seteuid(uid) < 0) rb_sys_fail(0);
6343 if (setruid(uid) < 0) rb_sys_fail(0);
6344 SAVED_USER_ID = uid;
6352#if defined(HAVE_SETRESUID)
6353 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6354 (geteuid() == uid)? (rb_uid_t)-1: uid,
6355 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6356 SAVED_USER_ID = uid;
6357#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6358 if (SAVED_USER_ID == uid) {
6359 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6360 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6363 else if (getuid() != uid) {
6364 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6366 SAVED_USER_ID = uid;
6368 else if ( geteuid() != uid) {
6369 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6370 SAVED_USER_ID = uid;
6371 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6374 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6375 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6376 SAVED_USER_ID = uid;
6377 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6379#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6380 if (SAVED_USER_ID == uid) {
6381 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6382 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6384 else if ( geteuid() == uid) {
6385 if (getuid() != uid) {
6386 if (setruid(uid) < 0) rb_sys_fail(0);
6387 SAVED_USER_ID = uid;
6390 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6391 SAVED_USER_ID = uid;
6392 if (setruid(uid) < 0) rb_sys_fail(0);
6395 else if ( getuid() == uid) {
6396 if (seteuid(uid) < 0) rb_sys_fail(0);
6397 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6398 SAVED_USER_ID = uid;
6399 if (setruid(uid) < 0) rb_sys_fail(0);
6404#elif defined HAVE_44BSD_SETUID
6405 if (getuid() == uid) {
6407 if (setuid(uid) < 0) rb_sys_fail(0);
6408 SAVED_USER_ID = uid;
6413#elif defined HAVE_SETEUID
6414 if (getuid() == uid && SAVED_USER_ID == uid) {
6415 if (seteuid(uid) < 0) rb_sys_fail(0);
6420#elif defined HAVE_SETUID
6421 if (getuid() == uid && SAVED_USER_ID == uid) {
6422 if (setuid(uid) < 0) rb_sys_fail(0);
6436#if defined HAVE_SETGID
6450 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6454#define p_sys_setgid rb_f_notimplement
6458#if defined HAVE_SETRGID
6472 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6476#define p_sys_setrgid rb_f_notimplement
6480#if defined HAVE_SETEGID
6494 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6498#define p_sys_setegid rb_f_notimplement
6502#if defined HAVE_SETREGID
6517 rb_gid_t rgid, egid;
6519 rgid = OBJ2GID(rid);
6520 egid = OBJ2GID(eid);
6521 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6525#define p_sys_setregid rb_f_notimplement
6528#if defined HAVE_SETRESGID
6543 rb_gid_t rgid, egid, sgid;
6545 rgid = OBJ2GID(rid);
6546 egid = OBJ2GID(eid);
6547 sgid = OBJ2GID(sid);
6548 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6552#define p_sys_setresgid rb_f_notimplement
6556#if defined HAVE_ISSETUGID
6570p_sys_issetugid(
VALUE obj)
6572 return RBOOL(issetugid());
6575#define p_sys_issetugid rb_f_notimplement
6592proc_getgid(
VALUE obj)
6594 rb_gid_t gid = getgid();
6599#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6618#if defined(HAVE_SETRESGID)
6619 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6620#elif defined HAVE_SETREGID
6621 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6622#elif defined HAVE_SETRGID
6623 if (setrgid(gid) < 0) rb_sys_fail(0);
6624#elif defined HAVE_SETGID
6626 if (getegid() == gid) {
6627 if (setgid(gid) < 0) rb_sys_fail(0);
6637#define proc_setgid rb_f_notimplement
6641#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6661static int _maxgroups = -1;
6663get_sc_ngroups_max(
void)
6665#ifdef _SC_NGROUPS_MAX
6666 return (
int)sysconf(_SC_NGROUPS_MAX);
6667#elif defined(NGROUPS_MAX)
6668 return (
int)NGROUPS_MAX;
6676 if (_maxgroups < 0) {
6677 _maxgroups = get_sc_ngroups_max();
6679 _maxgroups = RB_MAX_GROUPS;
6688#ifdef HAVE_GETGROUPS
6712proc_getgroups(
VALUE obj)
6718 ngroups = getgroups(0, NULL);
6722 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6724 ngroups = getgroups(ngroups, groups);
6729 for (i = 0; i < ngroups; i++)
6737#define proc_getgroups rb_f_notimplement
6741#ifdef HAVE_SETGROUPS
6766 if (ngroups > maxgroups())
6767 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6769 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6771 for (i = 0; i < ngroups; i++) {
6774 groups[i] = OBJ2GID1(g);
6778 if (setgroups(ngroups, groups) == -1)
6783 return proc_getgroups(obj);
6786#define proc_setgroups rb_f_notimplement
6790#ifdef HAVE_INITGROUPS
6816 return proc_getgroups(obj);
6819#define proc_initgroups rb_f_notimplement
6822#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6835proc_getmaxgroups(
VALUE obj)
6840#define proc_getmaxgroups rb_f_notimplement
6843#ifdef HAVE_SETGROUPS
6856 int ngroups_max = get_sc_ngroups_max();
6859 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6861 if (ngroups > RB_MAX_GROUPS)
6862 ngroups = RB_MAX_GROUPS;
6864 if (ngroups_max > 0 && ngroups > ngroups_max)
6865 ngroups = ngroups_max;
6867 _maxgroups = ngroups;
6872#define proc_setmaxgroups rb_f_notimplement
6875#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6876static int rb_daemon(
int nochdir,
int noclose);
6901 int n, nochdir = FALSE, noclose = FALSE;
6904 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6905 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6909 n = rb_daemon(nochdir, noclose);
6910 if (n < 0) rb_sys_fail(
"daemon");
6914extern const char ruby_null_device[];
6917rb_daemon(
int nochdir,
int noclose)
6921 rb_pid_t pid = getpid();
6923 rb_thread_acquire_fork_lock();
6924 err = daemon(nochdir, noclose);
6926 release_fork_lock_after_fork(getpid() == pid ? pid : 0);
6931 switch (rb_fork_ruby(NULL)) {
6934 default: _exit(EXIT_SUCCESS);
6938 if (setsid() < 0) (void)0;
6943 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
6955#define proc_daemon rb_f_notimplement
6968static rb_gid_t SAVED_GROUP_ID = -1;
6992 if (geteuid() == 0) {
6993#if defined(HAVE_SETRESGID)
6994 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
6995 SAVED_GROUP_ID = gid;
6996#elif defined HAVE_SETGID
6997 if (setgid(gid) < 0) rb_sys_fail(0);
6998 SAVED_GROUP_ID = gid;
6999#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7000 if (getgid() == gid) {
7001 if (SAVED_GROUP_ID == gid) {
7002 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7006 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7007 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
7009 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7010 SAVED_GROUP_ID = gid;
7013 if (setregid(0, 0) < 0) rb_sys_fail(0);
7015 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7016 SAVED_GROUP_ID = gid;
7021 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7022 SAVED_GROUP_ID = gid;
7024#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7025 if (getgid() == gid) {
7026 if (SAVED_GROUP_ID == gid) {
7027 if (setegid(gid) < 0) rb_sys_fail(0);
7031 if (setegid(gid) < 0) rb_sys_fail(0);
7032 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7034 if (setrgid(0) < 0) rb_sys_fail(0);
7037 if (setrgid(0) < 0) rb_sys_fail(0);
7039 if (setegid(gid) < 0) rb_sys_fail(0);
7040 if (setrgid(gid) < 0) rb_sys_fail(0);
7041 SAVED_GROUP_ID = gid;
7046 if (setegid(gid) < 0) rb_sys_fail(0);
7047 if (setrgid(gid) < 0) rb_sys_fail(0);
7048 SAVED_GROUP_ID = gid;
7055#if defined(HAVE_SETRESGID)
7056 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7057 (getegid() == gid)? (rb_gid_t)-1: gid,
7058 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7059 SAVED_GROUP_ID = gid;
7060#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7061 if (SAVED_GROUP_ID == gid) {
7062 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7063 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7066 else if (getgid() != gid) {
7067 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7069 SAVED_GROUP_ID = gid;
7071 else if ( getegid() != gid) {
7072 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7073 SAVED_GROUP_ID = gid;
7074 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7077 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7078 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7079 SAVED_GROUP_ID = gid;
7080 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7082#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7083 if (SAVED_GROUP_ID == gid) {
7084 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7085 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7087 else if ( getegid() == gid) {
7088 if (getgid() != gid) {
7089 if (setrgid(gid) < 0) rb_sys_fail(0);
7090 SAVED_GROUP_ID = gid;
7093 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7094 SAVED_GROUP_ID = gid;
7095 if (setrgid(gid) < 0) rb_sys_fail(0);
7098 else if ( getgid() == gid) {
7099 if (setegid(gid) < 0) rb_sys_fail(0);
7100 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7101 SAVED_GROUP_ID = gid;
7102 if (setrgid(gid) < 0) rb_sys_fail(0);
7107#elif defined HAVE_44BSD_SETGID
7108 if (getgid() == gid) {
7110 if (setgid(gid) < 0) rb_sys_fail(0);
7111 SAVED_GROUP_ID = gid;
7116#elif defined HAVE_SETEGID
7117 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7118 if (setegid(gid) < 0) rb_sys_fail(0);
7123#elif defined HAVE_SETGID
7124 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7125 if (setgid(gid) < 0) rb_sys_fail(0);
7152proc_geteuid(
VALUE obj)
7154 rb_uid_t euid = geteuid();
7158#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7160proc_seteuid(rb_uid_t uid)
7162#if defined(HAVE_SETRESUID)
7163 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7164#elif defined HAVE_SETREUID
7165 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7166#elif defined HAVE_SETEUID
7167 if (seteuid(uid) < 0) rb_sys_fail(0);
7168#elif defined HAVE_SETUID
7169 if (uid == getuid()) {
7170 if (setuid(uid) < 0) rb_sys_fail(0);
7181#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7195 proc_seteuid(OBJ2UID(euid));
7199#define proc_seteuid_m rb_f_notimplement
7203rb_seteuid_core(rb_uid_t euid)
7205#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7211#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7215#if defined(HAVE_SETRESUID)
7217 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7218 SAVED_USER_ID = euid;
7221 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7223#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7224 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7226 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7227 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7228 SAVED_USER_ID = euid;
7230#elif defined HAVE_SETEUID
7231 if (seteuid(euid) < 0) rb_sys_fail(0);
7232#elif defined HAVE_SETUID
7233 if (geteuid() == 0) rb_sys_fail(0);
7234 if (setuid(euid) < 0) rb_sys_fail(0);
7259 rb_seteuid_core(OBJ2UID(
id));
7278proc_getegid(
VALUE obj)
7280 rb_gid_t egid = getegid();
7285#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7298#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7304#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7305 gid = OBJ2GID(egid);
7308#if defined(HAVE_SETRESGID)
7309 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7310#elif defined HAVE_SETREGID
7311 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7312#elif defined HAVE_SETEGID
7313 if (setegid(gid) < 0) rb_sys_fail(0);
7314#elif defined HAVE_SETGID
7315 if (gid == getgid()) {
7316 if (setgid(gid) < 0) rb_sys_fail(0);
7328#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7329#define proc_setegid_m proc_setegid
7331#define proc_setegid_m rb_f_notimplement
7335rb_setegid_core(rb_gid_t egid)
7337#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7343#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7347#if defined(HAVE_SETRESGID)
7349 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7350 SAVED_GROUP_ID = egid;
7353 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7355#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7356 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7358 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7359 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7360 SAVED_GROUP_ID = egid;
7362#elif defined HAVE_SETEGID
7363 if (setegid(egid) < 0) rb_sys_fail(0);
7364#elif defined HAVE_SETGID
7365 if (geteuid() == 0 ) rb_sys_fail(0);
7366 if (setgid(egid) < 0) rb_sys_fail(0);
7391 rb_setegid_core(OBJ2GID(
id));
7406p_uid_exchangeable(
VALUE _)
7408#if defined(HAVE_SETRESUID)
7410#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7431p_uid_exchange(
VALUE obj)
7434#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7441#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7445#if defined(HAVE_SETRESUID)
7446 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7447 SAVED_USER_ID = uid;
7448#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7449 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7450 SAVED_USER_ID = uid;
7452 (void)SAVED_USER_ID;
7469p_gid_exchangeable(
VALUE _)
7471#if defined(HAVE_SETRESGID)
7473#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7494p_gid_exchange(
VALUE obj)
7497#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7504#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7508#if defined(HAVE_SETRESGID)
7509 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7510 SAVED_GROUP_ID = gid;
7511#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7512 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7513 SAVED_GROUP_ID = gid;
7515 (void)SAVED_GROUP_ID;
7533p_uid_have_saved_id(
VALUE _)
7535#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7543#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7545p_uid_sw_ensure(
VALUE i)
7547 rb_uid_t
id = (rb_uid_t)i;
7548 under_uid_switch = 0;
7549 id = rb_seteuid_core(
id);
7568p_uid_switch(
VALUE obj)
7580 under_uid_switch = 1;
7587 else if (euid != SAVED_USER_ID) {
7588 proc_seteuid(SAVED_USER_ID);
7590 under_uid_switch = 1;
7605p_uid_sw_ensure(
VALUE obj)
7607 under_uid_switch = 0;
7608 return p_uid_exchange(obj);
7612p_uid_switch(
VALUE obj)
7624 p_uid_exchange(obj);
7626 under_uid_switch = 1;
7648p_gid_have_saved_id(
VALUE _)
7650#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7657#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7659p_gid_sw_ensure(
VALUE i)
7661 rb_gid_t
id = (rb_gid_t)i;
7662 under_gid_switch = 0;
7663 id = rb_setegid_core(
id);
7682p_gid_switch(
VALUE obj)
7694 under_gid_switch = 1;
7701 else if (egid != SAVED_GROUP_ID) {
7702 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7704 under_gid_switch = 1;
7719p_gid_sw_ensure(
VALUE obj)
7721 under_gid_switch = 0;
7722 return p_gid_exchange(obj);
7726p_gid_switch(
VALUE obj)
7738 p_gid_exchange(obj);
7740 under_gid_switch = 1;
7750#if defined(HAVE_TIMES)
7754#ifdef HAVE__SC_CLK_TCK
7755 return sysconf(_SC_CLK_TCK);
7756#elif defined CLK_TCK
7779rb_proc_times(
VALUE obj)
7781 VALUE utime, stime, cutime, cstime, ret;
7782#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7783 struct rusage usage_s, usage_c;
7785 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7786 rb_sys_fail(
"getrusage");
7787 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7788 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7789 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7790 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7792 const double hertz = (double)get_clk_tck();
7796 utime =
DBL2NUM(buf.tms_utime / hertz);
7797 stime =
DBL2NUM(buf.tms_stime / hertz);
7798 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7799 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7801 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7809#define rb_proc_times rb_f_notimplement
7812#ifdef HAVE_LONG_LONG
7814#define TIMETICK_INT_MIN LLONG_MIN
7815#define TIMETICK_INT_MAX LLONG_MAX
7816#define TIMETICK_INT2NUM(v) LL2NUM(v)
7817#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7819typedef long timetick_int_t;
7820#define TIMETICK_INT_MIN LONG_MIN
7821#define TIMETICK_INT_MAX LONG_MAX
7822#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7823#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7826CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7827static timetick_int_t
7828gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7848reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7850 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7858reduce_factors(timetick_int_t *numerators,
int num_numerators,
7859 timetick_int_t *denominators,
int num_denominators)
7862 for (i = 0; i < num_numerators; i++) {
7863 if (numerators[i] == 1)
7865 for (j = 0; j < num_denominators; j++) {
7866 if (denominators[j] == 1)
7868 reduce_fraction(&numerators[i], &denominators[j]);
7874 timetick_int_t giga_count;
7879timetick2dblnum(
struct timetick *ttp,
7880 timetick_int_t *numerators,
int num_numerators,
7881 timetick_int_t *denominators,
int num_denominators)
7886 reduce_factors(numerators, num_numerators,
7887 denominators, num_denominators);
7889 d = ttp->giga_count * 1e9 + ttp->count;
7891 for (i = 0; i < num_numerators; i++)
7893 for (i = 0; i < num_denominators; i++)
7894 d /= denominators[i];
7900timetick2dblnum_reciprocal(
struct timetick *ttp,
7901 timetick_int_t *numerators,
int num_numerators,
7902 timetick_int_t *denominators,
int num_denominators)
7907 reduce_factors(numerators, num_numerators,
7908 denominators, num_denominators);
7911 for (i = 0; i < num_denominators; i++)
7912 d *= denominators[i];
7913 for (i = 0; i < num_numerators; i++)
7915 d /= ttp->giga_count * 1e9 + ttp->count;
7920#define NDIV(x,y) (-(-((x)+1)/(y))-1)
7921#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
7924timetick2integer(
struct timetick *ttp,
7925 timetick_int_t *numerators,
int num_numerators,
7926 timetick_int_t *denominators,
int num_denominators)
7931 reduce_factors(numerators, num_numerators,
7932 denominators, num_denominators);
7934 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
7935 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
7936 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
7937 for (i = 0; i < num_numerators; i++) {
7938 timetick_int_t factor = numerators[i];
7939 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
7943 for (i = 0; i < num_denominators; i++) {
7944 t = DIV(t, denominators[i]);
7946 return TIMETICK_INT2NUM(t);
7950 v = TIMETICK_INT2NUM(ttp->giga_count);
7953 for (i = 0; i < num_numerators; i++) {
7954 timetick_int_t factor = numerators[i];
7957 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
7959 for (i = 0; i < num_denominators; i++) {
7960 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
7966make_clock_result(
struct timetick *ttp,
7967 timetick_int_t *numerators,
int num_numerators,
7968 timetick_int_t *denominators,
int num_denominators,
7971 if (unit ==
ID2SYM(id_nanosecond)) {
7972 numerators[num_numerators++] = 1000000000;
7973 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7975 else if (unit ==
ID2SYM(id_microsecond)) {
7976 numerators[num_numerators++] = 1000000;
7977 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7979 else if (unit ==
ID2SYM(id_millisecond)) {
7980 numerators[num_numerators++] = 1000;
7981 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7983 else if (unit ==
ID2SYM(id_second)) {
7984 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7986 else if (unit ==
ID2SYM(id_float_microsecond)) {
7987 numerators[num_numerators++] = 1000000;
7988 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7990 else if (unit ==
ID2SYM(id_float_millisecond)) {
7991 numerators[num_numerators++] = 1000;
7992 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7994 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
7995 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7998 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
8002static const mach_timebase_info_data_t *
8003get_mach_timebase_info(
void)
8005 static mach_timebase_info_data_t sTimebaseInfo;
8007 if ( sTimebaseInfo.denom == 0 ) {
8008 (void) mach_timebase_info(&sTimebaseInfo);
8011 return &sTimebaseInfo;
8015ruby_real_ms_time(
void)
8017 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8018 uint64_t t = mach_absolute_time();
8019 return (
double)t * info->numer / info->denom / 1e6;
8023#if defined(NUM2CLOCKID)
8024# define NUMERIC_CLOCKID 1
8026# define NUMERIC_CLOCKID 0
8027# define NUM2CLOCKID(x) 0
8030#define clock_failed(name, err, arg) do { \
8031 int clock_error = (err); \
8032 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8212 timetick_int_t numerators[2];
8213 timetick_int_t denominators[2];
8214 int num_numerators = 0;
8215 int num_denominators = 0;
8218 VALUE clk_id = argv[0];
8219#ifdef HAVE_CLOCK_GETTIME
8224#ifdef CLOCK_REALTIME
8225 if (clk_id == RUBY_CLOCK_REALTIME) {
8231#ifdef CLOCK_MONOTONIC
8232 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8233 c = CLOCK_MONOTONIC;
8238#ifdef CLOCK_PROCESS_CPUTIME_ID
8239 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8240 c = CLOCK_PROCESS_CPUTIME_ID;
8245#ifdef CLOCK_THREAD_CPUTIME_ID
8246 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8247 c = CLOCK_THREAD_CPUTIME_ID;
8255#ifdef HAVE_GETTIMEOFDAY
8260#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8261 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8263 ret = gettimeofday(&tv, 0);
8265 rb_sys_fail(
"gettimeofday");
8266 tt.giga_count = tv.tv_sec;
8267 tt.count = (int32_t)tv.tv_usec * 1000;
8268 denominators[num_denominators++] = 1000000000;
8273#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8274 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8277 if (t == (time_t)-1)
8278 rb_sys_fail(
"time");
8281 denominators[num_denominators++] = 1000000000;
8286#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8287 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8288 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8291 unsigned_clock_t uc;
8293 if (c == (clock_t)-1)
8294 rb_sys_fail(
"times");
8295 uc = (unsigned_clock_t)c;
8296 tt.count = (int32_t)(uc % 1000000000);
8297 tt.giga_count = (uc / 1000000000);
8298 denominators[num_denominators++] = get_clk_tck();
8304#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8305 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8306 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8307 struct rusage usage;
8309 ret = getrusage(RUSAGE_SELF, &usage);
8311 rb_sys_fail(
"getrusage");
8312 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8313 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8314 if (1000000 <= usec) {
8318 tt.count = usec * 1000;
8319 denominators[num_denominators++] = 1000000000;
8325#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8326 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8327 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8329 unsigned_clock_t utime, stime;
8330 if (times(&buf) == (clock_t)-1)
8331 rb_sys_fail(
"times");
8332 utime = (unsigned_clock_t)buf.tms_utime;
8333 stime = (unsigned_clock_t)buf.tms_stime;
8334 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8335 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8336 if (1000000000 <= tt.count) {
8337 tt.count -= 1000000000;
8340 denominators[num_denominators++] = get_clk_tck();
8345#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8346 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8347 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8349 unsigned_clock_t uc;
8352 if (c == (clock_t)-1)
8353 rb_sys_fail(
"clock");
8354 uc = (unsigned_clock_t)c;
8355 tt.count = (int32_t)(uc % 1000000000);
8356 tt.giga_count = uc / 1000000000;
8357 denominators[num_denominators++] = CLOCKS_PER_SEC;
8362 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8363 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8364 uint64_t t = mach_absolute_time();
8365 tt.count = (int32_t)(t % 1000000000);
8366 tt.giga_count = t / 1000000000;
8367 numerators[num_numerators++] = info->numer;
8368 denominators[num_denominators++] = info->denom;
8369 denominators[num_denominators++] = 1000000000;
8374 else if (NUMERIC_CLOCKID) {
8375#if defined(HAVE_CLOCK_GETTIME)
8377 c = NUM2CLOCKID(clk_id);
8379 ret = clock_gettime(c, &ts);
8381 clock_failed(
"gettime",
errno, clk_id);
8382 tt.count = (int32_t)ts.tv_nsec;
8383 tt.giga_count = ts.tv_sec;
8384 denominators[num_denominators++] = 1000000000;
8391 clock_failed(
"gettime", EINVAL, clk_id);
8394 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8439 timetick_int_t numerators[2];
8440 timetick_int_t denominators[2];
8441 int num_numerators = 0;
8442 int num_denominators = 0;
8443#ifdef HAVE_CLOCK_GETRES
8448 VALUE clk_id = argv[0];
8451#ifdef CLOCK_REALTIME
8452 if (clk_id == RUBY_CLOCK_REALTIME) {
8458#ifdef CLOCK_MONOTONIC
8459 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8460 c = CLOCK_MONOTONIC;
8465#ifdef CLOCK_PROCESS_CPUTIME_ID
8466 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8467 c = CLOCK_PROCESS_CPUTIME_ID;
8472#ifdef CLOCK_THREAD_CPUTIME_ID
8473 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8474 c = CLOCK_THREAD_CPUTIME_ID;
8479#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8480 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8483 denominators[num_denominators++] = 1000000000;
8488#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8489 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8492 denominators[num_denominators++] = 1000000000;
8497#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8498 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8501 denominators[num_denominators++] = get_clk_tck();
8506#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8507 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8510 denominators[num_denominators++] = 1000000000;
8515#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8516 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8519 denominators[num_denominators++] = get_clk_tck();
8524#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8525 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8528 denominators[num_denominators++] = CLOCKS_PER_SEC;
8533#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8534 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8535 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8538 numerators[num_numerators++] = info->numer;
8539 denominators[num_denominators++] = info->denom;
8540 denominators[num_denominators++] = 1000000000;
8545 else if (NUMERIC_CLOCKID) {
8546#if defined(HAVE_CLOCK_GETRES)
8548 c = NUM2CLOCKID(clk_id);
8550 ret = clock_getres(c, &ts);
8552 clock_failed(
"getres",
errno, clk_id);
8553 tt.count = (int32_t)ts.tv_nsec;
8554 tt.giga_count = ts.tv_sec;
8555 denominators[num_denominators++] = 1000000000;
8562 clock_failed(
"getres", EINVAL, clk_id);
8565 if (unit ==
ID2SYM(id_hertz)) {
8566 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8569 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8574get_CHILD_STATUS(
ID _x,
VALUE *_y)
8580get_PROCESS_ID(
ID _x,
VALUE *_y)
8672static VALUE rb_mProcUID;
8673static VALUE rb_mProcGID;
8674static VALUE rb_mProcID_Syscall;
8710 rb_eager_load_detailed_message_extension();
8713 rb_gc_prepare_heap();
9141 rb_gvar_ractor_local(
"$$");
9142 rb_gvar_ractor_local(
"$?");
9143 rb_gvar_box_ready(
"$$");
9144 rb_gvar_box_ready(
"$?");
9199 process_status_dump, process_status_load);
9235#ifdef HAVE_GETPRIORITY
9246#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9248 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9249#ifdef RLIM_SAVED_MAX
9251 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9253 rb_define_const(
rb_mProcess,
"RLIM_SAVED_MAX", v);
9257 rb_define_const(
rb_mProcess,
"RLIM_INFINITY", inf);
9258#ifdef RLIM_SAVED_CUR
9260 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9262 rb_define_const(
rb_mProcess,
"RLIM_SAVED_CUR", v);
9301#ifdef RLIMIT_MEMLOCK
9308#ifdef RLIMIT_MSGQUEUE
9374#ifdef RLIMIT_SIGPENDING
9409#if defined(RUBY_CLOCK_REALTIME)
9410#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9411# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9412#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9413# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9415#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9417 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
9418#elif defined(RUBY_CLOCK_REALTIME)
9419 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
9422#if defined(RUBY_CLOCK_MONOTONIC)
9423#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9424# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9426#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9428 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
9429#elif defined(RUBY_CLOCK_MONOTONIC)
9430 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", RUBY_CLOCK_MONOTONIC);
9433#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9434#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9435# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9437#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9439 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
9440#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9441 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", RUBY_CLOCK_PROCESS_CPUTIME_ID);
9444#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9446 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
9447#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9448 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", RUBY_CLOCK_THREAD_CPUTIME_ID);
9454 rb_define_const(
rb_mProcess,
"CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
9458 rb_define_const(
rb_mProcess,
"CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
9460#ifdef CLOCK_REALTIME_FAST
9462 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
9464#ifdef CLOCK_REALTIME_PRECISE
9466 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
9468#ifdef CLOCK_REALTIME_COARSE
9470 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_COARSE", CLOCKID2NUM(CLOCK_REALTIME_COARSE));
9472#ifdef CLOCK_REALTIME_ALARM
9474 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_ALARM", CLOCKID2NUM(CLOCK_REALTIME_ALARM));
9476#ifdef CLOCK_MONOTONIC_FAST
9478 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
9480#ifdef CLOCK_MONOTONIC_PRECISE
9482 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
9484#ifdef CLOCK_MONOTONIC_RAW
9486 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
9488#ifdef CLOCK_MONOTONIC_RAW_APPROX
9490 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW_APPROX", CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX));
9492#ifdef CLOCK_MONOTONIC_COARSE
9494 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_COARSE", CLOCKID2NUM(CLOCK_MONOTONIC_COARSE));
9496#ifdef CLOCK_BOOTTIME
9498 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME", CLOCKID2NUM(CLOCK_BOOTTIME));
9500#ifdef CLOCK_BOOTTIME_ALARM
9502 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME_ALARM", CLOCKID2NUM(CLOCK_BOOTTIME_ALARM));
9506 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
9508#ifdef CLOCK_UPTIME_FAST
9510 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
9512#ifdef CLOCK_UPTIME_PRECISE
9514 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
9516#ifdef CLOCK_UPTIME_RAW
9518 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW", CLOCKID2NUM(CLOCK_UPTIME_RAW));
9520#ifdef CLOCK_UPTIME_RAW_APPROX
9522 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW_APPROX", CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX));
9526 rb_define_const(
rb_mProcess,
"CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
9530 rb_define_const(
rb_mProcess,
"CLOCK_TAI", CLOCKID2NUM(CLOCK_TAI));
9536#if defined(HAVE_TIMES)
9550 SAVED_USER_ID = geteuid();
9551 SAVED_GROUP_ID = getegid();
9553 rb_mProcUID = rb_define_module_under(
rb_mProcess,
"UID");
9554 rb_mProcGID = rb_define_module_under(
rb_mProcess,
"GID");
9574#ifdef p_uid_from_name
9577#ifdef p_gid_from_name
9581 rb_mProcID_Syscall = rb_define_module_under(
rb_mProcess,
"Sys");
9608#define define_id(name) id_##name = rb_intern_const(#name)
9621 define_id(new_pgroup);
9623 define_id(unsetenv_others);
9626 define_id(close_others);
9627 define_id(nanosecond);
9628 define_id(microsecond);
9629 define_id(millisecond);
9631 define_id(float_microsecond);
9632 define_id(float_millisecond);
9633 define_id(float_second);
9634 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9635 define_id(TIME_BASED_CLOCK_REALTIME);
9636#ifdef CLOCK_REALTIME
9637 define_id(CLOCK_REALTIME);
9639#ifdef CLOCK_MONOTONIC
9640 define_id(CLOCK_MONOTONIC);
9642#ifdef CLOCK_PROCESS_CPUTIME_ID
9643 define_id(CLOCK_PROCESS_CPUTIME_ID);
9645#ifdef CLOCK_THREAD_CPUTIME_ID
9646 define_id(CLOCK_THREAD_CPUTIME_ID);
9649 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9650 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9653 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9655 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9657 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
9660#ifdef HAVE_WORKING_FORK
#define ISUPPER
@old{rb_isupper}
#define TOUPPER
@old{rb_toupper}
#define ISLOWER
@old{rb_islower}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define GIDT2NUM
Converts a C's gid_t into an instance of rb_cInteger.
#define NUM2GIDT
Converts an instance of rb_cNumeric into C's gid_t.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines public accessor method(s) for an attribute.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define T_FILE
Old name of RUBY_T_FILE.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define NUM2UINT
Old name of RB_NUM2UINT.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
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.
#define rb_notimplement()
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_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_str_export_to_enc(VALUE obj, rb_encoding *enc)
Identical to rb_str_export(), except it additionally takes an encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
int rb_during_gc(void)
Queries if the GC is busy.
void rb_gc(void)
Triggers a GC process.
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.
VALUE rb_env_clear(void)
Destructively removes every environment variables of the running process.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
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.
VALUE rb_ractor_stderr(void)
Queries the standard error of the current Ractor that is calling this function.
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#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)
References an object in an array.
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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define errno
Ractor-aware version of errno.
#define InitVM(ext)
This macro is for internal use.
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.
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.