PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pprocess.h
Go to the documentation of this file.
1 /*
2  * pprocess.h
3  *
4  * Operating System Process (running program executable) class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
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/
14  *
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.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 #ifndef PTLIB_PROCESS_H
31 #define PTLIB_PROCESS_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <ptlib/mutex.h>
39 #include <ptlib/thread.h>
40 #include <ptlib/pfactory.h>
41 
42 
49 #ifdef P_VXWORKS
50 #define PCREATE_PROCESS(cls) \
51  cls instance; \
52  instance.InternalMain();
53 #elif defined(P_RTEMS)
54 #define PCREATE_PROCESS(cls) \
55 extern "C" {\
56  void* POSIX_Init( void* argument) \
57  { \
58  static cls instance; \
59  exit( instance.InternalMain() ); \
60  } \
61 }
62 #else
63 #define PCREATE_PROCESS(cls) \
64  int main(int argc, char * argv[]) \
65  { \
66  cls *pInstance = new cls(); \
67  pInstance->PreInitialise(argc, argv); \
68  int terminationValue = pInstance->InternalMain(); \
69  delete pInstance; \
70  return terminationValue; \
71  }
72 #endif // P_VXWORKS
73 
74 /*$MACRO PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build)
75  This macro is used to declare the components necessary for a user PWLib
76  process. This will declare the PProcess descendent class, eg PApplication,
77  and create an instance of the class. See the <code>PCREATE_PROCESS</code> macro
78  for more details.
79  */
80 #define PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build) \
81  class cls : public ancestor { \
82  PCLASSINFO(cls, ancestor); \
83  public: \
84  cls() : ancestor(manuf, name, major, minor, status, build) { } \
85  private: \
86  virtual void Main(); \
87  };
88 
89 class PExternalThread;
90 
92 // PProcess
93 
106 class PProcess : public PThread
107 {
108  PCLASSINFO(PProcess, PThread);
109 
110  public:
113  enum CodeStatus {
119  };
120 
123  PProcess(
124  const char * manuf = "",
125  const char * name = "",
126  unsigned majorVersion = 1,
127  unsigned minorVersion = 0,
128  CodeStatus status = ReleaseCode,
129  unsigned patchVersion = 1,
130  bool library = false,
131  bool suppressStartup = false,
132  unsigned oemVersion = 0
133  );
135 
145  const PObject & obj
146  ) const;
147 
151  virtual void PrintOn(
152  ostream & strm
153  ) const;
155 
160  virtual void Terminate();
162 
171  static PProcess & Current();
172 
175  void Startup();
176 
184  std::vector<PThreadIdentifier> & identifiers
185  );
186 
192  PThread * GetThread(
193  PThreadIdentifier threadId
194  ) const;
195 
199  virtual void OnThreadStart(
200  PThread & thread
201  );
202 
206  virtual void OnThreadEnded(
207  PThread & thread
208  );
209 
222  virtual bool OnInterrupt(
223  bool terminating
224  );
225 
232  static PBoolean IsInitialised();
233 
240  void SetTerminationValue(
241  int value
242  );
243 
253  int GetTerminationValue() const;
254 
262 
272  virtual const PString & GetManufacturer() const;
273 
283  virtual const PString & GetName() const;
284 
299  virtual PString GetVersion(
300  PBoolean full = true
301  ) const;
302 
308  const PFilePath & GetFile() const;
309 
317  PProcessIdentifier GetProcessID() const { return m_processID; }
318 
326  static PProcessIdentifier GetCurrentProcessID();
327 
330  bool IsMultipleInstance() const;
331 
334  PTime GetStartTime() const;
335 
336  struct MemoryUsage {
338  : m_virtual(0)
339  , m_resident(0)
340  , m_max(0)
341  , m_current(0)
342  , m_blocks(0)
343  { }
344 
345  size_t m_virtual;
346  size_t m_resident;
347  size_t m_max;
348  size_t m_current;
349  size_t m_blocks;
350  };
351 
354  void GetMemoryUsage(
355  MemoryUsage & usage
356  ) const;
357 
361  bool GetProcessTimes(
362  Times & times
363  ) const
364  );
365 
369  static bool GetSystemTimes(
370  Times & times
371  ));
372 
381  PString GetUserName() const;
382 
406  const PString & username,
407  PBoolean permanent = false
408  );
409 
415 
424  PString GetGroupName() const;
425 
451  const PString & groupname,
452  PBoolean permanent = false
453  );
454 
461  int GetMaxHandles() const;
462 
473  int newLimit
474  );
475 
476 #if P_CONFIG_FILE
477 
479  virtual PString GetConfigurationFile();
480 
496  const PString & path
497  );
498 #endif // P_CONFIG_FILE
499 
500 
509  static PString GetOSClass();
510 
517  static PString GetOSName();
518 
524  static PString GetOSHardware();
525 
532  static PString GetOSVersion();
533 
539  static bool IsOSVersion(
540  unsigned major,
541  unsigned minor = 0,
542  unsigned build = 0
543  );
544 
552  static PDirectory GetOSConfigDir();
553 
560  static PString GetLibVersion();
561 
563  struct VersionInfo
564  {
565  unsigned m_major;
566  unsigned m_minor;
568  unsigned m_patch;
569  unsigned m_oem;
570  unsigned m_svn;
571  const char * m_git;
572 
574  PString AsString(bool full = true) const;
575  };
577 
581  void PreInitialise(
582  int argc, // Number of program arguments.
583  char ** argv // Array of strings for program arguments.
584  );
585 
587  virtual int InternalMain(void * arg = NULL);
588 
590  virtual void AddRunTimeSignalHandlers(
592  const int * signals = NULL
593  );
594 
596  virtual void RemoveRunTimeSignalHandlers();
597 
599  virtual void AsynchronousRunTimeSignal(
600  int signal,
601  PProcessIdentifier source
602  );
603 
605  int m_signal;
606  PProcessIdentifier m_source;
607  };
608 
610  virtual void HandleRunTimeSignal(const RunTimeSignalInfo & signalInfo);
611  virtual void HandleRunTimeSignal(int signal); // Backward compatibility
612 
614  static const char * GetRunTimeSignalName(int signal);
616 
639  {
640  public:
642  { }
643 
645  : type(t)
646  { }
647 
648  static bool RegisterTypes(const PString & types, bool force = true);
649 
650  void SetIcon(const PString & icon);
651  PString GetIcon() const;
652 
653  void SetCommand(const PString & key, const PString & command);
654  PString GetCommand(const PString & key) const;
655 
656  bool GetFromSystem();
657  bool CheckIfRegistered();
658 
659  bool Register();
660 
662 
663  #if _WIN32
664  PString iconFileName;
665  PStringToString cmds;
666  #endif
667  };
669 
670  virtual bool IsServiceProcess() const;
671  bool SignalTimerChange();
672 
673  protected:
674  void PlatformConstruct();
675  void PlatformDestruct();
676 
677  // Member variables
678  bool m_library; // Indication PTLib is being used as a library for an external process.
679  int m_terminationValue; // Application return value
680 
681  PString m_manufacturer; // Application manufacturer name.
682  PString m_productName; // Application executable base name from argv[0]
683 
684  VersionInfo m_version; // Process (applications) version
685 
686  PFilePath m_executableFile; // Application executable file from argv[0] (not open)
687  PStringArray m_configurationPaths; // Explicit file or set of directories to find default PConfig
688  PArgList m_arguments; // The list of arguments
689  int m_maxHandles; // Maximum number of file handles process can open.
690 
691  PTime m_programStartTime; // time at which process was intantiated, i.e. started
692 
694 
695  // Do not write trace logs while holding m_threadMutex, as most trace logs lock
696  // the target log mutex before obtaining this in PThread::Current().
698 
699  typedef std::map<PThreadIdentifier, PThread *> ThreadMap;
701  void InternalThreadStarted(PThread * thread);
702  void InternalThreadEnded(PThread * thread);
703 
704  typedef std::list< PSharedPtr<PExternalThread> > ThreadList;
707 
709  PThread * m_houseKeeper; // Thread for doing timers, thread clean up etc.
711  void HouseKeeping();
712 
713 #if P_TIMERS
714  PTimer::List * m_timerList;
715  friend PTimer::List * PTimer::TimerList();
716 #endif
717 
718  PProcessIdentifier m_processID;
719 
720  static PRunTimeSignalHandler PlatformSetRunTimeSignalHandler(int signal);
721  static void PlatformResetRunTimeSignalHandler(int signal, PRunTimeSignalHandler previous);
723 
724  std::vector<PRunTimeSignalHandler> m_previousRunTimeSignalHandlers;
725  std::vector<RunTimeSignalInfo> m_RunTimeSignalsQueueBuffer;
728  void InternalPostRunTimeSignal(int signal, PProcessIdentifier source);
729  void InternalHandleRunTimeSignal(const RunTimeSignalInfo & signalInfo);
730 
731 #if PTRACING
732  PDECLARE_NOTIFIER(PTimer, PProcess, ProfileUpdateLogTimer) { PTRACE(3, "PTLibProfile", *this); }
733  void PrintProfileOn(ostream & strm);
734  PTimer * m_profileProcessTimer;
735  PCriticalSection m_profileProcessMutex;
736  Times m_profileLastProcessTimes;
737  set<Times> m_profileLastThreadTimes;
738 #endif // PTRACING
739 
740  friend class PThread;
741 
742 
743 // Include platform dependent part of class
744 #ifdef _WIN32
745 #include "msos/ptlib/pprocess.h"
746 #else
747 #include "unix/ptlib/pprocess.h"
748 #endif
749 };
750 
751 
754  class PLibraryProcess : public PProcess
755  {
756  PCLASSINFO(PLibraryProcess, PProcess);
757 
758  public:
764  const char * manuf = "",
765  const char * name = "",
766  unsigned majorVersionNum = 1,
767  unsigned minorVersionNum = 0,
768  CodeStatus statusCode = ReleaseCode,
769  unsigned buildNum = 1,
770  bool suppressStartup = false
771  ) : PProcess(manuf, name, majorVersionNum, minorVersionNum, statusCode, buildNum, true, suppressStartup) { }
773 
775  virtual void Main() { }
776 };
777 
778 
779 /*
780  * one instance of this class (or any descendants) will be instantiated
781  * via PGenericFactory<PProessStartup> one "main" has been started, and then
782  * the OnStartup() function will be called. The OnShutdown function will
783  * be called after main exits, and the instances will be destroyed if they
784  * are not singletons
785  */
786 class PProcessStartup : public PObject
787 {
789  public:
790  virtual void OnStartup() { }
791  virtual void OnShutdown() { }
792 };
793 
795 
796 #if PTRACING
797 
798 // using an inline definition rather than a #define crashes gcc 2.95. Go figure
799 #define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
800 
801 template <unsigned level, unsigned options = P_DEFAULT_TRACE_OPTIONS >
802 class PTraceLevelSetStartup : public PProcessStartup
803 {
804  public:
805  void OnStartup()
806  { PTrace::Initialise(level, NULL, options); }
807 };
808 
809 #endif // PTRACING
810 
811 
812 #endif // PTLIB_PROCESS_H
813 
814 
815 // End Of File ///////////////////////////////////////////////////////////////
static PString GetLibVersion()
Get the version of the PTLib library the process is running on, eg &quot;2.5beta3&quot;.
int m_terminationValue
Definition: pprocess.h:679
virtual bool OnInterrupt(bool terminating)
Callback for when a ^C (SIGINT), hangup (SIGHUP) or termination request (SIGTERM) is received by proc...
ThreadList m_externalThreads
Definition: pprocess.h:705
virtual const PString & GetName() const
Get the name of the process.
void InternalHandleRunTimeSignal(const RunTimeSignalInfo &signalInfo)
std::map< PThreadIdentifier, PThread * > ThreadMap
Definition: pprocess.h:699
virtual void Main()
&lt; Dummy Main() as libraries do not have one.
Definition: pprocess.h:775
void InternalThreadEnded(PThread *thread)
virtual bool IsServiceProcess() const
unsigned m_patch
Definition: pprocess.h:568
PString GetGroupName() const
Get the effective group name of the owner of the process, eg &quot;root&quot; etc.
unsigned m_oem
Definition: pprocess.h:569
static PString GetOSHardware()
Get the hardware the process is running on, eg &quot;sparc&quot;.
Times for execution of the thread.
Definition: thread.h:351
PStringArray m_configurationPaths
Definition: pprocess.h:687
size_t m_RunTimeSignalsQueueIn
Definition: pprocess.h:726
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
atomic< bool > m_shuttingDown
Definition: pprocess.h:693
MemoryUsage()
Definition: pprocess.h:337
static PProcess & Current()
Get the current processes object instance.
void InternalThreadStarted(PThread *thread)
This class defines an absolute time and date.
Definition: ptime.h:49
This class implements critical section mutexes using the most efficient mechanism available on the ho...
Definition: mutex.h:270
void SetIcon(const PString &icon)
bool m_library
Definition: pprocess.h:678
PCriticalSection m_RunTimeSignalsQueueMutex
Definition: pprocess.h:727
PFactory< PProcessStartup > PProcessStartupFactory
Definition: pprocess.h:794
This is a dictionary collection class of PString objects, keyed by another string.
Definition: pstring.h:3151
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
size_t m_virtual
Definition: pprocess.h:345
void SetTerminationValue(int value)
Set the termination value for the process.
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
static PString GetOSVersion()
Get the version of the operating system the process is running on, eg &quot;2.0.33&quot;.
HostSystemURLHandlerInfo()
Definition: pprocess.h:641
virtual void Terminate()
Terminate the process.
size_t m_resident
Definition: pprocess.h:346
static PRunTimeSignalHandler PlatformSetRunTimeSignalHandler(int signal)
Code is still very much under construction.
Definition: pprocess.h:115
bool SignalTimerChange()
static PString GetOSName()
Get the name of the operating system the process is running on, eg &quot;Linux&quot;.
PProcessIdentifier m_source
Definition: pprocess.h:606
PArgList & GetArguments()
Get the programme arguments.
virtual int InternalMain(void *arg=NULL)
Main function for process, called from real main after initialisation.
static PString GetOSClass()
Get the class of the operating system the process is running on, eg &quot;unix&quot;.
static bool IsOSVersion(unsigned major, unsigned minor=0, unsigned build=0)
See if operating system is later than the version specified.
void HouseKeeping()
This is an array collection class of PString objects.
Definition: pstring.h:2365
virtual void OnThreadEnded(PThread &thread)
Callback for when a thread is ended if wqas started in the PTLib system.
void SetCommand(const PString &key, const PString &command)
PCriticalSection m_threadMutex
Definition: pprocess.h:697
PBoolean SetMaxHandles(int newLimit)
Set the maximum number of file handles for the process.
virtual void HandleRunTimeSignal(const RunTimeSignalInfo &signalInfo)
Synchronous C run-time signal handler, this is executed in the housekeeper thread.
virtual PString GetConfigurationFile()
Get the default file to use in PConfig instances.
PSyncQueue< PThread * > m_autoDeleteThreads
Definition: pprocess.h:706
virtual const PString & GetManufacturer() const
Get the name of the manufacturer of the software.
static void Initialise(const PArgList &args, unsigned options=Timestamp|Thread|Blocks, const char *traceCount=PTRACE_ARG_TRACE, const char *outputFile=PTRACE_ARG_OUTPUT, const char *traceOpts=PTRACE_ARG_OPTION, const char *traceRollover=PTRACE_ARG_ROLLOVER, const char *traceLevel=PTRACE_ARG_LEVEL)
Set the most common trace options.
Comparison Compare(const PObject &obj) const
Compare two process instances.
Version information.
Definition: pprocess.h:563
Code is largely complete and is under test.
Definition: pprocess.h:116
This class represents an operating system process.
Definition: pprocess.h:106
Class to represent a directory in the operating system file system.
Definition: pdirect.h:173
VersionInfo m_version
Definition: pprocess.h:684
PProcessIdentifier m_processID
Definition: pprocess.h:718
void Startup()
Start up all items registered with PProcessStartupFactory.
int m_maxHandles
Definition: pprocess.h:689
#define PTRACE(...)
Output trace.
Definition: object.h:1039
Definition: pprocess.h:336
PString AsString(bool full=true) const
Build standard format string from version information.
int m_signal
Definition: pprocess.h:605
PString m_productName
Definition: pprocess.h:682
PTime GetStartTime() const
Return the time at which the program was started.
static PDirectory GetOSConfigDir()
Get the configuration directory of the operating system the process is running on, eg &quot;/etc&quot; for Unix, &quot;c:\windows&quot; for Win95 or &quot;c:\winnt\system32\drivers\etc&quot; for NT.
static PBoolean IsInitialised()
Determine if the current processes object instance has been initialised.
This class can be used to register various URL types with the host operating system so that URLs will...
Definition: pprocess.h:638
std::vector< RunTimeSignalInfo > m_RunTimeSignalsQueueBuffer
Definition: pprocess.h:725
PDirectory GetHomeDirectory() const
Get the &quot;home&quot; directory for the logged in user.
void PreInitialise(int argc, char **argv)
Internal initialisation function called directly from InternalMain().
Class for a process that is a dynamically loaded library.
Definition: pprocess.h:754
void InternalPostRunTimeSignal(int signal, PProcessIdentifier source)
bool PBoolean
Definition: object.h:174
PString GetUserName() const
Get the effective user name of the owner of the process, eg &quot;root&quot; etc.
The character string class.
Definition: pstring.h:108
const char * m_git
Definition: pprocess.h:571
int GetMaxHandles() const
Get the maximum file handle value for the process.
This class allows the parsing of a set of program arguments.
Definition: args.h:41
void PlatformConstruct()
static POrdinalToString::Initialiser const InternalSigNames[]
Definition: pprocess.h:722
PArgList m_arguments
Definition: pprocess.h:688
Definition: pprocess.h:604
PTime m_programStartTime
Definition: pprocess.h:691
PString m_manufacturer
Definition: pprocess.h:681
PSyncPoint m_signalHouseKeeper
Definition: pprocess.h:710
This class defines a thread of execution in the system.
Definition: thread.h:66
std::list< PSharedPtr< PExternalThread > > ThreadList
Definition: pprocess.h:704
static void PlatformResetRunTimeSignalHandler(int signal, PRunTimeSignalHandler previous)
unsigned m_major
Definition: pprocess.h:565
PPROFILE_EXCLUDE(bool GetProcessTimes(Times &times) const )
virtual void OnShutdown()
Definition: pprocess.h:791
PString GetCommand(const PString &key) const
PFilePath m_executableFile
Definition: pprocess.h:686
Class for a factory to create concrete class instances without parameters during construction.
Definition: pfactory.h:396
unsigned m_minor
Definition: pprocess.h:566
PBoolean SetUserName(const PString &username, PBoolean permanent=false)
Set the effective owner of the process.
size_t m_max
Definition: pprocess.h:347
virtual void AddRunTimeSignalHandlers(const int *signals=NULL)
Add all the C run-time signal handlers.
Definition: pprocess.h:786
size_t m_blocks
Definition: pprocess.h:349
size_t m_RunTimeSignalsQueueOut
Definition: pprocess.h:726
static PProcessIdentifier GetCurrentProcessID()
Get the platform dependent process identifier for the currentprocess.
const PFilePath & GetFile() const
Get the processes executable image file path.
bool IsMultipleInstance() const
Indicate if this is the second, or more, instance of this executable.
CodeStatus m_status
Definition: pprocess.h:567
PLibraryProcess(const char *manuf="", const char *name="", unsigned majorVersionNum=1, unsigned minorVersionNum=0, CodeStatus statusCode=ReleaseCode, unsigned buildNum=1, bool suppressStartup=false)
Create a new process instance.
Definition: pprocess.h:763
std::vector< PRunTimeSignalHandler > m_previousRunTimeSignalHandlers
Definition: pprocess.h:724
void PlatformDestruct()
atomic< bool > m_keepingHouse
Definition: pprocess.h:708
bool GetAllThreadIdentifiers(std::vector< PThreadIdentifier > &identifiers)
Return the operating system thread identifiers of all threads.
void GetMemoryUsage(MemoryUsage &usage) const
Get process memory suage.
Code has all known bugs removed and is shipping.
Definition: pprocess.h:117
PBoolean SetGroupName(const PString &groupname, PBoolean permanent=false)
Set the effective group of the process.
virtual void OnStartup()
Definition: pprocess.h:790
CodeStatus
Release status for the program.
Definition: pprocess.h:114
#define PDECLARE_NOTIFIER(notifierType, notifiee, func)
Declare PNotifier derived class with P_INT_PTR parameter. Uses PDECLARE_NOTIFIER_EXT macro...
Definition: notifier.h:202
PProcess(const char *manuf="", const char *name="", unsigned majorVersion=1, unsigned minorVersion=0, CodeStatus status=ReleaseCode, unsigned patchVersion=1, bool library=false, bool suppressStartup=false, unsigned oemVersion=0)
Create a new process instance.
Code has been updated after shipping.
Definition: pprocess.h:118
static bool RegisterTypes(const PString &types, bool force=true)
ThreadMap m_activeThreads
Definition: pprocess.h:700
HostSystemURLHandlerInfo(const PString &t)
Definition: pprocess.h:644
void SetConfigurationPath(const PString &path)
Set the default file or set of directories to search for use in PConfig.
virtual void RemoveRunTimeSignalHandlers()
Remove all the C run-time signal handlers.
static const char * GetRunTimeSignalName(int signal)
Get the name of the signal.
virtual void AsynchronousRunTimeSignal(int signal, PProcessIdentifier source)
Asynchronous C run-time signal handler, direct callback from OS.
virtual void OnThreadStart(PThread &thread)
Callback for when a thread is started by the PTLib system.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
PProcessIdentifier GetProcessID() const
Get the platform dependent process identifier for the process.
Definition: pprocess.h:317
PString type
Definition: pprocess.h:661
This class defines a thread synchronisation object.
Definition: syncpoint.h:63
unsigned m_svn
Definition: pprocess.h:570
PThread * GetThread(PThreadIdentifier threadId) const
Return the thread for the operating system thread identifier.
virtual PString GetVersion(PBoolean full=true) const
Get the version of the software.
PThread * m_houseKeeper
Definition: pprocess.h:709
int GetTerminationValue() const
Get the termination value for the process.
Structure for static array initialiser for class.
Definition: pstring.h:3053
size_t m_current
Definition: pprocess.h:348
virtual void PrintOn(ostream &strm) const
This will print out performance indicators for the process.