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;
4115 rb_pid_t pid = rb_fork_ruby(NULL);
4118 rb_sys_fail(
"fork(2)");
4125call_proc__fork_protected(
VALUE arg)
4134rb_call_proc__fork(
void)
4137 return proc_fork_pid();
4140 rb_pid_t parent = getpid(), pid;
4143 if (
NIL_P(rb_protect(call_proc__fork_protected, (
VALUE)&pid, &state))) {
4144 if (getpid() != parent) {
4154#if defined(HAVE_WORKING_FORK) && !defined(CANNOT_FORK_WITH_PTHREAD)
4175rb_proc__fork(
VALUE _obj)
4177 rb_pid_t pid = proc_fork_pid();
4246 pid = rb_call_proc__fork();
4260#define rb_proc__fork rb_f_notimplement
4261#define rb_f_fork rb_f_notimplement
4265exit_status_code(
VALUE status)
4271 istatus = EXIT_SUCCESS;
4274 istatus = EXIT_FAILURE;
4278#if EXIT_SUCCESS != 0
4280 istatus = EXIT_SUCCESS;
4287NORETURN(
static VALUE rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj));
4305rb_f_exit_bang(
int argc,
VALUE *argv,
VALUE obj)
4310 istatus = exit_status_code(argv[0]);
4313 istatus = EXIT_FAILURE;
4323 if (GET_EC()->tag) {
4339 istatus = exit_status_code(argv[0]);
4342 istatus = EXIT_SUCCESS;
4407 rb_execution_context_t *ec = GET_EC();
4408 VALUE errinfo = rb_ec_get_errinfo(ec);
4409 if (!
NIL_P(errinfo)) {
4410 rb_ec_error_print(ec, errinfo);
4417 args[1] = args[0] = argv[0];
4420 args[0] =
INT2NUM(EXIT_FAILURE);
4458#if !defined HAVE_WORKING_FORK && !defined HAVE_SPAWNV && !defined __EMSCRIPTEN__
4463 if (eargp && !eargp->use_shell) {
4464 VALUE str = eargp->invoke.cmd.argv_str;
4465 VALUE buf = eargp->invoke.cmd.argv_buf;
4466 char *p, **argv = ARGVSTR2ARGV(str);
4467 long i, argc = ARGVSTR2ARGC(str);
4468 const char *start = RSTRING_PTR(buf);
4470 p = RSTRING_PTR(cmd);
4471 for (i = 1; i < argc; ++i) {
4472 p[argv[i] - start - 1] =
' ';
4477 return RSTRING_PTR(*prog);
4482rb_spawn_process(
struct rb_execarg *eargp,
char *errmsg,
size_t errmsg_buflen)
4485#if !defined HAVE_WORKING_FORK || USE_SPAWNV
4488# if !defined HAVE_SPAWNV
4493#if defined HAVE_WORKING_FORK && !USE_SPAWNV
4494 pid = fork_check_err(eargp->status, rb_exec_atfork, eargp, eargp->redirect_fds, errmsg, errmsg_buflen, eargp);
4496 prog = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4498 if (rb_execarg_run_options(eargp, &sarg, errmsg, errmsg_buflen) < 0) {
4499 execarg_restore(&sarg);
4503 if (prog && !eargp->use_shell) {
4504 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4505 argv[0] = RSTRING_PTR(prog);
4507# if defined HAVE_SPAWNV
4508 if (eargp->use_shell) {
4509 pid = proc_spawn_sh(RSTRING_PTR(prog));
4512 char **argv = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
4513 pid = proc_spawn_cmd(argv, prog, eargp);
4520 status = system(rb_execarg_commandline(eargp, &prog));
4525 if (eargp->waitpid_state) {
4526 eargp->waitpid_state->pid = pid;
4529 execarg_restore(&sarg);
4544do_spawn_process(
VALUE arg)
4548 rb_execarg_parent_start1(argp->execarg);
4550 return (
VALUE)rb_spawn_process(rb_execarg_get(argp->execarg),
4551 argp->errmsg.ptr, argp->errmsg.buflen);
4554NOINLINE(
static rb_pid_t
4555 rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen));
4558rb_execarg_spawn(
VALUE execarg_obj,
char *errmsg,
size_t errmsg_buflen)
4562 args.execarg = execarg_obj;
4563 args.errmsg.ptr = errmsg;
4564 args.errmsg.buflen = errmsg_buflen;
4567 execarg_parent_end, execarg_obj);
4572rb_spawn_internal(
int argc,
const VALUE *argv,
char *errmsg,
size_t errmsg_buflen)
4576 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4577 return rb_execarg_spawn(execarg_obj, errmsg, errmsg_buflen);
4583 return rb_spawn_internal(argc, argv, errmsg, errmsg_buflen);
4589 return rb_spawn_internal(argc, argv, NULL, 0);
4710 rb_thread_t *th = GET_THREAD();
4711 VALUE execarg_obj = rb_execarg_new(argc, argv, TRUE, TRUE);
4712 struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
4715 eargp->status = &status;
4717 last_status_clear(th);
4721 rb_pid_t pid = rb_execarg_spawn(execarg_obj, 0, 0);
4728 th->last_status = status;
4730 if (data->status == EXIT_SUCCESS) {
4734 if (data->error != 0) {
4735 if (eargp->exception) {
4736 VALUE command = eargp->invoke.sh.shell_script;
4744 else if (eargp->exception) {
4745 VALUE command = eargp->invoke.sh.shell_script;
4759 if (eargp->exception) {
4760 VALUE command = eargp->invoke.sh.shell_script;
4876 char errmsg[CHILD_ERRMSG_BUFLEN] = {
'\0' };
4877 VALUE execarg_obj, fail_str;
4880 execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
4881 eargp = rb_execarg_get(execarg_obj);
4882 fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
4884 pid = rb_execarg_spawn(execarg_obj, errmsg,
sizeof(errmsg));
4888 rb_exec_fail(eargp, err, errmsg);
4892#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
4918 time_t beg = time(0);
4921 if (scheduler !=
Qnil) {
4925 if (argc == 0 || (argc == 1 &&
NIL_P(argv[0]))) {
4934 time_t end = time(0) - beg;
4936 return TIMET2NUM(end);
4940#if (defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)) || defined(HAVE_GETPGID)
4957#if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID)
4959 if (pgrp < 0) rb_sys_fail(0);
4963 if (pgrp < 0) rb_sys_fail(0);
4968#define proc_getpgrp rb_f_notimplement
4972#if defined(HAVE_SETPGID) || (defined(HAVE_SETPGRP) && defined(SETPGRP_VOID))
4990 if (setpgid(0,0) < 0) rb_sys_fail(0);
4991#elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID)
4992 if (setpgrp() < 0) rb_sys_fail(0);
4997#define proc_setpgrp rb_f_notimplement
5001#if defined(HAVE_GETPGID)
5019 if (i < 0) rb_sys_fail(0);
5023#define proc_getpgid rb_f_notimplement
5041 rb_pid_t ipid, ipgrp;
5046 if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0);
5050#define proc_setpgid rb_f_notimplement
5078 if (sid < 0) rb_sys_fail(0);
5082#define proc_getsid rb_f_notimplement
5086#if defined(HAVE_SETSID) || (defined(HAVE_SETPGRP) && defined(TIOCNOTTY))
5087#if !defined(HAVE_SETSID)
5088static rb_pid_t ruby_setsid(
void);
5089#define setsid() ruby_setsid()
5110 if (pid < 0) rb_sys_fail(0);
5114#if !defined(HAVE_SETSID)
5115#define HAVE_SETSID 1
5123#if defined(SETPGRP_VOID)
5129 ret = setpgrp(0, pid);
5131 if (ret == -1)
return -1;
5135 ioctl(fd, TIOCNOTTY, NULL);
5142#define proc_setsid rb_f_notimplement
5146#ifdef HAVE_GETPRIORITY
5174 int prio, iwhich, iwho;
5180 prio = getpriority(iwhich, iwho);
5181 if (
errno) rb_sys_fail(0);
5185#define proc_getpriority rb_f_notimplement
5189#ifdef HAVE_GETPRIORITY
5209 int iwhich, iwho, iprio;
5215 if (setpriority(iwhich, iwho, iprio) < 0)
5220#define proc_setpriority rb_f_notimplement
5223#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5225rlimit_resource_name2int(
const char *name,
rb_len_t len,
int casetype)
5229#define RESCHECK(r) \
5231 if (len == rb_strlen_lit(#r) && STRCASECMP(name, #r) == 0) { \
5232 resource = RLIMIT_##r; \
5266#ifdef RLIMIT_MEMLOCK
5269#ifdef RLIMIT_MSGQUEUE
5308#ifdef RLIMIT_SIGPENDING
5309 RESCHECK(SIGPENDING);
5318 for (p = name; *p; p++)
5324 for (p = name; *p; p++)
5330 rb_bug(
"unexpected casetype");
5337rlimit_type_by_hname(
const char *name,
rb_len_t len)
5339 return rlimit_resource_name2int(name,
len, 0);
5343rlimit_type_by_lname(
const char *name,
rb_len_t len)
5345 return rlimit_resource_name2int(name,
len, 1);
5349rlimit_type_by_sym(
VALUE key)
5352 const char *rname = RSTRING_PTR(name);
5355 static const char prefix[] =
"rlimit_";
5356 enum {prefix_len =
sizeof(prefix)-1};
5358 if (
len > prefix_len && strncmp(prefix, rname, prefix_len) == 0) {
5359 rtype = rlimit_type_by_lname(rname + prefix_len,
len - prefix_len);
5367rlimit_resource_type(
VALUE rtype)
5374 switch (
TYPE(rtype)) {
5377 name = RSTRING_PTR(v);
5378 len = RSTRING_LEN(v);
5387 len = RSTRING_LEN(rtype);
5397 r = rlimit_type_by_hname(name,
len);
5401 rb_raise(rb_eArgError,
"invalid resource name: % "PRIsVALUE, rtype);
5407rlimit_resource_value(
VALUE rval)
5412 switch (
TYPE(rval)) {
5415 name = RSTRING_PTR(v);
5430 return NUM2RLIM(rval);
5434 if (strcmp(name,
"INFINITY") == 0)
return RLIM_INFINITY;
5436#ifdef RLIM_SAVED_MAX
5437 if (strcmp(name,
"SAVED_MAX") == 0)
return RLIM_SAVED_MAX;
5439#ifdef RLIM_SAVED_CUR
5440 if (strcmp(name,
"SAVED_CUR") == 0)
return RLIM_SAVED_CUR;
5442 rb_raise(rb_eArgError,
"invalid resource value: %"PRIsVALUE, rval);
5448#if defined(HAVE_GETRLIMIT) && defined(RLIM2NUM)
5476 if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5477 rb_sys_fail(
"getrlimit");
5479 return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
5482#define proc_getrlimit rb_f_notimplement
5485#if defined(HAVE_SETRLIMIT) && defined(NUM2RLIM)
5539proc_setrlimit(
int argc,
VALUE *argv,
VALUE obj)
5541 VALUE resource, rlim_cur, rlim_max;
5547 if (argc < 3 ||
NIL_P(rlim_max = argv[2]))
5548 rlim_max = rlim_cur;
5550 rlim.rlim_cur = rlimit_resource_value(rlim_cur);
5551 rlim.rlim_max = rlimit_resource_value(rlim_max);
5553 if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) {
5554 rb_sys_fail(
"setrlimit");
5559#define proc_setrlimit rb_f_notimplement
5562static int under_uid_switch = 0;
5564check_uid_switch(
void)
5566 if (under_uid_switch) {
5567 rb_raise(
rb_eRuntimeError,
"can't handle UID while evaluating block given to Process::UID.switch method");
5571static int under_gid_switch = 0;
5573check_gid_switch(
void)
5575 if (under_gid_switch) {
5576 rb_raise(
rb_eRuntimeError,
"can't handle GID while evaluating block given to Process::UID.switch method");
5581#if defined(HAVE_PWD_H)
5583login_not_found(
int err)
5585 return (err == ENOTTY || err == ENXIO || err == ENOENT);
5596# if !defined(USE_GETLOGIN_R) && !defined(USE_GETLOGIN)
5599 char MAYBE_UNUSED(*login) = NULL;
5601# ifdef USE_GETLOGIN_R
5603# if defined(__FreeBSD__)
5604 typedef int getlogin_r_size_t;
5606 typedef size_t getlogin_r_size_t;
5609 rb_len_t loginsize = GETLOGIN_R_SIZE_INIT;
5612 loginsize = GETLOGIN_R_SIZE_DEFAULT;
5616 login = RSTRING_PTR(maybe_result);
5621 while ((gle = getlogin_r(login, (getlogin_r_size_t)loginsize)) != 0) {
5622 if (login_not_found(gle)) {
5623 rb_str_resize(maybe_result, 0);
5627 if (gle != ERANGE || loginsize >= GETLOGIN_R_SIZE_LIMIT) {
5628 rb_str_resize(maybe_result, 0);
5633 login = RSTRING_PTR(maybe_result);
5637 if (login == NULL) {
5638 rb_str_resize(maybe_result, 0);
5643 return maybe_result;
5645# elif defined(USE_GETLOGIN)
5651 if (login_not_found(err)) {
5665pwd_not_found(
int err)
5679# if defined(USE_GETPWNAM_R)
5680struct getpwnam_r_args {
5684 struct passwd *result;
5685 struct passwd pwstore;
5688# define GETPWNAM_R_ARGS(login_, buf_, bufsize_) (struct getpwnam_r_args) \
5689 {.login = login_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5692nogvl_getpwnam_r(
void *args)
5694 struct getpwnam_r_args *arg = args;
5695 return (
void *)(
VALUE)getpwnam_r(arg->login, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5700rb_getpwdirnam_for_login(
VALUE login_name)
5702#if !defined(USE_GETPWNAM_R) && !defined(USE_GETPWNAM)
5706 if (
NIL_P(login_name)) {
5711 const char *login = RSTRING_PTR(login_name);
5714# ifdef USE_GETPWNAM_R
5717 rb_len_t bufsizenm = GETPW_R_SIZE_INIT;
5720 bufsizenm = GETPW_R_SIZE_DEFAULT;
5724 bufnm = RSTRING_PTR(getpwnm_tmp);
5727 struct getpwnam_r_args args = GETPWNAM_R_ARGS(login, bufnm, (
size_t)bufsizenm);
5730 while ((enm = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5731 if (pwd_not_found(enm)) {
5732 rb_str_resize(getpwnm_tmp, 0);
5736 if (enm != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5737 rb_str_resize(getpwnm_tmp, 0);
5742 args.buf = RSTRING_PTR(getpwnm_tmp);
5746 if (args.result == NULL) {
5748 rb_str_resize(getpwnm_tmp, 0);
5754 rb_str_resize(getpwnm_tmp, 0);
5757# elif defined(USE_GETPWNAM)
5759 struct passwd *pwptr;
5761 if (!(pwptr = getpwnam(login))) {
5764 if (pwd_not_found(err)) {
5778# if defined(USE_GETPWUID_R)
5779struct getpwuid_r_args {
5783 struct passwd *result;
5784 struct passwd pwstore;
5787# define GETPWUID_R_ARGS(uid_, buf_, bufsize_) (struct getpwuid_r_args) \
5788 {.uid = uid_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5791nogvl_getpwuid_r(
void *args)
5793 struct getpwuid_r_args *arg = args;
5794 return (
void *)(
VALUE)getpwuid_r(arg->uid, &arg->pwstore, arg->buf, arg->bufsize, &arg->result);
5804# if !defined(USE_GETPWUID_R) && !defined(USE_GETPWUID)
5808 uid_t ruid = getuid();
5810# ifdef USE_GETPWUID_R
5813 rb_len_t bufsizeid = GETPW_R_SIZE_INIT;
5816 bufsizeid = GETPW_R_SIZE_DEFAULT;
5820 bufid = RSTRING_PTR(getpwid_tmp);
5823 struct getpwuid_r_args args = GETPWUID_R_ARGS(ruid, bufid, (
size_t)bufsizeid);
5826 while ((eid = IO_WITHOUT_GVL_INT(nogvl_getpwuid_r, &args)) != 0) {
5827 if (pwd_not_found(eid)) {
5828 rb_str_resize(getpwid_tmp, 0);
5832 if (eid != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5833 rb_str_resize(getpwid_tmp, 0);
5838 args.buf = RSTRING_PTR(getpwid_tmp);
5842 if (args.result == NULL) {
5844 rb_str_resize(getpwid_tmp, 0);
5850 rb_str_resize(getpwid_tmp, 0);
5853# elif defined(USE_GETPWUID)
5855 struct passwd *pwptr;
5857 if (!(pwptr = getpwuid(ruid))) {
5860 if (pwd_not_found(err)) {
5886#if defined(HAVE_PWD_H)
5889# ifdef USE_GETPWNAM_R
5902 struct passwd *pwptr;
5903#ifdef USE_GETPWNAM_R
5908 getpw_buf_len = GETPW_R_SIZE_INIT;
5909 if (getpw_buf_len < 0) getpw_buf_len = GETPW_R_SIZE_DEFAULT;
5912 getpw_buf = RSTRING_PTR(*getpw_tmp);
5916 struct getpwnam_r_args args = GETPWNAM_R_ARGS((
char *)usrname, getpw_buf, (
size_t)getpw_buf_len);
5918 while ((e = IO_WITHOUT_GVL_INT(nogvl_getpwnam_r, &args)) != 0) {
5919 if (e != ERANGE || args.bufsize >= GETPW_R_SIZE_LIMIT) {
5920 rb_str_resize(*getpw_tmp, 0);
5924 args.buf = RSTRING_PTR(*getpw_tmp);
5927 pwptr = args.result;
5929 pwptr = getpwnam(usrname);
5932#ifndef USE_GETPWNAM_R
5935 rb_raise(rb_eArgError,
"can't find user for %"PRIsVALUE,
id);
5937 uid = pwptr->pw_uid;
5938#ifndef USE_GETPWNAM_R
5945# ifdef p_uid_from_name
5965#if defined(HAVE_GRP_H)
5966# if defined(USE_GETGRNAM_R)
5967struct getgrnam_r_args {
5971 struct group *result;
5975# define GETGRNAM_R_ARGS(name_, buf_, bufsize_) (struct getgrnam_r_args) \
5976 {.name = name_, .buf = buf_, .bufsize = bufsize_, .result = NULL}
5979nogvl_getgrnam_r(
void *args)
5981 struct getgrnam_r_args *arg = args;
5982 return (
void *)(
VALUE)getgrnam_r(arg->name, &arg->grp, arg->buf, arg->bufsize, &arg->result);
5988# ifdef USE_GETGRNAM_R
6001 struct group *grptr;
6002#ifdef USE_GETGRNAM_R
6007 getgr_buf_len = GETGR_R_SIZE_INIT;
6008 if (getgr_buf_len < 0) getgr_buf_len = GETGR_R_SIZE_DEFAULT;
6011 getgr_buf = RSTRING_PTR(*getgr_tmp);
6015 struct getgrnam_r_args args = GETGRNAM_R_ARGS(grpname, getgr_buf, (
size_t)getgr_buf_len);
6017 while ((e = IO_WITHOUT_GVL_INT(nogvl_getgrnam_r, &args)) != 0) {
6018 if (e != ERANGE || args.bufsize >= GETGR_R_SIZE_LIMIT) {
6019 rb_str_resize(*getgr_tmp, 0);
6023 args.buf = RSTRING_PTR(*getgr_tmp);
6026 grptr = args.result;
6027#elif defined(HAVE_GETGRNAM)
6028 grptr = getgrnam(grpname);
6033#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6036 rb_raise(rb_eArgError,
"can't find group for %"PRIsVALUE,
id);
6038 gid = grptr->gr_gid;
6039#if !defined(USE_GETGRNAM_R) && defined(HAVE_ENDGRENT)
6046# ifdef p_gid_from_name
6066#if defined HAVE_SETUID
6080 if (setuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6084#define p_sys_setuid rb_f_notimplement
6088#if defined HAVE_SETRUID
6102 if (setruid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6106#define p_sys_setruid rb_f_notimplement
6110#if defined HAVE_SETEUID
6124 if (seteuid(OBJ2UID(
id)) != 0) rb_sys_fail(0);
6128#define p_sys_seteuid rb_f_notimplement
6132#if defined HAVE_SETREUID
6147 rb_uid_t ruid, euid;
6150 ruid = OBJ2UID1(rid);
6151 euid = OBJ2UID1(eid);
6153 if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
6157#define p_sys_setreuid rb_f_notimplement
6161#if defined HAVE_SETRESUID
6176 rb_uid_t ruid, euid, suid;
6179 ruid = OBJ2UID1(rid);
6180 euid = OBJ2UID1(eid);
6181 suid = OBJ2UID1(sid);
6183 if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
6187#define p_sys_setresuid rb_f_notimplement
6204proc_getuid(
VALUE obj)
6206 rb_uid_t uid = getuid();
6211#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETRUID) || defined(HAVE_SETUID)
6231#if defined(HAVE_SETRESUID)
6232 if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0);
6233#elif defined HAVE_SETREUID
6234 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6235#elif defined HAVE_SETRUID
6236 if (setruid(uid) < 0) rb_sys_fail(0);
6237#elif defined HAVE_SETUID
6239 if (geteuid() == uid) {
6240 if (setuid(uid) < 0) rb_sys_fail(0);
6250#define proc_setuid rb_f_notimplement
6264static rb_uid_t SAVED_USER_ID = -1;
6288 if (geteuid() == 0) {
6289#if defined(HAVE_SETRESUID)
6290 if (setresuid(uid, uid, uid) < 0) rb_sys_fail(0);
6291 SAVED_USER_ID = uid;
6292#elif defined(HAVE_SETUID)
6293 if (setuid(uid) < 0) rb_sys_fail(0);
6294 SAVED_USER_ID = uid;
6295#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6296 if (getuid() == uid) {
6297 if (SAVED_USER_ID == uid) {
6298 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
6302 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6303 if (setreuid(SAVED_USER_ID, 0) < 0) rb_sys_fail(0);
6305 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6306 SAVED_USER_ID = uid;
6309 if (setreuid(0, -1) < 0) rb_sys_fail(0);
6311 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6312 SAVED_USER_ID = uid;
6317 if (setreuid(uid, uid) < 0) rb_sys_fail(0);
6318 SAVED_USER_ID = uid;
6320#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6321 if (getuid() == uid) {
6322 if (SAVED_USER_ID == uid) {
6323 if (seteuid(uid) < 0) rb_sys_fail(0);
6327 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6329 if (setruid(0) < 0) rb_sys_fail(0);
6332 if (setruid(0) < 0) rb_sys_fail(0);
6334 if (seteuid(uid) < 0) rb_sys_fail(0);
6335 if (setruid(uid) < 0) rb_sys_fail(0);
6336 SAVED_USER_ID = uid;
6341 if (seteuid(uid) < 0) rb_sys_fail(0);
6342 if (setruid(uid) < 0) rb_sys_fail(0);
6343 SAVED_USER_ID = uid;
6351#if defined(HAVE_SETRESUID)
6352 if (setresuid((getuid() == uid)? (rb_uid_t)-1: uid,
6353 (geteuid() == uid)? (rb_uid_t)-1: uid,
6354 (SAVED_USER_ID == uid)? (rb_uid_t)-1: uid) < 0) rb_sys_fail(0);
6355 SAVED_USER_ID = uid;
6356#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
6357 if (SAVED_USER_ID == uid) {
6358 if (setreuid((getuid() == uid)? (rb_uid_t)-1: uid,
6359 (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6362 else if (getuid() != uid) {
6363 if (setreuid(uid, (geteuid() == uid)? (rb_uid_t)-1: uid) < 0)
6365 SAVED_USER_ID = uid;
6367 else if ( geteuid() != uid) {
6368 if (setreuid(geteuid(), uid) < 0) rb_sys_fail(0);
6369 SAVED_USER_ID = uid;
6370 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6373 if (setreuid(-1, SAVED_USER_ID) < 0) rb_sys_fail(0);
6374 if (setreuid(SAVED_USER_ID, uid) < 0) rb_sys_fail(0);
6375 SAVED_USER_ID = uid;
6376 if (setreuid(uid, -1) < 0) rb_sys_fail(0);
6378#elif defined(HAVE_SETRUID) && defined(HAVE_SETEUID)
6379 if (SAVED_USER_ID == uid) {
6380 if (geteuid() != uid && seteuid(uid) < 0) rb_sys_fail(0);
6381 if (getuid() != uid && setruid(uid) < 0) rb_sys_fail(0);
6383 else if ( geteuid() == uid) {
6384 if (getuid() != uid) {
6385 if (setruid(uid) < 0) rb_sys_fail(0);
6386 SAVED_USER_ID = uid;
6389 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6390 SAVED_USER_ID = uid;
6391 if (setruid(uid) < 0) rb_sys_fail(0);
6394 else if ( getuid() == uid) {
6395 if (seteuid(uid) < 0) rb_sys_fail(0);
6396 if (setruid(SAVED_USER_ID) < 0) rb_sys_fail(0);
6397 SAVED_USER_ID = uid;
6398 if (setruid(uid) < 0) rb_sys_fail(0);
6403#elif defined HAVE_44BSD_SETUID
6404 if (getuid() == uid) {
6406 if (setuid(uid) < 0) rb_sys_fail(0);
6407 SAVED_USER_ID = uid;
6412#elif defined HAVE_SETEUID
6413 if (getuid() == uid && SAVED_USER_ID == uid) {
6414 if (seteuid(uid) < 0) rb_sys_fail(0);
6419#elif defined HAVE_SETUID
6420 if (getuid() == uid && SAVED_USER_ID == uid) {
6421 if (setuid(uid) < 0) rb_sys_fail(0);
6435#if defined HAVE_SETGID
6449 if (setgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6453#define p_sys_setgid rb_f_notimplement
6457#if defined HAVE_SETRGID
6471 if (setrgid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6475#define p_sys_setrgid rb_f_notimplement
6479#if defined HAVE_SETEGID
6493 if (setegid(OBJ2GID(
id)) != 0) rb_sys_fail(0);
6497#define p_sys_setegid rb_f_notimplement
6501#if defined HAVE_SETREGID
6516 rb_gid_t rgid, egid;
6518 rgid = OBJ2GID(rid);
6519 egid = OBJ2GID(eid);
6520 if (setregid(rgid, egid) != 0) rb_sys_fail(0);
6524#define p_sys_setregid rb_f_notimplement
6527#if defined HAVE_SETRESGID
6542 rb_gid_t rgid, egid, sgid;
6544 rgid = OBJ2GID(rid);
6545 egid = OBJ2GID(eid);
6546 sgid = OBJ2GID(sid);
6547 if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
6551#define p_sys_setresgid rb_f_notimplement
6555#if defined HAVE_ISSETUGID
6569p_sys_issetugid(
VALUE obj)
6571 return RBOOL(issetugid());
6574#define p_sys_issetugid rb_f_notimplement
6591proc_getgid(
VALUE obj)
6593 rb_gid_t gid = getgid();
6598#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETRGID) || defined(HAVE_SETGID)
6617#if defined(HAVE_SETRESGID)
6618 if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0);
6619#elif defined HAVE_SETREGID
6620 if (setregid(gid, -1) < 0) rb_sys_fail(0);
6621#elif defined HAVE_SETRGID
6622 if (setrgid(gid) < 0) rb_sys_fail(0);
6623#elif defined HAVE_SETGID
6625 if (getegid() == gid) {
6626 if (setgid(gid) < 0) rb_sys_fail(0);
6636#define proc_setgid rb_f_notimplement
6640#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6660static int _maxgroups = -1;
6662get_sc_ngroups_max(
void)
6664#ifdef _SC_NGROUPS_MAX
6665 return (
int)sysconf(_SC_NGROUPS_MAX);
6666#elif defined(NGROUPS_MAX)
6667 return (
int)NGROUPS_MAX;
6675 if (_maxgroups < 0) {
6676 _maxgroups = get_sc_ngroups_max();
6678 _maxgroups = RB_MAX_GROUPS;
6687#ifdef HAVE_GETGROUPS
6711proc_getgroups(
VALUE obj)
6717 ngroups = getgroups(0, NULL);
6721 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6723 ngroups = getgroups(ngroups, groups);
6728 for (i = 0; i < ngroups; i++)
6736#define proc_getgroups rb_f_notimplement
6740#ifdef HAVE_SETGROUPS
6765 if (ngroups > maxgroups())
6766 rb_raise(rb_eArgError,
"too many groups, %d max", maxgroups());
6768 groups =
ALLOCV_N(rb_gid_t, tmp, ngroups);
6770 for (i = 0; i < ngroups; i++) {
6773 groups[i] = OBJ2GID1(g);
6777 if (setgroups(ngroups, groups) == -1)
6782 return proc_getgroups(obj);
6785#define proc_setgroups rb_f_notimplement
6789#ifdef HAVE_INITGROUPS
6815 return proc_getgroups(obj);
6818#define proc_initgroups rb_f_notimplement
6821#if defined(_SC_NGROUPS_MAX) || defined(NGROUPS_MAX)
6834proc_getmaxgroups(
VALUE obj)
6839#define proc_getmaxgroups rb_f_notimplement
6842#ifdef HAVE_SETGROUPS
6855 int ngroups_max = get_sc_ngroups_max();
6858 rb_raise(rb_eArgError,
"maxgroups %d should be positive", ngroups);
6860 if (ngroups > RB_MAX_GROUPS)
6861 ngroups = RB_MAX_GROUPS;
6863 if (ngroups_max > 0 && ngroups > ngroups_max)
6864 ngroups = ngroups_max;
6866 _maxgroups = ngroups;
6871#define proc_setmaxgroups rb_f_notimplement
6874#if defined(HAVE_DAEMON) || (defined(HAVE_WORKING_FORK) && defined(HAVE_SETSID))
6875static int rb_daemon(
int nochdir,
int noclose);
6900 int n, nochdir = FALSE, noclose = FALSE;
6903 case 2: noclose = TO_BOOL(argv[1],
"noclose");
6904 case 1: nochdir = TO_BOOL(argv[0],
"nochdir");
6908 n = rb_daemon(nochdir, noclose);
6909 if (n < 0) rb_sys_fail(
"daemon");
6913extern const char ruby_null_device[];
6916rb_daemon(
int nochdir,
int noclose)
6920 rb_pid_t pid = getpid();
6922 rb_thread_acquire_fork_lock();
6923 err = daemon(nochdir, noclose);
6925 release_fork_lock_after_fork(getpid() == pid ? pid : 0);
6930 switch (rb_fork_ruby(NULL)) {
6933 default: _exit(EXIT_SUCCESS);
6937 if (setsid() < 0) (void)0;
6942 if (!noclose && (n =
rb_cloexec_open(ruby_null_device, O_RDWR, 0)) != -1) {
6954#define proc_daemon rb_f_notimplement
6967static rb_gid_t SAVED_GROUP_ID = -1;
6991 if (geteuid() == 0) {
6992#if defined(HAVE_SETRESGID)
6993 if (setresgid(gid, gid, gid) < 0) rb_sys_fail(0);
6994 SAVED_GROUP_ID = gid;
6995#elif defined HAVE_SETGID
6996 if (setgid(gid) < 0) rb_sys_fail(0);
6997 SAVED_GROUP_ID = gid;
6998#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
6999 if (getgid() == gid) {
7000 if (SAVED_GROUP_ID == gid) {
7001 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7005 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7006 if (setregid(SAVED_GROUP_ID, 0) < 0) rb_sys_fail(0);
7008 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7009 SAVED_GROUP_ID = gid;
7012 if (setregid(0, 0) < 0) rb_sys_fail(0);
7014 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7015 SAVED_GROUP_ID = gid;
7020 if (setregid(gid, gid) < 0) rb_sys_fail(0);
7021 SAVED_GROUP_ID = gid;
7023#elif defined(HAVE_SETRGID) && defined (HAVE_SETEGID)
7024 if (getgid() == gid) {
7025 if (SAVED_GROUP_ID == gid) {
7026 if (setegid(gid) < 0) rb_sys_fail(0);
7030 if (setegid(gid) < 0) rb_sys_fail(0);
7031 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7033 if (setrgid(0) < 0) rb_sys_fail(0);
7036 if (setrgid(0) < 0) rb_sys_fail(0);
7038 if (setegid(gid) < 0) rb_sys_fail(0);
7039 if (setrgid(gid) < 0) rb_sys_fail(0);
7040 SAVED_GROUP_ID = gid;
7045 if (setegid(gid) < 0) rb_sys_fail(0);
7046 if (setrgid(gid) < 0) rb_sys_fail(0);
7047 SAVED_GROUP_ID = gid;
7054#if defined(HAVE_SETRESGID)
7055 if (setresgid((getgid() == gid)? (rb_gid_t)-1: gid,
7056 (getegid() == gid)? (rb_gid_t)-1: gid,
7057 (SAVED_GROUP_ID == gid)? (rb_gid_t)-1: gid) < 0) rb_sys_fail(0);
7058 SAVED_GROUP_ID = gid;
7059#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7060 if (SAVED_GROUP_ID == gid) {
7061 if (setregid((getgid() == gid)? (rb_uid_t)-1: gid,
7062 (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7065 else if (getgid() != gid) {
7066 if (setregid(gid, (getegid() == gid)? (rb_uid_t)-1: gid) < 0)
7068 SAVED_GROUP_ID = gid;
7070 else if ( getegid() != gid) {
7071 if (setregid(getegid(), gid) < 0) rb_sys_fail(0);
7072 SAVED_GROUP_ID = gid;
7073 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7076 if (setregid(-1, SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7077 if (setregid(SAVED_GROUP_ID, gid) < 0) rb_sys_fail(0);
7078 SAVED_GROUP_ID = gid;
7079 if (setregid(gid, -1) < 0) rb_sys_fail(0);
7081#elif defined(HAVE_SETRGID) && defined(HAVE_SETEGID)
7082 if (SAVED_GROUP_ID == gid) {
7083 if (getegid() != gid && setegid(gid) < 0) rb_sys_fail(0);
7084 if (getgid() != gid && setrgid(gid) < 0) rb_sys_fail(0);
7086 else if ( getegid() == gid) {
7087 if (getgid() != gid) {
7088 if (setrgid(gid) < 0) rb_sys_fail(0);
7089 SAVED_GROUP_ID = gid;
7092 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7093 SAVED_GROUP_ID = gid;
7094 if (setrgid(gid) < 0) rb_sys_fail(0);
7097 else if ( getgid() == gid) {
7098 if (setegid(gid) < 0) rb_sys_fail(0);
7099 if (setrgid(SAVED_GROUP_ID) < 0) rb_sys_fail(0);
7100 SAVED_GROUP_ID = gid;
7101 if (setrgid(gid) < 0) rb_sys_fail(0);
7106#elif defined HAVE_44BSD_SETGID
7107 if (getgid() == gid) {
7109 if (setgid(gid) < 0) rb_sys_fail(0);
7110 SAVED_GROUP_ID = gid;
7115#elif defined HAVE_SETEGID
7116 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7117 if (setegid(gid) < 0) rb_sys_fail(0);
7122#elif defined HAVE_SETGID
7123 if (getgid() == gid && SAVED_GROUP_ID == gid) {
7124 if (setgid(gid) < 0) rb_sys_fail(0);
7151proc_geteuid(
VALUE obj)
7153 rb_uid_t euid = geteuid();
7157#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID) || defined(_POSIX_SAVED_IDS)
7159proc_seteuid(rb_uid_t uid)
7161#if defined(HAVE_SETRESUID)
7162 if (setresuid(-1, uid, -1) < 0) rb_sys_fail(0);
7163#elif defined HAVE_SETREUID
7164 if (setreuid(-1, uid) < 0) rb_sys_fail(0);
7165#elif defined HAVE_SETEUID
7166 if (seteuid(uid) < 0) rb_sys_fail(0);
7167#elif defined HAVE_SETUID
7168 if (uid == getuid()) {
7169 if (setuid(uid) < 0) rb_sys_fail(0);
7180#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) || defined(HAVE_SETUID)
7194 proc_seteuid(OBJ2UID(euid));
7198#define proc_seteuid_m rb_f_notimplement
7202rb_seteuid_core(rb_uid_t euid)
7204#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7210#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7214#if defined(HAVE_SETRESUID)
7216 if (setresuid(-1,euid,euid) < 0) rb_sys_fail(0);
7217 SAVED_USER_ID = euid;
7220 if (setresuid(-1,euid,-1) < 0) rb_sys_fail(0);
7222#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7223 if (setreuid(-1, euid) < 0) rb_sys_fail(0);
7225 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7226 if (setreuid(uid,euid) < 0) rb_sys_fail(0);
7227 SAVED_USER_ID = euid;
7229#elif defined HAVE_SETEUID
7230 if (seteuid(euid) < 0) rb_sys_fail(0);
7231#elif defined HAVE_SETUID
7232 if (geteuid() == 0) rb_sys_fail(0);
7233 if (setuid(euid) < 0) rb_sys_fail(0);
7258 rb_seteuid_core(OBJ2UID(
id));
7277proc_getegid(
VALUE obj)
7279 rb_gid_t egid = getegid();
7284#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) || defined(_POSIX_SAVED_IDS)
7297#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7303#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7304 gid = OBJ2GID(egid);
7307#if defined(HAVE_SETRESGID)
7308 if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
7309#elif defined HAVE_SETREGID
7310 if (setregid(-1, gid) < 0) rb_sys_fail(0);
7311#elif defined HAVE_SETEGID
7312 if (setegid(gid) < 0) rb_sys_fail(0);
7313#elif defined HAVE_SETGID
7314 if (gid == getgid()) {
7315 if (setgid(gid) < 0) rb_sys_fail(0);
7327#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
7328#define proc_setegid_m proc_setegid
7330#define proc_setegid_m rb_f_notimplement
7334rb_setegid_core(rb_gid_t egid)
7336#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7342#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7346#if defined(HAVE_SETRESGID)
7348 if (setresgid(-1,egid,egid) < 0) rb_sys_fail(0);
7349 SAVED_GROUP_ID = egid;
7352 if (setresgid(-1,egid,-1) < 0) rb_sys_fail(0);
7354#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7355 if (setregid(-1, egid) < 0) rb_sys_fail(0);
7357 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7358 if (setregid(gid,egid) < 0) rb_sys_fail(0);
7359 SAVED_GROUP_ID = egid;
7361#elif defined HAVE_SETEGID
7362 if (setegid(egid) < 0) rb_sys_fail(0);
7363#elif defined HAVE_SETGID
7364 if (geteuid() == 0 ) rb_sys_fail(0);
7365 if (setgid(egid) < 0) rb_sys_fail(0);
7390 rb_setegid_core(OBJ2GID(
id));
7405p_uid_exchangeable(
VALUE _)
7407#if defined(HAVE_SETRESUID)
7409#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7430p_uid_exchange(
VALUE obj)
7433#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7440#if defined(HAVE_SETRESUID) || (defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID))
7444#if defined(HAVE_SETRESUID)
7445 if (setresuid(euid, uid, uid) < 0) rb_sys_fail(0);
7446 SAVED_USER_ID = uid;
7447#elif defined(HAVE_SETREUID) && !defined(OBSOLETE_SETREUID)
7448 if (setreuid(euid,uid) < 0) rb_sys_fail(0);
7449 SAVED_USER_ID = uid;
7451 (void)SAVED_USER_ID;
7468p_gid_exchangeable(
VALUE _)
7470#if defined(HAVE_SETRESGID)
7472#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7493p_gid_exchange(
VALUE obj)
7496#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7503#if defined(HAVE_SETRESGID) || (defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID))
7507#if defined(HAVE_SETRESGID)
7508 if (setresgid(egid, gid, gid) < 0) rb_sys_fail(0);
7509 SAVED_GROUP_ID = gid;
7510#elif defined(HAVE_SETREGID) && !defined(OBSOLETE_SETREGID)
7511 if (setregid(egid,gid) < 0) rb_sys_fail(0);
7512 SAVED_GROUP_ID = gid;
7514 (void)SAVED_GROUP_ID;
7532p_uid_have_saved_id(
VALUE _)
7534#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7542#if defined(HAVE_SETRESUID) || defined(HAVE_SETEUID) || defined(_POSIX_SAVED_IDS)
7544p_uid_sw_ensure(
VALUE i)
7546 rb_uid_t
id = (rb_uid_t)i;
7547 under_uid_switch = 0;
7548 id = rb_seteuid_core(
id);
7567p_uid_switch(
VALUE obj)
7579 under_uid_switch = 1;
7586 else if (euid != SAVED_USER_ID) {
7587 proc_seteuid(SAVED_USER_ID);
7589 under_uid_switch = 1;
7604p_uid_sw_ensure(
VALUE obj)
7606 under_uid_switch = 0;
7607 return p_uid_exchange(obj);
7611p_uid_switch(
VALUE obj)
7623 p_uid_exchange(obj);
7625 under_uid_switch = 1;
7647p_gid_have_saved_id(
VALUE _)
7649#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7656#if defined(HAVE_SETRESGID) || defined(HAVE_SETEGID) || defined(_POSIX_SAVED_IDS)
7658p_gid_sw_ensure(
VALUE i)
7660 rb_gid_t
id = (rb_gid_t)i;
7661 under_gid_switch = 0;
7662 id = rb_setegid_core(
id);
7681p_gid_switch(
VALUE obj)
7693 under_gid_switch = 1;
7700 else if (egid != SAVED_GROUP_ID) {
7701 proc_setegid(obj,
GIDT2NUM(SAVED_GROUP_ID));
7703 under_gid_switch = 1;
7718p_gid_sw_ensure(
VALUE obj)
7720 under_gid_switch = 0;
7721 return p_gid_exchange(obj);
7725p_gid_switch(
VALUE obj)
7737 p_gid_exchange(obj);
7739 under_gid_switch = 1;
7749#if defined(HAVE_TIMES)
7753#ifdef HAVE__SC_CLK_TCK
7754 return sysconf(_SC_CLK_TCK);
7755#elif defined CLK_TCK
7778rb_proc_times(
VALUE obj)
7780 VALUE utime, stime, cutime, cstime, ret;
7781#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN)
7782 struct rusage usage_s, usage_c;
7784 if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0)
7785 rb_sys_fail(
"getrusage");
7786 utime =
DBL2NUM((
double)usage_s.ru_utime.tv_sec + (
double)usage_s.ru_utime.tv_usec/1e6);
7787 stime =
DBL2NUM((
double)usage_s.ru_stime.tv_sec + (
double)usage_s.ru_stime.tv_usec/1e6);
7788 cutime =
DBL2NUM((
double)usage_c.ru_utime.tv_sec + (
double)usage_c.ru_utime.tv_usec/1e6);
7789 cstime =
DBL2NUM((
double)usage_c.ru_stime.tv_sec + (
double)usage_c.ru_stime.tv_usec/1e6);
7791 const double hertz = (double)get_clk_tck();
7795 utime =
DBL2NUM(buf.tms_utime / hertz);
7796 stime =
DBL2NUM(buf.tms_stime / hertz);
7797 cutime =
DBL2NUM(buf.tms_cutime / hertz);
7798 cstime =
DBL2NUM(buf.tms_cstime / hertz);
7800 ret =
rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime);
7808#define rb_proc_times rb_f_notimplement
7811#ifdef HAVE_LONG_LONG
7813#define TIMETICK_INT_MIN LLONG_MIN
7814#define TIMETICK_INT_MAX LLONG_MAX
7815#define TIMETICK_INT2NUM(v) LL2NUM(v)
7816#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_LONG_P(a, b)
7818typedef long timetick_int_t;
7819#define TIMETICK_INT_MIN LONG_MIN
7820#define TIMETICK_INT_MAX LONG_MAX
7821#define TIMETICK_INT2NUM(v) LONG2NUM(v)
7822#define MUL_OVERFLOW_TIMETICK_P(a, b) MUL_OVERFLOW_LONG_P(a, b)
7825CONSTFUNC(
static timetick_int_t gcd_timetick_int(timetick_int_t, timetick_int_t));
7826static timetick_int_t
7827gcd_timetick_int(timetick_int_t a, timetick_int_t b)
7847reduce_fraction(timetick_int_t *np, timetick_int_t *dp)
7849 timetick_int_t gcd = gcd_timetick_int(*np, *dp);
7857reduce_factors(timetick_int_t *numerators,
int num_numerators,
7858 timetick_int_t *denominators,
int num_denominators)
7861 for (i = 0; i < num_numerators; i++) {
7862 if (numerators[i] == 1)
7864 for (j = 0; j < num_denominators; j++) {
7865 if (denominators[j] == 1)
7867 reduce_fraction(&numerators[i], &denominators[j]);
7873 timetick_int_t giga_count;
7878timetick2dblnum(
struct timetick *ttp,
7879 timetick_int_t *numerators,
int num_numerators,
7880 timetick_int_t *denominators,
int num_denominators)
7885 reduce_factors(numerators, num_numerators,
7886 denominators, num_denominators);
7888 d = ttp->giga_count * 1e9 + ttp->count;
7890 for (i = 0; i < num_numerators; i++)
7892 for (i = 0; i < num_denominators; i++)
7893 d /= denominators[i];
7899timetick2dblnum_reciprocal(
struct timetick *ttp,
7900 timetick_int_t *numerators,
int num_numerators,
7901 timetick_int_t *denominators,
int num_denominators)
7906 reduce_factors(numerators, num_numerators,
7907 denominators, num_denominators);
7910 for (i = 0; i < num_denominators; i++)
7911 d *= denominators[i];
7912 for (i = 0; i < num_numerators; i++)
7914 d /= ttp->giga_count * 1e9 + ttp->count;
7919#define NDIV(x,y) (-(-((x)+1)/(y))-1)
7920#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
7923timetick2integer(
struct timetick *ttp,
7924 timetick_int_t *numerators,
int num_numerators,
7925 timetick_int_t *denominators,
int num_denominators)
7930 reduce_factors(numerators, num_numerators,
7931 denominators, num_denominators);
7933 if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, ttp->giga_count,
7934 TIMETICK_INT_MIN, TIMETICK_INT_MAX-ttp->count)) {
7935 timetick_int_t t = ttp->giga_count * 1000000000 + ttp->count;
7936 for (i = 0; i < num_numerators; i++) {
7937 timetick_int_t factor = numerators[i];
7938 if (MUL_OVERFLOW_TIMETICK_P(factor, t))
7942 for (i = 0; i < num_denominators; i++) {
7943 t = DIV(t, denominators[i]);
7945 return TIMETICK_INT2NUM(t);
7949 v = TIMETICK_INT2NUM(ttp->giga_count);
7952 for (i = 0; i < num_numerators; i++) {
7953 timetick_int_t factor = numerators[i];
7956 v =
rb_funcall(v,
'*', 1, TIMETICK_INT2NUM(factor));
7958 for (i = 0; i < num_denominators; i++) {
7959 v =
rb_funcall(v,
'/', 1, TIMETICK_INT2NUM(denominators[i]));
7965make_clock_result(
struct timetick *ttp,
7966 timetick_int_t *numerators,
int num_numerators,
7967 timetick_int_t *denominators,
int num_denominators,
7970 if (unit ==
ID2SYM(id_nanosecond)) {
7971 numerators[num_numerators++] = 1000000000;
7972 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7974 else if (unit ==
ID2SYM(id_microsecond)) {
7975 numerators[num_numerators++] = 1000000;
7976 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7978 else if (unit ==
ID2SYM(id_millisecond)) {
7979 numerators[num_numerators++] = 1000;
7980 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7982 else if (unit ==
ID2SYM(id_second)) {
7983 return timetick2integer(ttp, numerators, num_numerators, denominators, num_denominators);
7985 else if (unit ==
ID2SYM(id_float_microsecond)) {
7986 numerators[num_numerators++] = 1000000;
7987 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7989 else if (unit ==
ID2SYM(id_float_millisecond)) {
7990 numerators[num_numerators++] = 1000;
7991 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7993 else if (
NIL_P(unit) || unit ==
ID2SYM(id_float_second)) {
7994 return timetick2dblnum(ttp, numerators, num_numerators, denominators, num_denominators);
7997 rb_raise(rb_eArgError,
"unexpected unit: %"PRIsVALUE, unit);
8001static const mach_timebase_info_data_t *
8002get_mach_timebase_info(
void)
8004 static mach_timebase_info_data_t sTimebaseInfo;
8006 if ( sTimebaseInfo.denom == 0 ) {
8007 (void) mach_timebase_info(&sTimebaseInfo);
8010 return &sTimebaseInfo;
8014ruby_real_ms_time(
void)
8016 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8017 uint64_t t = mach_absolute_time();
8018 return (
double)t * info->numer / info->denom / 1e6;
8022#if defined(NUM2CLOCKID)
8023# define NUMERIC_CLOCKID 1
8025# define NUMERIC_CLOCKID 0
8026# define NUM2CLOCKID(x) 0
8029#define clock_failed(name, err, arg) do { \
8030 int clock_error = (err); \
8031 rb_syserr_fail_str(clock_error, rb_sprintf("clock_" name "(%+"PRIsVALUE")", (arg))); \
8211 timetick_int_t numerators[2];
8212 timetick_int_t denominators[2];
8213 int num_numerators = 0;
8214 int num_denominators = 0;
8217 VALUE clk_id = argv[0];
8218#ifdef HAVE_CLOCK_GETTIME
8223#ifdef CLOCK_REALTIME
8224 if (clk_id == RUBY_CLOCK_REALTIME) {
8230#ifdef CLOCK_MONOTONIC
8231 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8232 c = CLOCK_MONOTONIC;
8237#ifdef CLOCK_PROCESS_CPUTIME_ID
8238 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8239 c = CLOCK_PROCESS_CPUTIME_ID;
8244#ifdef CLOCK_THREAD_CPUTIME_ID
8245 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8246 c = CLOCK_THREAD_CPUTIME_ID;
8254#ifdef HAVE_GETTIMEOFDAY
8259#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
8260 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8262 ret = gettimeofday(&tv, 0);
8264 rb_sys_fail(
"gettimeofday");
8265 tt.giga_count = tv.tv_sec;
8266 tt.count = (int32_t)tv.tv_usec * 1000;
8267 denominators[num_denominators++] = 1000000000;
8272#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
8273 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8276 if (t == (time_t)-1)
8277 rb_sys_fail(
"time");
8280 denominators[num_denominators++] = 1000000000;
8285#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
8286 ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
8287 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8290 unsigned_clock_t uc;
8292 if (c == (clock_t)-1)
8293 rb_sys_fail(
"times");
8294 uc = (unsigned_clock_t)c;
8295 tt.count = (int32_t)(uc % 1000000000);
8296 tt.giga_count = (uc / 1000000000);
8297 denominators[num_denominators++] = get_clk_tck();
8303#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
8304 ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
8305 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8306 struct rusage usage;
8308 ret = getrusage(RUSAGE_SELF, &usage);
8310 rb_sys_fail(
"getrusage");
8311 tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
8312 usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
8313 if (1000000 <= usec) {
8317 tt.count = usec * 1000;
8318 denominators[num_denominators++] = 1000000000;
8324#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
8325 ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
8326 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8328 unsigned_clock_t utime, stime;
8329 if (times(&buf) == (clock_t)-1)
8330 rb_sys_fail(
"times");
8331 utime = (unsigned_clock_t)buf.tms_utime;
8332 stime = (unsigned_clock_t)buf.tms_stime;
8333 tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
8334 tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
8335 if (1000000000 <= tt.count) {
8336 tt.count -= 1000000000;
8339 denominators[num_denominators++] = get_clk_tck();
8344#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
8345 ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
8346 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8348 unsigned_clock_t uc;
8351 if (c == (clock_t)-1)
8352 rb_sys_fail(
"clock");
8353 uc = (unsigned_clock_t)c;
8354 tt.count = (int32_t)(uc % 1000000000);
8355 tt.giga_count = uc / 1000000000;
8356 denominators[num_denominators++] = CLOCKS_PER_SEC;
8361 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8362 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8363 uint64_t t = mach_absolute_time();
8364 tt.count = (int32_t)(t % 1000000000);
8365 tt.giga_count = t / 1000000000;
8366 numerators[num_numerators++] = info->numer;
8367 denominators[num_denominators++] = info->denom;
8368 denominators[num_denominators++] = 1000000000;
8373 else if (NUMERIC_CLOCKID) {
8374#if defined(HAVE_CLOCK_GETTIME)
8376 c = NUM2CLOCKID(clk_id);
8378 ret = clock_gettime(c, &ts);
8380 clock_failed(
"gettime",
errno, clk_id);
8381 tt.count = (int32_t)ts.tv_nsec;
8382 tt.giga_count = ts.tv_sec;
8383 denominators[num_denominators++] = 1000000000;
8390 clock_failed(
"gettime", EINVAL, clk_id);
8393 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8438 timetick_int_t numerators[2];
8439 timetick_int_t denominators[2];
8440 int num_numerators = 0;
8441 int num_denominators = 0;
8442#ifdef HAVE_CLOCK_GETRES
8447 VALUE clk_id = argv[0];
8450#ifdef CLOCK_REALTIME
8451 if (clk_id == RUBY_CLOCK_REALTIME) {
8457#ifdef CLOCK_MONOTONIC
8458 if (clk_id == RUBY_CLOCK_MONOTONIC) {
8459 c = CLOCK_MONOTONIC;
8464#ifdef CLOCK_PROCESS_CPUTIME_ID
8465 if (clk_id == RUBY_CLOCK_PROCESS_CPUTIME_ID) {
8466 c = CLOCK_PROCESS_CPUTIME_ID;
8471#ifdef CLOCK_THREAD_CPUTIME_ID
8472 if (clk_id == RUBY_CLOCK_THREAD_CPUTIME_ID) {
8473 c = CLOCK_THREAD_CPUTIME_ID;
8478#ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
8479 if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
8482 denominators[num_denominators++] = 1000000000;
8487#ifdef RUBY_TIME_BASED_CLOCK_REALTIME
8488 if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
8491 denominators[num_denominators++] = 1000000000;
8496#ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC
8497 if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
8500 denominators[num_denominators++] = get_clk_tck();
8505#ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
8506 if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8509 denominators[num_denominators++] = 1000000000;
8514#ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
8515 if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8518 denominators[num_denominators++] = get_clk_tck();
8523#ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
8524 if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
8527 denominators[num_denominators++] = CLOCKS_PER_SEC;
8532#ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
8533 if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
8534 const mach_timebase_info_data_t *info = get_mach_timebase_info();
8537 numerators[num_numerators++] = info->numer;
8538 denominators[num_denominators++] = info->denom;
8539 denominators[num_denominators++] = 1000000000;
8544 else if (NUMERIC_CLOCKID) {
8545#if defined(HAVE_CLOCK_GETRES)
8547 c = NUM2CLOCKID(clk_id);
8549 ret = clock_getres(c, &ts);
8551 clock_failed(
"getres",
errno, clk_id);
8552 tt.count = (int32_t)ts.tv_nsec;
8553 tt.giga_count = ts.tv_sec;
8554 denominators[num_denominators++] = 1000000000;
8561 clock_failed(
"getres", EINVAL, clk_id);
8564 if (unit ==
ID2SYM(id_hertz)) {
8565 return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
8568 return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
8573get_CHILD_STATUS(
ID _x,
VALUE *_y)
8579get_PROCESS_ID(
ID _x,
VALUE *_y)
8671static VALUE rb_mProcUID;
8672static VALUE rb_mProcGID;
8673static VALUE rb_mProcID_Syscall;
8709 rb_eager_load_detailed_message_extension();
8712 rb_gc_prepare_heap();
9140 rb_gvar_ractor_local(
"$$");
9141 rb_gvar_ractor_local(
"$?");
9142 rb_gvar_box_ready(
"$$");
9143 rb_gvar_box_ready(
"$?");
9198 process_status_dump, process_status_load);
9234#ifdef HAVE_GETPRIORITY
9245#if defined(RLIM2NUM) && defined(RLIM_INFINITY)
9247 VALUE inf = RLIM2NUM(RLIM_INFINITY);
9248#ifdef RLIM_SAVED_MAX
9250 VALUE v = RLIM_INFINITY == RLIM_SAVED_MAX ? inf : RLIM2NUM(RLIM_SAVED_MAX);
9252 rb_define_const(
rb_mProcess,
"RLIM_SAVED_MAX", v);
9256 rb_define_const(
rb_mProcess,
"RLIM_INFINITY", inf);
9257#ifdef RLIM_SAVED_CUR
9259 VALUE v = RLIM_INFINITY == RLIM_SAVED_CUR ? inf : RLIM2NUM(RLIM_SAVED_CUR);
9261 rb_define_const(
rb_mProcess,
"RLIM_SAVED_CUR", v);
9300#ifdef RLIMIT_MEMLOCK
9307#ifdef RLIMIT_MSGQUEUE
9373#ifdef RLIMIT_SIGPENDING
9408#if defined(RUBY_CLOCK_REALTIME)
9409#elif defined(RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
9410# define RUBY_CLOCK_REALTIME RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
9411#elif defined(RUBY_TIME_BASED_CLOCK_REALTIME)
9412# define RUBY_CLOCK_REALTIME RUBY_TIME_BASED_CLOCK_REALTIME
9414#if defined(CLOCK_REALTIME) && defined(CLOCKID2NUM)
9416 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
9417#elif defined(RUBY_CLOCK_REALTIME)
9418 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME", RUBY_CLOCK_REALTIME);
9421#if defined(RUBY_CLOCK_MONOTONIC)
9422#elif defined(RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
9423# define RUBY_CLOCK_MONOTONIC RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
9425#if defined(CLOCK_MONOTONIC) && defined(CLOCKID2NUM)
9427 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
9428#elif defined(RUBY_CLOCK_MONOTONIC)
9429 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC", RUBY_CLOCK_MONOTONIC);
9432#if defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9433#elif defined(RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
9434# define RUBY_CLOCK_PROCESS_CPUTIME_ID RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
9436#if defined(CLOCK_PROCESS_CPUTIME_ID) && defined(CLOCKID2NUM)
9438 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
9439#elif defined(RUBY_CLOCK_PROCESS_CPUTIME_ID)
9440 rb_define_const(
rb_mProcess,
"CLOCK_PROCESS_CPUTIME_ID", RUBY_CLOCK_PROCESS_CPUTIME_ID);
9443#if defined(CLOCK_THREAD_CPUTIME_ID) && defined(CLOCKID2NUM)
9445 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
9446#elif defined(RUBY_CLOCK_THREAD_CPUTIME_ID)
9447 rb_define_const(
rb_mProcess,
"CLOCK_THREAD_CPUTIME_ID", RUBY_CLOCK_THREAD_CPUTIME_ID);
9453 rb_define_const(
rb_mProcess,
"CLOCK_VIRTUAL", CLOCKID2NUM(CLOCK_VIRTUAL));
9457 rb_define_const(
rb_mProcess,
"CLOCK_PROF", CLOCKID2NUM(CLOCK_PROF));
9459#ifdef CLOCK_REALTIME_FAST
9461 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_FAST", CLOCKID2NUM(CLOCK_REALTIME_FAST));
9463#ifdef CLOCK_REALTIME_PRECISE
9465 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_PRECISE", CLOCKID2NUM(CLOCK_REALTIME_PRECISE));
9467#ifdef CLOCK_REALTIME_COARSE
9469 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_COARSE", CLOCKID2NUM(CLOCK_REALTIME_COARSE));
9471#ifdef CLOCK_REALTIME_ALARM
9473 rb_define_const(
rb_mProcess,
"CLOCK_REALTIME_ALARM", CLOCKID2NUM(CLOCK_REALTIME_ALARM));
9475#ifdef CLOCK_MONOTONIC_FAST
9477 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_FAST", CLOCKID2NUM(CLOCK_MONOTONIC_FAST));
9479#ifdef CLOCK_MONOTONIC_PRECISE
9481 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_PRECISE", CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE));
9483#ifdef CLOCK_MONOTONIC_RAW
9485 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW", CLOCKID2NUM(CLOCK_MONOTONIC_RAW));
9487#ifdef CLOCK_MONOTONIC_RAW_APPROX
9489 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_RAW_APPROX", CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX));
9491#ifdef CLOCK_MONOTONIC_COARSE
9493 rb_define_const(
rb_mProcess,
"CLOCK_MONOTONIC_COARSE", CLOCKID2NUM(CLOCK_MONOTONIC_COARSE));
9495#ifdef CLOCK_BOOTTIME
9497 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME", CLOCKID2NUM(CLOCK_BOOTTIME));
9499#ifdef CLOCK_BOOTTIME_ALARM
9501 rb_define_const(
rb_mProcess,
"CLOCK_BOOTTIME_ALARM", CLOCKID2NUM(CLOCK_BOOTTIME_ALARM));
9505 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME", CLOCKID2NUM(CLOCK_UPTIME));
9507#ifdef CLOCK_UPTIME_FAST
9509 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_FAST", CLOCKID2NUM(CLOCK_UPTIME_FAST));
9511#ifdef CLOCK_UPTIME_PRECISE
9513 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_PRECISE", CLOCKID2NUM(CLOCK_UPTIME_PRECISE));
9515#ifdef CLOCK_UPTIME_RAW
9517 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW", CLOCKID2NUM(CLOCK_UPTIME_RAW));
9519#ifdef CLOCK_UPTIME_RAW_APPROX
9521 rb_define_const(
rb_mProcess,
"CLOCK_UPTIME_RAW_APPROX", CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX));
9525 rb_define_const(
rb_mProcess,
"CLOCK_SECOND", CLOCKID2NUM(CLOCK_SECOND));
9529 rb_define_const(
rb_mProcess,
"CLOCK_TAI", CLOCKID2NUM(CLOCK_TAI));
9535#if defined(HAVE_TIMES)
9549 SAVED_USER_ID = geteuid();
9550 SAVED_GROUP_ID = getegid();
9552 rb_mProcUID = rb_define_module_under(
rb_mProcess,
"UID");
9553 rb_mProcGID = rb_define_module_under(
rb_mProcess,
"GID");
9573#ifdef p_uid_from_name
9576#ifdef p_gid_from_name
9580 rb_mProcID_Syscall = rb_define_module_under(
rb_mProcess,
"Sys");
9607#define define_id(name) id_##name = rb_intern_const(#name)
9620 define_id(new_pgroup);
9622 define_id(unsetenv_others);
9625 define_id(close_others);
9626 define_id(nanosecond);
9627 define_id(microsecond);
9628 define_id(millisecond);
9630 define_id(float_microsecond);
9631 define_id(float_millisecond);
9632 define_id(float_second);
9633 define_id(GETTIMEOFDAY_BASED_CLOCK_REALTIME);
9634 define_id(TIME_BASED_CLOCK_REALTIME);
9635#ifdef CLOCK_REALTIME
9636 define_id(CLOCK_REALTIME);
9638#ifdef CLOCK_MONOTONIC
9639 define_id(CLOCK_MONOTONIC);
9641#ifdef CLOCK_PROCESS_CPUTIME_ID
9642 define_id(CLOCK_PROCESS_CPUTIME_ID);
9644#ifdef CLOCK_THREAD_CPUTIME_ID
9645 define_id(CLOCK_THREAD_CPUTIME_ID);
9648 define_id(TIMES_BASED_CLOCK_MONOTONIC);
9649 define_id(TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID);
9652 define_id(GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID);
9654 define_id(CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID);
9656 define_id(MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC);
9659#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.