]> git.sesse.net Git - vlc/blob - modules/control/ntservice.c
decoder: drain the audio output properly
[vlc] / modules / control / ntservice.c
1 /*****************************************************************************
2  * ntservice.c: Windows NT/2K/XP service interface
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
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
37 #define VLCSERVICENAME "VLC media player"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Activate( vlc_object_t * );
43 static void Close   ( vlc_object_t * );
44
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)")
65
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 )
81
82     set_capability( "interface", 0 )
83     set_callbacks( Activate, Close )
84 vlc_module_end ()
85
86 struct intf_sys_t
87 {
88     SERVICE_STATUS_HANDLE hStatus;
89     SERVICE_STATUS status;
90     char *psz_service;
91     vlc_thread_t thread;
92 };
93
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 );
102
103 /* We need this global */
104 static intf_thread_t *p_global_intf;
105
106 /*****************************************************************************
107  * Activate: initialize and create stuff
108  *****************************************************************************/
109 static int Activate( vlc_object_t *p_this )
110 {
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;
115
116     p_intf->p_sys = p_sys;
117
118     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
119         return VLC_ENOMEM;
120
121     return VLC_SUCCESS;
122 }
123
124 /*****************************************************************************
125  * Close: destroy interface
126  *****************************************************************************/
127 void Close( vlc_object_t *p_this )
128 {
129     intf_thread_t *p_intf = (intf_thread_t*)p_this;
130     intf_sys_t *p_sys = p_intf->p_sys;
131
132     vlc_join( p_sys->thread, NULL );
133     free( p_sys );
134 }
135
136 /*****************************************************************************
137  * Run: interface thread
138  *****************************************************************************/
139 static void *Run( void *data )
140 {
141     intf_thread_t *p_intf = data;
142     SERVICE_TABLE_ENTRY dispatchTable[] =
143     {
144         { TEXT(VLCSERVICENAME), &ServiceDispatch },
145         { NULL, NULL }
146     };
147
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);
152
153     if( var_InheritBool( p_intf, "ntservice-install" ) )
154     {
155         NTServiceInstall( p_intf );
156         return NULL;
157     }
158
159     if( var_InheritBool( p_intf, "ntservice-uninstall" ) )
160     {
161         NTServiceUninstall( p_intf );
162         return NULL;
163     }
164
165     if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
166     {
167         msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
168     }
169
170     free( p_intf->p_sys->psz_service );
171
172     /* Make sure we exit (In case other interfaces have been spawned) */
173     libvlc_Quit( p_intf->p_libvlc );
174     return NULL;
175 }
176
177 /*****************************************************************************
178  * NT Service utility functions
179  *****************************************************************************/
180 static int NTServiceInstall( intf_thread_t *p_intf )
181 {
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];
185
186     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
187     if( handle == NULL )
188     {
189         msg_Err( p_intf,
190                  "could not connect to Services Control Manager database" );
191         return VLC_EGENERIC;
192     }
193
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 "MODULE_STRING, FromT(psz_pathtmp) );
198
199     psz_extra = var_InheritString( p_intf, "ntservice-extraintf" );
200     if( psz_extra )
201     {
202         strcat( psz_path, " --ntservice-extraintf " );
203         strcat( psz_path, psz_extra );
204         free( psz_extra );
205     }
206
207     psz_extra = var_InheritString( p_intf, "ntservice-options" );
208     if( psz_extra && *psz_extra )
209     {
210         strcat( psz_path, " " );
211         strcat( psz_path, psz_extra );
212         free( psz_extra );
213     }
214
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 )
222     {
223         if( GetLastError() != ERROR_SERVICE_EXISTS )
224         {
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;
229         }
230         else
231         {
232             msg_Warn( p_intf, "service \"%s\" already exists",
233                       p_sys->psz_service );
234         }
235     }
236     else
237     {
238         msg_Warn( p_intf, "service successfuly created" );
239     }
240
241     if( service ) CloseServiceHandle( service );
242     CloseServiceHandle( handle );
243
244     return VLC_SUCCESS;
245 }
246
247 static int NTServiceUninstall( intf_thread_t *p_intf )
248 {
249     intf_sys_t *p_sys  = p_intf->p_sys;
250
251     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
252     if( handle == NULL )
253     {
254         msg_Err( p_intf,
255                  "could not connect to Services Control Manager database" );
256         return VLC_EGENERIC;
257     }
258
259     /* First, open a handle to the service */
260     SC_HANDLE service = OpenServiceA( handle, p_sys->psz_service, DELETE );
261     if( service == NULL )
262     {
263         msg_Err( p_intf, "could not open service" );
264         CloseServiceHandle( handle );
265         return VLC_EGENERIC;
266     }
267
268     /* Remove the service */
269     if( !DeleteService( service ) )
270     {
271         msg_Err( p_intf, "could not delete service \"%s\"",
272                  p_sys->psz_service );
273     }
274     else
275     {
276         msg_Dbg( p_intf, "service deleted successfuly" );
277     }
278
279     CloseServiceHandle( service );
280     CloseServiceHandle( handle );
281
282     return VLC_SUCCESS;
283 }
284
285 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
286 {
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;
292
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;
298
299     p_sys->hStatus =
300         RegisterServiceCtrlHandlerA( p_sys->psz_service, &ServiceCtrlHandler );
301     if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
302     {
303         msg_Err( p_intf, "failed to register service control handler" );
304         return;
305     }
306
307     /*
308      * Load background interfaces
309      */
310     psz_modules = var_InheritString( p_intf, "ntservice-extraintf" );
311     psz_parser = psz_modules;
312     while( psz_parser && *psz_parser )
313     {
314         char *psz_module, *psz_temp;
315         psz_module = psz_parser;
316         psz_parser = strchr( psz_module, ',' );
317         if( psz_parser )
318         {
319             *psz_parser = '\0';
320             psz_parser++;
321         }
322
323         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
324         {
325             /* Try to create the interface */
326             if( intf_Create( pl_Get(p_intf), psz_temp ) )
327             {
328                 msg_Err( p_intf, "interface \"%s\" initialization failed",
329                          psz_temp );
330                 free( psz_temp );
331                 continue;
332             }
333             free( psz_temp );
334         }
335     }
336     free( psz_modules );
337
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;
342
343     SetServiceStatus( p_sys->hStatus, &p_sys->status );
344 }
345
346 static void WINAPI ServiceCtrlHandler( DWORD control )
347 {
348     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
349     intf_sys_t    *p_sys  = p_intf->p_sys;
350
351     switch( control )
352     {
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;
363     }
364
365     SetServiceStatus( p_sys->hStatus, &p_sys->status );
366 }