1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/utilsunx.cpp
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 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #include "wx/string.h"
29 #include "wx/apptrait.h"
32 #include "wx/process.h"
33 #include "wx/thread.h"
35 #include "wx/wfstream.h"
37 #include "wx/unix/execute.h"
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"
45 #endif // wxUSE_STREAMS
49 #if defined(__MWERKS__) && defined(__MACH__)
50 #ifndef WXWIN_OS_DESCRIPTION
51 #define WXWIN_OS_DESCRIPTION "MacOS X"
53 #ifndef HAVE_NANOSLEEP
54 #define HAVE_NANOSLEEP
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...
64 #undef wxUSE_ON_FATAL_EXCEPTION
65 #define wxUSE_ON_FATAL_EXCEPTION 0
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
76 #include <sys/param.h>
77 #include <sys/mount.h>
80 #endif // __BSD__/!__BSD__
82 #define wxStatfs statfs
86 #include <sys/statvfs.h>
88 #define wxStatfs statvfs
89 #endif // HAVE_STATVFS
91 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
92 // WX_STATFS_T is detected by configure
93 #define wxStatfs_t WX_STATFS_T
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
105 #include <sys/stat.h>
106 #include <sys/types.h>
107 #include <sys/wait.h>
112 #include <fcntl.h> // for O_WRONLY and friends
113 #include <time.h> // nanosleep() and/or usleep()
114 #include <ctype.h> // isspace()
115 #include <sys/time.h> // needed for FD_SETSIZE
118 #include <sys/utsname.h> // for uname()
121 // ----------------------------------------------------------------------------
122 // conditional compilation
123 // ----------------------------------------------------------------------------
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.
128 #if !defined(HAVE_USLEEP) && \
129 ((defined(__SUN__) && !defined(__SunOs_5_6) && \
130 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
131 defined(__osf__) || defined(__EMX__))
135 int usleep(unsigned int usec
);
138 /* I copied this from the XFree86 diffs. AV. */
139 #define INCL_DOSPROCESS
141 inline void usleep(unsigned long delay
)
143 DosSleep(delay
? (delay
/1000l) : 1l);
145 #else // !Sun && !EMX
146 void usleep(unsigned long usec
);
148 #endif // Sun/EMX/Something else
151 #define HAVE_USLEEP 1
152 #endif // Unices without usleep()
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 void wxSleep(int nSecs
)
167 void wxMicroSleep(unsigned long microseconds
)
169 #if defined(HAVE_NANOSLEEP)
171 tmReq
.tv_sec
= (time_t)(microseconds
/ 1000000);
172 tmReq
.tv_nsec
= (microseconds
% 1000000) * 1000;
174 // we're not interested in remaining time nor in return value
175 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
176 #elif defined(HAVE_USLEEP)
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
181 #if defined(__SUN__) && wxUSE_THREADS
182 #error "usleep() cannot be used in MT programs under Solaris."
185 usleep(microseconds
);
186 #elif defined(HAVE_SLEEP)
187 // under BeOS sleep() takes seconds (what about other platforms, if any?)
188 sleep(microseconds
* 1000000);
189 #else // !sleep function
190 #error "usleep() or nanosleep() function required for wxMicroSleep"
191 #endif // sleep function
194 void wxMilliSleep(unsigned long milliseconds
)
196 wxMicroSleep(milliseconds
*1000);
199 // ----------------------------------------------------------------------------
200 // process management
201 // ----------------------------------------------------------------------------
203 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
, int flags
)
205 int err
= kill((pid_t
) (flags
& wxKILL_CHILDREN
) ? -pid
: pid
, (int)sig
);
208 switch ( err
? errno
: 0 )
215 *rc
= wxKILL_BAD_SIGNAL
;
219 *rc
= wxKILL_ACCESS_DENIED
;
223 *rc
= wxKILL_NO_PROCESS
;
227 // this goes against Unix98 docs so log it
228 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
238 #define WXEXECUTE_NARGS 127
240 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
242 wxCHECK_MSG( !command
.empty(), 0, wxT("can't exec empty command") );
245 // fork() doesn't mix well with POSIX threads: on many systems the program
246 // deadlocks or crashes for some reason. Probably our code is buggy and
247 // doesn't do something which must be done to allow this to work, but I
248 // don't know what yet, so for now just warn the user (this is the least we
250 wxASSERT_MSG( wxThread::IsMain(),
251 _T("wxExecute() can be called only from the main thread") );
252 #endif // wxUSE_THREADS
255 wxChar
*argv
[WXEXECUTE_NARGS
];
257 const wxChar
*cptr
= command
.c_str();
258 wxChar quotechar
= wxT('\0'); // is arg quoted?
259 bool escaped
= false;
261 // split the command line in arguments
265 quotechar
= wxT('\0');
267 // eat leading whitespace:
268 while ( wxIsspace(*cptr
) )
271 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
276 if ( *cptr
== wxT('\\') && ! escaped
)
283 // all other characters:
287 // have we reached the end of the argument?
288 if ( (*cptr
== quotechar
&& ! escaped
)
289 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
290 || *cptr
== wxT('\0') )
292 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
293 wxT("too many arguments in wxExecute") );
295 argv
[argc
] = new wxChar
[argument
.length() + 1];
296 wxStrcpy(argv
[argc
], argument
.c_str());
299 // if not at end of buffer, swallow last character:
303 break; // done with this one, start over
309 // do execute the command
310 long lRc
= wxExecute(argv
, flags
, process
);
315 delete [] argv
[argc
++];
320 // ----------------------------------------------------------------------------
322 // ----------------------------------------------------------------------------
324 static wxString
wxMakeShellCommand(const wxString
& command
)
329 // just an interactive shell
334 // execute command in a shell
335 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
341 bool wxShell(const wxString
& command
)
343 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
346 bool wxShell(const wxString
& command
, wxArrayString
& output
)
348 wxCHECK_MSG( !command
.empty(), false, _T("can't exec shell non interactively") );
350 return wxExecute(wxMakeShellCommand(command
), output
);
353 // Shutdown or reboot the PC
354 bool wxShutdown(wxShutdownFlags wFlags
)
359 case wxSHUTDOWN_POWEROFF
:
363 case wxSHUTDOWN_REBOOT
:
368 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
372 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
375 wxPowerType
wxGetPowerType()
378 return wxPOWER_UNKNOWN
;
381 wxBatteryState
wxGetBatteryState()
384 return wxBATTERY_UNKNOWN_STATE
;
387 // ----------------------------------------------------------------------------
388 // wxStream classes to support IO redirection in wxExecute
389 // ----------------------------------------------------------------------------
393 bool wxPipeInputStream::CanRead() const
395 if ( m_lasterror
== wxSTREAM_EOF
)
398 // check if there is any input available
403 const int fd
= m_file
->fd();
407 FD_SET(fd
, &readfds
);
408 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
411 wxLogSysError(_("Impossible to get child process input"));
418 wxFAIL_MSG(_T("unexpected select() return value"));
419 // still fall through
422 // input available -- or maybe not, as select() returns 1 when a
423 // read() will complete without delay, but it could still not read
429 #endif // wxUSE_STREAMS
431 // ----------------------------------------------------------------------------
432 // wxExecute: the real worker function
433 // ----------------------------------------------------------------------------
436 #pragma message disable codeunreachable
439 long wxExecute(wxChar
**argv
,
443 // for the sync execution, we return -1 to indicate failure, but for async
444 // case we return 0 which is never a valid PID
446 // we define this as a macro, not a variable, to avoid compiler warnings
447 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
448 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
450 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
454 char *mb_argv
[WXEXECUTE_NARGS
];
456 while (argv
[mb_argc
])
458 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
459 mb_argv
[mb_argc
] = strdup(mb_arg
);
462 mb_argv
[mb_argc
] = (char *) NULL
;
464 // this macro will free memory we used above
465 #define ARGS_CLEANUP \
466 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
467 free(mb_argv[mb_argc])
469 // no need for cleanup
472 wxChar
**mb_argv
= argv
;
473 #endif // Unicode/ANSI
475 // we want this function to work even if there is no wxApp so ensure that
476 // we have a valid traits pointer
477 wxConsoleAppTraits traitsConsole
;
478 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
480 traits
= &traitsConsole
;
482 // this struct contains all information which we pass to and from
483 // wxAppTraits methods
484 wxExecuteData execData
;
485 execData
.flags
= flags
;
486 execData
.process
= process
;
489 if ( !traits
->CreateEndProcessPipe(execData
) )
491 wxLogError( _("Failed to execute '%s'\n"), *argv
);
495 return ERROR_RETURN_CODE
;
498 // pipes for inter process communication
499 wxPipe pipeIn
, // stdin
503 if ( process
&& process
->IsRedirected() )
505 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
507 wxLogError( _("Failed to execute '%s'\n"), *argv
);
511 return ERROR_RETURN_CODE
;
517 // NB: do *not* use vfork() here, it completely breaks this code for some
518 // reason under Solaris (and maybe others, although not under Linux)
519 // But on OpenVMS we do not have fork so we have to use vfork and
520 // cross our fingers that it works.
526 if ( pid
== -1 ) // error?
528 wxLogSysError( _("Fork failed") );
532 return ERROR_RETURN_CODE
;
534 else if ( pid
== 0 ) // we're in child
536 // These lines close the open file descriptors to to avoid any
537 // input/output which might block the process or irritate the user. If
538 // one wants proper IO for the subprocess, the right thing to do is to
539 // start an xterm executing it.
540 if ( !(flags
& wxEXEC_SYNC
) )
542 // FD_SETSIZE is unsigned under BSD, signed under other platforms
543 // so we need a cast to avoid warnings on all platforms
544 for ( int fd
= 0; fd
< (int)FD_SETSIZE
; fd
++ )
546 if ( fd
== pipeIn
[wxPipe::Read
]
547 || fd
== pipeOut
[wxPipe::Write
]
548 || fd
== pipeErr
[wxPipe::Write
]
549 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
551 // don't close this one, we still need it
555 // leave stderr opened too, it won't do any harm
556 if ( fd
!= STDERR_FILENO
)
561 #if !defined(__VMS) && !defined(__EMX__)
562 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
564 // Set process group to child process' pid. Then killing -pid
565 // of the parent will kill the process and all of its children.
570 // reading side can be safely closed but we should keep the write one
572 traits
->DetachWriteFDOfEndProcessPipe(execData
);
574 // redirect stdin, stdout and stderr
577 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
578 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
579 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
581 wxLogSysError(_("Failed to redirect child process input/output"));
589 execvp (*mb_argv
, mb_argv
);
591 fprintf(stderr
, "execvp(");
592 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
593 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
594 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
595 fprintf(stderr
, ") failed with error %d!\n", errno
);
597 // there is no return after successful exec()
600 // some compilers complain about missing return - of course, they
601 // should know that exit() doesn't return but what else can we do if
604 // and, sure enough, other compilers complain about unreachable code
605 // after exit() call, so we can just always have return here...
606 #if defined(__VMS) || defined(__INTEL_COMPILER)
610 else // we're in parent
614 // save it for WaitForChild() use
617 // prepare for IO redirection
620 // the input buffer bufOut is connected to stdout, this is why it is
621 // called bufOut and not bufIn
622 wxStreamTempInputBuffer bufOut
,
624 #endif // wxUSE_STREAMS
626 if ( process
&& process
->IsRedirected() )
629 wxOutputStream
*inStream
=
630 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
632 wxPipeInputStream
*outStream
=
633 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
635 wxPipeInputStream
*errStream
=
636 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
638 process
->SetPipeStreams(outStream
, inStream
, errStream
);
640 bufOut
.Init(outStream
);
641 bufErr
.Init(errStream
);
643 execData
.bufOut
= &bufOut
;
644 execData
.bufErr
= &bufErr
;
645 #endif // wxUSE_STREAMS
655 return traits
->WaitForChild(execData
);
658 return ERROR_RETURN_CODE
;
662 #pragma message enable codeunreachable
665 #undef ERROR_RETURN_CODE
668 // ----------------------------------------------------------------------------
669 // file and directory functions
670 // ----------------------------------------------------------------------------
672 const wxChar
* wxGetHomeDir( wxString
*home
)
674 *home
= wxGetUserHome( wxEmptyString
);
680 if ( tmp
.Last() != wxT(']'))
681 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
683 return home
->c_str();
687 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
688 #else // just for binary compatibility -- there is no 'const' here
689 char *wxGetUserHome( const wxString
&user
)
692 struct passwd
*who
= (struct passwd
*) NULL
;
698 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
701 wxWCharBuffer
buffer( ptr
);
707 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
709 who
= getpwnam(wxConvertWX2MB(ptr
));
712 // We now make sure the the user exists!
715 who
= getpwuid(getuid());
720 who
= getpwnam (user
.mb_str());
723 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
726 // ----------------------------------------------------------------------------
727 // network and user id routines
728 // ----------------------------------------------------------------------------
730 // retrieve either the hostname or FQDN depending on platform (caller must
731 // check whether it's one or the other, this is why this function is for
733 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
735 wxCHECK_MSG( buf
, false, wxT("NULL pointer in wxGetHostNameInternal") );
739 // we're using uname() which is POSIX instead of less standard sysinfo()
740 #if defined(HAVE_UNAME)
742 bool ok
= uname(&uts
) != -1;
745 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
748 #elif defined(HAVE_GETHOSTNAME)
749 bool ok
= gethostname(buf
, sz
) != -1;
750 #else // no uname, no gethostname
751 wxFAIL_MSG(wxT("don't know host name for this machine"));
754 #endif // uname/gethostname
758 wxLogSysError(_("Cannot get the hostname"));
764 bool wxGetHostName(wxChar
*buf
, int sz
)
766 bool ok
= wxGetHostNameInternal(buf
, sz
);
770 // BSD systems return the FQDN, we only want the hostname, so extract
771 // it (we consider that dots are domain separators)
772 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
783 bool wxGetFullHostName(wxChar
*buf
, int sz
)
785 bool ok
= wxGetHostNameInternal(buf
, sz
);
789 if ( !wxStrchr(buf
, wxT('.')) )
791 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
794 wxLogSysError(_("Cannot get the official hostname"));
800 // the canonical name
801 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
804 //else: it's already a FQDN (BSD behaves this way)
810 bool wxGetUserId(wxChar
*buf
, int sz
)
815 if ((who
= getpwuid(getuid ())) != NULL
)
817 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
824 bool wxGetUserName(wxChar
*buf
, int sz
)
829 if ((who
= getpwuid (getuid ())) != NULL
)
831 // pw_gecos field in struct passwd is not standard
833 char *comma
= strchr(who
->pw_gecos
, ',');
835 *comma
= '\0'; // cut off non-name comment fields
836 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
837 #else // !HAVE_PW_GECOS
838 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
839 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
846 // this function is in mac/utils.cpp for wxMac
849 wxString
wxGetOsDescription()
851 FILE *f
= popen("uname -s -r -m", "r");
855 size_t c
= fread(buf
, 1, sizeof(buf
) - 1, f
);
857 // Trim newline from output.
858 if (c
&& buf
[c
- 1] == '\n')
861 return wxString::FromAscii( buf
);
863 wxFAIL_MSG( _T("uname failed") );
869 unsigned long wxGetProcessId()
871 return (unsigned long)getpid();
874 wxMemorySize
wxGetFreeMemory()
876 #if defined(__LINUX__)
877 // get it from /proc/meminfo
878 FILE *fp
= fopen("/proc/meminfo", "r");
884 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
886 long memTotal
, memUsed
;
887 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
892 return (wxMemorySize
)memFree
;
894 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
895 return (wxMemorySize
)(sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
));
896 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
903 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
905 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
906 // the case to "char *" is needed for AIX 4.3
908 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
910 wxLogSysError( wxT("Failed to get file system statistics") );
915 // under Solaris we also have to use f_frsize field instead of f_bsize
916 // which is in general a multiple of f_frsize
918 wxLongLong blockSize
= fs
.f_frsize
;
920 wxLongLong blockSize
= fs
.f_bsize
;
921 #endif // HAVE_STATVFS/HAVE_STATFS
925 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
930 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
934 #else // !HAVE_STATFS && !HAVE_STATVFS
936 #endif // HAVE_STATFS
939 // ----------------------------------------------------------------------------
941 // ----------------------------------------------------------------------------
943 bool wxGetEnv(const wxString
& var
, wxString
*value
)
945 // wxGetenv is defined as getenv()
946 wxChar
*p
= wxGetenv(var
);
958 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
960 #if defined(HAVE_SETENV)
961 return setenv(variable
.mb_str(),
962 value
? (const char *)wxString(value
).mb_str()
964 1 /* overwrite */) == 0;
965 #elif defined(HAVE_PUTENV)
966 wxString s
= variable
;
968 s
<< _T('=') << value
;
971 const wxWX2MBbuf p
= s
.mb_str();
973 // the string will be free()d by libc
974 char *buf
= (char *)malloc(strlen(p
) + 1);
977 return putenv(buf
) == 0;
978 #else // no way to set an env var
983 // ----------------------------------------------------------------------------
985 // ----------------------------------------------------------------------------
987 #if wxUSE_ON_FATAL_EXCEPTION
991 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
995 // give the user a chance to do something special about this
996 wxTheApp
->OnFatalException();
1002 bool wxHandleFatalExceptions(bool doit
)
1005 static bool s_savedHandlers
= false;
1006 static struct sigaction s_handlerFPE
,
1012 if ( doit
&& !s_savedHandlers
)
1014 // install the signal handler
1015 struct sigaction act
;
1017 // some systems extend it with non std fields, so zero everything
1018 memset(&act
, 0, sizeof(act
));
1020 act
.sa_handler
= wxFatalSignalHandler
;
1021 sigemptyset(&act
.sa_mask
);
1024 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1025 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1026 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1027 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1030 wxLogDebug(_T("Failed to install our signal handler."));
1033 s_savedHandlers
= true;
1035 else if ( s_savedHandlers
)
1037 // uninstall the signal handler
1038 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1039 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1040 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1041 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1044 wxLogDebug(_T("Failed to uninstall our signal handler."));
1047 s_savedHandlers
= false;
1049 //else: nothing to do
1054 #endif // wxUSE_ON_FATAL_EXCEPTION
1056 // ----------------------------------------------------------------------------
1057 // error and debug output routines (deprecated, use wxLog)
1058 // ----------------------------------------------------------------------------
1060 #if WXWIN_COMPATIBILITY_2_2
1062 void wxDebugMsg( const char *format
, ... )
1065 va_start( ap
, format
);
1066 vfprintf( stderr
, format
, ap
);
1071 void wxError( const wxString
&msg
, const wxString
&title
)
1073 wxFprintf( stderr
, _("Error ") );
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") );
1079 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1081 wxFprintf( stderr
, _("Error ") );
1082 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1083 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1084 wxFprintf( stderr
, wxT(".\n") );
1085 exit(3); // the same exit code as for abort()
1088 #endif // WXWIN_COMPATIBILITY_2_2
1090 #endif // wxUSE_BASE
1094 // ----------------------------------------------------------------------------
1095 // wxExecute support
1096 // ----------------------------------------------------------------------------
1098 // Darwin doesn't use the same process end detection mechanisms so we don't
1099 // need wxExecute-related helpers for it
1100 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1102 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1104 return execData
.pipeEndProcDetect
.Create();
1107 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1109 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1112 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1114 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1115 execData
.pipeEndProcDetect
.Close();
1120 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1126 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1133 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1135 // nothing to do here, we don't use the pipe
1138 #endif // !Darwin/Darwin
1140 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1142 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1144 const int flags
= execData
.flags
;
1146 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1147 // callback function directly if the process terminates before
1148 // the callback can be added to the run loop. Set up the endProcData.
1149 if ( flags
& wxEXEC_SYNC
)
1151 // we may have process for capturing the program output, but it's
1152 // not used in wxEndProcessData in the case of sync execution
1153 endProcData
->process
= NULL
;
1155 // sync execution: indicate it by negating the pid
1156 endProcData
->pid
= -execData
.pid
;
1160 // async execution, nothing special to do -- caller will be
1161 // notified about the process termination if process != NULL, endProcData
1162 // will be deleted in GTK_EndProcessDetector
1163 endProcData
->process
= execData
.process
;
1164 endProcData
->pid
= execData
.pid
;
1168 #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))
1169 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1171 endProcData
->tag
= wxAddProcessCallback
1174 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1177 execData
.pipeEndProcDetect
.Close();
1178 #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))
1180 if ( flags
& wxEXEC_SYNC
)
1183 wxWindowDisabler
*wd
= flags
& wxEXEC_NODISABLE
? NULL
1184 : new wxWindowDisabler
;
1186 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1187 // process terminates
1188 while ( endProcData
->pid
!= 0 )
1193 if ( execData
.bufOut
)
1195 execData
.bufOut
->Update();
1199 if ( execData
.bufErr
)
1201 execData
.bufErr
->Update();
1204 #endif // wxUSE_STREAMS
1206 // don't consume 100% of the CPU while we're sitting in this
1211 // give GTK+ a chance to call GTK_EndProcessDetector here and
1212 // also repaint the GUI
1216 int exitcode
= endProcData
->exitcode
;
1223 else // async execution
1225 return execData
.pid
;
1232 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1234 // notify user about termination if required
1235 if ( proc_data
->process
)
1237 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1241 if ( proc_data
->pid
> 0 )
1247 // let wxExecute() know that the process has terminated
1252 #endif // wxUSE_BASE