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