Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
224 lines (209 loc) · 7.58 KB
/
Copy pathmain.c
File metadata and controls
224 lines (209 loc) · 7.58 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
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
#define _POSIX_C_SOURCE 200809L
#include "common.h"
#include "door.h"
#include "emit.h"
#include "mcp.h"
#include "server.h"
#include "store.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(__GLIBC__) && !defined(__FILC__) && !defined(__SANITIZE_ADDRESS__)
#include <malloc.h>
#endif
#define STIGI_SEED_VERSION "0.1.0"
#define STIGI_DEFAULT_RULES "vic/spec/datalog/all.dl"
static void print_usage(void) {
printf("usage:\n"
" stigi-seed --version\n"
" stigi-seed --serve <sock> [--load <dir>] [--user] [--rules <all.dl>]\n"
" stigi-seed --cc-socks <sock> [--peer <name>] [--from-mode prompting|bypass] [--load <dir>] [--user] [--rules <all.dl>]\n"
" stigi-seed --mcp [--socket <sock>] [--load <dir>] [--user] [--rules <all.dl>]\n"
" stigi-seed --load <dir> --emit <out.c> [--user] [--rules <all.dl>]\n"
" stigi-seed --load <dir> --emit-test <out.c> [--user] [--rules <all.dl>]\n");
}
int main(int argc, char **argv) {
/* Ignore SIGPIPE so disconnected clients never terminate the server */
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGPIPE, &sa, NULL);
const char *rules_path = STIGI_DEFAULT_RULES;
const char *serve_sock = NULL;
const char *cc_sock = NULL;
const char *peer_name = NULL;
StigiCcFromMode from_mode = STIGI_CC_FROM_PROMPTING;
const char *mcp_sock = NULL;
const char *load_dir = NULL;
const char *emit_path = NULL;
int emit_flags = 0;
StigiEmitProfile emit_profile = STIGI_EMIT_RELEASE;
bool mcp_mode = false;
bool kernel_mode = true;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--version") == 0) {
printf("stigi-seed %s\n", STIGI_SEED_VERSION);
return 0;
} else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
print_usage();
return 0;
} else if (strcmp(argv[i], "--rules") == 0 && i + 1 < argc) {
rules_path = argv[++i];
} else if (strcmp(argv[i], "--serve") == 0 && i + 1 < argc) {
serve_sock = argv[++i];
} else if (strcmp(argv[i], "--cc-socks") == 0 && i + 1 < argc) {
cc_sock = argv[++i];
} else if (strcmp(argv[i], "--peer") == 0 && i + 1 < argc) {
peer_name = argv[++i];
if (!stigi_cc_peer_name_valid(peer_name)) {
fprintf(stderr, "stigi-seed: --peer name must be non-empty with no <>\"\\ or control byte\n");
return 1;
}
} else if (strcmp(argv[i], "--from-mode") == 0 && i + 1 < argc) {
const char *m = argv[++i];
if (strcmp(m, "prompting") == 0) {
from_mode = STIGI_CC_FROM_PROMPTING;
} else if (strcmp(m, "bypass") == 0) {
from_mode = STIGI_CC_FROM_BYPASS;
} else {
fprintf(stderr, "stigi-seed: --from-mode must be prompting or bypass, found '%s'\n", m);
return 1;
}
} else if (strcmp(argv[i], "--mcp") == 0) {
mcp_mode = true;
} else if (strcmp(argv[i], "--socket") == 0 && i + 1 < argc) {
mcp_sock = argv[++i];
} else if (strcmp(argv[i], "--load") == 0 && i + 1 < argc) {
load_dir = argv[++i];
} else if (strcmp(argv[i], "--emit") == 0 && i + 1 < argc) {
emit_path = argv[++i];
emit_flags++;
} else if (strcmp(argv[i], "--emit-test") == 0 && i + 1 < argc) {
emit_path = argv[++i];
emit_flags++;
emit_profile = STIGI_EMIT_TEST;
} else if (strcmp(argv[i], "--user") == 0) {
kernel_mode = false;
} else {
fprintf(stderr, "stigi-seed: unknown or incomplete option '%s'\n", argv[i]);
print_usage();
return 1;
}
}
int mode_count = (serve_sock != NULL ? 1 : 0) + (cc_sock != NULL ? 1 : 0) +
(mcp_mode ? 1 : 0) + emit_flags;
if (mode_count == 0) {
fprintf(stderr, "stigi-seed: no mode specified (try --help)\n");
return 1;
}
if (mode_count > 1) {
fprintf(stderr, "stigi-seed: --serve, --cc-socks, --mcp, --emit, and --emit-test are mutually exclusive\n");
return 1;
}
if (mcp_sock != NULL && !mcp_mode) {
fprintf(stderr, "stigi-seed: --socket requires --mcp\n");
return 1;
}
if (emit_path != NULL && load_dir == NULL) {
fprintf(stderr, "stigi-seed: --emit/--emit-test requires --load <dir>\n");
return 1;
}
/* --mcp --socket <path> bridge mode: forwards verbatim frames to a live core socket */
if (mcp_mode && mcp_sock != NULL) {
Diag diag;
diag_clear(&diag);
Status st = stigi_serve_mcp_stdio_bridge(mcp_sock, &diag);
if (st != STIGI_OK) {
fprintf(stderr, "stigi-seed: mcp bridge error: %s\n", diag.msg[0] ? diag.msg : "error");
return 1;
}
return 0;
}
#if defined(__GLIBC__) && !defined(__FILC__) && !defined(__SANITIZE_ADDRESS__)
/* freed memory stays with the process: every Test reuses the pages the previous one freed instead of faulting fresh
ones in (X28-au: glibc served multi-MB evaluator blocks by mmap and gave them back to the kernel on free) */
(void)mallopt(M_MMAP_MAX, 0);
(void)mallopt(M_TRIM_THRESHOLD, -1);
#endif
StigiDoor door;
Diag diag;
diag_clear(&diag);
Status st = stigi_door_init(&door, rules_path, STIGI_DEFAULT_ROW_BUDGET, kernel_mode, &diag);
if (st != STIGI_OK) {
fprintf(stderr, "stigi-seed: init failed: %s\n", diag.msg[0] ? diag.msg : "error");
return 1;
}
/* Pre-load --load <dir> before starting emit or any server mode */
if (load_dir != NULL) {
StigiDoorResult res;
stigi_door_result_init(&res);
st = stigi_load_dir(&door, load_dir, &res, &diag);
if (st != STIGI_OK || !res.accepted) {
fprintf(stderr, "stigi-seed: load '%s' failed: %s\n",
load_dir, diag.msg[0] ? diag.msg : "rejected");
for (uint32_t i = 0; i < res.reject_count; i++) {
fprintf(stderr, " reject %s %s: %s\n",
res.rejects[i].subject, res.rejects[i].code, res.rejects[i].msg);
}
stigi_door_result_free(&res);
stigi_door_free(&door);
return 1;
}
stigi_door_result_free(&res);
}
if (emit_path != NULL) {
char *c_code = NULL;
size_t c_len = 0;
StigiEmitStats stats;
st = stigi_emit_c(&door, emit_profile, &c_code, &c_len, &stats, &diag);
if (st != STIGI_OK) {
fprintf(stderr, "stigi-seed: emit failed: %s\n", diag.msg[0] ? diag.msg : "error");
stigi_door_free(&door);
return 1;
}
FILE *fp = fopen(emit_path, "wb");
if (fp == NULL) {
fprintf(stderr, "stigi-seed: cannot open '%s' for writing\n", emit_path);
free(c_code);
stigi_door_free(&door);
return 1;
}
size_t nw = fwrite(c_code, 1, c_len, fp);
int crc = fclose(fp);
free(c_code);
stigi_door_free(&door);
if (nw != c_len || crc != 0) {
fprintf(stderr, "stigi-seed: write/close error on '%s'\n", emit_path);
return 1;
}
return 0;
}
StigiServerCtx srv;
stigi_server_init(&srv, &door);
if (peer_name != NULL) srv.peer_name = peer_name;
srv.from_mode = from_mode;
char reg_path[512];
reg_path[0] = '\0';
if (serve_sock != NULL) {
st = stigi_serve_socket(&srv, serve_sock, 0, &diag);
} else if (cc_sock != NULL) {
if (peer_name != NULL) {
(void)stigi_cc_peer_register(peer_name, cc_sock, reg_path, sizeof reg_path, &diag);
}
st = stigi_serve_cc_socks(&srv, cc_sock, 0, &diag);
if (reg_path[0] != '\0') (void)unlink(reg_path);
} else if (mcp_mode) {
st = stigi_serve_mcp_stdio(&srv, &diag);
}
if (st != STIGI_OK) {
fprintf(stderr, "stigi-seed: server error: %s\n", diag.msg[0] ? diag.msg : "error");
stigi_server_free(&srv);
stigi_door_free(&door);
return 1;
}
stigi_server_free(&srv);
stigi_door_free(&door);
return 0;
}