]>
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 | |
821d856a | 7 | // (c) 2013 Rob Bresalier, Vadim Zeitlin |
65571936 | 8 | // Licence: wxWindows licence |
518b5d2f VZ |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
14f355c2 VS |
19 | // for compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
88a7a4e1 WS |
22 | #include "wx/utils.h" |
23 | ||
c0472c7c VZ |
24 | #define USE_PUTENV (!defined(HAVE_SETENV) && defined(HAVE_PUTENV)) |
25 | ||
7520f3da WS |
26 | #ifndef WX_PRECOMP |
27 | #include "wx/string.h" | |
88a7a4e1 | 28 | #include "wx/intl.h" |
e4db172a | 29 | #include "wx/log.h" |
670f9935 | 30 | #include "wx/app.h" |
0cb7e05c | 31 | #include "wx/wxcrtvararg.h" |
c0472c7c VZ |
32 | #if USE_PUTENV |
33 | #include "wx/module.h" | |
34 | #include "wx/hashmap.h" | |
35 | #endif | |
7520f3da | 36 | #endif |
518b5d2f | 37 | |
46446cc2 | 38 | #include "wx/apptrait.h" |
518b5d2f | 39 | |
518b5d2f | 40 | #include "wx/process.h" |
821d856a | 41 | #include "wx/scopedptr.h" |
bdc72a22 | 42 | #include "wx/thread.h" |
518b5d2f | 43 | |
fae71206 VZ |
44 | #include "wx/cmdline.h" |
45 | ||
80d6dc0a | 46 | #include "wx/wfstream.h" |
8b33ae2d | 47 | |
33343395 | 48 | #include "wx/private/selectdispatcher.h" |
1d043598 | 49 | #include "wx/private/fdiodispatcher.h" |
e2478fde | 50 | #include "wx/unix/execute.h" |
ccf8bf20 | 51 | #include "wx/unix/pipe.h" |
17a1ebd1 VZ |
52 | #include "wx/unix/private.h" |
53 | ||
821d856a VZ |
54 | #include "wx/evtloop.h" |
55 | #include "wx/mstream.h" | |
56 | #include "wx/private/fdioeventloopsourcehandler.h" | |
46c7e1a1 | 57 | |
17a1ebd1 | 58 | #include <pwd.h> |
bc855d09 | 59 | #include <sys/wait.h> // waitpid() |
e2478fde | 60 | |
bc023abb MW |
61 | #ifdef HAVE_SYS_SELECT_H |
62 | # include <sys/select.h> | |
63 | #endif | |
64 | ||
f895c3fc | 65 | #define HAS_PIPE_STREAMS (wxUSE_STREAMS && wxUSE_FILE) |
1f3b2af0 | 66 | |
f895c3fc | 67 | #if HAS_PIPE_STREAMS |
2887179b | 68 | |
bfabc7f4 | 69 | #include "wx/private/pipestream.h" |
9b70925d | 70 | #include "wx/private/streamtempinput.h" |
821d856a | 71 | #include "wx/unix/private/executeiohandler.h" |
2887179b | 72 | |
f895c3fc | 73 | #endif // HAS_PIPE_STREAMS |
2887179b | 74 | |
85da04e9 VZ |
75 | // not only the statfs syscall is called differently depending on platform, but |
76 | // one of its incarnations, statvfs(), takes different arguments under | |
77 | // different platforms and even different versions of the same system (Solaris | |
78 | // 7 and 8): if you want to test for this, don't forget that the problems only | |
79 | // appear if the large files support is enabled | |
eadd7bd2 | 80 | #ifdef HAVE_STATFS |
85da04e9 VZ |
81 | #ifdef __BSD__ |
82 | #include <sys/param.h> | |
83 | #include <sys/mount.h> | |
84 | #else // !__BSD__ | |
85 | #include <sys/vfs.h> | |
86 | #endif // __BSD__/!__BSD__ | |
87 | ||
88 | #define wxStatfs statfs | |
84ae7ca4 VZ |
89 | |
90 | #ifndef HAVE_STATFS_DECL | |
91 | // some systems lack statfs() prototype in the system headers (AIX 4) | |
92 | extern "C" int statfs(const char *path, struct statfs *buf); | |
93 | #endif | |
eadd7bd2 VZ |
94 | #endif // HAVE_STATFS |
95 | ||
9952adac VZ |
96 | #ifdef HAVE_STATVFS |
97 | #include <sys/statvfs.h> | |
98 | ||
85da04e9 VZ |
99 | #define wxStatfs statvfs |
100 | #endif // HAVE_STATVFS | |
101 | ||
102 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) | |
103 | // WX_STATFS_T is detected by configure | |
104 | #define wxStatfs_t WX_STATFS_T | |
105 | #endif | |
9952adac | 106 | |
f6bcfd97 BP |
107 | // SGI signal.h defines signal handler arguments differently depending on |
108 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
109 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
110 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
111 | #endif // SGI hack | |
112 | ||
518b5d2f VZ |
113 | #include <stdarg.h> |
114 | #include <dirent.h> | |
115 | #include <string.h> | |
116 | #include <sys/stat.h> | |
117 | #include <sys/types.h> | |
518b5d2f | 118 | #include <sys/wait.h> |
e2478fde | 119 | #include <unistd.h> |
518b5d2f VZ |
120 | #include <errno.h> |
121 | #include <netdb.h> | |
122 | #include <signal.h> | |
518b5d2f | 123 | #include <time.h> // nanosleep() and/or usleep() |
fad866f4 | 124 | #include <ctype.h> // isspace() |
b12915c1 | 125 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 126 | |
0fcdf6dc | 127 | #ifdef HAVE_UNAME |
518b5d2f VZ |
128 | #include <sys/utsname.h> // for uname() |
129 | #endif // HAVE_UNAME | |
130 | ||
9ad34f61 VZ |
131 | // Used by wxGetFreeMemory(). |
132 | #ifdef __SGI__ | |
133 | #include <sys/sysmp.h> | |
134 | #include <sys/sysinfo.h> // for SAGET and MINFO structures | |
135 | #endif | |
136 | ||
eaf4bde6 VZ |
137 | #ifdef HAVE_SETPRIORITY |
138 | #include <sys/resource.h> // for setpriority() | |
139 | #endif | |
140 | ||
518b5d2f VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // conditional compilation | |
143 | // ---------------------------------------------------------------------------- | |
144 | ||
145 | // many versions of Unices have this function, but it is not defined in system | |
146 | // headers - please add your system here if it is the case for your OS. | |
147 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b | 148 | #if !defined(HAVE_USLEEP) && \ |
d67fee71 | 149 | ((defined(__SUN__) && !defined(__SunOs_5_6) && \ |
518b5d2f | 150 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
d67fee71 | 151 | defined(__osf__) || defined(__EMX__)) |
518b5d2f VZ |
152 | extern "C" |
153 | { | |
1a5e269b VZ |
154 | #ifdef __EMX__ |
155 | /* I copied this from the XFree86 diffs. AV. */ | |
156 | #define INCL_DOSPROCESS | |
157 | #include <os2.h> | |
158 | inline void usleep(unsigned long delay) | |
159 | { | |
160 | DosSleep(delay ? (delay/1000l) : 1l); | |
161 | } | |
162 | #else // Unix | |
1363811b | 163 | int usleep(unsigned int usec); |
1a5e269b | 164 | #endif // __EMX__/Unix |
518b5d2f | 165 | }; |
bdc72a22 VZ |
166 | |
167 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
168 | #endif // Unices without usleep() |
169 | ||
518b5d2f VZ |
170 | // ============================================================================ |
171 | // implementation | |
172 | // ============================================================================ | |
173 | ||
174 | // ---------------------------------------------------------------------------- | |
175 | // sleeping | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | void wxSleep(int nSecs) | |
179 | { | |
180 | sleep(nSecs); | |
181 | } | |
182 | ||
66cd9d7f | 183 | void wxMicroSleep(unsigned long microseconds) |
518b5d2f | 184 | { |
b12915c1 | 185 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 186 | timespec tmReq; |
66cd9d7f VZ |
187 | tmReq.tv_sec = (time_t)(microseconds / 1000000); |
188 | tmReq.tv_nsec = (microseconds % 1000000) * 1000; | |
518b5d2f VZ |
189 | |
190 | // we're not interested in remaining time nor in return value | |
d3b9f782 | 191 | (void)nanosleep(&tmReq, NULL); |
b12915c1 | 192 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
193 | // uncomment this if you feel brave or if you are sure that your version |
194 | // of Solaris has a safe usleep() function but please notice that usleep() | |
195 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
196 | // documented as MT-Safe | |
ea18eed9 | 197 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
198 | #error "usleep() cannot be used in MT programs under Solaris." |
199 | #endif // Sun | |
200 | ||
66cd9d7f | 201 | usleep(microseconds); |
b12915c1 VZ |
202 | #elif defined(HAVE_SLEEP) |
203 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
66cd9d7f | 204 | sleep(microseconds * 1000000); |
518b5d2f | 205 | #else // !sleep function |
66cd9d7f | 206 | #error "usleep() or nanosleep() function required for wxMicroSleep" |
518b5d2f VZ |
207 | #endif // sleep function |
208 | } | |
209 | ||
66cd9d7f VZ |
210 | void wxMilliSleep(unsigned long milliseconds) |
211 | { | |
212 | wxMicroSleep(milliseconds*1000); | |
213 | } | |
214 | ||
518b5d2f VZ |
215 | // ---------------------------------------------------------------------------- |
216 | // process management | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
e0f6b731 | 219 | int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags) |
518b5d2f | 220 | { |
e0f6b731 | 221 | int err = kill((pid_t) (flags & wxKILL_CHILDREN) ? -pid : pid, (int)sig); |
50567b69 VZ |
222 | if ( rc ) |
223 | { | |
f0bce4d4 | 224 | switch ( err ? errno : 0 ) |
50567b69 VZ |
225 | { |
226 | case 0: | |
227 | *rc = wxKILL_OK; | |
228 | break; | |
229 | ||
230 | case EINVAL: | |
231 | *rc = wxKILL_BAD_SIGNAL; | |
232 | break; | |
233 | ||
234 | case EPERM: | |
235 | *rc = wxKILL_ACCESS_DENIED; | |
236 | break; | |
237 | ||
238 | case ESRCH: | |
239 | *rc = wxKILL_NO_PROCESS; | |
240 | break; | |
241 | ||
242 | default: | |
243 | // this goes against Unix98 docs so log it | |
9a83f860 | 244 | wxLogDebug(wxT("unexpected kill(2) return value %d"), err); |
50567b69 VZ |
245 | |
246 | // something else... | |
247 | *rc = wxKILL_ERROR; | |
248 | } | |
249 | } | |
250 | ||
251 | return err; | |
518b5d2f VZ |
252 | } |
253 | ||
05718a98 | 254 | // Shutdown or reboot the PC |
c1baf4b5 | 255 | bool wxShutdown(int flags) |
518b5d2f | 256 | { |
c1baf4b5 VZ |
257 | flags &= ~wxSHUTDOWN_FORCE; |
258 | ||
05718a98 | 259 | wxChar level; |
c1baf4b5 | 260 | switch ( flags ) |
05718a98 VZ |
261 | { |
262 | case wxSHUTDOWN_POWEROFF: | |
9a83f860 | 263 | level = wxT('0'); |
05718a98 | 264 | break; |
647b8e37 | 265 | |
05718a98 | 266 | case wxSHUTDOWN_REBOOT: |
9a83f860 | 267 | level = wxT('6'); |
05718a98 | 268 | break; |
518b5d2f | 269 | |
c1baf4b5 VZ |
270 | case wxSHUTDOWN_LOGOFF: |
271 | // TODO: use dcop to log off? | |
272 | return false; | |
273 | ||
05718a98 | 274 | default: |
9a83f860 | 275 | wxFAIL_MSG( wxT("unknown wxShutdown() flag") ); |
05718a98 VZ |
276 | return false; |
277 | } | |
0ed9a934 | 278 | |
c1baf4b5 | 279 | return system(wxString::Format("init %c", level).mb_str()) == 0; |
05718a98 | 280 | } |
0ed9a934 | 281 | |
05718a98 VZ |
282 | // ---------------------------------------------------------------------------- |
283 | // wxStream classes to support IO redirection in wxExecute | |
284 | // ---------------------------------------------------------------------------- | |
0ed9a934 | 285 | |
f895c3fc | 286 | #if HAS_PIPE_STREAMS |
0ed9a934 | 287 | |
05718a98 VZ |
288 | bool wxPipeInputStream::CanRead() const |
289 | { | |
290 | if ( m_lasterror == wxSTREAM_EOF ) | |
291 | return false; | |
0ed9a934 | 292 | |
05718a98 VZ |
293 | // check if there is any input available |
294 | struct timeval tv; | |
295 | tv.tv_sec = 0; | |
296 | tv.tv_usec = 0; | |
0ed9a934 | 297 | |
05718a98 | 298 | const int fd = m_file->fd(); |
0ed9a934 | 299 | |
05718a98 | 300 | fd_set readfds; |
0ed9a934 | 301 | |
05718a98 VZ |
302 | wxFD_ZERO(&readfds); |
303 | wxFD_SET(fd, &readfds); | |
0ed9a934 | 304 | |
05718a98 VZ |
305 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) |
306 | { | |
307 | case -1: | |
308 | wxLogSysError(_("Impossible to get child process input")); | |
309 | // fall through | |
62705a27 | 310 | |
05718a98 VZ |
311 | case 0: |
312 | return false; | |
518b5d2f | 313 | |
05718a98 | 314 | default: |
9a83f860 | 315 | wxFAIL_MSG(wxT("unexpected select() return value")); |
05718a98 | 316 | // still fall through |
518b5d2f | 317 | |
05718a98 VZ |
318 | case 1: |
319 | // input available -- or maybe not, as select() returns 1 when a | |
320 | // read() will complete without delay, but it could still not read | |
321 | // anything | |
322 | return !Eof(); | |
323 | } | |
518b5d2f VZ |
324 | } |
325 | ||
3b816097 VZ |
326 | size_t wxPipeOutputStream::OnSysWrite(const void *buffer, size_t size) |
327 | { | |
328 | // We need to suppress error logging here, because on writing to a pipe | |
329 | // which is full, wxFile::Write reports a system error. However, this is | |
330 | // not an extraordinary situation, and it should not be reported to the | |
331 | // user (but if really needed, the program can recognize it by checking | |
332 | // whether LastRead() == 0.) Other errors will be reported below. | |
333 | size_t ret; | |
334 | { | |
335 | wxLogNull logNo; | |
336 | ret = m_file->Write(buffer, size); | |
337 | } | |
338 | ||
339 | switch ( m_file->GetLastError() ) | |
340 | { | |
341 | // pipe is full | |
342 | #ifdef EAGAIN | |
343 | case EAGAIN: | |
344 | #endif | |
345 | #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) | |
346 | case EWOULDBLOCK: | |
347 | #endif | |
348 | // do not treat it as an error | |
349 | m_file->ClearLastError(); | |
350 | // fall through | |
351 | ||
352 | // no error | |
353 | case 0: | |
354 | break; | |
355 | ||
356 | // some real error | |
357 | default: | |
358 | wxLogSysError(_("Can't write to child process's stdin")); | |
359 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
360 | } | |
361 | ||
362 | return ret; | |
363 | } | |
364 | ||
f895c3fc | 365 | #endif // HAS_PIPE_STREAMS |
05718a98 | 366 | |
2c8e4738 VZ |
367 | // ---------------------------------------------------------------------------- |
368 | // wxShell | |
369 | // ---------------------------------------------------------------------------- | |
370 | ||
371 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
372 | { |
373 | wxString cmd; | |
cd6ce4a9 | 374 | if ( !command ) |
2c8e4738 VZ |
375 | { |
376 | // just an interactive shell | |
9a83f860 | 377 | cmd = wxT("xterm"); |
2c8e4738 | 378 | } |
518b5d2f | 379 | else |
2c8e4738 VZ |
380 | { |
381 | // execute command in a shell | |
9a83f860 | 382 | cmd << wxT("/bin/sh -c '") << command << wxT('\''); |
2c8e4738 VZ |
383 | } |
384 | ||
385 | return cmd; | |
386 | } | |
387 | ||
388 | bool wxShell(const wxString& command) | |
389 | { | |
fbf456aa | 390 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
391 | } |
392 | ||
393 | bool wxShell(const wxString& command, wxArrayString& output) | |
394 | { | |
9a83f860 | 395 | wxCHECK_MSG( !command.empty(), false, wxT("can't exec shell non interactively") ); |
518b5d2f | 396 | |
2c8e4738 | 397 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
398 | } |
399 | ||
05718a98 | 400 | namespace |
f6ba47d9 | 401 | { |
05718a98 VZ |
402 | |
403 | // helper class for storing arguments as char** array suitable for passing to | |
404 | // execvp(), whatever form they were passed to us | |
405 | class ArgsArray | |
406 | { | |
407 | public: | |
408 | ArgsArray(const wxArrayString& args) | |
f6ba47d9 | 409 | { |
05718a98 | 410 | Init(args.size()); |
f6ba47d9 | 411 | |
05718a98 VZ |
412 | for ( int i = 0; i < m_argc; i++ ) |
413 | { | |
414 | m_argv[i] = wxStrdup(args[i]); | |
415 | } | |
416 | } | |
f6ba47d9 | 417 | |
d7ef641d | 418 | #if wxUSE_UNICODE |
05718a98 VZ |
419 | ArgsArray(wchar_t **wargv) |
420 | { | |
421 | int argc = 0; | |
64d029f1 | 422 | while ( wargv[argc] ) |
05718a98 VZ |
423 | argc++; |
424 | ||
425 | Init(argc); | |
426 | ||
427 | for ( int i = 0; i < m_argc; i++ ) | |
428 | { | |
429 | m_argv[i] = wxSafeConvertWX2MB(wargv[i]).release(); | |
430 | } | |
f6ba47d9 | 431 | } |
d7ef641d | 432 | #endif // wxUSE_UNICODE |
f6ba47d9 | 433 | |
05718a98 VZ |
434 | ~ArgsArray() |
435 | { | |
436 | for ( int i = 0; i < m_argc; i++ ) | |
437 | { | |
438 | free(m_argv[i]); | |
439 | } | |
440 | ||
441 | delete [] m_argv; | |
442 | } | |
443 | ||
444 | operator char**() const { return m_argv; } | |
445 | ||
446 | private: | |
447 | void Init(int argc) | |
448 | { | |
449 | m_argc = argc; | |
450 | m_argv = new char *[m_argc + 1]; | |
451 | m_argv[m_argc] = NULL; | |
452 | } | |
453 | ||
454 | int m_argc; | |
455 | char **m_argv; | |
456 | ||
c0c133e1 | 457 | wxDECLARE_NO_COPY_CLASS(ArgsArray); |
05718a98 VZ |
458 | }; |
459 | ||
460 | } // anonymous namespace | |
f6ba47d9 | 461 | |
cd6ce4a9 | 462 | // ---------------------------------------------------------------------------- |
05718a98 | 463 | // wxExecute implementations |
cd6ce4a9 | 464 | // ---------------------------------------------------------------------------- |
6dc6fda6 | 465 | |
05718a98 VZ |
466 | #if defined(__DARWIN__) |
467 | bool wxMacLaunch(char **argv); | |
468 | #endif | |
1e6feb95 | 469 | |
164db92c VZ |
470 | long wxExecute(const wxString& command, int flags, wxProcess *process, |
471 | const wxExecuteEnv *env) | |
8b33ae2d | 472 | { |
fae71206 VZ |
473 | ArgsArray argv(wxCmdLineParser::ConvertStringToArgs(command, |
474 | wxCMD_LINE_SPLIT_UNIX)); | |
05718a98 | 475 | |
164db92c | 476 | return wxExecute(argv, flags, process, env); |
8b33ae2d GL |
477 | } |
478 | ||
d7ef641d VZ |
479 | #if wxUSE_UNICODE |
480 | ||
164db92c VZ |
481 | long wxExecute(wchar_t **wargv, int flags, wxProcess *process, |
482 | const wxExecuteEnv *env) | |
05718a98 VZ |
483 | { |
484 | ArgsArray argv(wargv); | |
1e6feb95 | 485 | |
164db92c | 486 | return wxExecute(argv, flags, process, env); |
05718a98 | 487 | } |
79066131 | 488 | |
d7ef641d VZ |
489 | #endif // wxUSE_UNICODE |
490 | ||
821d856a VZ |
491 | namespace |
492 | { | |
493 | ||
494 | // Helper function of wxExecute(): wait for the process termination without | |
495 | // dispatching any events. | |
496 | // | |
497 | // This is used in wxEXEC_NOEVENTS case. | |
498 | int BlockUntilChildExit(wxExecuteData& execData) | |
499 | { | |
500 | wxCHECK_MSG( wxTheApp, -1, | |
501 | wxS("Can't block until child exit without wxTheApp") ); | |
502 | ||
ccf8bf20 VZ |
503 | #if wxUSE_SELECT_DISPATCHER |
504 | ||
821d856a VZ |
505 | // Even if we don't want to dispatch events, we still need to handle |
506 | // child IO notifications and process termination concurrently, i.e. | |
507 | // we can't simply block waiting for the child to terminate as we would | |
508 | // dead lock if it writes more than the pipe buffer size (typically | |
509 | // 4KB) bytes of output -- it would then block waiting for us to read | |
510 | // the data while we'd block waiting for it to terminate. | |
511 | // | |
512 | // So while we don't use the full blown event loop, we still do use a | |
513 | // dispatcher with which we register just the 3 FDs we're interested | |
514 | // in: the child stdout and stderr and the pipe written to by the | |
515 | // signal handler so that we could react to the child process | |
516 | // termination too. | |
517 | ||
518 | // Notice that we must create a new dispatcher object here instead of | |
519 | // reusing the global wxFDIODispatcher::Get() because we want to | |
520 | // monitor only the events on the FDs explicitly registered with this | |
521 | // one and not all the other ones that could be registered with the | |
522 | // global dispatcher (think about the case of nested wxExecute() calls). | |
523 | wxSelectDispatcher dispatcher; | |
524 | ||
525 | // Do register all the FDs we want to monitor here: first, the one used to | |
526 | // handle the signals asynchronously. | |
527 | wxScopedPtr<wxFDIOHandler> | |
528 | signalHandler(wxTheApp->RegisterSignalWakeUpPipe(dispatcher)); | |
529 | ||
530 | #if wxUSE_STREAMS | |
531 | // And then the two for the child output and error streams if necessary. | |
532 | wxScopedPtr<wxFDIOHandler> | |
533 | stdoutHandler, | |
534 | stderrHandler; | |
535 | if ( execData.IsRedirected() ) | |
536 | { | |
537 | stdoutHandler.reset(new wxExecuteFDIOHandler | |
538 | ( | |
539 | dispatcher, | |
540 | execData.fdOut, | |
541 | execData.bufOut | |
542 | )); | |
543 | stderrHandler.reset(new wxExecuteFDIOHandler | |
544 | ( | |
545 | dispatcher, | |
546 | execData.fdErr, | |
547 | execData.bufErr | |
548 | )); | |
549 | } | |
550 | #endif // wxUSE_STREAMS | |
551 | ||
552 | // And dispatch until the PID is reset from wxExecuteData::OnExit(). | |
553 | while ( execData.pid ) | |
554 | { | |
555 | dispatcher.Dispatch(); | |
556 | } | |
557 | ||
558 | return execData.exitcode; | |
ccf8bf20 VZ |
559 | #else // !wxUSE_SELECT_DISPATCHER |
560 | wxFAIL_MSG( wxS("Can't block until child exit without wxSelectDispatcher") ); | |
561 | ||
562 | return -1; | |
563 | #endif // wxUSE_SELECT_DISPATCHER/!wxUSE_SELECT_DISPATCHER | |
821d856a VZ |
564 | } |
565 | ||
566 | } // anonymous namespace | |
567 | ||
05718a98 | 568 | // wxExecute: the real worker function |
164db92c VZ |
569 | long wxExecute(char **argv, int flags, wxProcess *process, |
570 | const wxExecuteEnv *env) | |
518b5d2f | 571 | { |
f6bcfd97 | 572 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
573 | // case we return 0 which is never a valid PID |
574 | // | |
575 | // we define this as a macro, not a variable, to avoid compiler warnings | |
576 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 577 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 578 | |
accb3257 | 579 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 580 | |
05718a98 VZ |
581 | #if wxUSE_THREADS |
582 | // fork() doesn't mix well with POSIX threads: on many systems the program | |
583 | // deadlocks or crashes for some reason. Probably our code is buggy and | |
584 | // doesn't do something which must be done to allow this to work, but I | |
585 | // don't know what yet, so for now just warn the user (this is the least we | |
586 | // can do) about it | |
587 | wxASSERT_MSG( wxThread::IsMain(), | |
9a83f860 | 588 | wxT("wxExecute() can be called only from the main thread") ); |
05718a98 | 589 | #endif // wxUSE_THREADS |
05079acc | 590 | |
052e5ec5 | 591 | #if defined(__WXCOCOA__) || ( defined(__WXOSX_MAC__) && wxOSX_USE_COCOA_OR_CARBON ) |
05718a98 VZ |
592 | // wxMacLaunch() only executes app bundles and only does it asynchronously. |
593 | // It returns false if the target is not an app bundle, thus falling | |
594 | // through to the regular code for non app bundles. | |
595 | if ( !(flags & wxEXEC_SYNC) && wxMacLaunch(argv) ) | |
e90c1d2a | 596 | { |
05718a98 VZ |
597 | // we don't have any PID to return so just make up something non null |
598 | return -1; | |
05079acc | 599 | } |
05718a98 VZ |
600 | #endif // __DARWIN__ |
601 | ||
05718a98 | 602 | // this struct contains all information which we use for housekeeping |
821d856a VZ |
603 | wxScopedPtr<wxExecuteData> execDataPtr(new wxExecuteData); |
604 | wxExecuteData& execData = *execDataPtr; | |
605 | ||
e2478fde VZ |
606 | execData.flags = flags; |
607 | execData.process = process; | |
608 | ||
821d856a | 609 | // create pipes for inter process communication |
b477f956 VZ |
610 | wxPipe pipeIn, // stdin |
611 | pipeOut, // stdout | |
612 | pipeErr; // stderr | |
cd6ce4a9 VZ |
613 | |
614 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 615 | { |
b477f956 | 616 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 617 | { |
cd6ce4a9 | 618 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d | 619 | |
accb3257 | 620 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
621 | } |
622 | } | |
8b33ae2d | 623 | |
eaf4bde6 VZ |
624 | // priority: we need to map wxWidgets priority which is in the range 0..100 |
625 | // to Unix nice value which is in the range -20..19. As there is an odd | |
626 | // number of elements in our range and an even number in the Unix one, we | |
627 | // have to do it in this rather ugly way to guarantee that: | |
628 | // 1. wxPRIORITY_{MIN,DEFAULT,MAX} map to -20, 0 and 19 respectively. | |
629 | // 2. The mapping is monotonously increasing. | |
630 | // 3. The mapping is onto the target range. | |
27e8395e | 631 | int prio = process ? process->GetPriority() : 0; |
eaf4bde6 VZ |
632 | if ( prio <= 50 ) |
633 | prio = (2*prio)/5 - 20; | |
634 | else if ( prio < 55 ) | |
635 | prio = 1; | |
636 | else | |
637 | prio = (2*prio)/5 - 21; | |
638 | ||
518b5d2f | 639 | // fork the process |
ef5f8ab6 VZ |
640 | // |
641 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
642 | // reason under Solaris (and maybe others, although not under Linux) | |
b2ddee86 JJ |
643 | // But on OpenVMS we do not have fork so we have to use vfork and |
644 | // cross our fingers that it works. | |
645 | #ifdef __VMS | |
646 | pid_t pid = vfork(); | |
647 | #else | |
648 | pid_t pid = fork(); | |
649 | #endif | |
650 | if ( pid == -1 ) // error? | |
518b5d2f VZ |
651 | { |
652 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a | 653 | |
accb3257 | 654 | return ERROR_RETURN_CODE; |
518b5d2f | 655 | } |
cd6ce4a9 | 656 | else if ( pid == 0 ) // we're in child |
518b5d2f | 657 | { |
5ddfa074 VZ |
658 | // NB: we used to close all the unused descriptors of the child here |
659 | // but this broke some programs which relied on e.g. FD 1 being | |
660 | // always opened so don't do it any more, after all there doesn't | |
661 | // seem to be any real problem with keeping them opened | |
e1082c9f | 662 | |
e8e776aa | 663 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
664 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
665 | { | |
666 | // Set process group to child process' pid. Then killing -pid | |
667 | // of the parent will kill the process and all of its children. | |
668 | setsid(); | |
518b5d2f | 669 | } |
0c9c4401 VZ |
670 | #endif // !__VMS |
671 | ||
eaf4bde6 | 672 | #if defined(HAVE_SETPRIORITY) |
27e8395e | 673 | if ( prio && setpriority(PRIO_PROCESS, 0, prio) != 0 ) |
eaf4bde6 VZ |
674 | { |
675 | wxLogSysError(_("Failed to set process priority")); | |
676 | } | |
677 | #endif // HAVE_SETPRIORITY | |
678 | ||
80d6dc0a | 679 | // redirect stdin, stdout and stderr |
b477f956 | 680 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 681 | { |
b477f956 VZ |
682 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
683 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
684 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 685 | { |
f6bcfd97 | 686 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 687 | } |
518b5d2f | 688 | |
b477f956 VZ |
689 | pipeIn.Close(); |
690 | pipeOut.Close(); | |
691 | pipeErr.Close(); | |
cd6ce4a9 | 692 | } |
518b5d2f | 693 | |
5a1d70f9 VZ |
694 | // Close all (presumably accidentally) inherited file descriptors to |
695 | // avoid descriptor leaks. This means that we don't allow inheriting | |
696 | // them purposefully but this seems like a lesser evil in wx code. | |
697 | // Ideally we'd provide some flag to indicate that none (or some?) of | |
698 | // the descriptors do not need to be closed but for now this is better | |
699 | // than never closing them at all as wx code never used FD_CLOEXEC. | |
700 | ||
5a1d70f9 VZ |
701 | // TODO: Iterating up to FD_SETSIZE is both inefficient (because it may |
702 | // be quite big) and incorrect (because in principle we could | |
703 | // have more opened descriptions than this number). Unfortunately | |
704 | // there is no good portable solution for closing all descriptors | |
705 | // above a certain threshold but non-portable solutions exist for | |
706 | // most platforms, see [http://stackoverflow.com/questions/899038/ | |
707 | // getting-the-highest-allocated-file-descriptor] | |
708 | for ( int fd = 0; fd < (int)FD_SETSIZE; ++fd ) | |
709 | { | |
710 | if ( fd != STDIN_FILENO && | |
711 | fd != STDOUT_FILENO && | |
821d856a | 712 | fd != STDERR_FILENO ) |
5a1d70f9 VZ |
713 | { |
714 | close(fd); | |
715 | } | |
716 | } | |
717 | ||
718 | ||
164db92c VZ |
719 | // Process additional options if we have any |
720 | if ( env ) | |
721 | { | |
722 | // Change working directory if it is specified | |
723 | if ( !env->cwd.empty() ) | |
724 | wxSetWorkingDirectory(env->cwd); | |
725 | ||
726 | // Change environment if needed. | |
727 | // | |
728 | // NB: We can't use execve() currently because we allow using | |
729 | // non full paths to wxExecute(), i.e. we want to search for | |
730 | // the program in PATH. However it just might be simpler/better | |
731 | // to do the search manually and use execve() envp parameter to | |
732 | // set up the environment of the child process explicitly | |
733 | // instead of doing what we do below. | |
734 | if ( !env->env.empty() ) | |
735 | { | |
736 | wxEnvVariableHashMap oldenv; | |
737 | wxGetEnvMap(&oldenv); | |
738 | ||
739 | // Remove unwanted variables | |
740 | wxEnvVariableHashMap::const_iterator it; | |
741 | for ( it = oldenv.begin(); it != oldenv.end(); ++it ) | |
742 | { | |
743 | if ( env->env.find(it->first) == env->env.end() ) | |
744 | wxUnsetEnv(it->first); | |
745 | } | |
746 | ||
747 | // And add the new ones (possibly replacing the old values) | |
748 | for ( it = env->env.begin(); it != env->env.end(); ++it ) | |
749 | wxSetEnv(it->first, it->second); | |
750 | } | |
751 | } | |
752 | ||
05718a98 | 753 | execvp(*argv, argv); |
17a1ebd1 | 754 | |
d264d709 | 755 | fprintf(stderr, "execvp("); |
05718a98 VZ |
756 | for ( char **a = argv; *a; a++ ) |
757 | fprintf(stderr, "%s%s", a == argv ? "" : ", ", *a); | |
d264d709 VZ |
758 | fprintf(stderr, ") failed with error %d!\n", errno); |
759 | ||
518b5d2f | 760 | // there is no return after successful exec() |
518b5d2f | 761 | _exit(-1); |
1d8dd65e VZ |
762 | |
763 | // some compilers complain about missing return - of course, they | |
764 | // should know that exit() doesn't return but what else can we do if | |
765 | // they don't? | |
b477f956 VZ |
766 | // |
767 | // and, sure enough, other compilers complain about unreachable code | |
768 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
769 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
770 | return 0; | |
771 | #endif | |
518b5d2f | 772 | } |
cd6ce4a9 | 773 | else // we're in parent |
518b5d2f | 774 | { |
821d856a | 775 | execData.OnStart(pid); |
7764f973 | 776 | |
80d6dc0a VZ |
777 | // prepare for IO redirection |
778 | ||
f895c3fc | 779 | #if HAS_PIPE_STREAMS |
0e300ddd | 780 | |
cd6ce4a9 VZ |
781 | if ( process && process->IsRedirected() ) |
782 | { | |
3fee3116 VZ |
783 | // Avoid deadlocks which could result from trying to write to the |
784 | // child input pipe end while the child itself is writing to its | |
785 | // output end and waiting for us to read from it. | |
786 | if ( !pipeIn.MakeNonBlocking(wxPipe::Write) ) | |
787 | { | |
788 | // This message is not terrible useful for the user but what | |
789 | // else can we do? Also, should we fail here or take the risk | |
790 | // to continue and deadlock? Currently we choose the latter but | |
791 | // it might not be the best idea. | |
792 | wxLogSysError(_("Failed to set up non-blocking pipe, " | |
793 | "the program might hang.")); | |
736722fb | 794 | #if wxUSE_LOG |
3b816097 | 795 | wxLog::FlushActive(); |
736722fb | 796 | #endif |
3fee3116 VZ |
797 | } |
798 | ||
80d6dc0a | 799 | wxOutputStream *inStream = |
3b816097 | 800 | new wxPipeOutputStream(pipeIn.Detach(wxPipe::Write)); |
b477f956 | 801 | |
33343395 VZ |
802 | const int fdOut = pipeOut.Detach(wxPipe::Read); |
803 | wxPipeInputStream *outStream = new wxPipeInputStream(fdOut); | |
b477f956 | 804 | |
33343395 VZ |
805 | const int fdErr = pipeErr.Detach(wxPipe::Read); |
806 | wxPipeInputStream *errStream = new wxPipeInputStream(fdErr); | |
f6bcfd97 | 807 | |
80d6dc0a | 808 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 809 | |
821d856a VZ |
810 | if ( flags & wxEXEC_SYNC ) |
811 | { | |
812 | execData.bufOut.Init(outStream); | |
813 | execData.bufErr.Init(errStream); | |
33343395 | 814 | |
821d856a VZ |
815 | execData.fdOut = fdOut; |
816 | execData.fdErr = fdErr; | |
817 | } | |
b477f956 | 818 | } |
f895c3fc | 819 | #endif // HAS_PIPE_STREAMS |
1e6feb95 | 820 | |
b477f956 VZ |
821 | if ( pipeIn.IsOk() ) |
822 | { | |
823 | pipeIn.Close(); | |
824 | pipeOut.Close(); | |
825 | pipeErr.Close(); | |
cd6ce4a9 VZ |
826 | } |
827 | ||
821d856a VZ |
828 | // For the asynchronous case we don't have to do anything else, just |
829 | // let the process run. | |
830 | if ( !(flags & wxEXEC_SYNC) ) | |
831 | { | |
832 | // Ensure that the housekeeping data is kept alive, it will be | |
833 | // destroyed only when the child terminates. | |
834 | execDataPtr.release(); | |
835 | ||
836 | return execData.pid; | |
837 | } | |
838 | ||
05718a98 | 839 | |
821d856a VZ |
840 | // If we don't need to dispatch any events, things are relatively |
841 | // simple and we don't need to delegate to wxAppTraits. | |
842 | if ( flags & wxEXEC_NOEVENTS ) | |
843 | { | |
844 | return BlockUntilChildExit(execData); | |
845 | } | |
846 | ||
847 | ||
848 | // If we do need to dispatch events, enter a local event loop waiting | |
849 | // until the child exits. As the exact kind of event loop depends on | |
850 | // the sort of application we're in (console or GUI), we delegate this | |
851 | // to wxAppTraits which virtualizes all the differences between the | |
852 | // console and the GUI programs. | |
853 | return wxApp::GetValidTraits().WaitForChild(execData); | |
518b5d2f | 854 | } |
79656e30 | 855 | |
17a1ebd1 | 856 | #if !defined(__VMS) && !defined(__INTEL_COMPILER) |
79656e30 | 857 | return ERROR_RETURN_CODE; |
ef0ed19e | 858 | #endif |
17a1ebd1 | 859 | } |
518b5d2f | 860 | |
accb3257 | 861 | #undef ERROR_RETURN_CODE |
f6bcfd97 | 862 | |
518b5d2f VZ |
863 | // ---------------------------------------------------------------------------- |
864 | // file and directory functions | |
865 | // ---------------------------------------------------------------------------- | |
866 | ||
05079acc | 867 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f | 868 | { |
14d63513 | 869 | *home = wxGetUserHome(); |
79066131 | 870 | wxString tmp; |
8ea92b4d | 871 | if ( home->empty() ) |
223d09f6 | 872 | *home = wxT("/"); |
181cbcf4 | 873 | #ifdef __VMS |
79066131 VZ |
874 | tmp = *home; |
875 | if ( tmp.Last() != wxT(']')) | |
876 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 877 | #endif |
518b5d2f VZ |
878 | return home->c_str(); |
879 | } | |
880 | ||
14d63513 | 881 | wxString wxGetUserHome( const wxString &user ) |
518b5d2f VZ |
882 | { |
883 | struct passwd *who = (struct passwd *) NULL; | |
884 | ||
0fb67cd1 | 885 | if ( !user ) |
518b5d2f | 886 | { |
e90c1d2a | 887 | wxChar *ptr; |
518b5d2f | 888 | |
223d09f6 | 889 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f VZ |
890 | { |
891 | return ptr; | |
892 | } | |
14d63513 VZ |
893 | |
894 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || | |
895 | (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) | |
518b5d2f | 896 | { |
69c928ef | 897 | who = getpwnam(wxSafeConvertWX2MB(ptr)); |
518b5d2f VZ |
898 | } |
899 | ||
14d63513 VZ |
900 | // make sure the user exists! |
901 | if ( !who ) | |
518b5d2f VZ |
902 | { |
903 | who = getpwuid(getuid()); | |
904 | } | |
905 | } | |
906 | else | |
907 | { | |
05079acc | 908 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
909 | } |
910 | ||
69c928ef | 911 | return wxSafeConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
912 | } |
913 | ||
914 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 915 | // network and user id routines |
518b5d2f VZ |
916 | // ---------------------------------------------------------------------------- |
917 | ||
8bb6b2c0 VZ |
918 | // private utility function which returns output of the given command, removing |
919 | // the trailing newline | |
920 | static wxString wxGetCommandOutput(const wxString &cmd) | |
921 | { | |
8ea079b3 VZ |
922 | // Suppress stderr from the shell to avoid outputting errors if the command |
923 | // doesn't exist. | |
82e8b0ad | 924 | FILE *f = popen((cmd + " 2>/dev/null").ToAscii(), "r"); |
8bb6b2c0 VZ |
925 | if ( !f ) |
926 | { | |
8ea079b3 VZ |
927 | // Notice that this doesn't happen simply if the command doesn't exist, |
928 | // but only in case of some really catastrophic failure inside popen() | |
929 | // so we should really notify the user about this as this is not normal. | |
930 | wxLogSysError(wxT("Executing \"%s\" failed"), cmd); | |
931 | return wxString(); | |
8bb6b2c0 VZ |
932 | } |
933 | ||
934 | wxString s; | |
935 | char buf[256]; | |
936 | while ( !feof(f) ) | |
937 | { | |
938 | if ( !fgets(buf, sizeof(buf), f) ) | |
939 | break; | |
940 | ||
941 | s += wxString::FromAscii(buf); | |
942 | } | |
943 | ||
944 | pclose(f); | |
945 | ||
9a83f860 | 946 | if ( !s.empty() && s.Last() == wxT('\n') ) |
8bb6b2c0 VZ |
947 | s.RemoveLast(); |
948 | ||
949 | return s; | |
950 | } | |
951 | ||
0fb67cd1 VZ |
952 | // retrieve either the hostname or FQDN depending on platform (caller must |
953 | // check whether it's one or the other, this is why this function is for | |
954 | // private use only) | |
05079acc | 955 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 956 | { |
9d8aca48 | 957 | wxCHECK_MSG( buf, false, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 958 | |
223d09f6 | 959 | *buf = wxT('\0'); |
518b5d2f VZ |
960 | |
961 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
962 | #if defined(HAVE_UNAME) | |
cc743a6f | 963 | struct utsname uts; |
518b5d2f VZ |
964 | bool ok = uname(&uts) != -1; |
965 | if ( ok ) | |
966 | { | |
e408bf52 | 967 | wxStrlcpy(buf, wxSafeConvertMB2WX(uts.nodename), sz); |
518b5d2f VZ |
968 | } |
969 | #elif defined(HAVE_GETHOSTNAME) | |
1870f50b RD |
970 | char cbuf[sz]; |
971 | bool ok = gethostname(cbuf, sz) != -1; | |
972 | if ( ok ) | |
973 | { | |
e408bf52 | 974 | wxStrlcpy(buf, wxSafeConvertMB2WX(cbuf), sz); |
1870f50b | 975 | } |
0fb67cd1 | 976 | #else // no uname, no gethostname |
223d09f6 | 977 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f | 978 | |
9d8aca48 | 979 | bool ok = false; |
0fb67cd1 | 980 | #endif // uname/gethostname |
518b5d2f VZ |
981 | |
982 | if ( !ok ) | |
983 | { | |
984 | wxLogSysError(_("Cannot get the hostname")); | |
985 | } | |
986 | ||
987 | return ok; | |
988 | } | |
989 | ||
05079acc | 990 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
991 | { |
992 | bool ok = wxGetHostNameInternal(buf, sz); | |
993 | ||
994 | if ( ok ) | |
995 | { | |
996 | // BSD systems return the FQDN, we only want the hostname, so extract | |
997 | // it (we consider that dots are domain separators) | |
223d09f6 | 998 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
999 | if ( dot ) |
1000 | { | |
1001 | // nuke it | |
223d09f6 | 1002 | *dot = wxT('\0'); |
0fb67cd1 VZ |
1003 | } |
1004 | } | |
1005 | ||
1006 | return ok; | |
1007 | } | |
1008 | ||
05079acc | 1009 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
1010 | { |
1011 | bool ok = wxGetHostNameInternal(buf, sz); | |
1012 | ||
1013 | if ( ok ) | |
1014 | { | |
223d09f6 | 1015 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 1016 | { |
69c928ef | 1017 | struct hostent *host = gethostbyname(wxSafeConvertWX2MB(buf)); |
0fb67cd1 VZ |
1018 | if ( !host ) |
1019 | { | |
1020 | wxLogSysError(_("Cannot get the official hostname")); | |
1021 | ||
9d8aca48 | 1022 | ok = false; |
0fb67cd1 VZ |
1023 | } |
1024 | else | |
1025 | { | |
1026 | // the canonical name | |
e408bf52 | 1027 | wxStrlcpy(buf, wxSafeConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
1028 | } |
1029 | } | |
1030 | //else: it's already a FQDN (BSD behaves this way) | |
1031 | } | |
1032 | ||
1033 | return ok; | |
1034 | } | |
1035 | ||
05079acc | 1036 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
1037 | { |
1038 | struct passwd *who; | |
1039 | ||
223d09f6 | 1040 | *buf = wxT('\0'); |
518b5d2f VZ |
1041 | if ((who = getpwuid(getuid ())) != NULL) |
1042 | { | |
e408bf52 | 1043 | wxStrlcpy (buf, wxSafeConvertMB2WX(who->pw_name), sz); |
9d8aca48 | 1044 | return true; |
518b5d2f VZ |
1045 | } |
1046 | ||
9d8aca48 | 1047 | return false; |
518b5d2f VZ |
1048 | } |
1049 | ||
05079acc | 1050 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f | 1051 | { |
69c928ef | 1052 | #ifdef HAVE_PW_GECOS |
518b5d2f | 1053 | struct passwd *who; |
518b5d2f | 1054 | |
223d09f6 | 1055 | *buf = wxT('\0'); |
b12915c1 VZ |
1056 | if ((who = getpwuid (getuid ())) != NULL) |
1057 | { | |
b12915c1 | 1058 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
1059 | if (comma) |
1060 | *comma = '\0'; // cut off non-name comment fields | |
e408bf52 | 1061 | wxStrlcpy(buf, wxSafeConvertMB2WX(who->pw_gecos), sz); |
9d8aca48 | 1062 | return true; |
518b5d2f VZ |
1063 | } |
1064 | ||
9d8aca48 | 1065 | return false; |
69c928ef VZ |
1066 | #else // !HAVE_PW_GECOS |
1067 | return wxGetUserId(buf, sz); | |
1068 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS | |
518b5d2f VZ |
1069 | } |
1070 | ||
c655557f VZ |
1071 | bool wxIsPlatform64Bit() |
1072 | { | |
2f6aa043 VZ |
1073 | const wxString machine = wxGetCommandOutput(wxT("uname -m")); |
1074 | ||
1075 | // the test for "64" is obviously not 100% reliable but seems to work fine | |
1076 | // in practice | |
1077 | return machine.Contains(wxT("64")) || | |
1078 | machine.Contains(wxT("alpha")); | |
c655557f VZ |
1079 | } |
1080 | ||
23790a2a FM |
1081 | #ifdef __LINUX__ |
1082 | wxLinuxDistributionInfo wxGetLinuxDistributionInfo() | |
1083 | { | |
1084 | const wxString id = wxGetCommandOutput(wxT("lsb_release --id")); | |
1085 | const wxString desc = wxGetCommandOutput(wxT("lsb_release --description")); | |
1086 | const wxString rel = wxGetCommandOutput(wxT("lsb_release --release")); | |
1087 | const wxString codename = wxGetCommandOutput(wxT("lsb_release --codename")); | |
03647350 | 1088 | |
23790a2a | 1089 | wxLinuxDistributionInfo ret; |
03647350 | 1090 | |
23790a2a FM |
1091 | id.StartsWith("Distributor ID:\t", &ret.Id); |
1092 | desc.StartsWith("Description:\t", &ret.Description); | |
1093 | rel.StartsWith("Release:\t", &ret.Release); | |
1094 | codename.StartsWith("Codename:\t", &ret.CodeName); | |
1095 | ||
1096 | return ret; | |
1097 | } | |
1098 | #endif | |
1099 | ||
50a2e26f | 1100 | // these functions are in src/osx/utilsexc_base.cpp for wxMac |
da0ee16e | 1101 | #ifndef __DARWIN__ |
c655557f | 1102 | |
8bb6b2c0 VZ |
1103 | wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) |
1104 | { | |
1105 | // get OS version | |
1106 | int major, minor; | |
1107 | wxString release = wxGetCommandOutput(wxT("uname -r")); | |
86501081 VS |
1108 | if ( release.empty() || |
1109 | wxSscanf(release.c_str(), wxT("%d.%d"), &major, &minor) != 2 ) | |
8bb6b2c0 | 1110 | { |
e1379e29 | 1111 | // failed to get version string or unrecognized format |
8bb6b2c0 VZ |
1112 | major = |
1113 | minor = -1; | |
1114 | } | |
1115 | ||
1116 | if ( verMaj ) | |
1117 | *verMaj = major; | |
1118 | if ( verMin ) | |
1119 | *verMin = minor; | |
1120 | ||
1121 | // try to understand which OS are we running | |
1122 | wxString kernel = wxGetCommandOutput(wxT("uname -s")); | |
1123 | if ( kernel.empty() ) | |
1124 | kernel = wxGetCommandOutput(wxT("uname -o")); | |
1125 | ||
1126 | if ( kernel.empty() ) | |
1127 | return wxOS_UNKNOWN; | |
1128 | ||
1129 | return wxPlatformInfo::GetOperatingSystemId(kernel); | |
1130 | } | |
1131 | ||
bdc72a22 VZ |
1132 | wxString wxGetOsDescription() |
1133 | { | |
8bb6b2c0 | 1134 | return wxGetCommandOutput(wxT("uname -s -r -m")); |
bdc72a22 VZ |
1135 | } |
1136 | ||
da0ee16e | 1137 | #endif // !__DARWIN__ |
bd3277fe | 1138 | |
c1cb4153 VZ |
1139 | unsigned long wxGetProcessId() |
1140 | { | |
1141 | return (unsigned long)getpid(); | |
1142 | } | |
1143 | ||
9d8aca48 | 1144 | wxMemorySize wxGetFreeMemory() |
bd3277fe VZ |
1145 | { |
1146 | #if defined(__LINUX__) | |
1147 | // get it from /proc/meminfo | |
1148 | FILE *fp = fopen("/proc/meminfo", "r"); | |
1149 | if ( fp ) | |
1150 | { | |
1151 | long memFree = -1; | |
1152 | ||
1153 | char buf[1024]; | |
1154 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
1155 | { | |
7c28d921 VZ |
1156 | // /proc/meminfo changed its format in kernel 2.6 |
1157 | if ( wxPlatformInfo().CheckOSVersion(2, 6) ) | |
1158 | { | |
1159 | unsigned long cached, buffers; | |
1160 | sscanf(buf, "MemFree: %ld", &memFree); | |
1161 | ||
1162 | fgets(buf, WXSIZEOF(buf), fp); | |
1163 | sscanf(buf, "Buffers: %lu", &buffers); | |
1164 | ||
1165 | fgets(buf, WXSIZEOF(buf), fp); | |
1166 | sscanf(buf, "Cached: %lu", &cached); | |
1167 | ||
1168 | // add to "MemFree" also the "Buffers" and "Cached" values as | |
1169 | // free(1) does as otherwise the value never makes sense: for | |
1170 | // kernel 2.6 it's always almost 0 | |
1171 | memFree += buffers + cached; | |
1172 | ||
1173 | // values here are always expressed in kB and we want bytes | |
1174 | memFree *= 1024; | |
1175 | } | |
1176 | else // Linux 2.4 (or < 2.6, anyhow) | |
1177 | { | |
1178 | long memTotal, memUsed; | |
1179 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
1180 | } | |
bd3277fe VZ |
1181 | } |
1182 | ||
1183 | fclose(fp); | |
1184 | ||
9d8aca48 | 1185 | return (wxMemorySize)memFree; |
bd3277fe | 1186 | } |
9ad34f61 VZ |
1187 | #elif defined(__SGI__) |
1188 | struct rminfo realmem; | |
1189 | if ( sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0 ) | |
1190 | return ((wxMemorySize)realmem.physmem * sysconf(_SC_PAGESIZE)); | |
40f00746 VZ |
1191 | #elif defined(_SC_AVPHYS_PAGES) |
1192 | return ((wxMemorySize)sysconf(_SC_AVPHYS_PAGES))*sysconf(_SC_PAGESIZE); | |
bd3277fe VZ |
1193 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably |
1194 | #endif | |
1195 | ||
1196 | // can't find it out | |
1197 | return -1; | |
1198 | } | |
1199 | ||
7ba7c4e6 | 1200 | bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree) |
eadd7bd2 | 1201 | { |
9952adac | 1202 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 1203 | // the case to "char *" is needed for AIX 4.3 |
85da04e9 VZ |
1204 | wxStatfs_t fs; |
1205 | if ( wxStatfs((char *)(const char*)path.fn_str(), &fs) != 0 ) | |
eadd7bd2 | 1206 | { |
401eb3de | 1207 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 | 1208 | |
9d8aca48 | 1209 | return false; |
eadd7bd2 VZ |
1210 | } |
1211 | ||
125cb99b VZ |
1212 | // under Solaris we also have to use f_frsize field instead of f_bsize |
1213 | // which is in general a multiple of f_frsize | |
1214 | #ifdef HAVE_STATVFS | |
7ba7c4e6 | 1215 | wxDiskspaceSize_t blockSize = fs.f_frsize; |
125cb99b | 1216 | #else // HAVE_STATFS |
7ba7c4e6 | 1217 | wxDiskspaceSize_t blockSize = fs.f_bsize; |
125cb99b | 1218 | #endif // HAVE_STATVFS/HAVE_STATFS |
9952adac | 1219 | |
eadd7bd2 VZ |
1220 | if ( pTotal ) |
1221 | { | |
7ba7c4e6 | 1222 | *pTotal = wxDiskspaceSize_t(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
1223 | } |
1224 | ||
1225 | if ( pFree ) | |
1226 | { | |
7ba7c4e6 | 1227 | *pFree = wxDiskspaceSize_t(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
1228 | } |
1229 | ||
9d8aca48 | 1230 | return true; |
125cb99b | 1231 | #else // !HAVE_STATFS && !HAVE_STATVFS |
9d8aca48 | 1232 | return false; |
125cb99b | 1233 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
1234 | } |
1235 | ||
8fd0d89b VZ |
1236 | // ---------------------------------------------------------------------------- |
1237 | // env vars | |
1238 | // ---------------------------------------------------------------------------- | |
1239 | ||
c0472c7c VZ |
1240 | #if USE_PUTENV |
1241 | ||
1242 | WX_DECLARE_STRING_HASH_MAP(char *, wxEnvVars); | |
1243 | ||
1244 | static wxEnvVars gs_envVars; | |
1245 | ||
1246 | class wxSetEnvModule : public wxModule | |
1247 | { | |
1248 | public: | |
1249 | virtual bool OnInit() { return true; } | |
1250 | virtual void OnExit() | |
1251 | { | |
1252 | for ( wxEnvVars::const_iterator i = gs_envVars.begin(); | |
1253 | i != gs_envVars.end(); | |
1254 | ++i ) | |
1255 | { | |
1256 | free(i->second); | |
1257 | } | |
1258 | ||
1259 | gs_envVars.clear(); | |
1260 | } | |
1261 | ||
1262 | DECLARE_DYNAMIC_CLASS(wxSetEnvModule) | |
1263 | }; | |
1264 | ||
1265 | IMPLEMENT_DYNAMIC_CLASS(wxSetEnvModule, wxModule) | |
1266 | ||
1267 | #endif // USE_PUTENV | |
1268 | ||
97b305b7 | 1269 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
1270 | { |
1271 | // wxGetenv is defined as getenv() | |
86501081 | 1272 | char *p = wxGetenv(var); |
308978f6 | 1273 | if ( !p ) |
9d8aca48 | 1274 | return false; |
308978f6 VZ |
1275 | |
1276 | if ( value ) | |
1277 | { | |
1278 | *value = p; | |
1279 | } | |
1280 | ||
9d8aca48 | 1281 | return true; |
308978f6 VZ |
1282 | } |
1283 | ||
90a77e64 | 1284 | static bool wxDoSetEnv(const wxString& variable, const char *value) |
8fd0d89b VZ |
1285 | { |
1286 | #if defined(HAVE_SETENV) | |
a1353ea4 VZ |
1287 | if ( !value ) |
1288 | { | |
1289 | #ifdef HAVE_UNSETENV | |
65fd2fc2 VZ |
1290 | // don't test unsetenv() return value: it's void on some systems (at |
1291 | // least Darwin) | |
1292 | unsetenv(variable.mb_str()); | |
022a8d02 | 1293 | return true; |
a1353ea4 VZ |
1294 | #else |
1295 | value = ""; // we can't pass NULL to setenv() | |
1296 | #endif | |
1297 | } | |
1298 | ||
90a77e64 | 1299 | return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0; |
8fd0d89b VZ |
1300 | #elif defined(HAVE_PUTENV) |
1301 | wxString s = variable; | |
1302 | if ( value ) | |
9a83f860 | 1303 | s << wxT('=') << value; |
8fd0d89b VZ |
1304 | |
1305 | // transform to ANSI | |
67479dbd | 1306 | const wxWX2MBbuf p = s.mb_str(); |
8fd0d89b | 1307 | |
8fd0d89b VZ |
1308 | char *buf = (char *)malloc(strlen(p) + 1); |
1309 | strcpy(buf, p); | |
1310 | ||
c0472c7c VZ |
1311 | // store the string to free() it later |
1312 | wxEnvVars::iterator i = gs_envVars.find(variable); | |
1313 | if ( i != gs_envVars.end() ) | |
1314 | { | |
1315 | free(i->second); | |
1316 | i->second = buf; | |
1317 | } | |
1318 | else // this variable hadn't been set before | |
1319 | { | |
1320 | gs_envVars[variable] = buf; | |
1321 | } | |
1322 | ||
8fd0d89b VZ |
1323 | return putenv(buf) == 0; |
1324 | #else // no way to set an env var | |
67479dbd | 1325 | return false; |
8fd0d89b VZ |
1326 | #endif |
1327 | } | |
1328 | ||
90a77e64 VS |
1329 | bool wxSetEnv(const wxString& variable, const wxString& value) |
1330 | { | |
1331 | return wxDoSetEnv(variable, value.mb_str()); | |
1332 | } | |
1333 | ||
1334 | bool wxUnsetEnv(const wxString& variable) | |
1335 | { | |
1336 | return wxDoSetEnv(variable, NULL); | |
1337 | } | |
1338 | ||
a37a5a73 VZ |
1339 | // ---------------------------------------------------------------------------- |
1340 | // signal handling | |
1341 | // ---------------------------------------------------------------------------- | |
1342 | ||
1343 | #if wxUSE_ON_FATAL_EXCEPTION | |
1344 | ||
1345 | #include <signal.h> | |
1346 | ||
90350682 | 1347 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1348 | { |
1349 | if ( wxTheApp ) | |
1350 | { | |
1351 | // give the user a chance to do something special about this | |
1352 | wxTheApp->OnFatalException(); | |
1353 | } | |
1354 | ||
1355 | abort(); | |
1356 | } | |
1357 | ||
1358 | bool wxHandleFatalExceptions(bool doit) | |
1359 | { | |
1360 | // old sig handlers | |
9d8aca48 | 1361 | static bool s_savedHandlers = false; |
a37a5a73 VZ |
1362 | static struct sigaction s_handlerFPE, |
1363 | s_handlerILL, | |
1364 | s_handlerBUS, | |
1365 | s_handlerSEGV; | |
1366 | ||
9d8aca48 | 1367 | bool ok = true; |
a37a5a73 VZ |
1368 | if ( doit && !s_savedHandlers ) |
1369 | { | |
1370 | // install the signal handler | |
1371 | struct sigaction act; | |
1372 | ||
1373 | // some systems extend it with non std fields, so zero everything | |
1374 | memset(&act, 0, sizeof(act)); | |
1375 | ||
1376 | act.sa_handler = wxFatalSignalHandler; | |
1377 | sigemptyset(&act.sa_mask); | |
1378 | act.sa_flags = 0; | |
1379 | ||
1380 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1381 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1382 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1383 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1384 | if ( !ok ) | |
1385 | { | |
9a83f860 | 1386 | wxLogDebug(wxT("Failed to install our signal handler.")); |
a37a5a73 VZ |
1387 | } |
1388 | ||
9d8aca48 | 1389 | s_savedHandlers = true; |
a37a5a73 VZ |
1390 | } |
1391 | else if ( s_savedHandlers ) | |
1392 | { | |
1393 | // uninstall the signal handler | |
1394 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1395 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1396 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1397 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1398 | if ( !ok ) | |
1399 | { | |
9a83f860 | 1400 | wxLogDebug(wxT("Failed to uninstall our signal handler.")); |
a37a5a73 VZ |
1401 | } |
1402 | ||
9d8aca48 | 1403 | s_savedHandlers = false; |
a37a5a73 VZ |
1404 | } |
1405 | //else: nothing to do | |
1406 | ||
1407 | return ok; | |
1408 | } | |
1409 | ||
1410 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1411 | ||
e2478fde VZ |
1412 | // ---------------------------------------------------------------------------- |
1413 | // wxExecute support | |
1414 | // ---------------------------------------------------------------------------- | |
1415 | ||
821d856a | 1416 | int wxAppTraits::WaitForChild(wxExecuteData& execData) |
9d3845e8 | 1417 | { |
ccf8bf20 | 1418 | #if wxUSE_CONSOLE_EVENTLOOP |
821d856a VZ |
1419 | wxConsoleEventLoop loop; |
1420 | return RunLoopUntilChildExit(execData, loop); | |
ccf8bf20 VZ |
1421 | #else // !wxUSE_CONSOLE_EVENTLOOP |
1422 | wxFAIL_MSG( wxS("Can't wait for child process without wxConsoleEventLoop") ); | |
1423 | ||
1424 | return -1; | |
1425 | #endif // wxUSE_CONSOLE_EVENTLOOP/!wxUSE_CONSOLE_EVENTLOOP | |
9d3845e8 VZ |
1426 | } |
1427 | ||
821d856a VZ |
1428 | // This function is common code for both console and GUI applications and used |
1429 | // by wxExecute() to wait for the child exit while dispatching events. | |
dbd1c638 | 1430 | // |
821d856a VZ |
1431 | // Notice that it should not be used for all the other cases, e.g. when we |
1432 | // don't need to wait for the child (wxEXEC_ASYNC) or when the events must not | |
1433 | // dispatched (wxEXEC_NOEVENTS). | |
1434 | int | |
1435 | wxAppTraits::RunLoopUntilChildExit(wxExecuteData& execData, | |
1436 | wxEventLoopBase& loop) | |
33343395 | 1437 | { |
821d856a VZ |
1438 | // It is possible that wxExecuteData::OnExit() had already been called |
1439 | // and reset the PID to 0, in which case we don't need to do anything | |
1440 | // at all. | |
1441 | if ( !execData.pid ) | |
1442 | return execData.exitcode; | |
1443 | ||
1444 | #if wxUSE_STREAMS | |
1445 | // Monitor the child streams if necessary. | |
1446 | wxScopedPtr<wxEventLoopSourceHandler> | |
1447 | stdoutHandler, | |
1448 | stderrHandler; | |
1449 | if ( execData.IsRedirected() ) | |
33343395 | 1450 | { |
821d856a VZ |
1451 | stdoutHandler.reset(new wxExecuteEventLoopSourceHandler |
1452 | ( | |
1453 | execData.fdOut, execData.bufOut | |
1454 | )); | |
1455 | stderrHandler.reset(new wxExecuteEventLoopSourceHandler | |
1456 | ( | |
1457 | execData.fdErr, execData.bufErr | |
1458 | )); | |
33343395 | 1459 | } |
821d856a | 1460 | #endif // wxUSE_STREAMS |
33343395 | 1461 | |
821d856a VZ |
1462 | // Store the event loop in the data associated with the child |
1463 | // process so that it could exit the loop when the child exits. | |
1464 | execData.syncEventLoop = &loop; | |
46c7e1a1 | 1465 | |
821d856a VZ |
1466 | // And run it. |
1467 | loop.Run(); | |
33343395 | 1468 | |
821d856a VZ |
1469 | // The exit code will have been set when the child termination was detected. |
1470 | return execData.exitcode; | |
1471 | } | |
33343395 | 1472 | |
821d856a VZ |
1473 | // ---------------------------------------------------------------------------- |
1474 | // wxExecuteData | |
1475 | // ---------------------------------------------------------------------------- | |
33343395 | 1476 | |
821d856a | 1477 | namespace |
33343395 | 1478 | { |
33343395 | 1479 | |
821d856a VZ |
1480 | // Helper function that checks whether the child with the given PID has exited |
1481 | // and fills the provided parameter with its return code if it did. | |
1482 | bool CheckForChildExit(int pid, int* exitcodeOut) | |
b827d647 VZ |
1483 | { |
1484 | wxASSERT_MSG( pid > 0, "invalid PID" ); | |
1485 | ||
1486 | int status, rc; | |
1487 | ||
1488 | // loop while we're getting EINTR | |
1489 | for ( ;; ) | |
1490 | { | |
821d856a | 1491 | rc = waitpid(pid, &status, WNOHANG); |
b827d647 VZ |
1492 | |
1493 | if ( rc != -1 || errno != EINTR ) | |
1494 | break; | |
1495 | } | |
1496 | ||
821d856a | 1497 | switch ( rc ) |
b827d647 | 1498 | { |
821d856a VZ |
1499 | case 0: |
1500 | // No error but the child is still running. | |
1501 | return false; | |
34e3a810 | 1502 | |
821d856a VZ |
1503 | case -1: |
1504 | // Checking child status failed. Invalid PID? | |
1505 | wxLogLastError(wxString::Format("waitpid(%d)", pid)); | |
1506 | return false; | |
b827d647 | 1507 | |
821d856a VZ |
1508 | default: |
1509 | // Child did terminate. | |
1510 | wxASSERT_MSG( rc == pid, "unexpected waitpid() return value" ); | |
1511 | ||
1512 | // notice that the caller expects the exit code to be signed, e.g. -1 | |
1513 | // instead of 255 so don't assign WEXITSTATUS() to an int | |
1514 | signed char exitcode; | |
1515 | if ( WIFEXITED(status) ) | |
1516 | exitcode = WEXITSTATUS(status); | |
1517 | else if ( WIFSIGNALED(status) ) | |
1518 | exitcode = -WTERMSIG(status); | |
1519 | else | |
1520 | { | |
1521 | wxLogError("Child process (PID %d) exited for unknown reason, " | |
1522 | "status = %d", pid, status); | |
1523 | exitcode = -1; | |
1524 | } | |
1525 | ||
1526 | if ( exitcodeOut ) | |
1527 | *exitcodeOut = exitcode; | |
1528 | ||
1529 | return true; | |
1530 | } | |
b827d647 VZ |
1531 | } |
1532 | ||
1533 | } // anonymous namespace | |
1534 | ||
821d856a VZ |
1535 | wxExecuteData::ChildProcessesData wxExecuteData::ms_childProcesses; |
1536 | ||
1537 | /* static */ | |
1538 | void wxExecuteData::OnSomeChildExited(int WXUNUSED(sig)) | |
33343395 | 1539 | { |
821d856a VZ |
1540 | // We know that some child process has terminated, but we don't know which |
1541 | // one, so check all of them (notice that more than one could have exited). | |
1542 | // | |
1543 | // An alternative approach would be to call waitpid(-1, &status, WNOHANG) | |
1544 | // (in a loop to take care of the multiple children exiting case) and | |
1545 | // perhaps this would be more efficient. But for now this seems to work. | |
33343395 | 1546 | |
821d856a VZ |
1547 | |
1548 | // Make a copy of the list before iterating over it to avoid problems due | |
1549 | // to deleting entries from it in the process. | |
1550 | const ChildProcessesData allChildProcesses = ms_childProcesses; | |
1551 | for ( ChildProcessesData::const_iterator it = allChildProcesses.begin(); | |
1552 | it != allChildProcesses.end(); | |
1553 | ++it ) | |
33343395 | 1554 | { |
821d856a VZ |
1555 | const int pid = it->first; |
1556 | ||
1557 | // Check whether this child exited. | |
1558 | int exitcode; | |
1559 | if ( !CheckForChildExit(pid, &exitcode) ) | |
1560 | continue; | |
1561 | ||
1562 | // And handle its termination if it did. | |
33343395 | 1563 | // |
821d856a VZ |
1564 | // Notice that this will implicitly remove it from ms_childProcesses. |
1565 | it->second->OnExit(exitcode); | |
1566 | } | |
1567 | } | |
33343395 | 1568 | |
821d856a VZ |
1569 | void wxExecuteData::OnStart(int pid_) |
1570 | { | |
1571 | wxCHECK_RET( wxTheApp, | |
1572 | wxS("Ensure wxTheApp is set before calling wxExecute()") ); | |
33343395 | 1573 | |
821d856a VZ |
1574 | // Setup the signal handler for SIGCHLD to be able to detect the child |
1575 | // termination. | |
1576 | // | |
1577 | // Notice that SetSignalHandler() is idempotent, so it's fine to call | |
1578 | // it more than once with the same handler. | |
1579 | wxTheApp->SetSignalHandler(SIGCHLD, OnSomeChildExited); | |
33343395 | 1580 | |
33343395 | 1581 | |
821d856a VZ |
1582 | // Remember the child PID to be able to wait for it later. |
1583 | pid = pid_; | |
1584 | ||
1585 | // Also save it in wxProcess where it will be accessible to the user code. | |
1586 | if ( process ) | |
1587 | process->SetPid(pid); | |
1588 | ||
1589 | // Finally, add this object itself to the list of child processes so that | |
1590 | // we can check for its termination the next time we get SIGCHLD. | |
1591 | ms_childProcesses[pid] = this; | |
46c7e1a1 | 1592 | } |
46c7e1a1 | 1593 | |
821d856a | 1594 | void wxExecuteData::OnExit(int exitcode_) |
e2478fde | 1595 | { |
821d856a VZ |
1596 | // Remove this process from the hash list of child processes that are |
1597 | // still open as soon as possible to ensure we don't process it again even | |
1598 | // if another SIGCHLD happens. | |
1599 | if ( !ms_childProcesses.erase(pid) ) | |
1600 | { | |
1601 | wxFAIL_MSG(wxString::Format(wxS("Data for PID %d not in the list?"), pid)); | |
1602 | } | |
1603 | ||
b827d647 | 1604 | |
821d856a VZ |
1605 | exitcode = exitcode_; |
1606 | ||
1607 | #if wxUSE_STREAMS | |
1608 | if ( IsRedirected() ) | |
e2478fde | 1609 | { |
821d856a VZ |
1610 | // Read the remaining data in a blocking way: this is fine because the |
1611 | // child has already exited and hence all the data must be already | |
1612 | // available in the streams buffers. | |
1613 | bufOut.ReadAll(); | |
1614 | bufErr.ReadAll(); | |
e2478fde | 1615 | } |
821d856a | 1616 | #endif // wxUSE_STREAMS |
e2478fde | 1617 | |
821d856a VZ |
1618 | // Notify user about termination if required |
1619 | if ( !(flags & wxEXEC_SYNC) ) | |
e528a71b | 1620 | { |
821d856a VZ |
1621 | if ( process ) |
1622 | process->OnTerminate(pid, exitcode); | |
1623 | ||
1624 | // in case of asynchronous execution we don't need this object any more | |
e528a71b | 1625 | // after the child terminates |
821d856a | 1626 | delete this; |
e528a71b VZ |
1627 | } |
1628 | else // sync execution | |
1629 | { | |
1630 | // let wxExecute() know that the process has terminated | |
821d856a VZ |
1631 | pid = 0; |
1632 | ||
1633 | // Stop the event loop for synchronous wxExecute() if we're running one. | |
1634 | if ( syncEventLoop ) | |
1635 | syncEventLoop->ScheduleExit(); | |
e528a71b | 1636 | } |
e2478fde | 1637 | } |