windows-nsis-vs2005-20080409
[openafs.git] / src / WINNT / install / NSIS / ServiceLib.nsh
1
2 ; NSIS SERVICE LIBRARY - servicelib.nsh
3 ; Version 1.2 - 02/29/2004
4 ; Questions/Comments - dselkirk@hotmail.com
5 ;
6 ; Description:
7 ;   Provides an interface to window services
8 ;
9 ; Inputs:
10 ;   action      - systemlib action ie. create, delete, start, stop, pause,
11 ;               continue, installed, running, status
12 ;   name        - name of service to manipulate
13 ;   param       - action parameters; usage: var1=value1;var2=value2;...etc.
14 ;
15 ; Actions:
16 ;   create      - creates a new windows service
17 ;               Parameters:
18 ;                 path  - path to service executable
19 ;                 autostart     - automatically start with system ie. 1|0
20 ;                 interact      - interact with the desktop ie. 1|0
21 ;                 machine       - machine name where to install service
22 ;                 user          - user that runs the service
23 ;                 password      - password of the above user
24 ;
25 ;   delete      - deletes a windows service
26 ;   start       - start a stopped windows service
27 ;   stop        - stops a running windows service
28 ;   pause       - pauses a running windows service
29 ;   continue    - continues a paused windows service
30 ;   installed   - is the provided service installed
31 ;               Parameters:
32 ;                 action        - if true then invokes the specified action
33 ;   running     - is the provided service running
34 ;               Parameters:
35 ;                 action        - if true then invokes the specified action
36 ;   status      - check the status of the provided service
37 ;
38 ; If run from uninstall define "UN" as "un." gefore running.
39 ;
40 ; Usage:
41 ;   Method 1:
42 ;     Push "action"
43 ;     Push "name"
44 ;     Push "param"
45 ;     Call Service
46 ;     Pop $0 ;response
47 ;
48 ;   Method 2:
49 ;     !insertmacro SERVICE "action" "name" "param"
50 ;
51 ; History:
52 ;               1.0 - 09/15/2003 - Initial release
53 ;               1.1 - 09/16/2003 - Changed &l to i, thx brainsucker
54 ;               1.2 - 02/29/2004 - Fixed documentation.
55
56 !ifndef SERVICELIB
57   !define SERVICELIB
58
59   !define SC_MANAGER_ALL_ACCESS 0x3F
60   !define SERVICE_ALL_ACCESS 0xF01FF
61
62   !define SERVICE_CONTROL_STOP  1
63   !define SERVICE_CONTROL_PAUSE  2
64   !define SERVICE_CONTROL_CONTINUE  3
65
66   !define SERVICE_STOPPED 0x1
67   !define SERVICE_START_PENDING 0x2
68   !define SERVICE_STOP_PENDING 0x3
69   !define SERVICE_RUNNING 0x4
70   !define SERVICE_CONTINUE_PENDING 0x5
71   !define SERVICE_PAUSE_PENDING 0x6
72   !define SERVICE_PAUSED 0x7
73
74   !ifndef UN
75     !define UN ""
76   !endif
77
78   !macro SERVICE ACTION NAME PARAM
79     Push '${ACTION}'
80     Push '${NAME}'
81     Push '${PARAM}'
82     Call ${UN}Service
83   !macroend
84
85   !macro FUNC_GETPARAM
86     Push $0
87     Push $1
88     Push $2
89     Push $3
90     Push $4
91     Push $5
92     Push $6
93     Push $7
94     Exch 8
95     Pop $1 ;name
96     Exch 8
97     Pop $2 ;source
98     StrCpy $0 ""
99     StrLen $7 $2
100     StrCpy $3 0
101     lbl_loop:
102       IntCmp $3 $7 0 0 lbl_done
103       StrLen $4 "$1="
104       StrCpy $5 $2 $4 $3
105       StrCmp $5 "$1=" 0 lbl_next
106       IntOp $5 $3 + $4
107       StrCpy $3 $5
108       lbl_loop2:
109         IntCmp $3 $7 0 0 lbl_done
110         StrCpy $6 $2 1 $3
111         StrCmp $6 ";" 0 lbl_next2
112         IntOp $6 $3 - $5
113         StrCpy $0 $2 $6 $5
114         Goto lbl_done
115         lbl_next2:
116         IntOp $3 $3 + 1
117         Goto lbl_loop2
118       lbl_next:
119       IntOp $3 $3 + 1
120       Goto lbl_loop
121     lbl_done:
122     Pop $5
123     Pop $4
124     Pop $3
125     Pop $2
126     Pop $1
127     Exch 2
128     Pop $6
129     Pop $7
130     Exch $0
131   !macroend
132
133   !macro CALL_GETPARAM VAR NAME DEFAULT LABEL
134     Push $1
135     Push ${NAME}
136     Call ${UN}GETPARAM
137     Pop $6
138     StrCpy ${VAR} "${DEFAULT}"
139     StrCmp $6 "" "${LABEL}" 0
140     StrCpy ${VAR} $6
141   !macroend
142
143   !macro FUNC_SERVICE UN
144     Push $0
145     Push $1
146     Push $2
147     Push $3
148     Push $4
149     Push $5
150     Push $6
151     Push $7
152     Exch 8
153     Pop $1 ;param
154     Exch 8
155     Pop $2 ;name
156     Exch 8
157     Pop $3 ;action
158     ;$0 return
159     ;$4 OpenSCManager
160     ;$5 OpenService
161
162
163     StrCpy $0 "false"
164     System::Call 'advapi32::OpenSCManagerA(n, n, i ${SC_MANAGER_ALL_ACCESS}) i.r4'
165     IntCmp $4 0 lbl_done
166     StrCmp $3 "create" lbl_create
167     System::Call 'advapi32::OpenServiceA(i r4, t r2, i ${SERVICE_ALL_ACCESS}) i.r5'
168     IntCmp $5 0 lbl_done
169
170     lbl_select:
171     StrCmp $3 "delete" lbl_delete
172     StrCmp $3 "start" lbl_start
173     StrCmp $3 "stop" lbl_stop
174     StrCmp $3 "pause" lbl_pause
175     StrCmp $3 "continue" lbl_continue
176     StrCmp $3 "installed" lbl_installed
177     StrCmp $3 "running" lbl_running
178     StrCmp $3 "status" lbl_status
179     Goto lbl_done
180
181     ; create service
182     lbl_create:
183       Push $R1 ;machine
184       Push $R2 ;user
185       Push $R3 ;password
186       Push $R4 ;interact
187       Push $R5 ;autostart
188       Push $R6 ;path
189
190       !insertmacro CALL_GETPARAM $R1 "machine" "n" "lbl_machine"
191       lbl_machine:
192
193       !insertmacro CALL_GETPARAM $R2 "user" "n" "lbl_user"
194       lbl_user:
195
196       !insertmacro CALL_GETPARAM $R3 "password" "n" "lbl_password"
197       lbl_password:
198
199       !insertmacro CALL_GETPARAM $R4 "interact" "0x10" "lbl_interact"
200         StrCpy $6 0x10
201         IntCmp $R4 0 +2
202         IntOp $R4 $6 | 0x100
203         StrCpy $R4 $6
204       lbl_interact:
205
206       !insertmacro CALL_GETPARAM $R5 "autostart" "0x3" "lbl_autostart"
207         StrCpy $6 0x3
208         IntCmp $R5 0 +2
209         StrCpy $6 0x2
210         StrCpy $R5 $6
211       lbl_autostart:
212
213       !insertmacro CALL_GETPARAM $R6 "path" "n" "lbl_path"
214       lbl_path:
215
216       System::Call 'advapi32::CreateServiceA(i r4, t r2, t r2, i ${SERVICE_ALL_ACCESS}, i R4, i R5, i 0, t R6, n, n, R1, R2, R3) i.r6'
217       Pop $R6
218       Pop $R5
219       Pop $R4
220       Pop $R3
221       Pop $R2
222       Pop $R1
223       StrCmp $6 0 lbl_done lbl_good
224
225     ; delete service
226     lbl_delete:
227       System::Call 'advapi32::DeleteService(i r5) i.r6'
228       StrCmp $6 0 lbl_done lbl_good
229
230     ; start service
231     lbl_start:
232       System::Call 'advapi32::StartServiceA(i r5, i 0, i 0) i.r6'
233       StrCmp $6 0 lbl_done lbl_good
234
235     ; stop service
236     lbl_stop:
237       Push $R1
238       System::Call '*(i,i,i,i,i,i,i) i.R1'
239       System::Call 'advapi32::ControlService(i r5, i ${SERVICE_CONTROL_STOP}, i $R1) i'
240       System::Free $R1
241       Pop $R1
242       StrCmp $6 0 lbl_done lbl_good
243
244     ; pause service
245     lbl_pause:
246       Push $R1
247       System::Call '*(i,i,i,i,i,i,i) i.R1'
248       System::Call 'advapi32::ControlService(i r5, i ${SERVICE_CONTROL_PAUSE}, i $R1) i'
249       System::Free $R1
250       Pop $R1
251       StrCmp $6 0 lbl_done lbl_good
252
253     ; continue service
254     lbl_continue:
255       Push $R1
256       System::Call '*(i,i,i,i,i,i,i) i.R1'
257       System::Call 'advapi32::ControlService(i r5, i ${SERVICE_CONTROL_CONTINUE}, i $R1) i'
258       System::Free $R1
259       Pop $R1
260       StrCmp $6 0 lbl_done lbl_good
261
262     ; is installed
263     lbl_installed:
264       !insertmacro CALL_GETPARAM $7 "action" "" "lbl_good"
265         StrCpy $3 $7
266         Goto lbl_select
267
268     ; is service running
269     lbl_running:
270       Push $R1
271       System::Call '*(i,i,i,i,i,i,i) i.R1'
272       System::Call 'advapi32::QueryServiceStatus(i r5, i $R1) i'
273       System::Call '*$R1(i, i.r6)'
274       System::Free $R1
275       Pop $R1
276       IntFmt $6 "0x%X" $6
277       StrCmp $6 ${SERVICE_RUNNING} 0 lbl_done
278       !insertmacro CALL_GETPARAM $7 "action" "" "lbl_good"
279         StrCpy $3 $7
280         Goto lbl_select
281
282     lbl_status:
283       Push $R1
284       System::Call '*(i,i,i,i,i,i,i) i.R1'
285       System::Call 'advapi32::QueryServiceStatus(i r5, i $R1) i'
286       System::Call '*$R1(i, i .r6)'
287       System::Free $R1
288       Pop $R1
289       IntFmt $6 "0x%X" $6
290       StrCpy $0 "running"
291       IntCmp $6 ${SERVICE_RUNNING} lbl_done
292       StrCpy $0 "stopped"
293       IntCmp $6 ${SERVICE_STOPPED} lbl_done
294       StrCpy $0 "start_pending"
295       IntCmp $6 ${SERVICE_START_PENDING} lbl_done
296       StrCpy $0 "stop_pending"
297       IntCmp $6 ${SERVICE_STOP_PENDING} lbl_done
298       StrCpy $0 "running"
299       IntCmp $6 ${SERVICE_RUNNING} lbl_done
300       StrCpy $0 "continue_pending"
301       IntCmp $6 ${SERVICE_CONTINUE_PENDING} lbl_done
302       StrCpy $0 "pause_pending"
303       IntCmp $6 ${SERVICE_PAUSE_PENDING} lbl_done
304       StrCpy $0 "paused"
305       IntCmp $6 ${SERVICE_PAUSED} lbl_done
306       StrCpy $0 "unknown"
307
308     lbl_good:
309     StrCpy $0 "true"
310     lbl_done:
311     IntCmp $5 0 +2
312     System::Call 'advapi32::CloseServiceHandle(i r5) n'
313     IntCmp $4 0 +2
314     System::Call 'advapi32::CloseServiceHandle(i r4) n'
315     Pop $4
316     Pop $3
317     Pop $2
318     Pop $1
319     Exch 3
320     Pop $5
321     Pop $6
322     Pop $7
323     Exch $0
324   !macroend
325
326   Function Service
327     !insertmacro FUNC_SERVICE ""
328   FunctionEnd
329
330   Function un.Service
331     !insertmacro FUNC_SERVICE "un."
332   FunctionEnd
333
334   Function GetParam
335     !insertmacro FUNC_GETPARAM
336   FunctionEnd
337
338   Function un.GetParam
339     !insertmacro FUNC_GETPARAM
340   FunctionEnd
341
342 !endif