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