1 /*****************************************************************************
2 * ntservice.c: Windows NT/2K/XP service interface
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ntservice.c,v 1.2 2003/08/14 11:47:32 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 NT/2K/XP service" )
40 #define INSTALL_LONGTEXT N_( \
41 "If enabled the interface will install the service and exit." )
42 #define UNINSTALL_TEXT N_( "Uninstall NT/2K/XP 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 EXTRAINTF_TEXT N_("Extra interface modules")
49 #define EXTRAINTF_LONGTEXT N_( \
50 "This option allows you to select additional interfaces spawned by the " \
51 "service. It should be specified at install time so the service is " \
52 "properly configured. Use a comma separated list of interface modules. " \
53 "(common values are: logger,sap,rc,http)")
56 set_description( _("Windows NT/2K/XP service interface") );
57 add_category_hint( N_("NT service"), NULL, VLC_TRUE );
58 add_bool( "ntservice-install", 0, NULL, INSTALL_TEXT, INSTALL_LONGTEXT, VLC_TRUE );
59 add_bool( "ntservice-uninstall", 0, NULL, INSTALL_TEXT, INSTALL_LONGTEXT, VLC_TRUE );
60 add_string ( "ntservice-name", VLCSERVICENAME, NULL, NAME_TEXT, NAME_LONGTEXT, VLC_TRUE );
61 add_string ( "ntservice-extraintf", NULL, NULL, EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, VLC_TRUE );
63 set_capability( "interface", 0 );
64 set_callbacks( Activate, Close );
69 SERVICE_STATUS_HANDLE hStatus;
70 SERVICE_STATUS status;
74 /*****************************************************************************
76 *****************************************************************************/
77 static void Run( intf_thread_t *p_intf );
78 static int NTServiceInstall( intf_thread_t *p_intf );
79 static int NTServiceUninstall( intf_thread_t *p_intf );
80 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
81 static void WINAPI ServiceCtrlHandler( DWORD control );
83 /* We need this global */
84 static intf_thread_t *p_global_intf;
86 /*****************************************************************************
87 * Activate: initialize and create stuff
88 *****************************************************************************/
89 static int Activate( vlc_object_t *p_this )
91 intf_thread_t *p_intf = (intf_thread_t*)p_this;
93 /* Only works on NT/2K/XP */
94 if( !IS_WINNT ) return VLC_EGENERIC;
100 /*****************************************************************************
101 * Close: destroy interface
102 *****************************************************************************/
103 void Close( vlc_object_t *p_this )
107 /*****************************************************************************
108 * Run: interface thread
109 *****************************************************************************/
110 static void Run( intf_thread_t *p_intf )
112 SERVICE_TABLE_ENTRY dispatchTable[] =
114 { VLCSERVICENAME, &ServiceDispatch },
118 p_global_intf = p_intf;
119 p_intf->p_sys = alloca( sizeof( intf_sys_t ) );
120 p_intf->p_sys->psz_service = config_GetPsz( p_intf, "ntservice-name" );
121 p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
122 p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
124 if( config_GetInt( p_intf, "ntservice-install" ) )
126 NTServiceInstall( p_intf );
130 if( config_GetInt( p_intf, "ntservice-uninstall" ) )
132 NTServiceUninstall( p_intf );
136 if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
138 msg_Err( p_intf, "StartServiceCtrlDispatcher failed" );
141 free( p_intf->p_sys->psz_service );
143 /* Make sure we exit (In case other interfaces have been spawned) */
144 p_intf->p_vlc->b_die = VLC_TRUE;
147 /*****************************************************************************
148 * NT Service utility functions
149 *****************************************************************************/
150 static int NTServiceInstall( intf_thread_t *p_intf )
152 intf_sys_t *p_sys = p_intf->p_sys;
153 char psz_path[MAX_PATH], psz_pathtmp[MAX_PATH], *psz_extraintf;
154 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
157 msg_Err( p_intf, "Could not connect to SCM database" );
161 /* Find out the filename of ourselves so we can install it to the
162 * service control manager */
163 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
164 sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
166 psz_extraintf = config_GetPsz( p_intf, "ntservice-extraintf" );
167 if( psz_extraintf && *psz_extraintf )
169 strcat( psz_path, " --ntservice-extraintf " );
170 strcat( psz_path, psz_extraintf );
172 if( psz_extraintf ) free( psz_extraintf );
175 CreateService( handle, p_sys->psz_service, p_sys->psz_service,
176 GENERIC_READ | GENERIC_EXECUTE,
177 SERVICE_WIN32_OWN_PROCESS,
178 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
179 psz_path, NULL, NULL, NULL, NULL, NULL );
180 if( service == NULL )
182 if( GetLastError() != ERROR_SERVICE_EXISTS )
184 msg_Err( p_intf, "Could not create new service: \"%s\" (%s)",
185 p_sys->psz_service ,psz_path );
186 CloseServiceHandle( handle );
191 msg_Warn( p_intf, "Service \"%s\" already exists",
192 p_sys->psz_service );
197 msg_Warn( p_intf, "Service successfuly created" );
200 if( service ) CloseServiceHandle( service );
201 CloseServiceHandle( handle );
206 static int NTServiceUninstall( intf_thread_t *p_intf )
208 intf_sys_t *p_sys = p_intf->p_sys;
210 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
213 msg_Err( p_intf, "Could not connect to SCM database" );
217 /* First, open a handle to the service */
218 SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
219 if( service == NULL )
221 msg_Err( p_intf, "Could not open service" );
222 CloseServiceHandle( handle );
226 /* Remove the service */
227 if( !DeleteService( service ) )
229 msg_Err( p_intf, "Could not delete service \"%s\"",
230 p_sys->psz_service );
234 msg_Dbg( p_intf, "Service deleted successfuly" );
237 CloseServiceHandle( service );
238 CloseServiceHandle( handle );
243 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
245 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
246 intf_sys_t *p_sys = p_intf->p_sys;
247 char *psz_modules, *psz_parser;
249 /* We have to initialize the service-specific stuff */
250 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
251 p_sys->status.dwServiceType = SERVICE_WIN32;
252 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
253 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
256 RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
257 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
259 msg_Err( p_intf, "Failed to register service control handler" );
264 * Load background interfaces
266 psz_modules = config_GetPsz( p_intf, "ntservice-extraintf" );
267 psz_parser = psz_modules;
268 while( psz_parser && *psz_parser )
270 char *psz_module, *psz_temp;
271 psz_module = psz_parser;
272 psz_parser = strchr( psz_module, ',' );
278 psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
281 intf_thread_t *p_new_intf;
282 sprintf( psz_temp, "%s,none", psz_module );
284 /* Try to create the interface */
285 p_new_intf = intf_Create( p_intf, psz_temp );
286 if( p_new_intf == NULL )
288 msg_Err( p_intf, "interface \"%s\" initialization failed",
294 /* Try to run the interface */
295 p_new_intf->b_block = VLC_FALSE;
296 if( intf_RunThread( p_new_intf ) )
298 vlc_object_detach( p_new_intf );
299 intf_Destroy( p_new_intf );
300 msg_Err( p_intf, "interface \"%s\" cannot run", psz_temp );
311 /* Initialization complete - report running status */
312 p_sys->status.dwCurrentState = SERVICE_RUNNING;
313 p_sys->status.dwCheckPoint = 0;
314 p_sys->status.dwWaitHint = 0;
316 SetServiceStatus( p_sys->hStatus, &p_sys->status );
319 static void WINAPI ServiceCtrlHandler( DWORD control )
321 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
322 intf_sys_t *p_sys = p_intf->p_sys;
326 case SERVICE_CONTROL_SHUTDOWN:
327 case SERVICE_CONTROL_STOP:
328 p_sys->status.dwCurrentState = SERVICE_STOPPED;
329 p_sys->status.dwWin32ExitCode = 0;
330 p_sys->status.dwCheckPoint = 0;
331 p_sys->status.dwWaitHint = 0;
333 case SERVICE_CONTROL_INTERROGATE:
334 /* just set the current state to whatever it is... */
338 SetServiceStatus( p_sys->hStatus, &p_sys->status );