]> git.sesse.net Git - vlc/blob - modules/control/ntservice.c
* ALL: bumped version number to 0.6.3-cvs
[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.1 2003/08/13 23:26:55 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
49 vlc_module_begin();
50     set_description( _("Windows NT/2K/XP service interface") );
51     add_category_hint( N_("NT service"), NULL, VLC_TRUE );
52     add_bool( "ntservice-install", 0, NULL, INSTALL_TEXT, INSTALL_LONGTEXT, VLC_TRUE );
53     add_bool( "ntservice-uninstall", 0, NULL, INSTALL_TEXT, INSTALL_LONGTEXT, VLC_TRUE );
54     add_string ( "ntservice-name", VLCSERVICENAME, NULL, NAME_TEXT, NAME_LONGTEXT, VLC_TRUE );
55
56     set_capability( "interface", 0 );
57     set_callbacks( Activate, Close );
58 vlc_module_end();
59
60 struct intf_sys_t
61 {
62     SERVICE_STATUS_HANDLE hStatus;
63     SERVICE_STATUS status;
64     char *psz_service;
65 };
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static void Run( intf_thread_t *p_intf );
71 static int NTServiceInstall( intf_thread_t *p_intf );
72 static int NTServiceUninstall( intf_thread_t *p_intf );
73 static void WINAPI ServiceDispatch( DWORD numArgs, char **args );
74 static void WINAPI ServiceCtrlHandler( DWORD control );
75
76 /* We need this global */
77 static intf_thread_t *p_global_intf;
78
79 /*****************************************************************************
80  * Activate: initialize and create stuff
81  *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
83 {
84     intf_thread_t *p_intf = (intf_thread_t*)p_this;
85
86     /* Only works on NT/2K/XP */
87     if( !IS_WINNT ) return VLC_EGENERIC;
88
89     p_intf->pf_run = Run;
90     return VLC_SUCCESS;
91 }
92
93 /*****************************************************************************
94  * Close: destroy interface
95  *****************************************************************************/
96 void Close( vlc_object_t *p_this )
97 {
98 }
99
100 /*****************************************************************************
101  * Run: interface thread
102  *****************************************************************************/
103 static void Run( intf_thread_t *p_intf )
104 {
105     SERVICE_TABLE_ENTRY dispatchTable[] =
106     {
107         { VLCSERVICENAME, &ServiceDispatch },
108         { NULL, NULL }
109     };
110
111     p_global_intf = p_intf;
112     p_intf->p_sys = alloca( sizeof( intf_sys_t ) );
113     p_intf->p_sys->psz_service = config_GetPsz( p_intf, "ntservice-name" );
114     p_intf->p_sys->psz_service = p_intf->p_sys->psz_service ?
115         p_intf->p_sys->psz_service : strdup(VLCSERVICENAME);
116
117     if( config_GetInt( p_intf, "ntservice-install" ) )
118     {
119         NTServiceInstall( p_intf );
120         return;
121     }
122
123     if( config_GetInt( p_intf, "ntservice-uninstall" ) )
124     {
125         NTServiceUninstall( p_intf );
126         return;
127     }
128
129     if( StartServiceCtrlDispatcher( dispatchTable ) == 0 )
130     {
131         msg_Err( p_intf, "StartServiceCtrlDispatcher failed" );
132     }
133
134     free( p_intf->p_sys->psz_service );
135
136     /* Make sure we exit (In case other interfaces have been spawned) */
137     p_intf->p_vlc->b_die = VLC_TRUE;
138 }
139
140 /*****************************************************************************
141  * NT Service utility functions
142  *****************************************************************************/
143 static int NTServiceInstall( intf_thread_t *p_intf )
144 {
145     intf_sys_t *p_sys  = p_intf->p_sys;
146     char psz_path[MAX_PATH], psz_pathtmp[MAX_PATH];
147     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
148     if( handle == NULL )
149     {
150         msg_Err( p_intf, "Could not connect to SCM database" );
151         return VLC_EGENERIC;
152     }
153
154     /* Find out the filename of ourselves so we can install it to the
155      * service control manager */
156     GetModuleFileName( NULL, psz_pathtmp, MAX_PATH );
157     sprintf( psz_path, "\"%s\" -I "MODULE_STRING, psz_pathtmp );
158
159     SC_HANDLE service =
160         CreateService( handle, p_sys->psz_service, p_sys->psz_service,
161                        GENERIC_READ | GENERIC_EXECUTE,
162                        SERVICE_WIN32_OWN_PROCESS,
163                        SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
164                        psz_path, NULL, NULL, NULL, NULL, NULL );
165     if( service == NULL )
166     {
167         if( GetLastError() != ERROR_SERVICE_EXISTS )
168         {
169             msg_Err( p_intf, "Could not create new service" );
170             CloseServiceHandle( handle );
171             return VLC_EGENERIC;
172         }
173         else
174         {
175             msg_Warn( p_intf, "Service already exists" );
176         }
177     }
178     else
179     {
180         msg_Warn( p_intf, "Service successfuly created" );
181     }
182
183     if( service ) CloseServiceHandle( service );
184     CloseServiceHandle( handle );
185
186     return VLC_SUCCESS;
187 }
188
189 static int NTServiceUninstall( intf_thread_t *p_intf )
190 {
191     intf_sys_t *p_sys  = p_intf->p_sys;
192
193     SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
194     if( handle == NULL )
195     {
196         msg_Err( p_intf, "Could not connect to SCM database" );
197         return VLC_EGENERIC;
198     }
199
200     /* First, open a handle to the service */
201     SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE );
202     if( service == NULL )
203     {
204         msg_Err( p_intf, "Could not open service" );
205         CloseServiceHandle( handle );
206         return VLC_EGENERIC;
207     }
208
209     /* Remove the service */
210     if( !DeleteService( service ) )
211     {
212         msg_Err( p_intf, "Could not delete service" );
213     }
214     else
215     {
216         msg_Dbg( p_intf, "Service deleted successfuly" );
217     }
218
219     CloseServiceHandle( service );
220     CloseServiceHandle( handle );
221
222     return VLC_SUCCESS;
223 }
224
225 static void WINAPI ServiceDispatch( DWORD numArgs, char **args )
226 {
227     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
228     intf_sys_t    *p_sys  = p_intf->p_sys;
229
230     /* We have to initialize the service-specific stuff */
231     memset( &p_sys->status, 0, sizeof(SERVICE_STATUS) );
232     p_sys->status.dwServiceType = SERVICE_WIN32;
233     p_sys->status.dwCurrentState = SERVICE_START_PENDING;
234     p_sys->status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
235
236     p_sys->hStatus =
237         RegisterServiceCtrlHandler( p_sys->psz_service, &ServiceCtrlHandler );
238     if( p_sys->hStatus == (SERVICE_STATUS_HANDLE)0 )
239     {
240         msg_Err( p_intf, "Failed to register service control handler" );
241         return;
242     }
243
244     /* Initialization complete - report running status */
245     p_sys->status.dwCurrentState = SERVICE_RUNNING; 
246     p_sys->status.dwCheckPoint   = 0; 
247     p_sys->status.dwWaitHint     = 0; 
248
249     SetServiceStatus( p_sys->hStatus, &p_sys->status );
250 }
251
252 static void WINAPI ServiceCtrlHandler( DWORD control )
253 {
254     intf_thread_t *p_intf = (intf_thread_t *)p_global_intf;
255     intf_sys_t    *p_sys  = p_intf->p_sys;
256
257     switch( control )
258     {
259     case SERVICE_CONTROL_SHUTDOWN:
260     case SERVICE_CONTROL_STOP:
261         p_sys->status.dwCurrentState = SERVICE_STOPPED;
262         p_sys->status.dwWin32ExitCode = 0;
263         p_sys->status.dwCheckPoint = 0;
264         p_sys->status.dwWaitHint = 0;
265         break;
266     case SERVICE_CONTROL_INTERROGATE:
267         /* just set the current state to whatever it is... */
268         break;
269     }
270
271     SetServiceStatus( p_sys->hStatus, &p_sys->status );
272 }