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