1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/*****************************************************************************
* ntservice.c: Windows NT/2K/XP service interface
*****************************************************************************
* Copyright (C) 2004 the VideoLAN team
* $Id$
*
* Authors: Gildas Bazin <gbazin@videolan.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_interface.h>
#include <vlc_charset.h>
#include <vlc_memstream.h>
#define VLCSERVICENAME "VLC media player"
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Activate( vlc_object_t * );
static void Close ( vlc_object_t * );
#define INSTALL_TEXT N_( "Install Windows Service" )
#define INSTALL_LONGTEXT N_( \
"Install the Service and exit." )
#define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
#define UNINSTALL_LONGTEXT N_( \
"Uninstall the Service and exit." )
#define NAME_TEXT N_( "Display name of the Service" )
#define NAME_LONGTEXT N_( \
"Change the display name of the Service." )
#define OPTIONS_TEXT N_("Configuration options")
#define OPTIONS_LONGTEXT N_( \
"Configuration options that will be " \
"used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
"at install time so the Service is properly configured.")
#define EXTRAINTF_TEXT N_("Extra interface modules")
#define EXTRAINTF_LONGTEXT N_( \
"Additional interfaces spawned by the " \
"Service. It should be specified at install time so the Service is " \
"properly configured. Use a comma separated list of interface modules. " \
"(common values are: logger, sap, rc, http)")
vlc_module_begin ()
set_shortname( N_("NT Service"))
set_description( N_("Windows Service interface") )
set_category( CAT_INTERFACE )
set_subcategory( SUBCAT_INTERFACE_CONTROL )
add_bool( "ntservice-install", false,
INSTALL_TEXT, INSTALL_LONGTEXT, true )
add_bool( "ntservice-uninstall", false,
UNINSTALL_TEXT, UNINSTALL_LONGTEXT, true )
add_string ( "ntservice-name", VLCSERVICENAME,
NAME_TEXT, NAME_LONGTEXT, true )
add_string ( "ntservice-options", NULL,
OPTIONS_TEXT, OPTIONS_LONGTEXT, true )
add_string ( "ntservice-extraintf", NULL,
EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, true )
set_capability( "interface", 0 )
set_callbacks( Activate, Close )
vlc_module_end ()
struct intf_sys_t
{
SERVICE_STATUS_HANDLE hStatus;
SERVICE_STATUS status;
char *psz_service;
vlc_thread_t thread;
};
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void *Run( void * );
static int NTServiceInstall( intf_thread_t *p_intf );
static int NTServiceUninstall( intf_thread_t *p_intf );
static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
static void WINAPI ServiceCtrlHandler( DWORD control );
/* We need this global */
static intf_thread_t *p_global_intf;
/*****************************************************************************
* Activate: initialize and create stuff
*****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
intf_thread_t *p_intf = (intf_thread_t*)p_this;
intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
if( unlikely(p_sys == NULL) )
return VLC_ENOMEM;
p_intf->p_sys = p_sys;
if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
return VLC_ENOMEM;
return VLC_SUCCESS;
}
/*****************************************************************************
* Close: destroy interface
*****************************************************************************/
void Close( vlc_object_t *p_this )
{
intf_thread_t *p_intf = (intf_thread_t*)p_this;
intf_sys_t *p_sys = p_intf->p_sys;
vlc_join( p_sys->thread, NULL );
free( p_sys );
}
/*****************************************************************************
* Run: interface thread
*****************************************************************************/
static void *Run( void *data )
{
intf_thread_t *p_intf = data;
const SERVICE_TABLE_ENTRYA dispatchTable[] =
{
{ (LPSTR)VLCSERVICENAME, (LPSERVICE_MAIN_FUNCTIONA) &ServiceDispatch },
{ NULL, NULL }
};
p_global_intf = p_intf;
p_intf->p_sys->psz_service = var_InheritString( p_intf, "ntservice-name" );
p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
if( var_InheritBool( p_intf, "ntservice-install" ) )
{
NTServiceInstall( p_intf );
free( p_intf->p_sys->psz_service );
return NULL;
}
if( var_InheritBool( p_intf, "ntservice-uninstall" ) )
{
NTServiceUninstall( p_intf );
free( p_intf->p_sys->psz_service );
return NULL;
}
if( StartServiceCtrlDispatcherA( dispatchTable ) == 0 )
{
msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
}
free( p_intf->p_sys->psz_service );
/* Make sure we exit (In case other interfaces have been spawned) */
libvlc_Quit( p_intf->obj.libvlc );
return NULL;
}
/*****************************************************************************
* NT Service utility functions
*****************************************************************************/
static int NTServiceInstall( intf_thread_t *p_intf )
{
intf_sys_t *p_sys = p_intf->p_sys;
char *psz_extra;
struct vlc_memstream path_stream;
WCHAR psz_pathtmp[MAX_PATH];
SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( handle == NULL )
{
msg_Err( p_intf,
"could not connect to Services Control Manager database" );
return VLC_EGENERIC;
}
if( vlc_memstream_open(&path_stream) != 0 )
{
CloseServiceHandle( handle );
return VLC_ENOMEM;
}
/* Find out the filename of ourselves so we can install it to the
* service control manager */
GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
vlc_memstream_printf( &path_stream, "\"%ls\" -I ntservice", psz_pathtmp );
psz_extra = var_InheritString( p_intf, "ntservice-extraintf" );
if( psz_extra && *psz_extra )
vlc_memstream_printf( &path_stream, " --ntservice-extraintf %s", psz_extra );
free( psz_extra );
psz_extra = var_InheritString( p_intf, "ntservice-options" );
if( psz_extra && *psz_extra )
vlc_memstream_printf( &path_stream, " %s", psz_extra );
free( psz_extra );
if ( vlc_memstream_close( &path_stream ) != 0 )
{
CloseServiceHandle( handle );
return VLC_ENOMEM;
}
SC_HANDLE service =
CreateServiceA( handle, p_sys->psz_service, p_sys->psz_service,
GENERIC_READ | GENERIC_EXECUTE,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
path_stream.ptr, NULL, NULL, NULL, NULL, NULL );
if( service == NULL )
{
if( GetLastError() != ERROR_SERVICE_EXISTS )
{
msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
p_sys->psz_service, path_stream.ptr );
free( path_stream.ptr );
CloseServiceHandle( handle );
return VLC_EGENERIC;
}
else
{
msg_Warn( p_intf, "service \"%s\" already exists",
p_sys->psz_service );
}
}
else
{
msg_Warn( p_intf, "service successfully created" );
}
free( path_stream.ptr );
if( service ) CloseServiceHandle( service );
CloseServiceHandle( handle );
return VLC_SUCCESS;
}
static int NTServiceUninstall( intf_thread_t *p_intf )
{
intf_sys_t *p_sys = p_intf->p_sys;
SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( handle == NULL )
{
msg_Err( p_intf,
"could not connect to Services Control Manager database" );
return VLC_EGENERIC;
}
/* First, open a handle to the service */
SC_HANDLE service = OpenServiceA( handle, p_sys->psz_service, DELETE );
if( service == NULL )
{
msg_Err( p_intf, "could not open service" );
CloseServiceHandle( handle );
return VLC_EGENERIC;
}
/* Remove the service */
if( !DeleteService( service ) )
{
msg_Err( p_intf, "could not delete service \"%s\"",
p_sys->psz_service );
}
else
{
msg_Dbg( p_intf, "service deleted successfully" );
}
CloseServiceHandle( service );
CloseServiceHandle( handle );
return VLC_SUCCESS;
}
static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
{
(void)numArgs;
(void)args;
intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
intf_sys_t *p_sys = p_intf->p_sys;
char *psz_modules, *psz_parser;
/* We have to initialize the service-specific stuff */
memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
p_sys->status.dwServiceType = SERVICE_WIN32;
p_sys->status.dwCurrentState = SERVICE_START_PENDING;
p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
p_sys->hStatus =
RegisterServiceCtrlHandlerA( p_sys->psz_service, &ServiceCtrlHandler );
if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
{
msg_Err( p_intf, "failed to register service control handler" );
return;
}
/*
* Load background interfaces
*/
psz_modules = var_InheritString( p_intf, "ntservice-extraintf" );
psz_parser = psz_modules;
while( psz_parser && *psz_parser )
{
char *psz_module, *psz_temp;
psz_module = psz_parser;
psz_parser = strchr( psz_module, ',' );
if( psz_parser )
{
*psz_parser = '\0';
psz_parser++;
}
if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
{
/* Try to create the interface */
if( intf_Create( pl_Get(p_intf), psz_temp ) )
{
msg_Err( p_intf, "interface \"%s\" initialization failed",
psz_temp );
free( psz_temp );
continue;
}
free( psz_temp );
}
}
free( psz_modules );
/* Initialization complete - report running status */
p_sys->status.dwCurrentState = SERVICE_RUNNING;
p_sys->status.dwCheckPoint = 0;
p_sys->status.dwWaitHint = 0;
SetServiceStatus( p_sys->hStatus, &p_sys->status );
}
static void WINAPI ServiceCtrlHandler( DWORD control )
{
intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
intf_sys_t *p_sys = p_intf->p_sys;
switch( control )
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
p_sys->status.dwCurrentState = SERVICE_STOPPED;
p_sys->status.dwWin32ExitCode = 0;
p_sys->status.dwCheckPoint = 0;
p_sys->status.dwWaitHint = 0;
break;
case SERVICE_CONTROL_INTERROGATE:
/* just set the current state to whatever it is... */
break;
}
SetServiceStatus( p_sys->hStatus, &p_sys->status );
}
|