sipxportlib  Version 3.3
OsProcessMgr.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _OsProcessMgr_h_
13 #define _OsProcessMgr_h_
14 
15 // SYSTEM INCLUDES
16 
17 // APPLICATION INCLUDES
18 #include "os/OsDefs.h"
19 #include "os/OsStatus.h"
20 #include "os/OsFS.h"
21 #include "os/OsProcess.h"
22 #include "os/OsProcessIterator.h"
23 #include "os/OsMutex.h"
24 
25 //to turn on debug string uncomment the next line
26 //#define DEBUG_OUTPUT
27 
28 // DEFINES
29 
30 #define PROCESS_ALIAS_FILE "processAlias.dat"
31 #define PROCESS_ALIAS_LOCK_FILE "locked.lck"
32 // MACROS
33 // EXTERNAL FUNCTIONS
34 // EXTERNAL VARIABLES
35 // CONSTANTS
36  const int PROCESS_STARTED = 1;
37  const int PROCESS_STOPPED = 2;
38  const int PROCESS_FAILED = 3;
39  const int PROCESS_NEVERRUN = 4;
40  const int PROCESS_STOPPING = 5;
41  const int PROCESS_STARTING = 6;
42 
43 // User requested states
44  const int USER_PROCESS_NONE = 0;
45  const int USER_PROCESS_START = 1;
46  const int USER_PROCESS_STOP = 2;
47  const int USER_PROCESS_RESTART = 3;
48 
49 // STRUCTS
50 // TYPEDEFS
51 
52 // FORWARD DECLARATIONS
53 
54 //: This encapsulates a pid, and allows querying, killing and all the
55 //: other cool things you want to do to a process.
56 
58 {
59  friend class MonitoredProcess;
60 
61 /* //////////////////////////// PUBLIC //////////////////////////////////// */
62 public:
63 
64 /* ============================ CREATORS ================================== */
65  OsProcessMgr(const char* workingDirectory);
66 
67  //:Default constructor
68 
69  virtual ~OsProcessMgr();
70  //:Destructor
71 
72 /* ============================ MANIPULATORS ============================== */
73  OsStatus startProcess(UtlString &rAlias, UtlString &rExeName, UtlString rParameters[], UtlString &startupDir,
75  UtlBoolean bExeclusive = FALSE);
76  //: Start process
77 
78  OsStatus setIORedirect(OsPath &rStdInputFilename, OsPath &rStdOutputFilename, OsPath &rStdErrorFilename);
79  //: Sets the standard input, output and/or stderror
80  //: Applies to all processes created from this point on
81  //: Set them to "" to provide the defaul action.
82 
84  //: Stop process
85 
87  //: Stop process by id
88 
89  void setProcessListFilename(UtlString &rFilename);
90  //: Overrides the init file of processAlias.dat
91  //: Working directory will be prepended.
92 
93  void setAliasStopped(UtlString &rAlias);
94 
95  OsStatus setUserRequestState(UtlString &rAlias, int userRequestedState);
96  //: Sets a state which watchdog or another program can use to determine
97  //: if a user wishes to change the state of a process.
98  //: ProcessMgr directly does not change the state of the process.
99  //: This function is used to allow users to set a new state (as the third param)
100  //: in processAlias.dat. It is up to an external program to read this
101  //: state via getUserRequestState and change the process state.
102 
103 /* ============================ ACCESSORS ================================= */
104  int getUserRequestState(UtlString &rAlias);
105  //: Gets the state which a user set when they wish to change the state of a process.
106  //: ProcessMgr directly does not change the state of the process.
107  //: This function is used to allow users to set a new state (as the third param)
108  //: in processAlias.dat. It is up to an external program to read this
109  //: state and change the process state.
110 
111  OsStatus getProcessByAlias(UtlString &rAlias, OsProcess &rProcess);
112  //: Retrieve process object given ID.
113 
114  OsStatus getAliasByPID(PID pid ,UtlString &rAlias);
115  //: Retrieves the alias if you know the pid
116  //: Returns OS_SUCCESS if found, or OS_FAILED if....failed.
117 
118  static OsProcessMgr *getInstance(const char* workingDirectory);
119  //: returns the one and only process manager
120 
121 
122 /* ============================ INQUIRY =================================== */
123  UtlBoolean isStarted(UtlString &rAlias);
124 
125  int getAliasState(UtlString &rAlias);
126  //: Return the state of the alias
127 
128  void lockAliasFile();
129  void unlockAliasFile();
130 
131 /* //////////////////////////// PROTECTED ///////////////////////////////// */
132 protected:
133 
134  OsStatus setAliasState(UtlString &rAlias,int state);
135 
136 
137 /* //////////////////////////// PRIVATE /////////////////////////////////// */
138 private:
139  void getAliasFirstValue(UtlString &rinValue);
140  //: helper func to pull the first part of the two part value
141 
142  void getAliasSecondValue(UtlString &rinValue);
143  //: helper func to pull the second part of the two part value
144 
145  static OsProcessMgr * spManager;
146  //: pointer to the one and only process manager
147 
148  OsPath mProcessFilename;
149  //: Name of file which store the alias and PID
150 
151  OsPath mProcessLockFilename;
152  //: Simple Lock file for cross process locking
153 
154  OsPath mWorkPath;
155  //:Where files will be stored
156  //: Defaults to ProcessMgr
157 
158  OsPath mStdInputFilename;
159  //: Where will input come from?
160 
161  OsPath mStdOutputFilename;
162  //: Where will output go to?
163 
164  OsPath mStdErrorFilename;
165  //: Where will errors be sent?
166 
167  OsConfigDb *pProcessList;
168  // Internal list of alias and PID's
169 
170  OsStatus addEntry(UtlString &rAlias, int pid);
171  //: Add an entry to our process file
172  //: Returns OS_SUCCESS if added ok
173  //: or OS_FAILED on failure
174 
175  OsStatus removeEntry(UtlString &rAlias);
176  //: Remove the entry from the file
177  //: Returns OS_SUCCESS if removed
178  //: or OS_FAILED on failure
179 
180  OsStatus loadProcessFile();
181  //: Returns OS_SUCCESS if saved ok.
182  //: or OS_FAILED on failure
183 
184  OsStatus storeProcessFile();
185  //: Returns OS_SUCCESS if saved ok.
186  //: or OS_FAILED on failure
187 
188  int mAliasLockFileCount;
189 
191  OsMutex mMutex;
192 
193 };
194 
195 /* ============================ INLINE METHODS ============================ */
196 
197 
198 #endif // _OsProcessMgr_h_
199 
200 
const int PROCESS_STOPPING
Definition: OsProcessMgr.h:40
const int USER_PROCESS_STOP
Definition: OsProcessMgr.h:46
void setProcessListFilename(UtlString &rFilename)
Definition: OsProcessMgr.cpp:152
friend class MonitoredProcess
Definition: OsProcessMgr.h:59
const int PROCESS_NEVERRUN
Definition: OsProcessMgr.h:39
const int PROCESS_STARTING
Definition: OsProcessMgr.h:41
Definition: OsProcessMgr.h:57
OsProcessMgr(const char *workingDirectory)
Definition: OsProcessMgr.cpp:46
const int USER_PROCESS_NONE
Definition: OsProcessMgr.h:44
static OsProcessMgr * getInstance(const char *workingDirectory)
Definition: OsProcessMgr.cpp:474
OsStatus
Definition: OsStatus.h:27
OsStatus getProcessByAlias(UtlString &rAlias, OsProcess &rProcess)
Definition: OsProcessMgr.cpp:333
int PID
Definition: OsProcess.h:31
OsProcessPriorityClass
Definition: OsProcess.h:70
OsStatus setIORedirect(OsPath &rStdInputFilename, OsPath &rStdOutputFilename, OsPath &rStdErrorFilename)
Definition: OsProcessMgr.cpp:157
virtual ~OsProcessMgr()
Definition: OsProcessMgr.cpp:70
int getUserRequestState(UtlString &rAlias)
Definition: OsProcessMgr.cpp:484
OsStatus setUserRequestState(UtlString &rAlias, int userRequestedState)
Definition: OsProcessMgr.cpp:83
UtlBoolean isStarted(UtlString &rAlias)
Definition: OsProcessMgr.cpp:536
Definition: OsProcess.h:72
Definition: UtlString.h:48
OsStatus stopProcess(UtlString &rAlias)
Definition: OsProcessMgr.cpp:202
Definition: OsConfigDb.h:67
const int USER_PROCESS_START
Definition: OsProcessMgr.h:45
const int PROCESS_STARTED
Definition: OsProcessMgr.h:36
void setAliasStopped(UtlString &rAlias)
Definition: OsProcessMgr.cpp:464
const int PROCESS_STOPPED
Definition: OsProcessMgr.h:37
void lockAliasFile()
Definition: OsProcessMgr.cpp:692
OsStatus getAliasByPID(PID pid, UtlString &rAlias)
Definition: OsProcessMgr.cpp:288
const int PROCESS_FAILED
Definition: OsProcessMgr.h:38
int UtlBoolean
Definition: UtlDefs.h:41
OsStatus setAliasState(UtlString &rAlias, int state)
Definition: OsProcessMgr.cpp:581
void unlockAliasFile()
Definition: OsProcessMgr.cpp:755
#define FALSE
Definition: UtlDefs.h:21
OsStatus startProcess(UtlString &rAlias, UtlString &rExeName, UtlString rParameters[], UtlString &startupDir, OsProcessBase::OsProcessPriorityClass prio=OsProcessBase::NormalPriorityClass, UtlBoolean bExeclusive=FALSE)
Definition: OsProcessMgr.cpp:169
int getAliasState(UtlString &rAlias)
Definition: OsProcessMgr.cpp:388
const int USER_PROCESS_RESTART
Definition: OsProcessMgr.h:47