2
Plan 9 from User Space src/lib9/await.c
3
http://code.swtch.com/plan9port/src/tip/src/lib9/await.c
5
Copyright 2001-2007 Russ Cox. All Rights Reserved.
6
Portions Copyright 2009 The Go Authors. All Rights Reserved.
8
Permission is hereby granted, free of charge, to any person obtaining a copy
9
of this software and associated documentation files (the "Software"), to deal
10
in the Software without restriction, including without limitation the rights
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
copies of the Software, and to permit persons to whom the Software is
13
furnished to do so, subject to the following conditions:
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
#define NOPLAN9DEFINES
32
#include <sys/types.h>
35
#include <sys/resource.h>
37
#ifndef WCOREDUMP /* not on Mac OS X Tiger */
38
#define WCOREDUMP(status) 0
48
SIGILL, "sys: illegal instruction",
49
SIGTRAP, "sys: breakpoint",
50
SIGABRT, "sys: abort",
52
SIGEMT, "sys: emulate instruction executed",
54
SIGFPE, "sys: fp: trap",
56
SIGBUS, "sys: bus error",
57
SIGSEGV, "sys: segmentation violation",
60
SIGURG, "sys: urgent condition on socket",
64
SIGCHLD, "sys: child",
67
#ifdef SIGIO /* not on Mac OS X Tiger */
68
SIGIO, "sys: i/o possible on fd",
70
SIGXCPU, "sys: cpu time limit exceeded",
71
SIGXFSZ, "sys: file size limit exceeded",
72
SIGVTALRM, "sys: virtual time alarm",
73
SIGPROF, "sys: profiling timer alarm",
74
#ifdef SIGWINCH /* not on Mac OS X Tiger */
75
SIGWINCH, "sys: window size change",
78
SIGINFO, "sys: status request",
82
SIGPIPE, "sys: write on closed pipe",
86
_p9sigstr(int sig, char *tmp)
90
for(i=0; i<nelem(tab); i++)
95
sprint(tmp, "sys: signal %d", sig);
104
for(i=0; i<nelem(tab); i++)
105
if(strcmp(s, tab[i].str) == 0)
111
_wait(int pid4, int opt)
119
w = malloc(sizeof *w + 200);
122
memset(w, 0, sizeof *w);
123
w->msg = (char*)&w[1];
126
/* On Linux, pid==-1 means anyone; on SunOS, it's pid==0. */
128
pid = wait3(&status, opt, &ru);
130
pid = wait4(pid4, &status, opt, &ru);
135
u = ru.ru_utime.tv_sec*1000+((ru.ru_utime.tv_usec+500)/1000);
136
s = ru.ru_stime.tv_sec*1000+((ru.ru_stime.tv_usec+500)/1000);
141
if(WIFEXITED(status)){
143
sprint(w->msg, "%d", status);
146
if(WIFSIGNALED(status)){
147
cd = WCOREDUMP(status);
148
sprint(w->msg, "signal: %s", _p9sigstr(WTERMSIG(status), tmp));
150
strcat(w->msg, " (core dumped)");
165
return _wait(pid, 0);
171
return _wait(-1, WNOHANG);
178
return wait(&status);