diff --git a/.gitignore b/.gitignore index 5f55502..7947d06 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,9 @@ /starter/alpine-make-rootfs/ /run /starter/shared -/starter/starter +/starter/starter.so /starter/minrootfs +/transport/transport /pkg/go/gen /gen /runs diff --git a/Makefile b/Makefile index 619d406..6f3a04e 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,11 @@ .PHONY: all clean install uninstall test PROTOBUF=/usr/include/ -STARTER=starter GEN=gen -SRCS=$(STARTER)/cgroup_prepare.c $(STARTER)/ns_exec.c $(STARTER)/util.c $(STARTER)/starter.c RUNS=runs PROTO_OUT=$(GEN)/google/protobuf/empty.pb-c.c $(GEN)/google/protobuf/empty.pb-c.h $(GEN)/google/protobuf/timestamp.pb-c.c $(GEN)/google/protobuf/timestamp.pb-c.h $(GEN)/google/protobuf/duration.pb-c.c $(GEN)/google/protobuf/duration.pb-c.h gen/runner/v1/runner.pb-c.c gen/runner/v1/runner.pb-c.c -all: $(STARTER)/alpine-make-rootfs $(STARTER)/minrootfs $(STARTER)/starter transport/transport $(RUNS) +all: starter/alpine-make-rootfs starter/minrootfs starter/starter.so transport/transport $(RUNS) $(GEN)/google/protobuf/empty.pb-c.c $(GEN)/google/protobuf/empty.pb-c.h $(GEN)/google/protobuf/timestamp.pb-c.c $(GEN)/google/protobuf/timestamp.pb-c.h $(GEN)/google/protobuf/duration.pb-c.c $(GEN)/google/protobuf/duration.pb-c.h $(GEN)/runner/v1/runner.pb-c.c $(GEN)/runner/v1/runner.pb-c.h $(GEN): mkdir -p $(GEN) @@ -21,23 +19,21 @@ $(GEN)/google/protobuf/timestamp.pb-c.c $(GEN)/google/protobuf/timestamp.pb-c.h: $(GEN)/google/protobuf/duration.pb-c.c $(GEN)/google/protobuf/duration.pb-c.h: $(PROTOBUF)/google/protobuf/duration.proto $(GEN) protoc --c_out=$(GEN) google/protobuf/duration.proto -I $(PROTOBUF) -gen/runner/v1/runner.pb-c.c gen/runner/v1/runner.pb-c.h: proto/runner/v1/runner.proto $(GEN) +$(GEN)/runner/v1/runner.pb-c.c $(GEN)/runner/v1/runner.pb-c.h: proto/runner/v1/runner.proto $(GEN) protoc --c_out=$(GEN) runner/v1/runner.proto -I proto -$(STARTER)/alpine-make-rootfs: +starter/alpine-make-rootfs: git clone https://github.com/alpinelinux/alpine-make-rootfs starter/alpine-make-rootfs -$(STARTER)/minrootfs: $(STARTER)/create_rootfs.sh - $(STARTER)/create_rootfs.sh +starter/minrootfs: starter/create_rootfs.sh + starter/create_rootfs.sh -$(STARTER)/starter: $(PROTO) $(STARTER)/starter.c $(STARTER)/cgroup_prepare.c $(STARTER)/ns_exec.c Makefile - #$(CC) -shared -o $(STARTER)/starter $(SRCS) -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer -lrabbitmq -lprotobuf-c -I gen - $(CC) -fPIC -shared -o $(STARTER)/starter $(SRCS) -lrabbitmq -lprotobuf-c -I gen - #$(CC) $(STARTER)/starter.c -o $(STARTER)/starter +starter/starter.so: $(PROTO) starter/starter.c starter/cgroup_prepare.c starter/ns_exec.c starter/util.c + $(CC) -fPIC -shared -o starter/starter.so $^ -lrabbitmq -lprotobuf-c -I gen -transport/transport: $(PROTO) transport/utils.c transport/transport.c transport/transport.h - $(CC) -o $@ gen/runner/v1/runner.pb-c.c transport/utils.c transport/transport.c -I . -I gen -lrabbitmq -lprotobuf-c -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer +transport/transport: $(PROTO) transport/utils.c transport/transport.c transport/transport.h starter/starter.so + $(CC) -o $@ gen/runner/v1/runner.pb-c.c transport/utils.c transport/transport.c starter/starter.so -I . -I starter -I gen -lrabbitmq -lprotobuf-c -lcurl -fsanitize=address -g $(RUNS): mkdir $(RUNS) diff --git a/proto b/proto index 360832a..1138a54 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 360832a5ab10821a76b7df5e23950e217f2c5221 +Subproject commit 1138a5406c554ec29059da2da2a16eb001cc7b36 diff --git a/starter/ns_exec.c b/starter/ns_exec.c index 5e2008c..df56da2 100644 --- a/starter/ns_exec.c +++ b/starter/ns_exec.c @@ -9,14 +9,12 @@ void procfs_prepare() #define hostname "runner" #define put_old "oldfs" -void mnt_prepare(char* rootfs, char* shared_mountpoint) { - char* shared_folder = NULL; - asprintf(&shared_folder, "../%s", shared_mountpoint); +void mnt_prepare(char* rootfs, char* shared_folder) { if (mount(rootfs, rootfs, "ext4", MS_BIND,"")) die("failed to mount %s: %m", rootfs); if (chdir(rootfs)) die("falied to cd:%m"); //if (mount("/sys","sys","sysfs",0,"")) die("failed to mount sysfs: %m"); //if (mount("/dev","dev","udev",0,"")) die("failed to mount: %m"); - if (mkdir("shared", 0777) && errno != EEXIST) die("Failed to mkdir %s: %m\n", shared_mountpoint); + if (mkdir("shared", 0777) && errno != EEXIST) die("Failed to mkdir: %m\n"); if (mount(shared_folder, "shared", "ext4", MS_BIND, "")) die("failed to mount shared folder: %m"); if (mkdir(put_old, 0000) && errno != EEXIST) die("Failed to mkdir %s: %m\n", put_old); if (syscall(SYS_pivot_root, ".", put_old)) die("Failed to pivot_root from %s to %s: %m\n", rootfs, put_old); @@ -24,7 +22,6 @@ void mnt_prepare(char* rootfs, char* shared_mountpoint) { procfs_prepare(); if (umount2(put_old, MNT_DETACH)) die("Failed to umount put_old %s: %m\n", put_old); if (rmdir(put_old)) die("Failed to rmdir: %m"); - free(shared_folder); } void ro_fs(char* shared_mountpoint) { char* shared_folder = malloc(strlen(shared_mountpoint)+4); @@ -69,7 +66,7 @@ int nsrun(void* arg) { char* env[]={ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "PWD=/", - NULL, + (char*)0, }; if (execve(cmd, argv, env) == -1) die("failed to exec,%m"); return 1;// removes warning diff --git a/starter/starter.c b/starter/starter.c index 771fa14..bd54b2e 100644 --- a/starter/starter.c +++ b/starter/starter.c @@ -48,13 +48,12 @@ static void prepare_userns(int pid) { write_file(path, line); } -int starter(char* working_path, struct limits limits, struct params params) { +int starter(char* shared_folder, char* minrootfs_path, struct limits limits, struct params params) { + puts("starter started"); if(setuid(0)) die("must be run as root"); if(setgid(0)) die("must be run as root"); // get binary path - char* real_path=working_path; - //get_real_path(real_path, argv[0]); - if(chdir(real_path)) die("unable to chdir to binary path: %m"); + if(chdir(minrootfs_path)) die("unable to chdir: %m"); // set random seed srand(time(NULL)); // setup parameters diff --git a/starter/starter.h b/starter/starter.h index 953d613..acbf732 100644 --- a/starter/starter.h +++ b/starter/starter.h @@ -51,3 +51,4 @@ extern void await_setup(int pipe); extern int nsrun(void* arg); extern void die_func(const char *fmt, ...); extern void write_file(char* path, char* line); +extern int starter(char* shared_folder, char* minrootfs_path, struct limits limits, struct params params); diff --git a/tests/transport_test b/tests/transport_test deleted file mode 100755 index 1e558e6..0000000 Binary files a/tests/transport_test and /dev/null differ diff --git a/tests/transport_test.c b/tests/transport_test.c index b8f73f3..7e52c2c 100644 --- a/tests/transport_test.c +++ b/tests/transport_test.c @@ -51,12 +51,13 @@ void send_to_rbmq(struct connection_data condata, char const* msg) amqp_basic_publish(condata.conn, 1, amqp_cstring_bytes(condata.exchange), amqp_cstring_bytes(condata.routingkey), 0, 0, &props, amqp_cstring_bytes(msg)), "Publishing"; } -void build_serialize(int32_t solution_id, char* binding_key, int32_t language, char* solution, void** buf) { +void build_serialize(int32_t solution_id, char* binding_key, int32_t language, char* solution_uri, char* executable_uri, void** buf) { struct Runner__V1__Build build = RUNNER__V1__BUILD__INIT; - build.solution_id = solution_id; - build.binding_key = binding_key; - build.language = language; - build.solution = solution; + build.solution_id = solution_id; + build.binding_key = binding_key; + build.language = language; + build.solution_uri = solution_uri; + build.executable_uri = executable_uri; struct Runner__V1__Instruction inst = RUNNER__V1__INSTRUCTION__INIT; inst.instruction_case = RUNNER__V1__INSTRUCTION__INSTRUCTION_BUILD; @@ -91,11 +92,14 @@ int main(int argc, char const *const *argv) { void *messagebody; char* binding_key; asprintf(&binding_key,"bktest"); - char* solution; - asprintf(&solution,"soltest"); - build_serialize(0,binding_key,1,solution,&messagebody); + char* solution_uri; + asprintf(&solution_uri,"http://localhost/code.cpp"); + char* executable_uri; + asprintf(&executable_uri,"http://localhost/executable"); + build_serialize(0,binding_key,1,solution_uri, executable_uri, &messagebody); free(binding_key); - free(solution); + free(solution_uri); + free(executable_uri); send_to_rbmq(condata,messagebody); free(messagebody); diff --git a/transport/transport b/transport/transport deleted file mode 100755 index 2f424e2..0000000 Binary files a/transport/transport and /dev/null differ diff --git a/transport/transport.c b/transport/transport.c index 680607d..65c8844 100644 --- a/transport/transport.c +++ b/transport/transport.c @@ -1,5 +1,6 @@ #include "transport/transport.h" #include "starter/starter.h" +#include "curl/curl.h" void prepare_amqp_connection(int argc, char const *const *argv,struct connection_data *condata) { int status; @@ -45,7 +46,27 @@ void prepare_amqp_connection(int argc, char const *const *argv,struct connection amqp_assert(amqp_get_rpc_reply(condata->conn)); } +void download_file(char* uri, char* path) { + FILE* f = fopen(path, "w"); + //fwrite("test", 4,1,f); + CURL *handle; + handle = curl_easy_init(); + curl_easy_setopt(handle, CURLOPT_URL, uri); + curl_easy_setopt(handle, CURLOPT_WRITEDATA, f); + CURLcode code = curl_easy_perform(handle); + if(code==CURLE_OK) { + int response_code; + curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response_code); + printf("response code:%d", response_code); + } else {puts("not success");} + sleep(1); + //puts(res); + curl_easy_cleanup(handle); + fclose(f); +} + int main(int argc, char const *const *argv) { + curl_global_init (CURL_GLOBAL_ALL); struct connection_data condata; prepare_amqp_connection(argc,argv,&condata); @@ -70,7 +91,20 @@ int main(int argc, char const *const *argv) { switch(inst->instruction_case) { case RUNNER__V1__INSTRUCTION__INSTRUCTION_BUILD: puts("build request recieved"); - printf("solution_id: %d\nbinding_key:%s\nlanguage:%d\nsolution:%s\n\n",inst->build->solution_id,inst->build->binding_key,inst->build->language,inst->build->solution); + printf("solution_id: %d\nbinding_key:%s\nlanguage:%d\nsolution uri:%s\nexecutable uri: %s\n\n",inst->build->solution_id,inst->build->binding_key,inst->build->language,inst->build->solution_uri, inst->build->executable_uri); + download_file(inst->build->solution_uri, "/home/dragonmuffin/ms-runner/runs/solution"); + //struct limits limits; + //limits.memory = 10000000; + //limits.core = 2; + //limits.time = 3000; + //limits.processes = 1; + + //struct params params; + //params.shared_folder = "/home/dragonmuffin/ms-runner/runs/"; + //params.argv = (char*[]){"/bin/ls", "/", NULL}; + + //starter("/home/dragonmuffin/ms-runner/runs", "/home/dragonmuffin/ms-runner/starter", limits, params); // fix this + break; case RUNNER__V1__INSTRUCTION__INSTRUCTION_RUN: puts("run request recieved");