1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and / or modify it
7 // under the terms of the GNU Lesser General Public version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
21 #include <OSD_Process.ixx>
22 #include <OSD_WhoAmI.hxx>
23 #include <OSD_Environment.hxx>
25 const OSD_WhoAmI Iam = OSD_WProcess;
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
39 #if defined(HAVE_TIME_H) || defined(WNT)
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
48 # include <pwd.h> // For command getpwuid
51 OSD_Process::OSD_Process(){
55 void OSD_Process::Spawn (const TCollection_AsciiString& cmd,
56 const Standard_Boolean /*ShowWindow*/)
58 system(cmd.ToCString());
62 void OSD_Process::TerminalType(TCollection_AsciiString& Name){
63 TCollection_AsciiString which="TERM";
64 OSD_Environment term (which,"");
72 // Get date of system date
74 Quantity_Date OSD_Process::SystemDate(){
76 Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0;
79 struct timezone tzone;
82 status = gettimeofday( &tval, &tzone );
83 if (status == -1) myError.SetValue (errno, Iam, "GetSystem");
85 memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct
87 month = transfert.tm_mon + 1; // Add to January (month #1)
88 day = transfert.tm_mday;
89 year = transfert.tm_year;
90 hh = transfert.tm_hour;
91 mn = transfert.tm_min ;
92 ss = transfert.tm_sec ;
95 result.SetValues ( month, day, year+1900, hh, mn, ss);
100 Standard_Integer OSD_Process::ProcessId(){
105 Standard_Integer OSD_Process::UserId(){
110 TCollection_AsciiString OSD_Process::UserName(){
111 struct passwd *infos;
112 infos = getpwuid(getuid());
113 TCollection_AsciiString result=infos->pw_name;
118 Standard_Boolean OSD_Process::IsSuperUser (){
120 return Standard_False;
123 return Standard_True;
128 OSD_Path OSD_Process::CurrentDirectory(){
129 char cwd[MAXPATHLEN+1] ;
131 TCollection_AsciiString Name;
133 if (!getcwd(cwd,MAXPATHLEN+1))
134 myError.SetValue (errno, Iam, "Where");
138 // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif
139 // position = Name.SearchFromEnd(".");
140 // if (position != -1){
142 // Ext.Remove(1,position);
143 // Name.Remove( position,Ext.Length()+1);
145 // result.SetValues("","","","","",Name,Ext);
148 #if defined(vax) || defined(__vms)
149 Standard_Integer iDisk = Name.Search(":");
151 TCollection_AsciiString Disk;
152 TCollection_AsciiString Directory;
153 Disk = Name.SubString(1,iDisk-1);
154 Directory = Name.SubString(iDisk+1,Name.Length());
155 result.SetValues("","","",Disk,Directory,"","");
158 Name += TCollection_AsciiString("/");
159 result = OSD_Path(Name);
160 // result.SetValues("","","","",Name,"","");
168 void OSD_Process::SetCurrentDirectory(const OSD_Path& where){
169 TCollection_AsciiString Name;
172 where.SystemName(Name);
174 status = chdir (Name.ToCString());
175 if (status == -1) myError.SetValue(errno, Iam, "Move to directory");
179 void OSD_Process::Reset(){
183 Standard_Boolean OSD_Process::Failed()const{
184 return( myError.Failed());
187 void OSD_Process::Perror() {
192 Standard_Integer OSD_Process::Error()const{
193 return( myError.Error());
198 //------------------------------------------------------------------------
199 //------------------- WNT Sources of OSD_Path ---------------------------
200 //------------------------------------------------------------------------
202 //it is important to undefine NOUSER and enforce including <windows.h> before
203 //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors
205 #undef NOUSER /* we need SW_HIDE from windows.h */
209 #ifdef SetCurrentDirectory
210 # undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */
212 #include <OSD_Process.hxx>
214 #include <OSD_Path.hxx>
215 #include <Quantity_Date.hxx>
217 #include <OSD_WNT_1.hxx>
218 #include <LMCONS.H> /// pour UNLEN ( see MSDN about GetUserName() )
221 #pragma warning( disable : 4700 )
223 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
225 OSD_Process :: OSD_Process () {
229 void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd ,
230 const Standard_Boolean ShowWindow /* = Standard_True */) {
233 PROCESS_INFORMATION pi;
235 ZeroMemory ( &si, sizeof ( STARTUPINFO ) );
237 si.cb = sizeof ( STARTUPINFO );
238 //============================================
239 //---> Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
240 //---> Reason : to allow to hide the window
243 si.dwFlags = STARTF_USESHOWWINDOW;
244 si.wShowWindow = SW_HIDE;
246 //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01]
247 //============================================
250 NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi
254 _osd_wnt_set_error ( myError, OSD_WProcess );
258 CloseHandle ( pi.hThread );
260 WaitForSingleObject ( pi.hProcess, INFINITE );
262 CloseHandle ( pi.hProcess );
266 } // end OSD_Process :: Spawn
268 void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) {
270 Name = TEXT( "WIN32 console" );
272 } // end OSD_Process :: TerminalType
274 Quantity_Date OSD_Process :: SystemDate () {
276 Quantity_Date retVal;
279 GetLocalTime ( &st );
282 st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
287 } // end OSD_Process :: SystemDate
289 Standard_Integer OSD_Process :: UserId () {
292 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
293 PTOKEN_OWNER pTKowner = NULL;
295 if ( !OpenProcessToken (
296 GetCurrentProcess (),
297 TOKEN_QUERY, &hProcessToken
299 ( pTKowner = ( PTOKEN_OWNER )GetTokenInformationEx (
300 hProcessToken, TokenOwner
303 ( retVal = CopySidEx ( pTKowner -> Owner ) ) == NULL
306 _osd_wnt_set_error ( myError, OSD_WProcess );
308 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
309 if ( pTKowner != NULL ) FreeTokenInformation ( pTKowner );
311 return ( Standard_Integer )retVal;
313 } // end OSD_Process :: UserId
315 TCollection_AsciiString OSD_Process :: UserName ()
317 Standard_PCharacter pBuff = new char[UNLEN + 1];
318 DWORD dwSize = UNLEN + 1;
319 TCollection_AsciiString retVal;
320 if ( !GetUserName ( pBuff, &dwSize ) )
322 _osd_wnt_set_error ( myError, OSD_WProcess );
326 TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 );
327 retVal = theTmpUserName;
331 } // end OSD_Process :: UserName
333 Standard_Boolean OSD_Process :: IsSuperUser () {
335 Standard_Boolean retVal = FALSE;
337 HANDLE hProcessToken = INVALID_HANDLE_VALUE;
338 PTOKEN_GROUPS pTKgroups = NULL;
340 if ( !OpenProcessToken (
341 GetCurrentProcess (),
342 TOKEN_QUERY, &hProcessToken
344 ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx (
345 hProcessToken, TokenGroups
350 _osd_wnt_set_error ( myError, OSD_WProcess );
354 pSIDadmin = AdminSid ();
356 for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i )
358 if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) {
367 if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken );
368 if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups );
372 } // end OSD_Process :: IsSuperUser
374 Standard_Integer OSD_Process :: ProcessId () {
376 return ( Standard_Integer )GetCurrentProcessId ();
378 } // end OSD_Process :: ProcessId
380 OSD_Path OSD_Process :: CurrentDirectory () {
382 OSD_Path anCurrentDirectory;
384 DWORD dwSize = PATHLEN + 1;
385 Standard_PCharacter pBuff = new char[dwSize];
387 if ( GetCurrentDirectory(dwSize, pBuff) > 0 )
388 anCurrentDirectory = OSD_Path ( pBuff );
390 _osd_wnt_set_error ( myError, OSD_WProcess );
393 return anCurrentDirectory;
394 } // end OSD_Process :: CurrentDirectory
396 void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) {
399 # define SetCurrentDirectory SetCurrentDirectoryW
401 # define SetCurrentDirectory SetCurrentDirectoryA
404 TCollection_AsciiString path;
406 where.SystemName ( path );
408 if ( !::SetCurrentDirectory ( path.ToCString () ) )
410 _osd_wnt_set_error ( myError, OSD_WProcess );
412 } // end OSD_Process :: SetCurrentDirectory
414 Standard_Boolean OSD_Process :: Failed () const {
416 return myError.Failed ();
418 } // end OSD_Process :: Failed
420 void OSD_Process :: Reset () {
424 } // end OSD_Process :: Reset
426 void OSD_Process :: Perror () {
430 } // end OSD_Process :: Perror
432 Standard_Integer OSD_Process :: Error () const {
434 return myError.Error ();
436 } // end OSD_Process :: Error