HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Spawn.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Action (C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_Spawn_h__
13 #define __UT_Spawn_h__
14 
15 #include "UT_API.h"
16 #include <SYS/SYS_Types.h>
17 #include "UT_SysClone.h"
18 
19 #define INVALID_PID (pid_t)-1
20 
21 typedef void (*UTsysWaitLoop)(int state);
22 
23 // The wait loop class is used when starting up processes which may block
24 // the main thread. This mechanism allows the main thread to do some
25 // processing while the foreground system process (which is really run in
26 // the background) is still active.
27 // The wait loop will be called with 0==entering loop, 1==polling, 2==exit
29 public:
30  static void setWaitLoop(UTsysWaitLoop callback);
31 
32  static void enterWaitLoop();
33  static void pollWaitLoop();
34  static void exitWaitLoop();
35 };
36 
37 inline bool UTisValidPid( pid_t pid )
38 {
39  return pid >= 0;
40 }
41 
42 /// Set process group ID for a given pid. With pid==0 and gpid==0 this will
43 /// set a new process group for the current process.
44 /// @return 0 if successful or -1 on error.
45 UT_API extern int UTchangeProcessGroup(pid_t pid, pid_t gpid);
46 
47 //
48 // These first two basically fork and exec the command given as either
49 // an array of arguments or as a single string. The second invokes the
50 // first. The first uses fork and then execvp. If detachConsole is set
51 // then the child process will have it's stdout and stderr closed.
52 //
53 UT_API extern pid_t UTspawnvp(const char *const* args,
54  int detachConsole = 0,
55  bool ignore_output = false,
56  bool change_process_group = false);
57 UT_API extern pid_t UTspawn(const char *string,
58  int detachConsole = 0,
59  bool ignore_output = false,
60  bool change_process_group = false);
61 
62 // Wait for a spawned process and return the exit code
63 UT_API extern int UTwait(pid_t pid);
64 
65 /// Wait for a spawned process to exit. If the wait system call is
66 /// interrupted, the exit status will not be written and the function
67 /// will return false.
68 UT_API extern bool UTwait(pid_t pid, int &exit_code);
69 
70 // Return 1 if the specified *CHILD* process is still running
71 // NOTE: THIS ONLY WORKS FOR PROCESSES OF THE SAME USER ON WINDOWS!
72 UT_API extern int UTisRunning(pid_t pid, int *exit_code);
73 
74 // UTisRunning() does a wait, implying that the process in question is a child
75 // process. However, on occasion, we want a general form to determine whether
76 // any process (child or not) is still running.
77 // NOTE: THIS ONLY WORKS FOR PROCESSES OF THE SAME USER ON WINDOWS!
78 UT_API extern int UTisAnyProcessRunning(pid_t pid);
79 
80 // UTcheckProcessId() returns true if the given pid corresponds to an existing
81 // process on the current system. This will work for any process regardless
82 // of whether it is owned by the current user.
83 UT_API extern bool UTcheckProcessId(pid_t pid);
84 
85 //
86 // UTbackground will put the current process into the background and
87 // disassociate it from the controlling terminal. This will protect
88 // the process from signals generated from the tty. Setting the
89 // full_daemon flag means that you really want this process to be
90 // stand-alone. This causes the function to put error messages into
91 // the SYSLOG file instead of to stderr and it will close all open
92 // files, cd to the root of the file system and set the process
93 // umask to zero. It also logs a successful startup into the SYSLOG.
94 // The name argument is used for reporting purposes and must be
95 // supplied.
96 //
97 UT_API extern pid_t UTbackground(const char *name, int full_daemon = 0);
98 
99 //
100 // UTforeground will force the application to have a console where printf
101 // messages will be output. Killing this console will kill the application as
102 // well. The name argument will be the title of the console.
103 UT_API extern void UTforeground(const char *name = "Console");
104 
105 /// Kill a process (using SIGKILL on Irix/Linux, TerminateProcess on NT).
106 /// The NT version also closes the process handle.
107 UT_API extern void UTkill(pid_t pid, bool kill_tree=false);
108 
109 /// Kill a process (using SIGKILL on Irix/Linux, TerminateProcess on NT),
110 /// attempting to do a stack trace on the (parent) process to stderr.
111 /// The NT version also closes the process handle
112 UT_API extern void UTkill(pid_t pid, bool kill_tree, bool dump_stack);
113 
114 /// Kills a process (using SIGTERM on Irix/Linux, TerminateProcess on NT)
115 /// The NT version also closes the process handle.
116 UT_API extern void UTterminate(pid_t pid, bool terminate_tree=false);
117 
118 #endif
void(* UTsysWaitLoop)(int state)
Definition: UT_Spawn.h:21
UT_API int UTisRunning(pid_t pid, int *exit_code)
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
#define UT_API
Definition: UT_API.h:12
UT_API bool UTcheckProcessId(pid_t pid)
bool UTisValidPid(pid_t pid)
Definition: UT_Spawn.h:37
UT_API pid_t UTbackground(const char *name, int full_daemon=0)
UT_API int UTchangeProcessGroup(pid_t pid, pid_t gpid)
UT_API int UTwait(pid_t pid)
UT_API void UTforeground(const char *name="Console")
GLuint const GLchar * name
Definition: glcorearb.h:785
UT_API void UTterminate(pid_t pid, bool terminate_tree=false)
UT_API int UTisAnyProcessRunning(pid_t pid)
UT_API pid_t UTspawn(const char *string, int detachConsole=0, bool ignore_output=false, bool change_process_group=false)
UT_API void UTkill(pid_t pid, bool kill_tree=false)
UT_API pid_t UTspawnvp(const char *const *args, int detachConsole=0, bool ignore_output=false, bool change_process_group=false)