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") );
243 wxLogDebug(wxString(wxT("Launching: ")) + command
);
246 // fork() doesn't mix well with POSIX threads: on many systems the program
247 // deadlocks or crashes for some reason. Probably our code is buggy and
248 // doesn't do something which must be done to allow this to work, but I
249 // don't know what yet, so for now just warn the user (this is the least we
251 wxASSERT_MSG( wxThread::IsMain(),
252 _T("wxExecute() can be called only from the main thread") );
253 #endif // wxUSE_THREADS
256 wxChar
*argv
[WXEXECUTE_NARGS
];
258 const wxChar
*cptr
= command
.c_str();
259 wxChar quotechar
= wxT('\0'); // is arg quoted?
260 bool escaped
= false;
262 // split the command line in arguments
266 quotechar
= wxT('\0');
268 // eat leading whitespace:
269 while ( wxIsspace(*cptr
) )
272 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
277 if ( *cptr
== wxT('\\') && ! escaped
)
284 // all other characters:
288 // have we reached the end of the argument?
289 if ( (*cptr
== quotechar
&& ! escaped
)
290 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
291 || *cptr
== wxT('\0') )
293 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
294 wxT("too many arguments in wxExecute") );
296 argv
[argc
] = new wxChar
[argument
.length() + 1];
297 wxStrcpy(argv
[argc
], argument
.c_str());
300 // if not at end of buffer, swallow last character:
304 break; // done with this one, start over
310 // do execute the command
311 long lRc
= wxExecute(argv
, flags
, process
);
316 delete [] argv
[argc
++];
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 static wxString
wxMakeShellCommand(const wxString
& command
)
330 // just an interactive shell
335 // execute command in a shell
336 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
342 bool wxShell(const wxString
& command
)
344 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
347 bool wxShell(const wxString
& command
, wxArrayString
& output
)
349 wxCHECK_MSG( !command
.empty(), false, _T("can't exec shell non interactively") );
351 return wxExecute(wxMakeShellCommand(command
), output
);
354 // Shutdown or reboot the PC
355 bool wxShutdown(wxShutdownFlags wFlags
)
360 case wxSHUTDOWN_POWEROFF
:
364 case wxSHUTDOWN_REBOOT
:
369 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
373 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
376 wxPowerType
wxGetPowerType()
379 return wxPOWER_UNKNOWN
;
382 wxBatteryState
wxGetBatteryState()
385 return wxBATTERY_UNKNOWN_STATE
;
388 // ----------------------------------------------------------------------------
389 // wxStream classes to support IO redirection in wxExecute
390 // ----------------------------------------------------------------------------
394 bool wxPipeInputStream::CanRead() const
396 if ( m_lasterror
== wxSTREAM_EOF
)
399 // check if there is any input available
404 const int fd
= m_file
->fd();
408 FD_SET(fd
, &readfds
);
409 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
412 wxLogSysError(_("Impossible to get child process input"));
419 wxFAIL_MSG(_T("unexpected select() return value"));
420 // still fall through
423 // input available -- or maybe not, as select() returns 1 when a
424 // read() will complete without delay, but it could still not read
430 #endif // wxUSE_STREAMS
432 // ----------------------------------------------------------------------------
433 // wxExecute: the real worker function
434 // ----------------------------------------------------------------------------
437 #pragma message disable codeunreachable
440 long wxExecute(wxChar
**argv
,
444 // for the sync execution, we return -1 to indicate failure, but for async
445 // case we return 0 which is never a valid PID
447 // we define this as a macro, not a variable, to avoid compiler warnings
448 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
449 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
451 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
455 char *mb_argv
[WXEXECUTE_NARGS
];
457 while (argv
[mb_argc
])
459 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
460 mb_argv
[mb_argc
] = strdup(mb_arg
);
463 mb_argv
[mb_argc
] = (char *) NULL
;
465 // this macro will free memory we used above
466 #define ARGS_CLEANUP \
467 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
468 free(mb_argv[mb_argc])
470 // no need for cleanup
473 wxChar
**mb_argv
= argv
;
474 #endif // Unicode/ANSI
476 // we want this function to work even if there is no wxApp so ensure that
477 // we have a valid traits pointer
478 wxConsoleAppTraits traitsConsole
;
479 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
481 traits
= &traitsConsole
;
483 // this struct contains all information which we pass to and from
484 // wxAppTraits methods
485 wxExecuteData execData
;
486 execData
.flags
= flags
;
487 execData
.process
= process
;
490 if ( !traits
->CreateEndProcessPipe(execData
) )
492 wxLogError( _("Failed to execute '%s'\n"), *argv
);
496 return ERROR_RETURN_CODE
;
499 // pipes for inter process communication
500 wxPipe pipeIn
, // stdin
504 if ( process
&& process
->IsRedirected() )
506 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
508 wxLogError( _("Failed to execute '%s'\n"), *argv
);
512 return ERROR_RETURN_CODE
;
518 // NB: do *not* use vfork() here, it completely breaks this code for some
519 // reason under Solaris (and maybe others, although not under Linux)
520 // But on OpenVMS we do not have fork so we have to use vfork and
521 // cross our fingers that it works.
527 if ( pid
== -1 ) // error?
529 wxLogSysError( _("Fork failed") );
533 return ERROR_RETURN_CODE
;
535 else if ( pid
== 0 ) // we're in child
537 // These lines close the open file descriptors to to avoid any
538 // input/output which might block the process or irritate the user. If
539 // one wants proper IO for the subprocess, the right thing to do is to
540 // start an xterm executing it.
541 if ( !(flags
& wxEXEC_SYNC
) )
543 // FD_SETSIZE is unsigned under BSD, signed under other platforms
544 // so we need a cast to avoid warnings on all platforms
545 for ( int fd
= 0; fd
< (int)FD_SETSIZE
; fd
++ )
547 if ( fd
== pipeIn
[wxPipe::Read
]
548 || fd
== pipeOut
[wxPipe::Write
]
549 || fd
== pipeErr
[wxPipe::Write
]
550 || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) )
552 // don't close this one, we still need it
556 // leave stderr opened too, it won't do any harm
557 if ( fd
!= STDERR_FILENO
)
562 #if !defined(__VMS) && !defined(__EMX__)
563 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
565 // Set process group to child process' pid. Then killing -pid
566 // of the parent will kill the process and all of its children.
571 // reading side can be safely closed but we should keep the write one
573 traits
->DetachWriteFDOfEndProcessPipe(execData
);
575 // redirect stdin, stdout and stderr
578 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
579 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
580 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
582 wxLogSysError(_("Failed to redirect child process input/output"));
590 execvp (*mb_argv
, mb_argv
);
592 fprintf(stderr
, "execvp(");
593 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
594 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
595 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
596 fprintf(stderr
, ") failed with error %d!\n", errno
);
598 // there is no return after successful exec()
601 // some compilers complain about missing return - of course, they
602 // should know that exit() doesn't return but what else can we do if
605 // and, sure enough, other compilers complain about unreachable code
606 // after exit() call, so we can just always have return here...
607 #if defined(__VMS) || defined(__INTEL_COMPILER)
611 else // we're in parent
615 // save it for WaitForChild() use
618 // prepare for IO redirection
621 // the input buffer bufOut is connected to stdout, this is why it is
622 // called bufOut and not bufIn
623 wxStreamTempInputBuffer bufOut
,
625 #endif // wxUSE_STREAMS
627 if ( process
&& process
->IsRedirected() )
630 wxOutputStream
*inStream
=
631 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
633 wxPipeInputStream
*outStream
=
634 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
636 wxPipeInputStream
*errStream
=
637 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
639 process
->SetPipeStreams(outStream
, inStream
, errStream
);
641 bufOut
.Init(outStream
);
642 bufErr
.Init(errStream
);
644 execData
.bufOut
= &bufOut
;
645 execData
.bufErr
= &bufErr
;
646 #endif // wxUSE_STREAMS
656 return traits
->WaitForChild(execData
);
659 return ERROR_RETURN_CODE
;
663 #pragma message enable codeunreachable
666 #undef ERROR_RETURN_CODE
669 // ----------------------------------------------------------------------------
670 // file and directory functions
671 // ----------------------------------------------------------------------------
673 const wxChar
* wxGetHomeDir( wxString
*home
)
675 *home
= wxGetUserHome( wxEmptyString
);
681 if ( tmp
.Last() != wxT(']'))
682 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
684 return home
->c_str();
688 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
689 #else // just for binary compatibility -- there is no 'const' here
690 char *wxGetUserHome( const wxString
&user
)
693 struct passwd
*who
= (struct passwd
*) NULL
;
699 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
702 wxWCharBuffer
buffer( ptr
);
708 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
710 who
= getpwnam(wxConvertWX2MB(ptr
));
713 // We now make sure the the user exists!
716 who
= getpwuid(getuid());
721 who
= getpwnam (user
.mb_str());
724 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
727 // ----------------------------------------------------------------------------
728 // network and user id routines
729 // ----------------------------------------------------------------------------
731 // retrieve either the hostname or FQDN depending on platform (caller must
732 // check whether it's one or the other, this is why this function is for
734 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
736 wxCHECK_MSG( buf
, false, wxT("NULL pointer in wxGetHostNameInternal") );
740 // we're using uname() which is POSIX instead of less standard sysinfo()
741 #if defined(HAVE_UNAME)
743 bool ok
= uname(&uts
) != -1;
746 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
749 #elif defined(HAVE_GETHOSTNAME)
750 bool ok
= gethostname(buf
, sz
) != -1;
751 #else // no uname, no gethostname
752 wxFAIL_MSG(wxT("don't know host name for this machine"));
755 #endif // uname/gethostname
759 wxLogSysError(_("Cannot get the hostname"));
765 bool wxGetHostName(wxChar
*buf
, int sz
)
767 bool ok
= wxGetHostNameInternal(buf
, sz
);
771 // BSD systems return the FQDN, we only want the hostname, so extract
772 // it (we consider that dots are domain separators)
773 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
784 bool wxGetFullHostName(wxChar
*buf
, int sz
)
786 bool ok
= wxGetHostNameInternal(buf
, sz
);
790 if ( !wxStrchr(buf
, wxT('.')) )
792 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
795 wxLogSysError(_("Cannot get the official hostname"));
801 // the canonical name
802 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
805 //else: it's already a FQDN (BSD behaves this way)
811 bool wxGetUserId(wxChar
*buf
, int sz
)
816 if ((who
= getpwuid(getuid ())) != NULL
)
818 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
825 bool wxGetUserName(wxChar
*buf
, int sz
)
830 if ((who
= getpwuid (getuid ())) != NULL
)
832 // pw_gecos field in struct passwd is not standard
834 char *comma
= strchr(who
->pw_gecos
, ',');
836 *comma
= '\0'; // cut off non-name comment fields
837 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
838 #else // !HAVE_PW_GECOS
839 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
840 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
847 // this function is in mac/utils.cpp for wxMac
850 wxString
wxGetOsDescription()
852 FILE *f
= popen("uname -s -r -m", "r");
856 size_t c
= fread(buf
, 1, sizeof(buf
) - 1, f
);
858 // Trim newline from output.
859 if (c
&& buf
[c
- 1] == '\n')
862 return wxString::FromAscii( buf
);
864 wxFAIL_MSG( _T("uname failed") );
870 unsigned long wxGetProcessId()
872 return (unsigned long)getpid();
875 wxMemorySize
wxGetFreeMemory()
877 #if defined(__LINUX__)
878 // get it from /proc/meminfo
879 FILE *fp
= fopen("/proc/meminfo", "r");
885 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
887 long memTotal
, memUsed
;
888 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
893 return (wxMemorySize
)memFree
;
895 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
896 return (wxMemorySize
)(sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
));
897 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
904 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
906 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
907 // the case to "char *" is needed for AIX 4.3
909 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
911 wxLogSysError( wxT("Failed to get file system statistics") );
916 // under Solaris we also have to use f_frsize field instead of f_bsize
917 // which is in general a multiple of f_frsize
919 wxLongLong blockSize
= fs
.f_frsize
;
921 wxLongLong blockSize
= fs
.f_bsize
;
922 #endif // HAVE_STATVFS/HAVE_STATFS
926 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
931 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
935 #else // !HAVE_STATFS && !HAVE_STATVFS
937 #endif // HAVE_STATFS
940 // ----------------------------------------------------------------------------
942 // ----------------------------------------------------------------------------
944 bool wxGetEnv(const wxString
& var
, wxString
*value
)
946 // wxGetenv is defined as getenv()
947 wxChar
*p
= wxGetenv(var
);
959 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
961 #if defined(HAVE_SETENV)
962 return setenv(variable
.mb_str(),
963 value
? (const char *)wxString(value
).mb_str()
965 1 /* overwrite */) == 0;
966 #elif defined(HAVE_PUTENV)
967 wxString s
= variable
;
969 s
<< _T('=') << value
;
972 const wxWX2MBbuf p
= s
.mb_str();
974 // the string will be free()d by libc
975 char *buf
= (char *)malloc(strlen(p
) + 1);
978 return putenv(buf
) == 0;
979 #else // no way to set an env var
984 // ----------------------------------------------------------------------------
986 // ----------------------------------------------------------------------------
988 #if wxUSE_ON_FATAL_EXCEPTION
992 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
996 // give the user a chance to do something special about this
997 wxTheApp
->OnFatalException();
1003 bool wxHandleFatalExceptions(bool doit
)
1006 static bool s_savedHandlers
= false;
1007 static struct sigaction s_handlerFPE
,
1013 if ( doit
&& !s_savedHandlers
)
1015 // install the signal handler
1016 struct sigaction act
;
1018 // some systems extend it with non std fields, so zero everything
1019 memset(&act
, 0, sizeof(act
));
1021 act
.sa_handler
= wxFatalSignalHandler
;
1022 sigemptyset(&act
.sa_mask
);
1025 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1026 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1027 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1028 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1031 wxLogDebug(_T("Failed to install our signal handler."));
1034 s_savedHandlers
= true;
1036 else if ( s_savedHandlers
)
1038 // uninstall the signal handler
1039 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1040 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1041 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1042 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1045 wxLogDebug(_T("Failed to uninstall our signal handler."));
1048 s_savedHandlers
= false;
1050 //else: nothing to do
1055 #endif // wxUSE_ON_FATAL_EXCEPTION
1057 // ----------------------------------------------------------------------------
1058 // error and debug output routines (deprecated, use wxLog)
1059 // ----------------------------------------------------------------------------
1061 #if WXWIN_COMPATIBILITY_2_2
1063 void wxDebugMsg( const char *format
, ... )
1066 va_start( ap
, format
);
1067 vfprintf( stderr
, format
, ap
);
1072 void wxError( const wxString
&msg
, const wxString
&title
)
1074 wxFprintf( stderr
, _("Error ") );
1075 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1076 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1077 wxFprintf( stderr
, wxT(".\n") );
1080 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1082 wxFprintf( stderr
, _("Error ") );
1083 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1084 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1085 wxFprintf( stderr
, wxT(".\n") );
1086 exit(3); // the same exit code as for abort()
1089 #endif // WXWIN_COMPATIBILITY_2_2
1091 #endif // wxUSE_BASE
1095 // ----------------------------------------------------------------------------
1096 // wxExecute support
1097 // ----------------------------------------------------------------------------
1099 // Darwin doesn't use the same process end detection mechanisms so we don't
1100 // need wxExecute-related helpers for it
1101 #if !(defined(__DARWIN__) && defined(__WXMAC__))
1103 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
)
1105 return execData
.pipeEndProcDetect
.Create();
1108 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
)
1110 return fd
== (execData
.pipeEndProcDetect
)[wxPipe::Write
];
1113 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
)
1115 execData
.pipeEndProcDetect
.Detach(wxPipe::Write
);
1116 execData
.pipeEndProcDetect
.Close();
1121 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1127 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
),
1134 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
))
1136 // nothing to do here, we don't use the pipe
1139 #endif // !Darwin/Darwin
1141 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
)
1143 wxEndProcessData
*endProcData
= new wxEndProcessData
;
1145 const int flags
= execData
.flags
;
1147 // wxAddProcessCallback is now (with DARWIN) allowed to call the
1148 // callback function directly if the process terminates before
1149 // the callback can be added to the run loop. Set up the endProcData.
1150 if ( flags
& wxEXEC_SYNC
)
1152 // we may have process for capturing the program output, but it's
1153 // not used in wxEndProcessData in the case of sync execution
1154 endProcData
->process
= NULL
;
1156 // sync execution: indicate it by negating the pid
1157 endProcData
->pid
= -execData
.pid
;
1161 // async execution, nothing special to do -- caller will be
1162 // notified about the process termination if process != NULL, endProcData
1163 // will be deleted in GTK_EndProcessDetector
1164 endProcData
->process
= execData
.process
;
1165 endProcData
->pid
= execData
.pid
;
1169 #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))
1170 endProcData
->tag
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
);
1172 endProcData
->tag
= wxAddProcessCallback
1175 execData
.pipeEndProcDetect
.Detach(wxPipe::Read
)
1178 execData
.pipeEndProcDetect
.Close();
1179 #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__))
1181 if ( flags
& wxEXEC_SYNC
)
1184 wxWindowDisabler
*wd
= flags
& wxEXEC_NODISABLE
? NULL
1185 : new wxWindowDisabler
;
1187 // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the
1188 // process terminates
1189 while ( endProcData
->pid
!= 0 )
1194 if ( execData
.bufOut
)
1196 execData
.bufOut
->Update();
1200 if ( execData
.bufErr
)
1202 execData
.bufErr
->Update();
1205 #endif // wxUSE_STREAMS
1207 // don't consume 100% of the CPU while we're sitting in this
1212 // give GTK+ a chance to call GTK_EndProcessDetector here and
1213 // also repaint the GUI
1217 int exitcode
= endProcData
->exitcode
;
1224 else // async execution
1226 return execData
.pid
;
1233 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
1235 // notify user about termination if required
1236 if ( proc_data
->process
)
1238 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
1242 if ( proc_data
->pid
> 0 )
1248 // let wxExecute() know that the process has terminated
1253 #endif // wxUSE_BASE