-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-log.c
311 lines (264 loc) · 6.62 KB
/
run-log.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
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
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2022 Codethink
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <cyaml/cyaml.h>
#include "cmd/cmd.h"
#include "cmd/private.h"
#include "msg/msg.h"
#include "util/cli.h"
#include "util/log.h"
#include "util/util.h"
#include "util/decode.h"
/* The log messages arrive as an array of arrays as a JSON string.
* These are the schema to decode that to a `char ***` type. */
static const struct cyaml_schema_value value_entry_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char *, 0, CYAML_UNLIMITED),
};
static const struct cyaml_schema_value value_inner_schema = {
CYAML_VALUE_SEQUENCE_FIXED(CYAML_FLAG_POINTER, char *,
&value_entry_schema, 1),
};
/* Schema to decode log message response JSON. */
static const struct cyaml_schema_value value_schema = {
CYAML_VALUE_SEQUENCE(CYAML_FLAG_POINTER, char *,
&value_inner_schema, 0, CYAML_UNLIMITED),
};
/* Data structure for the log message response data. */
struct message_response {
int id;
struct run_log_response {
struct run_log {
char *type;
char *value; /* Log messages in single JSON string */
} result;
} result;
};
/**
* JavaScript log capture script.
*
* This makes a copy of the standard JavaScript `console.log()` at
* `console.stdlog`, and replaces `console.log` with a function that
* appends the log message to an array, before calling `console.stdlog`.
*
* If the log message array already exists, it clears the array.
*/
static const char *log_capture_script =
"if (console.logs === undefined) {"
" console.stdlog = console.log.bind(console);"
" console.logs = [];"
" console.log = function() {"
" console.logs.push(Array.from(arguments));"
" console.stdlog.apply(console, arguments);"
" }"
"} else {"
" console.logs.length = 0;"
"}";
/**
* JavaScript log fetch script.
*/
static const char *log_fetch_script =
"JSON.stringify(console.logs)";
/**
* JavaScript log reset script.
*
* Restores standard logging, so that the array doesn't accumulate forever.
*/
static const char *log_reset_script =
"if (console.logs !== undefined) {"
" console.log = console.stdlog;"
" console.logs.length = 0;"
" console.logs = undefined;"
"}";
static struct run_log_ctx {
int id_capture;
int id_expression;
int id_fetch;
int id_reset;
char ***log;
unsigned log_count;
const char *script;
const char *end_marker;
} run_log_g;
static const struct cli_table_entry cli_entries[] = {
CMD_CLI_COMMON("run-log"),
{
.p = true,
.l = "SCRIPT",
.t = CLI_STRING,
.v.s = &run_log_g.script,
.d = "JSON-escaped JavaScript."
},
{
.s = 'e',
.l = "end-marker",
.t = CLI_STRING,
.v.s = &run_log_g.end_marker,
.d = "String indicating end of log."
},
};
static const struct cli_table cli = {
.entries = cli_entries,
.count = (sizeof(cli_entries))/(sizeof(*cli_entries)),
.min_positional = 3,
};
static bool cmd_run_log_init(int argc, const char **argv,
struct cmd_options *options, void **pw_out)
{
if (!cmd_cli_parse(argc, argv, &cli, options)) {
return false;
}
/* Send log capture script. */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = log_capture_script,
},
},
}, &run_log_g.id_capture);
/* Send expression from command line */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = run_log_g.script,
},
},
}, &run_log_g.id_expression);
/* Send log fetch script. */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = log_fetch_script,
},
},
}, &run_log_g.id_fetch);
*pw_out = &run_log_g;
return true;
}
static const cyaml_config_t config = {
.flags = CYAML_CFG_IGNORE_UNKNOWN_KEYS,
.log_level = CYAML_LOG_WARNING,
.mem_fn = cyaml_mem,
.log_fn = cyaml_log,
};
static bool cmd_run_log__handle_raw(struct run_log_ctx *ctx, const char *raw)
{
char ***log;
bool complete;
size_t raw_len;
cyaml_err_t res;
unsigned log_count;
if (raw == NULL) {
return false;
}
raw_len = strlen(raw);
res = cyaml_load_data((const uint8_t *)raw, raw_len,
&config,
&value_schema,
(void **)&log, &log_count);
if (res != CYAML_OK) {
cdt_log(CDT_LOG_NOTICE, "Failed to parse log lines: %s",
cyaml_strerror(res));
return true;
}
if (ctx->log_count > log_count) {
cdt_log(CDT_LOG_WARNING, "Log tamper detected! Got %u, had %u",
log_count, ctx->log_count);
cyaml_free(&config, &value_schema, log, log_count);
return true;
}
for (unsigned i = 0; i < ctx->log_count; i++) {
if (log[i][0] != NULL && ctx->log[i][0] != NULL) {
if (strcmp(log[i][0], ctx->log[i][0]) != 0) {
cdt_log(CDT_LOG_WARNING,
"Log tamper detected!");
cyaml_free(&config, &value_schema,
log, log_count);
return true;
}
}
}
for (unsigned i = ctx->log_count; i < log_count; i++) {
if (log[i][0] != NULL) {
printf("%s\n", log[i][0]);
}
}
complete = false;
if (log_count > 0 && log[log_count - 1][0] != NULL) {
if (ctx->end_marker != NULL) {
if (strstr(log[log_count - 1][0],
ctx->end_marker) != NULL) {
complete = true;
}
}
}
cyaml_free(&config, &value_schema, ctx->log, ctx->log_count);
ctx->log = log;
ctx->log_count = log_count;
return complete;
}
static void cmd_run_log_msg(void *pw, int id, const char *msg, size_t len)
{
struct run_log_ctx *ctx = pw;
if (id != ctx->id_fetch) {
cdt_log(CDT_LOG_NOTICE, "Received message with id %i: %*s",
id, (int)len, msg);
return;
} else if (id == ctx->id_fetch) {
char *raw;
bool complete;
raw = decode_extract_response_value(msg, len);
if (raw == NULL) {
return;
}
complete = cmd_run_log__handle_raw(ctx, raw);
free(raw);
/* Send log fetch script. */
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = complete ?
log_reset_script :
log_fetch_script,
},
},
}, complete ?
&run_log_g.id_reset :
&run_log_g.id_fetch);
if (!complete) {
sleep(1);
}
return;
}
}
static void cmd_run_log_fini(void *pw)
{
struct run_log_ctx *ctx = pw;
cyaml_free(&config, &value_schema, ctx->log, ctx->log_count);
}
static void cmd_run_log_help(int argc, const char **argv);
const struct cmd_table cmd_run_log = {
.cmd = "run-log",
.init = cmd_run_log_init,
.help = cmd_run_log_help,
.msg = cmd_run_log_msg,
.fini = cmd_run_log_fini,
};
static void cmd_run_log_help(int argc, const char **argv)
{
cli_help(&cli, (argc > 0) ? argv[0] : "cdt");
}