sys-admin 1.7.3 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,17 +1,17 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
- require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
4
  require 'rbconfig'
5
5
 
6
- CLEAN.include("**/*.gem", "**/*.rbx", "**/*.rbc", "ruby.core")
6
+ CLEAN.include("**/*.gem", "**/*.rbx", "**/*.rbc", "ruby.core", "**/*.lock")
7
7
 
8
8
  namespace :gem do
9
9
  desc "Create the sys-uname gem"
10
10
  task :create => [:clean] do
11
11
  require 'rubygems/package'
12
- spec = eval(IO.read('sys-admin.gemspec'))
12
+ spec = Gem::Specification.load('sys-admin.gemspec')
13
13
  spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
14
- Gem::Package.build(spec, true)
14
+ Gem::Package.build(spec)
15
15
  end
16
16
 
17
17
  desc "Install the sys-uname gem"
@@ -21,27 +21,24 @@ namespace :gem do
21
21
  end
22
22
  end
23
23
 
24
- Rake::TestTask.new('test') do |t|
24
+ desc "Run the specs for the sys-admin library"
25
+ RSpec::Core::RakeTask.new(:spec) do |t|
26
+ t.pattern = 'spec/sys_admin_unix_spec.rb'
25
27
  case RbConfig::CONFIG['host_os']
26
28
  when /darwin|osx/i
27
- t.libs << 'lib/darwin'
29
+ t.rspec_opts = '-Ilib/darwin'
28
30
  when /linux/i
29
- t.libs << 'lib/linux'
31
+ t.rspec_opts = '-Ilib/linux'
30
32
  when /sunos|solaris/i
31
- t.libs << 'lib/sunos'
33
+ t.rspec_opts = '-Ilib/sunos'
32
34
  when /bsd/i
33
- t.libs << 'lib/bsd'
35
+ t.rspec_opts = '-Ilib/bsd'
34
36
  when /windows|win32|mingw|cygwin|dos/i
35
- t.libs << 'lib/windows'
37
+ t.rspec_opts = '-Ilib/windows'
38
+ t.pattern = 'spec/sys_admin_windows_spec.rb'
36
39
  else
37
- t.libs << 'lib/unix'
40
+ t.rspec_opts = '-Ilib/unix'
38
41
  end
39
-
40
- t.warning = true
41
- t.verbose = true
42
-
43
- t.libs << 'test'
44
- t.test_files = FileList['test/test_sys_admin.rb']
45
42
  end
46
43
 
47
- task :default => :test
44
+ task :default => :spec
data/lib/bsd/sys/admin.rb CHANGED
@@ -6,10 +6,9 @@ require 'rbconfig'
6
6
 
7
7
  module Sys
8
8
  class Admin
9
- private
10
-
11
9
  # :no-doc:
12
10
  BUF_MAX = 65536 # Max buffer for retry
11
+ private_constant :BUF_MAX
13
12
 
14
13
  # I'm making some aliases here to prevent potential conflicts
15
14
  attach_function :open_c, :open, [:string, :int], :int
@@ -22,8 +21,7 @@ module Sys
22
21
  attach_function :getgrnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
23
22
  attach_function :getgrgid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
24
23
 
25
- private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r, :getgrnam_r
26
- private_class_method :getgrgid_r
24
+ private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r, :getgrnam_r, :getgrgid_r
27
25
  private_class_method :open_c, :pread_c, :close_c
28
26
 
29
27
  # struct passwd from /usr/include/pwd.h
@@ -48,6 +46,8 @@ module Sys
48
46
  layout(*fields)
49
47
  end
50
48
 
49
+ private_constant :PasswdStruct
50
+
51
51
  # struct group from /usr/include/grp.h
52
52
  class GroupStruct < FFI::Struct
53
53
  layout(
@@ -58,6 +58,8 @@ module Sys
58
58
  )
59
59
  end
60
60
 
61
+ private_constant :GroupStruct
62
+
61
63
  # I'm blending the timeval struct in directly here
62
64
  class LastlogStruct < FFI::Struct
63
65
  layout(
@@ -67,7 +69,7 @@ module Sys
67
69
  )
68
70
  end
69
71
 
70
- public
72
+ private_constant :LastlogStruct
71
73
 
72
74
  # Returns the login for the current process.
73
75
  #
@@ -192,8 +194,6 @@ module Sys
192
194
  groups
193
195
  end
194
196
 
