1 /*****************************************************************************
2 * ntservice.c: Windows NT/2K/XP service interface
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_interface.h>
34 #define VLCSERVICENAME "VLC media player"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Activate( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 #define INSTALL_TEXT N_( "Install Windows Service" )
43 #define INSTALL_LONGTEXT N_( \
44 "Install the Service and exit." )
45 #define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
46 #define UNINSTALL_LONGTEXT N_( \
47 "Uninstall the Service and exit." )
48 #define NAME_TEXT N_( "Display name of the Service" )
49 #define NAME_LONGTEXT N_( \
50 "Change the display name of the Service." )
51 #define OPTIONS_TEXT N_("Configuration options")
52 #define OPTIONS_LONGTEXT N_( \
53 "Configuration options that will be " \
54 "used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
55 "at install time so the Service is properly configured.")
56 #define EXTRAINTF_TEXT N_("Extra interface modules")
57 #define EXTRAINTF_LONGTEXT N_( \
58 "Additional interfaces spawned by the " \
59 "Service. It should be specified at install time so the Service is " \
60 "properly configured. Use a comma separated list of interface modules. " \
61 "(common values are: logger, sap, rc, http)")
64 set_shortname( _("NT Service"));
65 set_description( _("Windows Service interface") );
66 set_category( CAT_INTERFACE );
67 set_subcategory( SUBCAT_INTERFACE_CONTROL );
68 add_bool( "ntservice-install", 0, NULL,
69 INSTALL_TEXT, INSTALL_LONGTEXT, true );
70 add_bool( "ntservice-uninstall", 0, NULL,
71 UNINSTALL_TEXT, UNINSTALL_LONGTEXT, true );
72 add_string ( "ntservice-name", VLCSERVICENAME, NULL,
73 NAME_TEXT, NAME_LONGTEXT, true );
74 add_string ( "ntservice-options", NULL, NULL,
75 OPTIONS_TEXT, OPTIONS_LONGTEXT, true );
76 add_string ( "ntservice-extraintf", NULL, NULL,
77 EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, true );
79 set_capability( "interface", 0 );
80 set_callbacks( Activate, Close );
85 SERVICE_STATUS_HANDLE hStatus;
86 SERVICE_STATUS status;
90 /*****************************************************************************
92 *****************************************************************************/
93 static void Run( intf_thread_t *p_intf );
94 static int NTServiceInstall( intf_thread_t *p_intf );
95 static int NTServiceUninstall( intf_thread_t *p_intf );
96 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
97 static void WINAPI ServiceCtrlHandler( DWORD control );
99 /* We need this global */
100 static intf_thread_t *p_global_intf;
102 /*****************************************************************************
103 * Activate: initialize and create stuff
104 *****************************************************************************/
105 static int Activate( vlc_object_t *p_this )
107 intf_thread_t *p_intf = (intf_thread_t*)p_this;
109 /* Only works on NT/2K/XP */
110 if( !IS_WINNT ) return VLC_EGENERIC;
112 p_intf->pf_run = Run;
116 /*****************************************************************************
117 * Close: destroy interface
118 *****************************************************************************/
119 void Close( vlc_object_t *p_this )
123 /*****************************************************************************
124 * Run: interface thread
125 *****************************************************************************/
126 static void Run( intf_thread_t *p_intf )
128 intf_thread_t *p_extraintf;
129 SERVICE_TABLE_ENTRY dispatchTable[] =
131 { VLCSERVICENAME, &ServiceDispatch },
135 p_global_intf = p_intf;
136 p_intf->p_sys = alloca( sizeof( intf_sys_t ) );
137 p_intf->p_sys->psz_service = config_GetPsz( p_intf, "ntservice-name" );
138 p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
139 p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
141 if( config_GetInt( p_intf, "ntservice-install" ) )
143 NTServiceInstall( p_intf );
147 if( config_GetInt( p_intf, "ntservice-uninstall" ) )
149 NTServiceUninstall( p_intf );
153 if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
155 msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
158 free( p_intf->p_sys->psz_service );
160 /* Stop and destroy the interfaces we spawned */
161 while( (p_extraintf = vlc_object_find(p_intf, VLC_OBJECT_INTF, FIND_CHILD)))
163 intf_StopThread( p_extraintf );
164 vlc_object_detach( p_extraintf );
165 vlc_object_release( p_extraintf );
166 intf_Destroy( p_extraintf );
169 /* Make sure we exit (In case other interfaces have been spawned) */
170 vlc_object_kill( p_intf->p_libvlc );
173 /*****************************************************************************
174 * NT Service utility functions
175 *****************************************************************************/
176 static int NTServiceInstall( intf_thread_t *p_intf )
178 intf_sys_t *p_sys = p_intf->p_sys;
179 char psz_path[10*MAX_PATH], psz_pathtmp[MAX_PATH], *psz_extra;
180 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
184 "could not connect to Services Control Manager database" );
188 /* Find out the filename of ourselves so we can install it to the
189 * service control manager */
190 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
191 sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
193 psz_extra = config_GetPsz( p_intf, "ntservice-extraintf" );
194 if( psz_extra && *psz_extra )
196 strcat( psz_path, " --ntservice-extraintf " );
197 strcat( psz_path, psz_extra );
201 psz_extra = config_GetPsz( p_intf, "ntservice-options" );
202 if( psz_extra && *psz_extra )
204 strcat( psz_path, " " );
205 strcat( psz_path, psz_extra );
210 CreateService( handle, p_sys->psz_service, p_sys->psz_service,
211 GENERIC_READ | GENERIC_EXECUTE,
212 SERVICE_WIN32_OWN_PROCESS,
213 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
214 psz_path, NULL, NULL, NULL, NULL, NULL );
215 if( service == NULL )
217 if( GetLastError() != ERROR_SERVICE_EXISTS )
219 msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
220 p_sys->psz_service ,psz_path );
221 CloseServiceHandle( handle );
226 msg_Warn( p_intf, "service \"%s\" already exists",
227 p_sys->psz_service );
232 msg_Warn( p_intf, "service successfuly created" );
235 if( service ) CloseServiceHandle( service );
236 CloseServiceHandle( handle );
241 static int NTServiceUninstall( intf_thread_t *p_intf )
243 intf_sys_t *p_sys = p_intf->p_sys;
245 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
249 "could not connect to Services Control Manager database" );
253 /* First, open a handle to the service */
254 SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
255 if( service == NULL )
257 msg_Err( p_intf, "could not open service" );
258 CloseServiceHandle( handle );
262 /* Remove the service */
263 if( !DeleteService( service ) )
265 msg_Err( p_intf, "could not delete service \"%s\"",
266 p_sys->psz_service );
270 msg_Dbg( p_intf, "service deleted successfuly" );
273 CloseServiceHandle( service );
274 CloseServiceHandle( handle );
279 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
281 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
282 intf_sys_t *p_sys = p_intf->p_sys;
283 char *psz_modules, *psz_parser;
285 /* We have to initialize the service-specific stuff */
286 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
287 p_sys->status.dwServiceType = SERVICE_WIN32;
288 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
289 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
292 RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
293 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
295 msg_Err( p_intf, "failed to register service control handler" );
300 * Load background interfaces
302 psz_modules = config_GetPsz( p_intf, "ntservice-extraintf" );
303 psz_parser = psz_modules;
304 while( psz_parser && *psz_parser )
306 char *psz_module, *psz_temp;
307 psz_module = psz_parser;
308 psz_parser = strchr( psz_module, ',' );
314 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
317 intf_thread_t *p_new_intf;
318 sprintf( psz_temp, "%s,none", psz_module );
320 /* Try to create the interface */
321 p_new_intf = intf_Create( p_intf, psz_temp, 0, NULL );
322 if( p_new_intf == NULL )
324 msg_Err( p_intf, "interface \"%s\" initialization failed",
330 /* Try to run the interface */
331 if( intf_RunThread( p_new_intf ) )
333 vlc_object_detach( p_new_intf );
334 intf_Destroy( p_new_intf );
335 msg_Err( p_intf, "interface \"%s\" cannot run", psz_temp );
343 /* Initialization complete - report running status */
344 p_sys->status.dwCurrentState = SERVICE_RUNNING;
345 p_sys->status.dwCheckPoint = 0;
346 p_sys->status.dwWaitHint = 0;
348 SetServiceStatus( p_sys->hStatus, &p_sys->status );
351 static void WINAPI ServiceCtrlHandler( DWORD control )
353 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
354 intf_sys_t *p_sys = p_intf->p_sys;
358 case SERVICE_CONTROL_SHUTDOWN:
359 case SERVICE_CONTROL_STOP:
360 p_sys->status.dwCurrentState = SERVICE_STOPPED;
361 p_sys->status.dwWin32ExitCode = 0;
362 p_sys->status.dwCheckPoint = 0;
363 p_sys->status.dwWaitHint = 0;
365 case SERVICE_CONTROL_INTERROGATE:
366 /* just set the current state to whatever it is... */
370 SetServiceStatus( p_sys->hStatus, &p_sys->status );