blob: e1115752d019278071a9f929fc84d84a587d1c3c [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39/**
40 * @file commoncpp/process.h
41 * @short Process services.
42 **/
43
44#ifndef COMMONCPP_PROCESS_H_
45#define COMMONCPP_PROCESS_H_
46
47#ifndef COMMONCPP_CONFIG_H_
48#include <commoncpp/config.h>
49#endif
50
51#ifndef COMMONCPP_THREAD_H_
52#include <commoncpp/thread.h>
53#endif
54
55NAMESPACE_COMMONCPP
56
57/**
58 * A class for containing portable process related functions
59 * that help create portable code. These are typically
60 * referenced thru Process::xxx static member functions.
61 * Many of these members are used both for win32 and posix
62 * systems although some may be platform specific.
63 *
64 * @short Peocess wrapper class.
65 * @author David Sugar <dyfet@ostel.com>
66 */
67class __EXPORT Process
68{
69private:
70 static bool rtflag;
71
72public:
73#ifndef _MSWINDOWS_
74 typedef void (*Trap)(int);
75
76 /**
77 * Detach current process into a daemon, posix
78 * only. Perhaps a similar method can be used
79 * for creating win32 "services"?
80 */
81 static void detach(void);
82
83 /**
84 * Attach the current process to another device
85 * or i/o session. It is deamonified and dissasociated
86 * with the prior parent process and controlling terminal.
87 *
88 * @param devname path to attach to.
89 */
90 static void attach(const char *devname);
91
92 /**
93 * Set a posix compliant signal handler.
94 *
95 * @return previous handler.
96 * @param signo signal no.
97 * @param handler trap handler.
98 */
99 static Trap setPosixSignal(int signo, Trap handler);
100
101 /**
102 * Set system call interuptable signal handler.
103 *
104 * #return previous handler.
105 * @param signo signal no.
106 * @param handler trap handler.
107 */
108 static Trap setInterruptSignal(int signo, Trap handler);
109#endif
110 /**
111 * Lock a process in memory. Ideally you should be deep enough
112 * where additional memallocs for functions will not kill you,
113 * or use false for future.
114 *
115 * @return true if successful.
116 * @param future pages as well...
117 */
118 bool lock(bool future = true);
119
120 /**
121 * Unlock process pages.
122 */
123 void unlock(void);
124
125 /**
126 * Spawn a process and wait for it's exit code. In win32
127 * this is done with the spawn system call. In posix,
128 * this is done with a fork, an execvp, and a waitpid.
129 *
130 * @warning The implementation differences between posix and
131 * win32 systems may cause side effects. For instance, if you
132 * use atexit() and this spawn method, on posix systems the
133 * function set up with atexit() will be called when the
134 * parent process of the fork exits, which will not happen on
135 * Win32 systems.
136 *
137 * @return error code from process.
138 * @param exec name of executable.
139 * @param argv list of command arguments.
140 * @param wait for process to exit before return.
141 */
142 static int spawn(const char *exec, const char **argv, bool wait = true);
143
144 /**
145 * Get the exit status of another process, waiting for it
146 * to exit.
147 *
148 * @return exit code from process.
149 * @param pid process id.
150 */
151 static int join(int pid);
152
153 /**
154 * Cancel a running child process.
155 *
156 * @return 0 on success.
157 * @param pid process id.
158 * @param sig cancel signal to apply.
159 */
160 static bool cancel(int pid, int sig = 0);
161
162 /**
163 * Get system environment.
164 *
165 * @return system environ symbol.
166 * @param name of symbol.
167 */
168 static const char *getEnv(const char *name);
169
170 /**
171 * Set system environment in a standard manner.
172 *
173 * @param name of environment symbol to set.
174 * @param value of environment symbol.
175 * @param overwrite true if replace existing symbol.
176 */
177 static void setEnv(const char *name, const char *value, bool overwrite);
178
179 /**
180 * Get etc prefix path.
181 *
182 * @return etc prefix.
183 */
184 static const char *getConfigDir(void);
185
186 /**
187 * Get home directory.
188 *
189 * @return user home directory.
190 */
191 static const char *getHomeDir(void);
192
193 /**
194 * Get user name.
195 *
196 * @return user login id.
197 */
198 static const char *getUser(void);
199
200 /**
201 * Set user id by name.
202 *
203 * @return true if successful.
204 */
205 static bool setUser(const char *id, bool grp = true);
206
207 /**
208 * Set the effective group id by name.
209 *
210 * @return true if successful.
211 */
212 static bool setGroup(const char *id);
213
214 /**
215 * Return the effective operating system page size.
216 *
217 * @return system page size.
218 */
219 static size_t getPageSize(void);
220
221 /**
222 * Used to set process priority and optionally enable realtime.
223 */
224 static void setPriority(int pri);
225
226 /**
227 * Used to set process scheduling policy.
228 */
229 static void setScheduler(const char *policy);
230
231 /**
232 * Portable shortcut for setting realtime...
233 */
234 static void setRealtime(int pri = 0);
235
236 /**
237 * Return true if scheduler settable.
238 */
239 static bool isScheduler(void);
240
241 /**
242 * Return true if realtime scheduling.
243 */
244 static inline bool isRealtime(void)
245 {return rtflag;};
246};
247
248/**
249 * This class is used to create a "named" lock entity that can be used
250 * to control access to a resource between multiple processes. The
251 * posix implimentation uses a pidfile and the win32 version uses a
252 * globally visible mutex.
253 *
254 * @author David Sugar <dyfet@ostel.com>
255 * @short System-wide named lock
256 */
257class __EXPORT Lockfile
258{
259private:
260#ifdef _MSWINDOWS_
261 HANDLE _mutex;
262 bool _flagged;
263#else
264 char *_path;
265#endif
266
267public:
268 /**
269 * Create a lock under a known name.
270 *
271 * @param name of system-wide lock to create.
272 */
273 Lockfile(const char *name);
274
275 /**
276 * Create a new lock object that can be used to make locks.
277 */
278 Lockfile();
279
280 /**
281 * Destroy the current lock and release it.
282 */
283 ~Lockfile()
284 {unlock();};
285
286 /**
287 * Lock a system-wide name for this process. If the lock
288 * is successful, return true. If an existing lock was
289 * already acquired, release it first.
290 *
291 * @return true if lock successful.
292 * @param name system-wide lock to use.
293 */
294 bool lock(const char *name);
295
296 /**
297 * Release an acquired lock.
298 */
299 void unlock(void);
300
301 /**
302 * Flag if the current process has aqcuired a lock.
303 *
304 * @return true if we have the lock.
305 */
306 bool isLocked(void);
307};
308
309END_NAMESPACE
310
311#endif