class SystemUniversal

def child_program config

def child_program config
  <<-program
    # encoding: utf-8
    PIPE = STDOUT.dup
    begin
      config = Marshal.load(IO.read('#{ config }',:mode=>"rb"))
      argv = config['argv']
      env = config['env']
      cwd = config['cwd']
      stdin = config['stdin']
      stdout = config['stdout']
      stderr = config['stderr']
      Dir.chdir cwd if cwd
      env.each{|k,v| ENV[k.to_s] = v.to_s} if env
      STDIN.reopen stdin
      STDOUT.reopen stdout
      STDERR.reopen stderr
      PIPE.puts "pid: \#{ Process.pid }"
      PIPE.flush                        ### the process is ready yo!
      PIPE.close
      exec *argv
    rescue Exception => e
      PIPE.write Marshal.dump(e) rescue nil
      exit 42
    end
  program
end