]> git.sesse.net Git - vlc/blob - modules/control/ntservice.c
mkv: Translation fix.
[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 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
34
35 #define VLCSERVICENAME "VLC media player"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Activate( vlc_object_t * );
41 static void Close   ( vlc_object_t * );
42
43 #define INSTALL_TEXT N_( "Install Windows Service" )
44 #define INSTALL_LONGTEXT N_( \
45     "Install the Service and exit." )
46 #define UNINSTALL_TEXT N_( "Uninstall Windows Service" )
47 #define UNINSTALL_LONGTEXT N_( \
48     "Uninstall the Service and exit." )
49 #define NAME_TEXT N_( "Display name of the Service" )
50 #define NAME_LONGTEXT N_( \
51     "Change the display name of the Service." )
52 #define OPTIONS_TEXT N_("Configuration options")
53 #define OPTIONS_LONGTEXT N_( \
54     "Configuration options that will be " \
55     "used by the Service (eg. --foo=bar --no-foobar). It should be specified "\
56     "at install time so the Service is properly configured.")
57 #define EXTRAINTF_TEXT N_("Extra interface modules")
58 #define EXTRAINTF_LONGTEXT N_( \
59     "Additional interfaces spawned by the " \
60     "Service. It should be specified at install time so the Service is " \
61     "properly configured. Use a comma separated list of interface modules. " \
62     "(common values are: logger, sap, rc, http)")
63
64 vlc_module_begin();
65     set_shortname( N_("NT Service"));
66     set_description( N_("Windows Service interface") );
67     set_category( CAT_INTERFACE );
68     set_subcategory( SUBCAT_INTERFACE_CONTROL );
69     add_bool( "ntservice-install", 0, NULL,
70               INSTALL_TEXT, INSTALL_LONGTEXT, true );
71     add_bool( "ntservice-uninstall", 0, NULL,
72               UNINSTALL_TEXT, UNINSTALL_LONGTEXT, true );
73     add_string ( "ntservice-name", VLCSERVICENAME, NULL,
74                  NAME_TEXT, NAME_LONGTEXT, true );
75     add_string ( "ntservice-options", NULL, NULL,
76                  OPTIONS_TEXT, OPTIONS_LONGTEXT, true );
77     add_string ( "ntservice-extraintf", NULL, NULL,
78                  EXTRAINTF_TEXT, EXTRAINTF_LONGTEXT, true );
79
80     set_capability( "interface", 0 );
81     set_callbacks( Activate, Close );
82 vlc_module_end();
83
84 struct intf_sys_t
85 {
86     SERVICE_STATUS_HANDLE hStatus;
87     SERVICE_STATUS status;
88     char *psz_service;
89 };
90
91 /*****************************************************************************
92  * Local prototypes
93  *****************************************************************************/
94 static void Run( intf_thread_t *p_intf );
95 static int NTServiceInstall( intf_thread_t *p_intf );
96 static int NTServiceUninstall( intf_thread_t *p_intf );
97 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
98 static void WINAPI ServiceCtrlHandler( DWORD control );
99
100 /* We need this global */
101 static intf_thread_t *p_global_intf;
102
103 /*****************************************************************************
104  * Activate: initialize and create stuff
105  *****************************************************************************/
106 static int Activate( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t*)p_this;
109
110     /* Only works on NT/2K/XP */
111     if( !IS_WINNT ) return VLC_EGENERIC;
112
113     p_intf->pf_run = Run;
114     return VLC_SUCCESS;
115 }
116
117 /*****************************************************************************
118  * Close: destroy interface
119  *****************************************************************************/
120 void Close( vlc_object_t *p_this )
121 {
122 }
123
124 /*****************************************************************************
125  * Run: interface thread
126  *****************************************************************************/
127 static void Run( intf_thread_t *p_intf )
128 {
129     intf_thread_t *p_extraintf;
130     SERVICE_TABLE_ENTRY dispatchTable[] =
131     {
132         { VLCSERVICENAME, &ServiceDispatch },
133         { NULL, NULL }
134     };
135
136     p_global_intf = p_intf;
137     p_intf->p_sys = alloca( sizeof( intf_sys_t ) );
138     p_intf->p_sys->psz_service = config_GetPsz( p_intf, "ntservice-name" );
139     p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
140         p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
141
142     if( config_GetInt( p_intf, "ntservice-install" ) )
143     {
144         NTServiceInstall( p_intf );
145         return;
146     }
147
148     if( config_GetInt( p_intf, "ntservice-uninstall" ) )
149     {
150         NTServiceUninstall( p_intf );
151         return;
152     }
153
154     if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
155     {
156         msg_Err( p_intf, "StartServiceCtrlDispatcher failed" ); /* str review */
157     }
158
159     free( p_intf->p_sys->psz_service );
160
161     /* Stop and destroy the interfaces we spawned */
162     while( (p_extraintf = vlc_object_find(p_intf, VLC_OBJECT_INTF, FIND_CHILD)))
163     {
164         intf_StopThread( p_extraintf );
165         vlc_object_detach( p_extraintf );
166         vlc_object_release( p_extraintf );
167         vlc_object_release( p_extraintf );
168     }
169
170     /* Make sure we exit (In case other interfaces have been spawned) */
171     vlc_object_kill( p_intf->p_libvlc );
172 }
173
174 /*****************************************************************************
175  * NT Service utility functions
176  *****************************************************************************/
177 static int NTServiceInstall( intf_thread_t *p_intf )
178 {
179     intf_sys_t *p_sys  = p_intf->p_sys;
180     char psz_path[10*MAX_PATH], psz_pathtmp[MAX_PATH], *psz_extra;
181     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
182     if( handle == NULL )
183     {
184         msg_Err( p_intf,
185                  "could not connect to Services Control Manager database" );
186         return VLC_EGENERIC;
187     }
188
189     /* Find out the filename of ourselves so we can install it to the
190      * service control manager */
191     GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
192     sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
193
194     psz_extra = config_GetPsz( p_intf, "ntservice-extraintf" );
195     if( psz_extra && *psz_extra )
196     {
197         strcat( psz_path, " --ntservice-extraintf " );
198         strcat( psz_path, psz_extra );
199     }
200     free( psz_extra );
201
202     psz_extra = config_GetPsz( p_intf, "ntservice-options" );
203     if( psz_extra && *psz_extra )
204     {
205         strcat( psz_path, " " );
206         strcat( psz_path, psz_extra );
207     }
208     free( psz_extra );
209
210     SC_HANDLE service =
211         CreateService( handle, p_sys->psz_service, p_sys->psz_service,
212                        GENERIC_READ | GENERIC_EXECUTE,
213                        SERVICE_WIN32_OWN_PROCESS,
214                        SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
215                        psz_path, NULL, NULL, NULL, NULL, NULL );
216     if( service == NULL )
217     {
218         if( GetLastError() != ERROR_SERVICE_EXISTS )
219         {
220             msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
221                      p_sys->psz_service ,psz_path );
222             CloseServiceHandle( handle );
223             return VLC_EGENERIC;
224         }
225         else
226         {
227             msg_Warn( p_intf, "service \"%s\" already exists",
228                       p_sys->psz_service );
229         }
230     }
231     else
232     {
233         msg_Warn( p_intf, "service successfuly created" );
234     }
235
236     if( service ) CloseServiceHandle( service );
237     CloseServiceHandle( handle );
238
239     return VLC_SUCCESS;
240 }
241
242 static int NTServiceUninstall( intf_thread_t *p_intf )
243 {
244     intf_sys_t *p_sys  = p_intf->p_sys;
245
246     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
247     if( handle == NULL )
248     {
249         msg_Err( p_intf,
250                  "could not connect to Services Control Manager database" );
251         return VLC_EGENERIC;
252     }
253
254     /* First, open a handle to the service */
255     SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
256     if( service == NULL )
257     {
258         msg_Err( p_intf, "could not open service" );
259         CloseServiceHandle( handle );
260         return VLC_EGENERIC;
261     }
262
263     /* Remove the service */
264     if( !DeleteService( service ) )
265     {
266         msg_Err( p_intf, "could not delete service \"%s\"",
267                  p_sys->psz_service );
268     }
269     else
270     {
271         msg_Dbg( p_intf, "service deleted successfuly" );
272     }
273
274     CloseServiceHandle( service );
275     CloseServiceHandle( handle );
276
277     return VLC_SUCCESS;
278 }
279
280 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
281 {
282     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
283     intf_sys_t    *p_sys  = p_intf->p_sys;
284     char *psz_modules, *psz_parser;
285
286     /* We have to initialize the service-specific stuff */
287     memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
288     p_sys->status.dwServiceType = SERVICE_WIN32;
289     p_sys->status.dwCurrentState = SERVICE_START_PENDING;
290     p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
291
292     p_sys->hStatus =
293         RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
294     if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
295     {
296         msg_Err( p_intf, "failed to register service control handler" );
297         return;
298     }
299
300     /*
301      * Load background interfaces
302      */
303     psz_modules = config_GetPsz( p_intf, "ntservice-extraintf" );
304     psz_parser = psz_modules;
305     while( psz_parser && *psz_parser )
306     {
307         char *psz_module, *psz_temp;
308         psz_module = psz_parser;
309         psz_parser = strchr( psz_module, ',' );
310         if( psz_parser )
311         {
312             *psz_parser = '\0';
313             psz_parser++;
314         }
315         psz_temp = (char *)malloc( strlen(psz_module) + sizeof(",none") );
316         if( psz_temp )
317         {
318             intf_thread_t *p_new_intf;
319             sprintf( psz_temp, "%s,none", psz_module );
320
321             /* Try to create the interface */
322             p_new_intf = intf_Create( p_intf, psz_temp );
323             if( p_new_intf == NULL )
324             {
325                 msg_Err( p_intf, "interface \"%s\" initialization failed",
326                          psz_temp );
327                 free( psz_temp );
328                 continue;
329             }
330
331             /* Try to run the interface */
332             if( intf_RunThread( p_new_intf ) )
333             {
334                 vlc_object_detach( p_new_intf );
335                 vlc_object_release( p_new_intf );
336                 msg_Err( p_intf, "interface \"%s\" cannot run", psz_temp );
337             }
338
339             free( psz_temp );
340         }
341     }
342     free( psz_modules );
343
344     /* Initialization complete - report running status */
345     p_sys->status.dwCurrentState = SERVICE_RUNNING;
346     p_sys->status.dwCheckPoint   = 0;
347     p_sys->status.dwWaitHint     = 0;
348
349     SetServiceStatus( p_sys->hStatus, &p_sys->status );
350 }
351
352 static void WINAPI ServiceCtrlHandler( DWORD control )
353 {
354     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
355     intf_sys_t    *p_sys  = p_intf->p_sys;
356
357     switch( control )
358     {
359     case SERVICE_CONTROL_SHUTDOWN:
360     case SERVICE_CONTROL_STOP:
361         p_sys->status.dwCurrentState = SERVICE_STOPPED;
362         p_sys->status.dwWin32ExitCode = 0;
363         p_sys->status.dwCheckPoint = 0;
364         p_sys->status.dwWaitHint = 0;
365         break;
366     case SERVICE_CONTROL_INTERROGATE:
367         /* just set the current state to whatever it is... */
368         break;
369     }
370
371     SetServiceStatus( p_sys->hStatus, &p_sys->status );
372 }