From 3503e62449ffe775fa18c360d9da6e32deb6a65f Mon Sep 17 00:00:00 2001 From: Nishi Date: Fri, 11 Oct 2024 21:05:32 +0000 Subject: [PATCH] can take args git-svn-id: file:///raid/svn-personal/rbuild/trunk@13 c68d3453-7f82-0740-9748-1d72386a946b --- Server/server.c | 12 +++++++- Server/task.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/Server/server.c b/Server/server.c index e765c36..c21e354 100644 --- a/Server/server.c +++ b/Server/server.c @@ -184,18 +184,21 @@ void rbs_server_handler(void* sockptr) { break; } else if(strcmp(cmd, "SECTION") == 0 && arg != NULL) { if(strcmp(rbs_config_get(arg, "auth"), "") == 0 || section != NULL) { + rbs_write(sock, "FAIL\n", 5); free(line); break; } section = cm_strdup(arg); } else if(strcmp(cmd, "USER") == 0 && arg != NULL) { if(section == NULL || user != NULL) { + rbs_write(sock, "FAIL\n", 5); free(line); break; } user = cm_strdup(arg); } else if(strcmp(cmd, "PASS") == 0 && arg != NULL) { if(user == NULL || pass != NULL) { + rbs_write(sock, "FAIL\n", 5); free(line); break; } @@ -209,7 +212,9 @@ void rbs_server_handler(void* sockptr) { break; } } else if(strcmp(cmd, "CC") == 0 && arg != NULL && authed) { - if(!rbs_task(sock, section, cmd, arg)) { + if(rbs_task(sock, section, cmd, arg)) { + rbs_write(sock, "SUCCESS\n", 8); + } else { rbs_write(sock, "FAIL\n", 5); free(line); break; @@ -222,6 +227,8 @@ void rbs_server_handler(void* sockptr) { } } + rbs_write(sock, "BYE\n", 4); + rbs_close(sock); #ifdef WINSOCK _endthread(); @@ -249,6 +256,9 @@ CMBOOL rbs_server_loop(void) { if(p == 0) { int* sockptr = malloc(sizeof(*sockptr)); *sockptr = sock; +#ifndef WINSOCK + signal(SIGCHLD, SIG_DFL); +#endif rbs_server_handler(sockptr); _exit(0); } else { diff --git a/Server/task.c b/Server/task.c index 5963445..882bc5d 100644 --- a/Server/task.c +++ b/Server/task.c @@ -5,9 +5,16 @@ #include "rbs_server.h" #include "rbs_config.h" +#include +#include #include #include +#ifdef __MINGW32__ +#else +#include +#endif + #include #include @@ -31,19 +38,31 @@ void rbs_stack_free(char** stack) { free(stack); } -char** rbs_parse_args(const char* arg) { +char** rbs_parse_args(const char* cmd, const char* arg) { char* str = cm_strdup(arg); char** stack = malloc(sizeof(*stack)); int i; int incr = 0; stack[0] = NULL; + rbs_push(&stack, cmd); for(i = 0;; i++) { if(str[i] == 0 || str[i] == ' ') { char oldc = str[i]; char* got = str + incr; str[i] = 0; - rbs_push(&stack, got); + if(strlen(got) > 2 && got[0] == '-' && got[1] == 'o') { + rbs_push(&stack, "-o"); + rbs_push(&stack, got + 2); + } else if(strlen(got) > 2 && got[0] == '-' && got[1] == 'L') { + rbs_push(&stack, "-L"); + rbs_push(&stack, got + 2); + } else if(strlen(got) > 2 && got[0] == '-' && got[1] == 'I') { + rbs_push(&stack, "-I"); + rbs_push(&stack, got + 2); + } else { + rbs_push(&stack, got); + } incr = i + 1; if(oldc == 0) break; @@ -56,8 +75,56 @@ char** rbs_parse_args(const char* arg) { return stack; } +#ifdef __MINGW32__ +#else +int outpipe[2]; +pid_t pid; + +CMBOOL rbs_start_process(const char* cmd, char** args) { + pipe(outpipe); + pid = fork(); + if(pid == 0) { + dup2(outpipe[1], 1); + dup2(outpipe[1], 2); + close(outpipe[0]); + execvp(cmd, args); + _exit(-1); + } else if(pid == -1) { + return CMFALSE; + } + close(outpipe[1]); + return CMTRUE; +} + +CMBOOL rbs_wait_process(int sock) { + int status; + unsigned char c = 0; + rbs_write(sock, "+", 1); + while(1) { + char oldc = c; + int len = read(outpipe[0], &c, 1); + if(len <= 0) break; + if(c == '\n') { + rbs_write(sock, "\n", 1); + } else { + if(oldc == '\n') rbs_write(sock, "+", 1); + rbs_write(sock, &c, 1); + } + } + if(c != '\n') { + rbs_write(sock, "\n", 1); + } + waitpid(pid, &status, 0); + close(outpipe[0]); + return WEXITSTATUS(status) == 0; +} +#endif + CMBOOL rbs_task(int sock, const char* section, const char* cmd, const char* arg) { - char** args = rbs_parse_args(arg); + char** args = rbs_parse_args("gcc", arg); + CMBOOL status = rbs_start_process("gcc", args); rbs_stack_free(args); - return CMFALSE; + if(!status) return CMFALSE; + rbs_write(sock, "SUCCESS\n", 8); + return rbs_wait_process(sock); } -- 2.43.0