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 | |
aafe169f |
15 | #ifdef _WIN32 |
16 | //it is important to undefine NOUSER and enforce including <windows.h> before |
17 | //Standard_Macro.hxx defines it and includes <windows.h> causing compilation errors |
18 | #ifdef NOUSER |
19 | #undef NOUSER // we need SW_HIDE from windows.h |
20 | #endif |
21 | #include <windows.h> |
22 | #endif |
7fd59977 |
23 | |
aafe169f |
24 | #include <OSD_Process.hxx> |
25 | |
26 | #include <NCollection_Array1.hxx> |
7fd59977 |
27 | #include <OSD_Environment.hxx> |
42cf5bc1 |
28 | #include <OSD_OSDError.hxx> |
29 | #include <OSD_Path.hxx> |
42cf5bc1 |
30 | #include <OSD_WhoAmI.hxx> |
aafe169f |
31 | #include <Standard_PExtCharacter.hxx> |
32 | #include <TCollection_ExtendedString.hxx> |
42cf5bc1 |
33 | #include <Quantity_Date.hxx> |
7fd59977 |
34 | |
aafe169f |
35 | #ifdef _WIN32 |
36 | #include <OSD_WNT.hxx> |
37 | #include <lmcons.h> // for UNLEN - maximum user name length GetUserName() |
38 | #else |
39 | const OSD_WhoAmI Iam = OSD_WProcess; |
40 | #include <errno.h> |
41 | #include <stdlib.h> |
42 | #include <sys/param.h> |
43 | #include <sys/time.h> |
44 | #include <pwd.h> // For command getpwuid |
45 | #include <unistd.h> |
46 | #endif |
7fd59977 |
47 | |
aafe169f |
48 | #if defined(__APPLE__) |
49 | #include <mach-o/dyld.h> |
50 | #endif |
51 | |
52 | #ifndef _WIN32 |
7fd59977 |
53 | |
54 | OSD_Process::OSD_Process(){ |
55 | } |
56 | |
57 | |
7fd59977 |
58 | void OSD_Process::TerminalType(TCollection_AsciiString& Name){ |
59 | TCollection_AsciiString which="TERM"; |
60 | OSD_Environment term (which,""); |
61 | |
62 | term.Value(); |
63 | which = term.Value(); |
64 | Name = term.Name(); |
65 | } |
66 | |
67 | |
68 | // Get date of system date |
69 | |
70 | Quantity_Date OSD_Process::SystemDate(){ |
71 | Quantity_Date result; |
72 | Standard_Integer month=0,day=0,year=0,hh=0,mn=0,ss=0; |
73 | struct tm transfert; |
74 | struct timeval tval; |
75 | struct timezone tzone; |
76 | int status; |
77 | |
78 | status = gettimeofday( &tval, &tzone ); |
79 | if (status == -1) myError.SetValue (errno, Iam, "GetSystem"); |
80 | else { |
81 | memcpy(&transfert, localtime((time_t *)&tval.tv_sec), sizeof(struct |
82 | tm)); |
83 | month = transfert.tm_mon + 1; // Add to January (month #1) |
84 | day = transfert.tm_mday; |
85 | year = transfert.tm_year; |
86 | hh = transfert.tm_hour; |
87 | mn = transfert.tm_min ; |
88 | ss = transfert.tm_sec ; |
89 | } |
90 | |
91 | result.SetValues ( month, day, year+1900, hh, mn, ss); |
92 | return (result); |
93 | } |
94 | |
95 | |
96 | Standard_Integer OSD_Process::ProcessId(){ |
97 | return (getpid()); |
98 | } |
99 | |
505e241c |
100 | TCollection_AsciiString OSD_Process::UserName() |
101 | { |
102 | struct passwd *anInfos = getpwuid (getuid()); |
103 | return TCollection_AsciiString (anInfos ? anInfos->pw_name : ""); |
7fd59977 |
104 | } |
105 | |
106 | Standard_Boolean OSD_Process::IsSuperUser (){ |
107 | if (getuid()) { |
108 | return Standard_False; |
109 | } |
110 | else { |
111 | return Standard_True; |
112 | } |
113 | } |
114 | |
115 | |
116 | OSD_Path OSD_Process::CurrentDirectory(){ |
117 | char cwd[MAXPATHLEN+1] ; |
118 | OSD_Path result; |
119 | TCollection_AsciiString Name; |
120 | |
121 | if (!getcwd(cwd,MAXPATHLEN+1)) |
122 | myError.SetValue (errno, Iam, "Where"); |
123 | else { |
124 | Name = cwd; |
125 | |
126 | // JPT : August,20 1993. This code has been replaced by #ifdef ... #endif |
127 | // position = Name.SearchFromEnd("."); |
128 | // if (position != -1){ |
129 | // Ext = Name; |
130 | // Ext.Remove(1,position); |
131 | // Name.Remove( position,Ext.Length()+1); |
132 | // } |
133 | // result.SetValues("","","","","",Name,Ext); |
134 | // End |
135 | |
136 | #if defined(vax) || defined(__vms) |
137 | Standard_Integer iDisk = Name.Search(":"); |
138 | if (iDisk){ |
139 | TCollection_AsciiString Disk; |
140 | TCollection_AsciiString Directory; |
141 | Disk = Name.SubString(1,iDisk-1); |
142 | Directory = Name.SubString(iDisk+1,Name.Length()); |
143 | result.SetValues("","","",Disk,Directory,"",""); |
144 | } |
145 | #else |
146 | Name += TCollection_AsciiString("/"); |
147 | result = OSD_Path(Name); |
148 | // result.SetValues("","","","",Name,"",""); |
149 | #endif |
150 | |
151 | } |
152 | return (result); |
153 | } |
154 | |
155 | |
156 | void OSD_Process::SetCurrentDirectory(const OSD_Path& where){ |
157 | TCollection_AsciiString Name; |
158 | int status; |
159 | |
160 | where.SystemName(Name); |
161 | |
162 | status = chdir (Name.ToCString()); |
163 | if (status == -1) myError.SetValue(errno, Iam, "Move to directory"); |
164 | } |
165 | |
166 | |
167 | void OSD_Process::Reset(){ |
168 | myError.Reset(); |
169 | } |
170 | |
171 | Standard_Boolean OSD_Process::Failed()const{ |
172 | return( myError.Failed()); |
173 | } |
174 | |
175 | void OSD_Process::Perror() { |
176 | myError.Perror(); |
177 | } |
178 | |
179 | |
180 | Standard_Integer OSD_Process::Error()const{ |
181 | return( myError.Error()); |
182 | } |
183 | |
184 | #else |
185 | |
186 | //------------------------------------------------------------------------ |
187 | //------------------- WNT Sources of OSD_Path --------------------------- |
188 | //------------------------------------------------------------------------ |
189 | |
1bd04b5a |
190 | void _osd_wnt_set_error ( OSD_Error&, Standard_Integer, ... ); |
7fd59977 |
191 | |
ad03c234 |
192 | // ======================================================================= |
193 | // function : OSD_Process |
194 | // purpose : |
195 | // ======================================================================= |
196 | OSD_Process::OSD_Process() |
197 | { |
198 | // |
199 | } |
98160038 |
200 | |
7fd59977 |
201 | void OSD_Process :: TerminalType ( TCollection_AsciiString& Name ) { |
202 | |
d9ff84e8 |
203 | Name = "WIN32 console"; |
7fd59977 |
204 | |
205 | } // end OSD_Process :: TerminalType |
206 | |
207 | Quantity_Date OSD_Process :: SystemDate () { |
208 | |
209 | Quantity_Date retVal; |
210 | SYSTEMTIME st; |
211 | |
212 | GetLocalTime ( &st ); |
213 | |
214 | retVal.SetValues ( |
215 | st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds |
216 | ); |
217 | |
218 | return retVal; |
219 | |
220 | } // end OSD_Process :: SystemDate |
221 | |
ad03c234 |
222 | // ======================================================================= |
223 | // function : UserName |
224 | // purpose : |
225 | // ======================================================================= |
226 | TCollection_AsciiString OSD_Process::UserName() |
7fd59977 |
227 | { |
742cc8b0 |
228 | #ifndef OCCT_UWP |
ad03c234 |
229 | wchar_t aUserName[UNLEN + 1]; |
230 | DWORD aNameSize = UNLEN + 1; |
231 | TCollection_AsciiString retVal; |
232 | if (!GetUserNameW (aUserName, &aNameSize)) |
233 | { |
234 | _osd_wnt_set_error(myError, OSD_WProcess); |
235 | return TCollection_AsciiString(); |
236 | } |
237 | return TCollection_AsciiString (aUserName); |
742cc8b0 |
238 | #else |
ad03c234 |
239 | return TCollection_AsciiString(); |
742cc8b0 |
240 | #endif |
ad03c234 |
241 | } |
7fd59977 |
242 | |
243 | Standard_Boolean OSD_Process :: IsSuperUser () { |
742cc8b0 |
244 | #ifndef OCCT_UWP |
7fd59977 |
245 | Standard_Boolean retVal = FALSE; |
246 | PSID pSIDadmin; |
247 | HANDLE hProcessToken = INVALID_HANDLE_VALUE; |
302f96fb |
248 | PTOKEN_GROUPS pTKgroups = NULL; |
7fd59977 |
249 | |
250 | if ( !OpenProcessToken ( |
251 | GetCurrentProcess (), |
252 | TOKEN_QUERY, &hProcessToken |
253 | ) || |
254 | ( pTKgroups = ( PTOKEN_GROUPS )GetTokenInformationEx ( |
255 | hProcessToken, TokenGroups |
256 | ) |
257 | ) == NULL |
258 | ) |
259 | |
260 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
261 | |
262 | else { |
263 | |
264 | pSIDadmin = AdminSid (); |
265 | |
266 | for ( int i = 0; i < ( int )pTKgroups -> GroupCount; ++i ) |
267 | |
268 | if ( EqualSid ( pTKgroups -> Groups[ i ].Sid, pSIDadmin ) ) { |
269 | |
270 | retVal = TRUE; |
271 | break; |
272 | |
273 | } // end if |
274 | |
275 | } // end else |
276 | |
277 | if ( hProcessToken != INVALID_HANDLE_VALUE ) CloseHandle ( hProcessToken ); |
278 | if ( pTKgroups != NULL ) FreeTokenInformation ( pTKgroups ); |
279 | |
280 | return retVal; |
742cc8b0 |
281 | #else |
282 | return FALSE; |
283 | #endif |
7fd59977 |
284 | } // end OSD_Process :: IsSuperUser |
285 | |
ad03c234 |
286 | // ======================================================================= |
287 | // function : ProcessId |
288 | // purpose : |
289 | // ======================================================================= |
290 | Standard_Integer OSD_Process::ProcessId() |
291 | { |
292 | return (Standard_Integer )GetCurrentProcessId(); |
293 | } |
7fd59977 |
294 | |
ad03c234 |
295 | // ======================================================================= |
296 | // function : CurrentDirectory |
297 | // purpose : |
298 | // ======================================================================= |
299 | OSD_Path OSD_Process::CurrentDirectory() |
300 | { |
a8195d65 |
301 | OSD_Path anCurrentDirectory; |
742cc8b0 |
302 | #ifndef OCCT_UWP |
ad03c234 |
303 | const DWORD aBuffLen = GetCurrentDirectoryW (0, NULL); |
304 | if (aBuffLen > 0) |
d9ff84e8 |
305 | { |
ad03c234 |
306 | wchar_t* aBuff = new wchar_t[aBuffLen + 1]; |
307 | GetCurrentDirectoryW (aBuffLen, aBuff); |
308 | aBuff[aBuffLen] = L'\0'; |
309 | const TCollection_AsciiString aPath (aBuff); |
310 | delete[] aBuff; |
311 | |
312 | anCurrentDirectory = OSD_Path (aPath); |
d9ff84e8 |
313 | } |
a8195d65 |
314 | else |
ad03c234 |
315 | { |
316 | _osd_wnt_set_error (myError, OSD_WProcess); |
317 | } |
742cc8b0 |
318 | #endif |
a8195d65 |
319 | return anCurrentDirectory; |
ad03c234 |
320 | } |
7fd59977 |
321 | |
322 | void OSD_Process :: SetCurrentDirectory ( const OSD_Path& where ) { |
323 | |
7fd59977 |
324 | TCollection_AsciiString path; |
325 | |
326 | where.SystemName ( path ); |
d9ff84e8 |
327 | TCollection_ExtendedString pathW(path); |
7fd59977 |
328 | |
fb0b0531 |
329 | if (!::SetCurrentDirectoryW (pathW.ToWideString())) |
7fd59977 |
330 | |
331 | _osd_wnt_set_error ( myError, OSD_WProcess ); |
332 | |
333 | } // end OSD_Process :: SetCurrentDirectory |
334 | |
335 | Standard_Boolean OSD_Process :: Failed () const { |
336 | |
337 | return myError.Failed (); |
338 | |
339 | } // end OSD_Process :: Failed |
340 | |
341 | void OSD_Process :: Reset () { |
342 | |
343 | myError.Reset (); |
344 | |
345 | } // end OSD_Process :: Reset |
346 | |
347 | void OSD_Process :: Perror () { |
348 | |
349 | myError.Perror (); |
350 | |
351 | } // end OSD_Process :: Perror |
352 | |
353 | Standard_Integer OSD_Process :: Error () const { |
354 | |
355 | return myError.Error (); |
356 | |
357 | } // end OSD_Process :: Error |
358 | |
359 | #endif |
aafe169f |
360 | |
361 | // ======================================================================= |
362 | // function : ExecutablePath |
363 | // purpose : |
364 | // ======================================================================= |
365 | TCollection_AsciiString OSD_Process::ExecutablePath() |
366 | { |
367 | #ifdef _WIN32 |
368 | wchar_t aBuff[MAX_PATH + 2]; |
369 | DWORD aLenFilled = GetModuleFileNameW (0, aBuff, MAX_PATH + 1); |
370 | aBuff[MAX_PATH + 1] = 0; |
371 | if (aLenFilled == 0) |
372 | { |
373 | return TCollection_AsciiString(); |
374 | } |
375 | else if (aLenFilled <= MAX_PATH) |
376 | { |
377 | return TCollection_AsciiString (aBuff); |
378 | } |
379 | |
380 | // buffer is not large enough (e.g. path uses \\?\ prefix) |
381 | wchar_t* aBuffDyn = NULL; |
382 | for (int anIter = 2;; ++anIter) |
383 | { |
384 | size_t aBuffLen = MAX_PATH * anIter; |
385 | aBuffDyn = reinterpret_cast<wchar_t*> (realloc (aBuffDyn, sizeof(wchar_t) * (aBuffLen + 1))); |
386 | if (aBuffDyn == NULL) |
387 | { |
388 | return TCollection_AsciiString(); |
389 | } |
390 | |
391 | aLenFilled = GetModuleFileNameW (NULL, aBuffDyn, DWORD(aBuffLen)); |
392 | if (aLenFilled != aBuffLen) |
393 | { |
394 | aBuffDyn[aBuffLen] = L'\0'; |
395 | TCollection_AsciiString aRes (aBuffDyn); |
396 | free (aBuffDyn); |
397 | return aRes; |
398 | } |
399 | } |
400 | #elif defined(__APPLE__) |
401 | // determine buffer size |
402 | uint32_t aNbBytes = 0; |
403 | _NSGetExecutablePath (NULL, &aNbBytes); |
404 | if (aNbBytes == 0) |
405 | { |
406 | return TCollection_AsciiString(); |
407 | } |
408 | |
409 | // retrieve path to executable (probably link) |
410 | NCollection_Array1<char> aBuff (0, aNbBytes); |
411 | _NSGetExecutablePath (&aBuff.ChangeFirst(), &aNbBytes); |
412 | aBuff[aNbBytes] = '\0'; |
413 | |
414 | // retrieve real path to executable (resolve links and normalize) |
415 | char* aResultBuf = realpath (&aBuff.First(), NULL); |
416 | if (aResultBuf == NULL) |
417 | { |
418 | return TCollection_AsciiString(); |
419 | } |
420 | |
421 | TCollection_AsciiString aProcessPath (aResultBuf); |
422 | free (aResultBuf); // according to man for realpath() |
423 | return aProcessPath; |
424 | #elif defined(__linux__) |
425 | // get info from /proc/PID/exe |
426 | |
427 | TCollection_AsciiString aSimLink = TCollection_AsciiString("/proc/") + TCollection_AsciiString(getpid()) + "/exe"; |
428 | char aBuff[4096]; |
429 | ssize_t aBytes = readlink (aSimLink.ToCString(), aBuff, 4096); |
430 | if (aBytes > 0) |
431 | { |
432 | aBuff[aBytes] = '\0'; |
433 | return TCollection_AsciiString(aBuff); |
434 | } |
435 | return TCollection_AsciiString(); |
436 | #else |
437 | // not implemented |
438 | return TCollection_AsciiString(); |
439 | #endif |
440 | } |
441 | |
442 | // ======================================================================= |
443 | // function : ExecutableFolder |
444 | // purpose : |
445 | // ======================================================================= |
446 | TCollection_AsciiString OSD_Process::ExecutableFolder() |
447 | { |
448 | TCollection_AsciiString aFullPath = ExecutablePath(); |
449 | Standard_Integer aLastSplit = -1; |
450 | #ifdef _WIN32 |
451 | const char THE_FILE_SEPARATOR = '\\'; |
452 | #else |
453 | const char THE_FILE_SEPARATOR = '/'; |
454 | #endif |
455 | for (Standard_Integer anIter = 1; anIter <= aFullPath.Length(); ++anIter) |
456 | { |
457 | if (aFullPath.Value (anIter) == THE_FILE_SEPARATOR) |
458 | { |
459 | aLastSplit = anIter; |
460 | } |
461 | } |
462 | |
463 | if (aLastSplit != -1) |
464 | { |
465 | return aFullPath.SubString (1, aLastSplit); |
466 | } |
467 | return TCollection_AsciiString(); |
468 | } |