Mercurial > hg > Applications > ie-cloud
changeset 2:8de3ca550e8f
fix comment
author | taiki |
---|---|
date | Thu, 12 Feb 2015 10:38:14 +0900 |
parents | f66d7a37dacd |
children | 6ebce132ee89 |
files | Makefile ie-cloud.c ie-cloud.h |
diffstat | 3 files changed, 101 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Wed Feb 11 20:24:43 2015 +0900 +++ b/Makefile Thu Feb 12 10:38:14 2015 +0900 @@ -21,7 +21,7 @@ chmod 755 $(INSTALL_DIR)/portops.py -mkdir $(IEDOCKERDIR) python numberfile.py $(PORTRANGE1) $(PORTRANGE2) - cp iecloudport.list $(IEDOCKERDIR) + -cp iecloudport.list $(IEDOCKERDIR) clean:
--- a/ie-cloud.c Wed Feb 11 20:24:43 2015 +0900 +++ b/ie-cloud.c Thu Feb 12 10:38:14 2015 +0900 @@ -18,7 +18,7 @@ { FILE *fp = NULL; if ((fp = fopen(portlist_file, "r")) == NULL) { - printf("file open error\n"); + printf("[!] file open error\n"); } // file format: portnumber,username,projectname @@ -34,10 +34,12 @@ char *ret = strtok(NULL, ","); if (ret == NULL) continue; user_name_flag = strncmp(user_name, ret, BUFF_SIZE); + printf("user :%s\n", user_name); ret = strtok(NULL, ","); if (ret == NULL) continue; project_name_flag = strncmp(project_name, ret, BUFF_SIZE); + printf("project :%s\n", project_name); if (user_name_flag == 0 && project_name_flag == 0) { printf("port :%s\n", port); @@ -47,11 +49,33 @@ } fclose(fp); if (user_name_flag == 1 || project_name_flag == 1) { - printf("[!] can't get port number for %s", user_name); + printf("[!] can't get port number for %s\n", user_name); exit(1); } } +void +parse_exec_command(const int argc, char **argv, exec_command_opt *opt) +{ + int i = 2; + int parse_ps_flag = 0; + for (i = 2; i < argc; i++) { + if (argv[i][0] == '-') { + if (argv[i][1] == 't') { + opt->tty = TRUE; + } else if (argv[i][1] == 'i') { + opt->interactive = TRUE; + } else if (argv[i][1] == 'd') { + opt->dettach = TRUE; + } + } else if (parse_ps_flag) { + strncpy(opt->exec_ps_command, argv[i], 64); + } else { // image name + parse_ps_flag = 1; + strncpy(opt->ps_name, argv[i], 16); + } + } +} void parse_run_command(const int argc, char **argv, run_command_opt *opt) @@ -76,7 +100,7 @@ strncpy(opt->innerport, argv[i + 1], 16); i++; } - } else if (parse_image_flag) { // image name + } else if (parse_image_flag) { strncpy(opt->exec_ps_command, argv[i], 64); } else { // image name parse_image_flag = 1; @@ -252,7 +276,8 @@ /* main(int argc, char **argv) - main process loop */ -int main(int argc, char **argv) +int +main(int argc, char **argv) { int gid; int uid; @@ -305,29 +330,40 @@ } ps_name[0] = '\0'; - run_command_opt *opt = (run_command_opt *)malloc(sizeof(run_command_opt)); - if (opt == NULL) { + run_command_opt *run_opt = (run_command_opt *)malloc(sizeof(run_command_opt)); + exec_command_opt *exec_opt = (exec_command_opt *)malloc(sizeof(exec_command_opt)); + if (run_opt == NULL && exec_opt == NULL) { printf("[!] malloc error."); + exit(1); } - opt->tty = FALSE; - opt->dettach = FALSE; - opt->interactive = FALSE; + run_opt->tty = FALSE; + run_opt->dettach = FALSE; + run_opt->interactive = FALSE; + + exec_opt->tty = FALSE; + exec_opt->dettach = FALSE; + exec_opt->interactive = FALSE; if (strncmp(argv[1], "ps", 4) != 0) { - if (strncmp(argv[1], "run", 3) == 0) { - parse_run_command(argc, argv, opt); - if (check_name(opt->ps_name)) { + if (strncmp(argv[1], run_command, 3) == 0) { + parse_run_command(argc, argv, run_opt); + if (check_name(run_opt->ps_name)) { fprintf(stderr, bad_name); exit(0); } - get_port_number(name, opt->ps_name, opt->outerport); - strncpy(ps_name, opt->ps_name, 64); - opt->ps_name[0] = '\0'; - make_ps_name(opt->ps_name, account_type, name, ps_name); + get_port_number(name, run_opt->ps_name, run_opt->outerport); + strncpy(ps_name, run_opt->ps_name, 64); + run_opt->ps_name[0] = '\0'; + make_ps_name(run_opt->ps_name, account_type, name, ps_name); + } else if (strncmp(argv[1], exec_command, 4)){ + parse_exec_command(argc, argv, exec_opt); + strncpy(ps_name, exec_opt->ps_name, 64); + make_ps_name(exec_opt->ps_name, account_type, name, ps_name); } else { make_ps_name(ps_name, account_type, name, argv[2]); + free(run_opt); + free(exec_opt); } - printf("process name : %s\n", opt->ps_name); } PSLISTPTR pslist = get_pslist(pattern); @@ -341,27 +377,27 @@ print_pslist(pslist); } else if (strncmp(argv[1], run_command, 5) == 0) { - char *args[16]; + char *run_args[16]; int i = 0; - args[i++] = command; - args[i++] = run_command; - if (opt->dettach) args[i++] = "-d"; - if (opt->tty) args[i++] = "-t"; - if (opt->interactive) args[i++] = "-i"; - args[i++] = "-m"; - args[i++] = "512m"; - args[i++] = "-v"; - args[i++] = opt->volume; - args[i++] = "-p"; + run_args[i++] = command; + run_args[i++] = run_command; + if (run_opt->dettach) run_args[i++] = "-d"; + if (run_opt->tty) run_args[i++] = "-t"; + if (run_opt->interactive) run_args[i++] = "-i"; + run_args[i++] = "-m"; + run_args[i++] = "512m"; + run_args[i++] = "-v"; + run_args[i++] = run_opt->volume; + run_args[i++] = "-p"; char port[32]; - sprintf(port, "%s:%s", opt->outerport, opt->innerport); - args[i++] = port; - args[i++] = "--name"; - args[i++] = opt->ps_name; - args[i++] = opt->image_name; - args[i++] = opt->exec_ps_command; - args[i++] = NULL; + sprintf(port, "%s:%s", run_opt->outerport, run_opt->innerport); + run_args[i++] = port; + run_args[i++] = "--name"; + run_args[i++] = run_opt->ps_name; + run_args[i++] = run_opt->image_name; + run_args[i++] = run_opt->exec_ps_command; + run_args[i++] = NULL; /* printf("run command opt ::memory-%s innerport-%s outerport-%s tty-%d dettach-%d interactive-%d ps_name-%s exec_ps_command-%s volume-%s image-name-%s\n", opt->memory, @@ -376,7 +412,7 @@ opt->image_name); */ - if (execv(args[0], args) < 0) { + if (execv(run_args[0], run_args) < 0) { perror("[!] Execv:"); } } else if (strncmp(argv[1], start_command, 5) == 0) { @@ -384,7 +420,20 @@ perror("[!] Execl:"); } } else if (strncmp(argv[1], exec_command, 5) == 0) { - if (execl(command, command, exec_command, argv[2], argv[3], argv[4], NULL) < 0) { + + int i = 0; + char *exec_args[8]; + + exec_args[i++] = command; + exec_args[i++] = exec_command; + if (exec_opt->dettach) exec_args[i++] = "-d"; + if (exec_opt->tty) exec_args[i++] = "-t"; + if (exec_opt->interactive) exec_args[i++] = "-i"; + exec_args[i++] = exec_opt->ps_name; + exec_args[i++] = exec_opt->exec_ps_command; + exec_args[i++] = NULL; + + if (execv(exec_args[0], exec_args) < 0) { perror("[!] Execl:"); } } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { @@ -407,7 +456,8 @@ usage(); } free(ps_name); - free(opt); + free(exec_opt); + free(run_opt); exit(0); }
--- a/ie-cloud.h Wed Feb 11 20:24:43 2015 +0900 +++ b/ie-cloud.h Thu Feb 12 10:38:14 2015 +0900 @@ -1,5 +1,5 @@ /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ -#ifndef IE_DOCKER +#ifndef IE_CLOUD #define command "/usr/bin/docker" #define ps_command "/usr/bin/docker ps -a" @@ -27,7 +27,7 @@ const char managers_sym[] = "managers"; const char guests_sym[] = "guests"; const char delimiter[] = "_"; -const char portlist_file[] = "/etc/iecloudport.list"; +const char portlist_file[] = "/etc/iecloud/iecloudport.list"; enum { NAME_LENGTH = 50, @@ -66,6 +66,14 @@ TRUE = 1 }; +typedef struct exec_command_opt_t { + int tty; // true = 1; false = 0 + int dettach; // true = 1; false = 0 + int interactive; // true = 1; false = 0 + char ps_name[64]; // user decide name + char exec_ps_command[64]; +} exec_command_opt; + typedef struct run_command_opt_t { char memory[16]; char innerport[PORT_LENGTH]; @@ -74,9 +82,9 @@ int dettach; // true = 1; false = 0 int interactive; // true = 1; false = 0 char ps_name[64]; // user decide name - char exec_ps_command[64]; // + char exec_ps_command[64]; char volume[128]; char image_name[16]; } run_command_opt; -#endif /* IE_DOCKER */ +#endif /* IE_CLOUD */