xref: /aoo42x/main/shell/source/win32/SysShExec.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_shell.hxx"
30 
31 //------------------------------------------------------------------------
32 // includes
33 //------------------------------------------------------------------------
34 #include <osl/diagnose.h>
35 #include "SysShExec.hxx"
36 #include <osl/file.hxx>
37 
38 #ifndef _COM_SUN_STAR_SYS_SHELL_SYSTEMSHELLEXECUTEFLAGS_HPP_
39 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
40 #endif
41 
42 #define WIN32_LEAN_AND_MEAN
43 #if defined _MSC_VER
44 #pragma warning(push, 1)
45 #endif
46 #include <windows.h>
47 #include <shellapi.h>
48 #include <objbase.h>
49 #if defined _MSC_VER
50 #pragma warning(pop)
51 #endif
52 
53 //------------------------------------------------------------------------
54 // namespace directives
55 //------------------------------------------------------------------------
56 
57 using com::sun::star::uno::Reference;
58 using com::sun::star::uno::RuntimeException;
59 using com::sun::star::uno::Sequence;
60 using com::sun::star::uno::XInterface;
61 using com::sun::star::lang::EventObject;
62 using com::sun::star::lang::XServiceInfo;
63 using com::sun::star::lang::IllegalArgumentException;
64 using rtl::OUString;
65 using osl::Mutex;
66 using com::sun::star::system::XSystemShellExecute;
67 using com::sun::star::system::SystemShellExecuteException;
68 
69 using namespace ::com::sun::star::system::SystemShellExecuteFlags;
70 using namespace cppu;
71 
72 //------------------------------------------------------------------------
73 // defines
74 //------------------------------------------------------------------------
75 
76 #define SYSSHEXEC_IMPL_NAME  "com.sun.star.sys.shell.SystemShellExecute"
77 
78 //------------------------------------------------------------------------
79 // helper functions
80 //------------------------------------------------------------------------
81 
82 namespace // private
83 {
84 	Sequence< OUString > SAL_CALL SysShExec_getSupportedServiceNames()
85 	{
86 		Sequence< OUString > aRet(1);
87 		aRet[0] = OUString::createFromAscii("com.sun.star.sys.shell.SystemShellExecute");
88 		return aRet;
89 	}
90 
91     /* This is the error table that defines the mapping between OS error
92     codes and errno values */
93 
94     struct errentry {
95         unsigned long oscode;	/* OS return value */
96         int errnocode;			/* System V error code */
97     };
98 
99     struct errentry errtable[] = {
100         {  ERROR_SUCCESS,				 osl_File_E_None     },  /* 0 */
101         {  ERROR_INVALID_FUNCTION,       osl_File_E_INVAL    },  /* 1 */
102         {  ERROR_FILE_NOT_FOUND,         osl_File_E_NOENT    },  /* 2 */
103         {  ERROR_PATH_NOT_FOUND,         osl_File_E_NOENT    },  /* 3 */
104         {  ERROR_TOO_MANY_OPEN_FILES,    osl_File_E_MFILE    },  /* 4 */
105         {  ERROR_ACCESS_DENIED,          osl_File_E_ACCES    },  /* 5 */
106         {  ERROR_INVALID_HANDLE,         osl_File_E_BADF     },  /* 6 */
107         {  ERROR_ARENA_TRASHED,          osl_File_E_NOMEM    },  /* 7 */
108         {  ERROR_NOT_ENOUGH_MEMORY,      osl_File_E_NOMEM    },  /* 8 */
109         {  ERROR_INVALID_BLOCK,          osl_File_E_NOMEM    },  /* 9 */
110         {  ERROR_BAD_ENVIRONMENT,        osl_File_E_2BIG     },  /* 10 */
111         {  ERROR_BAD_FORMAT,             osl_File_E_NOEXEC   },  /* 11 */
112         {  ERROR_INVALID_ACCESS,         osl_File_E_INVAL    },  /* 12 */
113         {  ERROR_INVALID_DATA,           osl_File_E_INVAL    },  /* 13 */
114         {  ERROR_INVALID_DRIVE,          osl_File_E_NOENT    },  /* 15 */
115         {  ERROR_CURRENT_DIRECTORY,      osl_File_E_ACCES    },  /* 16 */
116         {  ERROR_NOT_SAME_DEVICE,        osl_File_E_XDEV     },  /* 17 */
117         {  ERROR_NO_MORE_FILES,          osl_File_E_NOENT    },  /* 18 */
118         {  ERROR_LOCK_VIOLATION,         osl_File_E_ACCES    },  /* 33 */
119         {  ERROR_BAD_NETPATH,            osl_File_E_NOENT    },  /* 53 */
120         {  ERROR_NETWORK_ACCESS_DENIED,  osl_File_E_ACCES    },  /* 65 */
121         {  ERROR_BAD_NET_NAME,           osl_File_E_NOENT    },  /* 67 */
122         {  ERROR_FILE_EXISTS,            osl_File_E_EXIST    },  /* 80 */
123         {  ERROR_CANNOT_MAKE,            osl_File_E_ACCES    },  /* 82 */
124         {  ERROR_FAIL_I24,               osl_File_E_ACCES    },  /* 83 */
125         {  ERROR_INVALID_PARAMETER,      osl_File_E_INVAL    },  /* 87 */
126         {  ERROR_NO_PROC_SLOTS,          osl_File_E_AGAIN    },  /* 89 */
127         {  ERROR_DRIVE_LOCKED,           osl_File_E_ACCES    },  /* 108 */
128         {  ERROR_BROKEN_PIPE,            osl_File_E_PIPE     },  /* 109 */
129         {  ERROR_DISK_FULL,              osl_File_E_NOSPC    },  /* 112 */
130         {  ERROR_INVALID_TARGET_HANDLE,  osl_File_E_BADF     },  /* 114 */
131         {  ERROR_INVALID_HANDLE,         osl_File_E_INVAL    },  /* 124 */
132         {  ERROR_WAIT_NO_CHILDREN,       osl_File_E_CHILD    },  /* 128 */
133         {  ERROR_CHILD_NOT_COMPLETE,     osl_File_E_CHILD    },  /* 129 */
134         {  ERROR_DIRECT_ACCESS_HANDLE,   osl_File_E_BADF     },  /* 130 */
135         {  ERROR_NEGATIVE_SEEK,          osl_File_E_INVAL    },  /* 131 */
136         {  ERROR_SEEK_ON_DEVICE,         osl_File_E_ACCES    },  /* 132 */
137         {  ERROR_DIR_NOT_EMPTY,          osl_File_E_NOTEMPTY },  /* 145 */
138         {  ERROR_NOT_LOCKED,             osl_File_E_ACCES    },  /* 158 */
139         {  ERROR_BAD_PATHNAME,           osl_File_E_NOENT    },  /* 161 */
140         {  ERROR_MAX_THRDS_REACHED,      osl_File_E_AGAIN    },  /* 164 */
141         {  ERROR_LOCK_FAILED,            osl_File_E_ACCES    },  /* 167 */
142         {  ERROR_ALREADY_EXISTS,         osl_File_E_EXIST    },  /* 183 */
143         {  ERROR_FILENAME_EXCED_RANGE,   osl_File_E_NOENT    },  /* 206 */
144         {  ERROR_NESTING_NOT_ALLOWED,    osl_File_E_AGAIN    },  /* 215 */
145         {  ERROR_NOT_ENOUGH_QUOTA,       osl_File_E_NOMEM    }    /* 1816 */
146     };
147 
148     /* size of the table */
149     #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
150 
151     /* The following two constants must be the minimum and maximum
152     values in the (contiguous) range of osl_File_E_xec Failure errors. */
153     #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
154     #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
155 
156     /* These are the low and high value in the range of errors that are
157     access violations */
158     #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
159     #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
160 
161 
162     /*******************************************************************************/
163 
164     oslFileError _mapError( DWORD dwError )
165     {
166         int i;
167 
168         /* check the table for the OS error code */
169         for ( i = 0; i < ERRTABLESIZE; ++i )
170 	    {
171 		    if ( dwError == errtable[i].oscode )
172 			    return (oslFileError)errtable[i].errnocode;
173         }
174 
175         /* The error code wasn't in the table.  We check for a range of */
176         /* osl_File_E_ACCES errors or exec failure errors (ENOEXEC).  Otherwise   */
177         /* osl_File_E_INVAL is returned.                                          */
178 
179         if ( dwError >= MIN_EACCES_RANGE && dwError <= MAX_EACCES_RANGE)
180 		    return osl_File_E_ACCES;
181         else if ( dwError >= MIN_EXEC_ERROR && dwError <= MAX_EXEC_ERROR)
182 		    return osl_File_E_NOEXEC;
183         else
184 		    return osl_File_E_INVAL;
185     }
186 
187     #define MapError( oserror )	_mapError( oserror )
188 
189     #define E_UNKNOWN_EXEC_ERROR -1
190 
191     //-----------------------------------------
192     //-----------------------------------------
193 
194     bool is_system_path(const OUString& path_or_uri)
195     {
196         OUString url;
197         osl::FileBase::RC rc = osl::FileBase::getFileURLFromSystemPath(path_or_uri, url);
198         return (rc == osl::FileBase::E_None);
199     }
200 
201     //-----------------------------------------
202     // trying to identify a jump mark
203     //-----------------------------------------
204 
205     const OUString    JUMP_MARK_HTM  = OUString::createFromAscii(".htm#");
206     const OUString    JUMP_MARK_HTML = OUString::createFromAscii(".html#");
207     const sal_Unicode HASH_MARK      = (sal_Unicode)'#';
208 
209     bool has_jump_mark(const OUString& system_path, sal_Int32* jmp_mark_start = NULL)
210     {
211         sal_Int32 jmp_mark = std::max<int>(
212             system_path.lastIndexOf(JUMP_MARK_HTM),
213             system_path.lastIndexOf(JUMP_MARK_HTML));
214 
215         if (jmp_mark_start)
216             *jmp_mark_start = jmp_mark;
217 
218         return (jmp_mark > -1);
219     }
220 
221     //-----------------------------------------
222     //-----------------------------------------
223 
224     bool is_existing_file(const OUString& file_name)
225     {
226         OSL_ASSERT(is_system_path(file_name));
227 
228         bool exist = false;
229 
230         OUString file_url;
231         osl::FileBase::RC rc = osl::FileBase::getFileURLFromSystemPath(file_name, file_url);
232 
233         if (osl::FileBase::E_None == rc)
234         {
235             osl::DirectoryItem dir_item;
236             rc = osl::DirectoryItem::get(file_url, dir_item);
237             exist = (osl::FileBase::E_None == rc);
238         }
239         return exist;
240     }
241 
242     //-------------------------------------------------
243     // Jump marks in file urls are illegal.
244     //-------------------------------------------------
245 
246     void remove_jump_mark(OUString* p_command)
247     {
248         OSL_PRECOND(p_command, "invalid parameter");
249 
250         sal_Int32 pos;
251         if (has_jump_mark(*p_command, &pos))
252         {
253             const sal_Unicode* p_jmp_mark = p_command->getStr() + pos;
254             while (*p_jmp_mark && (*p_jmp_mark != HASH_MARK))
255                 p_jmp_mark++;
256 
257             *p_command = OUString(p_command->getStr(), p_jmp_mark - p_command->getStr());
258         }
259     }
260 
261 } // end namespace
262 
263 //-----------------------------------------------------------------------------------------
264 //
265 //-----------------------------------------------------------------------------------------
266 
267 CSysShExec::CSysShExec( ) :
268 	WeakComponentImplHelper2< XSystemShellExecute, XServiceInfo >( m_aMutex )
269 {
270     /*
271      * As this service is declared thread-affine, it is ensured to be called from a
272      * dedicated thread, so initialize COM here.
273      *
274      * We need COM to be initialized for STA, but osl thread get initialized for MTA.
275      * Once this changed, we can remove the uninitialize call.
276      */
277     CoUninitialize();
278     CoInitialize( NULL );
279 }
280 
281 //-------------------------------------------------
282 //
283 //-------------------------------------------------
284 
285 void SAL_CALL CSysShExec::execute( const OUString& aCommand, const OUString& aParameter, sal_Int32 nFlags )
286         throw (IllegalArgumentException, SystemShellExecuteException, RuntimeException)
287 {
288     // parameter checking
289     if (0 == aCommand.getLength())
290         throw IllegalArgumentException(
291             OUString::createFromAscii( "Empty command" ),
292             static_cast< XSystemShellExecute* >( this ),
293             1 );
294 
295     if (!(nFlags >= DEFAULTS && nFlags <= NO_SYSTEM_ERROR_MESSAGE))
296         throw IllegalArgumentException(
297             OUString::createFromAscii( "Invalid Flags specified" ),
298             static_cast< XSystemShellExecute* >( this ),
299             3 );
300 
301     /*  #i4789#; jump mark detection on system paths
302         if the given command is a system path (not http or
303         other uri schemes) and seems to have a jump mark
304         and names no existing file (remeber the jump mark
305         sign '#' is a valid file name character we remove
306         the jump mark, else ShellExecuteEx fails */
307     OUString preprocessed_command(aCommand);
308     if (is_system_path(preprocessed_command))
309     {
310         if (has_jump_mark(preprocessed_command) && !is_existing_file(preprocessed_command))
311             remove_jump_mark(&preprocessed_command);
312     }
313     /* Convert file uris to system paths */
314     else
315     {
316         OUString aSystemPath;
317         if (::osl::FileBase::E_None == ::osl::FileBase::getSystemPathFromFileURL(preprocessed_command, aSystemPath))
318             preprocessed_command = aSystemPath;
319     }
320 
321     SHELLEXECUTEINFOW sei;
322     ZeroMemory(&sei, sizeof( sei));
323 
324     sei.cbSize       = sizeof(sei);
325     sei.lpFile       = reinterpret_cast<LPCWSTR>(preprocessed_command.getStr());
326     sei.lpParameters = reinterpret_cast<LPCWSTR>(aParameter.getStr());
327     sei.nShow        = SW_SHOWNORMAL;
328 
329     if (NO_SYSTEM_ERROR_MESSAGE & nFlags)
330         sei.fMask = SEE_MASK_FLAG_NO_UI;
331 
332     SetLastError( 0 );
333 
334     sal_Bool bRet = ShellExecuteExW(&sei) ? sal_True : sal_False;
335 
336     if (!bRet && (nFlags & NO_SYSTEM_ERROR_MESSAGE))
337     {
338         // ShellExecuteEx fails to set an error code
339         // we return osl_File_E_INVAL
340         sal_Int32 psxErr = GetLastError();
341         if (ERROR_SUCCESS == psxErr)
342             psxErr = E_UNKNOWN_EXEC_ERROR;
343         else
344             psxErr = MapError(psxErr);
345 
346         throw SystemShellExecuteException(
347             OUString::createFromAscii("Error executing command"),
348             static_cast< XSystemShellExecute* >(this),
349             psxErr);
350     }
351 }
352 
353 // -------------------------------------------------
354 // XServiceInfo
355 // -------------------------------------------------
356 
357 OUString SAL_CALL CSysShExec::getImplementationName(  )
358 	throw( RuntimeException )
359 {
360 	return OUString::createFromAscii( SYSSHEXEC_IMPL_NAME );
361 }
362 
363 // -------------------------------------------------
364 //	XServiceInfo
365 // -------------------------------------------------
366 
367 sal_Bool SAL_CALL CSysShExec::supportsService( const OUString& ServiceName )
368 	throw( RuntimeException )
369 {
370 	Sequence < OUString > SupportedServicesNames = SysShExec_getSupportedServiceNames();
371 
372 	for ( sal_Int32 n = SupportedServicesNames.getLength(); n--; )
373 		if (SupportedServicesNames[n].compareTo(ServiceName) == 0)
374 			return sal_True;
375 
376 	return sal_False;
377 }
378 
379 // -------------------------------------------------
380 //	XServiceInfo
381 // -------------------------------------------------
382 
383 Sequence< OUString > SAL_CALL CSysShExec::getSupportedServiceNames(	 )
384 	throw( RuntimeException )
385 {
386 	return SysShExec_getSupportedServiceNames();
387 }
388 
389