Skip to content

Commit a750fa7

Browse files
committed
pthread
1 parent ccb1c4c commit a750fa7

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;
@@ -338,6 +346,54 @@ libkrun_configure_flavor (void *cookie, yajl_val *config_tree, libcrun_error_t *
338346
return 0;
339347
}
340348

349+
void *listen_enclave_output(void *opaque)
350+
{
351+
socklen_t addr_sz = sizeof(struct sockaddr_vm);
352+
struct sockaddr_vm addr;
353+
int ret, sock_fd, cid;
354+
struct timeval timeval;
355+
char buf[BUFSIZE];
356+
357+
cid = (int) opaque;
358+
359+
sock_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
360+
if (sock_fd < 0)
361+
return (void *) -1;
362+
363+
bzero((char *) &addr, sizeof(struct sockaddr_vm));
364+
addr.svm_family = AF_VSOCK;
365+
addr.svm_cid = VMADDR_CID_HYPERVISOR;
366+
addr.svm_port = cid + CID_TO_CONSOLE_PORT_OFFSET;
367+
368+
// Set vsock timeout limit to 5 seconds.
369+
memset(&timeval, 0, sizeof(struct timeval));
370+
timeval.tv_sec = 5;
371+
372+
ret = setsockopt(sock_fd, AF_VSOCK, SO_VM_SOCKETS_CONNECT_TIMEOUT,
373+
(void *) &timeval, sizeof(struct timeval));
374+
if (ret < 0) {
375+
close(sock_fd);
376+
return (void *) -1;
377+
}
378+
379+
ret = connect(sock_fd, (struct sockaddr *) &addr, addr_sz);
380+
if (ret < 0) {
381+
close(sock_fd);
382+
return (void *) -1;
383+
}
384+
385+
bzero(buf, BUFSIZE);
386+
for (;;) {
387+
ret = read(sock_fd, &buf, BUFSIZE);
388+
if (ret < 0)
389+
break;
390+
391+
buf[ret] = '\0';
392+
393+
printf("%s", buf);
394+
}
395+
}
396+
341397
static int
342398
libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname, char *const argv[])
343399
{
@@ -351,11 +407,12 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
351407
struct krun_config *kconf = (struct krun_config *) cookie;
352408
void *handle;
353409
uint32_t num_vcpus, ram_mib;
354-
int32_t ctx_id, ret;
410+
int32_t ctx_id, ret, cid;
355411
cpu_set_t set;
356412
libcrun_error_t err;
357413
bool configured = false;
358414
yajl_val config_tree = NULL;
415+
pthread_t thread;
359416

360417
ret = libkrun_read_vm_config (&config_tree, &err);
361418
if (UNLIKELY (ret < 0))
@@ -458,7 +515,22 @@ libkrun_exec (void *cookie, libcrun_container_t *container, const char *pathname
458515

459516
yajl_tree_free (config_tree);
460517

461-
ret = krun_start_enter (ctx_id);
518+
cid = krun_start_enter (ctx_id);
519+
520+
ret = pthread_create(&thread, NULL, listen_enclave_output, (void *) cid);
521+
if (ret < 0) {
522+
perror("unable to create new listener thread");
523+
exit(1);
524+
}
525+
526+
ret = pthread_join(thread, NULL);
527+
if (ret < 0) {
528+
perror("unable to join listener thread");
529+
exit(1);
530+
}
531+
532+
sleep(1);
533+
462534
return -ret;
463535
}
464536

0 commit comments

Comments
 (0)