1 /*****************************************************************************
2 * ntservice.c: Windows NT/2K/XP service interface
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #define VLCSERVICENAME "VLC media player"
33 /*****************************************************************************
35 *****************************************************************************/
36 static int Activate( vlc_object_t
* );
37 static void Close ( vlc_object_t
* );
39 #define INSTALL_TEXT N_( "Install Windows Service" )
40 #define INSTALL_LONGTEXT N_( \
41 "If enabled the interface will install the Service and exit." )
42 #define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
43 #define UNINSTALL_LONGTEXT N_( \
44 "If enabled the interface will uninstall the Service and exit." )
45 #define NAME_TEXT N_( "Display name of the Service" )
46 #define NAME_LONGTEXT N_( \
47 "This allows you to change the display name of the Service." )
48 #define OPTIONS_TEXT N_("Configuration options")
49 #define OPTIONS_LONGTEXT N_( \
50 "This option allows you to specify configuration options that will be " \
51 "used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
52 "at install time so the Service is properly configured.")
53 #define EXTRAINTF_TEXT N_("Extra interface modules")
54 #define EXTRAINTF_LONGTEXT N_( \
55 "This option allows you to select additional interfaces spawned by the " \
56 "Service. It should be specified at install time so the Service is " \
57 "properly configured. Use a comma separated list of interface modules. " \
58 "(common values are: logger, sap, rc, http)")
61 set_description( _("Windows Service interface") );
62 add_bool( "ntservice-install", 0, NULL
,
63 INSTALL_TEXT
, INSTALL_LONGTEXT
, VLC_TRUE
);
64 add_bool( "ntservice-uninstall", 0, NULL
,
65 UNINSTALL_TEXT
, UNINSTALL_LONGTEXT
, VLC_TRUE
);
66 add_string ( "ntservice-name", VLCSERVICENAME
, NULL
,
67 NAME_TEXT
, NAME_LONGTEXT
, VLC_TRUE
);
68 add_string ( "ntservice-options", NULL
, NULL
,
69 OPTIONS_TEXT
, OPTIONS_LONGTEXT
, VLC_TRUE
);
70 add_string ( "ntservice-extraintf", NULL
, NULL
,
71 EXTRAINTF_TEXT
, EXTRAINTF_LONGTEXT
, VLC_TRUE
);
73 set_capability( "interface", 0 );
74 set_callbacks( Activate
, Close
);
79 SERVICE_STATUS_HANDLE hStatus
;
80 SERVICE_STATUS status
;
84 /*****************************************************************************
86 *****************************************************************************/
87 static void Run( intf_thread_t
*p_intf
);
88 static int NTServiceInstall( intf_thread_t
*p_intf
);
89 static int NTServiceUninstall( intf_thread_t
*p_intf
);
90 static void WINAPI
ServiceDispatch( DWORD numArgs
, char **args
);
91 static void WINAPI
ServiceCtrlHandler( DWORD control
);
93 /* We need this global */
94 static intf_thread_t
*p_global_intf
;
96 /*****************************************************************************
97 * Activate: initialize and create stuff
98 *****************************************************************************/
99 static int Activate( vlc_object_t
*p_this
)
101 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
103 /* Only works on NT/2K/XP */
104 if( !IS_WINNT
) return VLC_EGENERIC
;
106 p_intf
->pf_run
= Run
;
110 /*****************************************************************************
111 * Close: destroy interface
112 *****************************************************************************/
113 void Close( vlc_object_t
*p_this
)
117 /*****************************************************************************
118 * Run: interface thread
119 *****************************************************************************/
120 static void Run( intf_thread_t
*p_intf
)
122 intf_thread_t
*p_extraintf
;
123 SERVICE_TABLE_ENTRY dispatchTable
[] =
125 { VLCSERVICENAME
, &ServiceDispatch
},
129 p_global_intf
= p_intf
;
130 p_intf
->p_sys
= alloca( sizeof( intf_sys_t
) );
131 p_intf
->p_sys
->psz_service
= config_GetPsz( p_intf
, "ntservice-name" );
132 p_intf
->p_sys
->psz_service
= p_intf
->p_sys
->psz_service
?
133 p_intf
->p_sys
->psz_service
: strdup(VLCSERVICENAME
);
135 if( config_GetInt( p_intf
, "ntservice-install" ) )
137 NTServiceInstall( p_intf
);
141 if( config_GetInt( p_intf
, "ntservice-uninstall" ) )
143 NTServiceUninstall( p_intf
);
147 if( StartServiceCtrlDispatcher( dispatchTable
) == 0 )
149 msg_Err( p_intf
, "StartServiceCtrlDispatcher failed" );
152 free( p_intf
->p_sys
->psz_service
);
154 /* Stop and destroy the interfaces we spawned */
155 while( (p_extraintf
= vlc_object_find(p_intf
, VLC_OBJECT_INTF
, FIND_CHILD
)))
157 intf_StopThread( p_extraintf
);
158 vlc_object_detach( p_extraintf
);
159 vlc_object_release( p_extraintf
);
160 intf_Destroy( p_extraintf
);
163 /* Make sure we exit (In case other interfaces have been spawned) */
164 p_intf
->p_vlc
->b_die
= VLC_TRUE
;
167 /*****************************************************************************
168 * NT Service utility functions
169 *****************************************************************************/
170 static int NTServiceInstall( intf_thread_t
*p_intf
)
172 intf_sys_t
*p_sys
= p_intf
->p_sys
;
173 char psz_path
[10*MAX_PATH
], psz_pathtmp
[MAX_PATH
], *psz_extra
;
174 SC_HANDLE handle
= OpenSCManager( NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
177 msg_Err( p_intf
, "could not connect to SCM database" );
181 /* Find out the filename of ourselves so we can install it to the
182 * service control manager */
183 GetModuleFileName( NULL
, psz_pathtmp
, MAX_PATH
);
184 sprintf( psz_path
, "\"%s\" -I "MODULE_STRING
, psz_pathtmp
);
186 psz_extra
= config_GetPsz( p_intf
, "ntservice-extraintf" );
187 if( psz_extra
&& *psz_extra
)
189 strcat( psz_path
, " --ntservice-extraintf " );
190 strcat( psz_path
, psz_extra
);
192 if( psz_extra
) free( psz_extra
);
194 psz_extra
= config_GetPsz( p_intf
, "ntservice-options" );
195 if( psz_extra
&& *psz_extra
)
197 strcat( psz_path
, " " );
198 strcat( psz_path
, psz_extra
);
200 if( psz_extra
) free( psz_extra
);
203 CreateService( handle
, p_sys
->psz_service
, p_sys
->psz_service
,
204 GENERIC_READ
| GENERIC_EXECUTE
,
205 SERVICE_WIN32_OWN_PROCESS
,
206 SERVICE_AUTO_START
, SERVICE_ERROR_IGNORE
,
207 psz_path
, NULL
, NULL
, NULL
, NULL
, NULL
);
208 if( service
== NULL
)
210 if( GetLastError() != ERROR_SERVICE_EXISTS
)
212 msg_Err( p_intf
, "could not create new service: \"%s\" (%s)",
213 p_sys
->psz_service
,psz_path
);
214 CloseServiceHandle( handle
);
219 msg_Warn( p_intf
, "service \"%s\" already exists",
220 p_sys
->psz_service
);
225 msg_Warn( p_intf
, "service successfuly created" );
228 if( service
) CloseServiceHandle( service
);
229 CloseServiceHandle( handle
);
234 static int NTServiceUninstall( intf_thread_t
*p_intf
)
236 intf_sys_t
*p_sys
= p_intf
->p_sys
;
238 SC_HANDLE handle
= OpenSCManager( NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
241 msg_Err( p_intf
, "could not connect to SCM database" );
245 /* First, open a handle to the service */
246 SC_HANDLE service
= OpenService( handle
, p_sys
->psz_service
, DELETE
);
247 if( service
== NULL
)
249 msg_Err( p_intf
, "could not open service" );
250 CloseServiceHandle( handle
);
254 /* Remove the service */
255 if( !DeleteService( service
) )
257 msg_Err( p_intf
, "could not delete service \"%s\"",
258 p_sys
->psz_service
);
262 msg_Dbg( p_intf
, "service deleted successfuly" );
265 CloseServiceHandle( service
);
266 CloseServiceHandle( handle
);
271 static void WINAPI
ServiceDispatch( DWORD numArgs
, char **args
)
273 intf_thread_t
*p_intf
= (intf_thread_t
*)p_global_intf
;
274 intf_sys_t
*p_sys
= p_intf
->p_sys
;
275 char *psz_modules
, *psz_parser
;
277 /* We have to initialize the service-specific stuff */
278 memset( &p_sys
->status
, 0, sizeof(SERVICE_STATUS
) );
279 p_sys
->status
.dwServiceType
= SERVICE_WIN32
;
280 p_sys
->status
.dwCurrentState
= SERVICE_START_PENDING
;
281 p_sys
->status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
284 RegisterServiceCtrlHandler( p_sys
->psz_service
, &ServiceCtrlHandler
);
285 if( p_sys
->hStatus
== (SERVICE_STATUS_HANDLE
)0 )
287 msg_Err( p_intf
, "failed to register service control handler" );
292 * Load background interfaces
294 psz_modules
= config_GetPsz( p_intf
, "ntservice-extraintf" );
295 psz_parser
= psz_modules
;
296 while( psz_parser
&& *psz_parser
)
298 char *psz_module
, *psz_temp
;
299 psz_module
= psz_parser
;
300 psz_parser
= strchr( psz_module
, ',' );
306 psz_temp
= (char *)malloc( strlen(psz_module
) + sizeof(",none") );
309 intf_thread_t
*p_new_intf
;
310 sprintf( psz_temp
, "%s,none", psz_module
);
312 /* Try to create the interface */
313 p_new_intf
= intf_Create( p_intf
, psz_temp
);
314 if( p_new_intf
== NULL
)
316 msg_Err( p_intf
, "interface \"%s\" initialization failed",
322 /* Try to run the interface */
323 p_new_intf
->b_block
= VLC_FALSE
;
324 if( intf_RunThread( p_new_intf
) )
326 vlc_object_detach( p_new_intf
);
327 intf_Destroy( p_new_intf
);
328 msg_Err( p_intf
, "interface \"%s\" cannot run", psz_temp
);
339 /* Initialization complete - report running status */
340 p_sys
->status
.dwCurrentState
= SERVICE_RUNNING
;
341 p_sys
->status
.dwCheckPoint
= 0;
342 p_sys
->status
.dwWaitHint
= 0;
344 SetServiceStatus( p_sys
->hStatus
, &p_sys
->status
);
347 static void WINAPI
ServiceCtrlHandler( DWORD control
)
349 intf_thread_t
*p_intf
= (intf_thread_t
*)p_global_intf
;
350 intf_sys_t
*p_sys
= p_intf
->p_sys
;
354 case SERVICE_CONTROL_SHUTDOWN
:
355 case SERVICE_CONTROL_STOP
:
356 p_sys
->status
.dwCurrentState
= SERVICE_STOPPED
;
357 p_sys
->status
.dwWin32ExitCode
= 0;
358 p_sys
->status
.dwCheckPoint
= 0;
359 p_sys
->status
.dwWaitHint
= 0;
361 case SERVICE_CONTROL_INTERROGATE
:
362 /* just set the current state to whatever it is... */
366 SetServiceStatus( p_sys
->hStatus
, &p_sys
->status
);