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