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