sys-admin 1.7.4 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
  #
@@ -77,8 +80,12 @@ module Sys
77
80
  #
78
81
  # Sys::Admin.get_user('joe')
79
82
  # Sys::Admin.get_user(501)
83
+ # Sys::Admin.get_user('joe', :lastlog => false) # Less info but faster
80
84
  #
81
- def self.get_user(uid)
85
+ # Set the :lastlog option to false if you want to ignore lastlog
86
+ # information and speed this method up considerably.
87
+ #
88
+ def self.get_user(uid, options = {})
82
89
  buf = FFI::MemoryPointer.new(:char, 1024)
83
90
  pbuf = FFI::MemoryPointer.new(PasswdStruct)
84
91
  temp = PasswdStruct.new
@@ -100,7 +107,7 @@ module Sys
100
107
  end
101
108
 
102
109
  pwd = PasswdStruct.new(ptr)
103
- get_user_from_struct(pwd)
110
+ get_user_from_struct(pwd, options)
104
111
  end
105
112
 
106
113
  # Returns a Group object for the given name or uid. Raises an error
@@ -145,11 +152,19 @@ module Sys
145
152
 
146
153
  # Returns an array of User objects for each user on the system.
147
154
  #
155
+ # Examples:
156
+ #
157
+ # Sys::Admin.users
158
+ # Sys::Admin.users(:lastlog => false) # Less info but faster
159
+ #
160
+ # Note that on Darwin this method can be very slow. If you want to
161
+ # speed it up considerably by ignoring lastlog information then set
162
+ # the :lastlog option to false as part of the +options+ hash.
148
163
  #--
149
164
  # This method is somewhat slow on OSX because of the call to get
150
165
  # lastlog information. I'm not sure why.
151
166
  #
152
- def self.users
167
+ def self.users(options = {})
153
168
  users = []
154
169
 
155
170
  begin
@@ -157,7 +172,7 @@ module Sys
157
172
 
158
173
  until (ptr = getpwent()).null?
159
174
  pwd = PasswdStruct.new(ptr)
160
- users << get_user_from_struct(pwd)
175
+ users << get_user_from_struct(pwd, options)
161
176
  end
162
177
  ensure
163
178
  endpwent()
@@ -185,8 +200,6 @@ module Sys
185
200
  groups
186
201
  end
187
202
 
188
- private
189
-
190
203
  # Takes a GroupStruct and converts it to a Group object.
191
204
  def self.get_group_from_struct(grp)
192
205
  Group.new do |g|
@@ -197,8 +210,10 @@ module Sys
197
210
  end
198
211
  end
199
212
 
213
+ private_class_method :get_group_from_struct
214
+
200
215
  # Takes a UserStruct and converts it to a User object.
201
- def self.get_user_from_struct(pwd)
216
+ def self.get_user_from_struct(pwd, options)
202
217
  user = User.new do |u|
203
218
  u.name = pwd[:pw_name]
204
219
  u.passwd = pwd[:pw_passwd]
@@ -212,17 +227,21 @@ module Sys
212
227
  u.expire = Time.at(pwd[:pw_expire])
213
228
  end
214
229
 
215
- log = get_lastlog_info(user.uid)
230
+ unless options[:lastlog] == false
231
+ log = get_lastlog_info(user.uid)
216
232
 
217
- if log
218
- user.login_time = Time.at(log[:tv_sec])
219
- user.login_device = log[:ll_line].to_s
220
- user.login_host = log[:ll_host].to_s
233
+ if log
234
+ user.login_time = Time.at(log[:tv_sec])
235
+ user.login_device = log[:ll_line].to_s
236
+ user.login_host = log[:ll_host].to_s
237
+ end
221
238
  end
222
239
 
223
240
  user
224
241
  end
225
242
 
243
+ private_class_method :get_user_from_struct
244
+
226
245
  # Gets lastlog information for the given user.
227
246
  def self.get_lastlog_info(uid)
228
247
  lastlog = LastlogxStruct.new
@@ -233,5 +252,7 @@ module Sys
233
252
 
234
253
  ptr.null? ? nil : lastlog
235
254
  end
255
+
256
+ private_class_method :get_lastlog_info
236
257
  end
237
258
  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.4'.freeze
4
+ VERSION = '1.8.1'.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