From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 7CE0A211BE for ; Wed, 26 Dec 2018 19:57:20 +0000 (UTC) From: Eric Wong To: yahns-public@yhbt.net Subject: [PATCH 5/5] extras/exec_cgi: support Process.spawn options (e.g. RLIMIT_*) Date: Wed, 26 Dec 2018 19:57:16 +0000 Message-Id: <20181226195716.31202-6-e@80x24.org> In-Reply-To: <20181226195716.31202-1-e@80x24.org> References: <20181226195716.31202-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: These options can be useful for limiting CGI process runtime and memory usage. --- extras/exec_cgi.rb | 6 ++++-- test/test_extras_exec_cgi.rb | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/extras/exec_cgi.rb b/extras/exec_cgi.rb index babda3c..2746c09 100644 --- a/extras/exec_cgi.rb +++ b/extras/exec_cgi.rb @@ -18,7 +18,8 @@ # use Rack::Chunked # # other Rack middlewares can go here... # -# run ExecCgi.new('/path/to/cgit.cgi') # cgit: https://git.zx2c4.com/cgit/ +# # cgit: https://git.zx2c4.com/cgit/ +# run ExecCgi.new('/path/to/cgit.cgi', opts) # class ExecCgi class MyIO @@ -91,6 +92,7 @@ def initialize(*args) first[0] == ?/ or args[0] = ::File.expand_path(first) File.executable?(args[0]) or raise ArgumentError, "#{args[0]} is not executable" + @opts = Hash === args[-1] ? args.pop : {} end # Calls the app @@ -104,7 +106,7 @@ def call(env) io = MyIO.new(rd) errbody = io errbody.my_pid = spawn(cgi_env.merge!(@env), *@args, - out: wr, close_others: true) + @opts.merge(out: wr, close_others: true)) wr.close begin diff --git a/test/test_extras_exec_cgi.rb b/test/test_extras_exec_cgi.rb index c6c5ad6..48d62b7 100644 --- a/test/test_extras_exec_cgi.rb +++ b/test/test_extras_exec_cgi.rb @@ -179,4 +179,27 @@ def _blocked_zombie(block_on, rtype) c.close if c quit_wait(pid) end + + def test_rlimit_options + err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] + tout = 1 + opts = { rlimit_cpu: tout, rlimit_core: 0 } + cmd = [ '/bin/sh', '-c', 'while :; do :;done', opts ] + pid = mkserver(cfg) do + require './extras/exec_cgi' + cfg.instance_eval do + stack = Rack::ContentLength.new(Rack::Chunked.new(ExecCgi.new(*cmd))) + app(:rack, stack) { listen "#{host}:#{port}" } + stderr_path err.path + worker_processes 1 + end + end + c = get_tcp_client(host, port) + c.write "GET / HTTP/1.0\r\n\r\n" + assert_same c, c.wait(tout + 0.5) + assert_match %r{ 500 Internal Server Error\b}, c.readpartial(4096) + c.close + ensure + quit_wait(pid) + end end -- EW