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