00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #ifndef _UXPROCESS_HPP_ 00019 #define _UXPROCESS_HPP_ 00020 00021 #include "ux.hpp" 00022 00023 #include <sys/uio.h> 00024 #include <utime.h> 00025 #include <sys/times.h> 00026 00027 #ifndef _XOPEN_UNIX 00028 typedef int idtype_t; 00029 typedef int id_t; 00030 typedef unsigned int useconds_t; 00031 #endif 00032 00033 namespace Ux { 00034 00035 /** 00036 \ingroup Ux 00037 Class for process-related functions. Most operate only on the process from which 00038 they're called, so are static because it would be misleading to suggest that 00039 they can operate on an arbitrary Process. (That is, the underlying function doesn't 00040 take a pid_t argument.) 00041 */ 00042 class Process : public Base { 00043 protected: 00044 pid_t pid; 00045 00046 public: 00047 Process(pid_t id = -1) 00048 : pid(id == -1 ? ::getpid() : id) 00049 { } 00050 void set(pid_t id) 00051 { pid = id; } 00052 operator pid_t() 00053 { return pid; } 00054 00055 /** 00056 Calls ::abort. 00057 */ 00058 static void abort(void) 00059 { ::abort(); } 00060 static void atexit(void (*fcn)(void)); 00061 static void chdir(const char *path); 00062 static void chdir(int fd); 00063 static void chroot(const char *path); 00064 static clock_t clock(void); 00065 static void execlp(const char *file, const char *arg0, ...); 00066 // execvpe member functions not thread safe because of setting of environ 00067 static void execvpe(const char *file, char *const argv[], char *const *envv = NULL); 00068 /** 00069 Calls ::_exit. 00070 */ 00071 static void _exit(int status = EXIT_SUCCESS) 00072 { ::_exit(status); } 00073 /** 00074 Calls ::exit. 00075 */ 00076 static void exit(int status = EXIT_SUCCESS) 00077 { ::exit(status); } 00078 static Process fork(void); 00079 static void getcwd(Dir& d); 00080 /** 00081 Calls ::getegid. 00082 */ 00083 static gid_t getegid(void) 00084 { return ::getegid(); } 00085 static char* getenv(const char *var); 00086 /** 00087 Calls ::geteuid. 00088 */ 00089 static uid_t geteuid(void) 00090 { return ::geteuid(); } 00091 /** 00092 Calls ::getgid. 00093 */ 00094 static gid_t getgid(void) 00095 { return ::getgid(); } 00096 /** 00097 Calls ::getpid. 00098 */ 00099 static pid_t getpid(void) 00100 { return ::getpid(); } 00101 static pid_t getpgid(pid_t pid); 00102 /** 00103 Calls ::getppid. 00104 */ 00105 static pid_t getppid(void) 00106 { return ::getppid(); } 00107 static void getrlimit(int resource, struct rlimit *rlp); 00108 static void getrusage(int who, struct rusage *r_usage); 00109 pid_t getsid(void); 00110 /** 00111 Calls ::getuid. 00112 */ 00113 static uid_t getuid(void) 00114 { return ::getuid(); } 00115 static void kill(pid_t pid, int signum); 00116 /** 00117 Calls ::kill. 00118 */ 00119 void kill(int signum) 00120 { return kill(pid, signum); } 00121 static void nice(int incr); 00122 static void pause(void); 00123 static void putenv(char *string); 00124 static void setegid(gid_t gid); 00125 static void setenv(const char *var, const char *val, int overwrite); 00126 static void seteuid(uid_t uid); 00127 static void setgid(gid_t gid); 00128 static void setpgid(pid_t pid, pid_t pgid); 00129 static void setrlimit(int resource, const struct rlimit *rlp); 00130 static pid_t setsid(void); 00131 static void setuid(uid_t uid); 00132 static void sigaction(int signum, const struct sigaction *act, struct sigaction *oact = NULL); 00133 static void sigaltstack(const stack_t *stack, stack_t *ostack = NULL); 00134 static void siginterrupt(int signum, int on = true); 00135 static void sigprocmask(int how, const sigset_t *set, sigset_t *oset = NULL); 00136 void sigqueue(int signum, const union sigval value); 00137 static void sigsuspend(const sigset_t *sigmask); 00138 static int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *ts); 00139 static void sigwait(const sigset_t *set, int *signum); 00140 static int sigwaitinfo(const sigset_t *set, siginfo_t *info = NULL); 00141 00142 static int system(const char *command); 00143 static clock_t times(struct tms *buffer); 00144 /** 00145 Calls ::umask. 00146 */ 00147 static mode_t umask(mode_t cmask) 00148 { return ::umask(cmask); } 00149 static void unsetenv(const char *var); 00150 /** 00151 Calls ::wait. 00152 */ 00153 static pid_t wait(ExitStatus *statusp = NULL) 00154 { return Process::waitpid(-1, statusp); } 00155 static void waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options = 0); 00156 static pid_t waitpid(pid_t pid, ExitStatus *statusp = NULL, int options = 0); 00157 /** 00158 Calls ::waitpid. The static version takes a pid_t argument in case you want to wait 00159 for other than the calling process. 00160 */ 00161 pid_t waitpid(ExitStatus *statusp = NULL, int options = 0) 00162 { return Process::waitpid(pid, statusp, options); } 00163 }; 00164 00165 } // namespace 00166 00167 #endif // _UXPROCESS_HPP_