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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
35 #define VLCSERVICENAME "VLC media player"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Activate( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 #define INSTALL_TEXT N_( "Install Windows Service" )
44 #define INSTALL_LONGTEXT N_( \
45 "Install the Service and exit." )
46 #define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
47 #define UNINSTALL_LONGTEXT N_( \
48 "Uninstall the Service and exit." )
49 #define NAME_TEXT N_( "Display name of the Service" )
50 #define NAME_LONGTEXT N_( \
51 "Change the display name of the Service." )
52 #define OPTIONS_TEXT N_("Configuration options")
53 #define OPTIONS_LONGTEXT N_( \
54 "Configuration options that will be " \
55 "used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
56 "at install time so the Service is properly configured.")
57 #define EXTRAINTF_TEXT N_("Extra interface modules")
58 #define EXTRAINTF_LONGTEXT N_( \
59 "Additional interfaces spawned by the " \
60 "Service. It should be specified at install time so the Service is " \
61 "properly configured. Use a comma separated list of interface modules. " \
62 "(common values are: logger, sap, rc, http)")
65 set_shortname( N_("NT Service"));
66 set_description( N_("Windows Service interface") );
67 set_category( CAT_INTERFACE );
68 set_subcategory( SUBCAT_INTERFACE_CONTROL );
69 add_bool( "ntservice-install", 0, NULL,
70 INSTALL_TEXT, INSTALL_LONGTEXT, true );
71 add_bool( "ntservice-uninstall", 0, NULL,
72 UNINSTALL_TEXT, UNINSTALL_LONGTEXT, true );
73 add_string ( "ntservice-name", VLCSERVICENAME, NULL,
74 NAME_TEXT, NAME_LONGTEXT, true );
75 add_string ( "ntservice-options", NULL, NULL,
76 OPTIONS_TEXT, OPTIONS_LONGTEXT, true );
77 add_string ( "ntservice-extraintf", NULL, NULL,
78 EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, true );
80 set_capability( "interface", 0 );
81 set_callbacks( Activate, Close );
86 SERVICE_STATUS_HANDLE hStatus;
87 SERVICE_STATUS status;
91 /*****************************************************************************
93 *****************************************************************************/
94 static void Run( intf_thread_t *p_intf );
95 static int NTServiceInstall( intf_thread_t *p_intf );
96 static int NTServiceUninstall( intf_thread_t *p_intf );
97 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
98 static void WINAPI ServiceCtrlHandler( DWORD control );
100 /* We need this global */
101 static intf_thread_t *p_global_intf;
103 /*****************************************************************************
104 * Activate: initialize and create stuff
105 *****************************************************************************/
106 static int Activate( vlc_object_t *p_this )
108 intf_thread_t *p_intf = (intf_thread_t*)p_this;
110 /* Only works on NT/2K/XP */
111 if( !IS_WINNT ) return VLC_EGENERIC;
113 p_intf->pf_run = Run;
117 /*****************************************************************************
118 * Close: destroy interface
119 *****************************************************************************/
120 void Close( vlc_object_t *p_this )
125 /*****************************************************************************
126 * Run: interface thread
127 *****************************************************************************/
128 static void Run( intf_thread_t *p_intf )
131 intf_thread_t *p_extraintf;
132 SERVICE_TABLE_ENTRY dispatchTable[] =
134 { VLCSERVICENAME, &ServiceDispatch },
138 int canc = vlc_savecancel();
139 p_global_intf = p_intf;
140 p_intf->p_sys = &sys;
141 p_intf->p_sys->psz_service = config_GetPsz( p_intf, "ntservice-name" );
142 p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
143 p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
145 if( config_GetInt( p_intf, "ntservice-install" ) )
147 NTServiceInstall( p_intf );
151 if( config_GetInt( p_intf, "ntservice-uninstall" ) )
153 NTServiceUninstall( p_intf );
157 if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
159 msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
162 free( p_intf->p_sys->psz_service );
164 /* Stop and destroy the interfaces we spawned */
165 while( (p_extraintf = vlc_object_find(p_intf, VLC_OBJECT_INTF, FIND_CHILD)))
167 intf_StopThread( p_extraintf );
168 vlc_object_detach( p_extraintf );
169 vlc_object_release( p_extraintf );
170 vlc_object_release( p_extraintf );
173 /* Make sure we exit (In case other interfaces have been spawned) */
174 vlc_object_kill( p_intf->p_libvlc );
175 vlc_restorecancel( canc );
178 /*****************************************************************************
179 * NT Service utility functions
180 *****************************************************************************/
181 static int NTServiceInstall( intf_thread_t *p_intf )
183 intf_sys_t *p_sys = p_intf->p_sys;
184 char psz_path[10*MAX_PATH], psz_pathtmp[MAX_PATH], *psz_extra;
185 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
189 "could not connect to Services Control Manager database" );
193 /* Find out the filename of ourselves so we can install it to the
194 * service control manager */
195 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
196 sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
198 psz_extra = config_GetPsz( p_intf, "ntservice-extraintf" );
199 if( psz_extra && *psz_extra )
201 strcat( psz_path, " --ntservice-extraintf " );
202 strcat( psz_path, psz_extra );
206 psz_extra = config_GetPsz( p_intf, "ntservice-options" );
207 if( psz_extra && *psz_extra )
209 strcat( psz_path, " " );
210 strcat( psz_path, psz_extra );
215 CreateService( handle, p_sys->psz_service, p_sys->psz_service,
216 GENERIC_READ | GENERIC_EXECUTE,
217 SERVICE_WIN32_OWN_PROCESS,
218 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
219 psz_path, NULL, NULL, NULL, NULL, NULL );
220 if( service == NULL )
222 if( GetLastError() != ERROR_SERVICE_EXISTS )
224 msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
225 p_sys->psz_service ,psz_path );
226 CloseServiceHandle( handle );
231 msg_Warn( p_intf, "service \"%s\" already exists",
232 p_sys->psz_service );
237 msg_Warn( p_intf, "service successfuly created" );
240 if( service ) CloseServiceHandle( service );
241 CloseServiceHandle( handle );
246 static int NTServiceUninstall( intf_thread_t *p_intf )
248 intf_sys_t *p_sys = p_intf->p_sys;
250 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
254 "could not connect to Services Control Manager database" );
258 /* First, open a handle to the service */
259 SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
260 if( service == NULL )
262 msg_Err( p_intf, "could not open service" );
263 CloseServiceHandle( handle );
267 /* Remove the service */
268 if( !DeleteService( service ) )
270 msg_Err( p_intf, "could not delete service \"%s\"",
271 p_sys->psz_service );
275 msg_Dbg( p_intf, "service deleted successfuly" );
278 CloseServiceHandle( service );
279 CloseServiceHandle( handle );
284 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
288 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
289 intf_sys_t *p_sys = p_intf->p_sys;
290 char *psz_modules, *psz_parser;
292 /* We have to initialize the service-specific stuff */
293 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
294 p_sys->status.dwServiceType = SERVICE_WIN32;
295 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
296 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
299 RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
300 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
302 msg_Err( p_intf, "failed to register service control handler" );
307 * Load background interfaces
309 psz_modules = config_GetPsz( p_intf, "ntservice-extraintf" );
310 psz_parser = psz_modules;
311 while( psz_parser && *psz_parser )
313 char *psz_module, *psz_temp;
314 psz_module = psz_parser;
315 psz_parser = strchr( psz_module, ',' );
321 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
324 intf_thread_t *p_new_intf;
325 sprintf( psz_temp, "%s,none", psz_module );
327 /* Try to create the interface */
328 p_new_intf = intf_Create( p_intf, psz_temp );
329 if( p_new_intf == NULL )
331 msg_Err( p_intf, "interface \"%s\" initialization failed",
337 /* Try to run the interface */
338 if( intf_RunThread( p_new_intf ) )
340 vlc_object_detach( p_new_intf );
341 vlc_object_release( p_new_intf );
342 msg_Err( p_intf, "interface \"%s\" cannot run", psz_temp );
350 /* Initialization complete - report running status */
351 p_sys->status.dwCurrentState = SERVICE_RUNNING;
352 p_sys->status.dwCheckPoint = 0;
353 p_sys->status.dwWaitHint = 0;
355 SetServiceStatus( p_sys->hStatus, &p_sys->status );
358 static void WINAPI ServiceCtrlHandler( DWORD control )
360 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
361 intf_sys_t *p_sys = p_intf->p_sys;
365 case SERVICE_CONTROL_SHUTDOWN:
366 case SERVICE_CONTROL_STOP:
367 p_sys->status.dwCurrentState = SERVICE_STOPPED;
368 p_sys->status.dwWin32ExitCode = 0;
369 p_sys->status.dwCheckPoint = 0;
370 p_sys->status.dwWaitHint = 0;
372 case SERVICE_CONTROL_INTERROGATE:
373 /* just set the current state to whatever it is... */
377 SetServiceStatus( p_sys->hStatus, &p_sys->status );