195
- private
196
-
197
197
  # Takes a GroupStruct and converts it to a Group object.
198
198
  def self.get_group_from_struct(grp)
199
199
  Group.new do |g|
@@ -204,6 +204,8 @@ module Sys
204
204
  end
205
205
  end
206
206
 
207
+ private_class_method :get_group_from_struct
208
+
207
209
  # Takes a UserStruct and converts it to a User object.
208
210
  def self.get_user_from_struct(pwd)
209
211
  user = User.new do |u|
@@ -230,6 +232,8 @@ module Sys
230
232
  user
231
233
  end
232
234
 
235
+ private_class_method :get_user_from_struct
236
+
233
237
  # Get lastlog information for the given user.
234
238
  def self.get_lastlog_info(uid)
235
239
  logfile = '/var/log/lastlog'
@@ -252,5 +256,7 @@ module Sys
252
256
 
253
257
  lastlog
254
258
  end
259
+
260
+ private_class_method :get_lastlog_info
255
261
  end
256
262
  end
@@ -5,10 +5,9 @@ require 'sys/admin/common'
5
5
 
6
6
  module Sys
7
7
  class Admin
8
- private
9
-
10
8
  # :no-doc:
11
9
  BUF_MAX = 65536 # Max buf size for retry.
10
+ private_constant :BUF_MAX
12
11
 
13
12
  attach_function :getlogin_r, [:pointer, :int], :int
14
13
  attach_function :getpwnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
@@ -17,8 +16,8 @@ module Sys
17
16
  attach_function :getgrgid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
18
17
  attach_function :getlastlogx, [:long, :pointer], :pointer
19
18
 
20
- private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r, :getgrnam_r
21
- private_class_method :getgrgid_r, :getlastlogx
19
+ private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r
20
+ private_class_method :getgrnam_r, :getgrgid_r, :getlastlogx
22
21
 
23
22
  # struct passwd from /usr/include/pwd.h
24
23
  class PasswdStruct < FFI::Struct
@@ -36,6 +35,8 @@ module Sys
36
35
  )
37
36
  end
38
37
 
38
+ private_constant :PasswdStruct
39
+
39
40
  # struct group from /usr/include/grp.h
40
41
  class GroupStruct < FFI::Struct
41
42
  layout(
@@ -46,6 +47,8 @@ module Sys
46
47
  )
47
48
  end
48
49
 
50
+ private_constant :GroupStruct
51
+
49
52
  # I'm blending the timeval struct in directly here
50
53
  class LastlogxStruct < FFI::Struct
51
54
  layout(
@@ -56,7 +59,7 @@ module Sys
56
59
  )
57
60
  end
58
61
 
59
- public
62
+ private_constant :LastlogxStruct
60
63
 
61
64
  # Returns the login for the current process.
62
65
  #
@@ -185,8 +188,6 @@ module Sys
185
188
  groups
186
189
  end
187
190
 
188
- private
189
-
190
191
  # Takes a GroupStruct and converts it to a Group object.
191
192
  def self.get_group_from_struct(grp)
192
193
  Group.new do |g|
@@ -197,6 +198,8 @@ module Sys
197
198
  end
198
199
  end
199
200
 
201
+ private_class_method :get_group_from_struct
202
+
200
203
  # Takes a UserStruct and converts it to a User object.
201
204
  def self.get_user_from_struct(pwd)
202
205
  user = User.new do |u|
@@ -223,6 +226,8 @@ module Sys
223
226
  user
224
227
  end
225
228
 
229
+ private_class_method :get_user_from_struct
230
+
226
231
  # Gets lastlog information for the given user.
227
232
  def self.get_lastlog_info(uid)
228
233
  lastlog = LastlogxStruct.new
@@ -233,5 +238,7 @@ module Sys
233
238
 
234
239
  ptr.null? ? nil : lastlog
235
240
  end
241
+
242
+ private_class_method :get_lastlog_info
236
243
  end
237
244
  end
@@ -5,17 +5,15 @@ require 'sys/admin/common'
5
5
 
6
6
  module Sys
7
7
  class Admin
8
- private
9
-
10
8
  # :no-doc:
11
9
  BUF_MAX = 65536 # Absolute max buffer size for retry attempts.
10
+ private_constant :BUF_MAX
12
11
 
13
12
  # I'm making some aliases here to prevent potential conflicts
14
13
  attach_function :open_c, :open, [:string, :int], :int
