X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7beba2fc73283f5b750227459da57e66bcd475f5..69e9bc09b8925c84435077dc31edd0140b668649:/src/unix/utilsunx.cpp diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index c4a312efbd..fe4abae50e 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -23,8 +23,13 @@ #include "wx/utils.h" #include "wx/process.h" +#include "wx/thread.h" -#include "wx/unix/execute.h" +#include "wx/stream.h" + +#if wxUSE_GUI + #include "wx/unix/execute.h" +#endif #include #include @@ -40,11 +45,9 @@ #include // for O_WRONLY and friends #include // nanosleep() and/or usleep() #include // isspace() +#include // needed for FD_SETSIZE -// JACS: needed for FD_SETSIZE -#include - -#if HAVE_UNAME +#ifdef HAVE_UNAME #include // for uname() #endif // HAVE_UNAME @@ -64,20 +67,21 @@ #ifdef __SUN__ int usleep(unsigned int usec); #else // !Sun - #ifdef __EMX - /* I copied this from the XFree86 diffs. AV. */ - #define INCL_DOSPROCESS - #include - void usleep(unsigned long delay) - { - DosSleep(delay ? (delay/1000l) : 1l); - } - #else - void usleep(unsigned long usec); - #endif + #ifdef __EMX__ + /* I copied this from the XFree86 diffs. AV. */ + #define INCL_DOSPROCESS + #include + inline void usleep(unsigned long delay) + { + DosSleep(delay ? (delay/1000l) : 1l); + } + #else // !Sun && !EMX + void usleep(unsigned long usec); + #endif #endif // Sun/EMX/Something else }; -#define HAVE_USLEEP 1 + + #define HAVE_USLEEP 1 #endif // Unices without usleep() // ============================================================================ @@ -95,14 +99,14 @@ void wxSleep(int nSecs) void wxUsleep(unsigned long milliseconds) { -#if HAVE_NANOSLEEP +#if defined(HAVE_NANOSLEEP) timespec tmReq; - tmReq.tv_sec = milliseconds / 1000; + tmReq.tv_sec = (time_t)(milliseconds / 1000); tmReq.tv_nsec = (milliseconds % 1000) * 1000 * 1000; // we're not interested in remaining time nor in return value (void)nanosleep(&tmReq, (timespec *)NULL); -#elif HAVE_USLEEP +#elif defined(HAVE_USLEEP) // uncomment this if you feel brave or if you are sure that your version // of Solaris has a safe usleep() function but please notice that usleep() // is known to lead to crashes in MT programs in Solaris 2.[67] and is not @@ -112,6 +116,9 @@ void wxUsleep(unsigned long milliseconds) #endif // Sun usleep(milliseconds * 1000); // usleep(3) wants microseconds +#elif defined(HAVE_SLEEP) + // under BeOS sleep() takes seconds (what about other platforms, if any?) + sleep(milliseconds * 1000); #else // !sleep function #error "usleep() or nanosleep() function required for wxUsleep" #endif // sleep function @@ -123,7 +130,7 @@ void wxUsleep(unsigned long milliseconds) int wxKill(long pid, wxSignal sig) { - return kill(pid, (int)sig); + return kill((pid_t)pid, (int)sig); } #define WXEXECUTE_NARGS 127 @@ -209,6 +216,8 @@ bool wxShell(const wxString& command) return wxExecute(cmd) != 0; } +#if wxUSE_GUI + void wxHandleProcessTermination(wxEndProcessData *proc_data) { int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid); @@ -217,9 +226,24 @@ void wxHandleProcessTermination(wxEndProcessData *proc_data) // systems wait() might be used instead in a loop (until the right pid // terminates) int status = 0; - if ( waitpid(pid, &status, 0) == -1 || !WIFEXITED(status) ) + int rc; + + // wait for child termination and if waitpid() was interrupted, try again + do { - wxLogSysError(_("Waiting for subprocess termination failed")); + rc = waitpid(pid, &status, 0); + } + while ( rc == -1 && errno == EINTR ); + + + if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) ) + { + wxLogSysError(_("Waiting for subprocess termination failed")); + /* AFAIK, this can only happen if something went wrong within + wxGTK, i.e. due to a race condition or some serious bug. + After having fixed the order of statements in + GTK_EndProcessDetector(). (KB) + */ } else { @@ -229,23 +253,109 @@ void wxHandleProcessTermination(wxEndProcessData *proc_data) proc_data->process->OnTerminate(proc_data->pid, WEXITSTATUS(status)); } + // clean up + if ( proc_data->pid > 0 ) + { + delete proc_data; + } + else + { + // wxExecute() will know about it + proc_data->exitcode = status; + + proc_data->pid = 0; + } } +} - // clean up - if ( proc_data->pid > 0 ) - { - delete proc_data; +#endif // wxUSE_GUI + +#if wxUSE_GUI + #define WXUNUSED_UNLESS_GUI(p) p +#else + #define WXUNUSED_UNLESS_GUI(p) +#endif + +// New wxStream classes to clean up the data when the process terminates + +#if wxUSE_GUI +class wxProcessFileInputStream: public wxInputStream { + public: + wxProcessFileInputStream(int fd); + ~wxProcessFileInputStream(); + + protected: + size_t OnSysRead(void *buffer, size_t bufsize); + + protected: + int m_fd; +}; + +class wxProcessFileOutputStream: public wxOutputStream { + public: + wxProcessFileOutputStream(int fd); + ~wxProcessFileOutputStream(); + + protected: + size_t OnSysWrite(const void *buffer, size_t bufsize); + + protected: + int m_fd; +}; + +wxProcessFileInputStream::wxProcessFileInputStream(int fd) +{ + m_fd = fd; +} + +wxProcessFileInputStream::~wxProcessFileInputStream() +{ + close(m_fd); +} + +size_t wxProcessFileInputStream::OnSysRead(void *buffer, size_t bufsize) +{ + int ret; + + ret = read(m_fd, buffer, bufsize); + m_lasterror = wxSTREAM_NOERROR; + if (ret == 0) + m_lasterror = wxSTREAM_EOF; + if (ret == -1) { + m_lasterror = wxSTREAM_READ_ERROR; + ret = 0; } - else - { - // wxExecute() will know about it - proc_data->exitcode = status; + return ret; +} + +wxProcessFileOutputStream::wxProcessFileOutputStream(int fd) +{ + m_fd = fd; +} - proc_data->pid = 0; +wxProcessFileOutputStream::~wxProcessFileOutputStream() +{ + close(m_fd); +} + +size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer, size_t bufsize) +{ + int ret; + + ret = write(m_fd, buffer, bufsize); + m_lasterror = wxSTREAM_NOERROR; + if (ret == -1) { + m_lasterror = wxSTREAM_WRITE_ERROR; + ret = 0; } + return ret; } -long wxExecute( wxChar **argv, bool sync, wxProcess *process ) +#endif + +long wxExecute(wxChar **argv, + bool sync, + wxProcess * WXUNUSED_UNLESS_GUI(process)) { wxCHECK_MSG( *argv, 0, wxT("can't exec empty command") ); @@ -285,14 +395,42 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) } #endif // wxUSE_GUI +#if wxUSE_GUI + int in_pipe[2] = { -1, -1 }; + int out_pipe[2] = { -1, -1 }; + // Only asynchronous mode is interresting + if (!sync && process && process->NeedPipe()) + { + if (pipe(in_pipe) == -1 || pipe(out_pipe) == -1) + { + /* Free fds */ + close(end_proc_detect[0]); + close(end_proc_detect[1]); + wxLogSysError( _("Pipe creation failed (Console pipes)") ); + + ARGS_CLEANUP; + + return 0; + } + } +#endif // wxUSE_GUI + // fork the process -#if HAVE_VFORK +#ifdef HAVE_VFORK pid_t pid = vfork(); #else pid_t pid = fork(); #endif if (pid == -1) { +#if wxUSE_GUI + close(end_proc_detect[0]); + close(end_proc_detect[1]); + close(in_pipe[0]); + close(in_pipe[1]); + close(out_pipe[0]); + close(out_pipe[1]); +#endif wxLogSysError( _("Fork failed") ); ARGS_CLEANUP; @@ -316,7 +454,7 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) for ( int fd = 0; fd < FD_SETSIZE; fd++ ) { #if wxUSE_GUI - if ( fd == end_proc_detect[1] ) + if ( fd == end_proc_detect[1] || fd == in_pipe[0] || fd == out_pipe[1] ) continue; #endif // wxUSE_GUI @@ -325,6 +463,16 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) } } + // Fake a console by duplicating pipes +#if wxUSE_GUI + if (in_pipe[0] != -1) { + dup2(in_pipe[0], STDIN_FILENO); + dup2(out_pipe[1], STDOUT_FILENO); + close(in_pipe[0]); + close(out_pipe[1]); + } +#endif // wxUSE_GUI + #if 0 close(STDERR_FILENO); @@ -345,11 +493,7 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) else { #if wxUSE_GUI - // we're in parent - close(end_proc_detect[1]); // close writing side - wxEndProcessData *data = new wxEndProcessData; - data->tag = wxAddProcessCallback(data, end_proc_detect[0]); ARGS_CLEANUP; @@ -359,7 +503,10 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) data->process = NULL; // sync execution: indicate it by negating the pid - data->pid = -pid; + data->pid = -pid; + data->tag = wxAddProcessCallback(data, end_proc_detect[0]); + // we're in parent + close(end_proc_detect[1]); // close writing side // it will be set to 0 from GTK_EndProcessDetector while (data->pid != 0) @@ -373,11 +520,28 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) } else { + // pipe initialization: construction of the wxStreams + if (process && process->NeedPipe()) { + // These two streams are relative to this process. + wxOutputStream *my_output_stream; + wxInputStream *my_input_stream; + + my_output_stream = new wxProcessFileOutputStream(in_pipe[1]); + my_input_stream = new wxProcessFileInputStream(out_pipe[0]); + close(in_pipe[0]); // close reading side + close(out_pipe[1]); // close writing side + + process->SetPipeStreams(my_input_stream, my_output_stream); + } + // async execution, nothing special to do - caller will be - // notified about the process terminationif process != NULL, data + // notified about the process termination if process != NULL, data // will be deleted in GTK_EndProcessDetector - data->process = process; - data->pid = pid; + data->process = process; + data->pid = pid; + data->tag = wxAddProcessCallback(data, end_proc_detect[0]); + // we're in parent + close(end_proc_detect[1]); // close writing side return pid; } @@ -393,6 +557,7 @@ long wxExecute( wxChar **argv, bool sync, wxProcess *process ) return exitcode; #endif // wxUSE_GUI } + return 0; #undef ARGS_CLEANUP } @@ -546,20 +711,34 @@ bool wxGetUserId(wxChar *buf, int sz) bool wxGetUserName(wxChar *buf, int sz) { struct passwd *who; - char *comma; *buf = wxT('\0'); - if ((who = getpwuid (getuid ())) != NULL) { - comma = strchr(who->pw_gecos, ','); + if ((who = getpwuid (getuid ())) != NULL) + { + // pw_gecos field in struct passwd is not standard +#if HAVE_PW_GECOS + char *comma = strchr(who->pw_gecos, ','); if (comma) *comma = '\0'; // cut off non-name comment fields wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); +#else // !HAVE_PW_GECOS + wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); +#endif // HAVE_PW_GECOS/!HAVE_PW_GECOS return TRUE; } return FALSE; } +wxString wxGetOsDescription() +{ +#ifndef WXWIN_OS_DESCRIPTION + #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure +#else + return WXWIN_OS_DESCRIPTION; +#endif +} + // ---------------------------------------------------------------------------- // error and debug output routines (deprecated, use wxLog) // ----------------------------------------------------------------------------