win32-service 1.0.1 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e27f59203c2e9c16dea5335981498b12a350f3b627e9991145fc51cccc7e1d8f
4
- data.tar.gz: d692ad6fbbc89954be51465f0a5af4906554e1dcc72d4c30748e71156a07ad8c
3
+ metadata.gz: 075b8e2ab771fc4ebc29a9aceb821964ab09a269818d29c817cc5a6e2f7a7963
4
+ data.tar.gz: '08b022f0e37b205362e78f62e1f3b1d50282c2d3b36e46d87a57051109cf30d0'
5
5
  SHA512:
6
- metadata.gz: 973eb074ad890a30ec58ff8befacd7a3c0b160dfe7f36c49d5a5ed6f70634cbf38909747e19609ccb085056c8070065029a0fed33440a8eeb0c339c036c1c0ae
7
- data.tar.gz: 79e03a845dbad972be71566e1208bdb56bab7a6ba9673301030b819bd1bc3a40b78ab45142bef2161da2e1ea9075f205d9f92d94e0442bac1efba60b2d625234
6
+ metadata.gz: 525aa1428c908b058d6bed619aa520da199de71297883d0532c749e79c4e9332be34a8e63fb17a943cdac67505d1fe753e653d357b00927600f792b1991a75ef
7
+ data.tar.gz: 3a806a552ddb136a761fdc008ce9475f2ee4cae9127d7e5c0299ddc0d1dcdfa49195482432e068afe301e6441f05016ece41084eaebfc9be8d3f31728befe101
@@ -1,6 +1,7 @@
1
1
  require_relative 'windows/constants'
2
2
  require_relative 'windows/structs'
3
3
  require_relative 'windows/functions'
4
+ require_relative 'windows/version'
4
5
 
5
6
  # The Win32 module serves as a namespace only.
6
7
  module Win32
@@ -15,7 +16,7 @@ module Win32
15
16
  extend Windows::ServiceFunctions
16
17
 
17
18
  # The version of this library
18
- VERSION = '0.8.10'.freeze
19
+ VERSION = Win32::Service::VERSION
19
20
 
20
21
  private
21
22
 
@@ -400,8 +400,8 @@ module Win32
400
400
  Service.configure_failure_actions(handle_scs, opts)
401
401
  end
402
402
  ensure
403
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
404
- CloseServiceHandle(handle_scm) if handle_scm && handle_scm > 0
403
+ close_service_handle(handle_scs)
404
+ close_service_handle(handle_scm)
405
405
  end
406
406
 
407
407
  self
@@ -580,8 +580,8 @@ module Win32
580
580
  configure_failure_actions(handle_scs, opts)
581
581
  end
582
582
  ensure
583
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
584
- CloseServiceHandle(handle_scm) if handle_scm && handle_scm > 0
583
+ close_service_handle(handle_scs)
584
+ close_service_handle(handle_scm)
585
585
  end
586
586
 
587
587
  self
@@ -605,8 +605,8 @@ module Win32
605
605
  handle_scs = OpenService(handle_scm, service, SERVICE_QUERY_STATUS)
606
606
  bool = true if handle_scs > 0
607
607
  ensure
608
- CloseServiceHandle(handle_scm) if handle_scm && handle_scm > 0
609
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
608
+ close_service_handle(handle_scm)
609
+ close_service_handle(handle_scs)
610
610
  end
611
611
 
612
612
  bool
@@ -644,7 +644,7 @@ module Win32
644
644
 
645
645
  FFI.raise_windows_error('OpenSCManager') unless bool
646
646
  ensure
647
- CloseServiceHandle(handle_scm)
647
+ close_service_handle(handle_scm)
648
648
  end
649
649
 
650
650
  display_name.read_string
@@ -681,7 +681,7 @@ module Win32
681
681
 
682
682
  FFI.raise_windows_error('GetServiceKeyName') unless bool
683
683
  ensure
684
- CloseServiceHandle(handle_scm)
684
+ close_service_handle(handle_scm)
685
685
  end
686
686
 
687
687
  service_name.read_string
@@ -735,8 +735,8 @@ module Win32
735
735
  end
736
736
 
737
737
  ensure
738
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
739
- CloseServiceHandle(handle_scm)
738
+ close_service_handle(handle_scs)
739
+ close_service_handle(handle_scm)
740
740
  end
741
741
 
742
742
  self
@@ -824,8 +824,8 @@ module Win32
824
824
  FFI.raise_windows_error('DeleteService')
