syscalls 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ = syscalls
2
+
3
+ * http://github.com/jdleesmiller/syscalls_ruby
4
+
5
+ == SYNOPSIS
6
+
7
+ An extension that lists the system's supported syscalls (e.g. SYS_open,
8
+ SYS_write, etc.)}. These are read from <tt>sys/syscall.h</tt> when the gem is
9
+ built.
10
+
11
+ So far this has been tested on a couple of Linux machines; it should in
12
+ principle work on other *nixes, but I haven't tested it.
13
+
14
+ === Usage
15
+
16
+ require 'rubygems'
17
+ require 'syscalls'
18
+
19
+ include Syscalls
20
+ puts Process.pid #=> (your process's pid)
21
+ puts syscall(SYS_getpid) #=> (your process's pid)
22
+
23
+ == REQUIREMENTS
24
+
25
+ Tested on:
26
+ * ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux] (Debian squeeze)
27
+ * ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux] (CentOS 5.5)
28
+ * ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux] (CentOS 5.5)
29
+
30
+ == INSTALLATION
31
+
32
+ sudo gem install syscalls
33
+
34
+ == LICENSE
35
+
36
+ (The MIT License)
37
+
38
+ Copyright (c) 2011 John Lees-Miller
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58
+
data/ext/extconf.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'mkmf'
2
+ require 'erb'
3
+
4
+ #
5
+ # sys/syscall.h comes from glibc; it lists the SYS_ constants; see
6
+ # http://www.delorie.com/gnu/docs/glibc/libc_556.html
7
+ #
8
+ raise "cannot find sys/syscall.h" unless have_header('sys/syscall.h')
9
+
10
+ #
11
+ # use "gcc -E -dD" on a file that includes sys/syscall.h to get the preprocessor
12
+ # output (including defines, due to -dD); this gives us a list of SYS_call
13
+ # constants
14
+ #
15
+ # the reasons for not just reading in syscall.h directly are that (1) we don't
16
+ # always know where it is, and (2) some of the constants are conditioned out at
17
+ # compile time (e.g. 32-bit vs. 64-bit); running it through the gcc preprocessor
18
+ # solves these problems
19
+ #
20
+ # the call to RbConfig::expand is based on cc_command in mkmf.rb from 1.9.2p290;
21
+ # we do this to ensure that we use the right gcc flags
22
+ #
23
+ conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote,
24
+ 'srcdir' => $srcdir.quote,
25
+ 'arch_hdrdir' => "#$arch_hdrdir",
26
+ 'top_srcdir' => ($top_srcdir || '').quote)
27
+ cmd = RbConfig::expand(
28
+ "$(CC) #$INCFLAGS #$CPPFLAGS #$CFLAGS #$ARCH_FLAG -E -dD -", conf)
29
+ lines = IO.popen(cmd, 'r+') {|io|
30
+ io.puts "#include <sys/syscall.h>"
31
+ io.close_write
32
+ io.readlines
33
+ }
34
+
35
+ syscall_names = lines.map {|l| $1 if l =~ /#define\s+(SYS_\S+)\s+/}.compact
36
+
37
+ #
38
+ # generate the extension
39
+ #
40
+ File.open('syscalls.c', 'w') do |f|
41
+ template = ERB.new(<<EOF)
42
+ /* THIS FILE IS GENERATED BY extconf.rb */
43
+ #include <ruby.h>
44
+ #include <sys/syscall.h>
45
+
46
+ void
47
+ Init_syscalls(void)
48
+ {
49
+ VALUE mod = rb_define_module("Syscalls");
50
+ <% syscall_names.each do |name| %>
51
+ rb_define_const(mod, "<%= name %>", INT2FIX(<%= name %>));
52
+ <% end %>
53
+ }
54
+ EOF
55
+ f.puts template.result(binding)
56
+ end
57
+
58
+ create_makefile('syscalls')
data/lib/syscalls.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'syscalls.so'
2
+ require 'syscalls/version'
3
+
4
+ #
5
+ # System call constants (e.g. SYS_open).
6
+ #
7
+ # The constant names and values in this module are system-dependent. They are
8
+ # read in from <tt>sys/syscall.h</tt> when this gem is built.
9
+ #
10
+ # The {SYS_NUMBER} and {SYS_NAME} hashes provide an easy way of converting from
11
+ # names to numbers.
12
+ #
13
+ module Syscalls
14
+ #
15
+ # Hash from system call names (as symbols) to system call numbers.
16
+ #
17
+ SYS_NUMBER = Hash[self.constants.grep(/SYS_/).map{|c|
18
+ [c.to_sym, self.const_get(c)]}]
19
+
20
+ #
21
+ # Hash from system call numbers to system call names (as symbols).
22
+ #
23
+ SYS_NAME = SYS_NUMBER.invert
24
+ end
@@ -0,0 +1,6 @@
1
+ module Syscalls
2
+ VERSION_MAJOR = 0
3
+ VERSION_MINOR = 0
4
+ VERSION_PATCH = 1
5
+ VERSION = [VERSION_MAJOR,VERSION_MINOR,VERSION_PATCH].join('.')
6
+ end
@@ -0,0 +1,16 @@
1
+ require 'syscalls'
2
+ require 'test/unit'
3
+
4
+ class TestSyscalls < Test::Unit::TestCase
5
+ include Syscalls
6
+
7
+ def test_syscalls
8
+ assert_equal SYS_open, SYS_NUMBER[:SYS_open]
9
+ assert_equal :SYS_open, SYS_NAME[SYS_open]
10
+ end
11
+
12
+ def test_getpid
13
+ assert_equal Process.pid, syscall(SYS_getpid)
14
+ end
15
+ end
16
+
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syscalls
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Lees-Miller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-23 00:00:00.000000000 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: An extension that lists the system's supported syscalls (e.g. SYS_open,
16
+ SYS_write, etc.)
17
+ email:
18
+ - jdleesmiller@gmail.com
19
+ executables: []
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/syscalls.rb
26
+ - lib/syscalls/version.rb
27
+ - README.rdoc
28
+ - test/syscalls_test.rb
29
+ - ext/extconf.rb
30
+ has_rdoc: true
31
+ homepage: https://github.com/jdleesmiller/syscalls_ruby
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main
36
+ - README.rdoc
37
+ - --title
38
+ - syscalls-0.0.1 Documentation
39
+ require_paths:
40
+ - lib
41
+ - ext
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project: ''
56
+ rubygems_version: 1.6.2
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Constants for syscall numbers
60
+ test_files:
61
+ - test/syscalls_test.rb