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