]>
Commit | Line | Data |
---|---|---|
518b5d2f | 1 | ///////////////////////////////////////////////////////////////////////////// |
7520f3da | 2 | // Name: src/unix/utilsunx.cpp |
518b5d2f VZ |
3 | // Purpose: generic Unix implementation of many wx functions |
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 | ||
7520f3da WS |
21 | #ifndef WX_PRECOMP |
22 | #include "wx/string.h" | |
23 | #endif | |
518b5d2f VZ |
24 | |
25 | #include "wx/intl.h" | |
26 | #include "wx/log.h" | |
a37a5a73 | 27 | #include "wx/app.h" |
46446cc2 | 28 | #include "wx/apptrait.h" |
518b5d2f VZ |
29 | |
30 | #include "wx/utils.h" | |
31 | #include "wx/process.h" | |
bdc72a22 | 32 | #include "wx/thread.h" |
518b5d2f | 33 | |
80d6dc0a | 34 | #include "wx/wfstream.h" |
8b33ae2d | 35 | |
e2478fde | 36 | #include "wx/unix/execute.h" |
17a1ebd1 VZ |
37 | #include "wx/unix/private.h" |
38 | ||
39 | #include <pwd.h> | |
e2478fde | 40 | |
2887179b VZ |
41 | #if wxUSE_STREAMS |
42 | ||
43 | // define this to let wxexec.cpp know that we know what we're doing | |
44 | #define _WX_USED_BY_WXEXECUTE_ | |
45 | #include "../common/execcmn.cpp" | |
46 | ||
47 | #endif // wxUSE_STREAMS | |
48 | ||
ec67cff1 | 49 | #if wxUSE_BASE |
e2478fde | 50 | |
74c719ed VZ |
51 | #if defined(__MWERKS__) && defined(__MACH__) |
52 | #ifndef WXWIN_OS_DESCRIPTION | |
53 | #define WXWIN_OS_DESCRIPTION "MacOS X" | |
54 | #endif | |
55 | #ifndef HAVE_NANOSLEEP | |
56 | #define HAVE_NANOSLEEP | |
57 | #endif | |
58 | #ifndef HAVE_UNAME | |
59 | #define HAVE_UNAME | |
60 | #endif | |
61 | ||
62 | // our configure test believes we can use sigaction() if the function is | |
63 | // available but Metrowekrs with MSL run-time does have the function but | |
64 | // doesn't have sigaction struct so finally we can't use it... | |
65 | #ifdef __MSL__ | |
66 | #undef wxUSE_ON_FATAL_EXCEPTION | |
67 | #define wxUSE_ON_FATAL_EXCEPTION 0 | |
68 | #endif | |
8d4f85ce SC |
69 | #endif |
70 | ||
85da04e9 VZ |
71 | // not only the statfs syscall is called differently depending on platform, but |
72 | // one of its incarnations, statvfs(), takes different arguments under | |
73 | // different platforms and even different versions of the same system (Solaris | |
74 | // 7 and 8): if you want to test for this, don't forget that the problems only | |
75 | // appear if the large files support is enabled | |
eadd7bd2 | 76 | #ifdef HAVE_STATFS |
85da04e9 VZ |
77 | #ifdef __BSD__ |
78 | #include <sys/param.h> | |
79 | #include <sys/mount.h> | |
80 | #else // !__BSD__ | |
81 | #include <sys/vfs.h> | |
82 | #endif // __BSD__/!__BSD__ | |
83 | ||
84 | #define wxStatfs statfs | |
84ae7ca4 VZ |
85 | |
86 | #ifndef HAVE_STATFS_DECL | |
87 | // some systems lack statfs() prototype in the system headers (AIX 4) | |
88 | extern "C" int statfs(const char *path, struct statfs *buf); | |
89 | #endif | |
eadd7bd2 VZ |
90 | #endif // HAVE_STATFS |
91 | ||
9952adac VZ |
92 | #ifdef HAVE_STATVFS |
93 | #include <sys/statvfs.h> | |
94 | ||
85da04e9 VZ |
95 | #define wxStatfs statvfs |
96 | #endif // HAVE_STATVFS | |
97 | ||
98 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) | |
99 | // WX_STATFS_T is detected by configure | |
100 | #define wxStatfs_t WX_STATFS_T | |
101 | #endif | |
9952adac | 102 | |
f6bcfd97 BP |
103 | // SGI signal.h defines signal handler arguments differently depending on |
104 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
105 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
106 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
107 | #endif // SGI hack | |
108 | ||
518b5d2f VZ |
109 | #include <stdarg.h> |
110 | #include <dirent.h> | |
111 | #include <string.h> | |
112 | #include <sys/stat.h> | |
113 | #include <sys/types.h> | |
518b5d2f | 114 | #include <sys/wait.h> |
e2478fde | 115 | #include <unistd.h> |
518b5d2f VZ |
116 | #include <errno.h> |
117 | #include <netdb.h> | |
118 | #include <signal.h> | |
119 | #include <fcntl.h> // for O_WRONLY and friends | |
120 | #include <time.h> // nanosleep() and/or usleep() | |
fad866f4 | 121 | #include <ctype.h> // isspace() |
b12915c1 | 122 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 123 | |
0fcdf6dc | 124 | #ifdef HAVE_UNAME |
518b5d2f VZ |
125 | #include <sys/utsname.h> // for uname() |
126 | #endif // HAVE_UNAME | |
127 | ||
9ad34f61 VZ |
128 | // Used by wxGetFreeMemory(). |
129 | #ifdef __SGI__ | |
130 | #include <sys/sysmp.h> | |
131 | #include <sys/sysinfo.h> // for SAGET and MINFO structures | |
132 | #endif | |
133 | ||
518b5d2f VZ |
134 | // ---------------------------------------------------------------------------- |
135 | // conditional compilation | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | // many versions of Unices have this function, but it is not defined in system | |
139 | // headers - please add your system here if it is the case for your OS. | |
140 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b | 141 | #if !defined(HAVE_USLEEP) && \ |
d67fee71 | 142 | ((defined(__SUN__) && !defined(__SunOs_5_6) && \ |
518b5d2f | 143 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
d67fee71 | 144 | defined(__osf__) || defined(__EMX__)) |
518b5d2f VZ |
145 | extern "C" |
146 | { | |
1363811b VZ |
147 | #ifdef __SUN__ |
148 | int usleep(unsigned int usec); | |
149 | #else // !Sun | |
bdc72a22 VZ |
150 | #ifdef __EMX__ |
151 | /* I copied this from the XFree86 diffs. AV. */ | |
152 | #define INCL_DOSPROCESS | |
153 | #include <os2.h> | |
154 | inline void usleep(unsigned long delay) | |
155 | { | |
156 | DosSleep(delay ? (delay/1000l) : 1l); | |
157 | } | |
158 | #else // !Sun && !EMX | |
159 | void usleep(unsigned long usec); | |
160 | #endif | |
e6daf794 | 161 | #endif // Sun/EMX/Something else |
518b5d2f | 162 | }; |
bdc72a22 VZ |
163 | |
164 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
165 | #endif // Unices without usleep() |
166 | ||
518b5d2f VZ |
167 | // ============================================================================ |
168 | // implementation | |
169 | // ============================================================================ | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // sleeping | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | void wxSleep(int nSecs) | |
176 | { | |
177 | sleep(nSecs); | |
178 | } | |
179 | ||
66cd9d7f | 180 | void wxMicroSleep(unsigned long microseconds) |
518b5d2f | 181 | { |
b12915c1 | 182 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 183 | timespec tmReq; |
66cd9d7f VZ |
184 | tmReq.tv_sec = (time_t)(microseconds / 1000000); |
185 | tmReq.tv_nsec = (microseconds % 1000000) * 1000; | |
518b5d2f VZ |
186 | |
187 | // we're not interested in remaining time nor in return value | |
188 | (void)nanosleep(&tmReq, (timespec *)NULL); | |
b12915c1 | 189 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
190 | // uncomment this if you feel brave or if you are sure that your version |
191 | // of Solaris has a safe usleep() function but please notice that usleep() | |
192 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
193 | // documented as MT-Safe | |
ea18eed9 | 194 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
195 | #error "usleep() cannot be used in MT programs under Solaris." |
196 | #endif // Sun | |
197 | ||
66cd9d7f | 198 | usleep(microseconds); |
b12915c1 VZ |
199 | #elif defined(HAVE_SLEEP) |
200 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
66cd9d7f | 201 | sleep(microseconds * 1000000); |
518b5d2f | 202 | #else // !sleep function |
66cd9d7f | 203 | #error "usleep() or nanosleep() function required for wxMicroSleep" |
518b5d2f VZ |
204 | #endif // sleep function |
205 | } | |
206 | ||
66cd9d7f VZ |
207 | void wxMilliSleep(unsigned long milliseconds) |
208 | { | |
209 | wxMicroSleep(milliseconds*1000); | |
210 | } | |
211 | ||
518b5d2f VZ |
212 | // ---------------------------------------------------------------------------- |
213 | // process management | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
e0f6b731 | 216 | int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags) |
518b5d2f | 217 | { |
e0f6b731 | 218 | int err = kill((pid_t) (flags & wxKILL_CHILDREN) ? -pid : pid, (int)sig); |
50567b69 VZ |
219 | if ( rc ) |
220 | { | |
f0bce4d4 | 221 | switch ( err ? errno : 0 ) |
50567b69 VZ |
222 | { |
223 | case 0: | |
224 | *rc = wxKILL_OK; | |
225 | break; | |
226 | ||
227 | case EINVAL: | |
228 | *rc = wxKILL_BAD_SIGNAL; | |
229 | break; | |
230 | ||
231 | case EPERM: | |
232 | *rc = wxKILL_ACCESS_DENIED; | |
233 | break; | |
234 | ||
235 | case ESRCH: | |
236 | *rc = wxKILL_NO_PROCESS; | |
237 | break; | |
238 | ||
239 | default: | |
240 | // this goes against Unix98 docs so log it | |
241 | wxLogDebug(_T("unexpected kill(2) return value %d"), err); | |
242 | ||
243 | // something else... | |
244 | *rc = wxKILL_ERROR; | |
245 | } | |
246 | } | |
247 | ||
248 | return err; | |
518b5d2f VZ |
249 | } |
250 | ||
fad866f4 KB |
251 | #define WXEXECUTE_NARGS 127 |
252 | ||
62705a27 RN |
253 | #if defined(__DARWIN__) |
254 | long wxMacExecute(wxChar **argv, | |
255 | int flags, | |
256 | wxProcess *process); | |
257 | #endif | |
258 | ||
fbf456aa | 259 | long wxExecute( const wxString& command, int flags, wxProcess *process ) |
518b5d2f | 260 | { |
8ea92b4d | 261 | wxCHECK_MSG( !command.empty(), 0, wxT("can't exec empty command") ); |
3bdb628b | 262 | wxLogDebug(wxString(wxT("Launching: ")) + command); |
518b5d2f | 263 | |
647b8e37 VZ |
264 | #if wxUSE_THREADS |
265 | // fork() doesn't mix well with POSIX threads: on many systems the program | |
266 | // deadlocks or crashes for some reason. Probably our code is buggy and | |
267 | // doesn't do something which must be done to allow this to work, but I | |
268 | // don't know what yet, so for now just warn the user (this is the least we | |
269 | // can do) about it | |
270 | wxASSERT_MSG( wxThread::IsMain(), | |
271 | _T("wxExecute() can be called only from the main thread") ); | |
272 | #endif // wxUSE_THREADS | |
273 | ||
518b5d2f | 274 | int argc = 0; |
05079acc | 275 | wxChar *argv[WXEXECUTE_NARGS]; |
fad866f4 | 276 | wxString argument; |
05079acc | 277 | const wxChar *cptr = command.c_str(); |
223d09f6 | 278 | wxChar quotechar = wxT('\0'); // is arg quoted? |
9d8aca48 | 279 | bool escaped = false; |
518b5d2f | 280 | |
0ed9a934 | 281 | // split the command line in arguments |
fad866f4 KB |
282 | do |
283 | { | |
7520f3da | 284 | argument = wxEmptyString; |
223d09f6 | 285 | quotechar = wxT('\0'); |
0ed9a934 | 286 | |
fad866f4 | 287 | // eat leading whitespace: |
05079acc | 288 | while ( wxIsspace(*cptr) ) |
fad866f4 | 289 | cptr++; |
0ed9a934 | 290 | |
223d09f6 | 291 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) |
fad866f4 | 292 | quotechar = *cptr++; |
0ed9a934 | 293 | |
fad866f4 KB |
294 | do |
295 | { | |
223d09f6 | 296 | if ( *cptr == wxT('\\') && ! escaped ) |
fad866f4 | 297 | { |
9d8aca48 | 298 | escaped = true; |
fad866f4 KB |
299 | cptr++; |
300 | continue; | |
301 | } | |
0ed9a934 | 302 | |
fad866f4 | 303 | // all other characters: |
0ed9a934 | 304 | argument += *cptr++; |
9d8aca48 | 305 | escaped = false; |
0ed9a934 VZ |
306 | |
307 | // have we reached the end of the argument? | |
308 | if ( (*cptr == quotechar && ! escaped) | |
223d09f6 KB |
309 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) |
310 | || *cptr == wxT('\0') ) | |
fad866f4 | 311 | { |
0ed9a934 | 312 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, |
223d09f6 | 313 | wxT("too many arguments in wxExecute") ); |
0ed9a934 | 314 | |
05079acc OK |
315 | argv[argc] = new wxChar[argument.length() + 1]; |
316 | wxStrcpy(argv[argc], argument.c_str()); | |
fad866f4 | 317 | argc++; |
0ed9a934 | 318 | |
fad866f4 | 319 | // if not at end of buffer, swallow last character: |
0ed9a934 VZ |
320 | if(*cptr) |
321 | cptr++; | |
322 | ||
fad866f4 KB |
323 | break; // done with this one, start over |
324 | } | |
0ed9a934 VZ |
325 | } while(*cptr); |
326 | } while(*cptr); | |
fad866f4 | 327 | argv[argc] = NULL; |
0ed9a934 | 328 | |
62705a27 RN |
329 | long lRc; |
330 | #if defined(__DARWIN__) | |
3afdfec4 | 331 | // wxMacExecute only executes app bundles. |
fb19fbab SC |
332 | // It returns an error code if the target is not an app bundle, thus falling |
333 | // through to the regular wxExecute for non app bundles. | |
3afdfec4 | 334 | lRc = wxMacExecute(argv, flags, process); |
fb19fbab | 335 | if( lRc != ((flags & wxEXEC_SYNC) ? -1 : 0)) |
62705a27 RN |
336 | return lRc; |
337 | #endif | |
338 | ||
0ed9a934 | 339 | // do execute the command |
62705a27 | 340 | lRc = wxExecute(argv, flags, process); |
518b5d2f | 341 | |
0ed9a934 | 342 | // clean up |
fad866f4 | 343 | argc = 0; |
0ed9a934 | 344 | while( argv[argc] ) |
fad866f4 | 345 | delete [] argv[argc++]; |
518b5d2f VZ |
346 | |
347 | return lRc; | |
348 | } | |
349 | ||
2c8e4738 VZ |
350 | // ---------------------------------------------------------------------------- |
351 | // wxShell | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
355 | { |
356 | wxString cmd; | |
cd6ce4a9 | 357 | if ( !command ) |
2c8e4738 VZ |
358 | { |
359 | // just an interactive shell | |
cd6ce4a9 | 360 | cmd = _T("xterm"); |
2c8e4738 | 361 | } |
518b5d2f | 362 | else |
2c8e4738 VZ |
363 | { |
364 | // execute command in a shell | |
365 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
366 | } | |
367 | ||
368 | return cmd; | |
369 | } | |
370 | ||
371 | bool wxShell(const wxString& command) | |
372 | { | |
fbf456aa | 373 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
374 | } |
375 | ||
376 | bool wxShell(const wxString& command, wxArrayString& output) | |
377 | { | |
9d8aca48 | 378 | wxCHECK_MSG( !command.empty(), false, _T("can't exec shell non interactively") ); |
518b5d2f | 379 | |
2c8e4738 | 380 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
381 | } |
382 | ||
f6ba47d9 VZ |
383 | // Shutdown or reboot the PC |
384 | bool wxShutdown(wxShutdownFlags wFlags) | |
385 | { | |
386 | wxChar level; | |
387 | switch ( wFlags ) | |
388 | { | |
389 | case wxSHUTDOWN_POWEROFF: | |
390 | level = _T('0'); | |
391 | break; | |
392 | ||
393 | case wxSHUTDOWN_REBOOT: | |
394 | level = _T('6'); | |
395 | break; | |
396 | ||
397 | default: | |
398 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
9d8aca48 | 399 | return false; |
f6ba47d9 VZ |
400 | } |
401 | ||
402 | return system(wxString::Format(_T("init %c"), level).mb_str()) == 0; | |
403 | } | |
404 | ||
8ea92b4d WS |
405 | wxPowerType wxGetPowerType() |
406 | { | |
407 | // TODO | |
408 | return wxPOWER_UNKNOWN; | |
409 | } | |
410 | ||
411 | wxBatteryState wxGetBatteryState() | |
412 | { | |
413 | // TODO | |
414 | return wxBATTERY_UNKNOWN_STATE; | |
415 | } | |
f6ba47d9 | 416 | |
cd6ce4a9 VZ |
417 | // ---------------------------------------------------------------------------- |
418 | // wxStream classes to support IO redirection in wxExecute | |
419 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 420 | |
1e6feb95 VZ |
421 | #if wxUSE_STREAMS |
422 | ||
2b5f62a0 | 423 | bool wxPipeInputStream::CanRead() const |
8b33ae2d | 424 | { |
cd6ce4a9 | 425 | if ( m_lasterror == wxSTREAM_EOF ) |
9d8aca48 | 426 | return false; |
cd6ce4a9 VZ |
427 | |
428 | // check if there is any input available | |
429 | struct timeval tv; | |
430 | tv.tv_sec = 0; | |
431 | tv.tv_usec = 0; | |
432 | ||
80d6dc0a VZ |
433 | const int fd = m_file->fd(); |
434 | ||
cd6ce4a9 | 435 | fd_set readfds; |
17a1ebd1 VZ |
436 | |
437 | wxFD_ZERO(&readfds); | |
438 | wxFD_SET(fd, &readfds); | |
439 | ||
80d6dc0a | 440 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) |
cd6ce4a9 VZ |
441 | { |
442 | case -1: | |
443 | wxLogSysError(_("Impossible to get child process input")); | |
444 | // fall through | |
8b33ae2d | 445 | |
cd6ce4a9 | 446 | case 0: |
9d8aca48 | 447 | return false; |
8b33ae2d | 448 | |
cd6ce4a9 VZ |
449 | default: |
450 | wxFAIL_MSG(_T("unexpected select() return value")); | |
451 | // still fall through | |
452 | ||
453 | case 1: | |
6f3d3c68 VZ |
454 | // input available -- or maybe not, as select() returns 1 when a |
455 | // read() will complete without delay, but it could still not read | |
456 | // anything | |
457 | return !Eof(); | |
cd6ce4a9 | 458 | } |
8b33ae2d GL |
459 | } |
460 | ||
1e6feb95 VZ |
461 | #endif // wxUSE_STREAMS |
462 | ||
b477f956 VZ |
463 | // ---------------------------------------------------------------------------- |
464 | // wxExecute: the real worker function | |
465 | // ---------------------------------------------------------------------------- | |
79066131 | 466 | |
17a1ebd1 | 467 | long wxExecute(wxChar **argv, int flags, wxProcess *process) |
518b5d2f | 468 | { |
f6bcfd97 | 469 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
470 | // case we return 0 which is never a valid PID |
471 | // | |
472 | // we define this as a macro, not a variable, to avoid compiler warnings | |
473 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 474 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 475 | |
accb3257 | 476 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 477 | |
05079acc OK |
478 | #if wxUSE_UNICODE |
479 | int mb_argc = 0; | |
480 | char *mb_argv[WXEXECUTE_NARGS]; | |
481 | ||
e90c1d2a VZ |
482 | while (argv[mb_argc]) |
483 | { | |
cd6ce4a9 VZ |
484 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
485 | mb_argv[mb_argc] = strdup(mb_arg); | |
486 | mb_argc++; | |
05079acc OK |
487 | } |
488 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
489 | |
490 | // this macro will free memory we used above | |
491 | #define ARGS_CLEANUP \ | |
345b0247 | 492 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
493 | free(mb_argv[mb_argc]) |
494 | #else // ANSI | |
495 | // no need for cleanup | |
496 | #define ARGS_CLEANUP | |
497 | ||
05079acc | 498 | wxChar **mb_argv = argv; |
e90c1d2a | 499 | #endif // Unicode/ANSI |
518b5d2f | 500 | |
e2478fde VZ |
501 | // we want this function to work even if there is no wxApp so ensure that |
502 | // we have a valid traits pointer | |
503 | wxConsoleAppTraits traitsConsole; | |
504 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
505 | if ( !traits ) | |
506 | traits = &traitsConsole; | |
507 | ||
508 | // this struct contains all information which we pass to and from | |
509 | // wxAppTraits methods | |
510 | wxExecuteData execData; | |
511 | execData.flags = flags; | |
512 | execData.process = process; | |
513 | ||
518b5d2f | 514 | // create pipes |
e2478fde | 515 | if ( !traits->CreateEndProcessPipe(execData) ) |
518b5d2f | 516 | { |
cd6ce4a9 | 517 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
518 | |
519 | ARGS_CLEANUP; | |
520 | ||
accb3257 | 521 | return ERROR_RETURN_CODE; |
518b5d2f VZ |
522 | } |
523 | ||
f6bcfd97 | 524 | // pipes for inter process communication |
b477f956 VZ |
525 | wxPipe pipeIn, // stdin |
526 | pipeOut, // stdout | |
527 | pipeErr; // stderr | |
cd6ce4a9 VZ |
528 | |
529 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 530 | { |
b477f956 | 531 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 532 | { |
cd6ce4a9 | 533 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d GL |
534 | |
535 | ARGS_CLEANUP; | |
536 | ||
accb3257 | 537 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
538 | } |
539 | } | |
8b33ae2d | 540 | |
518b5d2f | 541 | // fork the process |
ef5f8ab6 VZ |
542 | // |
543 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
544 | // reason under Solaris (and maybe others, although not under Linux) | |
b2ddee86 JJ |
545 | // But on OpenVMS we do not have fork so we have to use vfork and |
546 | // cross our fingers that it works. | |
547 | #ifdef __VMS | |
548 | pid_t pid = vfork(); | |
549 | #else | |
550 | pid_t pid = fork(); | |
551 | #endif | |
552 | if ( pid == -1 ) // error? | |
518b5d2f VZ |
553 | { |
554 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a VZ |
555 | |
556 | ARGS_CLEANUP; | |
557 | ||
accb3257 | 558 | return ERROR_RETURN_CODE; |
518b5d2f | 559 | } |
cd6ce4a9 | 560 | else if ( pid == 0 ) // we're in child |
518b5d2f | 561 | { |
cd6ce4a9 | 562 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 563 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
564 | // one wants proper IO for the subprocess, the right thing to do is to |
565 | // start an xterm executing it. | |
fbf456aa | 566 | if ( !(flags & wxEXEC_SYNC) ) |
518b5d2f | 567 | { |
35ef537b VZ |
568 | // FD_SETSIZE is unsigned under BSD, signed under other platforms |
569 | // so we need a cast to avoid warnings on all platforms | |
570 | for ( int fd = 0; fd < (int)FD_SETSIZE; fd++ ) | |
518b5d2f | 571 | { |
b477f956 VZ |
572 | if ( fd == pipeIn[wxPipe::Read] |
573 | || fd == pipeOut[wxPipe::Write] | |
574 | || fd == pipeErr[wxPipe::Write] | |
e2478fde | 575 | || traits->IsWriteFDOfEndProcessPipe(execData, fd) ) |
cd6ce4a9 VZ |
576 | { |
577 | // don't close this one, we still need it | |
578 | continue; | |
579 | } | |
e90c1d2a | 580 | |
b477f956 | 581 | // leave stderr opened too, it won't do any harm |
e90c1d2a | 582 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
583 | close(fd); |
584 | } | |
0c9c4401 | 585 | } |
e1082c9f | 586 | |
e8e776aa | 587 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
588 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
589 | { | |
590 | // Set process group to child process' pid. Then killing -pid | |
591 | // of the parent will kill the process and all of its children. | |
592 | setsid(); | |
518b5d2f | 593 | } |
0c9c4401 VZ |
594 | #endif // !__VMS |
595 | ||
0c9c4401 VZ |
596 | // reading side can be safely closed but we should keep the write one |
597 | // opened | |
e2478fde | 598 | traits->DetachWriteFDOfEndProcessPipe(execData); |
518b5d2f | 599 | |
80d6dc0a | 600 | // redirect stdin, stdout and stderr |
b477f956 | 601 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 602 | { |
b477f956 VZ |
603 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
604 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
605 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 606 | { |
f6bcfd97 | 607 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 608 | } |
518b5d2f | 609 | |
b477f956 VZ |
610 | pipeIn.Close(); |
611 | pipeOut.Close(); | |
612 | pipeErr.Close(); | |
cd6ce4a9 | 613 | } |
518b5d2f | 614 | |
05079acc | 615 | execvp (*mb_argv, mb_argv); |
17a1ebd1 | 616 | |
d264d709 | 617 | fprintf(stderr, "execvp("); |
8d4f85ce SC |
618 | // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O |
619 | for ( char **ppc_ = mb_argv; *ppc_; ppc_++ ) | |
620 | fprintf(stderr, "%s%s", ppc_ == mb_argv ? "" : ", ", *ppc_); | |
d264d709 VZ |
621 | fprintf(stderr, ") failed with error %d!\n", errno); |
622 | ||
518b5d2f | 623 | // there is no return after successful exec() |
518b5d2f | 624 | _exit(-1); |
1d8dd65e VZ |
625 | |
626 | // some compilers complain about missing return - of course, they | |
627 | // should know that exit() doesn't return but what else can we do if | |
628 | // they don't? | |
b477f956 VZ |
629 | // |
630 | // and, sure enough, other compilers complain about unreachable code | |
631 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
632 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
633 | return 0; | |
634 | #endif | |
518b5d2f | 635 | } |
cd6ce4a9 | 636 | else // we're in parent |
518b5d2f | 637 | { |
cd6ce4a9 VZ |
638 | ARGS_CLEANUP; |
639 | ||
7764f973 VZ |
640 | // save it for WaitForChild() use |
641 | execData.pid = pid; | |
642 | ||
80d6dc0a VZ |
643 | // prepare for IO redirection |
644 | ||
0e300ddd | 645 | #if wxUSE_STREAMS |
80d6dc0a VZ |
646 | // the input buffer bufOut is connected to stdout, this is why it is |
647 | // called bufOut and not bufIn | |
648 | wxStreamTempInputBuffer bufOut, | |
649 | bufErr; | |
0e300ddd VZ |
650 | #endif // wxUSE_STREAMS |
651 | ||
cd6ce4a9 VZ |
652 | if ( process && process->IsRedirected() ) |
653 | { | |
1e6feb95 | 654 | #if wxUSE_STREAMS |
80d6dc0a VZ |
655 | wxOutputStream *inStream = |
656 | new wxFileOutputStream(pipeIn.Detach(wxPipe::Write)); | |
b477f956 | 657 | |
79066131 VZ |
658 | wxPipeInputStream *outStream = |
659 | new wxPipeInputStream(pipeOut.Detach(wxPipe::Read)); | |
b477f956 | 660 | |
79066131 VZ |
661 | wxPipeInputStream *errStream = |
662 | new wxPipeInputStream(pipeErr.Detach(wxPipe::Read)); | |
f6bcfd97 | 663 | |
80d6dc0a | 664 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 665 | |
80d6dc0a | 666 | bufOut.Init(outStream); |
b477f956 | 667 | bufErr.Init(errStream); |
e2478fde VZ |
668 | |
669 | execData.bufOut = &bufOut; | |
670 | execData.bufErr = &bufErr; | |
1e6feb95 | 671 | #endif // wxUSE_STREAMS |
b477f956 | 672 | } |
1e6feb95 | 673 | |
b477f956 VZ |
674 | if ( pipeIn.IsOk() ) |
675 | { | |
676 | pipeIn.Close(); | |
677 | pipeOut.Close(); | |
678 | pipeErr.Close(); | |
cd6ce4a9 VZ |
679 | } |
680 | ||
e2478fde | 681 | return traits->WaitForChild(execData); |
518b5d2f | 682 | } |
79656e30 | 683 | |
17a1ebd1 | 684 | #if !defined(__VMS) && !defined(__INTEL_COMPILER) |
79656e30 | 685 | return ERROR_RETURN_CODE; |
ef0ed19e | 686 | #endif |
17a1ebd1 | 687 | } |
518b5d2f | 688 | |
accb3257 | 689 | #undef ERROR_RETURN_CODE |
f6bcfd97 BP |
690 | #undef ARGS_CLEANUP |
691 | ||
518b5d2f VZ |
692 | // ---------------------------------------------------------------------------- |
693 | // file and directory functions | |
694 | // ---------------------------------------------------------------------------- | |
695 | ||
05079acc | 696 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f | 697 | { |
8ea92b4d | 698 | *home = wxGetUserHome( wxEmptyString ); |
79066131 | 699 | wxString tmp; |
8ea92b4d | 700 | if ( home->empty() ) |
223d09f6 | 701 | *home = wxT("/"); |
181cbcf4 | 702 | #ifdef __VMS |
79066131 VZ |
703 | tmp = *home; |
704 | if ( tmp.Last() != wxT(']')) | |
705 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 706 | #endif |
518b5d2f VZ |
707 | return home->c_str(); |
708 | } | |
709 | ||
05079acc OK |
710 | #if wxUSE_UNICODE |
711 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 712 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 713 | char *wxGetUserHome( const wxString &user ) |
05079acc | 714 | #endif |
518b5d2f VZ |
715 | { |
716 | struct passwd *who = (struct passwd *) NULL; | |
717 | ||
0fb67cd1 | 718 | if ( !user ) |
518b5d2f | 719 | { |
e90c1d2a | 720 | wxChar *ptr; |
518b5d2f | 721 | |
223d09f6 | 722 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f | 723 | { |
2b5f62a0 VZ |
724 | #if wxUSE_UNICODE |
725 | wxWCharBuffer buffer( ptr ); | |
726 | return buffer; | |
727 | #else | |
518b5d2f | 728 | return ptr; |
2b5f62a0 | 729 | #endif |
518b5d2f | 730 | } |
223d09f6 | 731 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 732 | { |
e90c1d2a | 733 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
734 | } |
735 | ||
736 | // We now make sure the the user exists! | |
737 | if (who == NULL) | |
738 | { | |
739 | who = getpwuid(getuid()); | |
740 | } | |
741 | } | |
742 | else | |
743 | { | |
05079acc | 744 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
745 | } |
746 | ||
af111fc3 | 747 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
748 | } |
749 | ||
750 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 751 | // network and user id routines |
518b5d2f VZ |
752 | // ---------------------------------------------------------------------------- |
753 | ||
0fb67cd1 VZ |
754 | // retrieve either the hostname or FQDN depending on platform (caller must |
755 | // check whether it's one or the other, this is why this function is for | |
756 | // private use only) | |
05079acc | 757 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 758 | { |
9d8aca48 | 759 | wxCHECK_MSG( buf, false, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 760 | |
223d09f6 | 761 | *buf = wxT('\0'); |
518b5d2f VZ |
762 | |
763 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
764 | #if defined(HAVE_UNAME) | |
cc743a6f | 765 | struct utsname uts; |
518b5d2f VZ |
766 | bool ok = uname(&uts) != -1; |
767 | if ( ok ) | |
768 | { | |
e90c1d2a | 769 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 770 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
771 | } |
772 | #elif defined(HAVE_GETHOSTNAME) | |
773 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 774 | #else // no uname, no gethostname |
223d09f6 | 775 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f | 776 | |
9d8aca48 | 777 | bool ok = false; |
0fb67cd1 | 778 | #endif // uname/gethostname |
518b5d2f VZ |
779 | |
780 | if ( !ok ) | |
781 | { | |
782 | wxLogSysError(_("Cannot get the hostname")); | |
783 | } | |
784 | ||
785 | return ok; | |
786 | } | |
787 | ||
05079acc | 788 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
789 | { |
790 | bool ok = wxGetHostNameInternal(buf, sz); | |
791 | ||
792 | if ( ok ) | |
793 | { | |
794 | // BSD systems return the FQDN, we only want the hostname, so extract | |
795 | // it (we consider that dots are domain separators) | |
223d09f6 | 796 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
797 | if ( dot ) |
798 | { | |
799 | // nuke it | |
223d09f6 | 800 | *dot = wxT('\0'); |
0fb67cd1 VZ |
801 | } |
802 | } | |
803 | ||
804 | return ok; | |
805 | } | |
806 | ||
05079acc | 807 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
808 | { |
809 | bool ok = wxGetHostNameInternal(buf, sz); | |
810 | ||
811 | if ( ok ) | |
812 | { | |
223d09f6 | 813 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 814 | { |
e90c1d2a | 815 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
816 | if ( !host ) |
817 | { | |
818 | wxLogSysError(_("Cannot get the official hostname")); | |
819 | ||
9d8aca48 | 820 | ok = false; |
0fb67cd1 VZ |
821 | } |
822 | else | |
823 | { | |
824 | // the canonical name | |
e90c1d2a | 825 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
826 | } |
827 | } | |
828 | //else: it's already a FQDN (BSD behaves this way) | |
829 | } | |
830 | ||
831 | return ok; | |
832 | } | |
833 | ||
05079acc | 834 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
835 | { |
836 | struct passwd *who; | |
837 | ||
223d09f6 | 838 | *buf = wxT('\0'); |
518b5d2f VZ |
839 | if ((who = getpwuid(getuid ())) != NULL) |
840 | { | |
e90c1d2a | 841 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
9d8aca48 | 842 | return true; |
518b5d2f VZ |
843 | } |
844 | ||
9d8aca48 | 845 | return false; |
518b5d2f VZ |
846 | } |
847 | ||
05079acc | 848 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
849 | { |
850 | struct passwd *who; | |
518b5d2f | 851 | |
223d09f6 | 852 | *buf = wxT('\0'); |
b12915c1 VZ |
853 | if ((who = getpwuid (getuid ())) != NULL) |
854 | { | |
855 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 856 | #ifdef HAVE_PW_GECOS |
b12915c1 | 857 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
858 | if (comma) |
859 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 860 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 861 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 862 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 863 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
9d8aca48 | 864 | return true; |
518b5d2f VZ |
865 | } |
866 | ||
9d8aca48 | 867 | return false; |
518b5d2f VZ |
868 | } |
869 | ||
e2478fde | 870 | // this function is in mac/utils.cpp for wxMac |
6e73695c | 871 | #ifndef __WXMAC__ |
e2478fde | 872 | |
bdc72a22 VZ |
873 | wxString wxGetOsDescription() |
874 | { | |
db1d0193 VZ |
875 | FILE *f = popen("uname -s -r -m", "r"); |
876 | if (f) | |
877 | { | |
878 | char buf[256]; | |
879 | size_t c = fread(buf, 1, sizeof(buf) - 1, f); | |
880 | pclose(f); | |
881 | // Trim newline from output. | |
882 | if (c && buf[c - 1] == '\n') | |
883 | --c; | |
884 | buf[c] = '\0'; | |
885 | return wxString::FromAscii( buf ); | |
886 | } | |
887 | wxFAIL_MSG( _T("uname failed") ); | |
7520f3da | 888 | return wxEmptyString; |
bdc72a22 VZ |
889 | } |
890 | ||
e2478fde | 891 | #endif // !__WXMAC__ |
bd3277fe | 892 | |
c1cb4153 VZ |
893 | unsigned long wxGetProcessId() |
894 | { | |
895 | return (unsigned long)getpid(); | |
896 | } | |
897 | ||
9d8aca48 | 898 | wxMemorySize wxGetFreeMemory() |
bd3277fe VZ |
899 | { |
900 | #if defined(__LINUX__) | |
901 | // get it from /proc/meminfo | |
902 | FILE *fp = fopen("/proc/meminfo", "r"); | |
903 | if ( fp ) | |
904 | { | |
905 | long memFree = -1; | |
906 | ||
907 | char buf[1024]; | |
908 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
909 | { | |
910 | long memTotal, memUsed; | |
911 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
912 | } | |
913 | ||
914 | fclose(fp); | |
915 | ||
9d8aca48 | 916 | return (wxMemorySize)memFree; |
bd3277fe VZ |
917 | } |
918 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
9d8aca48 | 919 | return (wxMemorySize)(sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE)); |
9ad34f61 VZ |
920 | #elif defined(__SGI__) |
921 | struct rminfo realmem; | |
922 | if ( sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0 ) | |
923 | return ((wxMemorySize)realmem.physmem * sysconf(_SC_PAGESIZE)); | |
bd3277fe VZ |
924 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably |
925 | #endif | |
926 | ||
927 | // can't find it out | |
928 | return -1; | |
929 | } | |
930 | ||
7ba7c4e6 | 931 | bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree) |
eadd7bd2 | 932 | { |
9952adac | 933 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 934 | // the case to "char *" is needed for AIX 4.3 |
85da04e9 VZ |
935 | wxStatfs_t fs; |
936 | if ( wxStatfs((char *)(const char*)path.fn_str(), &fs) != 0 ) | |
eadd7bd2 | 937 | { |
401eb3de | 938 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 | 939 | |
9d8aca48 | 940 | return false; |
eadd7bd2 VZ |
941 | } |
942 | ||
125cb99b VZ |
943 | // under Solaris we also have to use f_frsize field instead of f_bsize |
944 | // which is in general a multiple of f_frsize | |
945 | #ifdef HAVE_STATVFS | |
7ba7c4e6 | 946 | wxDiskspaceSize_t blockSize = fs.f_frsize; |
125cb99b | 947 | #else // HAVE_STATFS |
7ba7c4e6 | 948 | wxDiskspaceSize_t blockSize = fs.f_bsize; |
125cb99b | 949 | #endif // HAVE_STATVFS/HAVE_STATFS |
9952adac | 950 | |
eadd7bd2 VZ |
951 | if ( pTotal ) |
952 | { | |
7ba7c4e6 | 953 | *pTotal = wxDiskspaceSize_t(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
954 | } |
955 | ||
956 | if ( pFree ) | |
957 | { | |
7ba7c4e6 | 958 | *pFree = wxDiskspaceSize_t(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
959 | } |
960 | ||
9d8aca48 | 961 | return true; |
125cb99b | 962 | #else // !HAVE_STATFS && !HAVE_STATVFS |
9d8aca48 | 963 | return false; |
125cb99b | 964 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
965 | } |
966 | ||
8fd0d89b VZ |
967 | // ---------------------------------------------------------------------------- |
968 | // env vars | |
969 | // ---------------------------------------------------------------------------- | |
970 | ||
97b305b7 | 971 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
972 | { |
973 | // wxGetenv is defined as getenv() | |
974 | wxChar *p = wxGetenv(var); | |
975 | if ( !p ) | |
9d8aca48 | 976 | return false; |
308978f6 VZ |
977 | |
978 | if ( value ) | |
979 | { | |
980 | *value = p; | |
981 | } | |
982 | ||
9d8aca48 | 983 | return true; |
308978f6 VZ |
984 | } |
985 | ||
8fd0d89b VZ |
986 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
987 | { | |
988 | #if defined(HAVE_SETENV) | |
d90b2df8 VZ |
989 | return setenv(variable.mb_str(), |
990 | value ? (const char *)wxString(value).mb_str() | |
991 | : NULL, | |
992 | 1 /* overwrite */) == 0; | |
8fd0d89b VZ |
993 | #elif defined(HAVE_PUTENV) |
994 | wxString s = variable; | |
995 | if ( value ) | |
996 | s << _T('=') << value; | |
997 | ||
998 | // transform to ANSI | |
67479dbd | 999 | const wxWX2MBbuf p = s.mb_str(); |
8fd0d89b VZ |
1000 | |
1001 | // the string will be free()d by libc | |
1002 | char *buf = (char *)malloc(strlen(p) + 1); | |
1003 | strcpy(buf, p); | |
1004 | ||
1005 | return putenv(buf) == 0; | |
1006 | #else // no way to set an env var | |
67479dbd | 1007 | return false; |
8fd0d89b VZ |
1008 | #endif |
1009 | } | |
1010 | ||
a37a5a73 VZ |
1011 | // ---------------------------------------------------------------------------- |
1012 | // signal handling | |
1013 | // ---------------------------------------------------------------------------- | |
1014 | ||
1015 | #if wxUSE_ON_FATAL_EXCEPTION | |
1016 | ||
1017 | #include <signal.h> | |
1018 | ||
90350682 | 1019 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1020 | { |
1021 | if ( wxTheApp ) | |
1022 | { | |
1023 | // give the user a chance to do something special about this | |
1024 | wxTheApp->OnFatalException(); | |
1025 | } | |
1026 | ||
1027 | abort(); | |
1028 | } | |
1029 | ||
1030 | bool wxHandleFatalExceptions(bool doit) | |
1031 | { | |
1032 | // old sig handlers | |
9d8aca48 | 1033 | static bool s_savedHandlers = false; |
a37a5a73 VZ |
1034 | static struct sigaction s_handlerFPE, |
1035 | s_handlerILL, | |
1036 | s_handlerBUS, | |
1037 | s_handlerSEGV; | |
1038 | ||
9d8aca48 | 1039 | bool ok = true; |
a37a5a73 VZ |
1040 | if ( doit && !s_savedHandlers ) |
1041 | { | |
1042 | // install the signal handler | |
1043 | struct sigaction act; | |
1044 | ||
1045 | // some systems extend it with non std fields, so zero everything | |
1046 | memset(&act, 0, sizeof(act)); | |
1047 | ||
1048 | act.sa_handler = wxFatalSignalHandler; | |
1049 | sigemptyset(&act.sa_mask); | |
1050 | act.sa_flags = 0; | |
1051 | ||
1052 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1053 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1054 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1055 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1056 | if ( !ok ) | |
1057 | { | |
1058 | wxLogDebug(_T("Failed to install our signal handler.")); | |
1059 | } | |
1060 | ||
9d8aca48 | 1061 | s_savedHandlers = true; |
a37a5a73 VZ |
1062 | } |
1063 | else if ( s_savedHandlers ) | |
1064 | { | |
1065 | // uninstall the signal handler | |
1066 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1067 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1068 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1069 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1070 | if ( !ok ) | |
1071 | { | |
1072 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1073 | } | |
1074 | ||
9d8aca48 | 1075 | s_savedHandlers = false; |
a37a5a73 VZ |
1076 | } |
1077 | //else: nothing to do | |
1078 | ||
1079 | return ok; | |
1080 | } | |
1081 | ||
1082 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1083 | ||
ec67cff1 | 1084 | #endif // wxUSE_BASE |
e2478fde VZ |
1085 | |
1086 | #if wxUSE_GUI | |
1087 | ||
1088 | // ---------------------------------------------------------------------------- | |
1089 | // wxExecute support | |
1090 | // ---------------------------------------------------------------------------- | |
1091 | ||
1092 | // Darwin doesn't use the same process end detection mechanisms so we don't | |
1093 | // need wxExecute-related helpers for it | |
1094 | #if !(defined(__DARWIN__) && defined(__WXMAC__)) | |
1095 | ||
1096 | bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData& execData) | |
1097 | { | |
1098 | return execData.pipeEndProcDetect.Create(); | |
1099 | } | |
1100 | ||
1101 | bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData& execData, int fd) | |
1102 | { | |
46446cc2 | 1103 | return fd == (execData.pipeEndProcDetect)[wxPipe::Write]; |
e2478fde VZ |
1104 | } |
1105 | ||
1106 | void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData& execData) | |
1107 | { | |
1108 | execData.pipeEndProcDetect.Detach(wxPipe::Write); | |
1109 | execData.pipeEndProcDetect.Close(); | |
1110 | } | |
1111 | ||
1112 | #else // !Darwin | |
1113 | ||
1114 | bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData& WXUNUSED(execData)) | |
1115 | { | |
1116 | return true; | |
1117 | } | |
1118 | ||
1119 | bool | |
1120 | wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData& WXUNUSED(execData), | |
1121 | int WXUNUSED(fd)) | |
1122 | { | |
1123 | return false; | |
1124 | } | |
1125 | ||
1126 | void | |
1127 | wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData& WXUNUSED(execData)) | |
1128 | { | |
1129 | // nothing to do here, we don't use the pipe | |
1130 | } | |
1131 | ||
1132 | #endif // !Darwin/Darwin | |
1133 | ||
1134 | int wxGUIAppTraits::WaitForChild(wxExecuteData& execData) | |
1135 | { | |
1136 | wxEndProcessData *endProcData = new wxEndProcessData; | |
1137 | ||
f38f6899 VZ |
1138 | const int flags = execData.flags; |
1139 | ||
e2478fde VZ |
1140 | // wxAddProcessCallback is now (with DARWIN) allowed to call the |
1141 | // callback function directly if the process terminates before | |
1142 | // the callback can be added to the run loop. Set up the endProcData. | |
f38f6899 | 1143 | if ( flags & wxEXEC_SYNC ) |
e2478fde VZ |
1144 | { |
1145 | // we may have process for capturing the program output, but it's | |
1146 | // not used in wxEndProcessData in the case of sync execution | |
1147 | endProcData->process = NULL; | |
1148 | ||
1149 | // sync execution: indicate it by negating the pid | |
1150 | endProcData->pid = -execData.pid; | |
1151 | } | |
1152 | else | |
1153 | { | |
1154 | // async execution, nothing special to do -- caller will be | |
1155 | // notified about the process termination if process != NULL, endProcData | |
1156 | // will be deleted in GTK_EndProcessDetector | |
1157 | endProcData->process = execData.process; | |
1158 | endProcData->pid = execData.pid; | |
1159 | } | |
1160 | ||
1161 | ||
f7ef0602 | 1162 | #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) |
e2478fde VZ |
1163 | endProcData->tag = wxAddProcessCallbackForPid(endProcData, execData.pid); |
1164 | #else | |
1165 | endProcData->tag = wxAddProcessCallback | |
1166 | ( | |
1167 | endProcData, | |
1168 | execData.pipeEndProcDetect.Detach(wxPipe::Read) | |
1169 | ); | |
1170 | ||
1171 | execData.pipeEndProcDetect.Close(); | |
f7ef0602 | 1172 | #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) |
e2478fde | 1173 | |
f38f6899 | 1174 | if ( flags & wxEXEC_SYNC ) |
e2478fde VZ |
1175 | { |
1176 | wxBusyCursor bc; | |
f38f6899 VZ |
1177 | wxWindowDisabler *wd = flags & wxEXEC_NODISABLE ? NULL |
1178 | : new wxWindowDisabler; | |
e2478fde VZ |
1179 | |
1180 | // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the | |
1181 | // process terminates | |
1182 | while ( endProcData->pid != 0 ) | |
1183 | { | |
05df0f1b VZ |
1184 | bool idle = true; |
1185 | ||
e2478fde VZ |
1186 | #if wxUSE_STREAMS |
1187 | if ( execData.bufOut ) | |
05df0f1b | 1188 | { |
e2478fde | 1189 | execData.bufOut->Update(); |
05df0f1b VZ |
1190 | idle = false; |
1191 | } | |
e2478fde VZ |
1192 | |
1193 | if ( execData.bufErr ) | |
05df0f1b | 1194 | { |
e2478fde | 1195 | execData.bufErr->Update(); |
05df0f1b VZ |
1196 | idle = false; |
1197 | } | |
e2478fde VZ |
1198 | #endif // wxUSE_STREAMS |
1199 | ||
1012c2ce | 1200 | // don't consume 100% of the CPU while we're sitting in this |
05df0f1b VZ |
1201 | // loop |
1202 | if ( idle ) | |
39ef7151 | 1203 | wxMilliSleep(1); |
05df0f1b | 1204 | |
e2478fde VZ |
1205 | // give GTK+ a chance to call GTK_EndProcessDetector here and |
1206 | // also repaint the GUI | |
1207 | wxYield(); | |
1208 | } | |
1209 | ||
1210 | int exitcode = endProcData->exitcode; | |
1211 | ||
f38f6899 | 1212 | delete wd; |
e2478fde VZ |
1213 | delete endProcData; |
1214 | ||
1215 | return exitcode; | |
1216 | } | |
1217 | else // async execution | |
1218 | { | |
1219 | return execData.pid; | |
1220 | } | |
1221 | } | |
1222 | ||
2dbc444a RD |
1223 | #endif // wxUSE_GUI |
1224 | #if wxUSE_BASE | |
1225 | ||
e2478fde VZ |
1226 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
1227 | { | |
1228 | // notify user about termination if required | |
1229 | if ( proc_data->process ) | |
1230 | { | |
1231 | proc_data->process->OnTerminate(proc_data->pid, proc_data->exitcode); | |
1232 | } | |
1233 | ||
1234 | // clean up | |
1235 | if ( proc_data->pid > 0 ) | |
1236 | { | |
1237 | delete proc_data; | |
1238 | } | |
1239 | else | |
1240 | { | |
1241 | // let wxExecute() know that the process has terminated | |
1242 | proc_data->pid = 0; | |
1243 | } | |
1244 | } | |
1245 | ||
2dbc444a | 1246 | #endif // wxUSE_BASE |