]>
Commit | Line | Data |
---|---|---|
518b5d2f | 1 | ///////////////////////////////////////////////////////////////////////////// |
7520f3da | 2 | // Name: src/unix/utilsunx.cpp |
7e86b10b | 3 | // Purpose: generic Unix implementation of many wx functions (for wxBase) |
518b5d2f VZ |
4 | // Author: Vadim Zeitlin |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin | |
65571936 | 7 | // Licence: wxWindows licence |
518b5d2f VZ |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
14f355c2 VS |
18 | // for compilers that support precompilation, includes "wx.h". |
19 | #include "wx/wxprec.h" | |
20 | ||
88a7a4e1 WS |
21 | #include "wx/utils.h" |
22 | ||
c0472c7c VZ |
23 | #define USE_PUTENV (!defined(HAVE_SETENV) && defined(HAVE_PUTENV)) |
24 | ||
7520f3da WS |
25 | #ifndef WX_PRECOMP |
26 | #include "wx/string.h" | |
88a7a4e1 | 27 | #include "wx/intl.h" |
e4db172a | 28 | #include "wx/log.h" |
670f9935 | 29 | #include "wx/app.h" |
0cb7e05c | 30 | #include "wx/wxcrtvararg.h" |
c0472c7c VZ |
31 | #if USE_PUTENV |
32 | #include "wx/module.h" | |
33 | #include "wx/hashmap.h" | |
34 | #endif | |
7520f3da | 35 | #endif |
518b5d2f | 36 | |
46446cc2 | 37 | #include "wx/apptrait.h" |
518b5d2f | 38 | |
518b5d2f | 39 | #include "wx/process.h" |
bdc72a22 | 40 | #include "wx/thread.h" |
518b5d2f | 41 | |
fae71206 VZ |
42 | #include "wx/cmdline.h" |
43 | ||
80d6dc0a | 44 | #include "wx/wfstream.h" |
8b33ae2d | 45 | |
33343395 | 46 | #include "wx/private/selectdispatcher.h" |
1d043598 | 47 | #include "wx/private/fdiodispatcher.h" |
e2478fde | 48 | #include "wx/unix/execute.h" |
17a1ebd1 VZ |
49 | #include "wx/unix/private.h" |
50 | ||
46c7e1a1 VS |
51 | #ifdef wxHAS_GENERIC_PROCESS_CALLBACK |
52 | #include "wx/private/fdiodispatcher.h" | |
53 | #endif | |
54 | ||
17a1ebd1 | 55 | #include <pwd.h> |
bc855d09 | 56 | #include <sys/wait.h> // waitpid() |
e2478fde | 57 | |
bc023abb MW |
58 | #ifdef HAVE_SYS_SELECT_H |
59 | # include <sys/select.h> | |
60 | #endif | |
61 | ||
1f3b2af0 VS |
62 | #define HAS_PIPE_INPUT_STREAM (wxUSE_STREAMS && wxUSE_FILE) |
63 | ||
64 | #if HAS_PIPE_INPUT_STREAM | |
2887179b VZ |
65 | |
66 | // define this to let wxexec.cpp know that we know what we're doing | |
67 | #define _WX_USED_BY_WXEXECUTE_ | |
68 | #include "../common/execcmn.cpp" | |
69 | ||
1f3b2af0 | 70 | #endif // HAS_PIPE_INPUT_STREAM |
2887179b | 71 | |
74c719ed VZ |
72 | #if defined(__MWERKS__) && defined(__MACH__) |
73 | #ifndef WXWIN_OS_DESCRIPTION | |
74 | #define WXWIN_OS_DESCRIPTION "MacOS X" | |
75 | #endif | |
76 | #ifndef HAVE_NANOSLEEP | |
77 | #define HAVE_NANOSLEEP | |
78 | #endif | |
79 | #ifndef HAVE_UNAME | |
80 | #define HAVE_UNAME | |
81 | #endif | |
82 | ||
83 | // our configure test believes we can use sigaction() if the function is | |
84 | // available but Metrowekrs with MSL run-time does have the function but | |
85 | // doesn't have sigaction struct so finally we can't use it... | |
86 | #ifdef __MSL__ | |
87 | #undef wxUSE_ON_FATAL_EXCEPTION | |
88 | #define wxUSE_ON_FATAL_EXCEPTION 0 | |
89 | #endif | |
8d4f85ce SC |
90 | #endif |
91 | ||
85da04e9 VZ |
92 | // not only the statfs syscall is called differently depending on platform, but |
93 | // one of its incarnations, statvfs(), takes different arguments under | |
94 | // different platforms and even different versions of the same system (Solaris | |
95 | // 7 and 8): if you want to test for this, don't forget that the problems only | |
96 | // appear if the large files support is enabled | |
eadd7bd2 | 97 | #ifdef HAVE_STATFS |
85da04e9 VZ |
98 | #ifdef __BSD__ |
99 | #include <sys/param.h> | |
100 | #include <sys/mount.h> | |
101 | #else // !__BSD__ | |
102 | #include <sys/vfs.h> | |
103 | #endif // __BSD__/!__BSD__ | |
104 | ||
105 | #define wxStatfs statfs | |
84ae7ca4 VZ |
106 | |
107 | #ifndef HAVE_STATFS_DECL | |
108 | // some systems lack statfs() prototype in the system headers (AIX 4) | |
109 | extern "C" int statfs(const char *path, struct statfs *buf); | |
110 | #endif | |
eadd7bd2 VZ |
111 | #endif // HAVE_STATFS |
112 | ||
9952adac VZ |
113 | #ifdef HAVE_STATVFS |
114 | #include <sys/statvfs.h> | |
115 | ||
85da04e9 VZ |
116 | #define wxStatfs statvfs |
117 | #endif // HAVE_STATVFS | |
118 | ||
119 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) | |
120 | // WX_STATFS_T is detected by configure | |
121 | #define wxStatfs_t WX_STATFS_T | |
122 | #endif | |
9952adac | 123 | |
f6bcfd97 BP |
124 | // SGI signal.h defines signal handler arguments differently depending on |
125 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
126 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
127 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
128 | #endif // SGI hack | |
129 | ||
518b5d2f VZ |
130 | #include <stdarg.h> |
131 | #include <dirent.h> | |
132 | #include <string.h> | |
133 | #include <sys/stat.h> | |
134 | #include <sys/types.h> | |
518b5d2f | 135 | #include <sys/wait.h> |
e2478fde | 136 | #include <unistd.h> |
518b5d2f VZ |
137 | #include <errno.h> |
138 | #include <netdb.h> | |
139 | #include <signal.h> | |
140 | #include <fcntl.h> // for O_WRONLY and friends | |
141 | #include <time.h> // nanosleep() and/or usleep() | |
fad866f4 | 142 | #include <ctype.h> // isspace() |
b12915c1 | 143 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 144 | |
0fcdf6dc | 145 | #ifdef HAVE_UNAME |
518b5d2f VZ |
146 | #include <sys/utsname.h> // for uname() |
147 | #endif // HAVE_UNAME | |
148 | ||
9ad34f61 VZ |
149 | // Used by wxGetFreeMemory(). |
150 | #ifdef __SGI__ | |
151 | #include <sys/sysmp.h> | |
152 | #include <sys/sysinfo.h> // for SAGET and MINFO structures | |
153 | #endif | |
154 | ||
518b5d2f VZ |
155 | // ---------------------------------------------------------------------------- |
156 | // conditional compilation | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
159 | // many versions of Unices have this function, but it is not defined in system | |
160 | // headers - please add your system here if it is the case for your OS. | |
161 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b | 162 | #if !defined(HAVE_USLEEP) && \ |
d67fee71 | 163 | ((defined(__SUN__) && !defined(__SunOs_5_6) && \ |
518b5d2f | 164 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
d67fee71 | 165 | defined(__osf__) || defined(__EMX__)) |
518b5d2f VZ |
166 | extern "C" |
167 | { | |
1a5e269b VZ |
168 | #ifdef __EMX__ |
169 | /* I copied this from the XFree86 diffs. AV. */ | |
170 | #define INCL_DOSPROCESS | |
171 | #include <os2.h> | |
172 | inline void usleep(unsigned long delay) | |
173 | { | |
174 | DosSleep(delay ? (delay/1000l) : 1l); | |
175 | } | |
176 | #else // Unix | |
1363811b | 177 | int usleep(unsigned int usec); |
1a5e269b | 178 | #endif // __EMX__/Unix |
518b5d2f | 179 | }; |
bdc72a22 VZ |
180 | |
181 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
182 | #endif // Unices without usleep() |
183 | ||
518b5d2f VZ |
184 | // ============================================================================ |
185 | // implementation | |
186 | // ============================================================================ | |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // sleeping | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | void wxSleep(int nSecs) | |
193 | { | |
194 | sleep(nSecs); | |
195 | } | |
196 | ||
66cd9d7f | 197 | void wxMicroSleep(unsigned long microseconds) |
518b5d2f | 198 | { |
b12915c1 | 199 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 200 | timespec tmReq; |
66cd9d7f VZ |
201 | tmReq.tv_sec = (time_t)(microseconds / 1000000); |
202 | tmReq.tv_nsec = (microseconds % 1000000) * 1000; | |
518b5d2f VZ |
203 | |
204 | // we're not interested in remaining time nor in return value | |
d3b9f782 | 205 | (void)nanosleep(&tmReq, NULL); |
b12915c1 | 206 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
207 | // uncomment this if you feel brave or if you are sure that your version |
208 | // of Solaris has a safe usleep() function but please notice that usleep() | |
209 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
210 | // documented as MT-Safe | |
ea18eed9 | 211 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
212 | #error "usleep() cannot be used in MT programs under Solaris." |
213 | #endif // Sun | |
214 | ||
66cd9d7f | 215 | usleep(microseconds); |
b12915c1 VZ |
216 | #elif defined(HAVE_SLEEP) |
217 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
66cd9d7f | 218 | sleep(microseconds * 1000000); |
518b5d2f | 219 | #else // !sleep function |
66cd9d7f | 220 | #error "usleep() or nanosleep() function required for wxMicroSleep" |
518b5d2f VZ |
221 | #endif // sleep function |
222 | } | |
223 | ||
66cd9d7f VZ |
224 | void wxMilliSleep(unsigned long milliseconds) |
225 | { | |
226 | wxMicroSleep(milliseconds*1000); | |
227 | } | |
228 | ||
518b5d2f VZ |
229 | // ---------------------------------------------------------------------------- |
230 | // process management | |
231 | // ---------------------------------------------------------------------------- | |
232 | ||
e0f6b731 | 233 | int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags) |
518b5d2f | 234 | { |
e0f6b731 | 235 | int err = kill((pid_t) (flags & wxKILL_CHILDREN) ? -pid : pid, (int)sig); |
50567b69 VZ |
236 | if ( rc ) |
237 | { | |
f0bce4d4 | 238 | switch ( err ? errno : 0 ) |
50567b69 VZ |
239 | { |
240 | case 0: | |
241 | *rc = wxKILL_OK; | |
242 | break; | |
243 | ||
244 | case EINVAL: | |
245 | *rc = wxKILL_BAD_SIGNAL; | |
246 | break; | |
247 | ||
248 | case EPERM: | |
249 | *rc = wxKILL_ACCESS_DENIED; | |
250 | break; | |
251 | ||
252 | case ESRCH: | |
253 | *rc = wxKILL_NO_PROCESS; | |
254 | break; | |
255 | ||
256 | default: | |
257 | // this goes against Unix98 docs so log it | |
258 | wxLogDebug(_T("unexpected kill(2) return value %d"), err); | |
259 | ||
260 | // something else... | |
261 | *rc = wxKILL_ERROR; | |
262 | } | |
263 | } | |
264 | ||
265 | return err; | |
518b5d2f VZ |
266 | } |
267 | ||
05718a98 | 268 | // Shutdown or reboot the PC |
c1baf4b5 | 269 | bool wxShutdown(int flags) |
518b5d2f | 270 | { |
c1baf4b5 VZ |
271 | flags &= ~wxSHUTDOWN_FORCE; |
272 | ||
05718a98 | 273 | wxChar level; |
c1baf4b5 | 274 | switch ( flags ) |
05718a98 VZ |
275 | { |
276 | case wxSHUTDOWN_POWEROFF: | |
277 | level = _T('0'); | |
278 | break; | |
647b8e37 | 279 | |
05718a98 VZ |
280 | case wxSHUTDOWN_REBOOT: |
281 | level = _T('6'); | |
282 | break; | |
518b5d2f | 283 | |
c1baf4b5 VZ |
284 | case wxSHUTDOWN_LOGOFF: |
285 | // TODO: use dcop to log off? | |
286 | return false; | |
287 | ||
05718a98 VZ |
288 | default: |
289 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
290 | return false; | |
291 | } | |
0ed9a934 | 292 | |
c1baf4b5 | 293 | return system(wxString::Format("init %c", level).mb_str()) == 0; |
05718a98 | 294 | } |
0ed9a934 | 295 | |
05718a98 VZ |
296 | // ---------------------------------------------------------------------------- |
297 | // wxStream classes to support IO redirection in wxExecute | |
298 | // ---------------------------------------------------------------------------- | |
0ed9a934 | 299 | |
05718a98 | 300 | #if HAS_PIPE_INPUT_STREAM |
0ed9a934 | 301 | |
05718a98 VZ |
302 | bool wxPipeInputStream::CanRead() const |
303 | { | |
304 | if ( m_lasterror == wxSTREAM_EOF ) | |
305 | return false; | |
0ed9a934 | 306 | |
05718a98 VZ |
307 | // check if there is any input available |
308 | struct timeval tv; | |
309 | tv.tv_sec = 0; | |
310 | tv.tv_usec = 0; | |
0ed9a934 | 311 | |
05718a98 | 312 | const int fd = m_file->fd(); |
0ed9a934 | 313 | |
05718a98 | 314 | fd_set readfds; |
0ed9a934 | 315 | |
05718a98 VZ |
316 | wxFD_ZERO(&readfds); |
317 | wxFD_SET(fd, &readfds); | |
0ed9a934 | 318 | |
05718a98 VZ |
319 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) |
320 | { | |
321 | case -1: | |
322 | wxLogSysError(_("Impossible to get child process input")); | |
323 | // fall through | |
62705a27 | 324 | |
05718a98 VZ |
325 | case 0: |
326 | return false; | |
518b5d2f | 327 | |
05718a98 VZ |
328 | default: |
329 | wxFAIL_MSG(_T("unexpected select() return value")); | |
330 | // still fall through | |
518b5d2f | 331 | |
05718a98 VZ |
332 | case 1: |
333 | // input available -- or maybe not, as select() returns 1 when a | |
334 | // read() will complete without delay, but it could still not read | |
335 | // anything | |
336 | return !Eof(); | |
337 | } | |
518b5d2f VZ |
338 | } |
339 | ||
05718a98 VZ |
340 | #endif // HAS_PIPE_INPUT_STREAM |
341 | ||
2c8e4738 VZ |
342 | // ---------------------------------------------------------------------------- |
343 | // wxShell | |
344 | // ---------------------------------------------------------------------------- | |
345 | ||
346 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
347 | { |
348 | wxString cmd; | |
cd6ce4a9 | 349 | if ( !command ) |
2c8e4738 VZ |
350 | { |
351 | // just an interactive shell | |
cd6ce4a9 | 352 | cmd = _T("xterm"); |
2c8e4738 | 353 | } |
518b5d2f | 354 | else |
2c8e4738 VZ |
355 | { |
356 | // execute command in a shell | |
357 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
358 | } | |
359 | ||
360 | return cmd; | |
361 | } | |
362 | ||
363 | bool wxShell(const wxString& command) | |
364 | { | |
fbf456aa | 365 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
366 | } |
367 | ||
368 | bool wxShell(const wxString& command, wxArrayString& output) | |
369 | { | |
9d8aca48 | 370 | wxCHECK_MSG( !command.empty(), false, _T("can't exec shell non interactively") ); |
518b5d2f | 371 | |
2c8e4738 | 372 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
373 | } |
374 | ||
05718a98 | 375 | namespace |
f6ba47d9 | 376 | { |
05718a98 VZ |
377 | |
378 | // helper class for storing arguments as char** array suitable for passing to | |
379 | // execvp(), whatever form they were passed to us | |
380 | class ArgsArray | |
381 | { | |
382 | public: | |
383 | ArgsArray(const wxArrayString& args) | |
f6ba47d9 | 384 | { |
05718a98 | 385 | Init(args.size()); |
f6ba47d9 | 386 | |
05718a98 VZ |
387 | for ( int i = 0; i < m_argc; i++ ) |
388 | { | |
389 | m_argv[i] = wxStrdup(args[i]); | |
390 | } | |
391 | } | |
f6ba47d9 | 392 | |
d7ef641d | 393 | #if wxUSE_UNICODE |
05718a98 VZ |
394 | ArgsArray(wchar_t **wargv) |
395 | { | |
396 | int argc = 0; | |
397 | while ( *wargv++ ) | |
398 | argc++; | |
399 | ||
400 | Init(argc); | |
401 | ||
402 | for ( int i = 0; i < m_argc; i++ ) | |
403 | { | |
404 | m_argv[i] = wxSafeConvertWX2MB(wargv[i]).release(); | |
405 | } | |
f6ba47d9 | 406 | } |
d7ef641d | 407 | #endif // wxUSE_UNICODE |
f6ba47d9 | 408 | |
05718a98 VZ |
409 | ~ArgsArray() |
410 | { | |
411 | for ( int i = 0; i < m_argc; i++ ) | |
412 | { | |
413 | free(m_argv[i]); | |
414 | } | |
415 | ||
416 | delete [] m_argv; | |
417 | } | |
418 | ||
419 | operator char**() const { return m_argv; } | |
420 | ||
421 | private: | |
422 | void Init(int argc) | |
423 | { | |
424 | m_argc = argc; | |
425 | m_argv = new char *[m_argc + 1]; | |
426 | m_argv[m_argc] = NULL; | |
427 | } | |
428 | ||
429 | int m_argc; | |
430 | char **m_argv; | |
431 | ||
c0c133e1 | 432 | wxDECLARE_NO_COPY_CLASS(ArgsArray); |
05718a98 VZ |
433 | }; |
434 | ||
435 | } // anonymous namespace | |
f6ba47d9 | 436 | |
cd6ce4a9 | 437 | // ---------------------------------------------------------------------------- |
05718a98 | 438 | // wxExecute implementations |
cd6ce4a9 | 439 | // ---------------------------------------------------------------------------- |
6dc6fda6 | 440 | |
05718a98 VZ |
441 | #if defined(__DARWIN__) |
442 | bool wxMacLaunch(char **argv); | |
443 | #endif | |
1e6feb95 | 444 | |
05718a98 | 445 | long wxExecute(const wxString& command, int flags, wxProcess *process) |
8b33ae2d | 446 | { |
fae71206 VZ |
447 | ArgsArray argv(wxCmdLineParser::ConvertStringToArgs(command, |
448 | wxCMD_LINE_SPLIT_UNIX)); | |
05718a98 | 449 | |
05718a98 | 450 | return wxExecute(argv, flags, process); |
8b33ae2d GL |
451 | } |
452 | ||
d7ef641d VZ |
453 | #if wxUSE_UNICODE |
454 | ||
05718a98 VZ |
455 | long wxExecute(wchar_t **wargv, int flags, wxProcess *process) |
456 | { | |
457 | ArgsArray argv(wargv); | |
1e6feb95 | 458 | |
05718a98 VZ |
459 | return wxExecute(argv, flags, process); |
460 | } | |
79066131 | 461 | |
d7ef641d VZ |
462 | #endif // wxUSE_UNICODE |
463 | ||
05718a98 VZ |
464 | // wxExecute: the real worker function |
465 | long wxExecute(char **argv, int flags, wxProcess *process) | |
518b5d2f | 466 | { |
f6bcfd97 | 467 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
468 | // case we return 0 which is never a valid PID |
469 | // | |
470 | // we define this as a macro, not a variable, to avoid compiler warnings | |
471 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 472 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 473 | |
accb3257 | 474 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 475 | |
05718a98 VZ |
476 | #if wxUSE_THREADS |
477 | // fork() doesn't mix well with POSIX threads: on many systems the program | |
478 | // deadlocks or crashes for some reason. Probably our code is buggy and | |
479 | // doesn't do something which must be done to allow this to work, but I | |
480 | // don't know what yet, so for now just warn the user (this is the least we | |
481 | // can do) about it | |
482 | wxASSERT_MSG( wxThread::IsMain(), | |
483 | _T("wxExecute() can be called only from the main thread") ); | |
484 | #endif // wxUSE_THREADS | |
05079acc | 485 | |
052e5ec5 | 486 | #if defined(__WXCOCOA__) || ( defined(__WXOSX_MAC__) && wxOSX_USE_COCOA_OR_CARBON ) |
05718a98 VZ |
487 | // wxMacLaunch() only executes app bundles and only does it asynchronously. |
488 | // It returns false if the target is not an app bundle, thus falling | |
489 | // through to the regular code for non app bundles. | |
490 | if ( !(flags & wxEXEC_SYNC) && wxMacLaunch(argv) ) | |
e90c1d2a | 491 | { |
05718a98 VZ |
492 | // we don't have any PID to return so just make up something non null |
493 | return -1; | |
05079acc | 494 | } |
05718a98 VZ |
495 | #endif // __DARWIN__ |
496 | ||
497 | ||
498 | // this struct contains all information which we use for housekeeping | |
e2478fde VZ |
499 | wxExecuteData execData; |
500 | execData.flags = flags; | |
501 | execData.process = process; | |
502 | ||
518b5d2f | 503 | // create pipes |
3bbd09c3 | 504 | if ( !execData.pipeEndProcDetect.Create() ) |
518b5d2f | 505 | { |
cd6ce4a9 | 506 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a | 507 | |
accb3257 | 508 | return ERROR_RETURN_CODE; |
518b5d2f VZ |
509 | } |
510 | ||
f6bcfd97 | 511 | // pipes for inter process communication |
b477f956 VZ |
512 | wxPipe pipeIn, // stdin |
513 | pipeOut, // stdout | |
514 | pipeErr; // stderr | |
cd6ce4a9 VZ |
515 | |
516 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 517 | { |
b477f956 | 518 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 519 | { |
cd6ce4a9 | 520 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d | 521 | |
accb3257 | 522 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
523 | } |
524 | } | |
8b33ae2d | 525 | |
518b5d2f | 526 | // fork the process |
ef5f8ab6 VZ |
527 | // |
528 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
529 | // reason under Solaris (and maybe others, although not under Linux) | |
b2ddee86 JJ |
530 | // But on OpenVMS we do not have fork so we have to use vfork and |
531 | // cross our fingers that it works. | |
532 | #ifdef __VMS | |
533 | pid_t pid = vfork(); | |
534 | #else | |
535 | pid_t pid = fork(); | |
536 | #endif | |
537 | if ( pid == -1 ) // error? | |
518b5d2f VZ |
538 | { |
539 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a | 540 | |
accb3257 | 541 | return ERROR_RETURN_CODE; |
518b5d2f | 542 | } |
cd6ce4a9 | 543 | else if ( pid == 0 ) // we're in child |
518b5d2f | 544 | { |
5ddfa074 VZ |
545 | // NB: we used to close all the unused descriptors of the child here |
546 | // but this broke some programs which relied on e.g. FD 1 being | |
547 | // always opened so don't do it any more, after all there doesn't | |
548 | // seem to be any real problem with keeping them opened | |
e1082c9f | 549 | |
e8e776aa | 550 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
551 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
552 | { | |
553 | // Set process group to child process' pid. Then killing -pid | |
554 | // of the parent will kill the process and all of its children. | |
555 | setsid(); | |
518b5d2f | 556 | } |
0c9c4401 VZ |
557 | #endif // !__VMS |
558 | ||
0c9c4401 | 559 | // reading side can be safely closed but we should keep the write one |
3bbd09c3 VZ |
560 | // opened, it will be only closed when the process terminates resulting |
561 | // in a read notification to the parent | |
562 | execData.pipeEndProcDetect.Detach(wxPipe::Write); | |
563 | execData.pipeEndProcDetect.Close(); | |
518b5d2f | 564 | |
80d6dc0a | 565 | // redirect stdin, stdout and stderr |
b477f956 | 566 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 567 | { |
b477f956 VZ |
568 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
569 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
570 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 571 | { |
f6bcfd97 | 572 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 573 | } |
518b5d2f | 574 | |
b477f956 VZ |
575 | pipeIn.Close(); |
576 | pipeOut.Close(); | |
577 | pipeErr.Close(); | |
cd6ce4a9 | 578 | } |
518b5d2f | 579 | |
05718a98 | 580 | execvp(*argv, argv); |
17a1ebd1 | 581 | |
d264d709 | 582 | fprintf(stderr, "execvp("); |
05718a98 VZ |
583 | for ( char **a = argv; *a; a++ ) |
584 | fprintf(stderr, "%s%s", a == argv ? "" : ", ", *a); | |
d264d709 VZ |
585 | fprintf(stderr, ") failed with error %d!\n", errno); |
586 | ||
518b5d2f | 587 | // there is no return after successful exec() |
518b5d2f | 588 | _exit(-1); |
1d8dd65e VZ |
589 | |
590 | // some compilers complain about missing return - of course, they | |
591 | // should know that exit() doesn't return but what else can we do if | |
592 | // they don't? | |
b477f956 VZ |
593 | // |
594 | // and, sure enough, other compilers complain about unreachable code | |
595 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
596 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
597 | return 0; | |
598 | #endif | |
518b5d2f | 599 | } |
cd6ce4a9 | 600 | else // we're in parent |
518b5d2f | 601 | { |
7764f973 VZ |
602 | // save it for WaitForChild() use |
603 | execData.pid = pid; | |
ca5016d4 FM |
604 | if (execData.process) |
605 | execData.process->SetPid(pid); // and also in the wxProcess | |
7764f973 | 606 | |
80d6dc0a VZ |
607 | // prepare for IO redirection |
608 | ||
1f3b2af0 | 609 | #if HAS_PIPE_INPUT_STREAM |
80d6dc0a VZ |
610 | // the input buffer bufOut is connected to stdout, this is why it is |
611 | // called bufOut and not bufIn | |
612 | wxStreamTempInputBuffer bufOut, | |
613 | bufErr; | |
0e300ddd | 614 | |
cd6ce4a9 VZ |
615 | if ( process && process->IsRedirected() ) |
616 | { | |
80d6dc0a VZ |
617 | wxOutputStream *inStream = |
618 | new wxFileOutputStream(pipeIn.Detach(wxPipe::Write)); | |
b477f956 | 619 | |
33343395 VZ |
620 | const int fdOut = pipeOut.Detach(wxPipe::Read); |
621 | wxPipeInputStream *outStream = new wxPipeInputStream(fdOut); | |
b477f956 | 622 | |
33343395 VZ |
623 | const int fdErr = pipeErr.Detach(wxPipe::Read); |
624 | wxPipeInputStream *errStream = new wxPipeInputStream(fdErr); | |
f6bcfd97 | 625 | |
80d6dc0a | 626 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 627 | |
80d6dc0a | 628 | bufOut.Init(outStream); |
b477f956 | 629 | bufErr.Init(errStream); |
e2478fde VZ |
630 | |
631 | execData.bufOut = &bufOut; | |
632 | execData.bufErr = &bufErr; | |
33343395 VZ |
633 | |
634 | execData.fdOut = fdOut; | |
635 | execData.fdErr = fdErr; | |
b477f956 | 636 | } |
33343395 | 637 | #endif // HAS_PIPE_INPUT_STREAM |
1e6feb95 | 638 | |
b477f956 VZ |
639 | if ( pipeIn.IsOk() ) |
640 | { | |
641 | pipeIn.Close(); | |
642 | pipeOut.Close(); | |
643 | pipeErr.Close(); | |
cd6ce4a9 VZ |
644 | } |
645 | ||
05718a98 VZ |
646 | // we want this function to work even if there is no wxApp so ensure |
647 | // that we have a valid traits pointer | |
648 | wxConsoleAppTraits traitsConsole; | |
649 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
650 | if ( !traits ) | |
651 | traits = &traitsConsole; | |
652 | ||
e2478fde | 653 | return traits->WaitForChild(execData); |
518b5d2f | 654 | } |
79656e30 | 655 | |
17a1ebd1 | 656 | #if !defined(__VMS) && !defined(__INTEL_COMPILER) |
79656e30 | 657 | return ERROR_RETURN_CODE; |
ef0ed19e | 658 | #endif |
17a1ebd1 | 659 | } |
518b5d2f | 660 | |
accb3257 | 661 | #undef ERROR_RETURN_CODE |
f6bcfd97 | 662 | |
518b5d2f VZ |
663 | // ---------------------------------------------------------------------------- |
664 | // file and directory functions | |
665 | // ---------------------------------------------------------------------------- | |
666 | ||
05079acc | 667 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f | 668 | { |
14d63513 | 669 | *home = wxGetUserHome(); |
79066131 | 670 | wxString tmp; |
8ea92b4d | 671 | if ( home->empty() ) |
223d09f6 | 672 | *home = wxT("/"); |
181cbcf4 | 673 | #ifdef __VMS |
79066131 VZ |
674 | tmp = *home; |
675 | if ( tmp.Last() != wxT(']')) | |
676 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 677 | #endif |
518b5d2f VZ |
678 | return home->c_str(); |
679 | } | |
680 | ||
14d63513 | 681 | wxString wxGetUserHome( const wxString &user ) |
518b5d2f VZ |
682 | { |
683 | struct passwd *who = (struct passwd *) NULL; | |
684 | ||
0fb67cd1 | 685 | if ( !user ) |
518b5d2f | 686 | { |
e90c1d2a | 687 | wxChar *ptr; |
518b5d2f | 688 | |
223d09f6 | 689 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f VZ |
690 | { |
691 | return ptr; | |
692 | } | |
14d63513 VZ |
693 | |
694 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || | |
695 | (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) | |
518b5d2f | 696 | { |
69c928ef | 697 | who = getpwnam(wxSafeConvertWX2MB(ptr)); |
518b5d2f VZ |
698 | } |
699 | ||
14d63513 VZ |
700 | // make sure the user exists! |
701 | if ( !who ) | |
518b5d2f VZ |
702 | { |
703 | who = getpwuid(getuid()); | |
704 | } | |
705 | } | |
706 | else | |
707 | { | |
05079acc | 708 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
709 | } |
710 | ||
69c928ef | 711 | return wxSafeConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
712 | } |
713 | ||
714 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 715 | // network and user id routines |
518b5d2f VZ |
716 | // ---------------------------------------------------------------------------- |
717 | ||
8bb6b2c0 VZ |
718 | // private utility function which returns output of the given command, removing |
719 | // the trailing newline | |
720 | static wxString wxGetCommandOutput(const wxString &cmd) | |
721 | { | |
722 | FILE *f = popen(cmd.ToAscii(), "r"); | |
723 | if ( !f ) | |
724 | { | |
725 | wxLogSysError(_T("Executing \"%s\" failed"), cmd.c_str()); | |
726 | return wxEmptyString; | |
727 | } | |
728 | ||
729 | wxString s; | |
730 | char buf[256]; | |
731 | while ( !feof(f) ) | |
732 | { | |
733 | if ( !fgets(buf, sizeof(buf), f) ) | |
734 | break; | |
735 | ||
736 | s += wxString::FromAscii(buf); | |
737 | } | |
738 | ||
739 | pclose(f); | |
740 | ||
741 | if ( !s.empty() && s.Last() == _T('\n') ) | |
742 | s.RemoveLast(); | |
743 | ||
744 | return s; | |
745 | } | |
746 | ||
0fb67cd1 VZ |
747 | // retrieve either the hostname or FQDN depending on platform (caller must |
748 | // check whether it's one or the other, this is why this function is for | |
749 | // private use only) | |
05079acc | 750 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 751 | { |
9d8aca48 | 752 | wxCHECK_MSG( buf, false, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 753 | |
223d09f6 | 754 | *buf = wxT('\0'); |
518b5d2f VZ |
755 | |
756 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
757 | #if defined(HAVE_UNAME) | |
cc743a6f | 758 | struct utsname uts; |
518b5d2f VZ |
759 | bool ok = uname(&uts) != -1; |
760 | if ( ok ) | |
761 | { | |
e408bf52 | 762 | wxStrlcpy(buf, wxSafeConvertMB2WX(uts.nodename), sz); |
518b5d2f VZ |
763 | } |
764 | #elif defined(HAVE_GETHOSTNAME) | |
1870f50b RD |
765 | char cbuf[sz]; |
766 | bool ok = gethostname(cbuf, sz) != -1; | |
767 | if ( ok ) | |
768 | { | |
e408bf52 | 769 | wxStrlcpy(buf, wxSafeConvertMB2WX(cbuf), sz); |
1870f50b | 770 | } |
0fb67cd1 | 771 | #else // no uname, no gethostname |
223d09f6 | 772 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f | 773 | |
9d8aca48 | 774 | bool ok = false; |
0fb67cd1 | 775 | #endif // uname/gethostname |
518b5d2f VZ |
776 | |
777 | if ( !ok ) | |
778 | { | |
779 | wxLogSysError(_("Cannot get the hostname")); | |
780 | } | |
781 | ||
782 | return ok; | |
783 | } | |
784 | ||
05079acc | 785 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
786 | { |
787 | bool ok = wxGetHostNameInternal(buf, sz); | |
788 | ||
789 | if ( ok ) | |
790 | { | |
791 | // BSD systems return the FQDN, we only want the hostname, so extract | |
792 | // it (we consider that dots are domain separators) | |
223d09f6 | 793 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
794 | if ( dot ) |
795 | { | |
796 | // nuke it | |
223d09f6 | 797 | *dot = wxT('\0'); |
0fb67cd1 VZ |
798 | } |
799 | } | |
800 | ||
801 | return ok; | |
802 | } | |
803 | ||
05079acc | 804 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
805 | { |
806 | bool ok = wxGetHostNameInternal(buf, sz); | |
807 | ||
808 | if ( ok ) | |
809 | { | |
223d09f6 | 810 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 811 | { |
69c928ef | 812 | struct hostent *host = gethostbyname(wxSafeConvertWX2MB(buf)); |
0fb67cd1 VZ |
813 | if ( !host ) |
814 | { | |
815 | wxLogSysError(_("Cannot get the official hostname")); | |
816 | ||
9d8aca48 | 817 | ok = false; |
0fb67cd1 VZ |
818 | } |
819 | else | |
820 | { | |
821 | // the canonical name | |
e408bf52 | 822 | wxStrlcpy(buf, wxSafeConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
823 | } |
824 | } | |
825 | //else: it's already a FQDN (BSD behaves this way) | |
826 | } | |
827 | ||
828 | return ok; | |
829 | } | |
830 | ||
05079acc | 831 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
832 | { |
833 | struct passwd *who; | |
834 | ||
223d09f6 | 835 | *buf = wxT('\0'); |
518b5d2f VZ |
836 | if ((who = getpwuid(getuid ())) != NULL) |
837 | { | |
e408bf52 | 838 | wxStrlcpy (buf, wxSafeConvertMB2WX(who->pw_name), sz); |
9d8aca48 | 839 | return true; |
518b5d2f VZ |
840 | } |
841 | ||
9d8aca48 | 842 | return false; |
518b5d2f VZ |
843 | } |
844 | ||
05079acc | 845 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f | 846 | { |
69c928ef | 847 | #ifdef HAVE_PW_GECOS |
518b5d2f | 848 | struct passwd *who; |
518b5d2f | 849 | |
223d09f6 | 850 | *buf = wxT('\0'); |
b12915c1 VZ |
851 | if ((who = getpwuid (getuid ())) != NULL) |
852 | { | |
b12915c1 | 853 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
854 | if (comma) |
855 | *comma = '\0'; // cut off non-name comment fields | |
e408bf52 | 856 | wxStrlcpy(buf, wxSafeConvertMB2WX(who->pw_gecos), sz); |
9d8aca48 | 857 | return true; |
518b5d2f VZ |
858 | } |
859 | ||
9d8aca48 | 860 | return false; |
69c928ef VZ |
861 | #else // !HAVE_PW_GECOS |
862 | return wxGetUserId(buf, sz); | |
863 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS | |
518b5d2f VZ |
864 | } |
865 | ||
c655557f VZ |
866 | bool wxIsPlatform64Bit() |
867 | { | |
2f6aa043 VZ |
868 | const wxString machine = wxGetCommandOutput(wxT("uname -m")); |
869 | ||
870 | // the test for "64" is obviously not 100% reliable but seems to work fine | |
871 | // in practice | |
872 | return machine.Contains(wxT("64")) || | |
873 | machine.Contains(wxT("alpha")); | |
c655557f VZ |
874 | } |
875 | ||
50a2e26f | 876 | // these functions are in src/osx/utilsexc_base.cpp for wxMac |
c655557f VZ |
877 | #ifndef __WXMAC__ |
878 | ||
8bb6b2c0 VZ |
879 | wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) |
880 | { | |
881 | // get OS version | |
882 | int major, minor; | |
883 | wxString release = wxGetCommandOutput(wxT("uname -r")); | |
86501081 VS |
884 | if ( release.empty() || |
885 | wxSscanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 ) | |
8bb6b2c0 | 886 | { |
e1379e29 | 887 | // failed to get version string or unrecognized format |
8bb6b2c0 VZ |
888 | major = |
889 | minor = -1; | |
890 | } | |
891 | ||
892 | if ( verMaj ) | |
893 | *verMaj = major; | |
894 | if ( verMin ) | |
895 | *verMin = minor; | |
896 | ||
897 | // try to understand which OS are we running | |
898 | wxString kernel = wxGetCommandOutput(wxT("uname -s")); | |
899 | if ( kernel.empty() ) | |
900 | kernel = wxGetCommandOutput(wxT("uname -o")); | |
901 | ||
902 | if ( kernel.empty() ) | |
903 | return wxOS_UNKNOWN; | |
904 | ||
905 | return wxPlatformInfo::GetOperatingSystemId(kernel); | |
906 | } | |
907 | ||
bdc72a22 VZ |
908 | wxString wxGetOsDescription() |
909 | { | |
8bb6b2c0 | 910 | return wxGetCommandOutput(wxT("uname -s -r -m")); |
bdc72a22 VZ |
911 | } |
912 | ||
e2478fde | 913 | #endif // !__WXMAC__ |
bd3277fe | 914 | |
c1cb4153 VZ |
915 | unsigned long wxGetProcessId() |
916 | { | |
917 | return (unsigned long)getpid(); | |
918 | } | |
919 | ||
9d8aca48 | 920 | wxMemorySize wxGetFreeMemory() |
bd3277fe VZ |
921 | { |
922 | #if defined(__LINUX__) | |
923 | // get it from /proc/meminfo | |
924 | FILE *fp = fopen("/proc/meminfo", "r"); | |
925 | if ( fp ) | |
926 | { | |
927 | long memFree = -1; | |
928 | ||
929 | char buf[1024]; | |
930 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
931 | { | |
7c28d921 VZ |
932 | // /proc/meminfo changed its format in kernel 2.6 |
933 | if ( wxPlatformInfo().CheckOSVersion(2, 6) ) | |
934 | { | |
935 | unsigned long cached, buffers; | |
936 | sscanf(buf, "MemFree: %ld", &memFree); | |
937 | ||
938 | fgets(buf, WXSIZEOF(buf), fp); | |
939 | sscanf(buf, "Buffers: %lu", &buffers); | |
940 | ||
941 | fgets(buf, WXSIZEOF(buf), fp); | |
942 | sscanf(buf, "Cached: %lu", &cached); | |
943 | ||
944 | // add to "MemFree" also the "Buffers" and "Cached" values as | |
945 | // free(1) does as otherwise the value never makes sense: for | |
946 | // kernel 2.6 it's always almost 0 | |
947 | memFree += buffers + cached; | |
948 | ||
949 | // values here are always expressed in kB and we want bytes | |
950 | memFree *= 1024; | |
951 | } | |
952 | else // Linux 2.4 (or < 2.6, anyhow) | |
953 | { | |
954 | long memTotal, memUsed; | |
955 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
956 | } | |
bd3277fe VZ |
957 | } |
958 | ||
959 | fclose(fp); | |
960 | ||
9d8aca48 | 961 | return (wxMemorySize)memFree; |
bd3277fe | 962 | } |
9ad34f61 VZ |
963 | #elif defined(__SGI__) |
964 | struct rminfo realmem; | |
965 | if ( sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0 ) | |
966 | return ((wxMemorySize)realmem.physmem * sysconf(_SC_PAGESIZE)); | |
40f00746 VZ |
967 | #elif defined(_SC_AVPHYS_PAGES) |
968 | return ((wxMemorySize)sysconf(_SC_AVPHYS_PAGES))*sysconf(_SC_PAGESIZE); | |
bd3277fe VZ |
969 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably |
970 | #endif | |
971 | ||
972 | // can't find it out | |
973 | return -1; | |
974 | } | |
975 | ||
7ba7c4e6 | 976 | bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree) |
eadd7bd2 | 977 | { |
9952adac | 978 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 979 | // the case to "char *" is needed for AIX 4.3 |
85da04e9 VZ |
980 | wxStatfs_t fs; |
981 | if ( wxStatfs((char *)(const char*)path.fn_str(), &fs) != 0 ) | |
eadd7bd2 | 982 | { |
401eb3de | 983 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 | 984 | |
9d8aca48 | 985 | return false; |
eadd7bd2 VZ |
986 | } |
987 | ||
125cb99b VZ |
988 | // under Solaris we also have to use f_frsize field instead of f_bsize |
989 | // which is in general a multiple of f_frsize | |
990 | #ifdef HAVE_STATVFS | |
7ba7c4e6 | 991 | wxDiskspaceSize_t blockSize = fs.f_frsize; |
125cb99b | 992 | #else // HAVE_STATFS |
7ba7c4e6 | 993 | wxDiskspaceSize_t blockSize = fs.f_bsize; |
125cb99b | 994 | #endif // HAVE_STATVFS/HAVE_STATFS |
9952adac | 995 | |
eadd7bd2 VZ |
996 | if ( pTotal ) |
997 | { | |
7ba7c4e6 | 998 | *pTotal = wxDiskspaceSize_t(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
999 | } |
1000 | ||
1001 | if ( pFree ) | |
1002 | { | |
7ba7c4e6 | 1003 | *pFree = wxDiskspaceSize_t(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
1004 | } |
1005 | ||
9d8aca48 | 1006 | return true; |
125cb99b | 1007 | #else // !HAVE_STATFS && !HAVE_STATVFS |
9d8aca48 | 1008 | return false; |
125cb99b | 1009 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
1010 | } |
1011 | ||
8fd0d89b VZ |
1012 | // ---------------------------------------------------------------------------- |
1013 | // env vars | |
1014 | // ---------------------------------------------------------------------------- | |
1015 | ||
c0472c7c VZ |
1016 | #if USE_PUTENV |
1017 | ||
1018 | WX_DECLARE_STRING_HASH_MAP(char *, wxEnvVars); | |
1019 | ||
1020 | static wxEnvVars gs_envVars; | |
1021 | ||
1022 | class wxSetEnvModule : public wxModule | |
1023 | { | |
1024 | public: | |
1025 | virtual bool OnInit() { return true; } | |
1026 | virtual void OnExit() | |
1027 | { | |
1028 | for ( wxEnvVars::const_iterator i = gs_envVars.begin(); | |
1029 | i != gs_envVars.end(); | |
1030 | ++i ) | |
1031 | { | |
1032 | free(i->second); | |
1033 | } | |
1034 | ||
1035 | gs_envVars.clear(); | |
1036 | } | |
1037 | ||
1038 | DECLARE_DYNAMIC_CLASS(wxSetEnvModule) | |
1039 | }; | |
1040 | ||
1041 | IMPLEMENT_DYNAMIC_CLASS(wxSetEnvModule, wxModule) | |
1042 | ||
1043 | #endif // USE_PUTENV | |
1044 | ||
97b305b7 | 1045 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
1046 | { |
1047 | // wxGetenv is defined as getenv() | |
86501081 | 1048 | char *p = wxGetenv(var); |
308978f6 | 1049 | if ( !p ) |
9d8aca48 | 1050 | return false; |
308978f6 VZ |
1051 | |
1052 | if ( value ) | |
1053 | { | |
1054 | *value = p; | |
1055 | } | |
1056 | ||
9d8aca48 | 1057 | return true; |
308978f6 VZ |
1058 | } |
1059 | ||
90a77e64 | 1060 | static bool wxDoSetEnv(const wxString& variable, const char *value) |
8fd0d89b VZ |
1061 | { |
1062 | #if defined(HAVE_SETENV) | |
a1353ea4 VZ |
1063 | if ( !value ) |
1064 | { | |
1065 | #ifdef HAVE_UNSETENV | |
65fd2fc2 VZ |
1066 | // don't test unsetenv() return value: it's void on some systems (at |
1067 | // least Darwin) | |
1068 | unsetenv(variable.mb_str()); | |
022a8d02 | 1069 | return true; |
a1353ea4 VZ |
1070 | #else |
1071 | value = ""; // we can't pass NULL to setenv() | |
1072 | #endif | |
1073 | } | |
1074 | ||
90a77e64 | 1075 | return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0; |
8fd0d89b VZ |
1076 | #elif defined(HAVE_PUTENV) |
1077 | wxString s = variable; | |
1078 | if ( value ) | |
1079 | s << _T('=') << value; | |
1080 | ||
1081 | // transform to ANSI | |
67479dbd | 1082 | const wxWX2MBbuf p = s.mb_str(); |
8fd0d89b | 1083 | |
8fd0d89b VZ |
1084 | char *buf = (char *)malloc(strlen(p) + 1); |
1085 | strcpy(buf, p); | |
1086 | ||
c0472c7c VZ |
1087 | // store the string to free() it later |
1088 | wxEnvVars::iterator i = gs_envVars.find(variable); | |
1089 | if ( i != gs_envVars.end() ) | |
1090 | { | |
1091 | free(i->second); | |
1092 | i->second = buf; | |
1093 | } | |
1094 | else // this variable hadn't been set before | |
1095 | { | |
1096 | gs_envVars[variable] = buf; | |
1097 | } | |
1098 | ||
8fd0d89b VZ |
1099 | return putenv(buf) == 0; |
1100 | #else // no way to set an env var | |
67479dbd | 1101 | return false; |
8fd0d89b VZ |
1102 | #endif |
1103 | } | |
1104 | ||
90a77e64 VS |
1105 | bool wxSetEnv(const wxString& variable, const wxString& value) |
1106 | { | |
1107 | return wxDoSetEnv(variable, value.mb_str()); | |
1108 | } | |
1109 | ||
1110 | bool wxUnsetEnv(const wxString& variable) | |
1111 | { | |
1112 | return wxDoSetEnv(variable, NULL); | |
1113 | } | |
1114 | ||
a37a5a73 VZ |
1115 | // ---------------------------------------------------------------------------- |
1116 | // signal handling | |
1117 | // ---------------------------------------------------------------------------- | |
1118 | ||
1119 | #if wxUSE_ON_FATAL_EXCEPTION | |
1120 | ||
1121 | #include <signal.h> | |
1122 | ||
90350682 | 1123 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1124 | { |
1125 | if ( wxTheApp ) | |
1126 | { | |
1127 | // give the user a chance to do something special about this | |
1128 | wxTheApp->OnFatalException(); | |
1129 | } | |
1130 | ||
1131 | abort(); | |
1132 | } | |
1133 | ||
1134 | bool wxHandleFatalExceptions(bool doit) | |
1135 | { | |
1136 | // old sig handlers | |
9d8aca48 | 1137 | static bool s_savedHandlers = false; |
a37a5a73 VZ |
1138 | static struct sigaction s_handlerFPE, |
1139 | s_handlerILL, | |
1140 | s_handlerBUS, | |
1141 | s_handlerSEGV; | |
1142 | ||
9d8aca48 | 1143 | bool ok = true; |
a37a5a73 VZ |
1144 | if ( doit && !s_savedHandlers ) |
1145 | { | |
1146 | // install the signal handler | |
1147 | struct sigaction act; | |
1148 | ||
1149 | // some systems extend it with non std fields, so zero everything | |
1150 | memset(&act, 0, sizeof(act)); | |
1151 | ||
1152 | act.sa_handler = wxFatalSignalHandler; | |
1153 | sigemptyset(&act.sa_mask); | |
1154 | act.sa_flags = 0; | |
1155 | ||
1156 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1157 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1158 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1159 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1160 | if ( !ok ) | |
1161 | { | |
1162 | wxLogDebug(_T("Failed to install our signal handler.")); | |
1163 | } | |
1164 | ||
9d8aca48 | 1165 | s_savedHandlers = true; |
a37a5a73 VZ |
1166 | } |
1167 | else if ( s_savedHandlers ) | |
1168 | { | |
1169 | // uninstall the signal handler | |
1170 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1171 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1172 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1173 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1174 | if ( !ok ) | |
1175 | { | |
1176 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1177 | } | |
1178 | ||
9d8aca48 | 1179 | s_savedHandlers = false; |
a37a5a73 VZ |
1180 | } |
1181 | //else: nothing to do | |
1182 | ||
1183 | return ok; | |
1184 | } | |
1185 | ||
1186 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1187 | ||
e2478fde VZ |
1188 | // ---------------------------------------------------------------------------- |
1189 | // wxExecute support | |
1190 | // ---------------------------------------------------------------------------- | |
1191 | ||
1d043598 | 1192 | int wxAppTraits::AddProcessCallback(wxEndProcessData *data, int fd) |
e2478fde | 1193 | { |
1d043598 VZ |
1194 | // define a custom handler processing only the closure of the descriptor |
1195 | struct wxEndProcessFDIOHandler : public wxFDIOHandler | |
e2478fde | 1196 | { |
1d043598 VZ |
1197 | wxEndProcessFDIOHandler(wxEndProcessData *data, int fd) |
1198 | : m_data(data), m_fd(fd) | |
b827d647 VZ |
1199 | { |
1200 | } | |
e2478fde | 1201 | |
1d043598 | 1202 | virtual void OnReadWaiting() |
e2478fde | 1203 | { |
1d043598 VZ |
1204 | wxFDIODispatcher::Get()->UnregisterFD(m_fd); |
1205 | close(m_fd); | |
05df0f1b | 1206 | |
1d043598 | 1207 | wxHandleProcessTermination(m_data); |
e2478fde | 1208 | |
1d043598 | 1209 | delete this; |
bc855d09 | 1210 | } |
e2478fde | 1211 | |
b827d647 VZ |
1212 | virtual void OnWriteWaiting() { wxFAIL_MSG("unreachable"); } |
1213 | virtual void OnExceptionWaiting() { wxFAIL_MSG("unreachable"); } | |
1214 | ||
1d043598 VZ |
1215 | wxEndProcessData * const m_data; |
1216 | const int m_fd; | |
1217 | }; | |
e2478fde | 1218 | |
1d043598 VZ |
1219 | wxFDIODispatcher::Get()->RegisterFD |
1220 | ( | |
1221 | fd, | |
1222 | new wxEndProcessFDIOHandler(data, fd), | |
b827d647 | 1223 | wxFDIO_INPUT |
1d043598 VZ |
1224 | ); |
1225 | return fd; // unused, but return something unique for the tag | |
e2478fde VZ |
1226 | } |
1227 | ||
9d3845e8 VZ |
1228 | bool wxAppTraits::CheckForRedirectedIO(wxExecuteData& execData) |
1229 | { | |
1230 | #if HAS_PIPE_INPUT_STREAM | |
1231 | bool hasIO = false; | |
1232 | ||
4d425dee | 1233 | if ( execData.bufOut && execData.bufOut->Update() ) |
9d3845e8 | 1234 | hasIO = true; |
9d3845e8 | 1235 | |
4d425dee | 1236 | if ( execData.bufErr && execData.bufErr->Update() ) |
9d3845e8 | 1237 | hasIO = true; |
9d3845e8 VZ |
1238 | |
1239 | return hasIO; | |
1240 | #else // !HAS_PIPE_INPUT_STREAM | |
1241 | return false; | |
1242 | #endif // HAS_PIPE_INPUT_STREAM/!HAS_PIPE_INPUT_STREAM | |
1243 | } | |
1244 | ||
dbd1c638 VZ |
1245 | // helper classes/functions used by WaitForChild() |
1246 | namespace | |
1247 | { | |
1248 | ||
1249 | // convenient base class for IO handlers which are registered for read | |
1250 | // notifications only and which also stores the FD we're reading from | |
1251 | // | |
1252 | // the derived classes still have to implement OnReadWaiting() | |
33343395 | 1253 | class wxReadFDIOHandler : public wxFDIOHandler |
46c7e1a1 | 1254 | { |
33343395 VZ |
1255 | public: |
1256 | wxReadFDIOHandler(wxFDIODispatcher& disp, int fd) : m_fd(fd) | |
46c7e1a1 | 1257 | { |
33343395 VZ |
1258 | if ( fd ) |
1259 | disp.RegisterFD(fd, this, wxFDIO_INPUT); | |
1260 | } | |
46c7e1a1 | 1261 | |
33343395 VZ |
1262 | virtual void OnWriteWaiting() { wxFAIL_MSG("unreachable"); } |
1263 | virtual void OnExceptionWaiting() { wxFAIL_MSG("unreachable"); } | |
46c7e1a1 | 1264 | |
dbd1c638 | 1265 | protected: |
33343395 VZ |
1266 | const int m_fd; |
1267 | ||
c0c133e1 | 1268 | wxDECLARE_NO_COPY_CLASS(wxReadFDIOHandler); |
33343395 VZ |
1269 | }; |
1270 | ||
dbd1c638 VZ |
1271 | // class for monitoring our end of the process detection pipe, simply sets a |
1272 | // flag when input on the pipe (which must be due to EOF) is detected | |
33343395 VZ |
1273 | class wxEndHandler : public wxReadFDIOHandler |
1274 | { | |
1275 | public: | |
1276 | wxEndHandler(wxFDIODispatcher& disp, int fd) | |
1277 | : wxReadFDIOHandler(disp, fd) | |
1278 | { | |
1279 | m_terminated = false; | |
1280 | } | |
1281 | ||
1282 | bool Terminated() const { return m_terminated; } | |
46c7e1a1 | 1283 | |
33343395 | 1284 | virtual void OnReadWaiting() { m_terminated = true; } |
46c7e1a1 | 1285 | |
33343395 VZ |
1286 | private: |
1287 | bool m_terminated; | |
1288 | ||
c0c133e1 | 1289 | wxDECLARE_NO_COPY_CLASS(wxEndHandler); |
33343395 VZ |
1290 | }; |
1291 | ||
82db3c3d | 1292 | #if HAS_PIPE_INPUT_STREAM |
33343395 | 1293 | |
dbd1c638 VZ |
1294 | // class for monitoring our ends of child stdout/err, should be constructed |
1295 | // with the FD and stream from wxExecuteData and will do nothing if they're | |
1296 | // invalid | |
1297 | // | |
1298 | // unlike wxEndHandler this class registers itself with the provided dispatcher | |
33343395 VZ |
1299 | class wxRedirectedIOHandler : public wxReadFDIOHandler |
1300 | { | |
1301 | public: | |
1302 | wxRedirectedIOHandler(wxFDIODispatcher& disp, | |
1303 | int fd, | |
1304 | wxStreamTempInputBuffer *buf) | |
1305 | : wxReadFDIOHandler(disp, fd), | |
1306 | m_buf(buf) | |
1307 | { | |
46c7e1a1 | 1308 | } |
33343395 VZ |
1309 | |
1310 | virtual void OnReadWaiting() | |
1d043598 | 1311 | { |
33343395 VZ |
1312 | m_buf->Update(); |
1313 | } | |
1314 | ||
1315 | private: | |
1316 | wxStreamTempInputBuffer * const m_buf; | |
1317 | ||
c0c133e1 | 1318 | wxDECLARE_NO_COPY_CLASS(wxRedirectedIOHandler); |
33343395 VZ |
1319 | }; |
1320 | ||
82db3c3d | 1321 | #endif // HAS_PIPE_INPUT_STREAM |
33343395 | 1322 | |
b827d647 | 1323 | // helper function which calls waitpid() and analyzes the result |
b827d647 VZ |
1324 | int DoWaitForChild(int pid, int flags = 0) |
1325 | { | |
1326 | wxASSERT_MSG( pid > 0, "invalid PID" ); | |
1327 | ||
1328 | int status, rc; | |
1329 | ||
1330 | // loop while we're getting EINTR | |
1331 | for ( ;; ) | |
1332 | { | |
1333 | rc = waitpid(pid, &status, flags); | |
1334 | ||
1335 | if ( rc != -1 || errno != EINTR ) | |
1336 | break; | |
1337 | } | |
1338 | ||
1339 | if ( rc == 0 ) | |
1340 | { | |
1341 | // This can only happen if the child application closes our dummy pipe | |
1342 | // that is used to monitor its lifetime; in that case, our best bet is | |
1343 | // to pretend the process did terminate, because otherwise wxExecute() | |
1344 | // would hang indefinitely (OnReadWaiting() won't be called again, the | |
1345 | // descriptor is closed now). | |
1346 | wxLogDebug("Child process (PID %d) still alive but pipe closed so " | |
1347 | "generating a close notification", pid); | |
1348 | } | |
1349 | else if ( rc == -1 ) | |
1350 | { | |
1351 | wxLogLastError(wxString::Format("waitpid(%d)", pid)); | |
1352 | } | |
1353 | else // child did terminate | |
1354 | { | |
1355 | wxASSERT_MSG( rc == pid, "unexpected waitpid() return value" ); | |
1356 | ||
1357 | if ( WIFEXITED(status) ) | |
1358 | return WEXITSTATUS(status); | |
1359 | else if ( WIFSIGNALED(status) ) | |
1360 | return -WTERMSIG(status); | |
1361 | else | |
1362 | { | |
1363 | wxLogError("Child process (PID %d) exited for unknown reason, " | |
1364 | "status = %d", pid, status); | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | return -1; | |
1369 | } | |
1370 | ||
1371 | } // anonymous namespace | |
1372 | ||
33343395 VZ |
1373 | int wxAppTraits::WaitForChild(wxExecuteData& execData) |
1374 | { | |
1375 | if ( !(execData.flags & wxEXEC_SYNC) ) | |
1376 | { | |
e528a71b VZ |
1377 | // asynchronous execution: just launch the process and return, |
1378 | // endProcData will be destroyed when it terminates (currently we leak | |
1379 | // it if the process doesn't terminate before we do and this should be | |
1380 | // fixed but it's not a real leak so it's not really very high | |
1381 | // priority) | |
1d043598 | 1382 | wxEndProcessData *endProcData = new wxEndProcessData; |
e528a71b VZ |
1383 | endProcData->process = execData.process; |
1384 | endProcData->pid = execData.pid; | |
1d043598 VZ |
1385 | endProcData->tag = AddProcessCallback |
1386 | ( | |
1387 | endProcData, | |
33343395 | 1388 | execData.GetEndProcReadFD() |
1d043598 | 1389 | ); |
e528a71b | 1390 | endProcData->async = true; |
46c7e1a1 | 1391 | |
1d043598 | 1392 | return execData.pid; |
33343395 | 1393 | } |
e528a71b | 1394 | //else: synchronous execution case |
33343395 | 1395 | |
82db3c3d | 1396 | #if HAS_PIPE_INPUT_STREAM |
33343395 VZ |
1397 | wxProcess * const process = execData.process; |
1398 | if ( process && process->IsRedirected() ) | |
1399 | { | |
1400 | // we can't simply block waiting for the child to terminate as we would | |
1401 | // dead lock if it writes more than the pipe buffer size (typically | |
1402 | // 4KB) bytes of output -- it would then block waiting for us to read | |
1403 | // the data while we'd block waiting for it to terminate | |
1404 | // | |
1405 | // so multiplex here waiting for any input from the child or closure of | |
1406 | // the pipe used to indicate its termination | |
1407 | wxSelectDispatcher disp; | |
1408 | ||
1409 | wxEndHandler endHandler(disp, execData.GetEndProcReadFD()); | |
1410 | ||
1411 | wxRedirectedIOHandler outHandler(disp, execData.fdOut, execData.bufOut), | |
1412 | errHandler(disp, execData.fdErr, execData.bufErr); | |
1413 | ||
1414 | while ( !endHandler.Terminated() ) | |
1415 | { | |
1416 | disp.Dispatch(); | |
1417 | } | |
1418 | } | |
1419 | //else: no IO redirection, just block waiting for the child to exit | |
82db3c3d | 1420 | #endif // HAS_PIPE_INPUT_STREAM |
33343395 | 1421 | |
b827d647 | 1422 | return DoWaitForChild(execData.pid); |
46c7e1a1 | 1423 | } |
46c7e1a1 | 1424 | |
b827d647 | 1425 | void wxHandleProcessTermination(wxEndProcessData *data) |
e2478fde | 1426 | { |
b827d647 VZ |
1427 | data->exitcode = DoWaitForChild(data->pid, WNOHANG); |
1428 | ||
e2478fde | 1429 | // notify user about termination if required |
b827d647 | 1430 | if ( data->process ) |
e2478fde | 1431 | { |
b827d647 | 1432 | data->process->OnTerminate(data->pid, data->exitcode); |
e2478fde VZ |
1433 | } |
1434 | ||
e528a71b VZ |
1435 | if ( data->async ) |
1436 | { | |
1437 | // in case of asynchronous execution we don't need this data any more | |
1438 | // after the child terminates | |
1439 | delete data; | |
1440 | } | |
1441 | else // sync execution | |
1442 | { | |
1443 | // let wxExecute() know that the process has terminated | |
1444 | data->pid = 0; | |
1445 | } | |
e2478fde VZ |
1446 | } |
1447 |