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