d3d11: handle VLC_CODEC_D3D11_OPAQUE_10B upload/download
[vlc.git] / modules / control / ntservice.c
blob995aa9dbb29f3e8f0669289a65749012c144ab0e
1 /*****************************************************************************
2 * ntservice.c: Windows NT/2K/XP service interface
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_charset.h>
37 #define VLCSERVICENAME "VLC media player"
39 /*****************************************************************************
40 * Module descriptor
41 *****************************************************************************/
42 static int Activate( vlc_object_t * );
43 static void Close ( vlc_object_t * );
45 #define INSTALL_TEXT N_( "Install Windows Service" )
46 #define INSTALL_LONGTEXT N_( \
47 "Install the Service and exit." )
48 #define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
49 #define UNINSTALL_LONGTEXT N_( \
50 "Uninstall the Service and exit." )
51 #define NAME_TEXT N_( "Display name of the Service" )
52 #define NAME_LONGTEXT N_( \
53 "Change the display name of the Service." )
54 #define OPTIONS_TEXT N_("Configuration options")
55 #define OPTIONS_LONGTEXT N_( \
56 "Configuration options that will be " \
57 "used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
58 "at install time so the Service is properly configured.")
59 #define EXTRAINTF_TEXT N_("Extra interface modules")
60 #define EXTRAINTF_LONGTEXT N_( \
61 "Additional interfaces spawned by the " \
62 "Service. It should be specified at install time so the Service is " \
63 "properly configured. Use a comma separated list of interface modules. " \
64 "(common values are: logger, sap, rc, http)")
66 vlc_module_begin ()
67 set_shortname( N_("NT Service"))
68 set_description( N_("Windows Service interface") )
69 set_category( CAT_INTERFACE )
70 set_subcategory( SUBCAT_INTERFACE_CONTROL )
71 add_bool( "ntservice-install", false,
72 INSTALL_TEXT, INSTALL_LONGTEXT, true )
73 add_bool( "ntservice-uninstall", false,
74 UNINSTALL_TEXT, UNINSTALL_LONGTEXT, true )
75 add_string ( "ntservice-name", VLCSERVICENAME,
76 NAME_TEXT, NAME_LONGTEXT, true )
77 add_string ( "ntservice-options", NULL,
78 OPTIONS_TEXT, OPTIONS_LONGTEXT, true )
79 add_string ( "ntservice-extraintf", NULL,
80 EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, true )
82 set_capability( "interface", 0 )
83 set_callbacks( Activate, Close )
84 vlc_module_end ()
86 struct intf_sys_t
88 SERVICE_STATUS_HANDLE hStatus;
89 SERVICE_STATUS status;
90 char *psz_service;
91 vlc_thread_t thread;
94 /*****************************************************************************
95 * Local prototypes
96 *****************************************************************************/
97 static void *Run( void * );
98 static int NTServiceInstall( intf_thread_t *p_intf );
99 static int NTServiceUninstall( intf_thread_t *p_intf );
100 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
101 static void WINAPI ServiceCtrlHandler( DWORD control );
103 /* We need this global */
104 static intf_thread_t *p_global_intf;
106 /*****************************************************************************
107 * Activate: initialize and create stuff
108 *****************************************************************************/
109 static int Activate( vlc_object_t *p_this )
111 intf_thread_t *p_intf = (intf_thread_t*)p_this;
112 intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
113 if( unlikely(p_sys == NULL) )
114 return VLC_ENOMEM;
116 p_intf->p_sys = p_sys;
118 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
119 return VLC_ENOMEM;
121 return VLC_SUCCESS;
124 /*****************************************************************************
125 * Close: destroy interface
126 *****************************************************************************/
127 void Close( vlc_object_t *p_this )
129 intf_thread_t *p_intf = (intf_thread_t*)p_this;
130 intf_sys_t *p_sys = p_intf->p_sys;
132 vlc_join( p_sys->thread, NULL );
133 free( p_sys );
136 /*****************************************************************************
137 * Run: interface thread
138 *****************************************************************************/
139 static void *Run( void *data )
141 intf_thread_t *p_intf = data;
142 SERVICE_TABLE_ENTRY dispatchTable[] =
144 { TEXT(VLCSERVICENAME), &ServiceDispatch },
145 { NULL, NULL }
148 p_global_intf = p_intf;
149 p_intf->p_sys->psz_service = var_InheritString( p_intf, "ntservice-name" );
150 p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
151 p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
153 if( var_InheritBool( p_intf, "ntservice-install" ) )
155 NTServiceInstall( p_intf );
156 return NULL;
159 if( var_InheritBool( p_intf, "ntservice-uninstall" ) )
161 NTServiceUninstall( p_intf );
162 return NULL;
165 if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
167 msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
170 free( p_intf->p_sys->psz_service );
172 /* Make sure we exit (In case other interfaces have been spawned) */
173 libvlc_Quit( p_intf->obj.libvlc );
174 return NULL;
177 /*****************************************************************************
178 * NT Service utility functions
179 *****************************************************************************/
180 static int NTServiceInstall( intf_thread_t *p_intf )
182 intf_sys_t *p_sys = p_intf->p_sys;
183 char psz_path[10*MAX_PATH], *psz_extra;
184 TCHAR psz_pathtmp[MAX_PATH];
186 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
187 if( handle == NULL )
189 msg_Err( p_intf,
190 "could not connect to Services Control Manager database" );
191 return VLC_EGENERIC;
194 /* Find out the filename of ourselves so we can install it to the
195 * service control manager */
196 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
197 sprintf( psz_path, "\"%s\" -I ntservice", FromT(psz_pathtmp) );
199 psz_extra = var_InheritString( p_intf, "ntservice-extraintf" );
200 if( psz_extra && *psz_extra )
202 strcat( psz_path, " --ntservice-extraintf " );
203 strncat( psz_path, psz_extra, MAX_PATH - strlen( psz_path ) - 1 );
205 free( psz_extra );
207 psz_extra = var_InheritString( p_intf, "ntservice-options" );
208 if( psz_extra && *psz_extra )
210 strcat( psz_path, " " );
211 strncat( psz_path, psz_extra, MAX_PATH - strlen( psz_path ) - 1 );
213 free( psz_extra );
215 SC_HANDLE service =
216 CreateServiceA( handle, p_sys->psz_service, p_sys->psz_service,
217 GENERIC_READ | GENERIC_EXECUTE,
218 SERVICE_WIN32_OWN_PROCESS,
219 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
220 psz_path, NULL, NULL, NULL, NULL, NULL );
221 if( service == NULL )
223 if( GetLastError() != ERROR_SERVICE_EXISTS )
225 msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
226 p_sys->psz_service ,psz_path );
227 CloseServiceHandle( handle );
228 return VLC_EGENERIC;
230 else
232 msg_Warn( p_intf, "service \"%s\" already exists",
233 p_sys->psz_service );
236 else
238 msg_Warn( p_intf, "service successfuly created" );
241 if( service ) CloseServiceHandle( service );
242 CloseServiceHandle( handle );
244 return VLC_SUCCESS;
247 static int NTServiceUninstall( intf_thread_t *p_intf )
249 intf_sys_t *p_sys = p_intf->p_sys;
251 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
252 if( handle == NULL )
254 msg_Err( p_intf,
255 "could not connect to Services Control Manager database" );
256 return VLC_EGENERIC;
259 /* First, open a handle to the service */
260 SC_HANDLE service = OpenServiceA( handle, p_sys->psz_service, DELETE );
261 if( service == NULL )
263 msg_Err( p_intf, "could not open service" );
264 CloseServiceHandle( handle );
265 return VLC_EGENERIC;
268 /* Remove the service */
269 if( !DeleteService( service ) )
271 msg_Err( p_intf, "could not delete service \"%s\"",
272 p_sys->psz_service );
274 else
276 msg_Dbg( p_intf, "service deleted successfuly" );
279 CloseServiceHandle( service );
280 CloseServiceHandle( handle );
282 return VLC_SUCCESS;
285 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
287 (void)numArgs;
288 (void)args;
289 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
290 intf_sys_t *p_sys = p_intf->p_sys;
291 char *psz_modules, *psz_parser;
293 /* We have to initialize the service-specific stuff */
294 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
295 p_sys->status.dwServiceType = SERVICE_WIN32;
296 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
297 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
299 p_sys->hStatus =
300 RegisterServiceCtrlHandlerA( p_sys->psz_service, &ServiceCtrlHandler );
301 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
303 msg_Err( p_intf, "failed to register service control handler" );
304 return;
308 * Load background interfaces
310 psz_modules = var_InheritString( p_intf, "ntservice-extraintf" );
311 psz_parser = psz_modules;
312 while( psz_parser && *psz_parser )
314 char *psz_module, *psz_temp;
315 psz_module = psz_parser;
316 psz_parser = strchr( psz_module, ',' );
317 if( psz_parser )
319 *psz_parser = '\0';
320 psz_parser++;
323 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
325 /* Try to create the interface */
326 if( intf_Create( pl_Get(p_intf), psz_temp ) )
328 msg_Err( p_intf, "interface \"%s\" initialization failed",
329 psz_temp );
330 free( psz_temp );
331 continue;
333 free( psz_temp );
336 free( psz_modules );
338 /* Initialization complete - report running status */
339 p_sys->status.dwCurrentState = SERVICE_RUNNING;
340 p_sys->status.dwCheckPoint = 0;
341 p_sys->status.dwWaitHint = 0;
343 SetServiceStatus( p_sys->hStatus, &p_sys->status );
346 static void WINAPI ServiceCtrlHandler( DWORD control )
348 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
349 intf_sys_t *p_sys = p_intf->p_sys;
351 switch( control )
353 case SERVICE_CONTROL_SHUTDOWN:
354 case SERVICE_CONTROL_STOP:
355 p_sys->status.dwCurrentState = SERVICE_STOPPED;
356 p_sys->status.dwWin32ExitCode = 0;
357 p_sys->status.dwCheckPoint = 0;
358 p_sys->status.dwWaitHint = 0;
359 break;
360 case SERVICE_CONTROL_INTERROGATE:
361 /* just set the current state to whatever it is... */
362 break;
365 SetServiceStatus( p_sys->hStatus, &p_sys->status );