Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 1cf7b55

Browse files
committed
add hyperstart-exec
The target process is directly execve()-ed by the hyperstart process in the namespaces of the hyperstart rather than in any container. So that we can use this special api for misc operations rather than add new hyperstart commands. We may use this hyperstart-exec operation to replace the existed some misc-style hyperstart commands in the future. Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
1 parent 793064d commit 1cf7b55

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ enum {
3232
PROCESSASYNCEVENT,
3333
};
3434

35+
// "hyperstart" is the special container ID for adding processes.
36+
//
37+
// The processes will be execve()-ed in the same namespaces as the
38+
// hyperstart(init) process rather than in any container.
39+
// This operation is "hyperstart-exec"
40+
#define HYPERSTART_EXEC_CONTAINER "hyperstart"
41+
3542
/*
3643
* control message format
3744
* | ctrl id | length | payload (length-8) |

src/container.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _CONTAINER_H_
33

44
#include "exec.h"
5+
#include "api.h"
56

67
struct volume {
78
char *device;
@@ -61,4 +62,7 @@ void hyper_cleanup_container(struct hyper_container *container, struct hyper_pod
6162
void hyper_cleanup_containers(struct hyper_pod *pod);
6263
void hyper_free_container(struct hyper_container *c);
6364

65+
static inline int hyper_has_container(struct hyper_pod *pod, const char *id) {
66+
return strcmp(id, HYPERSTART_EXEC_CONTAINER) == 0 || hyper_find_container(pod, id) != NULL;
67+
}
6468
#endif

src/exec.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ int hyper_exec_cmd(struct hyper_pod *pod, char *json, int length)
599599
return -1;
600600
}
601601

602-
if (hyper_find_container(pod, exec->container_id) == NULL) {
602+
if (!hyper_has_container(pod, exec->container_id)) {
603603
fprintf(stderr, "call hyper_exec_cmd, no such container: %s\n", exec->container_id);
604604
hyper_free_exec(exec);
605605
return -1;
@@ -646,6 +646,11 @@ int hyper_run_process(struct hyper_exec *exec)
646646
perror("fork prerequisite process failed");
647647
goto close_tty;
648648
} else if (pid == 0) {
649+
if (strcmp(exec->container_id, HYPERSTART_EXEC_CONTAINER) == 0) {
650+
hyper_send_type(pipe[1], getpid());
651+
hyper_exec_process(exec, &io);
652+
_exit(125);
653+
}
649654
hyper_do_exec_cmd(exec, pipe[1], &io);
650655
}
651656
fprintf(stdout, "prerequisite process pid %d\n", pid);

src/init.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,22 @@ static int hyper_setup_shared(struct hyper_pod *pod)
472472
}
473473
#endif
474474

475+
static int hyper_setup_virtual_hyperstart_exec_container(struct hyper_pod *pod)
476+
{
477+
if (hyper_mkdir("/tmp/hyper/" HYPERSTART_EXEC_CONTAINER, 0755) < 0) {
478+
perror("create virtual hyperstart-exec container directory failed");
479+
return -1;
480+
}
481+
482+
// for creating ptymaster when adding process with terminal=true
483+
if (symlink("/dev/pts", "/tmp/hyper/" HYPERSTART_EXEC_CONTAINER "/devpts") < 0) {
484+
perror("create virtual hyperstart-exec container's /dev symlink failed");
485+
return -1;
486+
}
487+
488+
return 0;
489+
}
490+
475491
static int hyper_setup_pod(struct hyper_pod *pod)
476492
{
477493
/* create sandbox directory */
@@ -505,6 +521,10 @@ static int hyper_setup_pod(struct hyper_pod *pod)
505521
return -1;
506522
}
507523

524+
if (hyper_setup_virtual_hyperstart_exec_container(pod) < 0) {
525+
return -1;
526+
}
527+
508528
return 0;
509529
}
510530

@@ -599,7 +619,7 @@ static int hyper_new_container(struct hyper_pod *pod, char *json, int length)
599619
return -1;
600620
}
601621

602-
if (hyper_find_container(pod, c->id) != NULL) {
622+
if (hyper_has_container(pod, c->id)) {
603623
fprintf(stderr, "container id conflicts");
604624
hyper_cleanup_container(c, pod);
605625
return -1;

0 commit comments

Comments
 (0)