825
825
  end
826
826
  ensure
827
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
828
- CloseServiceHandle(handle_scm)
827
+ close_service_handle(handle_scs)
828
+ close_service_handle(handle_scm)
829
829
  end
830
830
 
831
831
  self
@@ -873,8 +873,8 @@ module Win32
873
873
 
874
874
  FFI.raise_windows_error('QueryServiceConfig') unless bool
875
875
  ensure
876
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
877
- CloseServiceHandle(handle_scm)
876
+ close_service_handle(handle_scs)
877
+ close_service_handle(handle_scm)
878
878
  end
879
879
 
880
880
  ConfigStruct.new(
@@ -947,8 +947,8 @@ module Win32
947
947
  status[:dwServiceFlags]
948
948
  )
949
949
  ensure
950
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
951
- CloseServiceHandle(handle_scm)
950
+ close_service_handle(handle_scs)
951
+ close_service_handle(handle_scm)
952
952
  end
953
953
 
954
954
  status_struct
@@ -1087,18 +1087,7 @@ module Win32
1087
1087
  description = ''
1088
1088
  end
1089
1089
 
1090
- begin
1091
- delayed_start_buf = get_config2_info(handle_scs, SERVICE_CONFIG_DELAYED_AUTO_START_INFO)
1092
- if delayed_start_buf.is_a?(FFI::MemoryPointer)
1093
- delayed_start_info = SERVICE_DELAYED_AUTO_START_INFO.new(delayed_start_buf)
1094
- delayed_start = delayed_start_info[:fDelayedAutostart]
1095
- else
1096
- delayed_start = false
1097
- end
1098
- rescue
1099
- warn "WARNING: Unable to get delayed auto start information for the #{service_name} service"
1100
- delayed_start = nil
1101
- end
1090
+ delayed_start = delayed_start(service_name)
1102
1091
  else
1103
1092
  warn "WARNING: The registry entry for the #{service_name} service could not be found"
1104
1093
 
@@ -1161,7 +1150,7 @@ module Win32
1161
1150
  actions = nil
1162
1151
  end
1163
1152
  ensure
1164
- CloseServiceHandle(handle_scs) if handle_scs > 0
1153
+ close_service_handle(handle_scs)
1165
1154
  end
1166
1155
 
1167
1156
  struct = ServiceStruct.new(
@@ -1202,12 +1191,132 @@ module Win32
1202
1191
  service_buf += ENUM_SERVICE_STATUS_PROCESS.size
1203
1192
  }
1204
1193
  ensure
1205
- CloseServiceHandle(handle_scm)
1194
+ close_service_handle(handle_scm)
1206
1195
  end
1207
1196
 
1208
1197
  block_given? ? nil : services_array
1209
1198
  end
1210
1199
 
