b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
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. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
7fd59977 |
14 | |
57c28b61 |
15 | #ifndef _WIN32 |
7fd59977 |
16 | |
42cf5bc1 |
17 | |
7fd59977 |
18 | #include <OSD_Environment.hxx> |
42cf5bc1 |
19 | #include <OSD_OSDError.hxx> |
20 | #include <OSD_Path.hxx> |
21 | #include <OSD_Process.hxx> |
22 | #include <OSD_WhoAmI.hxx> |
23 | #include <Quantity_Date.hxx> |
24 | #include <TCollection_AsciiString.hxx> |
7fd59977 |
25 | |
26 | const OSD_WhoAmI Iam = OSD_WProcess; |
27 | |
28 | #include <errno.h> |
7fd59977 |
29 | #include <stdlib.h> |
03155c18 |
30 | #include <sys/param.h> |
31 | #include <sys/time.h> |
32 | #include <pwd.h> // For command getpwuid |
33 | #include <unistd.h> |
7fd59977 |
34 | |
35 | OSD_Process::OSD_Process(){ |
36 | } |
37 | |
38 | |
39 | void OSD_Process::Spawn (const TCollection_AsciiString& cmd, |
40 | const Standard_Boolean /*ShowWindow*/) |
41 | { |
42 | system(cmd.ToCString()); |
43 | } |
44 | |
45 | |
46 | void OSD_Process::TerminalType(TCollection_AsciiString& Name){ |
47 | TCollection_AsciiString which="TERM"; |
48 | OSD_Environment term (which,""); |
49 | |
50 | term.Value(); |
51 | which = term.Value(); |
52 | Name = term.Name(); |
53 | } |
54 | |
55 | |
56 | // Get date of system date |
57 | |
58 | Quantity_Date OSD_Process::SystemDate(){ |
59 | Quantity_Date result; |
60 | Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0; |
61 | struct tm transfert; |
62 | struct timeval tval; |
63 | struct timezone tzone; |
64 | int status; |
65 | |
66 | status = gettimeofday( &tval, &tzone ); |
67 | if (status == -1) myError.SetValue (errno, Iam, "GetSystem"); |
68 | else { |
69 | memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct |
70 | tm)); |
71 | month = transfert.tm_mon + 1; // Add to January (month #1) |
72 | day = transfert.tm_mday; |
73 | year = transfert.tm_year; |
74 | hh = transfert.tm_hour; |
75 | mn = transfert.tm_min ; |
76 | ss = transfert.tm_sec ; |
77 | } |
78 | |
79 | result.SetValues ( month, day, year+1900, hh, mn, ss); |
80 | return (result); |
81 | } |
82 | |
83 | |
84 | Standard_Integer OSD_Process::ProcessId(){ |
85 | return (getpid()); |
86 | } |
87 | |
88 | |
89 | Standard_Integer OSD_Process::UserId(){ |
90 | return (getuid()); |
91 | } |
92 | |
93 | |
94 | TCollection_AsciiString OSD_Process::UserName(){ |
95 | struct passwd *infos; |
96 | infos = getpwuid(getuid()); |
97 | TCollection_AsciiString result=infos->pw_name; |
98 | |
99 | return(result); |
100 | } |
101 | |
102 | Standard_Boolean OSD_Process::IsSuperUser (){ |
103 | if (getuid()) { |
104 | return Standard_False; |
105 | } |
106 | else { |
107 | return Standard_True; |
108 | } |
109 | } |
110 | |
111 | |
112 | OSD_Path OSD_Process::CurrentDirectory(){ |
113 | char cwd[MAXPATHLEN+1] ; |
114 | OSD_Path result; |
115 | TCollection_AsciiString Name; |
116 | |
117 | if (!getcwd(cwd,MAXPATHLEN+1)) |
118 | myError.SetValue (errno, Iam, "Where"); |
119 | else { |
120 | Name = cwd; |
121 | |
122 | // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif |
123 | // position = Name.SearchFromEnd("."); |
124 | // if (position != -1){ |
125 | // Ext = Name; |
126 | // Ext.Remove(1,position); |
127 | // Name.Remove( position,Ext.Length()+1); |
128 | // } |
129 | // result.SetValues("","","","","",Name,Ext); |
130 | // End |
131 | |
132 | #if defined(vax) || defined(__vms) |
133 | Standard_Integer iDisk = Name.Search(":"); |
134 | if (iDisk){ |
135 | TCollection_AsciiString Disk; |
136 | TCollection_AsciiString Directory; |
137 | Disk = Name.SubString(1,iDisk-1); |
138 | Directory = Name.SubString(iDisk+1,Name.Length()); |
139 | result.SetValues("","","",Disk,Directory,"",""); |
140 | } |
141 | #else |
142 | Name += TCollection_AsciiString("/"); |
143 | result = OSD_Path(Name); |
144 | // result.SetValues("","","","",Name,"",""); |
145 | #endif |
146 | |
147 | } |
148 | return (result); |
149 | } |
150 | |
151 | |
152 | void OSD_Process::SetCurrentDirectory(const OSD_Path& where){ |
153 | TCollection_AsciiString Name; |
154 | int status; |
155 | |
156 | where.SystemName(Name); |
157 | |
158 | status = chdir (Name.ToCString()); |
159 | if (status == -1) myError.SetValue(errno, Iam, "Move to directory"); |
160 | } |
161 | |
162 | |
163 | void OSD_Process::Reset(){ |
164 | myError.Reset(); |
165 | } |
166 | |
167 | Standard_Boolean OSD_Process::Failed()const{ |
168 | return( myError.Failed()); |
169 | } |
170 | |
171 | void OSD_Process::Perror() { |
172 | myError.Perror(); |
173 | } |
174 | |
175 | |
176 | Standard_Integer OSD_Process::Error()const{ |
177 | return( myError.Error()); |
178 | } |
179 | |
180 | #else |
181 | |
182 | //------------------------------------------------------------------------ |
183 | //------------------- WNT Sources of OSD_Path --------------------------- |
184 | //------------------------------------------------------------------------ |
185 | |
186 | //it is important to undefine NOUSER and enforce including <windows.h> before |
187 | //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors |
188 | #ifdef NOUSER |
189 | #undef NOUSER /* we need SW_HIDE from windows.h */ |
190 | #endif |
191 | #include <windows.h> |
192 | |
193 | #ifdef SetCurrentDirectory |
194 | # undef SetCurrentDirectory /* undefine SetCurrentDirectory from <winbase.h> to correctly include <OSD_Process.hxx> */ |
195 | #endif |
196 | #include <OSD_Process.hxx> |
197 | |
198 | #include <OSD_Path.hxx> |
199 | #include <Quantity_Date.hxx> |
d9ff84e8 |
200 | #include <Standard_PExtCharacter.hxx> |
201 | #include <TCollection_ExtendedString.hxx> |
7fd59977 |
202 | |
203 | #include <OSD_WNT_1.hxx> |
204 | #include <LMCONS.H> /// pour UNLEN ( see MSDN about GetUserName() ) |
205 | |
206 | |
207 | #pragma warning( disable : 4700 ) |
208 | |
209 | void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... ); |
210 | |
211 | OSD_Process :: OSD_Process () { |
212 | |
213 | } // end constructor |
214 | |
215 | void OSD_Process :: Spawn ( const TCollection_AsciiString& cmd , |
216 | const Standard_Boolean ShowWindow /* = Standard_True */) { |
217 | |
218 | STARTUPINFO si; |
219 | PROCESS_INFORMATION pi; |
220 | |
221 | ZeroMemory ( &si, sizeof ( STARTUPINFO ) ); |
222 | |
223 | si.cb = sizeof ( STARTUPINFO ); |
224 | //============================================ |
225 | //---> Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01] |
226 | //---> Reason : to allow to hide the window |
227 | if ( !ShowWindow ) |
228 | { |
229 | si.dwFlags = STARTF_USESHOWWINDOW; |
230 | si.wShowWindow = SW_HIDE; |
231 | } |
232 | //<--- End Added by Stephane Routelous ( stephane.routelous@altavista.net ) [16.03.01] |
233 | //============================================ |
234 | |
235 | if (!CreateProcess ( |
236 | NULL, (char *)cmd.ToCString (), NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi |
237 | ) |
238 | ) |
239 | |
240 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
241 | |
242 | else { |
243 | |
244 | CloseHandle ( pi.hThread ); |
245 | |
246 | WaitForSingleObject ( pi.hProcess, INFINITE ); |
247 | |
248 | CloseHandle ( pi.hProcess ); |
249 | |
250 | } // end else |
251 | |
252 | } // end OSD_Process :: Spawn |
253 | |
254 | void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) { |
255 | |
d9ff84e8 |
256 | Name = "WIN32 console"; |
7fd59977 |
257 | |
258 | } // end OSD_Process :: TerminalType |
259 | |
260 | Quantity_Date OSD_Process :: SystemDate () { |
261 | |
262 | Quantity_Date retVal; |
263 | SYSTEMTIME st; |
264 | |
265 | GetLocalTime ( &st ); |
266 | |
267 | retVal.SetValues ( |
268 | st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds |
269 | ); |
270 | |
271 | return retVal; |
272 | |
273 | } // end OSD_Process :: SystemDate |
274 | |
275 | Standard_Integer OSD_Process :: UserId () { |
276 | |
277 | PSID retVal = NULL; |
278 | HANDLE hProcessToken = INVALID_HANDLE_VALUE; |
279 | PTOKEN_OWNER pTKowner = NULL; |
280 | |
281 | if ( !OpenProcessToken ( |
282 | GetCurrentProcess (), |
283 | TOKEN_QUERY, &hProcessToken |
284 | ) || |
285 | ( pTKowner = ( PTOKEN_OWNER )GetTokenInformationEx ( |
286 | hProcessToken, TokenOwner |
287 | ) |
288 | ) == NULL || |
289 | ( retVal = CopySidEx ( pTKowner -> Owner ) ) == NULL |
290 | ) |
291 | |
292 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
293 | |
294 | if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken ); |
295 | if ( pTKowner != NULL ) FreeTokenInformation ( pTKowner ); |
296 | |
297 | return ( Standard_Integer )retVal; |
298 | |
299 | } // end OSD_Process :: UserId |
300 | |
301 | TCollection_AsciiString OSD_Process :: UserName () |
302 | { |
303 | Standard_PCharacter pBuff = new char[UNLEN + 1]; |
304 | DWORD dwSize = UNLEN + 1; |
305 | TCollection_AsciiString retVal; |
306 | if ( !GetUserName ( pBuff, &dwSize ) ) |
307 | { |
308 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
309 | } |
310 | else |
311 | { |
312 | TCollection_AsciiString theTmpUserName(pBuff,(int)dwSize -1 ); |
313 | retVal = theTmpUserName; |
314 | } |
315 | delete [] pBuff; |
316 | return retVal; |
317 | } // end OSD_Process :: UserName |
318 | |
319 | Standard_Boolean OSD_Process :: IsSuperUser () { |
320 | |
321 | Standard_Boolean retVal = FALSE; |
322 | PSID pSIDadmin; |
323 | HANDLE hProcessToken = INVALID_HANDLE_VALUE; |
302f96fb |
324 | PTOKEN_GROUPS pTKgroups = NULL; |
7fd59977 |
325 | |
326 | if ( !OpenProcessToken ( |
327 | GetCurrentProcess (), |
328 | TOKEN_QUERY, &hProcessToken |
329 | ) || |
330 | ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx ( |
331 | hProcessToken, TokenGroups |
332 | ) |
333 | ) == NULL |
334 | ) |
335 | |
336 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
337 | |
338 | else { |
339 | |
340 | pSIDadmin = AdminSid (); |
341 | |
342 | for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i ) |
343 | |
344 | if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) { |
345 | |
346 | retVal = TRUE; |
347 | break; |
348 | |
349 | } // end if |
350 | |
351 | } // end else |
352 | |
353 | if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken ); |
354 | if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups ); |
355 | |
356 | return retVal; |
357 | |
358 | } // end OSD_Process :: IsSuperUser |
359 | |
360 | Standard_Integer OSD_Process :: ProcessId () { |
361 | |
362 | return ( Standard_Integer )GetCurrentProcessId (); |
363 | |
364 | } // end OSD_Process :: ProcessId |
365 | |
366 | OSD_Path OSD_Process :: CurrentDirectory () { |
367 | |
a8195d65 |
368 | OSD_Path anCurrentDirectory; |
7fd59977 |
369 | |
a8195d65 |
370 | DWORD dwSize = PATHLEN + 1; |
d9ff84e8 |
371 | Standard_WideChar* pBuff = new wchar_t[dwSize]; |
7fd59977 |
372 | |
d9ff84e8 |
373 | if ( GetCurrentDirectoryW(dwSize, (wchar_t*)pBuff) > 0 ) |
374 | { |
375 | // conversion to UTF-8 is performed inside |
376 | TCollection_AsciiString aPath(TCollection_ExtendedString((Standard_ExtString)pBuff)); |
377 | anCurrentDirectory = OSD_Path ( aPath ); |
378 | } |
a8195d65 |
379 | else |
380 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
7fd59977 |
381 | |
a8195d65 |
382 | delete[] pBuff; |
383 | return anCurrentDirectory; |
7fd59977 |
384 | } // end OSD_Process :: CurrentDirectory |
385 | |
386 | void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) { |
387 | |
7fd59977 |
388 | TCollection_AsciiString path; |
389 | |
390 | where.SystemName ( path ); |
d9ff84e8 |
391 | TCollection_ExtendedString pathW(path); |
7fd59977 |
392 | |
d9ff84e8 |
393 | if ( !::SetCurrentDirectoryW ( (const wchar_t*) pathW.ToExtString () ) ) |
7fd59977 |
394 | |
395 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
396 | |
397 | } // end OSD_Process :: SetCurrentDirectory |
398 | |
399 | Standard_Boolean OSD_Process :: Failed () const { |
400 | |
401 | return myError.Failed (); |
402 | |
403 | } // end OSD_Process :: Failed |
404 | |
405 | void OSD_Process :: Reset () { |
406 | |
407 | myError.Reset (); |
408 | |
409 | } // end OSD_Process :: Reset |
410 | |
411 | void OSD_Process :: Perror () { |
412 | |
413 | myError.Perror (); |
414 | |
415 | } // end OSD_Process :: Perror |
416 | |
417 | Standard_Integer OSD_Process :: Error () const { |
418 | |
419 | return myError.Error (); |
420 | |
421 | } // end OSD_Process :: Error |
422 | |
423 | #endif |