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