1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic Unix implementation of many wx functions
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #include "wx/string.h"
26 #include "wx/process.h"
27 #include "wx/thread.h"
29 #include "wx/stream.h"
33 # include <sys/param.h>
34 # include <sys/mount.h>
41 #include <sys/statvfs.h>
43 #define statfs statvfs
44 #endif // HAVE_STATVFS
47 #include "wx/unix/execute.h"
50 // SGI signal.h defines signal handler arguments differently depending on
51 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
52 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
53 #define _LANGUAGE_C_PLUS_PLUS 1
60 #include <sys/types.h>
67 #include <fcntl.h> // for O_WRONLY and friends
68 #include <time.h> // nanosleep() and/or usleep()
69 #include <ctype.h> // isspace()
70 #include <sys/time.h> // needed for FD_SETSIZE
73 #include <sys/utsname.h> // for uname()
76 // ----------------------------------------------------------------------------
77 // conditional compilation
78 // ----------------------------------------------------------------------------
80 // many versions of Unices have this function, but it is not defined in system
81 // headers - please add your system here if it is the case for your OS.
82 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
83 #if !defined(HAVE_USLEEP) && \
84 (defined(__SUN__) && !defined(__SunOs_5_6) && \
85 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
86 defined(__osf__) || defined(__EMX__)
90 int usleep(unsigned int usec
);
93 /* I copied this from the XFree86 diffs. AV. */
94 #define INCL_DOSPROCESS
96 inline void usleep(unsigned long delay
)
98 DosSleep(delay
? (delay
/1000l) : 1l);
100 #else // !Sun && !EMX
101 void usleep(unsigned long usec
);
103 #endif // Sun/EMX/Something else
106 #define HAVE_USLEEP 1
107 #endif // Unices without usleep()
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 void wxSleep(int nSecs
)
122 void wxUsleep(unsigned long milliseconds
)
124 #if defined(HAVE_NANOSLEEP)
126 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
127 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
129 // we're not interested in remaining time nor in return value
130 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
131 #elif defined(HAVE_USLEEP)
132 // uncomment this if you feel brave or if you are sure that your version
133 // of Solaris has a safe usleep() function but please notice that usleep()
134 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
135 // documented as MT-Safe
136 #if defined(__SUN__) && wxUSE_THREADS
137 #error "usleep() cannot be used in MT programs under Solaris."
140 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
141 #elif defined(HAVE_SLEEP)
142 // under BeOS sleep() takes seconds (what about other platforms, if any?)
143 sleep(milliseconds
* 1000);
144 #else // !sleep function
145 #error "usleep() or nanosleep() function required for wxUsleep"
146 #endif // sleep function
149 // ----------------------------------------------------------------------------
150 // process management
151 // ----------------------------------------------------------------------------
153 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
155 int err
= kill((pid_t
)pid
, (int)sig
);
165 *rc
= wxKILL_BAD_SIGNAL
;
169 *rc
= wxKILL_ACCESS_DENIED
;
173 *rc
= wxKILL_NO_PROCESS
;
177 // this goes against Unix98 docs so log it
178 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
188 #define WXEXECUTE_NARGS 127
190 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
192 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
195 wxChar
*argv
[WXEXECUTE_NARGS
];
197 const wxChar
*cptr
= command
.c_str();
198 wxChar quotechar
= wxT('\0'); // is arg quoted?
199 bool escaped
= FALSE
;
201 // split the command line in arguments
205 quotechar
= wxT('\0');
207 // eat leading whitespace:
208 while ( wxIsspace(*cptr
) )
211 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
216 if ( *cptr
== wxT('\\') && ! escaped
)
223 // all other characters:
227 // have we reached the end of the argument?
228 if ( (*cptr
== quotechar
&& ! escaped
)
229 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
230 || *cptr
== wxT('\0') )
232 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
233 wxT("too many arguments in wxExecute") );
235 argv
[argc
] = new wxChar
[argument
.length() + 1];
236 wxStrcpy(argv
[argc
], argument
.c_str());
239 // if not at end of buffer, swallow last character:
243 break; // done with this one, start over
249 // do execute the command
250 long lRc
= wxExecute(argv
, sync
, process
);
255 delete [] argv
[argc
++];
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 static wxString
wxMakeShellCommand(const wxString
& command
)
269 // just an interactive shell
274 // execute command in a shell
275 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
281 bool wxShell(const wxString
& command
)
283 return wxExecute(wxMakeShellCommand(command
), TRUE
/* sync */) == 0;
286 bool wxShell(const wxString
& command
, wxArrayString
& output
)
288 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
290 return wxExecute(wxMakeShellCommand(command
), output
);
295 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
297 // notify user about termination if required
298 if ( proc_data
->process
)
300 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
304 if ( proc_data
->pid
> 0 )
310 // let wxExecute() know that the process has terminated
317 // ----------------------------------------------------------------------------
318 // wxStream classes to support IO redirection in wxExecute
319 // ----------------------------------------------------------------------------
323 class wxProcessFileInputStream
: public wxInputStream
326 wxProcessFileInputStream(int fd
) { m_fd
= fd
; }
327 ~wxProcessFileInputStream() { close(m_fd
); }
329 virtual bool Eof() const;
332 size_t OnSysRead(void *buffer
, size_t bufsize
);
338 class wxProcessFileOutputStream
: public wxOutputStream
341 wxProcessFileOutputStream(int fd
) { m_fd
= fd
; }
342 ~wxProcessFileOutputStream() { close(m_fd
); }
345 size_t OnSysWrite(const void *buffer
, size_t bufsize
);
351 bool wxProcessFileInputStream::Eof() const
353 if ( m_lasterror
== wxSTREAM_EOF
)
356 // check if there is any input available
363 FD_SET(m_fd
, &readfds
);
364 switch ( select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
367 wxLogSysError(_("Impossible to get child process input"));
374 wxFAIL_MSG(_T("unexpected select() return value"));
375 // still fall through
378 // input available: check if there is any
379 return wxInputStream::Eof();
383 size_t wxProcessFileInputStream::OnSysRead(void *buffer
, size_t bufsize
)
385 int ret
= read(m_fd
, buffer
, bufsize
);
388 m_lasterror
= wxSTREAM_EOF
;
390 else if ( ret
== -1 )
392 m_lasterror
= wxSTREAM_READ_ERROR
;
397 m_lasterror
= wxSTREAM_NOERROR
;
403 size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
)
405 int ret
= write(m_fd
, buffer
, bufsize
);
408 m_lasterror
= wxSTREAM_WRITE_ERROR
;
413 m_lasterror
= wxSTREAM_NOERROR
;
419 // ----------------------------------------------------------------------------
420 // wxStreamTempBuffer
421 // ----------------------------------------------------------------------------
424 Extract of a mail to wx-users to give the context of the problem we are
425 trying to solve here:
427 MC> If I run the command:
428 MC> find . -name "*.h" -exec grep linux {} \;
429 MC> in the exec sample synchronously from the 'Capture command output'
430 MC> menu, wxExecute never returns. I have to xkill it. Has anyone
431 MC> else encountered this?
433 Yes, I can reproduce it too.
435 I even think I understand why it happens: before launching the external
436 command we set up a pipe with a valid file descriptor on the reading side
437 when the output is redirected. So the subprocess happily writes to it ...
438 until the pipe buffer (which is usually quite big on Unix, I think the
439 default is 4Mb) is full. Then the writing process stops and waits until we
440 read some data from the pipe to be able to continue writing to it but we
441 never do it because we wait until it terminates to start reading and so we
442 have a classical deadlock.
444 Here is the fix: we now read the output as soon as it appears into a temp
445 buffer (wxStreamTempBuffer object) and later just stuff it back into the
446 stream when the process terminates. See supporting code in wxExecute()
450 class wxStreamTempBuffer
453 wxStreamTempBuffer();
455 // call to associate a stream with this buffer, otherwise nothing happens
457 void Init(wxInputStream
*stream
);
459 // check for input on our stream and cache it in our buffer if any
462 ~wxStreamTempBuffer();
465 // the stream we're buffering, if NULL we don't do anything at all
466 wxInputStream
*m_stream
;
468 // the buffer of size m_size (NULL if m_size == 0)
471 // the size of the buffer
475 wxStreamTempBuffer::wxStreamTempBuffer()
482 void wxStreamTempBuffer::Init(wxInputStream
*stream
)
487 void wxStreamTempBuffer::Update()
489 if ( m_stream
&& !m_stream
->Eof() )
491 // realloc in blocks of 1Kb - surely not the best strategy but which
493 static const size_t incSize
= 1024;
495 void *buf
= realloc(m_buffer
, m_size
+ incSize
);
498 // don't read any more, we don't have enough memory to do it
501 else // got memory for the buffer
504 m_stream
->Read((char *)m_buffer
+ m_size
, incSize
);
510 wxStreamTempBuffer::~wxStreamTempBuffer()
514 m_stream
->Ungetch(m_buffer
, m_size
);
519 #endif // wxUSE_STREAMS
521 long wxExecute(wxChar
**argv
,
525 // for the sync execution, we return -1 to indicate failure, but for async
526 // case we return 0 which is never a valid PID
528 // we define this as a macro, not a variable, to avoid compiler warnings
529 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
530 #define ERROR_RETURN_CODE ((sync) ? -1 : 0)
532 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
536 char *mb_argv
[WXEXECUTE_NARGS
];
538 while (argv
[mb_argc
])
540 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
541 mb_argv
[mb_argc
] = strdup(mb_arg
);
544 mb_argv
[mb_argc
] = (char *) NULL
;
546 // this macro will free memory we used above
547 #define ARGS_CLEANUP \
548 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
549 free(mb_argv[mb_argc])
551 // no need for cleanup
554 wxChar
**mb_argv
= argv
;
555 #endif // Unicode/ANSI
559 int end_proc_detect
[2];
560 if ( pipe(end_proc_detect
) == -1 )
562 wxLogSysError( _("Pipe creation failed") );
563 wxLogError( _("Failed to execute '%s'\n"), *argv
);
567 return ERROR_RETURN_CODE
;
571 // pipes for inter process communication
572 int pipeIn
[2], // stdin
573 pipeOut
[2], // stdout
574 pipeErr
[2]; // stderr
576 pipeIn
[0] = pipeIn
[1] =
577 pipeOut
[0] = pipeOut
[1] =
578 pipeErr
[0] = pipeErr
[1] = -1;
580 if ( process
&& process
->IsRedirected() )
582 if ( pipe(pipeIn
) == -1 || pipe(pipeOut
) == -1 || pipe(pipeErr
) == -1 )
585 // free previously allocated resources
586 close(end_proc_detect
[0]);
587 close(end_proc_detect
[1]);
590 wxLogSysError( _("Pipe creation failed") );
591 wxLogError( _("Failed to execute '%s'\n"), *argv
);
595 return ERROR_RETURN_CODE
;
606 if ( pid
== -1 ) // error?
609 close(end_proc_detect
[0]);
610 close(end_proc_detect
[1]);
619 wxLogSysError( _("Fork failed") );
623 return ERROR_RETURN_CODE
;
625 else if ( pid
== 0 ) // we're in child
628 close(end_proc_detect
[0]); // close reading side
631 // These lines close the open file descriptors to to avoid any
632 // input/output which might block the process or irritate the user. If
633 // one wants proper IO for the subprocess, the right thing to do is to
634 // start an xterm executing it.
637 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
639 if ( fd
== pipeIn
[0] || fd
== pipeOut
[1] || fd
== pipeErr
[1]
641 || fd
== end_proc_detect
[1]
645 // don't close this one, we still need it
649 // leave stderr opened too, it won't do any hurm
650 if ( fd
!= STDERR_FILENO
)
655 // redirect stdio, stdout and stderr
656 if ( pipeIn
[0] != -1 )
658 if ( dup2(pipeIn
[0], STDIN_FILENO
) == -1 ||
659 dup2(pipeOut
[1], STDOUT_FILENO
) == -1 ||
660 dup2(pipeErr
[1], STDERR_FILENO
) == -1 )
662 wxLogSysError(_("Failed to redirect child process input/output"));
670 execvp (*mb_argv
, mb_argv
);
672 // there is no return after successful exec()
675 // some compilers complain about missing return - of course, they
676 // should know that exit() doesn't return but what else can we do if
678 #if defined(__VMS) || defined(__INTEL_COMPILER)
682 else // we're in parent
686 // pipe initialization: construction of the wxStreams
688 wxStreamTempBuffer bufIn
, bufErr
;
689 #endif // wxUSE_STREAMS
691 if ( process
&& process
->IsRedirected() )
694 // in/out for subprocess correspond to our out/in
695 wxOutputStream
*outStream
= new wxProcessFileOutputStream(pipeIn
[1]);
696 wxInputStream
*inStream
= new wxProcessFileInputStream(pipeOut
[0]);
697 wxInputStream
*errStream
= new wxProcessFileInputStream(pipeErr
[0]);
699 process
->SetPipeStreams(inStream
, outStream
, errStream
);
701 bufIn
.Init(inStream
);
702 bufErr
.Init(inStream
);
703 #endif // wxUSE_STREAMS
705 close(pipeIn
[0]); // close reading side
706 close(pipeOut
[1]); // close writing side
707 close(pipeErr
[1]); // close writing side
710 #if wxUSE_GUI && !defined(__WXMICROWIN__)
711 wxEndProcessData
*data
= new wxEndProcessData
;
715 // we may have process for capturing the program output, but it's
716 // not used in wxEndProcessData in the case of sync execution
717 data
->process
= NULL
;
719 // sync execution: indicate it by negating the pid
721 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
723 close(end_proc_detect
[1]); // close writing side
728 // data->pid will be set to 0 from GTK_EndProcessDetector when the
729 // process terminates
730 while ( data
->pid
!= 0 )
735 #endif // wxUSE_STREAMS
737 // give GTK+ a chance to call GTK_EndProcessDetector here and
738 // also repaint the GUI
742 int exitcode
= data
->exitcode
;
748 else // async execution
750 // async execution, nothing special to do - caller will be
751 // notified about the process termination if process != NULL, data
752 // will be deleted in GTK_EndProcessDetector
753 data
->process
= process
;
755 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
757 close(end_proc_detect
[1]); // close writing side
762 wxASSERT_MSG( sync
, wxT("async execution not supported yet") );
765 if ( waitpid(pid
, &exitcode
, 0) == -1 || !WIFEXITED(exitcode
) )
767 wxLogSysError(_("Waiting for subprocess termination failed"));
775 #undef ERROR_RETURN_CODE
778 // ----------------------------------------------------------------------------
779 // file and directory functions
780 // ----------------------------------------------------------------------------
782 const wxChar
* wxGetHomeDir( wxString
*home
)
784 *home
= wxGetUserHome( wxString() );
786 if ( home
->IsEmpty() )
790 if ( tmp
.Last() != wxT(']'))
791 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
793 return home
->c_str();
797 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
798 #else // just for binary compatibility -- there is no 'const' here
799 char *wxGetUserHome( const wxString
&user
)
802 struct passwd
*who
= (struct passwd
*) NULL
;
808 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
812 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
814 who
= getpwnam(wxConvertWX2MB(ptr
));
817 // We now make sure the the user exists!
820 who
= getpwuid(getuid());
825 who
= getpwnam (user
.mb_str());
828 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
831 // ----------------------------------------------------------------------------
832 // network and user id routines
833 // ----------------------------------------------------------------------------
835 // retrieve either the hostname or FQDN depending on platform (caller must
836 // check whether it's one or the other, this is why this function is for
838 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
840 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
844 // we're using uname() which is POSIX instead of less standard sysinfo()
845 #if defined(HAVE_UNAME)
847 bool ok
= uname(&uts
) != -1;
850 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
853 #elif defined(HAVE_GETHOSTNAME)
854 bool ok
= gethostname(buf
, sz
) != -1;
855 #else // no uname, no gethostname
856 wxFAIL_MSG(wxT("don't know host name for this machine"));
859 #endif // uname/gethostname
863 wxLogSysError(_("Cannot get the hostname"));
869 bool wxGetHostName(wxChar
*buf
, int sz
)
871 bool ok
= wxGetHostNameInternal(buf
, sz
);
875 // BSD systems return the FQDN, we only want the hostname, so extract
876 // it (we consider that dots are domain separators)
877 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
888 bool wxGetFullHostName(wxChar
*buf
, int sz
)
890 bool ok
= wxGetHostNameInternal(buf
, sz
);
894 if ( !wxStrchr(buf
, wxT('.')) )
896 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
899 wxLogSysError(_("Cannot get the official hostname"));
905 // the canonical name
906 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
909 //else: it's already a FQDN (BSD behaves this way)
915 bool wxGetUserId(wxChar
*buf
, int sz
)
920 if ((who
= getpwuid(getuid ())) != NULL
)
922 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
929 bool wxGetUserName(wxChar
*buf
, int sz
)
934 if ((who
= getpwuid (getuid ())) != NULL
)
936 // pw_gecos field in struct passwd is not standard
938 char *comma
= strchr(who
->pw_gecos
, ',');
940 *comma
= '\0'; // cut off non-name comment fields
941 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
942 #else // !HAVE_PW_GECOS
943 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
944 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
952 wxString
wxGetOsDescription()
954 #ifndef WXWIN_OS_DESCRIPTION
955 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
957 return WXWIN_OS_DESCRIPTION
;
962 // this function returns the GUI toolkit version in GUI programs, but OS
963 // version in non-GUI ones
966 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
971 if ( sscanf(WXWIN_OS_DESCRIPTION
, "%s %d.%d", name
, &major
, &minor
) != 3 )
973 // unreckognized uname string format
987 long wxGetFreeMemory()
989 #if defined(__LINUX__)
990 // get it from /proc/meminfo
991 FILE *fp
= fopen("/proc/meminfo", "r");
997 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
999 long memTotal
, memUsed
;
1000 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
1007 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
1008 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
1009 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
1012 // can't find it out
1016 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
1018 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
1020 if ( statfs(path
, &fs
) != 0 )
1022 wxLogSysError("Failed to get file system statistics");
1027 // under Solaris we might have to use fs.f_frsize instead as I think it
1028 // may be a multiple of the block size in general (TODO)
1032 *pTotal
= wxLongLong(fs
.f_blocks
) * fs
.f_bsize
;
1037 *pFree
= wxLongLong(fs
.f_bavail
) * fs
.f_bsize
;
1041 #endif // HAVE_STATFS
1046 // ----------------------------------------------------------------------------
1048 // ----------------------------------------------------------------------------
1050 bool wxGetEnv(const wxString
& var
, wxString
*value
)
1052 // wxGetenv is defined as getenv()
1053 wxChar
*p
= wxGetenv(var
);
1065 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
1067 #if defined(HAVE_SETENV)
1068 return setenv(variable
.mb_str(),
1069 value
? (const char *)wxString(value
).mb_str()
1071 1 /* overwrite */) == 0;
1072 #elif defined(HAVE_PUTENV)
1073 wxString s
= variable
;
1075 s
<< _T('=') << value
;
1077 // transform to ANSI
1078 const char *p
= s
.mb_str();
1080 // the string will be free()d by libc
1081 char *buf
= (char *)malloc(strlen(p
) + 1);
1084 return putenv(buf
) == 0;
1085 #else // no way to set an env var
1090 // ----------------------------------------------------------------------------
1092 // ----------------------------------------------------------------------------
1094 #if wxUSE_ON_FATAL_EXCEPTION
1098 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
1102 // give the user a chance to do something special about this
1103 wxTheApp
->OnFatalException();
1109 bool wxHandleFatalExceptions(bool doit
)
1112 static bool s_savedHandlers
= FALSE
;
1113 static struct sigaction s_handlerFPE
,
1119 if ( doit
&& !s_savedHandlers
)
1121 // install the signal handler
1122 struct sigaction act
;
1124 // some systems extend it with non std fields, so zero everything
1125 memset(&act
, 0, sizeof(act
));
1127 act
.sa_handler
= wxFatalSignalHandler
;
1128 sigemptyset(&act
.sa_mask
);
1131 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1132 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1133 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1134 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1137 wxLogDebug(_T("Failed to install our signal handler."));
1140 s_savedHandlers
= TRUE
;
1142 else if ( s_savedHandlers
)
1144 // uninstall the signal handler
1145 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1146 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1147 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1148 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1151 wxLogDebug(_T("Failed to uninstall our signal handler."));
1154 s_savedHandlers
= FALSE
;
1156 //else: nothing to do
1161 #endif // wxUSE_ON_FATAL_EXCEPTION
1163 // ----------------------------------------------------------------------------
1164 // error and debug output routines (deprecated, use wxLog)
1165 // ----------------------------------------------------------------------------
1167 void wxDebugMsg( const char *format
, ... )
1170 va_start( ap
, format
);
1171 vfprintf( stderr
, format
, ap
);
1176 void wxError( const wxString
&msg
, const wxString
&title
)
1178 wxFprintf( stderr
, _("Error ") );
1179 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1180 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1181 wxFprintf( stderr
, wxT(".\n") );
1184 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1186 wxFprintf( stderr
, _("Error ") );
1187 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1188 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1189 wxFprintf( stderr
, wxT(".\n") );
1190 exit(3); // the same exit code as for abort()