Ruby  3.4.0dev (2024-11-22 revision 0989400a925cd201defdca9eb28eb87200b30785)
process.h
1 #ifndef INTERNAL_PROCESS_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_PROCESS_H
11 #include "ruby/internal/config.h" /* for rb_pid_t */
12 #include <stddef.h> /* for size_t */
13 
14 #ifdef HAVE_SYS_TYPES_H
15 # include <sys/types.h> /* for mode_t */
16 #endif
17 
18 #ifdef _WIN32
19 # include "ruby/win32.h" /* for mode_t */
20 #endif
21 
22 #include "ruby/ruby.h" /* for VALUE */
23 #include "internal/compilers.h" /* for __has_warning */
24 #include "internal/imemo.h" /* for RB_IMEMO_TMPBUF_PTR */
25 
26 #define RB_MAX_GROUPS (65536)
27 
28 struct waitpid_state;
29 struct rb_process_status;
30 struct rb_execarg {
31  union {
32  struct {
33  VALUE shell_script;
34  } sh;
35  struct {
36  VALUE command_name;
37  VALUE command_abspath; /* full path string or nil */
38  VALUE argv_str;
39  VALUE argv_buf;
40  } cmd;
41  } invoke;
42  VALUE redirect_fds;
43  VALUE envp_str;
44  VALUE envp_buf;
45  VALUE dup2_tmpbuf;
46  unsigned use_shell : 1;
47  unsigned pgroup_given : 1;
48  unsigned umask_given : 1;
49  unsigned unsetenv_others_given : 1;
50  unsigned unsetenv_others_do : 1;
51  unsigned close_others_given : 1;
52  unsigned close_others_do : 1;
53  unsigned chdir_given : 1;
54  unsigned new_pgroup_given : 1;
55  unsigned new_pgroup_flag : 1;
56  unsigned uid_given : 1;
57  unsigned gid_given : 1;
58  unsigned exception : 1;
59  unsigned exception_given : 1;
60  struct rb_process_status *status;
61  struct waitpid_state *waitpid_state; /* for async process management */
62  rb_pid_t pgroup_pgid; /* asis(-1), new pgroup(0), specified pgroup (0<V). */
63  VALUE rlimit_limits; /* Qfalse or [[rtype, softlim, hardlim], ...] */
64  mode_t umask_mask;
65  rb_uid_t uid;
66  rb_gid_t gid;
67  int close_others_maxhint;
68  VALUE fd_dup2;
69  VALUE fd_close;
70  VALUE fd_open;
71  VALUE fd_dup2_child;
72  VALUE env_modification; /* Qfalse or [[k1,v1], ...] */
73  VALUE path_env;
74  VALUE chdir_dir;
75 };
76 
77 /* process.c */
78 rb_pid_t rb_call_proc__fork(void);
79 void rb_last_status_clear(void);
80 static inline char **ARGVSTR2ARGV(VALUE argv_str);
81 static inline size_t ARGVSTR2ARGC(VALUE argv_str);
82 
83 #ifdef HAVE_PWD_H
84 VALUE rb_getlogin(void);
85 VALUE rb_getpwdirnam_for_login(VALUE login); /* read as: "get pwd db home dir by username for login" */
86 VALUE rb_getpwdiruid(void); /* read as: "get pwd db home dir for getuid()" */
87 #endif
88 
89 RUBY_SYMBOL_EXPORT_BEGIN
90 /* process.c (export) */
91 int rb_exec_async_signal_safe(const struct rb_execarg *e, char *errmsg, size_t errmsg_buflen);
92 rb_pid_t rb_fork_async_signal_safe(int *status, int (*chfunc)(void*, char *, size_t), void *charg, VALUE fds, char *errmsg, size_t errmsg_buflen);
93 VALUE rb_execarg_new(int argc, const VALUE *argv, int accept_shell, int allow_exc_opt);
94 struct rb_execarg *rb_execarg_get(VALUE execarg_obj); /* dangerous. needs GC guard. */
95 int rb_execarg_addopt(VALUE execarg_obj, VALUE key, VALUE val);
96 void rb_execarg_parent_start(VALUE execarg_obj);
97 void rb_execarg_parent_end(VALUE execarg_obj);
98 int rb_execarg_run_options(const struct rb_execarg *e, struct rb_execarg *s, char* errmsg, size_t errmsg_buflen);
99 VALUE rb_execarg_extract_options(VALUE execarg_obj, VALUE opthash);
100 void rb_execarg_setenv(VALUE execarg_obj, VALUE env);
101 RUBY_SYMBOL_EXPORT_END
102 
103 /* argv_str contains extra two elements.
104  * The beginning one is for /bin/sh used by exec_with_sh.
105  * The last one for terminating NULL used by execve.
106  * See rb_exec_fillarg() in process.c. */
107 static inline char **
108 ARGVSTR2ARGV(VALUE argv_str)
109 {
110  char **buf = RB_IMEMO_TMPBUF_PTR(argv_str);
111  return &buf[1];
112 }
113 
114 static inline size_t
115 ARGVSTR2ARGC(VALUE argv_str)
116 {
117  size_t i = 0;
118  char *const *p = ARGVSTR2ARGV(argv_str);
119  while (p[i++])
120  ;
121  return i - 1;
122 }
123 
124 #endif /* INTERNAL_PROCESS_H */
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40