This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
141 lines (131 loc) · 2.35 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "sys.h"
int
xread(int sd, void *buf, size_t len)
{
char *p = (char *)buf;
size_t nrecv = 0;
ssize_t rv;
while (nrecv < len) {
rv = read(sd, p, len - nrecv);
if (0 > rv && errno == EINTR)
continue;
if (0 > rv)
return -1;
if (0 == rv)
return 0;
nrecv += rv;
p += rv;
}
return nrecv;
}
int
xwrite(int sd, const void *buf, size_t len)
{
char *p = (char *)buf;
size_t nsent = 0;
ssize_t rv;
while (nsent < len) {
rv = write(sd, p, len - nsent);
if (0 > rv && (errno == EINTR || errno == EAGAIN))
continue;
if (0 > rv)
return -1;
nsent += rv;
p += rv;
}
return nsent;
}
void
call_if_present(char *name, char *lib)
{
void *h;
void (*f)();
h = dlopen(lib, RTLD_NOW);
if (!h)
return;
f = dlsym(h, name);
if (!f)
return;
f();
}
void
fatal(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(1);
}
void *
xmalloc(size_t size)
{
void *p;
p = malloc(size);
if (!p)
fatal("out of memory");
bzero(p, size);
return p;
}
char *
xstrdup(char *p)
{
char *q;
q = strdup(p);
if(!q)
fatal("out of memory");
return q;
}
static struct {
char *name;
int num;
} map[] = {
{ "0", 0 },
{ "SIGHUP", SIGHUP },
{ "SIGINT", SIGINT },
{ "SIGQUIT", SIGQUIT },
{ "SIGILL", SIGILL },
{ "SIGTRAP", SIGTRAP },
{ "SIGABRT", SIGABRT },
{ "SIGIOT", SIGIOT },
{ "SIGBUS", SIGBUS },
{ "SIGFPE", SIGFPE },
{ "SIGKILL", SIGKILL },
{ "SIGUSR1", SIGUSR1 },
{ "SIGSEGV", SIGSEGV },
{ "SIGUSR2", SIGUSR2 },
{ "SIGPIPE", SIGPIPE },
{ "SIGALRM", SIGALRM },
{ "SIGTERM", SIGTERM },
{ "SIGSTKFLT", SIGSTKFLT },
{ "SIGCLD", SIGCLD },
{ "SIGCHLD", SIGCHLD },
{ "SIGCONT", SIGCONT },
{ "SIGSTOP", SIGSTOP },
{ "SIGTSTP", SIGTSTP },
{ "SIGTTIN", SIGTTIN },
{ "SIGTTOU", SIGTTOU },
{ "SIGURG", SIGURG },
{ "SIGXCPU", SIGXCPU },
{ "SIGXFSZ", SIGXFSZ },
{ "SIGVTALRM", SIGVTALRM },
{ "SIGPROF", SIGPROF },
{ "SIGWINCH", SIGWINCH },
{ "SIGPOLL", SIGPOLL },
{ "SIGIO", SIGIO },
{ "SIGPWR", SIGPWR },
{ "SIGSYS", SIGSYS },
{ NULL, 0 }
};
int
ckpt_mapsig(char *s)
{
int i;
i = 0;
while(map[i].name) {
if (!strcmp(map[i].name, s))
return map[i].num;
i++;
}
return -1;
}