1200
+ #
1201
+ # Attempts to get the delayed_start attribute for the named +service+ on +host+,
1202
+ # or the local machine if no host is provided.
1203
+ #
1204
+ # @example Get 'SomeSvc' delayed start on the local machine
1205
+ # Service.delayed_start('SomeSvc') # => 1
1206
+ # @example Get 'SomeSvc' delayed start on host foo
1207
+ # Service.delayed_start('SomeSvc', 'foo') # => 1
1208
+ #
1209
+ # @param service [String] Service name (e.g. `"Dhcp"`)
1210
+ # @param host [String] Host of service (e.g. `"mymachine"`)
1211
+ # @return [Integer, false, nil] Returns `1` when delayed start is enabled
1212
+ # and `0` when it is not enabled. Returns nil or false when there is
1213
+ # a problem of some kind.
1214
+ #
1215
+ def self.delayed_start(service, host = nil)
1216
+ handle_scm = OpenSCManager(host, nil, SC_MANAGER_ENUMERATE_SERVICE)
1217
+
1218
+ FFI.raise_windows_error('OpenSCManager') if handle_scm == 0
1219
+
1220
+ handle_scs = OpenService(
1221
+ handle_scm,
1222
+ service,
1223
+ SERVICE_QUERY_CONFIG
1224
+ )
1225
+
1226
+ FFI.raise_windows_error('OpenService') if handle_scs == 0
1227
+
1228
+ delayed_start_buf = get_config2_info(handle_scs, SERVICE_CONFIG_DELAYED_AUTO_START_INFO)
1229
+ if delayed_start_buf.is_a?(FFI::MemoryPointer)
1230
+ delayed_start_info = SERVICE_DELAYED_AUTO_START_INFO.new(delayed_start_buf)
1231
+ delayed_start = delayed_start_info[:fDelayedAutostart]
1232
+ else
1233
+ delayed_start = false
1234
+ end
1235
+ rescue SystemCallError
1236
+ delayed_start = nil
1237
+ ensure
1238
+ close_service_handle(handle_scs)
1239
+ close_service_handle(handle_scm)
1240
+ end
1241
+
1242
+ def self.close_service_handle(handle)
1243
+ case handle
1244
+ when NilClass
1245
+ false
1246
+ when Integer
1247
+ handle > 0 ? CloseServiceHandle(handle) : false
1248
+ else
1249
+ raise ArgumentError, "You must pass a valid handle to ::close_service_handle"
1250
+ end
1251
+ end
1252
+
1253
+ #
1254
+ # Opens an existing service.
1255
+ #
1256
+ # @example
1257
+ # open_sc_manager do |scm_handle|
1258
+ # open_service(scm_handle, 'Dhcp', SERVICE_ALL_ACCESS) do |service_handle|
1259
+ # p service_handle
1260
+ # end
1261
+ # end
1262
+ #
1263
+ # @param scm_handle [Integer] pointer to Service Control Manager
1264
+ # @param service_name [String] Name of the service
1265
+ # @param desired_access [Integer] The access to the service. For a list of
1266
+ # access rights, see `Windows::ServiceConstants`.
1267
+ #
1268
+ # @see Windows::ServiceConstants
1269
+ #
1270
+ def self.open_service(scm_handle, service_name, desired_access)
1271
+ service_handle = OpenService(
1272
+ scm_handle,
1273
+ service_name,
1274
+ desired_access
1275
+ )
1276
+ FFI.raise_windows_error('OpenService') if service_handle == 0
1277
+
1278
+ if block_given?
1279
+ yield service_handle
1280
+ else
1281
+ service_handle
1282
+ end
1283
+ ensure
1284
+ close_service_handle(service_handle) if block_given?
1285
+ end
1286
+
1287
+ #
1288
+ # Establishes a connection to the service control manager on the specified
1289
+ # host and opens the SERVICES_ACTIVE_DATABASE service control manager
1290
+ # database.
1291
+ #
1292
+ # @example
1293
+ # open_sc_manager(nil, SC_MANAGER_ENUMERATE_SERVICE) do |scm_handle|
1294
+ # p scm_handle
1295
+ # end
1296
+ #
1297
+ # @param host [String] Name of host you want to connect to. If `nil` it
1298
+ # will connect to the local host.
1299
+ # @param desired_access [Integer] The access to the service control
1300
+ # manager.
1301
+ # @return [Integer] If the function succeeds, the return value is a handle
1302
+ # to the specified service control manager database. If the function
1303
+ # fails, the return value is 0.
1304
+ #
1305
+ # @see Windows::ServiceConstants
1306
+ #
1307
+ def self.open_sc_manager(host = nil, desired_access = SC_MANAGER_CONNECT)
1308
+ scm_handle = OpenSCManager(host, nil, desired_access)
1309
+ FFI.raise_windows_error('OpenSCManager') if scm_handle == 0
1310
+
1311
+ if block_given?
1312
+ yield scm_handle
1313
+ else
1314
+ scm_handle
1315
+ end
1316
+ ensure
1317
+ close_service_handle(scm_handle) if block_given?
1318
+ end
1319
+
1211
1320
  private
1212
1321
 
1213
1322
  # Configures failure actions for a given service.
@@ -1224,7 +1333,7 @@ module Win32
1224
1333
 
1225
1334
  unless bool
1226
1335
  error = FFI.errno
1227
- CloseServiceHandle(handle_scs)
1336
+ close_service_handle(handle_scs)
1228
1337
  raise SystemCallError.new('OpenProcessToken', error)
1229
1338
  end
1230
1339
 
@@ -1235,7 +1344,7 @@ module Win32
1235
1344
 
1236
1345
  unless LookupPrivilegeValue('', 'SeShutdownPrivilege', luid)
1237
1346
  error = FFI.errno
1238
- CloseServiceHandle(handle_scs)
1347
+ close_service_handle(handle_scs)
1239
1348
  raise SystemCallError.new('LookupPrivilegeValue', error)
1240
1349
  end
1241
1350
 
@@ -1259,7 +1368,7 @@ module Win32
1259
1368
 