15
14
  attach_function :pread_c, :pread, [:int, :pointer, :size_t, :off_t], :size_t
16
15
  attach_function :close_c, :close, [:int], :int
17
16
 
18
- attach_function :getlogin_r, [:pointer, :size_t], :int
19
17
  attach_function :getpwnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
20
18
  attach_function :getpwuid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
21
19
  attach_function :getpwent_r, [:pointer, :pointer, :size_t, :pointer], :int
@@ -23,7 +21,6 @@ module Sys
23
21
  attach_function :getgrnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
24
22
  attach_function :getgrgid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
25
23
 
26
- private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r, :getpwent_r
27
24
  private_class_method :getgrent_r, :getgrnam_r, :getgrgid_r
28
25
  private_class_method :open_c, :pread_c, :close_c
29
26
 
@@ -40,6 +37,8 @@ module Sys
40
37
  )
41
38
  end
42
39
 
40
+ private_constant :PasswdStruct
41
+
43
42
  # struct group from /usr/include/grp.h
44
43
  class GroupStruct < FFI::Struct
45
44
  layout(
@@ -50,6 +49,8 @@ module Sys
50
49
  )
51
50
  end
52
51
 
52
+ private_constant :GroupStruct
53
+
53
54
  # I'm blending the timeval struct in directly here
54
55
  class LastlogStruct < FFI::Struct
55
56
  layout(
@@ -59,18 +60,12 @@ module Sys
59
60
  )
60
61
  end
61
62
 
62
- public
63
+ private_constant :LastlogStruct
63
64
 
64
65
  # Returns the login for the current process.
65
66
  #
66
67
  def self.get_login
67
- buf = FFI::MemoryPointer.new(:char, 256)
68
-
69
- if getlogin_r(buf, buf.size) != 0
70
- raise Error, "getlogin_r function failed: " + strerror(FFI.errno)
71
- end
72
-
73
- buf.read_string
68
+ get_user(geteuid()).name
74
69
  end
75
70
 
76
71
  # Returns a User object for the given name or uid. Raises an error
@@ -204,8 +199,6 @@ module Sys
204
199
  groups
205
200
  end
206
201
 
207
- private
208
-
209
202
  # Takes a GroupStruct and converts it to a Group object.
210
203
  def self.get_group_from_struct(grp)
211
204
  Group.new do |g|
@@ -216,6 +209,8 @@ module Sys
216
209
  end
217
210
  end
218
211
 
212
+ private_class_method :get_group_from_struct
213
+
219
214
  # Takes a UserStruct and converts it to a User object.
220
215
  def self.get_user_from_struct(pwd)
221
216
  user = User.new do |u|
@@ -242,6 +237,8 @@ module Sys
242
237
  user
243
238
  end
244
239
 
240
+ private_class_method :get_user_from_struct
241
+
245
242
  # Note: it seems that Linux, or at least Ubuntu, does not track logins
246
243
  # via GDM (Gnome Display Manager) for some reason, so this may not return
247
244
  # anything useful.
@@ -269,5 +266,7 @@ module Sys
269
266
 
270
267
  lastlog
271
268
  end
269
+
270
+ private_class_method :get_lastlog_info
272
271
  end
273
272
  end
@@ -5,10 +5,9 @@ require 'sys/admin/common'
5
5
 
6
6
  module Sys
7
7
  class Admin
8
- private
9
-
10
8
  # :no-doc:
11
9
  BUF_MAX = 65536 # Max buffer size for retry.
10
+ private_constant :BUF_MAX
12
11
 
13
12
  # I'm making some aliases here to prevent potential conflicts
14
13
  attach_function :open_c, :open, [:string, :int], :int
@@ -42,6 +41,8 @@ module Sys
42
41
  )
43
42
  end
44
43
 
44
+ private_constant :PasswdStruct
45
+
45
46
  # struct group from /usr/include/grp.h
46
47
  class GroupStruct < FFI::Struct
47
48
  layout(
@@ -52,6 +53,8 @@ module Sys
52
53
  )
53
54
  end
54
55
 
56
+ private_constant :GroupStruct
57
+
55
58
  # I'm blending the timeval struct in directly here
56
59
  class LastlogStruct < FFI::Struct
57
60
  layout(
@@ -61,7 +64,7 @@ module Sys
61
64
  )
62
65
  end
63
66
 
64
- public
67
+ private_constant :LastlogStruct
65
68
 
66
69
  # Returns the login for the current process.
