demux: mp4: set bit per sample for twos
[vlc.git] / modules / control / ntservice.c
blob2215989a3e3d1d16ad62fa6bf2f156d4ecedeb3e
1 /*****************************************************************************
2 * ntservice.c: Windows NT/2K/XP service interface
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
6 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
34 #include <vlc_charset.h>
35 #include <vlc_memstream.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_extra;
184 struct vlc_memstream path_stream;
185 TCHAR psz_pathtmp[MAX_PATH];
187 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
188 if( handle == NULL )
190 msg_Err( p_intf,
191 "could not connect to Services Control Manager database" );
192 return VLC_EGENERIC;
195 if( vlc_memstream_open(&path_stream) != 0 )
197 CloseServiceHandle( handle );
198 return VLC_ENOMEM;
201 /* Find out the filename of ourselves so we can install it to the
202 * service control manager */
203 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
204 psz_extra = FromT( psz_pathtmp );
205 if ( !psz_extra )
207 CloseServiceHandle( handle );
208 return VLC_ENOMEM;
210 vlc_memstream_printf( &path_stream, "\"%s\" -I ntservice", psz_extra );
211 free(psz_extra);
213 psz_extra = var_InheritString( p_intf, "ntservice-extraintf" );
214 if( psz_extra && *psz_extra )
215 vlc_memstream_printf( &path_stream, " --ntservice-extraintf %s", psz_extra );
216 free( psz_extra );
218 psz_extra = var_InheritString( p_intf, "ntservice-options" );
219 if( psz_extra && *psz_extra )
220 vlc_memstream_printf( &path_stream, " %s", psz_extra );
221 free( psz_extra );
223 if ( vlc_memstream_close( &path_stream ) != 0 )
225 CloseServiceHandle( handle );
226 return VLC_ENOMEM;
229 SC_HANDLE service =
230 CreateServiceA( handle, p_sys->psz_service, p_sys->psz_service,
231 GENERIC_READ | GENERIC_EXECUTE,
232 SERVICE_WIN32_OWN_PROCESS,
233 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
234 path_stream.ptr, NULL, NULL, NULL, NULL, NULL );
235 if( service == NULL )
237 if( GetLastError() != ERROR_SERVICE_EXISTS )
239 msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
240 p_sys->psz_service, path_stream.ptr );
241 free( path_stream.ptr );
242 CloseServiceHandle( handle );
243 return VLC_EGENERIC;
245 else
247 msg_Warn( p_intf, "service \"%s\" already exists",
248 p_sys->psz_service );
251 else
253 msg_Warn( p_intf, "service successfuly created" );
256 free( path_stream.ptr );
258 if( service ) CloseServiceHandle( service );
259 CloseServiceHandle( handle );
261 return VLC_SUCCESS;
264 static int NTServiceUninstall( intf_thread_t *p_intf )
266 intf_sys_t *p_sys = p_intf->p_sys;
268 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
269 if( handle == NULL )
271 msg_Err( p_intf,
272 "could not connect to Services Control Manager database" );
273 return VLC_EGENERIC;
276 /* First, open a handle to the service */
277 SC_HANDLE service = OpenServiceA( handle, p_sys->psz_service, DELETE );
278 if( service == NULL )
280 msg_Err( p_intf, "could not open service" );
281 CloseServiceHandle( handle );
282 return VLC_EGENERIC;
285 /* Remove the service */
286 if( !DeleteService( service ) )
288 msg_Err( p_intf, "could not delete service \"%s\"",
289 p_sys->psz_service );
291 else
293 msg_Dbg( p_intf, "service deleted successfuly" );
296 CloseServiceHandle( service );
297 CloseServiceHandle( handle );
299 return VLC_SUCCESS;
302 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
304 (void)numArgs;
305 (void)args;
306 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
307 intf_sys_t *p_sys = p_intf->p_sys;
308 char *psz_modules, *psz_parser;
310 /* We have to initialize the service-specific stuff */
311 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
312 p_sys->status.dwServiceType = SERVICE_WIN32;
313 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
314 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
316 p_sys->hStatus =
317 RegisterServiceCtrlHandlerA( p_sys->psz_service, &ServiceCtrlHandler );
318 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
320 msg_Err( p_intf, "failed to register service control handler" );
321 return;
325 * Load background interfaces
327 psz_modules = var_InheritString( p_intf, "ntservice-extraintf" );
328 psz_parser = psz_modules;
329 while( psz_parser && *psz_parser )
331 char *psz_module, *psz_temp;
332 psz_module = psz_parser;
333 psz_parser = strchr( psz_module, ',' );
334 if( psz_parser )
336 *psz_parser = '\0';
337 psz_parser++;
340 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
342 /* Try to create the interface */
343 if( intf_Create( pl_Get(p_intf), psz_temp ) )
345 msg_Err( p_intf, "interface \"%s\" initialization failed",
346 psz_temp );
347 free( psz_temp );
348 continue;
350 free( psz_temp );
353 free( psz_modules );
355 /* Initialization complete - report running status */
356 p_sys->status.dwCurrentState = SERVICE_RUNNING;
357 p_sys->status.dwCheckPoint = 0;
358 p_sys->status.dwWaitHint = 0;
360 SetServiceStatus( p_sys->hStatus, &p_sys->status );
363 static void WINAPI ServiceCtrlHandler( DWORD control )
365 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
366 intf_sys_t *p_sys = p_intf->p_sys;
368 switch( control )
370 case SERVICE_CONTROL_SHUTDOWN:
371 case SERVICE_CONTROL_STOP:
372 p_sys->status.dwCurrentState = SERVICE_STOPPED;
373 p_sys->status.dwWin32ExitCode = 0;
374 p_sys->status.dwCheckPoint = 0;
375 p_sys->status.dwWaitHint = 0;
376 break;
377 case SERVICE_CONTROL_INTERROGATE:
378 /* just set the current state to whatever it is... */
379 break;
382 SetServiceStatus( p_sys->hStatus, &p_sys->status );