forked from chef/win32-service
/
daemon.rb
364 lines (292 loc) · 11.5 KB
/
daemon.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
require File.join(File.dirname(__FILE__), 'windows', 'helper')
require File.join(File.dirname(__FILE__), 'windows', 'constants')
require File.join(File.dirname(__FILE__), 'windows', 'structs')
require File.join(File.dirname(__FILE__), 'windows', 'functions')
require 'ffi'
# The Win32 module serves as a namespace only.
module Win32
# The Daemon class
class Daemon
include Windows::Constants
include Windows::Structs
include Windows::Functions
extend Windows::Structs
extend Windows::Functions
# The version of this library
VERSION = '0.8.4'
private
# Service is not running
STOPPED = SERVICE_STOPPED
# Service has received a start signal but is not yet running
START_PENDING = SERVICE_START_PENDING
# Service has received a stop signal but is not yet stopped
STOP_PENDING = SERVICE_STOP_PENDING
# Service is running
RUNNING = SERVICE_RUNNING
# Service has received a signal to resume but is not yet running
CONTINUE_PENDING = SERVICE_CONTINUE_PENDING
# Service has received a signal to pause but is not yet paused
PAUSE_PENDING = SERVICE_PAUSE_PENDING
# Service is paused
PAUSED = SERVICE_PAUSED
# Service controls
# Notifies service that it should stop
CONTROL_STOP = SERVICE_CONTROL_STOP
# Notifies service that it should pause
CONTROL_PAUSE = SERVICE_CONTROL_PAUSE
# Notifies service that it should resume
CONTROL_CONTINUE = SERVICE_CONTROL_CONTINUE
# Notifies service that it should return its current status information
CONTROL_INTERROGATE = SERVICE_CONTROL_INTERROGATE
# Notifies a service that its parameters have changed
CONTROL_PARAMCHANGE = SERVICE_CONTROL_PARAMCHANGE
# Notifies a service that there is a new component for binding
CONTROL_NETBINDADD = SERVICE_CONTROL_NETBINDADD
# Notifies a service that a component for binding has been removed
CONTROL_NETBINDREMOVE = SERVICE_CONTROL_NETBINDREMOVE
# Notifies a service that a component for binding has been enabled
CONTROL_NETBINDENABLE = SERVICE_CONTROL_NETBINDENABLE
# Notifies a service that a component for binding has been disabled
CONTROL_NETBINDDISABLE = SERVICE_CONTROL_NETBINDDISABLE
IDLE = 0
# Wraps SetServiceStatus.
SetTheServiceStatus = Proc.new do |dwCurrentState, dwWin32ExitCode,dwCheckPoint, dwWaitHint|
ss = SERVICE_STATUS.new # Current status of the service.
# Disable control requests until the service is started.
if dwCurrentState == SERVICE_START_PENDING
ss[:dwControlsAccepted] = 0
else
ss[:dwControlsAccepted] =
SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN|
SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_SHUTDOWN
end
# Initialize ss structure.
ss[:dwServiceType] = SERVICE_WIN32_OWN_PROCESS
ss[:dwServiceSpecificExitCode] = 0
ss[:dwCurrentState] = dwCurrentState
ss[:dwWin32ExitCode] = dwWin32ExitCode
ss[:dwCheckPoint] = dwCheckPoint
ss[:dwWaitHint] = dwWaitHint
@@dwServiceState = dwCurrentState
# Send status of the service to the Service Controller.
if !SetServiceStatus(@@ssh, ss)
SetEvent(@@hStopEvent)
end
end
ERROR_CALL_NOT_IMPLEMENTED = 0x78
# Handles control signals from the service control manager.
Service_Ctrl_ex = Proc.new do |dwCtrlCode,dwEventType,lpEventData,lpContext|
@@waiting_control_code = dwCtrlCode;
return_value = NO_ERROR
begin
dwState = SERVICE_RUNNING
case dwCtrlCode
when SERVICE_CONTROL_STOP
dwState = SERVICE_STOP_PENDING
when SERVICE_CONTROL_SHUTDOWN
dwState = SERVICE_STOP_PENDING
when SERVICE_CONTROL_PAUSE
dwState = SERVICE_PAUSED
when SERVICE_CONTROL_CONTINUE
dwState = SERVICE_RUNNING
#else
# TODO: Handle other control codes? Retain the current state?
end
# Set the status of the service except on interrogation.
unless dwCtrlCode == SERVICE_CONTROL_INTERROGATE
SetTheServiceStatus.call(dwState, NO_ERROR, 0, 0)
end
# Tell service_main thread to stop.
if dwCtrlCode == SERVICE_CONTROL_STOP || dwCtrlCode == SERVICE_CONTROL_SHUTDOWN
if SetEvent(@@hStopEvent) == 0
SetTheServiceStatus.call(SERVICE_STOPPED, FFI.errno, 0, 0)
end
end
rescue
return_value = ERROR_CALL_NOT_IMPLEMENTED
end
return_value
end
# Called by the service control manager after the call to StartServiceCtrlDispatcher.
Service_Main = FFI::Function.new(:void, [:ulong, :pointer], :blocking => false) do |dwArgc,lpszArgv|
begin
# Obtain the name of the service.
if lpszArgv.address!=0
argv = lpszArgv.get_array_of_string(0,dwArgc)
lpszServiceName = argv[0]
else
lpszServiceName = ''
end
# Args passed to Service.start
if(dwArgc > 1)
@@Argv = argv[1..-1]
else
@@Argv = nil
end
# Register the service ctrl handler.
@@ssh = RegisterServiceCtrlHandlerEx(
lpszServiceName,
Service_Ctrl_ex,
nil
)
# No service to stop, no service handle to notify, nothing to do but exit.
return if @@ssh == 0
# The service has started.
SetTheServiceStatus.call(SERVICE_RUNNING, NO_ERROR, 0, 0)
SetEvent(@@hStartEvent)
# Main loop for the service.
while(WaitForSingleObject(@@hStopEvent, 1000) != WAIT_OBJECT_0) do
end
# Main loop for the service.
while(WaitForSingleObject(@@hStopCompletedEvent, 1000) != WAIT_OBJECT_0) do
end
ensure
# Stop the service.
SetTheServiceStatus.call(SERVICE_STOPPED, NO_ERROR, 0, 0)
end
end
ThreadProc = FFI::Function.new(:ulong,[:pointer]) do |lpParameter|
ste = FFI::MemoryPointer.new(SERVICE_TABLE_ENTRY, 2)
s = SERVICE_TABLE_ENTRY.new(ste[0])
s[:lpServiceName] = FFI::MemoryPointer.from_string('')
s[:lpServiceProc] = lpParameter
s = SERVICE_TABLE_ENTRY.new(ste[1])
s[:lpServiceName] = nil
s[:lpServiceProc] = nil
# No service to step, no service handle, no ruby exceptions, just terminate the thread..
if !StartServiceCtrlDispatcher(ste)
return 1
end
return 0
end
public
# This is a shortcut for Daemon.new + Daemon#mainloop.
#
def self.mainloop
self.new.mainloop
end
# This is the method that actually puts your code into a loop and allows it
# to run as a service. The code that is actually run while in the mainloop
# is what you defined in your own Daemon#service_main method.
#
def mainloop
@@waiting_control_code = IDLE_CONTROL_CODE
@@dwServiceState = 0
# Redirect STDIN, STDOUT and STDERR to the NUL device if they're still
# associated with a tty. This helps newbs avoid Errno::EBADF errors.
STDIN.reopen('NUL') if STDIN.isatty
STDOUT.reopen('NUL') if STDOUT.isatty
STDERR.reopen('NUL') if STDERR.isatty
# Calling init here so that init failures never even tries to start the
# service. Of course that means that init methods must be very quick
# because the SCM will be receiving no START_PENDING messages while
# init's running.
#
# TODO: Fix?
service_init() if respond_to?('service_init')
# Create the event to signal the service to start.
@@hStartEvent = CreateEvent(nil, true, false, nil)
if @@hStartEvent == 0
raise SystemCallError.new('CreateEvent', FFI.errno)
end
# Create the event to signal the service to stop.
@@hStopEvent = CreateEvent(nil, true, false, nil)
if @@hStopEvent == 0
raise SystemCallError.new('CreateEvent', FFI.errno)
end
# Create the event to signal the service that stop has completed
@@hStopCompletedEvent = CreateEvent(nil, true, false, nil)
if @@hStopCompletedEvent == 0
raise SystemCallError.new('CreateEvent', FFI.errno)
end
hThread = CreateThread(nil, 0, ThreadProc, Service_Main, 0, nil)
if hThread == 0
raise SystemCallError.new('CreateThread', FFI.errno)
end
events = FFI::MemoryPointer.new(:pointer, 2)
events.put_pointer(0, FFI::Pointer.new(hThread))
events.put_pointer(FFI::Pointer.size, FFI::Pointer.new(@@hStartEvent))
while ((index = WaitForMultipleObjects(2, events, false, 1000)) == WAIT_TIMEOUT) do
end
if index == WAIT_FAILED
raise SystemCallError.new('WaitForMultipleObjects', FFI.errno)
end
# The thread exited, so the show is off.
if index == WAIT_OBJECT_0
raise "Service_Main thread exited abnormally"
end
thr = Thread.new do
begin
while(WaitForSingleObject(@@hStopEvent, 10) == WAIT_TIMEOUT)
# Check to see if anything interesting has been signaled
case @@waiting_control_code
when SERVICE_CONTROL_PAUSE
service_pause() if respond_to?('service_pause')
when SERVICE_CONTROL_CONTINUE
service_resume() if respond_to?('service_resume')
when SERVICE_CONTROL_INTERROGATE
service_interrogate() if respond_to?('service_interrogate')
when SERVICE_CONTROL_SHUTDOWN
service_shutdown() if respond_to?('service_shutdown')
when SERVICE_CONTROL_PARAMCHANGE
service_paramchange() if respond_to?('service_paramchange')
when SERVICE_CONTROL_NETBINDADD
service_netbindadd() if respond_to?('service_netbindadd')
when SERVICE_CONTROL_NETBINDREMOVE
service_netbindremove() if respond_to?('service_netbindremove')
when SERVICE_CONTROL_NETBINDENABLE
service_netbindenable() if respond_to?('service_netbindenable')
when SERVICE_CONTROL_NETBINDDISABLE
service_netbinddisable() if respond_to?('service_netbinddisable')
end
@@waiting_control_code = IDLE_CONTROL_CODE
end
service_stop() if respond_to?('service_stop')
ensure
SetEvent(@@hStopCompletedEvent)
end
end
if respond_to?('service_main')
service_main(*@@Argv)
end
thr.join
end
# Returns the state of the service (as an constant integer) which can be any
# of the service status constants, e.g. RUNNING, PAUSED, etc.
#
# This method is typically used within your service_main method to setup the
# loop. For example:
#
# class MyDaemon < Daemon
# def service_main
# while state == RUNNING || state == PAUSED || state == IDLE
# # Your main loop here
# end
# end
# end
#
# See the Daemon#running? method for an abstraction of the above code.
#
def state
@@dwServiceState
end
#
# Returns whether or not the service is in a running state, i.e. the service
# status is either RUNNING, PAUSED or IDLE.
#
# This is typically used within your service_main method to setup the main
# loop. For example:
#
# class MyDaemon < Daemon
# def service_main
# while running?
# # Your main loop here
# end
# end
# end
#
def running?
[SERVICE_RUNNING, SERVICE_PAUSED, 0].include?(@@dwServiceState)
end
end # Daemon
end # Win32