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