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
);
218 *rc
= wxKILL_BAD_SIGNAL
;
222 *rc
= wxKILL_ACCESS_DENIED
;
226 *rc
= wxKILL_NO_PROCESS
;
230 // this goes against Unix98 docs so log it
231 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
241 #define WXEXECUTE_NARGS 127
243 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
245 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
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
253 wxASSERT_MSG( wxThread::IsMain(),
254 _T("wxExecute() can be called only from the main thread") );
255 #endif // wxUSE_THREADS
258 wxChar
*argv
[WXEXECUTE_NARGS
];
260 const wxChar
*cptr
= command
.c_str();
261 wxChar quotechar
= wxT('\0'); // is arg quoted?
262 bool escaped
= false;
264 // split the command line in arguments
268 quotechar
= wxT('\0');
270 // eat leading whitespace:
271 while ( wxIsspace(*cptr
) )
274 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
279 if ( *cptr
== wxT('\\') && ! escaped
)
286 // all other characters:
290 // have we reached the end of the argument?
291 if ( (*cptr
== quotechar
&& ! escaped
)
292 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
293 || *cptr
== wxT('\0') )
295 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
296 wxT("too many arguments in wxExecute") );
298 argv
[argc
] = new wxChar
[argument
.length() + 1];
299 wxStrcpy(argv
[argc
], argument
.c_str());
302 // if not at end of buffer, swallow last character:
306 break; // done with this one, start over
312 // do execute the command
313 long lRc
= wxExecute(argv
, flags
, process
);
318 delete [] argv
[argc
++];
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 static wxString
wxMakeShellCommand(const wxString
& command
)
332 // just an interactive shell
337 // execute command in a shell
338 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
344 bool wxShell(const wxString
& command
)
346 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
349 bool wxShell(const wxString
& command
, wxArrayString
& output
)
351 wxCHECK_MSG( !command
.empty(), false, _T("can't exec shell non interactively") );
353 return wxExecute(wxMakeShellCommand(command
), output
);
356 // Shutdown or reboot the PC
357 bool wxShutdown(wxShutdownFlags wFlags
)
362 case wxSHUTDOWN_POWEROFF
:
366 case wxSHUTDOWN_REBOOT
:
371 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
375 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
379 // ----------------------------------------------------------------------------
380 // wxStream classes to support IO redirection in wxExecute
381 // ----------------------------------------------------------------------------
385 bool wxPipeInputStream::CanRead() const
387 if ( m_lasterror
== wxSTREAM_EOF
)
390 // check if there is any input available
395 const int fd
= m_file
->fd();
399 FD_SET(fd
, &readfds
);
400 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
403 wxLogSysError(_("Impossible to get child process input"));
410 wxFAIL_MSG(_T("unexpected select() return value"));
411 // still fall through
414 // input available -- or maybe not, as select() returns 1 when a
415 // read() will complete without delay, but it could still not read
421 #endif // wxUSE_STREAMS
423 // ----------------------------------------------------------------------------
424 // wxExecute: the real worker function
425 // ----------------------------------------------------------------------------
428 #pragma message disable codeunreachable
431 long wxExecute(wxChar
**argv
,
435 // for the sync execution, we return -1 to indicate failure, but for async
436 // case we return 0 which is never a valid PID
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()"
440 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
442 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
446 char *mb_argv
[WXEXECUTE_NARGS
];
448 while (argv
[mb_argc
])
450 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
451 mb_argv
[mb_argc
] = strdup(mb_arg
);
454 mb_argv
[mb_argc
] = (char *) NULL
;
456 // this macro will free memory we used above
457 #define ARGS_CLEANUP \
458 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
459 free(mb_argv[mb_argc])
461 // no need for cleanup
464 wxChar
**mb_argv
= argv
;
465 #endif // Unicode/ANSI
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
;
472 traits
= &traitsConsole
;
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
;
481 if ( !traits
->CreateEndProcessPipe(execData
) )
483 wxLogError( _("Failed to execute '%s'\n"), *argv
);
487 return ERROR_RETURN_CODE
;
490 // pipes for inter process communication
491 wxPipe pipeIn
, // stdin
495 if ( process
&& process
->IsRedirected() )
497 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
499 wxLogError( _("Failed to execute '%s'\n"), *argv
);
503 return ERROR_RETURN_CODE
;
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)
511 // But on OpenVMS we do not have fork so we have to use vfork and
512 // cross our fingers that it works.
518 if ( pid
== -1 ) // error?
520 wxLogSysError( _("Fork failed") );
524 return ERROR_RETURN_CODE
;
526 else if ( pid
== 0 ) // we're in child
528 // These lines close the open file descriptors to to avoid any
529 // input/output which might block the process or irritate the user. If
530 // one wants proper IO for the subprocess, the right thing to do is to
531 // start an xterm executing it.
532 if ( !(flags
& wxEXEC_SYNC
) )
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
++ )
538 if ( fd
== pipeIn
[wxPipe::Read
]
539 || fd
== pipeOut
[wxPipe::Write
]
540 || fd
== pipeErr
[wxPipe::Write
]
541 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
543 // don't close this one, we still need it
547 // leave stderr opened too, it won't do any harm
548 if ( fd
!= STDERR_FILENO
)
553 #if !defined(__VMS) && !defined(__EMX__)
554 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
556 // Set process group to child process' pid. Then killing -pid
557 // of the parent will kill the process and all of its children.
562 // reading side can be safely closed but we should keep the write one
564 traits
->DetachWriteFDOfEndProcessPipe(execData
);
566 // redirect stdin, stdout and stderr
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 )
573 wxLogSysError(_("Failed to redirect child process input/output"));
581 execvp (*mb_argv
, mb_argv
);
583 fprintf(stderr
, "execvp(");
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_
);
587 fprintf(stderr
, ") failed with error %d!\n", errno
);
589 // there is no return after successful exec()
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
596 // and, sure enough, other compilers complain about unreachable code
597 // after exit() call, so we can just always have return here...
598 #if defined(__VMS) || defined(__INTEL_COMPILER)
602 else // we're in parent
606 // save it for WaitForChild() use
609 // prepare for IO redirection
612 // the input buffer bufOut is connected to stdout, this is why it is
613 // called bufOut and not bufIn
614 wxStreamTempInputBuffer bufOut
,
616 #endif // wxUSE_STREAMS
618 if ( process
&& process
->IsRedirected() )
621 wxOutputStream
*inStream
=
622 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
624 wxPipeInputStream
*outStream
=
625 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
627 wxPipeInputStream
*errStream
=
628 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
630 process
->SetPipeStreams(outStream
, inStream
, errStream
);
632 bufOut
.Init(outStream
);
633 bufErr
.Init(errStream
);
635 execData
.bufOut
= &bufOut
;
636 execData
.bufErr
= &bufErr
;
637 #endif // wxUSE_STREAMS
647 return traits
->WaitForChild(execData
);
650 return ERROR_RETURN_CODE
;
654 #pragma message enable codeunreachable
657 #undef ERROR_RETURN_CODE
660 // ----------------------------------------------------------------------------
661 // file and directory functions
662 // ----------------------------------------------------------------------------
664 const wxChar
* wxGetHomeDir( wxString
*home
)
666 *home
= wxGetUserHome( wxString() );
668 if ( home
->IsEmpty() )
672 if ( tmp
.Last() != wxT(']'))
673 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
675 return home
->c_str();
679 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
680 #else // just for binary compatibility -- there is no 'const' here
681 char *wxGetUserHome( const wxString
&user
)
684 struct passwd
*who
= (struct passwd
*) NULL
;
690 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
693 wxWCharBuffer
buffer( ptr
);
699 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
701 who
= getpwnam(wxConvertWX2MB(ptr
));
704 // We now make sure the the user exists!
707 who
= getpwuid(getuid());
712 who
= getpwnam (user
.mb_str());
715 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
718 // ----------------------------------------------------------------------------
719 // network and user id routines
720 // ----------------------------------------------------------------------------
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
725 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
727 wxCHECK_MSG( buf
, false, wxT("NULL pointer in wxGetHostNameInternal") );
731 // we're using uname() which is POSIX instead of less standard sysinfo()
732 #if defined(HAVE_UNAME)
734 bool ok
= uname(&uts
) != -1;
737 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
740 #elif defined(HAVE_GETHOSTNAME)
741 bool ok
= gethostname(buf
, sz
) != -1;
742 #else // no uname, no gethostname
743 wxFAIL_MSG(wxT("don't know host name for this machine"));
746 #endif // uname/gethostname
750 wxLogSysError(_("Cannot get the hostname"));
756 bool wxGetHostName(wxChar
*buf
, int sz
)
758 bool ok
= wxGetHostNameInternal(buf
, sz
);
762 // BSD systems return the FQDN, we only want the hostname, so extract
763 // it (we consider that dots are domain separators)
764 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
775 bool wxGetFullHostName(wxChar
*buf
, int sz
)
777 bool ok
= wxGetHostNameInternal(buf
, sz
);
781 if ( !wxStrchr(buf
, wxT('.')) )
783 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
786 wxLogSysError(_("Cannot get the official hostname"));
792 // the canonical name
793 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
796 //else: it's already a FQDN (BSD behaves this way)
802 bool wxGetUserId(wxChar
*buf
, int sz
)
807 if ((who
= getpwuid(getuid ())) != NULL
)
809 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
816 bool wxGetUserName(wxChar
*buf
, int sz
)
821 if ((who
= getpwuid (getuid ())) != NULL
)
823 // pw_gecos field in struct passwd is not standard
825 char *comma
= strchr(who
->pw_gecos
, ',');
827 *comma
= '\0'; // cut off non-name comment fields
828 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
829 #else // !HAVE_PW_GECOS
830 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
831 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
838 // this function is in mac/utils.cpp for wxMac
841 wxString
wxGetOsDescription()
843 FILE *f
= popen("uname -s -r -m", "r");
847 size_t c
= fread(buf
, 1, sizeof(buf
) - 1, f
);
849 // Trim newline from output.
850 if (c
&& buf
[c
- 1] == '\n')
853 return wxString::FromAscii( buf
);
855 wxFAIL_MSG( _T("uname failed") );
861 unsigned long wxGetProcessId()
863 return (unsigned long)getpid();
866 wxMemorySize
wxGetFreeMemory()
868 #if defined(__LINUX__)
869 // get it from /proc/meminfo
870 FILE *fp
= fopen("/proc/meminfo", "r");
876 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
878 long memTotal
, memUsed
;
879 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
884 return (wxMemorySize
)memFree
;
886 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
887 return (wxMemorySize
)(sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
));
888 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
895 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
897 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
898 // the case to "char *" is needed for AIX 4.3
900 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
902 wxLogSysError( wxT("Failed to get file system statistics") );
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
910 wxLongLong blockSize
= fs
.f_frsize
;
912 wxLongLong blockSize
= fs
.f_bsize
;
913 #endif // HAVE_STATVFS/HAVE_STATFS
917 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
922 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
926 #else // !HAVE_STATFS && !HAVE_STATVFS
928 #endif // HAVE_STATFS
931 // ----------------------------------------------------------------------------
933 // ----------------------------------------------------------------------------
935 bool wxGetEnv(const wxString
& var
, wxString
*value
)
937 // wxGetenv is defined as getenv()
938 wxChar
*p
= wxGetenv(var
);
950 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
952 #if defined(HAVE_SETENV)
953 return setenv(variable
.mb_str(),
954 value
? (const char *)wxString(value
).mb_str()
956 1 /* overwrite */) == 0;
957 #elif defined(HAVE_PUTENV)
958 wxString s
= variable
;
960 s
<< _T('=') << value
;
963 const wxWX2MBbuf p
= s
.mb_str();
965 // the string will be free()d by libc
966 char *buf
= (char *)malloc(strlen(p
) + 1);
969 return putenv(buf
) == 0;
970 #else // no way to set an env var
975 // ----------------------------------------------------------------------------
977 // ----------------------------------------------------------------------------
979 #if wxUSE_ON_FATAL_EXCEPTION
983 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
987 // give the user a chance to do something special about this
988 wxTheApp
->OnFatalException();
994 bool wxHandleFatalExceptions(bool doit
)
997 static bool s_savedHandlers
= false;
998 static struct sigaction s_handlerFPE
,
1004 if ( doit
&& !s_savedHandlers
)
1006 // install the signal handler
1007 struct sigaction act
;
1009 // some systems extend it with non std fields, so zero everything
1010 memset(&act
, 0, sizeof(act
));
1012 act
.sa_handler
= wxFatalSignalHandler
;
1013 sigemptyset(&act
.sa_mask
);
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;
1022 wxLogDebug(_T("Failed to install our signal handler."));
1025 s_savedHandlers
= true;
1027 else if ( s_savedHandlers
)
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;
1036 wxLogDebug(_T("Failed to uninstall our signal handler."));
1039 s_savedHandlers
= false;
1041 //else: nothing to do
1046 #endif // wxUSE_ON_FATAL_EXCEPTION
1048 // ----------------------------------------------------------------------------
1049 // error and debug output routines (deprecated, use wxLog)
1050 // ----------------------------------------------------------------------------
1052 #if WXWIN_COMPATIBILITY_2_2
1054 void wxDebugMsg( const char *format
, ... )
1057 va_start( ap
, format
);
1058 vfprintf( stderr
, format
, ap
);
1063 void wxError( const wxString
&msg
, const wxString
&title
)
1065 wxFprintf( stderr
, _("Error ") );
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") );
1071 void wxFatalError( 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") );
1077 exit(3); // the same exit code as for abort()
1080 #endif // WXWIN_COMPATIBILITY_2_2
1082 #endif // wxUSE_BASE
1086 // ----------------------------------------------------------------------------
1087 // wxExecute support
1088 // ----------------------------------------------------------------------------
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__))
1094 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1096 return execData
.pipeEndProcDetect
.Create();
1099 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1101 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1104 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1106 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1107 execData
.pipeEndProcDetect
.Close();
1112 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1118 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1125 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1127 // nothing to do here, we don't use the pipe
1130 #endif // !Darwin/Darwin
1132 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1134 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1136 const int flags
= execData
.flags
;
1138 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1139 // callback function directly if the process terminates before
1140 // the callback can be added to the run loop. Set up the endProcData.
1141 if ( flags
& wxEXEC_SYNC
)
1143 // we may have process for capturing the program output, but it's
1144 // not used in wxEndProcessData in the case of sync execution
1145 endProcData
->process
= NULL
;
1147 // sync execution: indicate it by negating the pid
1148 endProcData
->pid
= -execData
.pid
;
1152 // async execution, nothing special to do -- caller will be
1153 // notified about the process termination if process != NULL, endProcData
1154 // will be deleted in GTK_EndProcessDetector
1155 endProcData
->process
= execData
.process
;
1156 endProcData
->pid
= execData
.pid
;
1160 #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))
1161 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1163 endProcData
->tag
= wxAddProcessCallback
1166 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1169 execData
.pipeEndProcDetect
.Close();
1170 #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))
1172 if ( flags
& wxEXEC_SYNC
)
1175 wxWindowDisabler
*wd
= flags
& wxEXEC_NODISABLE
? NULL
1176 : new wxWindowDisabler
;
1178 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1179 // process terminates
1180 while ( endProcData
->pid
!= 0 )
1185 if ( execData
.bufOut
)
1187 execData
.bufOut
->Update();
1191 if ( execData
.bufErr
)
1193 execData
.bufErr
->Update();
1196 #endif // wxUSE_STREAMS
1198 // don't consume 100% of the CPU while we're sitting in this
1203 // give GTK+ a chance to call GTK_EndProcessDetector here and
1204 // also repaint the GUI
1208 int exitcode
= endProcData
->exitcode
;
1215 else // async execution
1217 return execData
.pid
;
1224 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1226 // notify user about termination if required
1227 if ( proc_data
->process
)
1229 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1233 if ( proc_data
->pid
> 0 )
1239 // let wxExecute() know that the process has terminated
1244 #endif // wxUSE_BASE