github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/lib9/await.c (about) 1 // +build !windows 2 3 /* 4 Plan 9 from User Space src/lib9/await.c 5 http://code.swtch.com/plan9port/src/tip/src/lib9/await.c 6 7 Copyright 2001-2007 Russ Cox. All Rights Reserved. 8 Portions Copyright 2009 The Go Authors. All Rights Reserved. 9 10 Permission is hereby granted, free of charge, to any person obtaining a copy 11 of this software and associated documentation files (the "Software"), to deal 12 in the Software without restriction, including without limitation the rights 13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 copies of the Software, and to permit persons to whom the Software is 15 furnished to do so, subject to the following conditions: 16 17 The above copyright notice and this permission notice shall be included in 18 all copies or substantial portions of the Software. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 THE SOFTWARE. 27 */ 28 29 #define NOPLAN9DEFINES 30 #include <u.h> 31 #include <libc.h> 32 33 #include <signal.h> 34 #include <sys/types.h> 35 #include <sys/wait.h> 36 #include <sys/time.h> 37 #include <sys/resource.h> 38 39 #ifndef WCOREDUMP /* not on Mac OS X Tiger */ 40 #define WCOREDUMP(status) 0 41 #endif 42 43 static struct { 44 int sig; 45 char *str; 46 } tab[] = { 47 SIGHUP, "hangup", 48 SIGINT, "interrupt", 49 SIGQUIT, "quit", 50 SIGILL, "sys: illegal instruction", 51 SIGTRAP, "sys: breakpoint", 52 SIGABRT, "sys: abort", 53 #ifdef SIGEMT 54 SIGEMT, "sys: emulate instruction executed", 55 #endif 56 SIGFPE, "sys: fp: trap", 57 SIGKILL, "sys: kill", 58 SIGBUS, "sys: bus error", 59 SIGSEGV, "sys: segmentation violation", 60 SIGALRM, "alarm", 61 SIGTERM, "kill", 62 SIGURG, "sys: urgent condition on socket", 63 SIGSTOP, "sys: stop", 64 SIGTSTP, "sys: tstp", 65 SIGCONT, "sys: cont", 66 SIGCHLD, "sys: child", 67 SIGTTIN, "sys: ttin", 68 SIGTTOU, "sys: ttou", 69 #ifdef SIGIO /* not on Mac OS X Tiger */ 70 SIGIO, "sys: i/o possible on fd", 71 #endif 72 SIGXCPU, "sys: cpu time limit exceeded", 73 SIGXFSZ, "sys: file size limit exceeded", 74 SIGVTALRM, "sys: virtual time alarm", 75 SIGPROF, "sys: profiling timer alarm", 76 #ifdef SIGWINCH /* not on Mac OS X Tiger */ 77 SIGWINCH, "sys: window size change", 78 #endif 79 #ifdef SIGINFO 80 SIGINFO, "sys: status request", 81 #endif 82 SIGUSR1, "sys: usr1", 83 SIGUSR2, "sys: usr2", 84 SIGPIPE, "sys: write on closed pipe", 85 }; 86 87 char* 88 _p9sigstr(int sig, char *tmp) 89 { 90 int i; 91 92 for(i=0; i<nelem(tab); i++) 93 if(tab[i].sig == sig) 94 return tab[i].str; 95 if(tmp == nil) 96 return nil; 97 sprint(tmp, "sys: signal %d", sig); 98 return tmp; 99 } 100 101 int 102 _p9strsig(char *s) 103 { 104 int i; 105 106 for(i=0; i<nelem(tab); i++) 107 if(strcmp(s, tab[i].str) == 0) 108 return tab[i].sig; 109 return 0; 110 } 111 112 static Waitmsg* 113 _wait(int pid4, int opt) 114 { 115 int pid, status, cd; 116 struct rusage ru; 117 char tmp[64]; 118 ulong u, s; 119 Waitmsg *w; 120 121 w = malloc(sizeof *w + 200); 122 if(w == nil) 123 return nil; 124 memset(w, 0, sizeof *w); 125 w->msg = (char*)&w[1]; 126 127 for(;;){ 128 /* On Linux, pid==-1 means anyone; on SunOS, it's pid==0. */ 129 if(pid4 == -1) 130 pid = wait3(&status, opt, &ru); 131 else 132 pid = wait4(pid4, &status, opt, &ru); 133 if(pid <= 0) { 134 free(w); 135 return nil; 136 } 137 u = (ulong)(ru.ru_utime.tv_sec*1000+((ru.ru_utime.tv_usec+500)/1000)); 138 s = (ulong)(ru.ru_stime.tv_sec*1000+((ru.ru_stime.tv_usec+500)/1000)); 139 w->pid = pid; 140 w->time[0] = u; 141 w->time[1] = s; 142 w->time[2] = u+s; 143 if(WIFEXITED(status)){ 144 if(status) 145 sprint(w->msg, "%d", status); 146 return w; 147 } 148 if(WIFSIGNALED(status)){ 149 cd = WCOREDUMP(status); 150 sprint(w->msg, "signal: %s", _p9sigstr(WTERMSIG(status), tmp)); 151 if(cd) 152 strcat(w->msg, " (core dumped)"); 153 return w; 154 } 155 } 156 } 157 158 Waitmsg* 159 p9wait(void) 160 { 161 return _wait(-1, 0); 162 } 163 164 Waitmsg* 165 p9waitfor(int pid) 166 { 167 return _wait(pid, 0); 168 } 169 170 Waitmsg* 171 p9waitnohang(void) 172 { 173 return _wait(-1, WNOHANG); 174 } 175 176 int 177 p9waitpid(void) 178 { 179 int status; 180 return wait(&status); 181 }