Blob


1 #define NOPLAN9DEFINES
2 #include <u.h>
3 #include <libc.h>
5 #include <signal.h>
6 #include <sys/types.h>
7 #include <sys/resource.h>
8 #include <sys/wait.h>
9 #include <sys/time.h>
11 static struct {
12 int sig;
13 char *str;
14 } tab[] = {
15 SIGHUP, "hangup",
16 SIGINT, "interrupt",
17 SIGQUIT, "quit",
18 SIGILL, "sys: trap: illegal instruction",
19 SIGTRAP, "sys: trace trap",
20 SIGABRT, "sys: abort",
21 SIGEMT, "sys: emulate instruction executed",
22 SIGFPE, "sys: fp: trap",
23 SIGKILL, "sys: kill",
24 SIGBUS, "sys: bus error",
25 SIGSEGV, "sys: segmentation violation",
26 SIGALRM, "alarm",
27 SIGTERM, "kill",
28 SIGURG, "sys: urgent condition on socket",
29 SIGSTOP, "sys: stop",
30 SIGTSTP, "sys: tstp",
31 SIGCONT, "sys: cont",
32 SIGCHLD, "sys: child",
33 SIGTTIN, "sys: ttin",
34 SIGTTOU, "sys: ttou",
35 SIGIO, "sys: i/o possible on fd",
36 SIGXCPU, "sys: cpu time limit exceeded",
37 SIGXFSZ, "sys: file size limit exceeded",
38 SIGVTALRM, "sys: virtual time alarm",
39 SIGPROF, "sys: profiling timer alarm",
40 SIGWINCH, "sys: window size change",
41 SIGINFO, "sys: status request",
42 SIGUSR1, "sys: usr1",
43 SIGUSR2, "sys: usr2",
44 };
46 char*
47 _p9sigstr(int sig, char *tmp)
48 {
49 int i;
51 for(i=0; i<nelem(tab); i++)
52 if(tab[i].sig == sig)
53 return tab[i].str;
54 sprint(tmp, "sys: signal %d", sig);
55 return tmp;
56 }
58 int
59 _p9strsig(char *s)
60 {
61 int i;
63 for(i=0; i<nelem(tab); i++)
64 if(strcmp(s, tab[i].str) == 0)
65 return tab[i].sig;
66 return 0;
67 }
69 int
70 await(char *str, int n)
71 {
72 int pid, status, cd;
73 struct rusage ru;
74 char buf[128], tmp[64];
75 ulong u, s;
77 for(;;){
78 pid = wait3(&status, 0, &ru);
79 if(pid < 0)
80 return -1;
81 u = ru.ru_utime.tv_sec*1000+((ru.ru_utime.tv_usec+500)/1000);
82 s = ru.ru_stime.tv_sec*1000+((ru.ru_stime.tv_usec+500)/1000);
83 if(WIFEXITED(status)){
84 status = WEXITSTATUS(status);
85 if(status)
86 snprint(buf, sizeof buf, "%d %lud %lud %lud %d", pid, u, s, u+s, status);
87 else
88 snprint(buf, sizeof buf, "%d %lud %lud %lud ''", pid, u, s, u+s, status);
89 strecpy(str, str+n, buf);
90 return strlen(str);
91 }
92 if(WIFSIGNALED(status)){
93 cd = WCOREDUMP(status);
94 USED(cd);
95 snprint(buf, sizeof buf, "%d %lud %lud %lud '%s'", pid, u, s, u+s, _p9sigstr(WTERMSIG(status), tmp));
96 strecpy(str, str+n, buf);
97 return strlen(str);
98 }
99 }