Fixed DevStudio 2003 build with memory check code.
[pwlib.git] / src / ptlib / unix / tlib.cxx
blobf3b70839b2b021b2e5bf62516c87781299294164
1 /*
2 * tlib.cxx
4 * Miscelaneous class implementation
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
29 * $Log$
30 * Revision 1.83 2007/07/09 00:13:27 csoutheren
31 * Fix compile on MacOSX
33 * Revision 1.82 2007/07/06 02:12:14 csoutheren
34 * Add extra memory leak debugging on Linux
35 * Remove compile warnings
37 * Revision 1.81 2007/06/29 02:47:28 rjongbloed
38 * Added PString::FindSpan() function (strspn equivalent) with slightly nicer semantics.
40 * Revision 1.80 2007/05/01 10:20:44 csoutheren
41 * Applied 1703617 - Prevention of application deadlock caused by too many timers
42 * Thanks to Fabrizio Ammollo
44 * Revision 1.79 2006/07/14 04:55:10 csoutheren
45 * Applied 1520151 - Adds PID to tracefile + Rolling Date pattern
46 * Thanks to Paul Nader
48 * Revision 1.78 2006/06/21 13:27:03 csoutheren
49 * Fixed link problem with gcc 2.95.3
51 * Revision 1.77 2006/06/21 03:28:44 csoutheren
52 * Various cleanups thanks for Frederic Heem
54 * Revision 1.76 2005/11/30 12:47:42 csoutheren
55 * Removed tabs, reformatted some code, and changed tags for Doxygen
57 * Revision 1.75 2005/11/08 10:35:53 csoutheren
58 * Added missing pragma
60 * Revision 1.74 2005/08/04 18:54:10 csoutheren
61 * Applied fix for bug Bugs item #1244310
62 * Fix problem wioth --disable-openh323 option
63 * Thanks to Michael Manousos
65 * Revision 1.73 2005/02/22 03:25:43 dereksmithies
66 * Add fix from huangzb@a-star.com.cn to correct a typo. Many thanks.
68 * Revision 1.72 2005/01/26 05:38:05 csoutheren
69 * Added ability to remove config file support
71 * Revision 1.71 2005/01/04 08:09:43 csoutheren
72 * Fixed Linux configure problems
74 * Revision 1.70 2004/07/11 07:56:36 csoutheren
75 * Applied jumbo VxWorks patch, thanks to Eize Slange
77 * Revision 1.69 2003/09/18 23:52:08 dereksmithies
78 * Fix checks on NULL pointers. Many thanks to Chris Rankin.
80 * Revision 1.68 2003/09/17 01:18:04 csoutheren
81 * Removed recursive include file system and removed all references
82 * to deprecated coooperative threading support
84 * Revision 1.67 2003/01/24 10:21:06 robertj
85 * Fixed issues in RTEMS support, thanks Vladimir Nesic
87 * Revision 1.66 2002/12/11 23:02:39 robertj
88 * Added ability to set user identity temporarily and permanently.
89 * Added ability to have username exclusively digits which corresponds to a uid.
90 * Added get and set users group functions.
92 * Revision 1.65 2002/12/02 03:57:18 robertj
93 * More RTEMS support patches, thank you Vladimir Nesic.
95 * Revision 1.64 2002/11/22 10:14:07 robertj
96 * QNX port, thanks Xiaodan Tang
98 * Revision 1.63 2002/11/02 00:32:21 robertj
99 * Further fixes to VxWorks (Tornado) port, thanks Andreas Sikkema.
101 * Revision 1.62 2002/10/17 13:44:27 robertj
102 * Port to RTEMS, thanks Vladimir Nesic.
104 * Revision 1.61 2002/10/10 04:43:44 robertj
105 * VxWorks port, thanks Martijn Roest
107 * Revision 1.60 2002/06/27 08:09:06 robertj
108 * GNU GCC 3.1 compatibility under Solaris
110 * Revision 1.59 2001/11/25 23:30:31 robertj
111 * Added PProcess::SetUserName() function to set euid
113 * Revision 1.58 2001/10/11 02:20:54 robertj
114 * Added IRIX support (no audio/video), thanks Andre Schulze.
116 * Revision 1.57 2001/09/18 05:56:03 robertj
117 * Fixed numerous problems with thread suspend/resume and signals handling.
119 * Revision 1.56 2001/08/11 15:38:43 rogerh
120 * Add Mac OS Carbon changes from John Woods <jfw@jfwhome.funhouse.com>
122 * Revision 1.55 2001/06/30 06:59:07 yurik
123 * Jac Goudsmit from Be submit these changes 6/28. Implemented by Yuri Kiryanov
125 * Revision 1.54 2001/03/29 03:24:31 robertj
126 * Removed capture of SIGQUIT so can still dro a core on demand.
128 * Revision 1.53 2001/03/14 01:16:11 robertj
129 * Fixed signals processing, now uses housekeeping thread to handle signals
130 * synchronously. This also fixes issues with stopping PServiceProcess.
132 * Revision 1.52 2001/03/07 07:31:25 yurik
133 * refined BeOS constants
135 * Revision 1.51 2000/06/21 01:01:22 robertj
136 * AIX port, thanks Wolfgang Platzer (wolfgang.platzer@infonova.at).
138 * Revision 1.50 2000/04/09 18:19:23 rogerh
139 * Add my changes for NetBSD support.
141 * Revision 1.49 2000/04/06 12:19:49 rogerh
142 * Add Mac OS X support submitted by Kevin Packard
144 * Revision 1.48 2000/03/08 12:17:09 rogerh
145 * Add OpenBSD support
147 * Revision 1.47 1999/09/03 02:26:25 robertj
148 * Changes to aid in breaking I/O locks on thread termination. Still needs more work esp in BSD!
150 * Revision 1.46 1999/07/19 01:32:24 craigs
151 * Changed signals used in pthreads code, is used by linux version.
153 * Revision 1.45 1999/07/11 13:42:13 craigs
154 * pthreads support for Linux
156 * Revision 1.44 1999/06/28 09:28:02 robertj
157 * Portability issues, especially n BeOS (thanks Yuri!)
159 * Revision 1.43 1999/05/13 04:44:18 robertj
160 * Added SIGHUP and SIGWINCH handlers to increase and decrease the log levels.
162 * Revision 1.42 1999/03/02 05:41:59 robertj
163 * More BeOS changes
165 * Revision 1.41 1999/02/26 04:10:39 robertj
166 * More BeOS port changes
168 * Revision 1.40 1999/02/19 11:34:15 robertj
169 * Added platform dependent function for "system configuration" directory.
171 * Revision 1.39 1999/02/06 05:49:44 robertj
172 * BeOS port effort by Yuri Kiryanov <openh323@kiryanov.com>
174 * Revision 1.38 1999/01/11 12:10:39 robertj
175 * Improved operating system version display.
177 * Revision 1.37 1999/01/08 01:31:01 robertj
178 * Support for pthreads under FreeBSD
180 * Revision 1.36 1998/11/24 11:24:40 robertj
181 * Added FreeBSD OSName
183 * Revision 1.35 1998/11/24 09:39:16 robertj
184 * FreeBSD port.
186 * Revision 1.34 1998/10/31 14:14:21 robertj
187 * Changed syncptack.h to syncthrd.h for more thread synchronisation objects.
189 * Revision 1.33 1998/10/19 00:29:57 robertj
190 * Moved error stream to common.
192 * Revision 1.32 1998/09/24 04:12:22 robertj
193 * Added open software license.
195 * Revision 1.31 1998/05/30 14:58:56 robertj
196 * Fixed shutdown deadlock (and other failure modes) in cooperative threads.
198 * Revision 1.30 1998/04/17 15:13:08 craigs
199 * Added lazy writes to Config cache
201 * Revision 1.29 1998/03/29 10:42:16 craigs
202 * Changed for new initialisation scheme
204 * Revision 1.28 1998/03/26 05:01:12 robertj
205 * Added PMutex and PSyncPoint classes.
207 * Revision 1.27 1998/01/04 08:09:23 craigs
208 * Added support for PThreads through use of reentrant system calls
210 * Revision 1.26 1998/01/03 22:46:44 craigs
211 * Added PThread support
213 * Revision 1.25 1997/05/10 08:04:15 craigs
214 * Added new routines for access to PErrorStream
216 * Revision 1.24 1997/04/22 10:57:53 craigs
217 * Removed DLL functions and added call the FreeStack
219 * Revision 1.23 1997/02/23 03:06:00 craigs
220 * Changed for PProcess::Current reference
222 * Revision 1.22 1997/02/14 09:18:36 craigs
223 * Changed for PProcess::Current being a reference rather that a ptr
225 * Revision 1.21 1996/12/30 03:21:46 robertj
226 * Added timer to block on wait for child process.
228 * Revision 1.20 1996/12/29 13:25:02 robertj
229 * Fixed GCC warnings.
231 * Revision 1.19 1996/11/16 11:11:46 craigs
232 * Fixed problem with timeout on blocked IO channels
234 * Revision 1.18 1996/11/03 04:35:58 craigs
235 * Added hack to avoid log timeouts, which shouldn't happen!
237 * Revision 1.17 1996/09/21 05:40:10 craigs
238 * Changed signal hcnalding
240 * Revision 1.16 1996/09/03 11:55:19 craigs
241 * Removed some potential problems with return values from system calls
243 * Revision 1.15 1996/06/29 01:43:11 craigs
244 * Moved AllocateStack to switch.cxx to keep platform dependent routines in one place
246 * Revision 1.14 1996/06/10 12:46:53 craigs
247 * Changed process.h include
249 * Revision 1.13 1996/05/25 06:06:33 craigs
250 * Sun4 fixes and updated for gcc 2.7.2
252 * Revision 1.12 1996/05/09 10:55:59 craigs
253 * More SunOS fixes
255 * Revision 1.11 1996/05/03 13:15:27 craigs
256 * More Sun4 & Solaris fixes
258 * Revision 1.10 1996/05/03 13:11:35 craigs
259 * More Sun4 fixes
261 * Revision 1.9 1996/05/02 12:11:54 craigs
262 * Sun4 fixes
264 * Revision 1.8 1996/04/18 11:43:38 craigs
265 * Changed GetHomeDir to use effective UID for uid, and changed to
266 * look at passwd file info *before* $HOME variable
268 * Revision 1.7 1996/04/15 10:49:11 craigs
269 * Last build prior to release of MibMaster v1.0
271 * Revision 1.6 1996/01/26 11:09:42 craigs
272 * Added signal handlers
276 #define _OSUTIL_CXX
278 //#define SIGNALS_DEBUG
280 #pragma implementation "args.h"
281 #pragma implementation "pprocess.h"
282 #pragma implementation "thread.h"
283 #pragma implementation "semaphor.h"
284 #pragma implementation "mutex.h"
285 #pragma implementation "critsec.h"
286 #pragma implementation "psync.h"
287 #pragma implementation "syncpoint.h"
288 #pragma implementation "syncthrd.h"
290 #include "ptlib.h"
291 #include <ptlib/pprocess.h>
293 #ifdef P_VXWORKS
294 #include <sys/times.h>
295 #include <time.h>
296 #include <hostLib.h>
297 #include <remLib.h>
298 #include <taskLib.h>
299 #include <intLib.h>
300 #else
301 #include <sys/time.h>
302 #include <pwd.h>
303 #include <grp.h>
304 #endif // P_VXWORKS
305 #include <signal.h>
306 #include <sys/wait.h>
307 #include <errno.h>
309 #if defined(P_LINUX)
310 #include <sys/cdefs.h>
311 #include <sys/types.h>
312 #include <sys/mman.h>
313 #endif
315 #if defined(P_LINUX) || defined(P_SUN4) || defined(P_SOLARIS) || defined(P_FREEBSD) || defined(P_OPENBSD) || defined(P_NETBSD) || defined(P_MACOSX) || defined(P_MACOS) || defined (P_AIX) || defined(__BEOS__) || defined(P_IRIX) || defined(P_QNX)
316 #include <sys/utsname.h>
317 #define HAS_UNAME
318 #elif defined(P_RTEMS)
319 extern "C" {
320 #include <sys/utsname.h>
322 #define HAS_UNAME
323 #endif
325 #include "uerror.h"
327 #if defined(P_HPUX9)
328 #define SELECT(p1,p2,p3,p4,p5) select(p1,(int *)(p2),(int *)(p3),(int *)(p4),p5)
329 #else
330 #define SELECT(p1,p2,p3,p4,p5) select(p1,p2,p3,p4,p5)
331 #endif
333 #if defined(P_SUN4)
334 extern "C" void bzero(void *, int);
335 extern "C" int select(int width,
336 fd_set *readfds,
337 fd_set *writefds,
338 fd_set *exceptfds,
339 struct timeval *timeout);
340 #endif
342 #ifdef __BEOS__
343 #include "OS.h"
344 #endif
346 #include "../common/pglobalstatic.cxx"
349 PString PProcess::GetOSClass()
351 #ifndef __BEOS__
352 return PString("Unix");
353 #elif defined P_VXWORKS
354 return PString("VxWorks");
355 #else
356 return PString("Be Inc.");
357 #endif
360 PString PProcess::GetOSName()
362 #if defined(HAS_UNAME)
363 struct utsname info;
364 uname(&info);
365 #ifdef P_SOLARIS
366 return PString(info.sysname) & info.release;
367 #else
368 return PString(info.sysname);
369 #endif
370 #elif defined(P_VXWORKS)
371 return PString::Empty();
372 #else
373 #warning No GetOSName specified
374 return PString("Unknown");
375 #endif
378 PString PProcess::GetOSHardware()
380 #if defined(HAS_UNAME)
381 struct utsname info;
382 uname(&info);
383 return PString(info.machine);
384 #elif defined(P_VXWORKS)
385 return PString(sysModel());
386 #else
387 #warning No GetOSHardware specified
388 return PString("unknown");
389 #endif
392 PString PProcess::GetOSVersion()
394 #if defined(HAS_UNAME)
395 struct utsname info;
396 uname(&info);
397 #ifdef P_SOLARIS
398 return PString(info.version);
399 #else
400 return PString(info.release);
401 #endif
402 #elif defined(P_VXWORKS)
403 return PString(sysBspRev());
404 #else
405 #warning No GetOSVersion specified
406 return PString("?.?");
407 #endif
410 PDirectory PProcess::GetOSConfigDir()
412 #ifdef P_VXWORKS
413 return "./";
414 #else
415 return "/etc";
416 #endif // P_VXWORKS
419 PDirectory PProcess::PXGetHomeDir ()
422 #ifdef P_VXWORKS
423 return "./";
424 #else
425 PString dest;
426 char *ptr;
427 struct passwd *pw = NULL;
429 #if defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB)
430 struct passwd pwd;
431 char buffer[1024];
432 #if defined (P_LINUX) || defined(P_AIX) || defined(P_IRIX) || (__GNUC__>=3 && defined(P_SOLARIS)) || defined(P_RTEMS)
433 ::getpwuid_r(geteuid(), &pwd,
434 buffer, 1024,
435 &pw);
436 #else
437 pw = ::getpwuid_r(geteuid(), &pwd, buffer, 1024);
438 #endif
439 #else
440 pw = ::getpwuid(geteuid());
441 #endif
443 if (pw != NULL && pw->pw_dir != NULL)
444 dest = pw->pw_dir;
445 else if ((ptr = getenv ("HOME")) != NULL)
446 dest = ptr;
447 else
448 dest = ".";
450 if (dest.GetLength() > 0 && dest[dest.GetLength()-1] != '/')
451 dest += "/";
453 return dest;
454 #endif
457 ///////////////////////////////////////////////////////////////////////////////
459 // PProcess
461 // Return the effective user name of the process, eg "root" etc.
463 PString PProcess::GetUserName() const
466 #ifdef P_VXWORKS
468 char pnamebuf[1024];
469 int len = 1024;
470 STATUS gethostresult;
471 gethostresult =::gethostname(pnamebuf,len);
472 if (gethostresult == OK)
473 return PString(pnamebuf,len);
474 else
475 return PString("VxWorks");
477 #else
479 #if defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB)
480 struct passwd pwd;
481 char buffer[1024];
482 struct passwd * pw = NULL;
483 #if defined (P_LINUX) || defined (P_AIX) || defined(P_IRIX) || (__GNUC__>=3 && defined(P_SOLARIS)) || defined(P_RTEMS)
484 ::getpwuid_r(geteuid(), &pwd, buffer, 1024, &pw);
485 #else
486 pw = ::getpwuid_r(geteuid(), &pwd, buffer, 1024);
487 #endif
488 #else
489 struct passwd * pw = ::getpwuid(geteuid());
490 #endif
492 char * ptr;
493 if (pw != NULL && pw->pw_name != NULL)
494 return PString(pw->pw_name);
495 else if ((ptr = getenv("USER")) != NULL)
496 return PString(ptr);
497 else
498 return PString("user");
499 #endif // P_VXWORKS
503 BOOL PProcess::SetUserName(const PString & username, BOOL permanent)
505 #ifdef P_VXWORKS
506 PAssertAlways("PProcess::SetUserName - not implemented for VxWorks");
507 return FALSE;
508 #else
509 if (username.IsEmpty())
510 return seteuid(getuid()) != -1;
512 int uid = -1;
514 if (username[0] == '#') {
515 PString s = username.Mid(1);
516 if (s.FindSpan("1234567890") == P_MAX_INDEX)
517 uid = s.AsInteger();
519 else {
520 #if defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB)
521 struct passwd pwd;
522 char buffer[1024];
523 struct passwd * pw = NULL;
524 #if defined (P_LINUX) || defined (P_AIX) || defined(P_IRIX) || (__GNUC__>=3 && defined(P_SOLARIS)) || defined(P_RTEMS)
525 ::getpwnam_r(username, &pwd, buffer, 1024, &pw);
526 #else
527 pw = ::getpwnam_r(username, &pwd, buffer, 1024);
528 #endif
529 #else
530 struct passwd * pw = ::getpwnam(username);
531 #endif
533 if (pw != NULL && pw->pw_name != NULL)
534 uid = pw->pw_uid;
535 else {
536 if (username.FindSpan("1234567890") == P_MAX_INDEX)
537 uid = username.AsInteger();
541 if (uid < 0)
542 return FALSE;
544 if (permanent)
545 return setuid(uid) != -1;
547 return seteuid(uid) != -1;
548 #endif // P_VXWORKS
552 ///////////////////////////////////////////////////////////////////////////////
554 // PProcess
556 // Return the effective group name of the process, eg "wheel" etc.
558 PString PProcess::GetGroupName() const
561 #ifdef P_VXWORKS
563 return PString("VxWorks");
565 #else
567 #if defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB)
568 struct group grp;
569 char buffer[1024];
570 struct group * gr = NULL;
571 #if defined (P_LINUX) || defined (P_AIX) || defined(P_IRIX) || (__GNUC__>=3 && defined(P_SOLARIS)) || defined(P_RTEMS)
572 ::getgrgid_r(getegid(), &grp, buffer, 1024, &gr);
573 #else
574 gr = ::getgrgid_r(getegid(), &grp, buffer, 1024);
575 #endif
576 #else
577 struct group * gr = ::getgrgid(getegid());
578 #endif
580 char * ptr;
581 if (gr != NULL && gr->gr_name != NULL)
582 return PString(gr->gr_name);
583 else if ((ptr = getenv("GROUP")) != NULL)
584 return PString(ptr);
585 else
586 return PString("group");
587 #endif // P_VXWORKS
591 BOOL PProcess::SetGroupName(const PString & groupname, BOOL permanent)
593 #ifdef P_VXWORKS
594 PAssertAlways("PProcess::SetGroupName - not implemented for VxWorks");
595 return FALSE;
596 #else
597 if (groupname.IsEmpty())
598 return setegid(getgid()) != -1;
600 int gid = -1;
602 if (groupname[0] == '#') {
603 PString s = groupname.Mid(1);
604 if (s.FindSpan("1234567890") == P_MAX_INDEX)
605 gid = s.AsInteger();
607 else {
608 #if defined(P_PTHREADS) && !defined(P_THREAD_SAFE_CLIB)
609 struct group grp;
610 char buffer[1024];
611 struct group * gr = NULL;
612 #if defined (P_LINUX) || defined (P_AIX) || defined(P_IRIX) || (__GNUC__>=3 && defined(P_SOLARIS)) || defined(P_RTEMS)
613 ::getgrnam_r(groupname, &grp, buffer, 1024, &gr);
614 #else
615 gr = ::getgrnam_r(groupname, &grp, buffer, 1024);
616 #endif
617 #else
618 struct group * gr = ::getgrnam(groupname);
619 #endif
621 if (gr != NULL && gr->gr_name != NULL)
622 gid = gr->gr_gid;
623 else {
624 if (groupname.FindSpan("1234567890") == P_MAX_INDEX)
625 gid = groupname.AsInteger();
629 if (gid < 0)
630 return FALSE;
632 if (permanent)
633 return setgid(gid) != -1;
635 return setegid(gid) != -1;
636 #endif // P_VXWORKS
639 extern PProcess * PProcessInstance;;
641 PString PX_GetThreadName(pthread_t id)
643 if (PProcessInstance != NULL) {
644 PWaitAndSignal m(PProcessInstance->threadMutex);
645 PThread & thread = PProcessInstance->activeThreads[(unsigned)id];
646 return thread.GetThreadName();
648 return psprintf("%08x", id);
651 void PProcess::PXShowSystemWarning(PINDEX num)
653 PXShowSystemWarning(num, "");
656 void PProcess::PXShowSystemWarning(PINDEX num, const PString & str)
658 PProcess::Current()._PXShowSystemWarning(num, str);
661 void PProcess::_PXShowSystemWarning(PINDEX code, const PString & str)
663 PError << "PWLib " << GetOSClass() << " error #" << code << '-' << str << endl;
666 void PXSignalHandler(int sig)
668 #ifdef SIGNALS_DEBUG
669 fprintf(stderr,"\nSIGNAL<%u>\n",sig);
670 #endif
672 PProcess & process = PProcess::Current();
673 process.pxSignals |= 1 << sig;
674 process.PXOnAsyncSignal(sig);
675 #if defined(P_MAC_MPTHREADS)
676 process.SignalTimerChange();
677 #elif defined(P_PTHREADS)
678 // Inform house keeping thread we have a signal to be processed
679 process.SignalTimerChange();
680 #endif
681 signal(sig, PXSignalHandler);
684 void PProcess::PXCheckSignals()
686 if (pxSignals == 0)
687 return;
689 #ifdef SIGNALS_DEBUG
690 fprintf(stderr,"\nCHKSIG<%x>\n",pxSignals);
691 #endif
693 for (int sig = 0; sig < 32; sig++) {
694 int bit = 1 << sig;
695 if ((pxSignals&bit) != 0) {
696 pxSignals &= ~bit;
697 PXOnSignal(sig);
703 void SetSignals(void (*handler)(int))
705 #ifdef SIGNALS_DEBUG
706 fprintf(stderr,"\nSETSIG<%x>\n",(INT)handler);
707 #endif
709 if (handler == NULL)
710 handler = SIG_DFL;
712 #ifdef SIGHUP
713 signal(SIGHUP, handler);
714 #endif
715 #ifdef SIGINT
716 signal(SIGINT, handler);
717 #endif
718 #ifdef SIGUSR1
719 signal(SIGUSR1, handler);
720 #endif
721 #ifdef SIGUSR2
722 signal(SIGUSR2, handler);
723 #endif
724 #ifdef SIGPIPE
725 signal(SIGPIPE, handler);
726 #endif
727 #ifdef SIGTERM
728 signal(SIGTERM, handler);
729 #endif
730 #ifdef SIGWINCH
731 signal(SIGWINCH, handler);
732 #endif
733 #ifdef SIGPROF
734 signal(SIGPROF, handler);
735 #endif
739 void PProcess::PXOnAsyncSignal(int sig)
741 #ifdef SIGNALS_DEBUG
742 fprintf(stderr,"\nASYNCSIG<%u>\n",sig);
743 #endif
745 switch (sig) {
746 case SIGINT:
747 case SIGTERM:
748 case SIGHUP:
749 raise(SIGKILL);
750 break;
751 default:
752 return;
756 void PProcess::PXOnSignal(int sig)
758 #ifdef SIGNALS_DEBUG
759 fprintf(stderr,"\nSYNCSIG<%u>\n",sig);
760 #endif
761 if (sig == 28) {
762 #if PMEMORY_CHECK
763 BOOL oldIgnore = PMemoryHeap::SetIgnoreAllocations(TRUE);
764 static DWORD allocationIndex = 0;
765 #endif
766 PStringStream strm;
767 threadMutex.Wait();
768 PINDEX i;
769 strm << "===============\n";
770 strm << activeThreads.GetSize() << " active threads\n";
771 for (i = 0; i < activeThreads.GetSize(); ++i) {
772 POrdinalKey key = activeThreads.GetKeyAt(i);
773 PThread & thread = activeThreads[key];
774 strm << " " << thread << "\n";
776 #if PMEMORY_CHECK
777 strm << "---------------\n";
778 PMemoryHeap::DumpObjectsSince(allocationIndex, strm);
779 allocationIndex = PMemoryHeap::GetAllocationRequest();
780 #endif
781 strm << "===============\n";
782 threadMutex.Signal();
783 fprintf(stderr, "%s", strm.GetPointer());
784 #if PMEMORY_CHECK
785 PMemoryHeap::SetIgnoreAllocations(oldIgnore);
786 #endif
790 void PProcess::CommonConstruct()
792 // Setup signal handlers
793 pxSignals = 0;
795 SetSignals(&PXSignalHandler);
797 #if !defined(P_VXWORKS) && !defined(P_RTEMS)
798 // initialise the timezone information
799 tzset();
800 #endif
802 #ifdef P_CONFIG_FILE
803 CreateConfigFilesDictionary();
804 #endif
807 void PProcess::CommonDestruct()
809 #ifdef P_CONFIG_FILE
810 delete configFiles;
811 #endif
812 configFiles = NULL;
813 SetSignals(NULL);
816 // rtems fixes
817 #ifdef P_RTEMS
819 extern "C" {
820 #include <netinet/in.h>
821 #include <rtems/rtems_bsdnet.h>
824 int socketpair(int d, int type, int protocol, int sv[2])
826 static int port_count = IPPORT_USERRESERVED;
827 int s;
828 int addrlen;
829 struct sockaddr_in addr1, addr2;
830 static int network_status = 1;
833 if (network_status>0)
835 printf("\"Network\" initializing!\n");
836 network_status = rtems_bsdnet_initialize_network();
837 if (network_status == 0)
838 printf("\"Network\" initialized!\n");
839 else
841 printf("Error: %s\n", strerror(errno));
842 return -1;
846 /* prepare sv */
847 sv[0]=sv[1]=-1;
849 /* make socket */
850 s = socket( d, type, protocol);
851 if (s<0)
852 return -1;
854 memset(&addr1, 0, sizeof addr1);
855 addr1.sin_family = d;
856 addr1.sin_port = htons(++port_count);
857 addr1.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
859 if (bind(s, (struct sockaddr *)&addr1, sizeof addr1) < 0)
861 close(s);
862 return -1;
864 if (listen(s, 2) < 0 )
866 close(s);
867 return -1;
870 sv[0] = socket(d, type, protocol);
871 if (sv[0] < 0)
873 close(s);
874 return -1;
877 memset(&addr2, 0, sizeof addr2);
878 addr2.sin_family = d;
879 addr2.sin_port = htons(++port_count);
880 addr2.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
882 if (bind(sv[0], (struct sockaddr *)&addr2, sizeof addr2) < 0)
884 close(s);
885 close(sv[0]);
886 sv[0]=-1;
887 return -1;
889 if (connect(sv[0], (struct sockaddr *)&addr1, sizeof addr1) < 0)
891 close(s);
892 close(sv[0]);
893 sv[0]=-1;
894 return -1;
897 sv[1] = accept(s, (struct sockaddr *)&addr2, &addrlen);
898 if (sv[1] < 0)
900 close(s);
901 close(sv[0]);
902 sv[0]=-1;
903 return -1;
906 close(s);
907 return 0;
911 * Loopback interface
913 extern int rtems_bsdnet_loopattach(rtems_bsdnet_ifconfig *);
914 static struct rtems_bsdnet_ifconfig loopback_config = {
915 "lo0", /* name */
916 rtems_bsdnet_loopattach, /* attach function */
918 NULL, /* link to next interface */
920 "127.0.0.1", /* IP address */
921 "255.0.0.0", /* IP net mask */
924 #include <bsp.h>
925 #warning Change lines below to match Your system settings
928 * Default network interface
930 static struct rtems_bsdnet_ifconfig netdriver_config = {
931 RTEMS_BSP_NETWORK_DRIVER_NAME, /* name */
932 RTEMS_BSP_NETWORK_DRIVER_ATTACH, /* attach function */
934 &loopback_config, /* link to next interface */
936 "10.0.0.2", /* IP address */
937 "255.255.255.0", /* IP net mask */
939 NULL, /* Driver supplies hardware address */
940 0 /* Use default driver parameters */
944 * Network configuration
946 struct rtems_bsdnet_config rtems_bsdnet_config = {
947 &netdriver_config,
949 NULL, /* no bootp function */
951 1, /* Default network task priority */
952 0, /* Default mbuf capacity */
953 0, /* Default mbuf cluster capacity */
955 "computer.name", /* Host name */
956 "domain.name", /* Domain name */
957 "10.0.0.1", /* Gateway */
958 "10.0.0.1", /* Log host */
959 {"10.0.0.1" }, /* Name server(s) */
960 {"10.0.0.1" }, /* NTP server(s) */
964 #define CONFIGURE_TEST_NEEDS_CONSOLE_DRIVER
965 #define CONFIGURE_TEST_NEEDS_CLOCK_DRIVER
966 #define CONFIGURE_TEST_NEEDS_TIMER_DRIVER
968 #define CONFIGURE_MICROSECONDS_PER_TICK 1000
969 #define CONFIGURE_TICKS_PER_TIMESLICE 50
971 #define CONFIGURE_MAXIMUM_TASKS rtems_resource_unlimited(50)
972 #define CONFIGURE_MAXIMUM_TIMERS rtems_resource_unlimited(50)
973 #define CONFIGURE_MAXIMUM_SEMAPHORES rtems_resource_unlimited(50)
974 #define CONFIGURE_MAXIMUM_MESSAGE_QUEUES rtems_resource_unlimited(50)
975 #define CONFIGURE_MAXIMUM_MUTEXES rtems_resource_unlimited(50)
977 #define CONFIGURE_MAXIMUM_POSIX_THREADS 500
978 #define CONFIGURE_MAXIMUM_POSIX_MUTEXES 500
979 #define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 500
980 #define CONFIGURE_MAXIMUM_POSIX_KEYS 500
981 #define CONFIGURE_MAXIMUM_POSIX_TIMERS 500
982 #define CONFIGURE_MAXIMUM_POSIX_QUEUED_SIGNALS 500
983 #define CONFIGURE_MAXIMUM_POSIX_MESSAGE_QUEUES 500
984 #define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES 500
986 #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 500
987 #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
989 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
990 #define CONFIGURE_INIT_TASK_INITIAL_MODES (RTEMS_PREEMPT | RTEMS_TIMESLICE)
992 #ifdef DEBUG
993 #define STACK_CHECKER_ON
994 #endif
996 void* POSIX_Init(void*);
997 #define CONFIGURE_INIT
998 #include <confdefs.h>
1001 #endif // P_RTEMS
1004 //////////////////////////////////////////////////////////////////
1006 // Non-PTHREAD based routines
1009 #if defined(P_MAC_MPTHREADS)
1010 #include "tlibmpthrd.cxx"
1011 #elif defined(P_PTHREADS)
1012 #include "tlibthrd.cxx"
1013 #elif defined(BE_THREADS)
1014 #include "tlibbe.cxx"
1015 #elif defined(VX_TASKS)
1016 #include "tlibvx.cxx"
1017 #endif