1 /*****************************************************************************
2 * ntservice.c: Windows NT/2K/XP service interface
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ntservice.c,v 1.1 2003/08/13 23:26:55 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." )
50 set_description( _("Windows NT/2K/XP service interface") );
51 add_category_hint( N_("NT service"), NULL, VLC_TRUE );
52 add_bool( "ntservice-install", 0, NULL, INSTALL_TEXT, INSTALL_LONGTEXT, VLC_TRUE );
53 add_bool( "ntservice-uninstall", 0, NULL, INSTALL_TEXT, INSTALL_LONGTEXT, VLC_TRUE );
54 add_string ( "ntservice-name", VLCSERVICENAME, NULL, NAME_TEXT, NAME_LONGTEXT, VLC_TRUE );
56 set_capability( "interface", 0 );
57 set_callbacks( Activate, Close );
62 SERVICE_STATUS_HANDLE hStatus;
63 SERVICE_STATUS status;
67 /*****************************************************************************
69 *****************************************************************************/
70 static void Run( intf_thread_t *p_intf );
71 static int NTServiceInstall( intf_thread_t *p_intf );
72 static int NTServiceUninstall( intf_thread_t *p_intf );
73 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
74 static void WINAPI ServiceCtrlHandler( DWORD control );
76 /* We need this global */
77 static intf_thread_t *p_global_intf;
79 /*****************************************************************************
80 * Activate: initialize and create stuff
81 *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
84 intf_thread_t *p_intf = (intf_thread_t*)p_this;
86 /* Only works on NT/2K/XP */
87 if( !IS_WINNT ) return VLC_EGENERIC;
93 /*****************************************************************************
94 * Close: destroy interface
95 *****************************************************************************/
96 void Close( vlc_object_t *p_this )
100 /*****************************************************************************
101 * Run: interface thread
102 *****************************************************************************/
103 static void Run( intf_thread_t *p_intf )
105 SERVICE_TABLE_ENTRY dispatchTable[] =
107 { VLCSERVICENAME, &ServiceDispatch },
111 p_global_intf = p_intf;
112 p_intf->p_sys = alloca( sizeof( intf_sys_t ) );
113 p_intf->p_sys->psz_service = config_GetPsz( p_intf, "ntservice-name" );
114 p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
115 p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
117 if( config_GetInt( p_intf, "ntservice-install" ) )
119 NTServiceInstall( p_intf );
123 if( config_GetInt( p_intf, "ntservice-uninstall" ) )
125 NTServiceUninstall( p_intf );
129 if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
131 msg_Err( p_intf, "StartServiceCtrlDispatcher failed" );
134 free( p_intf->p_sys->psz_service );
136 /* Make sure we exit (In case other interfaces have been spawned) */
137 p_intf->p_vlc->b_die = VLC_TRUE;
140 /*****************************************************************************
141 * NT Service utility functions
142 *****************************************************************************/
143 static int NTServiceInstall( intf_thread_t *p_intf )
145 intf_sys_t *p_sys = p_intf->p_sys;
146 char psz_path[MAX_PATH], psz_pathtmp[MAX_PATH];
147 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
150 msg_Err( p_intf, "Could not connect to SCM database" );
154 /* Find out the filename of ourselves so we can install it to the
155 * service control manager */
156 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
157 sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
160 CreateService( handle, p_sys->psz_service, p_sys->psz_service,
161 GENERIC_READ | GENERIC_EXECUTE,
162 SERVICE_WIN32_OWN_PROCESS,
163 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
164 psz_path, NULL, NULL, NULL, NULL, NULL );
165 if( service == NULL )
167 if( GetLastError() != ERROR_SERVICE_EXISTS )
169 msg_Err( p_intf, "Could not create new service" );
170 CloseServiceHandle( handle );
175 msg_Warn( p_intf, "Service already exists" );
180 msg_Warn( p_intf, "Service successfuly created" );
183 if( service ) CloseServiceHandle( service );
184 CloseServiceHandle( handle );
189 static int NTServiceUninstall( intf_thread_t *p_intf )
191 intf_sys_t *p_sys = p_intf->p_sys;
193 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
196 msg_Err( p_intf, "Could not connect to SCM database" );
200 /* First, open a handle to the service */
201 SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
202 if( service == NULL )
204 msg_Err( p_intf, "Could not open service" );
205 CloseServiceHandle( handle );
209 /* Remove the service */
210 if( !DeleteService( service ) )
212 msg_Err( p_intf, "Could not delete service" );
216 msg_Dbg( p_intf, "Service deleted successfuly" );
219 CloseServiceHandle( service );
220 CloseServiceHandle( handle );
225 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
227 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
228 intf_sys_t *p_sys = p_intf->p_sys;
230 /* We have to initialize the service-specific stuff */
231 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
232 p_sys->status.dwServiceType = SERVICE_WIN32;
233 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
234 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
237 RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
238 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
240 msg_Err( p_intf, "Failed to register service control handler" );
244 /* Initialization complete - report running status */
245 p_sys->status.dwCurrentState = SERVICE_RUNNING;
246 p_sys->status.dwCheckPoint = 0;
247 p_sys->status.dwWaitHint = 0;
249 SetServiceStatus( p_sys->hStatus, &p_sys->status );
252 static void WINAPI ServiceCtrlHandler( DWORD control )
254 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
255 intf_sys_t *p_sys = p_intf->p_sys;
259 case SERVICE_CONTROL_SHUTDOWN:
260 case SERVICE_CONTROL_STOP:
261 p_sys->status.dwCurrentState = SERVICE_STOPPED;
262 p_sys->status.dwWin32ExitCode = 0;
263 p_sys->status.dwCheckPoint = 0;
264 p_sys->status.dwWaitHint = 0;
266 case SERVICE_CONTROL_INTERROGATE:
267 /* just set the current state to whatever it is... */
271 SetServiceStatus( p_sys->hStatus, &p_sys->status );