67
70
  #
@@ -194,8 +197,6 @@ module Sys
194
197
  groups
195
198
  end
196
199
 
197
- private
198
-
199
200
  # Takes a GroupStruct and converts it to a Group object.
200
201
  def self.get_group_from_struct(grp)
201
202
  Group.new do |g|
@@ -206,6 +207,8 @@ module Sys
206
207
  end
207
208
  end
208
209
 
210
+ private_class_method :get_group_from_struct
211
+
209
212
  # Takes a UserStruct and converts it to a User object.
210
213
  def self.get_user_from_struct(pwd)
211
214
  user = User.new do |u|
@@ -232,6 +235,8 @@ module Sys
232
235
  user
233
236
  end
234
237
 
238
+ private_class_method :get_use_from_struct
239
+
235
240
  # The use of pread was necessary here because it's a sparse file. Note
236
241
  # also that while Solaris supports the getuserattr function, it doesn't
237
242
  # appear to store anything regarding login information.
@@ -257,5 +262,7 @@ module Sys
257
262
 
258
263
  lastlog
259
264
  end
265
+
266
+ private_class_method :get_lastlog_info
260
267
  end
261
268
  end
@@ -9,12 +9,11 @@ module Sys
9
9
  extend FFI::Library
10
10
  ffi_lib FFI::Library::LIBC
11
11
 
12
- private
13
-
14
12
  attach_function :strerror, [:int], :string
15
13
 
16
14
  attach_function :getlogin, [], :string
17
15
  attach_function :getuid, [], :long
16
+ attach_function :geteuid, [], :long
18
17
  attach_function :getpwnam, [:string], :pointer
19
18
  attach_function :getpwuid, [:long], :pointer
20
19
  attach_function :getpwent, [], :pointer
@@ -27,12 +26,10 @@ module Sys
27
26
  attach_function :endgrent, [], :void
28
27
  attach_function :setgrent, [], :void
29
28
 
30
- private_class_method :getlogin, :getuid, :getpwnam, :getpwuid, :getpwent
31
- private_class_method :setpwent, :endpwent, :getgrgid, :getgrnam
29
+ private_class_method :getlogin, :getuid, :geteuid, :getpwnam, :getpwuid
30
+ private_class_method :getpwent, :setpwent, :endpwent, :getgrgid, :getgrnam
32
31
  private_class_method :getgrent, :endgrent, :setgrent, :strerror
33
32
 
34
- public
35
-
36
33
  # Error typically raised if any of the Sys::Admin methods fail.
37
34
  class Error < StandardError; end
38
35
 
data/lib/sys/admin.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Sys
2
2
  class Admin
3
3
  # The version of the sys-admin library.
4
- VERSION = '1.7.3'.freeze
4
+ VERSION = '1.8.0'.freeze
5
5
  end
6
6
  end
7
7
 
@@ -5,8 +5,6 @@ require 'sys/admin/common'
5
5
 
6
6
  module Sys
7
7
  class Admin
8
- private
9
-
10
8
  class PasswdStruct < FFI::Struct
11
9
  layout(
12
10
  :pw_name, :string,
@@ -19,6 +17,8 @@ module Sys
19
17
  )
20
18
  end
21
19
 
20
+ private_constant :PasswdStruct
21
+
22
22
  class GroupStruct < FFI::Struct
23
23
  layout(
24
24
  :gr_name, :string,
@@ -28,7 +28,7 @@ module Sys
28
28
  )
29
29
  end
30
30
 
31
- public
31
+ private_constant :GroupStruct
32
32
 
33
33
  # Returns the login for the current process.
34
34
  #
@@ -133,8 +133,6 @@ module Sys
133
133
  groups
134
134
  end
135
135
 
136
- private
137
-
138
136
  # Takes a GroupStruct and converts it to a Group object.
139
137
  def self.get_group_from_struct(grp)
140
138
  Group.new do |g|
@@ -145,6 +143,8 @@ module Sys
145
143
  end
146
144
  end
147
145
 
146
+ private_class_method :get_group_from_struct
147
+
148
148
  # Takes a UserStruct and converts it to a User object.
149
149
  def self.get_user_from_struct(pwd)
150
150
  user = User.new do |u|
@@ -159,5 +159,7 @@ module Sys
159
159
 
160
160
  user
161
161
  end
162
+
163
+ private_class_method :get_user_from_struct
162
164
  end
163
165
  end