Blob


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