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"
32 #include "wx/unix/execute.h"
35 // SGI signal.h defines signal handler arguments differently depending on
36 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
37 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
38 #define _LANGUAGE_C_PLUS_PLUS 1
45 #include <sys/types.h>
52 #include <fcntl.h> // for O_WRONLY and friends
53 #include <time.h> // nanosleep() and/or usleep()
54 #include <ctype.h> // isspace()
55 #include <sys/time.h> // needed for FD_SETSIZE
58 #include <sys/utsname.h> // for uname()
61 // ----------------------------------------------------------------------------
62 // conditional compilation
63 // ----------------------------------------------------------------------------
65 // many versions of Unices have this function, but it is not defined in system
66 // headers - please add your system here if it is the case for your OS.
67 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
68 #if !defined(HAVE_USLEEP) && \
69 (defined(__SUN__) && !defined(__SunOs_5_6) && \
70 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
71 defined(__osf__) || defined(__EMX__)
75 int usleep(unsigned int usec
);
78 /* I copied this from the XFree86 diffs. AV. */
79 #define INCL_DOSPROCESS
81 inline void usleep(unsigned long delay
)
83 DosSleep(delay
? (delay
/1000l) : 1l);
86 void usleep(unsigned long usec
);
88 #endif // Sun/EMX/Something else
92 #endif // Unices without usleep()
94 // ============================================================================
96 // ============================================================================
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 void wxSleep(int nSecs
)
107 void wxUsleep(unsigned long milliseconds
)
109 #if defined(HAVE_NANOSLEEP)
111 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
112 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
114 // we're not interested in remaining time nor in return value
115 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
116 #elif defined(HAVE_USLEEP)
117 // uncomment this if you feel brave or if you are sure that your version
118 // of Solaris has a safe usleep() function but please notice that usleep()
119 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
120 // documented as MT-Safe
121 #if defined(__SUN__) && wxUSE_THREADS
122 #error "usleep() cannot be used in MT programs under Solaris."
125 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
126 #elif defined(HAVE_SLEEP)
127 // under BeOS sleep() takes seconds (what about other platforms, if any?)
128 sleep(milliseconds
* 1000);
129 #else // !sleep function
130 #error "usleep() or nanosleep() function required for wxUsleep"
131 #endif // sleep function
134 // ----------------------------------------------------------------------------
135 // process management
136 // ----------------------------------------------------------------------------
138 int wxKill(long pid
, wxSignal sig
)
140 return kill((pid_t
)pid
, (int)sig
);
143 #define WXEXECUTE_NARGS 127
145 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
147 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
150 wxChar
*argv
[WXEXECUTE_NARGS
];
152 const wxChar
*cptr
= command
.c_str();
153 wxChar quotechar
= wxT('\0'); // is arg quoted?
154 bool escaped
= FALSE
;
156 // split the command line in arguments
160 quotechar
= wxT('\0');
162 // eat leading whitespace:
163 while ( wxIsspace(*cptr
) )
166 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
171 if ( *cptr
== wxT('\\') && ! escaped
)
178 // all other characters:
182 // have we reached the end of the argument?
183 if ( (*cptr
== quotechar
&& ! escaped
)
184 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
185 || *cptr
== wxT('\0') )
187 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
188 wxT("too many arguments in wxExecute") );
190 argv
[argc
] = new wxChar
[argument
.length() + 1];
191 wxStrcpy(argv
[argc
], argument
.c_str());
194 // if not at end of buffer, swallow last character:
198 break; // done with this one, start over
204 // do execute the command
205 long lRc
= wxExecute(argv
, sync
, process
);
210 delete [] argv
[argc
++];
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 static wxString
wxMakeShellCommand(const wxString
& command
)
224 // just an interactive shell
229 // execute command in a shell
230 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
236 bool wxShell(const wxString
& command
)
238 return wxExecute(wxMakeShellCommand(command
), TRUE
/* sync */) == 0;
241 bool wxShell(const wxString
& command
, wxArrayString
& output
)
243 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
245 return wxExecute(wxMakeShellCommand(command
), output
);
250 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
252 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
254 // waitpid is POSIX so should be available everywhere, however on older
255 // systems wait() might be used instead in a loop (until the right pid
260 // wait for child termination and if waitpid() was interrupted, try again
263 rc
= waitpid(pid
, &status
, 0);
265 while ( rc
== -1 && errno
== EINTR
);
268 if( rc
== -1 || ! (WIFEXITED(status
) || WIFSIGNALED(status
)) )
270 wxLogSysError(_("Waiting for subprocess termination failed"));
271 /* AFAIK, this can only happen if something went wrong within
272 wxGTK, i.e. due to a race condition or some serious bug.
273 After having fixed the order of statements in
274 GTK_EndProcessDetector(). (KB)
279 // notify user about termination if required
280 if (proc_data
->process
)
282 proc_data
->process
->OnTerminate(proc_data
->pid
,
283 WEXITSTATUS(status
));
286 if ( proc_data
->pid
> 0 )
292 // wxExecute() will know about it
293 proc_data
->exitcode
= status
;
302 // ----------------------------------------------------------------------------
303 // wxStream classes to support IO redirection in wxExecute
304 // ----------------------------------------------------------------------------
306 class wxProcessFileInputStream
: public wxInputStream
309 wxProcessFileInputStream(int fd
) { m_fd
= fd
; }
310 ~wxProcessFileInputStream() { close(m_fd
); }
312 virtual bool Eof() const;
315 size_t OnSysRead(void *buffer
, size_t bufsize
);
321 class wxProcessFileOutputStream
: public wxOutputStream
324 wxProcessFileOutputStream(int fd
) { m_fd
= fd
; }
325 ~wxProcessFileOutputStream() { close(m_fd
); }
328 size_t OnSysWrite(const void *buffer
, size_t bufsize
);
334 bool wxProcessFileInputStream::Eof() const
336 if ( m_lasterror
== wxSTREAM_EOF
)
339 // check if there is any input available
346 FD_SET(m_fd
, &readfds
);
347 switch ( select(m_fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
350 wxLogSysError(_("Impossible to get child process input"));
357 wxFAIL_MSG(_T("unexpected select() return value"));
358 // still fall through
361 // input available: check if there is any
362 return wxInputStream::Eof();
366 size_t wxProcessFileInputStream::OnSysRead(void *buffer
, size_t bufsize
)
368 int ret
= read(m_fd
, buffer
, bufsize
);
371 m_lasterror
= wxSTREAM_EOF
;
373 else if ( ret
== -1 )
375 m_lasterror
= wxSTREAM_READ_ERROR
;
380 m_lasterror
= wxSTREAM_NOERROR
;
386 size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer
, size_t bufsize
)
388 int ret
= write(m_fd
, buffer
, bufsize
);
391 m_lasterror
= wxSTREAM_WRITE_ERROR
;
396 m_lasterror
= wxSTREAM_NOERROR
;
402 long wxExecute(wxChar
**argv
,
406 // for the sync execution, we return -1 to indicate failure, but for async
407 // case we return 0 which is never a valid PID
409 // we define this as a macro, not a variable, to avoid compiler warnings
410 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
411 #define ERROR_RETURN_CODE ((sync) ? -1 : 0)
413 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
417 char *mb_argv
[WXEXECUTE_NARGS
];
419 while (argv
[mb_argc
])
421 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
422 mb_argv
[mb_argc
] = strdup(mb_arg
);
425 mb_argv
[mb_argc
] = (char *) NULL
;
427 // this macro will free memory we used above
428 #define ARGS_CLEANUP \
429 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
430 free(mb_argv[mb_argc])
432 // no need for cleanup
435 wxChar
**mb_argv
= argv
;
436 #endif // Unicode/ANSI
440 int end_proc_detect
[2];
441 if ( pipe(end_proc_detect
) == -1 )
443 wxLogSysError( _("Pipe creation failed") );
444 wxLogError( _("Failed to execute '%s'\n"), *argv
);
448 return ERROR_RETURN_CODE
;
452 // pipes for inter process communication
453 int pipeIn
[2], // stdin
454 pipeOut
[2], // stdout
455 pipeErr
[2]; // stderr
457 pipeIn
[0] = pipeIn
[1] =
458 pipeOut
[0] = pipeOut
[1] =
459 pipeErr
[0] = pipeErr
[1] = -1;
461 if ( process
&& process
->IsRedirected() )
463 if ( pipe(pipeIn
) == -1 || pipe(pipeOut
) == -1 || pipe(pipeErr
) == -1 )
466 // free previously allocated resources
467 close(end_proc_detect
[0]);
468 close(end_proc_detect
[1]);
471 wxLogSysError( _("Pipe creation failed") );
472 wxLogError( _("Failed to execute '%s'\n"), *argv
);
476 return ERROR_RETURN_CODE
;
487 if ( pid
== -1 ) // error?
490 close(end_proc_detect
[0]);
491 close(end_proc_detect
[1]);
500 wxLogSysError( _("Fork failed") );
504 return ERROR_RETURN_CODE
;
506 else if ( pid
== 0 ) // we're in child
509 close(end_proc_detect
[0]); // close reading side
512 // These lines close the open file descriptors to to avoid any
513 // input/output which might block the process or irritate the user. If
514 // one wants proper IO for the subprocess, the right thing to do is to
515 // start an xterm executing it.
518 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
520 if ( fd
== pipeIn
[0] || fd
== pipeOut
[1] || fd
== pipeErr
[1]
522 || fd
== end_proc_detect
[1]
526 // don't close this one, we still need it
530 // leave stderr opened too, it won't do any hurm
531 if ( fd
!= STDERR_FILENO
)
536 // redirect stdio, stdout and stderr
537 if ( pipeIn
[0] != -1 )
539 if ( dup2(pipeIn
[0], STDIN_FILENO
) == -1 ||
540 dup2(pipeOut
[1], STDOUT_FILENO
) == -1 ||
541 dup2(pipeErr
[1], STDERR_FILENO
) == -1 )
543 wxLogSysError(_("Failed to redirect child process input/output"));
551 execvp (*mb_argv
, mb_argv
);
553 // there is no return after successful exec()
556 else // we're in parent
560 // pipe initialization: construction of the wxStreams
561 if ( process
&& process
->IsRedirected() )
563 // These two streams are relative to this process.
564 wxOutputStream
*outStream
= new wxProcessFileOutputStream(pipeIn
[1]);
565 wxInputStream
*inStream
= new wxProcessFileInputStream(pipeOut
[0]);
566 wxInputStream
*errStream
= new wxProcessFileInputStream(pipeErr
[0]);
568 close(pipeIn
[0]); // close reading side
569 close(pipeOut
[1]); // close writing side
570 close(pipeErr
[1]); // close writing side
572 process
->SetPipeStreams(inStream
, outStream
, errStream
);
576 wxEndProcessData
*data
= new wxEndProcessData
;
580 // we may have process for capturing the program output, but it's
581 // not used in wxEndProcessData in the case of sync execution
582 data
->process
= NULL
;
584 // sync execution: indicate it by negating the pid
586 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
588 close(end_proc_detect
[1]); // close writing side
593 // it will be set to 0 from GTK_EndProcessDetector
594 while (data
->pid
!= 0)
597 int exitcode
= data
->exitcode
;
603 else // async execution
605 // async execution, nothing special to do - caller will be
606 // notified about the process termination if process != NULL, data
607 // will be deleted in GTK_EndProcessDetector
608 data
->process
= process
;
610 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
612 close(end_proc_detect
[1]); // close writing side
617 wxASSERT_MSG( sync
, wxT("async execution not supported yet") );
620 if ( waitpid(pid
, &exitcode
, 0) == -1 || !WIFEXITED(exitcode
) )
622 wxLogSysError(_("Waiting for subprocess termination failed"));
629 // VMS does not recognise exit as a return and complains about
631 // I think VMS is wrong in this
637 #undef ERROR_RETURN_CODE
640 // ----------------------------------------------------------------------------
641 // file and directory functions
642 // ----------------------------------------------------------------------------
644 const wxChar
* wxGetHomeDir( wxString
*home
)
646 *home
= wxGetUserHome( wxString() );
648 if ( home
->IsEmpty() )
652 if ( tmp
.Last() != wxT(']'))
653 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
655 return home
->c_str();
659 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
660 #else // just for binary compatibility -- there is no 'const' here
661 char *wxGetUserHome( const wxString
&user
)
664 struct passwd
*who
= (struct passwd
*) NULL
;
670 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
674 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
676 who
= getpwnam(wxConvertWX2MB(ptr
));
679 // We now make sure the the user exists!
682 who
= getpwuid(getuid());
687 who
= getpwnam (user
.mb_str());
690 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
693 // ----------------------------------------------------------------------------
694 // network and user id routines
695 // ----------------------------------------------------------------------------
697 // retrieve either the hostname or FQDN depending on platform (caller must
698 // check whether it's one or the other, this is why this function is for
700 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
702 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
706 // we're using uname() which is POSIX instead of less standard sysinfo()
707 #if defined(HAVE_UNAME)
709 bool ok
= uname(&uts
) != -1;
712 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
715 #elif defined(HAVE_GETHOSTNAME)
716 bool ok
= gethostname(buf
, sz
) != -1;
717 #else // no uname, no gethostname
718 wxFAIL_MSG(wxT("don't know host name for this machine"));
721 #endif // uname/gethostname
725 wxLogSysError(_("Cannot get the hostname"));
731 bool wxGetHostName(wxChar
*buf
, int sz
)
733 bool ok
= wxGetHostNameInternal(buf
, sz
);
737 // BSD systems return the FQDN, we only want the hostname, so extract
738 // it (we consider that dots are domain separators)
739 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
750 bool wxGetFullHostName(wxChar
*buf
, int sz
)
752 bool ok
= wxGetHostNameInternal(buf
, sz
);
756 if ( !wxStrchr(buf
, wxT('.')) )
758 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
761 wxLogSysError(_("Cannot get the official hostname"));
767 // the canonical name
768 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
771 //else: it's already a FQDN (BSD behaves this way)
777 bool wxGetUserId(wxChar
*buf
, int sz
)
782 if ((who
= getpwuid(getuid ())) != NULL
)
784 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
791 bool wxGetUserName(wxChar
*buf
, int sz
)
796 if ((who
= getpwuid (getuid ())) != NULL
)
798 // pw_gecos field in struct passwd is not standard
800 char *comma
= strchr(who
->pw_gecos
, ',');
802 *comma
= '\0'; // cut off non-name comment fields
803 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
804 #else // !HAVE_PW_GECOS
805 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
806 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
813 wxString
wxGetOsDescription()
815 #ifndef WXWIN_OS_DESCRIPTION
816 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
818 return WXWIN_OS_DESCRIPTION
;
822 // this function returns the GUI toolkit version in GUI programs, but OS
823 // version in non-GUI ones
826 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
831 if ( sscanf(WXWIN_OS_DESCRIPTION
, "%s %d.%d", name
, &major
, &minor
) != 3 )
833 // unreckognized uname string format
847 long wxGetFreeMemory()
849 #if defined(__LINUX__)
850 // get it from /proc/meminfo
851 FILE *fp
= fopen("/proc/meminfo", "r");
857 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
859 long memTotal
, memUsed
;
860 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
867 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
868 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
869 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
876 // ----------------------------------------------------------------------------
878 // ----------------------------------------------------------------------------
880 bool wxGetEnv(const wxString
& var
, wxString
*value
)
882 // wxGetenv is defined as getenv()
883 wxChar
*p
= wxGetenv(var
);
895 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
897 #if defined(HAVE_SETENV)
898 return setenv(variable
.mb_str(), value
? wxString(value
).mb_str().data()
899 : NULL
, 1 /* overwrite */) == 0;
900 #elif defined(HAVE_PUTENV)
901 wxString s
= variable
;
903 s
<< _T('=') << value
;
906 const char *p
= s
.mb_str();
908 // the string will be free()d by libc
909 char *buf
= (char *)malloc(strlen(p
) + 1);
912 return putenv(buf
) == 0;
913 #else // no way to set an env var
918 // ----------------------------------------------------------------------------
920 // ----------------------------------------------------------------------------
922 #if wxUSE_ON_FATAL_EXCEPTION
926 static void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
930 // give the user a chance to do something special about this
931 wxTheApp
->OnFatalException();
937 bool wxHandleFatalExceptions(bool doit
)
940 static bool s_savedHandlers
= FALSE
;
941 static struct sigaction s_handlerFPE
,
947 if ( doit
&& !s_savedHandlers
)
949 // install the signal handler
950 struct sigaction act
;
952 // some systems extend it with non std fields, so zero everything
953 memset(&act
, 0, sizeof(act
));
955 act
.sa_handler
= wxFatalSignalHandler
;
956 sigemptyset(&act
.sa_mask
);
959 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
960 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
961 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
962 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
965 wxLogDebug(_T("Failed to install our signal handler."));
968 s_savedHandlers
= TRUE
;
970 else if ( s_savedHandlers
)
972 // uninstall the signal handler
973 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
974 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
975 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
976 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
979 wxLogDebug(_T("Failed to uninstall our signal handler."));
982 s_savedHandlers
= FALSE
;
984 //else: nothing to do
989 #endif // wxUSE_ON_FATAL_EXCEPTION
991 // ----------------------------------------------------------------------------
992 // error and debug output routines (deprecated, use wxLog)
993 // ----------------------------------------------------------------------------
995 void wxDebugMsg( const char *format
, ... )
998 va_start( ap
, format
);
999 vfprintf( stderr
, format
, ap
);
1004 void wxError( const wxString
&msg
, const wxString
&title
)
1006 wxFprintf( stderr
, _("Error ") );
1007 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1008 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1009 wxFprintf( stderr
, wxT(".\n") );
1012 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1014 wxFprintf( stderr
, _("Error ") );
1015 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1016 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1017 wxFprintf( stderr
, wxT(".\n") );
1018 exit(3); // the same exit code as for abort()