-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.h
360 lines (278 loc) · 6.86 KB
/
msgs.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef MSGS_H
#define MSGS_H
#include <sys/ioctl.h>
#include "common.h"
enum msg_type
{
CMD_MSG,
IO_MSG,
WINSIZE_MSG,
};
struct cmd_msg
{
bool tty;
struct winsize winsize;
int num_cmd_strings;
int strtab_size;
char strtab[];
};
struct io_msg
{
int destfd;
int data_size;
char data[];
};
struct winsize_msg
{
struct winsize winsize;
};
struct msg_wrapper
{
int type;
union {
struct cmd_msg cmd;
struct io_msg io;
struct winsize_msg winsize;
} msg;
};
static inline int send_cmd_msg(
int fd,
char **cmd,
int num_elements,
bool tty,
struct winsize *winsize)
{
struct cmd_msg message = {0};
int string_lengths[num_elements];
message.tty = tty;
if (winsize != NULL) {
message.winsize = *winsize;
}
message.num_cmd_strings = num_elements;
for (int i = 0; i < num_elements; i++) {
string_lengths[i] = strlen(cmd[i]) + 1;
message.strtab_size += string_lengths[i];
}
int type = CMD_MSG;
int n = write_all(fd, (char *)&type, sizeof(int));
if (n < 0) {
return n;
}
if (n < sizeof(type)) {
return -1;
}
n = write_all(fd, (char *)&message, sizeof(cmd_msg));
if (n < 0) {
return n;
}
for (int i = 0; i < num_elements; i++) {
int n = write_all(fd, cmd[i], string_lengths[i]);
if (n < 0) {
return n;
}
}
return 0;
}
static inline int send_io_msg(int fd, int destfd, char *buffer, int size)
{
struct msg_wrapper message;
message.type = IO_MSG;
message.msg.io.destfd = destfd;
message.msg.io.data_size = size;
int n = write_all(fd, (char*)&message, sizeof(message.type));
if (n < 0) {
return n;
}
n = write_all(fd, (char*)&message.msg.io, sizeof(struct io_msg));
if (n < 0) {
return n;
}
n = write_all(fd, buffer, size);
if (n < 0) {
return n;
}
return 0;
}
static inline int send_winsize_msg(int fd, struct winsize *winsize)
{
struct msg_wrapper message;
message.type = WINSIZE_MSG;
message.msg.winsize.winsize = *winsize;
int n = write_all(fd, (char*)&message, sizeof(message.type));
if (n < 0) {
return n;
}
n = write_all(fd, (char*)&message.msg.winsize, sizeof(struct winsize_msg));
if (n < 0) {
return n;
}
return 0;
}
static inline int recv_cmd_msg(int fd, struct msg_wrapper **message)
{
struct cmd_msg cmd_msg;
int n = read_all(fd, (char *)&cmd_msg, sizeof(struct cmd_msg));
if (n < 0) {
return n;
}
if (n < sizeof(cmd_msg)) {
return -1;
}
*message = (struct msg_wrapper*) malloc(
sizeof(struct msg_wrapper) + cmd_msg.strtab_size);
(*message)->msg.cmd = cmd_msg;
n = read_all(
fd,
(*message)->msg.cmd.strtab,
(*message)->msg.cmd.strtab_size);
if (n < 0) {
return n;
}
if (n < ((*message)->msg.cmd.strtab_size)) {
return -1;
}
return 0;
}
static inline int recv_msg(int fd, struct msg_wrapper **message)
{
int type;
int n = read_all(fd, (char *)&type, sizeof(type));
if (n < 0) {
return n;
}
if (n < sizeof(type)) {
return -1;
}
switch (type) {
case CMD_MSG:
return recv_cmd_msg(fd, message);
case IO_MSG:
break;
case WINSIZE_MSG:
break;
}
assert(0);
}
struct async_msg_state
{
int type;
int phase;
int phase_total;
int msg_total;
bool finished;
struct msg_wrapper *message;
char buffer[sizeof((struct msg_wrapper*)0)->msg];
};
int recv_msg_async(
int fd,
struct async_msg_state *state)
{
int phase_size = 0;
char *phase_dst = NULL;
while (true) {
switch (state->phase) {
case 0:
phase_size = sizeof(state->type);
phase_dst = state->buffer;
break;
case 1:
state->type = *((int*)state->buffer);
if (state->type == CMD_MSG) {
phase_size = sizeof(struct cmd_msg);
}
if (state->type == IO_MSG) {
phase_size = sizeof(struct io_msg);
}
if (state->type == WINSIZE_MSG) {
phase_size = sizeof(struct winsize_msg);
}
phase_dst = state->buffer;
break;
case 2:
if (state->type == CMD_MSG) {
phase_size = ((struct cmd_msg*)state->buffer)->strtab_size;
if (state->phase_total == 0) {
state->message = (struct msg_wrapper*) malloc(
sizeof(struct msg_wrapper) + phase_size);
state->message->type = state->type;
state->message->msg.cmd =
*((struct cmd_msg*)state->buffer);
}
phase_dst = (char*) state->message->msg.cmd.strtab;
}
if (state->type == IO_MSG) {
phase_size = ((struct io_msg*)state->buffer)->data_size;
if (state->phase_total == 0) {
state->message = (struct msg_wrapper*) malloc(
sizeof(struct msg_wrapper) + phase_size);
state->message->type = state->type;
state->message->msg.io =
*((struct io_msg*)state->buffer);
}
phase_dst = (char*) state->message->msg.io.data;
}
if (state->type == WINSIZE_MSG) {
if (state->phase_total == 0) {
state->message = (struct msg_wrapper*) malloc(
sizeof(struct msg_wrapper));
state->message->type = state->type;
state->message->msg.winsize =
*((struct winsize_msg*)state->buffer);
state->finished = true;
return state->msg_total;
}
}
break;
case 3:
state->finished = true;
return state->msg_total;
}
int n = read_all(
fd,
phase_dst + state->phase_total,
phase_size - state->phase_total);
if (n < 0) {
return n;
}
if (n == 0) {
return state->msg_total;
}
state->phase_total += n;
state->msg_total += n;
if (state->phase_total < phase_size) {
return state->msg_total;
}
assert(state->phase_total == phase_size);
state->phase_total = 0;
state->phase++;
}
}
static inline char **build_cmd_array(struct cmd_msg *message)
{
char **cmd = (char **)malloc(message->num_cmd_strings + 1);
char *strtab_ptr = message->strtab;
for (int i = 0; i < message->num_cmd_strings; i++) {
cmd[i] = strtab_ptr;
strtab_ptr += strlen(strtab_ptr) + 1;
}
cmd[message->num_cmd_strings] = NULL;
return cmd;
}
static inline void dump_cmd_msg(struct cmd_msg *message)
{
printf("tty: %s\n", message->tty ? "true" : "false");
printf("num_cmd_strings: %d\n", message->num_cmd_strings);
printf("strtab_size: %d\n", message->strtab_size);
char *strtab_ptr = message->strtab;
for (int i = 0; i < message->num_cmd_strings; i++) {
printf("string[%d]: %s\n", i, strtab_ptr);
strtab_ptr += strlen(strtab_ptr) + 1;
}
}
static inline void dump_io_msg(struct io_msg *message)
{
printf("destfd: %d\n", message->destfd);
printf("data_size: %d\n", message->data_size);
printf("data: %s\n", message->data);
}
#endif // MSGS_H