demux: heif: send extradata with avif
[vlc.git] / modules / control / ntservice.c
blob7b20bd579746db75899234016c5de1d2308c6f16
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>
36 #include <vlc_memstream.h>
38 #define VLCSERVICENAME "VLC media player"
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int Activate( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 #define INSTALL_TEXT N_( "Install Windows Service" )
47 #define INSTALL_LONGTEXT N_( \
48 "Install the Service and exit." )
49 #define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
50 #define UNINSTALL_LONGTEXT N_( \
51 "Uninstall the Service and exit." )
52 #define NAME_TEXT N_( "Display name of the Service" )
53 #define NAME_LONGTEXT N_( \
54 "Change the display name of the Service." )
55 #define OPTIONS_TEXT N_("Configuration options")
56 #define OPTIONS_LONGTEXT N_( \
57 "Configuration options that will be " \
58 "used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
59 "at install time so the Service is properly configured.")
60 #define EXTRAINTF_TEXT N_("Extra interface modules")
61 #define EXTRAINTF_LONGTEXT N_( \
62 "Additional interfaces spawned by the " \
63 "Service. It should be specified at install time so the Service is " \
64 "properly configured. Use a comma separated list of interface modules. " \
65 "(common values are: logger, sap, rc, http)")
67 vlc_module_begin ()
68 set_shortname( N_("NT Service"))
69 set_description( N_("Windows Service interface") )
70 set_category( CAT_INTERFACE )
71 set_subcategory( SUBCAT_INTERFACE_CONTROL )
72 add_bool( "ntservice-install", false,
73 INSTALL_TEXT, INSTALL_LONGTEXT, true )
74 add_bool( "ntservice-uninstall", false,
75 UNINSTALL_TEXT, UNINSTALL_LONGTEXT, true )
76 add_string ( "ntservice-name", VLCSERVICENAME,
77 NAME_TEXT, NAME_LONGTEXT, true )
78 add_string ( "ntservice-options", NULL,
79 OPTIONS_TEXT, OPTIONS_LONGTEXT, true )
80 add_string ( "ntservice-extraintf", NULL,
81 EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, true )
83 set_capability( "interface", 0 )
84 set_callbacks( Activate, Close )
85 vlc_module_end ()
87 struct intf_sys_t
89 SERVICE_STATUS_HANDLE hStatus;
90 SERVICE_STATUS status;
91 char *psz_service;
92 vlc_thread_t thread;
95 /*****************************************************************************
96 * Local prototypes
97 *****************************************************************************/
98 static void *Run( void * );
99 static int NTServiceInstall( intf_thread_t *p_intf );
100 static int NTServiceUninstall( intf_thread_t *p_intf );
101 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
102 static void WINAPI ServiceCtrlHandler( DWORD control );
104 /* We need this global */
105 static intf_thread_t *p_global_intf;
107 /*****************************************************************************
108 * Activate: initialize and create stuff
109 *****************************************************************************/
110 static int Activate( vlc_object_t *p_this )
112 intf_thread_t *p_intf = (intf_thread_t*)p_this;
113 intf_sys_t *p_sys = malloc( sizeof( *p_sys ) );
114 if( unlikely(p_sys == NULL) )
115 return VLC_ENOMEM;
117 p_intf->p_sys = p_sys;
119 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
120 return VLC_ENOMEM;
122 return VLC_SUCCESS;
125 /*****************************************************************************
126 * Close: destroy interface
127 *****************************************************************************/
128 void Close( vlc_object_t *p_this )
130 intf_thread_t *p_intf = (intf_thread_t*)p_this;
131 intf_sys_t *p_sys = p_intf->p_sys;
133 vlc_join( p_sys->thread, NULL );
134 free( p_sys );
137 /*****************************************************************************
138 * Run: interface thread
139 *****************************************************************************/
140 static void *Run( void *data )
142 intf_thread_t *p_intf = data;
143 SERVICE_TABLE_ENTRY dispatchTable[] =
145 { TEXT(VLCSERVICENAME), &ServiceDispatch },
146 { NULL, NULL }
149 p_global_intf = p_intf;
150 p_intf->p_sys->psz_service = var_InheritString( p_intf, "ntservice-name" );
151 p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
152 p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
154 if( var_InheritBool( p_intf, "ntservice-install" ) )
156 NTServiceInstall( p_intf );
157 return NULL;
160 if( var_InheritBool( p_intf, "ntservice-uninstall" ) )
162 NTServiceUninstall( p_intf );
163 return NULL;
166 if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
168 msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
171 free( p_intf->p_sys->psz_service );
173 /* Make sure we exit (In case other interfaces have been spawned) */
174 libvlc_Quit( p_intf->obj.libvlc );
175 return NULL;
178 /*****************************************************************************
179 * NT Service utility functions
180 *****************************************************************************/
181 static int NTServiceInstall( intf_thread_t *p_intf )
183 intf_sys_t *p_sys = p_intf->p_sys;
184 char *psz_extra;
185 struct vlc_memstream path_stream;
186 TCHAR psz_pathtmp[MAX_PATH];
188 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
189 if( handle == NULL )
191 msg_Err( p_intf,
192 "could not connect to Services Control Manager database" );
193 return VLC_EGENERIC;
196 if( vlc_memstream_open(&path_stream) != 0 )
198 CloseServiceHandle( handle );
199 return VLC_ENOMEM;
202 /* Find out the filename of ourselves so we can install it to the
203 * service control manager */
204 GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
205 psz_extra = FromT( psz_pathtmp );
206 if ( !psz_extra )
208 CloseServiceHandle( handle );
209 return VLC_ENOMEM;
211 vlc_memstream_printf( &path_stream, "\"%s\" -I ntservice", psz_extra );
212 free(psz_extra);
214 psz_extra = var_InheritString( p_intf, "ntservice-extraintf" );
215 if( psz_extra && *psz_extra )
216 vlc_memstream_printf( &path_stream, " --ntservice-extraintf %s", psz_extra );
217 free( psz_extra );
219 psz_extra = var_InheritString( p_intf, "ntservice-options" );
220 if( psz_extra && *psz_extra )
221 vlc_memstream_printf( &path_stream, " %s", psz_extra );
222 free( psz_extra );
224 if ( vlc_memstream_close( &path_stream ) != 0 )
226 CloseServiceHandle( handle );
227 return VLC_ENOMEM;
230 SC_HANDLE service =
231 CreateServiceA( handle, p_sys->psz_service, p_sys->psz_service,
232 GENERIC_READ | GENERIC_EXECUTE,
233 SERVICE_WIN32_OWN_PROCESS,
234 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
235 path_stream.ptr, NULL, NULL, NULL, NULL, NULL );
236 if( service == NULL )
238 if( GetLastError() != ERROR_SERVICE_EXISTS )
240 msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
241 p_sys->psz_service, path_stream.ptr );
242 free( path_stream.ptr );
243 CloseServiceHandle( handle );
244 return VLC_EGENERIC;
246 else
248 msg_Warn( p_intf, "service \"%s\" already exists",
249 p_sys->psz_service );
252 else
254 msg_Warn( p_intf, "service successfuly created" );
257 free( path_stream.ptr );
259 if( service ) CloseServiceHandle( service );
260 CloseServiceHandle( handle );
262 return VLC_SUCCESS;
265 static int NTServiceUninstall( intf_thread_t *p_intf )
267 intf_sys_t *p_sys = p_intf->p_sys;
269 SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
270 if( handle == NULL )
272 msg_Err( p_intf,
273 "could not connect to Services Control Manager database" );
274 return VLC_EGENERIC;
277 /* First, open a handle to the service */
278 SC_HANDLE service = OpenServiceA( handle, p_sys->psz_service, DELETE );
279 if( service == NULL )
281 msg_Err( p_intf, "could not open service" );
282 CloseServiceHandle( handle );
283 return VLC_EGENERIC;
286 /* Remove the service */
287 if( !DeleteService( service ) )
289 msg_Err( p_intf, "could not delete service \"%s\"",
290 p_sys->psz_service );
292 else
294 msg_Dbg( p_intf, "service deleted successfuly" );
297 CloseServiceHandle( service );
298 CloseServiceHandle( handle );
300 return VLC_SUCCESS;
303 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
305 (void)numArgs;
306 (void)args;
307 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
308 intf_sys_t *p_sys = p_intf->p_sys;
309 char *psz_modules, *psz_parser;
311 /* We have to initialize the service-specific stuff */
312 memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
313 p_sys->status.dwServiceType = SERVICE_WIN32;
314 p_sys->status.dwCurrentState = SERVICE_START_PENDING;
315 p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
317 p_sys->hStatus =
318 RegisterServiceCtrlHandlerA( p_sys->psz_service, &ServiceCtrlHandler );
319 if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
321 msg_Err( p_intf, "failed to register service control handler" );
322 return;
326 * Load background interfaces
328 psz_modules = var_InheritString( p_intf, "ntservice-extraintf" );
329 psz_parser = psz_modules;
330 while( psz_parser && *psz_parser )
332 char *psz_module, *psz_temp;
333 psz_module = psz_parser;
334 psz_parser = strchr( psz_module, ',' );
335 if( psz_parser )
337 *psz_parser = '\0';
338 psz_parser++;
341 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
343 /* Try to create the interface */
344 if( intf_Create( pl_Get(p_intf), psz_temp ) )
346 msg_Err( p_intf, "interface \"%s\" initialization failed",
347 psz_temp );
348 free( psz_temp );
349 continue;
351 free( psz_temp );
354 free( psz_modules );
356 /* Initialization complete - report running status */
357 p_sys->status.dwCurrentState = SERVICE_RUNNING;
358 p_sys->status.dwCheckPoint = 0;
359 p_sys->status.dwWaitHint = 0;
361 SetServiceStatus( p_sys->hStatus, &p_sys->status );
364 static void WINAPI ServiceCtrlHandler( DWORD control )
366 intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
367 intf_sys_t *p_sys = p_intf->p_sys;
369 switch( control )
371 case SERVICE_CONTROL_SHUTDOWN:
372 case SERVICE_CONTROL_STOP:
373 p_sys->status.dwCurrentState = SERVICE_STOPPED;
374 p_sys->status.dwWin32ExitCode = 0;
375 p_sys->status.dwCheckPoint = 0;
376 p_sys->status.dwWaitHint = 0;
377 break;
378 case SERVICE_CONTROL_INTERROGATE:
379 /* just set the current state to whatever it is... */
380 break;
383 SetServiceStatus( p_sys->hStatus, &p_sys->status );