Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mrbgems/mruby-process/include/process_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ mrb_int mrb_hal_process_gid(mrb_state *mrb);
Returns -1 with errno set on failure. */
mrb_int mrb_hal_process_egid(mrb_state *mrb);

/* The fork(2) system call.
Returns -1 with errno set on failure. */
mrb_int mrb_hal_process_fork(mrb_state *mrb);

#ifdef MRB_HAL_PROCESS_HAS_WAIT
/*
* Wait for a child process to change state.
Expand Down
4 changes: 4 additions & 0 deletions mrbgems/mruby-process/mrbgem.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ MRuby::Gem::Specification.new('mruby-process') do |spec|
# Struct already does: the members, `#to_a`, `#==` and `#inspect`.
spec.add_dependency 'mruby-struct', core: 'mruby-struct'

# 'Process.fork' requires access to Kernel#exit from stdlib.h
# when a child process shuts down.
spec.add_dependency 'mruby-exit', core: 'mruby-exit'

# mruby-process needs no I/O of its own. The tests do: waiting on a child
# is only testable with a child, and IO.popen is how one is made. The
# dependency stops at the tests; see README.md.
Expand Down
9 changes: 9 additions & 0 deletions mrbgems/mruby-process/mrblib/kernel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Kernel
module_function
##
# call-seq:
# Kernel#fork -> integer, nil
def fork(&)
Process.fork(&)
end
end
36 changes: 36 additions & 0 deletions mrbgems/mruby-process/mrblib/process.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Process
##
# call-seq:
# Process.fork -> integer, nil
def self.fork
pid = Process.__fork
return pid if pid != 0
##
# Context switch:
# From this point onwards (pid == 0) the
# active process is the child process.
if block_given?
begin
yield
##
# Given a block:
# Don't return control to the caller.
# Same as CRuby.
exit!(0)
rescue SystemExit
exit!($!.status)
rescue Exception
##
# FIXME:
# we should dump the error to $stderr here.
exit!(1)
end
else
##
# No block given:
# Return control to the caller that's now
# running on the child process.
nil
end
end
end
8 changes: 8 additions & 0 deletions mrbgems/mruby-process/ports/posix/process_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ mrb_hal_process_egid(mrb_state *mrb)
return (mrb_int)getegid();
}

mrb_int
mrb_hal_process_fork(mrb_state *mrb)
{
pid_t pid;
pid = fork();
return pid < 0 ? -1 : (mrb_int)pid;
}

/*
* Waiting
*/
Expand Down
13 changes: 13 additions & 0 deletions mrbgems/mruby-process/ports/win/process_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ mrb_hal_process_ppid(mrb_state *mrb)
return ppid;
}

/*
Windows does not implement fork(2):
we set errno (ENOSYS), return -1 and
then raise NotImplementedError in the
process_fork function (see process.c)
*/
mrb_int
mrb_hal_process_fork(mrb_state *mrb)
{
errno = ENOSYS;
return -1;
}

/* Windows has no numeric user or group IDs; like CRuby, report 0 for all
of them. */
mrb_int
Expand Down
31 changes: 31 additions & 0 deletions mrbgems/mruby-process/src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ process_euid(mrb_state *mrb, mrb_value self)
return mrb_int_value(mrb, euid);
}

/*
* call-seq:
* Process.__fork -> integer
*
* The fork(2) system call creates a new process that
* inherits from its parent.
*
* See mruby-process/mrblib/kernel.rb for where and how
* this method is called.
*/
static mrb_value
process_fork(mrb_state *mrb, mrb_value self)
{
mrb_int pid;
pid = mrb_hal_process_fork(mrb);
if (pid == -1) {
if (errno == ENOSYS) {
mrb_raise(mrb, E_NOTIMP_ERROR, "fork(2) is not implemented on this platform");
} else {
mrb_sys_fail(mrb, "fork");
}
}
if (pid == 0) {
mrb_int childpid;
childpid = mrb_hal_process_pid(mrb);
mrb_gv_set(mrb, mrb_intern_lit(mrb, "$$"), mrb_int_value(mrb, childpid));
}
return mrb_int_value(mrb, pid);
}

/*
* call-seq:
* Process.gid -> integer
Expand Down Expand Up @@ -397,6 +427,7 @@ mrb_mruby_process_gem_init(mrb_state *mrb)
mrb_define_module_function_id(mrb, process, MRB_SYM(ppid), process_ppid, MRB_ARGS_NONE());
mrb_define_module_function_id(mrb, process, MRB_SYM(uid), process_uid, MRB_ARGS_NONE());
mrb_define_module_function_id(mrb, process, MRB_SYM(euid), process_euid, MRB_ARGS_NONE());
mrb_define_module_function_id(mrb, process, MRB_SYM(__fork), process_fork, MRB_ARGS_NONE());
mrb_define_module_function_id(mrb, process, MRB_SYM(gid), process_gid, MRB_ARGS_NONE());
mrb_define_module_function_id(mrb, process, MRB_SYM(egid), process_egid, MRB_ARGS_NONE());
mrb_define_module_function_id(mrb, process, MRB_SYM(kill), process_kill, MRB_ARGS_REQ(2)|MRB_ARGS_REST());
Expand Down
37 changes: 37 additions & 0 deletions mrbgems/mruby-process/test/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,40 @@ def self.spawn(cmd)
end
end
end

##
# Windows + fork
assert("Process.fork raises NotImplementedError") do
skip "test requires windows" unless ProcessTestUtil.windows?
assert_raise(NotImplementedError) { Process.fork {} }
assert_raise(NotImplementedError) { Process.fork }
end

##
# UNIX + fork
assert("Process.fork returns 42") do
skip "test requires unix" if ProcessTestUtil.windows?
skip "test requires wait" if not ProcessTestUtil.wait?
##
# Given no block
pid = Process.fork
if pid.nil?
exit!(42)
else
Process.wait(pid)
assert_equal 42, $?.exitstatus
end
##
# Given a block
Process.wait Process.fork { exit(42) }
assert_equal 42, $?.exitstatus
end

assert("Process.fork: default exit codes") do
skip "test requires unix" if ProcessTestUtil.windows?
skip "test requires wait" if not ProcessTestUtil.wait?
Process.wait Process.fork { 123 }
assert_equal 0, $?.exitstatus
Process.wait Process.fork { raise "boom" }
assert_equal 1, $?.exitstatus
end
Loading