git: track the specific branch only
[akaros.git] / tests / prov.c
index ef2ee64..8e9cb8e 100644 (file)
@@ -35,89 +35,90 @@ static struct argp_option options[] = {
        { 0 }
 };
 
-#define PROV_MODE_PID                  1
-#define PROV_MODE_CMD                  2
-#define PROV_MODE_SHOW                 3
+#define PROV_MODE_PID          1
+#define PROV_MODE_CMD          2
+#define PROV_MODE_SHOW         3
 
 struct prog_args {
-       char                                            **cmd_argv;
-       int                                                     cmd_argc;
-       int                                                     mode;           /* PROV_MODE_ETC */
-       pid_t                                           pid;
-       char                                            res_type;       /* cores (c), ram (m), etc */
-       char                                            *res_val;       /* type-specific value, unparsed */
-       struct core_set                         cores;
-       bool                                            max;
-       int                                                     dummy_val_flag;
+       char                    **cmd_argv;
+       int                     cmd_argc;
+       int                     mode;           /* PROV_MODE_ETC */
+       pid_t                   pid;
+       char                    res_type;       /* cores (c), ram (m), etc */
+       char                    *res_val; /* type-specific value, unparsed */
+       struct core_set         cores;
+       bool                    max;
+       int                     dummy_val_flag;
 };
 
 static error_t parse_opt (int key, char *arg, struct argp_state *state)
 {
        struct prog_args *pargs = state->input;
        switch (key) {
-               case 't':
-                       pargs->res_type = arg[0];
-                       if (arg[1] != '\0')
-                               printf("Warning, extra letters detected for -t's argument\n");
-                       break;
-               case 'p':
-                       if (pargs->mode) {
-                               printf("Too many modes given (-p, -s, COMMAND, etc)\n\n");
-                               argp_usage(state);
-                       }
-                       pargs->mode = PROV_MODE_PID;
-                       pargs->pid = atoi(arg);
-                       break;
-               case 's':
-                       if (pargs->mode) {
-                               printf("Too many modes given (-p, -s, COMMAND, etc)\n\n");
-                               argp_usage(state);
-                       }
-                       pargs->mode = PROV_MODE_SHOW;
-                       break;
-               case 'c':
-               case ARGP_KEY_ARG:
-                       if (pargs->mode) {
-                               printf("Too many modes given (-p, -s, COMMAND, etc)\n\n");
-                               argp_usage(state);
-                       }
-                       pargs->mode = PROV_MODE_CMD;
+       case 't':
+               pargs->res_type = arg[0];
+               if (arg[1] != '\0')
+                       printf("Warning, extra letters detected for -t's argument\n");
+               break;
+       case 'p':
+               if (pargs->mode) {
+                       printf("Too many modes given (-p, -s, CMD, etc)\n\n");
+                       argp_usage(state);
+               }
+               pargs->mode = PROV_MODE_PID;
+               pargs->pid = atoi(arg);
+               break;
+       case 's':
+               if (pargs->mode) {
+                       printf("Too many modes given (-p, -s, CMD, etc)\n\n");
+                       argp_usage(state);
+               }
+               pargs->mode = PROV_MODE_SHOW;
+               break;
+       case 'c':
+       case ARGP_KEY_ARG:
+               if (pargs->mode) {
+                       printf("Too many modes given (-p, -s, CMD, etc)\n\n");
+                       argp_usage(state);
+               }
+               pargs->mode = PROV_MODE_CMD;
 
-                       pargs->cmd_argc = state->argc - state->next + 1;
-                       pargs->cmd_argv = malloc(sizeof(char*) * (pargs->cmd_argc + 1));
-                       assert(pargs->cmd_argv);
-                       pargs->cmd_argv[0] = arg;
-                       memcpy(&pargs->cmd_argv[1], &state->argv[state->next],
-                              sizeof(char*) * (pargs->cmd_argc - 1));
-                       pargs->cmd_argv[pargs->cmd_argc] = NULL;
-                       state->next = state->argc;
-                       break;
-               case 'v':
-                       /* could also check to make sure we're not -s (and vice versa) */
-                       if (pargs->dummy_val_flag) {
-                               printf("Provide only -v or -m, not both\n\n");
-                               argp_usage(state);
-                       }
-                       pargs->dummy_val_flag = 1;
-                       pargs->res_val = arg;
-                       break;
-               case 'm':
-                       if (pargs->dummy_val_flag) {
-                               printf("Provide only -v or -m, not both\n\n");
-                               argp_usage(state);
-                       }
-                       pargs->dummy_val_flag = 1;
-                       pargs->max = true;
-                       break;
-               case ARGP_KEY_END:
-                       /* Make sure we selected a mode */
-                       if (!pargs->mode) {
-                               printf("No mode selected (-p, -s, etc).\n\n");
-                               argp_usage(state);
-                       }
-                       break;
-               default:
-                       return ARGP_ERR_UNKNOWN;
+               pargs->cmd_argc = state->argc - state->next + 1;
+               pargs->cmd_argv = malloc(sizeof(char*) * (pargs->cmd_argc + 1));
+               assert(pargs->cmd_argv);
+               pargs->cmd_argv[0] = arg;
+               memcpy(&pargs->cmd_argv[1], &state->argv[state->next],
+                      sizeof(char*) * (pargs->cmd_argc - 1));
+               pargs->cmd_argv[pargs->cmd_argc] = NULL;
+               state->next = state->argc;
+               break;
+       case 'v':
+               /* could also check to make sure we're not -s (and vice versa)
+                */
+               if (pargs->dummy_val_flag) {
+                       printf("Provide only -v or -m, not both\n\n");
+                       argp_usage(state);
+               }
+               pargs->dummy_val_flag = 1;
+               pargs->res_val = arg;
+               break;
+       case 'm':
+               if (pargs->dummy_val_flag) {
+                       printf("Provide only -v or -m, not both\n\n");
+                       argp_usage(state);
+               }
+               pargs->dummy_val_flag = 1;
+               pargs->max = true;
+               break;
+       case ARGP_KEY_END:
+               /* Make sure we selected a mode */
+               if (!pargs->mode) {
+                       printf("No mode selected (-p, -s, etc).\n\n");
+                       argp_usage(state);
+               }
+               break;
+       default:
+               return ARGP_ERR_UNKNOWN;
        }
        return 0;
 }
