00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Mark Elendt 00008 * Side Effects Software Inc. 00009 * 20 Maud St. 00010 * Toronto, Ontario, M5V 2M5 00011 * Canada 00012 * 416-504-9876 00013 * 00014 * NAME: Action (C++) 00015 * 00016 * COMMENTS: 00017 * 00018 */ 00019 00020 #ifndef __UT_Spawn_h__ 00021 #define __UT_Spawn_h__ 00022 00023 #include "UT_API.h" 00024 #include <SYS/SYS_Types.h> 00025 #include "UT_SysClone.h" 00026 00027 #define INVALID_PID (pid_t)-1 00028 00029 typedef void (*UTsysWaitLoop)(int state); 00030 00031 // The wait loop class is used when starting up processes which may block 00032 // the main thread. This mechanism allows the main thread to do some 00033 // processing while the foreground system process (which is really run in 00034 // the background) is still active. 00035 // The wait loop will be called with 0==entering loop, 1==polling, 2==exit 00036 class UT_API UT_SystemWaitLoop { 00037 public: 00038 static void setWaitLoop(UTsysWaitLoop callback); 00039 00040 static void enterWaitLoop(); 00041 static void pollWaitLoop(); 00042 static void exitWaitLoop(); 00043 }; 00044 00045 inline bool UTisValidPid( pid_t pid ) 00046 { 00047 return pid >= 0; 00048 } 00049 00050 // 00051 // These first two basically fork and exec the command given as either 00052 // an array of arguments or as a single string. The second invokes the 00053 // first. The first uses fork and then execvp. If detachConsole is set 00054 // then the child process will have it's stdout and stderr closed. 00055 // 00056 UT_API extern pid_t UTspawnvp(const char *const* args, 00057 int detachConsole = 0, 00058 bool ignore_output = false); 00059 UT_API extern pid_t UTspawn(const char *string, 00060 int detachConsole = 0, 00061 bool ignore_output = false); 00062 00063 // Wait for a spawned process and return the exit code 00064 UT_API extern int UTwait(pid_t pid); 00065 00066 // Return 1 if the specified *CHILD* process is still running 00067 // NOTE: THIS ONLY WORKS FOR PROCESSES OF THE SAME USER ON WINDOWS! 00068 UT_API extern int UTisRunning(pid_t pid, int *exit_code); 00069 00070 // UTisRunning() does a wait, implying that the process in question is a child 00071 // process. However, on occasion, we want a general form to determine whether 00072 // any process (child or not) is still running. 00073 // NOTE: THIS ONLY WORKS FOR PROCESSES OF THE SAME USER ON WINDOWS! 00074 UT_API extern int UTisAnyProcessRunning(pid_t pid); 00075 00076 // UTcheckProcessId() returns true if the given pid corresponds to an existing 00077 // process on the current system. This will work for any process regardless 00078 // of whether it is owned by the current user. 00079 UT_API extern bool UTcheckProcessId(pid_t pid); 00080 00081 // 00082 // UTbackground will put the current process into the background and 00083 // disassociate it from the controlling terminal. This will protect 00084 // the process from signals generated from the tty. Setting the 00085 // full_daemon flag means that you really want this process to be 00086 // stand-alone. This causes the function to put error messages into 00087 // the SYSLOG file instead of to stderr and it will close all open 00088 // files, cd to the root of the file system and set the process 00089 // umask to zero. It also logs a successful startup into the SYSLOG. 00090 // The name argument is used for reporting purposes and must be 00091 // supplied. 00092 // 00093 UT_API extern pid_t UTbackground(const char *name, int full_daemon = 0); 00094 00095 // 00096 // UTforeground will force the application to have a console where printf 00097 // messages will be output. Killing this console will kill the application as 00098 // well. The name argument will be the title of the console. 00099 UT_API extern void UTforeground(const char *name = "Console"); 00100 00101 // 00102 // Kills a process (using SIGKILL on Irix/Linux, TerminateProcess on NT. 00103 // The NT version also closes the process handle. 00104 UT_API extern void UTkill(pid_t pid); 00105 00106 // 00107 // Kills a process (using SIGTERM on Irix/Linux, TerminateProcess on NT. 00108 // The NT version also closes the process handle. 00109 UT_API extern void UTterminate(pid_t pid); 00110 00111 #endif
1.5.9