Skip to content

Commit 8ccfcb5

Browse files
committed
pthread
1 parent 4ae67ac commit 8ccfcb5

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

src/libcrun/handlers/krun.c

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
#include <sys/stat.h>
2727
#include <errno.h>
2828
#include <sys/param.h>
29+
#include <sys/time.h>
2930
#include <sys/types.h>
3031
#include <sys/sysmacros.h>
3132
#include <fcntl.h>
33+
#include <pthread.h>
3234
#include <sched.h>
3335
#include <ocispec/runtime_spec_schema_config_schema.h>
36+
#include <linux/vm_sockets.h>
3437

3538
#ifdef HAVE_DLOPEN
3639
# include <dlfcn.h>
@@ -62,6 +65,11 @@
6265
#define KRUN_FLAVOR_NITRO "aws-nitro"
6366
#define KRUN_FLAVOR_SEV "sev"
6467

68+
#define VMADDR_CID_HYPERVISOR 0
69+
#define CID_TO_CONSOLE_PORT_OFFSET 10000
70+
71+
#define BUFSIZE 512
72+
6573
struct krun_config
6674
{
6775
void *handle;
@@ -353,6 +361,54 @@ libkrun_configure_flavor (void *cookie, yajl_val *config_tree, libcrun_error_t *
353361
return 0;
354362
}
355363

364+
void *listen_enclave_output(void *opaque)
365+
{
366+
socklen_t addr_sz = sizeof(struct sockaddr_vm);
367+
struct sockaddr_vm addr;
368+
int ret, sock_fd, cid;
369+
struct timeval timeval;
370+
char buf[BUFSIZE];
371+
372+
cid = (int) opaque;
373+
374+
sock_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
375+
if (sock_fd < 0)
376+
return (void *) -1;
377+
378+
bzero((char *) &addr, sizeof(struct sockaddr_vm));
379+
addr.svm_family = AF_VSOCK;
380+
addr.svm_cid = VMADDR_CID_HYPERVISOR;
381+
addr.svm_port = cid + CID_TO_CONSOLE_PORT_OFFSET;
382+
383+
// Set vsock timeout limit to 5 seconds.
384+
memset(&timeval, 0, sizeof(struct timeval));
385+
timeval.tv_sec = 5;
386+
387+
ret = setsockopt(sock_fd, AF_VSOCK, SO_VM_SOCKETS_CONNECT_TIMEOUT,
388+
(void *) &timeval, sizeof(struct timeval));
389+
if (ret < 0) {
390+
close(sock_fd);
391+
return (void *) -1;
392+
}
393+
394+
ret = connect(sock_fd, (struct sockaddr *) &addr, addr_sz);
395+
if (ret < 0) {
396+
close(sock_fd);
397+
return (void *) -1;
398+
}
399+
400+
bzero(buf, BUFSIZE);
401+
for (;;) {
402+
ret = read(sock_fd, &buf, BUFSIZE);
403+
if (ret <= 0)
404+
break;
405+
406+
buf[ret] = '\0';
407+
408+
printf("%s", buf);
409+
}
410+
}
411+
356412
static int
357413
libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname, char *const argv[])
358414
{
@@ -366,11 +422,12 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
366422
struct krun_config *kconf = (struct krun_config *) cookie;
367423
void *handle;
368424
uint32_t num_vcpus, ram_mib;
369-
int32_t ctx_id, ret;
425+
int32_t ctx_id, ret, cid;
370426
cpu_set_t set;
371427
libcrun_error_t err;
372428
bool configured = false;
373429
yajl_val config_tree = NULL;
430+
pthread_t thread;
374431

375432
ret = libkrun_read_vm_config (&config_tree, &err);
376433
if (UNLIKELY (ret < 0))
@@ -473,7 +530,22 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
473530

474531
yajl_tree_free (config_tree);
475532

476-
ret = krun_start_enter (ctx_id);
533+
cid = krun_start_enter (ctx_id);
534+
535+
ret = pthread_create(&thread, NULL, listen_enclave_output, (void *) cid);
536+
if (ret < 0) {
537+
perror("unable to create new listener thread");
538+
exit(1);
539+
}
540+
541+
ret = pthread_join(thread, NULL);
542+
if (ret < 0) {
543+
perror("unable to join listener thread");
544+
exit(1);
545+
}
546+
547+
sleep(1);
548+
477549
return -ret;
478550
}
479551

0 commit comments

Comments
 (0)