sysctl 0.0.1.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
@@ -0,0 +1,69 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'sysctl/extensions'
21
+
22
+ module Sysctl
23
+ extend FFI::Library
24
+ ffi_lib 'c'
25
+
26
+ attach_function :sysctl, [ :pointer, :size_t, :pointer, :pointer, :pointer, :size_t ], :int
27
+
28
+ class << self
29
+ alias __c_sysctl__ sysctl
30
+ private :__c_sysctl__
31
+ end
32
+
33
+ def self.sysctl (ctl)
34
+ mib = CTL_NAMES[ctl]
35
+ return unless mib
36
+
37
+ type = mib.type.is_a?(Symbol) ? FFI.find_type(mib.type) : mib.type
38
+
39
+ if type.is_a?(FFI::Type::Builtin) and type.name.downcase == :string
40
+ tvp = FFI::MemoryPointer.from_string("\0" * 8192)
41
+ tvs = FFI::MemoryPointer.new(:int).write_int(tvp.size)
42
+ else
43
+ tvp = FFI::MemoryPointer.new(type)
44
+ tvs = FFI::MemoryPointer.new(:int).write_int(type.size)
45
+ end
46
+
47
+ mip = FFI::MemoryPointer.new(:int, mib.mib.size).write_array_of_int(mib.mib)
48
+
49
+ raise Errno.const_get(Errno.constants[FFI.errno]) if __c_sysctl__(mip, mib.mib.size, tvp, tvs, nil, 0) != 0
50
+
51
+ tvp.typecast(type)
52
+ end
53
+ end
54
+
55
+ def sysctl (name)
56
+ Sysctl.sysctl(name)
57
+ end
58
+
59
+ module Kernel
60
+ def self.sysctl (name)
61
+ Sysctl.sysctl(name)
62
+ end
63
+ end
64
+
65
+ begin
66
+ require "sysctl/platform/#{RUBY_PLATFORM.split('-').last}"
67
+ rescue LoadError
68
+ raise "Platform not supported."
69
+ end
@@ -0,0 +1,74 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'ffi'
21
+ require 'memoized'
22
+
23
+ module FFI
24
+ module Library
25
+ def has_function? (sym, libraries=[])
26
+ if libraries.empty?
27
+ libraries << FFI::Library::LIBC
28
+ end
29
+
30
+ libraries.any? {|lib|
31
+ DynamicLibrary.new(lib, 0).find_function(sym.to_s) rescue nil
32
+ }
33
+ end
34
+
35
+ def attach_function! (*args, &block)
36
+ begin
37
+ attach_function(*args, &block)
38
+ rescue Exception => e
39
+ false
40
+ end
41
+ end
42
+ end
43
+
44
+ class Type::Builtin
45
+ memoize
46
+ def name
47
+ Type::Builtin.constants.find {|name|
48
+ Type::Builtin.const_get(name) == self
49
+ }
50
+ end
51
+ end
52
+
53
+ class Pointer
54
+ def typecast (type)
55
+ if type.is_a?(Symbol)
56
+ type = FFI.find_type(type)
57
+ end
58
+
59
+ if type.is_a?(Class) and type.ancestors.member?(FFI::Struct)
60
+ type.new(self)
61
+ elsif type.is_a?(Type::Builtin)
62
+ if type.name == :STRING
63
+ read_string
64
+ else
65
+ send "read_#{type.name.downcase}"
66
+ end
67
+ else
68
+ ArgumentError.new "You have to pass a Struct, a Builtin type or a Symbol"
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ FFI.find_type(:size_t) rescue FFI.typedef(:ulong, :size_t)
@@ -0,0 +1,20 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'sysctl/tree_parser'
@@ -0,0 +1,20 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'sysctl/tree_parser'
@@ -0,0 +1,20 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'sysctl/tree_parser'
@@ -0,0 +1,100 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'sysctl/tree_parser'
21
+
22
+ module Sysctl
23
+ FFI.typedef(:uint32, :fixpt_t)
24
+
25
+ class LoadAVG < FFI::Struct
26
+ layout \
27
+ :ldavg, [:fixpt_t, 3],
28
+ :fscale, :long
29
+ end
30
+
31
+ CTL_NAMES = CTLRoot.new([
32
+ ['fs', 3, :node,
33
+ [
34
+ ['posix', nil, :node, [
35
+ ['setuid', 1]
36
+ ]]
37
+ ]
38
+ ],
39
+ ['hw', 6, :node,
40
+ [
41
+ ['machine', 1, :string],
42
+ ['model', 2, :string],
43
+ ['ncpu', 3],
44
+ ['byteorder', 4],
45
+ ['pagesize', 7],
46
+ ['disknames', 8, :string],
47
+ ['diskstats', 9, :struct],
48
+ ['diskcount', 10],
49
+ ['sensors', 11, :node],
50
+ ['cpuspeed', 12],
51
+ ['setperf', 13],
52
+ ['vendor', 14, :string],
53
+ ['product', 15, :string],
54
+ ['version', 16, :string],
55
+ ['serialno', 17, :string],
56
+ ['uuid', 18, :string],
57
+ ['physmem', 10],
58
+ ['usermem', 20],
59
+ ['ncpufound', 21]
60
+ ]
61
+ ],
62
+ ['user', 8, :node,
63
+ [
64
+ ['cs_path', 1, :string],
65
+ ['bc_base_max', 2],
66
+ ['bc_dim_max', 3],
67
+ ['bc_scale_max', 4],
68
+ ['bc_string_max', 5],
69
+ ['coll_weights_max', 6],
70
+ ['expr_nest_max', 7],
71
+ ['line_max', 8],
72
+ ['re_dup_max', 9],
73
+ ['posix2_version', 10],
74
+ ['posix2_c_bind', 11],
75
+ ['posix2_c_dev', 12],
76
+ ['posix2_char_term', 13],
77
+ ['posix2_fort_dev', 14],
78
+ ['posix2_fort_run', 15],
79
+ ['posix2_localedef', 16],
80
+ ['posix2_sw_dev', 17],
81
+ ['posix2_upe', 18],
82
+ ['stream_max', 19],
83
+ ['tzname_max', 20]
84
+ ]
85
+ ],
86
+ ['ddb', 9, :node,
87
+ [
88
+ ['radix', 1],
89
+ ['maxwidth', 2],
90
+ ['maxline', 3],
91
+ ['tabstop', 4],
92
+ ['panic', 5],
93
+ ['console', 6],
94
+ ['log', 7],
95
+ ['trigger', 8],
96
+ ['maxid', 9]
97
+ ]
98
+ ]
99
+ ])
100
+ end
@@ -0,0 +1,106 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of sysctl.
5
+ #
6
+ # sysctl is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # sysctl is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with sysctl. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Sysctl
21
+ class MIB < Struct.new(:mib, :type)
22
+ def initialize (mib, type=nil)
23
+ self.mib = mib
24
+ self.type = type
25
+ end
26
+
27
+ alias __set_type__ type=
28
+ def type= (type)
29
+ self.__set_type__(type || :int)
30
+ end
31
+
32
+ def << (mib)
33
+ self.mib << mib
34
+ end
35
+ end
36
+
37
+ class CTLRoot
38
+ def initialize (children=nil)
39
+ @children = {}
40
+
41
+ if children.is_a?(Array)
42
+ children.each {|child|
43
+ self << (child.is_a?(CTLNode) ? child : CTLNode.new(*child))
44
+ }
45
+ end
46
+ end
47
+
48
+ def << (node)
49
+ return unless node.is_a?(CTLNode)
50
+ @children[node.name] = node
51
+ self
52
+ end
53
+
54
+ def children
55
+ @children
56
+ end
57
+
58
+ def [] (name)
59
+ tree = name.downcase.split('.')
60
+
61
+ mib, * = tree.inject([MIB.new([]), self.children]) {|(mib, node), point|
62
+ node = node[point]
63
+ mib << node.value
64
+ mib.type = node.type
65
+ [mib, node]
66
+ }
67
+
68
+ mib.mib.compact!
69
+ mib
70
+ rescue
71
+ nil
72
+ end
73
+ end
74
+
75
+ class CTLNode
76
+ attr_reader :name, :value, :type
77
+
78
+ def initialize (name, value, type=:int, children=nil)
79
+ @name, @value, @type = name, value, type || :int
80
+
81
+ if type == :node
82
+ @children = {}
83
+
84
+ (class << self; self; end).send(:define_method, :<<) {|node|
85
+ return unless node.is_a?(CTLNode)
86
+ @children[node.name] = node
87
+ self
88
+ }
89
+
90
+ (class << self; self; end).send(:define_method, :children) {
91
+ @children.dup
92
+ }
93
+
94
+ (class << self; self; end).send(:define_method, :[]) {|name|
95
+ @children[name]
96
+ }
97
+
98
+ if children.is_a?(Array)
99
+ children.each {|child|
100
+ self << (child.is_a?(CTLNode) ? child : CTLNode.new(*child))
101
+ }
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sysctl
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ - alpha1
10
+ version: 0.0.1.alpha1
11
+ platform: ruby
12
+ authors:
13
+ - shura
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-08 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ffi
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: memoized
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ description: A wrapper around sysctl to make its use cool in OpenBSD too
48
+ email: shura1991@gmail.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - lib/sysctl.rb
57
+ - lib/sysctl/extensions.rb
58
+ - lib/sysctl/platform/openbsd.rb
59
+ - lib/sysctl/platform/darwin.rb
60
+ - lib/sysctl/platform/linux.rb
61
+ - lib/sysctl/platform/freebsd.rb
62
+ - lib/sysctl/tree_parser.rb
63
+ - lib/new.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/shurizzle/ruby-sysctl
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">"
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 1
88
+ - 3
89
+ - 1
90
+ version: 1.3.1
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.7
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: A wrapper around sysctl to make its use cool in OpenBSD too
98
+ test_files: []
99
+