forkexec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Adam Kliment
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = forkexec
2
+
3
+ * It's usefull for timeout actions running outside ruby eg. external system calls.
4
+ * Executes given block in new forked process.
5
+ * After timeout the forked process is killed and Timeout::Error is raised.
6
+ * Return value of forked prcess is given through tmpfs in /dev/shm as serialized YAML object.
7
+ * It's prototype = no tests, no doc, no warranty !
8
+
9
+ ==example
10
+ > require 'forkexec'
11
+ => true
12
+
13
+ > ForkExec.timeout(1){ exec "sleep 10"}
14
+ Timeout::Error: PID 11185 timeouted
15
+ from ./forkexec.rb:28:in `timeout'
16
+ from (irb):2
17
+ from /usr/lib/ruby/1.8/x86_64-linux/rbconfig.rb:168
18
+
19
+ > ForkExec.timeout(1){ "foo bar" if true}
20
+ => "foo bar"
21
+
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2010 Adam Kliment. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "forkexec"
8
+ gem.summary = "Executes given block in new forked process with timeout"
9
+ gem.description = "Executes given block in new forked process. After timeout process is killed. Return value of forked prcess is given through tmpfs in /dev/shm as serialized YAML object."
10
+ gem.email = "adam.kliment@virtualmaster.cz"
11
+ gem.homepage = "http://github.com/n1k/forkexec"
12
+ gem.authors = ["Adam Kliment"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "forkexec #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/forkexec.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "rubygems"
2
+ require "yaml"
3
+ require "timeout"
4
+
5
+ module ForkExec
6
+ def self.timeout timeout
7
+ raise "Tmpfs in /dev/shm does not exist! Create it..." if not File.exist?("/dev/shm")
8
+
9
+ shm = "/dev/shm/forkexec"
10
+
11
+ Dir.mkdir(shm) if not File.exist?(shm)
12
+
13
+ pid = fork do
14
+ output = yield
15
+ File.open(File.join("/dev/shm/forkexec",Process.pid.to_s),"w"){|f| f.write(output.to_yaml)}
16
+ exit! 0
17
+ end
18
+
19
+ result = nil
20
+
21
+ begin
22
+ Timeout::timeout(timeout) do
23
+ Process.waitpid pid
24
+ result = YAML::load_file(File.join(shm,pid.to_s))
25
+ end
26
+ rescue Timeout::Error
27
+ Process.kill(9, pid)
28
+ Process.waitpid pid
29
+ raise Timeout::Error, "PID #{pid} timeouted"
30
+ end
31
+
32
+ shm_file = File.join("/dev/shm/forkexec/",pid.to_s)
33
+ File.delete(shm_file) if File.exist?(shm_file)
34
+
35
+ return result
36
+ end
37
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'forkexec'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestForkexec < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forkexec
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Adam Kliment
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-08 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Executes given block in new forked process. After timeout process is killed. Return value of forked prcess is given through tmpfs in /dev/shm as serialized YAML object.
23
+ email: adam.kliment@virtualmaster.cz
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - .document
33
+ - .gitignore
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - VERSION
38
+ - lib/forkexec.rb
39
+ - test/helper.rb
40
+ - test/test_forkexec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/n1k/forkexec
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Executes given block in new forked process with timeout
75
+ test_files:
76
+ - test/test_forkexec.rb
77
+ - test/helper.rb