@@ -128,28 +129,29 @@ static struct argp argp = {options, parse_opt, args_doc, doc};
 static int prov_pid(pid_t pid, struct prog_args *pargs)
 {
        switch (pargs->res_type) {
-               case ('c'):
-                       if (pargs->max) {
-                               parlib_get_all_core_set(&pargs->cores);
-                       } else {
-                               if (!pargs->res_val) {
-                                       printf("Need a list of cores to provision\n");
-                                       return -1;
-                               }
-                               parlib_parse_cores(pargs->res_val, &pargs->cores);
+       case ('c'):
+               if (pargs->max) {
+                       parlib_get_all_core_set(&pargs->cores);
+               } else {
+                       if (!pargs->res_val) {
+                               printf("Need a list of cores to provision\n");
+                               return -1;
                        }
-                       provision_core_set(pid, &pargs->cores);
-                       break;
-               case ('m'):
-                       printf("Provisioning memory is not supported yet\n");
-                       return -1;
-                       break;
-               default:
-                       if (!pargs->res_type)
-                               printf("No resource type selected.  Use -t\n");
-                       else
-                               printf("Unsupported resource type %c\n", pargs->res_type);
-                       return -1;
+                       parlib_parse_cores(pargs->res_val, &pargs->cores);
+               }
+               provision_core_set(pid, &pargs->cores);
+               break;
+       case ('m'):
+               printf("Provisioning memory is not supported yet\n");
+               return -1;
+               break;
+       default:
+               if (!pargs->res_type)
+                       printf("No resource type selected.  Use -t\n");
+               else
+                       printf("Unsupported resource type %c\n",
+                              pargs->res_type);
+               return -1;
        }
        return 0;
 }
@@ -162,31 +164,32 @@ int main(int argc, char **argv, char **envp)
        argp_parse(&argp, argc, argv, 0, 0, &pargs);
 
        switch (pargs.mode) {
-               case (PROV_MODE_PID):
-                       return prov_pid(pargs.pid, &pargs);
-                       break;
-               case (PROV_MODE_CMD):
-                       pid = create_child_with_stdfds(pargs.cmd_argv[0], pargs.cmd_argc,
-                                                      pargs.cmd_argv, envp);
-                       if (pid < 0) {
-                               perror("Unable to spawn child");
-                               exit(-1);
-                       }
-                       if (prov_pid(pid, &pargs)) {
-                               perror("Unable to provision to child");
-                               sys_proc_destroy(pid, -1);
-                               exit(-1);
-                       }
-                       sys_proc_run(pid);
-                       waitpid(pid, NULL, 0);
-                       return 0;
-               case (PROV_MODE_SHOW):
-                       printf("Show mode not supported yet, using ghetto interface\n\n");
-                       printf("Check 'dmesg' if you aren't on the console\n\n");
-                       sys_provision(-1, 0, 0);
-                       return 0;
-                       break;
-               default:
-                       return -1;
+       case (PROV_MODE_PID):
+               return prov_pid(pargs.pid, &pargs);
+               break;
+       case (PROV_MODE_CMD):
+               pid = create_child_with_stdfds(pargs.cmd_argv[0],
+                                              pargs.cmd_argc, pargs.cmd_argv,
+                                              envp);
+               if (pid < 0) {
+                       perror("Unable to spawn child");
+                       exit(-1);
+               }
+               if (prov_pid(pid, &pargs)) {
+                       perror("Unable to provision to child");
+                       sys_proc_destroy(pid, -1);
+                       exit(-1);
+               }
+               sys_proc_run(pid);
+               waitpid(pid, NULL, 0);
+               return 0;
+       case (PROV_MODE_SHOW):
+               printf("Show mode not supported, using ghetto interface\n\n");
+               printf("Check 'dmesg' if you aren't on the console\n\n");
+               sys_provision(-1, 0, 0);
+               return 0;
+               break;
+       default:
+               return -1;
        }
 }