From 2f4a08d5e83ad07fb3a3a53c16ddf7cbdc9903aa Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Fri, 11 Oct 2024 08:27:56 +0200 Subject: [PATCH] Flock: more container cbor --- flock/container.c | 31 +++++++++++++++++------------- flock/ctl.c | 36 ++++++++++++---------------------- flock/hypervisor.c | 48 +++++++++------------------------------------- 3 files changed, 39 insertions(+), 76 deletions(-) diff --git a/flock/container.c b/flock/container.c index 568433ec..476ef8ff 100644 --- a/flock/container.c +++ b/flock/container.c @@ -822,24 +822,28 @@ void hypervisor_container_start(struct cbor_channel *cch, struct flock_machine_container_config *ccf) { birdloop_enter(hcf.loop); -// const char *name, const char *basedir, const char *workdir + +#define FAIL(id, msg) do { \ + CBOR_REPLY(cch, cw) CBOR_PUT_MAP(cw) { \ + cbor_put_int(cw, id); cbor_put_string(cw, msg);\ + } cbor_done_channel(cch); \ + birdloop_leave(hcf.loop); \ + return; } while (0) + + if (!ccf->cf.name) + FAIL(-101, "Machine name not specified"); + + if (!ccf->workdir) + FAIL(-102, "Machine workdir not specified"); + + if (!ccf->basedir) + FAIL(-103, "Machine basedir not specified"); const char *name = ccf->cf.name; uint h = mem_hash(name, strlen(name)); struct container_runtime *crt = HASH_FIND(hcf.hash, CRT, name, h); if (crt) - { - CBOR_REPLY(cch, cw) - CBOR_PUT_MAP(cw) - { - cbor_put_int(cw, -127); - cbor_put_string(cw, "Container not created: Already exists"); - } - - cbor_done_channel(cch); - birdloop_leave(hcf.loop); - return; - } + FAIL(-127, "Container already exists"); uint nlen = strlen(name); crt = mb_allocz(hcf.p, sizeof *crt + nlen + 1); @@ -869,6 +873,7 @@ hypervisor_container_start(struct cbor_channel *cch, struct flock_machine_contai cbor_put_string(cw, ccf->workdir); } +#undef FAIL birdloop_leave(hcf.loop); } diff --git a/flock/ctl.c b/flock/ctl.c index d633b740..60db3d65 100644 --- a/flock/ctl.c +++ b/flock/ctl.c @@ -21,12 +21,13 @@ struct hcs_parser_context { struct cbor_parser_context *ctx; - struct cbor_stream *stream; struct hcs_parser_channel *channel; sock *sock; u64 bytes_consumed; u64 major_state; + + struct cbor_stream stream; }; struct hcs_parser_channel { @@ -74,7 +75,7 @@ hcs_parser_init(sock *s) htx->ctx = ctx; htx->sock = s; - htx->stream = cbor_stream_new(s->pool, 4); + cbor_stream_init(&htx->stream, 3); return htx; } @@ -94,7 +95,7 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size) bool finish_cmd = false; for (int pos = 0; pos < size; pos++) - { + {// TODO here → convert to the new channel parser if (!hpc) { struct cbor_channel *cch = cbor_parse_channel(ctx, htx->stream, buf[pos]); @@ -275,27 +276,14 @@ hcs_parse(struct hcs_parser_context *htx, const byte *buf, s64 size) break; case 501: - /* - if (!hpc->cfg.cf.type) - CBOR_PARSER_ERROR("Machine type not specified"); - - if (!hpc->cfg.cf.name) - CBOR_PARSER_ERROR("Machine name not specified"); - - if (!hpc->cfg.container.workdir) - CBOR_PARSER_ERROR("Machine workdir not specified"); - - if (!hpc->cfg.container.basedir) - CBOR_PARSER_ERROR("Machine basedir not specified"); - - hypervisor_container_request( - htx->sock, - hpc->cfg.cf.name, - hpc->cfg.container.basedir, - hpc->cfg.container.workdir); - */ - - hypervisor_container_start(&hpc->cch, &hpc->cfg.container); + switch (hpc->cfg.cf.type) + { + case 1: + hypervisor_container_start(&hpc->cch, &hpc->cfg.container); + break; + default: + CBOR_PARSER_ERROR("Unknown machine type: %d", hpc->cfg.cf.type); + } htx->major_state = 1; break; diff --git a/flock/hypervisor.c b/flock/hypervisor.c index 3c36b450..18d21a31 100644 --- a/flock/hypervisor.c +++ b/flock/hypervisor.c @@ -417,49 +417,19 @@ hexp_get_telnet(sock *s, const char *name) sk_pause_rx(s->loop, s); } -static void hexp_received_telnet(void *_data) +static void hexp_received_telnet(struct hexp_received_telnet *hrt) { - struct hexp_received_telnet *hrt = _data; - - ASSERT_DIE(he.port_name); - const char *name = he.port_name; - he.port_name = NULL; - - sock *s = he.port_sreq; - he.port_sreq = NULL; - - if (name[0]) + if (hrt->name[0]) { /* Transferring the received listening socket to the container */ - int fd = container_ctl_fd(name); + struct cbor_channel *ccc = container_get_channel(hrt->name); - /* TODO: unduplicate this code */ - byte outbuf[128]; - linpool *lp = lp_new(hcs_pool); - struct cbor_writer *cw = cbor_init(outbuf, sizeof outbuf, lp); - cbor_open_block_with_length(cw, 1); - cbor_add_int(cw, -2); - write_item(cw, 7, 22); - struct iovec v = { - .iov_base = outbuf, - .iov_len = cw->pt, - }; - byte cbuf[CMSG_SPACE(sizeof hrt->fd)]; - struct msghdr m = { - .msg_iov = &v, - .msg_iovlen = 1, - .msg_control = &cbuf, - .msg_controllen = sizeof cbuf, - }; - struct cmsghdr *c = CMSG_FIRSTHDR(&m); - c->cmsg_level = SOL_SOCKET; - c->cmsg_type = SCM_RIGHTS; - c->cmsg_len = CMSG_LEN(sizeof hrt->fd); - memcpy(CMSG_DATA(c), &hrt->fd, sizeof hrt->fd); - - int e = sendmsg(fd, &m, 0); - if (e < 0) - log(L_ERR "Failed to send socket: %m"); + CBOR_REPLY(ccc, cw) + CBOR_PUT_MAP(cw) { + cbor_put_int(cw, -2); + cbor_put_null(cw); + ccc->stream->s->txfd = hrt->fd; + } close(hrt->fd); }