]> git.sesse.net Git - vlc/blob - modules/control/ntservice.c
Use var_Inherit* instead of var_CreateGet*.
[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", false, NULL,
70               INSTALL_TEXT, INSTALL_LONGTEXT, true )
71     add_bool( "ntservice-uninstall", false, 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     p_intf->pf_run = Run;
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * Close: destroy interface
116  *****************************************************************************/
117 void Close( vlc_object_t *p_this )
118 {
119     (void)p_this;
120 }
121
122 /*****************************************************************************
123  * Run: interface thread
124  *****************************************************************************/
125 static void Run( intf_thread_t *p_intf )
126 {
127     intf_sys_t sys;
128     SERVICE_TABLE_ENTRY dispatchTable[] =
129     {
130         { (LPTSTR)VLCSERVICENAME, &ServiceDispatch },
131         { NULL, NULL }
132     };
133
134     int canc = vlc_savecancel();
135     p_global_intf = p_intf;
136     p_intf->p_sys = &sys;
137     p_intf->p_sys->psz_service = var_InheritString( 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( var_InheritBool( p_intf, "ntservice-install" ) )
142     {
143         NTServiceInstall( p_intf );
144         return;
145     }
146
147     if( var_InheritBool( 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     /* Make sure we exit (In case other interfaces have been spawned) */
161     libvlc_Quit( p_intf->p_libvlc );
162     vlc_restorecancel( canc );
163 }
164
165 /*****************************************************************************
166  * NT Service utility functions
167  *****************************************************************************/
168 static int NTServiceInstall( intf_thread_t *p_intf )
169 {
170     intf_sys_t *p_sys  = p_intf->p_sys;
171     char psz_path[10*MAX_PATH], psz_pathtmp[MAX_PATH], *psz_extra;
172     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
173     if( handle == NULL )
174     {
175         msg_Err( p_intf,
176                  "could not connect to Services Control Manager database" );
177         return VLC_EGENERIC;
178     }
179
180     /* Find out the filename of ourselves so we can install it to the
181      * service control manager */
182     GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
183     sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
184
185     psz_extra = var_InheritString( p_intf, "ntservice-extraintf" );
186     if( psz_extra )
187     {
188         strcat( psz_path, " --ntservice-extraintf " );
189         strcat( psz_path, psz_extra );
190         free( psz_extra );
191     }
192
193     psz_extra = var_InheritString( p_intf, "ntservice-options" );
194     if( psz_extra && *psz_extra )
195     {
196         strcat( psz_path, " " );
197         strcat( psz_path, psz_extra );
198         free( psz_extra );
199     }
200
201     SC_HANDLE service =
202         CreateService( handle, p_sys->psz_service, p_sys->psz_service,
203                        GENERIC_READ | GENERIC_EXECUTE,
204                        SERVICE_WIN32_OWN_PROCESS,
205                        SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
206                        psz_path, NULL, NULL, NULL, NULL, NULL );
207     if( service == NULL )
208     {
209         if( GetLastError() != ERROR_SERVICE_EXISTS )
210         {
211             msg_Err( p_intf, "could not create new service: \"%s\" (%s)",
212                      p_sys->psz_service ,psz_path );
213             CloseServiceHandle( handle );
214             return VLC_EGENERIC;
215         }
216         else
217         {
218             msg_Warn( p_intf, "service \"%s\" already exists",
219                       p_sys->psz_service );
220         }
221     }
222     else
223     {
224         msg_Warn( p_intf, "service successfuly created" );
225     }
226
227     if( service ) CloseServiceHandle( service );
228     CloseServiceHandle( handle );
229
230     return VLC_SUCCESS;
231 }
232
233 static int NTServiceUninstall( intf_thread_t *p_intf )
234 {
235     intf_sys_t *p_sys  = p_intf->p_sys;
236
237     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
238     if( handle == NULL )
239     {
240         msg_Err( p_intf,
241                  "could not connect to Services Control Manager database" );
242         return VLC_EGENERIC;
243     }
244
245     /* First, open a handle to the service */
246     SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
247     if( service == NULL )
248     {
249         msg_Err( p_intf, "could not open service" );
250         CloseServiceHandle( handle );
251         return VLC_EGENERIC;
252     }
253
254     /* Remove the service */
255     if( !DeleteService( service ) )
256     {
257         msg_Err( p_intf, "could not delete service \"%s\"",
258                  p_sys->psz_service );
259     }
260     else
261     {
262         msg_Dbg( p_intf, "service deleted successfuly" );
263     }
264
265     CloseServiceHandle( service );
266     CloseServiceHandle( handle );
267
268     return VLC_SUCCESS;
269 }
270
271 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
272 {
273     (void)numArgs;
274     (void)args;
275     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
276     intf_sys_t    *p_sys  = p_intf->p_sys;
277     char *psz_modules, *psz_parser;
278
279     /* We have to initialize the service-specific stuff */
280     memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
281     p_sys->status.dwServiceType = SERVICE_WIN32;
282     p_sys->status.dwCurrentState = SERVICE_START_PENDING;
283     p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
284
285     p_sys->hStatus =
286         RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
287     if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
288     {
289         msg_Err( p_intf, "failed to register service control handler" );
290         return;
291     }
292
293     /*
294      * Load background interfaces
295      */
296     psz_modules = var_InheritString( p_intf, "ntservice-extraintf" );
297     psz_parser = psz_modules;
298     while( psz_parser && *psz_parser )
299     {
300         char *psz_module, *psz_temp;
301         psz_module = psz_parser;
302         psz_parser = strchr( psz_module, ',' );
303         if( psz_parser )
304         {
305             *psz_parser = '\0';
306             psz_parser++;
307         }
308
309         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1 )
310         {
311             /* Try to create the interface */
312             if( intf_Create( p_intf, psz_temp ) )
313             {
314                 msg_Err( p_intf, "interface \"%s\" initialization failed",
315                          psz_temp );
316                 free( psz_temp );
317                 continue;
318             }
319             free( psz_temp );
320         }
321     }
322     free( psz_modules );
323
324     /* Initialization complete - report running status */
325     p_sys->status.dwCurrentState = SERVICE_RUNNING;
326     p_sys->status.dwCheckPoint   = 0;
327     p_sys->status.dwWaitHint     = 0;
328
329     SetServiceStatus( p_sys->hStatus, &p_sys->status );
330 }
331
332 static void WINAPI ServiceCtrlHandler( DWORD control )
333 {
334     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
335     intf_sys_t    *p_sys  = p_intf->p_sys;
336
337     switch( control )
338     {
339     case SERVICE_CONTROL_SHUTDOWN:
340     case SERVICE_CONTROL_STOP:
341         p_sys->status.dwCurrentState = SERVICE_STOPPED;
342         p_sys->status.dwWin32ExitCode = 0;
343         p_sys->status.dwCheckPoint = 0;
344         p_sys->status.dwWaitHint = 0;
345         break;
346     case SERVICE_CONTROL_INTERROGATE:
347         /* just set the current state to whatever it is... */
348         break;
349     }
350
351     SetServiceStatus( p_sys->hStatus, &p_sys->status );
352 }