-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsvctl_dump.c
More file actions
191 lines (141 loc) · 3.15 KB
/
svctl_dump.c
File metadata and controls
191 lines (141 loc) · 3.15 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <sys/proc.h>
#include <string.h>
#include <format.h>
#include <util.h>
#include "common.h"
#include "svctl.h"
static int cmp_str(attr at, attr bt, int key)
{
char* na = uc_sub_str(at, key);
char* nb = uc_sub_str(bt, key);
if(!na || !nb)
return 0;
return strcmp(na, nb);
}
static int rec_ord(const void* a, const void* b)
{
attr at = *((attr*)a);
attr bt = *((attr*)b);
int ret;
if((ret = cmp_str(at, bt, ATTR_NAME)))
return ret;
return 0;
}
static attr* prep_list(CTX, MSG, int key, qcmp2 cmp)
{
int n = 0, i = 0;
attr at;
for(at = uc_get_0(msg); at; at = uc_get_n(msg, at))
if(at->key == key)
n++;
attr* refs = heap_alloc(ctx, (n+1)*sizeof(void*));
for(at = uc_get_0(msg); at && i < n; at = uc_get_n(msg, at))
if(at->key == key)
refs[i++] = at;
refs[i] = NULL;
qsort(refs, i, sizeof(void*), cmp);
return refs;
}
static int intlen(int x)
{
int len = 1;
if(x < 0) { x = -x; len++; };
for(; x >= 10; len++) x /= 10;
return len;
}
static int max_pid_len(attr* procs)
{
int len, max = 0;
int* pid;
for(attr* ap = procs; *ap; ap++)
if((pid = uc_sub_int(*ap, ATTR_PID)))
if((len = intlen(*pid)) > max)
max = len;
return max;
}
static void dump_proc(CTX, AT, int maxpidlen)
{
char buf[100];
char* p = buf;
char* e = buf + sizeof(buf) - 1;
char* q;
char* name = uc_sub_str(at, ATTR_NAME);
int* pid = uc_sub_int(at, ATTR_PID);
if(pid)
q = fmtint(p, e, *pid);
else
q = fmtstr(p, e, "-");
p = fmtpad(p, e, maxpidlen, q);
if(uc_sub(at, ATTR_RING))
p = fmtstr(p, e, "*");
else
p = fmtstr(p, e, " ");
p = fmtstr(p, e, " ");
p = fmtstr(p, e, name ? name : "???");
*p++ = '\n';
output(ctx, buf, p - buf);
}
void dump_list(CTX, MSG)
{
attr* procs = prep_list(ctx, msg, ATTR_PROC, rec_ord);
int maxlen = max_pid_len(procs);
for(attr* ap = procs; *ap; ap++)
dump_proc(ctx, *ap, maxlen);
}
static void newline(CTX)
{
output(ctx, "\n", 1);
}
static void dump_proc_ring(CTX, MSG)
{
attr ring;
int paylen;
if(!(ring = uc_get(msg, ATTR_RING)))
return;
if((paylen = uc_paylen(ring)) <= 0)
return;
output(ctx, ring->payload, uc_paylen(ring));
if(ring->payload[paylen-1] == '\n')
newline(ctx);
}
void dump_info(CTX, MSG)
{
char buf[200];
char* p = buf;
char* e = buf + sizeof(buf) - 1;
int* pid = uc_get_int(msg, ATTR_PID);
char* name = uc_get_str(msg, ATTR_NAME);
int* exit = uc_get_int(msg, ATTR_EXIT);
dump_proc_ring(ctx, msg);
p = fmtstr(p, e, name ? name : "???");
if(pid) {
p = fmtstr(p, e, " is running, PID ");
p = fmtint(p, e, *pid);
} else if(exit) {
int status = *exit;
p = fmtstr(p, e, " has failed");
if(WIFEXITED(status)) {
p = fmtstr(p, e, ", exit code ");
p = fmtint(p, e, WEXITSTATUS(status));
} else if(WIFSIGNALED(status)) {
p = fmtstr(p, e, ", killed by signal ");
p = fmtint(p, e, WTERMSIG(status));
}
} else {
p = fmtstr(p, e, " is not running");
}
*p++ = '\n';
output(ctx, buf, p - buf);
}
void dump_pid(CTX, MSG)
{
char buf[50];
char* p = buf;
char* e = buf + sizeof(buf) - 1;
int* pid;
if(!(pid = uc_get_int(msg, ATTR_PID)))
fail("no PID in reply", NULL, 0);
p = fmtint(p, e, *pid);
*p++ = '\n';
output(ctx, buf, p - buf);
}