A subprocess command builder/runner with a focus on enforcing limits to runtime, memory use, CPU use, and command output.
Add this line to your application's Gemfile:
gem 'comlim'And then execute:
$ bundle
Or install it yourself as:
$ gem install comlim
(Then get upset when you realise I haven't pushed this to the masses yet).
Limit stdout and stderr to a total of 1KiB.
QuietRunner = Comlim.output(1024)Limit it to 32MiB of memory.
MemLimitedRunner = QuietRunner.memory(1024 * 1024 * 32)2 seconds of CPU should be plenty:
CpuMemLimitedRunner = MemLimitedRunner.cputime(2)4 seconds of runtime, followed by summary execution:
RestrictedRunner = CpuMemLimitedRunner.walltime(4)Use it to run inline Ruby:
RubyRunner = RestrictedRunner.command('ruby').arg('-e')And finally use it to run some potentially runaway process:
result = RubyRunner.arg('loop { puts "SMASH THE STATE!" }')
pp result
# => #<struct Comlim::Result
# => pid=95544,
# => status=#<Process::Status: pid 95544 exit 1>,
# => exitstatus=1,
# => exitreason=Comlim::ExitReason::OutputExceeded,
# => stdout=
# => "SMASH THE STATE!\n" +
# => "SMASH THE STATE!\n" +
# => ...
# => [SUBVERSIVE MESSAGE REDACTED]
# => stderr="",
# => walltime=0.1225816321093589>Bwahahaha. Ahem.
Eh, it probably mostly works on my machine. I guess?
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/comlim.
The gem is available as open source under the terms of the MIT License.