]> git.saurik.com Git - wxWidgets.git/blob - src/unix/utilsunx.cpp
Still with the same 'read after close' thing...
[wxWidgets.git] / src / unix / utilsunx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilsunx.cpp
3 // Purpose: generic Unix implementation of many wx functions
4 // Author: Vadim Zeitlin
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #include "wx/defs.h"
19 #include "wx/string.h"
20
21 #include "wx/intl.h"
22 #include "wx/log.h"
23
24 #include "wx/utils.h"
25 #include "wx/process.h"
26 #include "wx/thread.h"
27
28 #if wxUSE_GUI
29 #include "wx/unix/execute.h"
30 #endif
31
32 #include <stdarg.h>
33 #include <dirent.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <sys/wait.h>
39 #include <pwd.h>
40 #include <errno.h>
41 #include <netdb.h>
42 #include <signal.h>
43 #include <fcntl.h> // for O_WRONLY and friends
44 #include <time.h> // nanosleep() and/or usleep()
45 #include <ctype.h> // isspace()
46 #include <sys/time.h> // needed for FD_SETSIZE
47
48 #ifdef HAVE_UNAME
49 #include <sys/utsname.h> // for uname()
50 #endif // HAVE_UNAME
51
52 // ----------------------------------------------------------------------------
53 // conditional compilation
54 // ----------------------------------------------------------------------------
55
56 // many versions of Unices have this function, but it is not defined in system
57 // headers - please add your system here if it is the case for your OS.
58 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
59 #if !defined(HAVE_USLEEP) && \
60 (defined(__SUN__) && !defined(__SunOs_5_6) && \
61 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
62 defined(__osf__) || defined(__EMX__)
63 extern "C"
64 {
65 #ifdef __SUN__
66 int usleep(unsigned int usec);
67 #else // !Sun
68 #ifdef __EMX__
69 /* I copied this from the XFree86 diffs. AV. */
70 #define INCL_DOSPROCESS
71 #include <os2.h>
72 inline void usleep(unsigned long delay)
73 {
74 DosSleep(delay ? (delay/1000l) : 1l);
75 }
76 #else // !Sun && !EMX
77 void usleep(unsigned long usec);
78 #endif
79 #endif // Sun/EMX/Something else
80 };
81
82 #define HAVE_USLEEP 1
83 #endif // Unices without usleep()
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // sleeping
91 // ----------------------------------------------------------------------------
92
93 void wxSleep(int nSecs)
94 {
95 sleep(nSecs);
96 }
97
98 void wxUsleep(unsigned long milliseconds)
99 {
100 #if defined(HAVE_NANOSLEEP)
101 timespec tmReq;
102 tmReq.tv_sec = (time_t)(milliseconds / 1000);
103 tmReq.tv_nsec = (milliseconds % 1000) * 1000 * 1000;
104
105 // we're not interested in remaining time nor in return value
106 (void)nanosleep(&tmReq, (timespec *)NULL);
107 #elif defined(HAVE_USLEEP)
108 // uncomment this if you feel brave or if you are sure that your version
109 // of Solaris has a safe usleep() function but please notice that usleep()
110 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
111 // documented as MT-Safe
112 #if defined(__SUN__) && wxUSE_THREADS
113 #error "usleep() cannot be used in MT programs under Solaris."
114 #endif // Sun
115
116 usleep(milliseconds * 1000); // usleep(3) wants microseconds
117 #elif defined(HAVE_SLEEP)
118 // under BeOS sleep() takes seconds (what about other platforms, if any?)
119 sleep(milliseconds * 1000);
120 #else // !sleep function
121 #error "usleep() or nanosleep() function required for wxUsleep"
122 #endif // sleep function
123 }
124
125 // ----------------------------------------------------------------------------
126 // process management
127 // ----------------------------------------------------------------------------
128
129 int wxKill(long pid, wxSignal sig)
130 {
131 return kill((pid_t)pid, (int)sig);
132 }
133
134 #define WXEXECUTE_NARGS 127
135
136 long wxExecute( const wxString& command, bool sync, wxProcess *process )
137 {
138 wxCHECK_MSG( !command.IsEmpty(), 0, wxT("can't exec empty command") );
139
140 int argc = 0;
141 wxChar *argv[WXEXECUTE_NARGS];
142 wxString argument;
143 const wxChar *cptr = command.c_str();
144 wxChar quotechar = wxT('\0'); // is arg quoted?
145 bool escaped = FALSE;
146
147 // split the command line in arguments
148 do
149 {
150 argument=wxT("");
151 quotechar = wxT('\0');
152
153 // eat leading whitespace:
154 while ( wxIsspace(*cptr) )
155 cptr++;
156
157 if ( *cptr == wxT('\'') || *cptr == wxT('"') )
158 quotechar = *cptr++;
159
160 do
161 {
162 if ( *cptr == wxT('\\') && ! escaped )
163 {
164 escaped = TRUE;
165 cptr++;
166 continue;
167 }
168
169 // all other characters:
170 argument += *cptr++;
171 escaped = FALSE;
172
173 // have we reached the end of the argument?
174 if ( (*cptr == quotechar && ! escaped)
175 || (quotechar == wxT('\0') && wxIsspace(*cptr))
176 || *cptr == wxT('\0') )
177 {
178 wxASSERT_MSG( argc < WXEXECUTE_NARGS,
179 wxT("too many arguments in wxExecute") );
180
181 argv[argc] = new wxChar[argument.length() + 1];
182 wxStrcpy(argv[argc], argument.c_str());
183 argc++;
184
185 // if not at end of buffer, swallow last character:
186 if(*cptr)
187 cptr++;
188
189 break; // done with this one, start over
190 }
191 } while(*cptr);
192 } while(*cptr);
193 argv[argc] = NULL;
194
195 // do execute the command
196 long lRc = wxExecute(argv, sync, process);
197
198 // clean up
199 argc = 0;
200 while( argv[argc] )
201 delete [] argv[argc++];
202
203 return lRc;
204 }
205
206 bool wxShell(const wxString& command)
207 {
208 wxString cmd;
209 if ( !!command )
210 cmd.Printf(wxT("xterm -e %s"), command.c_str());
211 else
212 cmd = command;
213
214 return wxExecute(cmd) != 0;
215 }
216
217 #if wxUSE_GUI
218
219 void wxHandleProcessTermination(wxEndProcessData *proc_data)
220 {
221 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
222
223 // waitpid is POSIX so should be available everywhere, however on older
224 // systems wait() might be used instead in a loop (until the right pid
225 // terminates)
226 int status = 0;
227 int rc;
228
229 // wait for child termination and if waitpid() was interrupted, try again
230 do
231 {
232 rc = waitpid(pid, &status, 0);
233 }
234 while ( rc == -1 && errno == EINTR );
235
236
237 if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) )
238 {
239 wxLogSysError(_("Waiting for subprocess termination failed"));
240 /* AFAIK, this can only happen if something went wrong within
241 wxGTK, i.e. due to a race condition or some serious bug.
242 After having fixed the order of statements in
243 GTK_EndProcessDetector(). (KB)
244 */
245 }
246 else
247 {
248 // notify user about termination if required
249 if (proc_data->process)
250 {
251 proc_data->process->OnTerminate(proc_data->pid,
252 WEXITSTATUS(status));
253 }
254 // clean up
255 if ( proc_data->pid > 0 )
256 {
257 delete proc_data;
258 }
259 else
260 {
261 // wxExecute() will know about it
262 proc_data->exitcode = status;
263
264 proc_data->pid = 0;
265 }
266 }
267 }
268
269 #endif // wxUSE_GUI
270
271 #if wxUSE_GUI
272 #define WXUNUSED_UNLESS_GUI(p) p
273 #else
274 #define WXUNUSED_UNLESS_GUI(p)
275 #endif
276
277 long wxExecute(wxChar **argv,
278 bool sync,
279 wxProcess * WXUNUSED_UNLESS_GUI(process))
280 {
281 wxCHECK_MSG( *argv, 0, wxT("can't exec empty command") );
282
283 #if wxUSE_UNICODE
284 int mb_argc = 0;
285 char *mb_argv[WXEXECUTE_NARGS];
286
287 while (argv[mb_argc])
288 {
289 wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]);
290 mb_argv[mb_argc] = strdup(mb_arg);
291 mb_argc++;
292 }
293 mb_argv[mb_argc] = (char *) NULL;
294
295 // this macro will free memory we used above
296 #define ARGS_CLEANUP \
297 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
298 free(mb_argv[mb_argc])
299 #else // ANSI
300 // no need for cleanup
301 #define ARGS_CLEANUP
302
303 wxChar **mb_argv = argv;
304 #endif // Unicode/ANSI
305
306 #if wxUSE_GUI
307 // create pipes
308 int end_proc_detect[2];
309 if (pipe(end_proc_detect) == -1)
310 {
311 wxLogSysError( _("Pipe creation failed") );
312
313 ARGS_CLEANUP;
314
315 return 0;
316 }
317 #endif // wxUSE_GUI
318
319 // fork the process
320 #ifdef HAVE_VFORK
321 pid_t pid = vfork();
322 #else
323 pid_t pid = fork();
324 #endif
325 if (pid == -1)
326 {
327 wxLogSysError( _("Fork failed") );
328
329 ARGS_CLEANUP;
330
331 return 0;
332 }
333 else if (pid == 0)
334 {
335 #if wxUSE_GUI
336 // we're in child
337 close(end_proc_detect[0]); // close reading side
338 #endif // wxUSE_GUI
339
340 // These three lines close the open file descriptors to to avoid any
341 // input/output which might block the process or irritate the user. If
342 // one wants proper IO for the subprocess, the right thing to do is
343 // to start an xterm executing it.
344 if (sync == 0)
345 {
346 // leave stderr opened, it won't do any hurm
347 for ( int fd = 0; fd < FD_SETSIZE; fd++ )
348 {
349 #if wxUSE_GUI
350 if ( fd == end_proc_detect[1] )
351 continue;
352 #endif // wxUSE_GUI
353
354 if ( fd != STDERR_FILENO )
355 close(fd);
356 }
357 }
358
359 #if 0
360 close(STDERR_FILENO);
361
362 // some programs complain about stderr not being open, so redirect
363 // them:
364 open("/dev/null", O_RDONLY); // stdin
365 open("/dev/null", O_WRONLY); // stdout
366 open("/dev/null", O_WRONLY); // stderr
367 #endif
368
369 execvp (*mb_argv, mb_argv);
370
371 // there is no return after successful exec()
372 wxFprintf(stderr, _("Can't execute '%s'\n"), *argv);
373
374 _exit(-1);
375 }
376 else
377 {
378 #if wxUSE_GUI
379 wxEndProcessData *data = new wxEndProcessData;
380
381 ARGS_CLEANUP;
382
383 if ( sync )
384 {
385 wxASSERT_MSG( !process, wxT("wxProcess param ignored for sync exec") );
386 data->process = NULL;
387
388 // sync execution: indicate it by negating the pid
389 data->pid = -pid;
390 data->tag = wxAddProcessCallback(data, end_proc_detect[0]);
391 // we're in parent
392 close(end_proc_detect[1]); // close writing side
393
394 // it will be set to 0 from GTK_EndProcessDetector
395 while (data->pid != 0)
396 wxYield();
397
398 int exitcode = data->exitcode;
399
400 delete data;
401
402 return exitcode;
403 }
404 else
405 {
406 // async execution, nothing special to do - caller will be
407 // notified about the process termination if process != NULL, data
408 // will be deleted in GTK_EndProcessDetector
409 data->process = process;
410 data->pid = pid;
411 data->tag = wxAddProcessCallback(data, end_proc_detect[0]);
412 // we're in parent
413 close(end_proc_detect[1]); // close writing side
414
415 return pid;
416 }
417 #else // !wxUSE_GUI
418 wxASSERT_MSG( sync, wxT("async execution not supported yet") );
419
420 int exitcode = 0;
421 if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) )
422 {
423 wxLogSysError(_("Waiting for subprocess termination failed"));
424 }
425
426 return exitcode;
427 #endif // wxUSE_GUI
428 }
429 return 0;
430
431 #undef ARGS_CLEANUP
432 }
433
434 // ----------------------------------------------------------------------------
435 // file and directory functions
436 // ----------------------------------------------------------------------------
437
438 const wxChar* wxGetHomeDir( wxString *home )
439 {
440 *home = wxGetUserHome( wxString() );
441 if ( home->IsEmpty() )
442 *home = wxT("/");
443
444 return home->c_str();
445 }
446
447 #if wxUSE_UNICODE
448 const wxMB2WXbuf wxGetUserHome( const wxString &user )
449 #else // just for binary compatibility -- there is no 'const' here
450 char *wxGetUserHome( const wxString &user )
451 #endif
452 {
453 struct passwd *who = (struct passwd *) NULL;
454
455 if ( !user )
456 {
457 wxChar *ptr;
458
459 if ((ptr = wxGetenv(wxT("HOME"))) != NULL)
460 {
461 return ptr;
462 }
463 if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL)
464 {
465 who = getpwnam(wxConvertWX2MB(ptr));
466 }
467
468 // We now make sure the the user exists!
469 if (who == NULL)
470 {
471 who = getpwuid(getuid());
472 }
473 }
474 else
475 {
476 who = getpwnam (user.mb_str());
477 }
478
479 return wxConvertMB2WX(who ? who->pw_dir : 0);
480 }
481
482 // ----------------------------------------------------------------------------
483 // network and user id routines
484 // ----------------------------------------------------------------------------
485
486 // retrieve either the hostname or FQDN depending on platform (caller must
487 // check whether it's one or the other, this is why this function is for
488 // private use only)
489 static bool wxGetHostNameInternal(wxChar *buf, int sz)
490 {
491 wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") );
492
493 *buf = wxT('\0');
494
495 // we're using uname() which is POSIX instead of less standard sysinfo()
496 #if defined(HAVE_UNAME)
497 struct utsname uts;
498 bool ok = uname(&uts) != -1;
499 if ( ok )
500 {
501 wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1);
502 buf[sz] = wxT('\0');
503 }
504 #elif defined(HAVE_GETHOSTNAME)
505 bool ok = gethostname(buf, sz) != -1;
506 #else // no uname, no gethostname
507 wxFAIL_MSG(wxT("don't know host name for this machine"));
508
509 bool ok = FALSE;
510 #endif // uname/gethostname
511
512 if ( !ok )
513 {
514 wxLogSysError(_("Cannot get the hostname"));
515 }
516
517 return ok;
518 }
519
520 bool wxGetHostName(wxChar *buf, int sz)
521 {
522 bool ok = wxGetHostNameInternal(buf, sz);
523
524 if ( ok )
525 {
526 // BSD systems return the FQDN, we only want the hostname, so extract
527 // it (we consider that dots are domain separators)
528 wxChar *dot = wxStrchr(buf, wxT('.'));
529 if ( dot )
530 {
531 // nuke it
532 *dot = wxT('\0');
533 }
534 }
535
536 return ok;
537 }
538
539 bool wxGetFullHostName(wxChar *buf, int sz)
540 {
541 bool ok = wxGetHostNameInternal(buf, sz);
542
543 if ( ok )
544 {
545 if ( !wxStrchr(buf, wxT('.')) )
546 {
547 struct hostent *host = gethostbyname(wxConvertWX2MB(buf));
548 if ( !host )
549 {
550 wxLogSysError(_("Cannot get the official hostname"));
551
552 ok = FALSE;
553 }
554 else
555 {
556 // the canonical name
557 wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz);
558 }
559 }
560 //else: it's already a FQDN (BSD behaves this way)
561 }
562
563 return ok;
564 }
565
566 bool wxGetUserId(wxChar *buf, int sz)
567 {
568 struct passwd *who;
569
570 *buf = wxT('\0');
571 if ((who = getpwuid(getuid ())) != NULL)
572 {
573 wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
574 return TRUE;
575 }
576
577 return FALSE;
578 }
579
580 bool wxGetUserName(wxChar *buf, int sz)
581 {
582 struct passwd *who;
583
584 *buf = wxT('\0');
585 if ((who = getpwuid (getuid ())) != NULL)
586 {
587 // pw_gecos field in struct passwd is not standard
588 #if HAVE_PW_GECOS
589 char *comma = strchr(who->pw_gecos, ',');
590 if (comma)
591 *comma = '\0'; // cut off non-name comment fields
592 wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1);
593 #else // !HAVE_PW_GECOS
594 wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
595 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
596 return TRUE;
597 }
598
599 return FALSE;
600 }
601
602 wxString wxGetOsDescription()
603 {
604 #ifndef WXWIN_OS_DESCRIPTION
605 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
606 #else
607 return WXWIN_OS_DESCRIPTION;
608 #endif
609 }
610
611 // ----------------------------------------------------------------------------
612 // error and debug output routines (deprecated, use wxLog)
613 // ----------------------------------------------------------------------------
614
615 void wxDebugMsg( const char *format, ... )
616 {
617 va_list ap;
618 va_start( ap, format );
619 vfprintf( stderr, format, ap );
620 fflush( stderr );
621 va_end(ap);
622 }
623
624 void wxError( const wxString &msg, const wxString &title )
625 {
626 wxFprintf( stderr, _("Error ") );
627 if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) );
628 if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) );
629 wxFprintf( stderr, wxT(".\n") );
630 }
631
632 void wxFatalError( const wxString &msg, const wxString &title )
633 {
634 wxFprintf( stderr, _("Error ") );
635 if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) );
636 if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) );
637 wxFprintf( stderr, wxT(".\n") );
638 exit(3); // the same exit code as for abort()
639 }
640