1260
1369
  unless bool
1261
1370
  error = FFI.errno
1262
- CloseServiceHandle(handle_scs)
1371
+ close_service_handle(handle_scs)
1263
1372
  raise SystemCallError.new('AdjustTokenPrivileges', error)
1264
1373
  end
1265
1374
  end
@@ -1303,7 +1412,7 @@ module Win32
1303
1412
 
1304
1413
  unless bool
1305
1414
  error = FFI.errno
1306
- CloseServiceHandle(handle_scs)
1415
+ close_service_handle(handle_scs)
1307
1416
  raise SystemCallError.new('ChangeServiceConfig2', error)
1308
1417
  end
1309
1418
  end
@@ -1559,8 +1668,8 @@ module Win32
1559
1668
  FFI.raise_windows_error('ControlService')
1560
1669
  end
1561
1670
  ensure
1562
- CloseServiceHandle(handle_scs) if handle_scs && handle_scs > 0
1563
- CloseServiceHandle(handle_scm) if handle_scm && handle_scm > 0
1671
+ close_service_handle(handle_scs)
1672
+ close_service_handle(handle_scm)
1564
1673
  end
1565
1674
 
1566
1675
  status
@@ -1,6 +1,6 @@
1
1
  module Win32
2
2
  class Service
3
- VERSION = "1.0.1"
3
+ VERSION = "2.1.2"
4
4
  MAJOR, MINOR, TINY = VERSION.split(".")
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: win32-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-06-15 00:00:00.000000000 Z
12
+ date: 2019-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
@@ -39,6 +39,48 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec-core
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec-expectations
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec-mocks
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
42
84
  - !ruby/object:Gem::Dependency
43
85
  name: test-unit
44
86
  requirement: !ruby/object:Gem::Requirement
@@ -91,25 +133,8 @@ description: |2
91
133
  email: djberg96@gmail.com
92
134
  executables: []
93
135
  extensions: []
94
- extra_rdoc_files:
95
- - CHANGELOG.md
96
- - README.md
97
- - MANIFEST
98
- - doc/service.txt
99
- - doc/daemon.txt
136
+ extra_rdoc_files: []
100
137
  files:
101
- - CHANGELOG.md
102
- - Gemfile
103
- - MANIFEST
104
- - README.md
105
- - Rakefile
106
- - VERSION
107
- - appveyor.yml
108
- - doc/daemon.txt
109
- - doc/service.txt
110
- - examples/demo_daemon.rb
111
- - examples/demo_daemon_ctl.rb
112
- - examples/demo_services.rb
113
138
  - lib/win32-daemon.rb
114
139
  - lib/win32-service.rb
115
140
  - lib/win32/daemon.rb
@@ -118,16 +143,9 @@ files:
118
143
  - lib/win32/windows/functions.rb
119
144
  - lib/win32/windows/structs.rb
120
145
  - lib/win32/windows/version.rb
121
- - test/test_win32_daemon.rb
122
- - test/test_win32_service.rb
123
- - test/test_win32_service_configure.rb
124
- - test/test_win32_service_create.rb
125
- - test/test_win32_service_info.rb
126
- - test/test_win32_service_status.rb
127
- - win32-service.gemspec
128
- homepage: http://github.com/chef/win32-service
146
+ homepage: https://github.com/chef/win32-service
129
147
  licenses:
130
- - Artistic 2.0
148
+ - Artistic-2.0
131
149
  metadata: {}
132
150
  post_install_message:
133
151
  rdoc_options: []
@@ -137,22 +155,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
155
  requirements:
138
156
  - - ">="
139
157
  - !ruby/object:Gem::Version
140
- version: '0'
158
+ version: '2.2'
141
159
  required_rubygems_version: !ruby/object:Gem::Requirement
142
160
  requirements:
143
161
  - - ">="
144
162
  - !ruby/object:Gem::Version
145
163
  version: '0'
146
164
  requirements: []
147
- rubyforge_project:
148
- rubygems_version: 2.7.6
165
+ rubygems_version: 3.0.3
149
166
  signing_key:
150
167
  specification_version: 4
151
168
  summary: An interface for MS Windows services
152
- test_files:
153
- - test/test_win32_service.rb
154
- - test/test_win32_daemon.rb
155
- - test/test_win32_service_info.rb
156
- - test/test_win32_service_configure.rb
157
- - test/test_win32_service_status.rb
158
- - test/test_win32_service_create.rb
169
+ test_files: []