changeset 19:fe7095c365cf

WIP: Cleanup source code
author atton
date Tue, 24 Nov 2015 17:22:12 +0900
parents 8efb8b39ae0c
children c908e2c7315d
files ie-docker.c ie-docker.h
diffstat 2 files changed, 42 insertions(+), 153 deletions(-) [+]
line wrap: on
line diff
--- a/ie-docker.c	Tue Nov 24 16:59:54 2015 +0900
+++ b/ie-docker.c	Tue Nov 24 17:22:12 2015 +0900
@@ -230,6 +230,12 @@
     printf("\tie-docker [option] --name [ps_name] {image name}:{tag} [execute command] [argument]");
 }
 
+int
+command_is_matched(const char const * arg, const char const * command)
+{
+    return strcmp(arg, command) == 0;
+}
+
 /* main(int argc, char **argv) - main process loop */
 
 int
@@ -247,6 +253,11 @@
     setegid(getgid());
     seteuid(getuid());
 
+    regex_t *pattern = NEW(regex_t);
+    if (regcomp(pattern, name, 0) != 0) {
+        exit(0);
+    }
+
     /* Confirm user is in GROUP(999) group */
 
     /*
@@ -258,72 +269,16 @@
 
     /* Set uid, gid, euid and egid to root */
 
-    regex_t *pattern = NEW(regex_t);
-    if (regcomp(pattern, name, 0) != 0) {
-        exit(0);
-    }
-
     setegid(0);
     seteuid(0);
     setgid(0);
     setuid(0);
 
-
-    char *ps_name = (char *)malloc(sizeof(char) * PS_NAME_LENGTH);
-    if (ps_name == NULL) {
-        printf("[!] malloc error.");
-    }
-    ps_name[0] = '\0';
-
-    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);
-    }
-    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 (argc < 2) {
         usage();
         exit(0);
     }
 
-    if (strncmp(argv[1], "ps", 4) != 0) {
-        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, 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) == 0){
-            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 if (strncmp(argv[1], rm_command, 4) == 0) {
-            char exec[512];
-            sprintf(exec, "/usr/local/bin/remove.py %s", argv[2]);
-            system(exec);
-
-            //make_ps_name(ps_name, account_type, name, argv[2]);
-            free(run_opt);
-            free(exec_opt);
-        } else {
-            //make_ps_name(ps_name, account_type, name, argv[2]);
-            free(run_opt);
-            free(exec_opt);
-        }
-    }
-
     PSLISTPTR pslist = get_pslist(pattern);
 
     /*
@@ -331,93 +286,26 @@
      * the corresponding script, if invoked.
      */
 
-    if (argv[1]==0 || strncmp(argv[1], "ps", 4) == 0 ) {
+    if (command_is_matched(argv[1], ps_command)) {
+        printf("ps"); //TODO
         print_pslist(pslist);
-    } else if (strncmp(argv[1], run_command, 5) == 0) {
-
-        char *run_args[17];
-        int i = 0;
-
-        run_args[i++] = command;
-        run_args[i++] = run_command;
-        run_args[i++] = "--privileged";
-        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", 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,
-            opt->innerport,
-            opt->outerport,
-            opt->tty,
-            opt->dettach,
-            opt->interactive,
-            opt->ps_name,
-            opt->exec_ps_command,
-            opt->volume,
-            opt->image_name);
-
-*/
-        if (execv(run_args[0], run_args) < 0) {
-            perror("[!] Execv:");
-        }
-    } else if (strncmp(argv[1], start_command, 5) == 0) {
-        if (execl(command, command, start_command, ps_name,  NULL) < 0) {
-            perror("[!] Execl:");
-        }
-    } else if (strncmp(argv[1], exec_command, 5) == 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 ) {
-        if (execl(command, command, stop_command, ps_name, NULL) < 0) {
-            perror("[!] Execl:");
-        }
-    } else if ( strncmp(argv[1], build_command, 8) == 0 ) {
-        if (execl(command, command, build_command, argv[2], NULL) < 0) {
-            perror("[!] Execl:");
-        }
-    } else if (strncmp(argv[1], attach_command, 6) == 0 ) {
-        if (execl(command, command, attach_command, ps_name, NULL) < 0) {
-            perror("[!] Execl:");
-        }
-    } else if ( strncmp(argv[1], rm_command, 2) == 0 ) {
-        if (execl(command, command, rm_command, ps_name, NULL) < 0) {
-            perror("[!] Execl:");
-        }
+    } else if (command_is_matched(argv[1], run_command)) {
+        printf("run"); // TODO
+    } else if (command_is_matched(argv[1], start_command)) {
+        printf("start"); // TODO
+    } else if (command_is_matched(argv[1], stop_command)) {
+        printf("stop"); // TODO
+    } else if (command_is_matched(argv[1], attach_command)) {
+        printf("attach"); // TODO
+    } else if (command_is_matched(argv[1], rm_command)) {
+        printf("rm"); // TODO
+    } else if (command_is_matched(argv[1], cp_command)) {
+        printf("cp"); // TODO
     } else {
         usage();
+        return 0;
     }
-    free(ps_name);
-    free(exec_opt);
-    free(run_opt);
-    exit(0);
+    return 0;
 }
 
 /* end */
--- a/ie-docker.h	Tue Nov 24 16:59:54 2015 +0900
+++ b/ie-docker.h	Tue Nov 24 17:22:12 2015 +0900
@@ -1,26 +1,27 @@
 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
 #ifndef IE_DOCKER
 
-#define command "/usr/bin/docker"
-#define ps_command "/usr/bin/docker ps -a"
+#define base_exec_command "/usr/bin/docker"
+#define ps_exec_command   "/usr/bin/docker ps -a"
+
 #define run_command "run"
-#define build_command "build"
+#define build_command ""
 #define attach_command "attach"
 // #define pull_command "pull" /* download docker image command */
-#define images_command "images" /* list images command */
-#define commit_command "commit" /* make image command */
+#define images_command "" /* list images command */
+#define commit_command "" /* make image command */
 #define rm_command "rm" /* remove container command */
 #define rmi_command "rmi" /* remove image command */
-#define start_command "start" 
-#define stop_command "stop" 
+#define start_command "start"
+#define stop_command "stop"
 #define exec_command "exec"
+#define cp_command "cp"
+#define ps_command "ps"
 
-#define create_command "create"
+#define create_command ""
 
 /* Define global variables */
 
-static char bad_name[] = "Bad process name. Try students_e11_e115711_01 or teachers_kono_02\n";
-
 const char *guests[] = {"mata"};
 const char *managers[] = {"taira"};
 const char students_sym[] = "students";
@@ -29,7 +30,7 @@
 const char delimiter[] = "_";
 const char portlist_file[] = "/etc/iedocker/iedockerport.list";
 
-enum { 
+enum {
     NAME_LENGTH = 50,
     PS_NAME_LENGTH = 50,
     RUN_COMMAND_LENGTH = 1024,
@@ -37,7 +38,7 @@
     BUFF_SIZE = 50
 };
 
-enum { 
+enum {
     STUDENTS,
     GUESTS,
     MANAGERS
@@ -58,7 +59,7 @@
  * -v volume
  * -m memory
  * image name
- * -i skeep open tdin 
+ * -i skeep open tdin
  */
 
 enum {
@@ -82,7 +83,7 @@
     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;