0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

Flock: adapted to the new cbor encoder api

This commit is contained in:
Maria Matejka 2024-10-07 18:57:09 +02:00
parent b5e29c8dd8
commit ffec1451a9
2 changed files with 126 additions and 72 deletions

View File

@ -71,13 +71,21 @@ static int container_forker_fd = -1;
static void static void
container_poweroff(int fd, int sig) container_poweroff(int fd, int sig)
{ {
byte outbuf[128]; struct {
linpool *lp = lp_new(&root_pool); struct cbor_writer w;
struct cbor_writer *cw = cbor_init(outbuf, sizeof outbuf, lp); struct cbor_writer_stack_item si[2];
cbor_open_block_with_length(cw, 1); byte buf[128];
cbor_add_int(cw, -4); } _cw;
cbor_add_int(cw, sig);
ASSERT_DIE(write(fd, outbuf, cw->pt) == cw->pt); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, _cw.buf, sizeof _cw.buf);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -4);
cbor_put_int(cw, sig);
}
ASSERT_DIE(cbor_writer_done(cw) == 1);
s64 sz = cw->data.pos - cw->data.start;
ASSERT_DIE(write(fd, cw->data.start, sz) == sz);
unlink("/dev/log"); unlink("/dev/log");
} }
@ -597,15 +605,22 @@ container_start(void)
close(fds[1]); close(fds[1]);
byte outbuf[128]; struct {
linpool *lp = lp_new(&root_pool); struct cbor_writer w;
struct cbor_writer *cw = cbor_init(outbuf, sizeof outbuf, lp); struct cbor_writer_stack_item si[2];
cbor_open_block_with_length(cw, 1); byte buf[128];
cbor_add_int(cw, -2); } _cw;
cbor_add_int(cw, pid);
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, _cw.buf, sizeof _cw.buf);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -2);
cbor_put_int(cw, pid);
}
struct iovec v = { struct iovec v = {
.iov_base = outbuf, .iov_base = cw->data.start,
.iov_len = cw->pt, .iov_len = cw->data.pos - cw->data.start,
}; };
byte cbuf[CMSG_SPACE(sizeof fds[0])]; byte cbuf[CMSG_SPACE(sizeof fds[0])];
struct msghdr m = { struct msghdr m = {
@ -766,13 +781,18 @@ container_created(callback *cb)
SKIP_BACK_DECLARE(struct container_operation_callback, ccc, cb, cb); SKIP_BACK_DECLARE(struct container_operation_callback, ccc, cb, cb);
sock *s = ccc->s; sock *s = ccc->s;
linpool *lp = lp_new(s->pool); struct {
struct cbor_writer *cw = cbor_init(s->tbuf, s->tbsize, lp); struct cbor_writer w;
cbor_open_block_with_length(cw, 1); struct cbor_writer_stack_item si[2];
cbor_add_int(cw, -1); } _cw;
cbor_add_string(cw, "OK");
sk_send(s, cw->pt); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize);
rfree(lp); CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -1);
cbor_put_string(cw, "OK");
}
sk_send(s, cw->data.pos - cw->data.start);
s->data = ccc->data; s->data = ccc->data;
sk_resume_rx(s->loop, s); sk_resume_rx(s->loop, s);
@ -789,13 +809,19 @@ hypervisor_container_request(sock *s, const char *name, const char *basedir, con
struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h); struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h);
if (crt) if (crt)
{ {
linpool *lp = lp_new(hcf.p); struct {
struct cbor_writer *cw = cbor_init(s->tbuf, s->tbsize, lp); struct cbor_writer w;
cbor_open_block_with_length(cw, 1); struct cbor_writer_stack_item si[2];
cbor_add_int(cw, -127); } _cw;
cbor_add_string(cw, "BAD: Already exists");
sk_send(s, cw->pt); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -127);
cbor_put_string(cw, "BAD: Already exists");
}
sk_send(s, cw->data.pos - cw->data.start);
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
return; return;
@ -838,17 +864,23 @@ hypervisor_container_request(sock *s, const char *name, const char *basedir, con
log(L_INFO "requesting machine creation, socket %p", s); log(L_INFO "requesting machine creation, socket %p", s);
linpool *lp = lp_new(hcf.p); struct {
struct cbor_writer *cw = cbor_init(hcf.s->tbuf, hcf.s->tbsize, lp); struct cbor_writer w;
cbor_open_block_with_length(cw, 3); struct cbor_writer_stack_item si[2];
cbor_add_int(cw, 0); } _cw;
cbor_add_string(cw, name);
cbor_add_int(cw, 1); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize);
cbor_add_string(cw, basedir); CBOR_PUT_MAP(cw)
cbor_add_int(cw, 2); {
cbor_add_string(cw, workdir); cbor_put_int(cw, 0);
sk_send(hcf.s, cw->pt); cbor_put_string(cw, name);
rfree(lp); cbor_put_int(cw, 1);
cbor_put_string(cw, basedir);
cbor_put_int(cw, 2);
cbor_put_string(cw, workdir);
}
sk_send(s, cw->data.pos - cw->data.start);
s->err_paused = crt_err; s->err_paused = crt_err;
s->data = crt; s->data = crt;
@ -863,13 +895,19 @@ container_stopped(callback *cb)
SKIP_BACK_DECLARE(struct container_operation_callback, ccc, cb, cb); SKIP_BACK_DECLARE(struct container_operation_callback, ccc, cb, cb);
sock *s = ccc->s; sock *s = ccc->s;
linpool *lp = lp_new(s->pool); struct {
struct cbor_writer *cw = cbor_init(s->tbuf, s->tbsize, lp); struct cbor_writer w;
cbor_open_block_with_length(cw, 1); struct cbor_writer_stack_item si[2];
cbor_add_int(cw, -1); } _cw;
cbor_add_string(cw, "OK");
sk_send(s, cw->pt); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize);
rfree(lp); CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -1);
cbor_put_string(cw, "OK");
}
sk_send(s, cw->data.pos - cw->data.start);
s->data = ccc->data; s->data = ccc->data;
sk_resume_rx(s->loop, s); sk_resume_rx(s->loop, s);
@ -885,28 +923,38 @@ hypervisor_container_shutdown(sock *s, const char *name)
uint h = mem_hash(name, strlen(name)); uint h = mem_hash(name, strlen(name));
struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h); struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h);
linpool *lp = lp_new(hcf.p);
if (!crt || !crt->s) if (!crt || !crt->s)
{ {
struct cbor_writer *cw = cbor_init(s->tbuf, s->tbsize, lp); struct {
cbor_open_block_with_length(cw, 1); struct cbor_writer w;
cbor_add_int(cw, -127); struct cbor_writer_stack_item si[2];
cbor_add_string(cw, "BAD: Not found"); } _cw;
sk_send(s, cw->pt); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize);
rfree(lp); CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -127);
cbor_put_string(cw, "BAD: Not found");
}
sk_send(s, cw->data.pos - cw->data.start);
birdloop_leave(hcf.loop); birdloop_leave(hcf.loop);
return; return;
} }
struct cbor_writer *cw = cbor_init(crt->s->tbuf, crt->s->tbsize, lp); struct {
cbor_open_block_with_length(cw, 1); struct cbor_writer w;
cbor_add_int(cw, 0); struct cbor_writer_stack_item si[2];
write_item(cw, 7, 22); } _cw;
sk_send(crt->s, cw->pt); struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, s->tbuf, s->tbsize);
rfree(lp); CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, 0);
cbor_put_null(cw);
}
sk_send(s, cw->data.pos - cw->data.start);
struct container_operation_callback *ccc = mb_alloc(s->pool, sizeof *ccc); struct container_operation_callback *ccc = mb_alloc(s->pool, sizeof *ccc);
*ccc = (struct container_operation_callback) { *ccc = (struct container_operation_callback) {

View File

@ -44,16 +44,25 @@ struct hcs_parser_channel {
}; };
static void static void
hcs_request_poweroff(struct hcs_parser_channel *hpc) //hcs_request_poweroff(struct hcs_parser_channel *hpc)
hcs_request_poweroff(struct hcs_parser_context *htx)
{ {
log(L_INFO "Requested shutdown via CLI"); log(L_INFO "Requested shutdown via CLI");
ev_send_loop(&main_birdloop, &poweroff_event); ev_send_loop(&main_birdloop, &poweroff_event);
struct cbor_writer *cw = cbor_init(htx->sock->tbuf, htx->sock->tbsize, ctx->lp); struct {
cbor_open_block_with_length(cw, 1); struct cbor_writer w;
cbor_add_int(cw, -1); struct cbor_writer_stack_item si[2];
cbor_add_string(cw, "OK"); } _cw;
sk_send(htx->sock, cw->pt);
struct cbor_writer *cw = cbor_writer_init(&_cw.w, 2, htx->sock->tbuf, htx->sock->tbsize);
CBOR_PUT_MAP(cw)
{
cbor_put_int(cw, -1);
cbor_put_string(cw, "OK");
}
sk_send(htx->sock, cw->data.pos - cw->data.start);
} }
static void static void
@ -87,6 +96,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
for (int pos = 0; pos < size; pos++) for (int pos = 0; pos < size; pos++)
{ {
/*
if (!htx->channel) if (!htx->channel)
{ {
htx->channel = cbor_parse_channel(ctx, htx->stream, buf[pos]); htx->channel = cbor_parse_channel(ctx, htx->stream, buf[pos]);
@ -94,6 +104,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
return -(htx->bytes_consumed + pos + 1); return -(htx->bytes_consumed + pos + 1);
continue; continue;
} }
*/
switch (cbor_parse_byte(ctx, buf[pos])) switch (cbor_parse_byte(ctx, buf[pos]))
{ {
@ -119,12 +130,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size)
htx->major_state = 1; htx->major_state = 1;
break; break;
case 1: /* ID */ case 1: /* Command */
CBOR_PARSE_ONLY(ctx, POSINT, htx->id);
htx->major_state = 2;
break;
case 2: /* Command */
CBOR_PARSE_ONLY(ctx, POSINT, htx->cmd); CBOR_PARSE_ONLY(ctx, POSINT, htx->cmd);
if (htx->cmd > HCS_CMD__MAX) if (htx->cmd > HCS_CMD__MAX)
CBOR_PARSER_ERROR("Command key too high, got %lu", htx->cmd); CBOR_PARSER_ERROR("Command key too high, got %lu", htx->cmd);